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) Coverage Total Hit
Test: PARI/GP v2.18.1 lcov report (development 31041-bd73e9fcdd) Lines: 91.8 % 1852 1701
Test Date: 2026-07-22 22:45:42 Functions: 92.0 % 187 172
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Copyright (C) 2000  The PARI group.
       2              : 
       3              : This file is part of the PARI/GP package.
       4              : 
       5              : PARI/GP is free software; you can redistribute it and/or modify it under the
       6              : terms of the GNU General Public License as published by the Free Software
       7              : Foundation; either version 2 of the License, or (at your option) any later
       8              : version. It is distributed in the hope that it will be useful, but WITHOUT
       9              : ANY WARRANTY WHATSOEVER.
      10              : 
      11              : Check the License for details. You should have received a copy of it, along
      12              : with the package; see the file 'COPYING'. If not, write to the Free Software
      13              : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14              : 
      15              : /*******************************************************************/
      16              : /*                                                                 */
      17              : /*                       BASIC NF OPERATIONS                       */
      18              : /*                           (continued)                           */
      19              : /*                                                                 */
      20              : /*******************************************************************/
      21              : #include "pari.h"
      22              : #include "paripriv.h"
      23              : 
      24              : #define DEBUGLEVEL DEBUGLEVEL_nf
      25              : 
      26              : /*******************************************************************/
      27              : /*                                                                 */
      28              : /*                     IDEAL OPERATIONS                            */
      29              : /*                                                                 */
      30              : /*******************************************************************/
      31              : 
      32              : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
      33              :  * on the integer basis in HNF.
      34              :  * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
      35              :  * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
      36              :  * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
      37              :  *
      38              :  * An extended ideal is a couple [I,F] where I is an ideal and F is either an
      39              :  * algebraic number, or a factorization matrix attached to an algebraic number.
      40              :  * All routines work with either extended ideals or ideals (an omitted F is
      41              :  * assumed to be factor(1)). All ideals are output in HNF form. */
      42              : 
      43              : /* types and conversions */
      44              : 
      45              : long
      46     16552244 : idealtyp(GEN *ideal, GEN *arch)
      47              : {
      48     16552244 :   GEN x = *ideal;
      49     16552244 :   long t,lx,tx = typ(x);
      50              : 
      51     16552244 :   if (tx!=t_VEC || lg(x)!=3) { if (arch) *arch = NULL; }
      52              :   else
      53              :   {
      54      1240396 :     GEN a = gel(x,2);
      55      1240396 :     if (typ(a) == t_MAT && lg(a) != 3)
      56              :     { /* allow [;] */
      57           14 :       if (lg(a) != 1) pari_err_TYPE("idealtyp [extended ideal]",x);
      58            7 :       if (arch) *arch = trivial_fact();
      59              :     }
      60              :     else
      61      1240382 :       if (arch) *arch = a;
      62      1240389 :     x = gel(x,1); tx = typ(x);
      63              :   }
      64     16552237 :   switch(tx)
      65              :   {
      66     12539442 :     case t_MAT: lx = lg(x);
      67     12539442 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      68     12539267 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [nonsquare t_MAT]",x);
      69     12539239 :       t = id_MAT;
      70     12539239 :       break;
      71              : 
      72      3050761 :     case t_VEC:
      73      3050761 :       if (!checkprid_i(x)) pari_err_TYPE("idealtyp [fake prime ideal]",x);
      74      3050713 :       t = id_PRIME; break;
      75              : 
      76       962396 :     case t_POL: case t_POLMOD: case t_COL:
      77              :     case t_INT: case t_FRAC:
      78       962396 :       t = id_PRINCIPAL; break;
      79            6 :     default:
      80            6 :       pari_err_TYPE("idealtyp",x);
      81              :       return 0; /*LCOV_EXCL_LINE*/
      82              :   }
      83     16552523 :   *ideal = x; return t;
      84              : }
      85              : 
      86              : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      87              : GEN
      88       803500 : idealhnf_two(GEN nf, GEN v)
      89              : {
      90       803500 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      91       803501 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      92       730120 :   return ZM_hnfmodid(m, p);
      93              : }
      94              : /* true nf */
      95              : GEN
      96      3802931 : pr_hnf(GEN nf, GEN pr)
      97              : {
      98      3802931 :   GEN p = pr_get_p(pr), m;
      99      3802910 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
     100      3366131 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
     101      3365804 :   return ZM_hnfmodprime(m, p);
     102              : }
     103              : 
     104              : GEN
     105      1393275 : idealhnf_principal(GEN nf, GEN x)
     106              : {
     107              :   GEN cx;
     108      1393275 :   x = nf_to_scalar_or_basis(nf, x);
     109      1393272 :   switch(typ(x))
     110              :   {
     111      1132051 :     case t_COL: break;
     112       242977 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     113       241311 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     114        18245 :     case t_FRAC:
     115        18245 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     116            0 :     default: pari_err_TYPE("idealhnf",x);
     117              :   }
     118      1132051 :   x = Q_primitive_part(x, &cx);
     119      1132051 :   RgV_check_ZV(x, "idealhnf");
     120      1132050 :   x = zk_multable(nf, x);
     121      1132046 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     122      1132054 :   return cx? ZM_Q_mul(x,cx): x;
     123              : }
     124              : 
     125              : /* true nf; x integral Z_K-module as t_MAT generated by its columns.
     126              :  * Return square hnf representation */
     127              : static GEN
     128          455 : vec_mulid(GEN nf, GEN x)
     129              : {
     130          455 :   long i, j, l = lg(x);
     131          455 :   GEN D = NULL, v;
     132          455 :   v = cgetg(l, t_VEC);
     133          777 :   for (i = j = 1; i < l; i++)
     134              :   {
     135              :     GEN m, d;
     136          630 :     if (D && ZV_Z_dvd(gel(x,i), D)) l--;
     137          630 :     gel(v,j++) = m = zk_multable(nf, gel(x,i));
     138          630 :     d = zkmultable_capZ(m);
     139          630 :     D = D? gcdii(D, d): d;
     140          630 :     if (is_pm1(D)) return matid(lg(m)-1);
     141              :   }
     142          147 :   setlg(v, l); if (l == 1) return cgetg(1, t_MAT);
     143          147 :   return ZM_hnfmodid(shallowconcat1(v), D);
     144              : }
     145              : 
     146              : static GEN
     147          448 : nfV_to_ZM(GEN nf, GEN x)
     148         2254 : { pari_APPLY_type(t_MAT, algtobasis(nf, gel(x,i))) }
     149              : 
     150              : 
     151              : GEN
     152          448 : nfV_idealhnf(GEN nf, GEN v, GEN *pden)
     153              : {
     154          448 :   GEN H = ZM_hnf(Q_remove_denom(nfV_to_ZM(nf, v), pden));
     155          448 :   return vec_mulid(nf, H);
     156              : }
     157              : 
     158              : GEN
     159           42 : idealfromgens(GEN nf, GEN v)
     160              : {
     161           42 :   pari_sp av = avma;
     162              :   GEN H, den;
     163           42 :   long t = typ(v);
     164           42 :   if (t==t_MAT) { v = shallowcopy(v); settyp(v, t_VEC); }
     165           35 :   else if (!is_vec_t(t)) pari_err_TYPE("idealfromgens", v);
     166           42 :   nf = checknf(nf);
     167           42 :   H = nfV_idealhnf(nf, v, &den);
     168           42 :   return gc_upto(av, den ? gdiv(H, den): H);
     169              : }
     170              : 
     171              : /* true nf */
     172              : GEN
     173      1857374 : idealhnf_shallow(GEN nf, GEN x)
     174              : {
     175      1857374 :   long tx = typ(x), lx = lg(x), N;
     176              : 
     177              :   /* cannot use idealtyp because here we allow nonsquare matrices */
     178      1857374 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     179      1857374 :   if (tx == t_VEC && lx == 6)
     180              :   {
     181       538657 :     if (!checkprid_i(x)) pari_err_TYPE("idealhnf [fake prime ideal]",x);
     182       538645 :     return pr_hnf(nf,x); /* PRIME */
     183              :   }
     184      1318717 :   switch(tx)
     185              :   {
     186        91929 :     case t_MAT:
     187              :     {
     188              :       GEN cx;
     189        91929 :       long nx = lx-1;
     190        91929 :       N = nf_get_degree(nf);
     191        91929 :       if (nx == 0) return cgetg(1, t_MAT);
     192        91908 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     193        91901 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     194              : 
     195        75018 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     196        46382 :       x = Q_primitive_part(x, &cx);
     197        46382 :       if (nx < N)
     198            7 :         x = vec_mulid(nf, x); /* build ZK-module generated from cols */
     199              :       else
     200        46375 :         x = ZM_hnfmod(x, ZM_detmult(x)); /* assume Z-span cols is ZK-module */
     201        46382 :       return cx? ZM_Q_mul(x,cx): x;
     202              :     }
     203           14 :     case t_QFB:
     204              :     {
     205           14 :       pari_sp av = avma;
     206           14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     207           14 :       GEN A = gel(x,1), B = gel(x,2);
     208           14 :       N = nf_get_degree(nf);
     209           14 :       if (N != 2)
     210            0 :         pari_err_TYPE("idealhnf [Qfb for nonquadratic fields]", x);
     211           14 :       if (!equalii(qfb_disc(x), D))
     212            7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     213              :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     214              :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     215              :          => t = (-u + sqrt(D) f)/2
     216              :          => sqrt(D)/2 = (t + u/2)/f */
     217            7 :       u = gel(T,3);
     218            7 :       B = deg1pol_shallow(ginv(f),
     219              :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     220            7 :                           varn(T));
     221            7 :       return gc_upto(av, idealhnf_two(nf, mkvec2(A,B)));
     222              :     }
     223      1226774 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     224              :   }
     225              : }
     226              : /* true nf */
     227              : GEN
     228          665 : idealhnf(GEN nf, GEN x)
     229              : {
     230          665 :   pari_sp av = avma;
     231          665 :   GEN y = idealhnf_shallow(nf, x);
     232          651 :   return (avma == av)? gcopy(y): gc_upto(av, y);
     233              : }
     234              : 
     235              : static GEN
     236           84 : nfV_eltembed(GEN nf, GEN x, long prec)
     237          518 : { pari_APPLY_type(t_VEC, nfeltembed(nf, gel(x,i), NULL, prec)) }
     238              : 
     239              : /* true nf */
     240              : static GEN
     241           84 : nfweilheight_i(GEN nf, GEN v, long prec)
     242              : {
     243           84 :   long i, j, r1, r2, u, N, l = lg(v);
     244           84 :   GEN den, h = gen_1, id = nfV_idealhnf(nf, v, &den);
     245           84 :   GEN V = nfV_eltembed(nf, v, prec);
     246              : 
     247           84 :   nf_get_sign(nf, &r1, &r2); u = r1 + r2; N = u + r2;
     248          259 :   for (i = 1; i <= r1; i++)
     249         1029 :     for (j = 1; j < l; j++) gmael(V,j,i) = gabs(gmael(V,j,i), prec);
     250          343 :   for (     ; i <= u; i++)
     251         1771 :     for (j = 1; j < l; j++) gmael(V,j,i) = gnorm(gmael(V,j,i));
     252          518 :   for (i = 1; i <= u; i++)
     253              :   {
     254          434 :     long j0 = 1;
     255         2366 :     for (j = 2; j < l; j++)
     256         1932 :       if (gcmp(gmael(V,j,i), gmael(V,j0,i)) > 0) j0 = j;
     257          434 :     h = gmul(h, gmael(V,j0,i));
     258              :   }
     259           84 :   if (den) h = gmul(h, powiu(den, N));
     260           84 :   return divru(glog(gdiv(h, idealnorm(nf, id)), prec), N);
     261              : }
     262              : 
     263              : GEN
     264           84 : nfweilheight(GEN nf, GEN v, long prec)
     265              : {
     266           84 :   pari_sp av = avma;
     267           84 :   nf = checknf(nf);
     268           84 :   if (!is_vec_t(typ(v)) || lg(v) < 2) pari_err_TYPE("nfweilheight",v);
     269           84 :   return gc_upto(av, nfweilheight_i(nf, v, prec));
     270              : }
     271              : 
     272              : /* GP functions */
     273              : 
     274              : GEN
     275         2485 : idealtwoelt0(GEN nf, GEN x, GEN a)
     276              : {
     277         2485 :   if (!a) return idealtwoelt(nf,x);
     278           42 :   return idealtwoelt2(nf,x,a);
     279              : }
     280              : 
     281              : GEN
     282         2499 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     283              : {
     284         2499 :   if (flag) return idealpowred(nf,x,n);
     285         2492 :   return idealpow(nf,x,n);
     286              : }
     287              : 
     288              : GEN
     289           70 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     290              : {
     291           70 :   if (flag) return idealmulred(nf,x,y);
     292           63 :   return idealmul(nf,x,y);
     293              : }
     294              : 
     295              : GEN
     296           56 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     297              : {
     298           56 :   switch(flag)
     299              :   {
     300           28 :     case 0: return idealdiv(nf,x,y);
     301           28 :     case 1: return idealdivexact(nf,x,y);
     302            0 :     default: pari_err_FLAG("idealdiv");
     303              :   }
     304              :   return NULL; /* LCOV_EXCL_LINE */
     305              : }
     306              : 
     307              : GEN
     308           70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     309              : {
     310           70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     311           35 :   return idealaddtoone(nf,arg1,arg2);
     312              : }
     313              : 
     314              : /* b not a scalar */
     315              : static GEN
     316           77 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     317              : /* b not a scalar */
     318              : static GEN
     319           70 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     320              : {
     321              :   GEN db;
     322           70 :   b = Q_remove_denom(b, &db);
     323           70 :   if (db) a = mulii(a, db);
     324           70 :   b = hnf_Z_ZC(nf,a,b);
     325           70 :   return db? RgM_Rg_div(b, db): b;
     326              : }
     327              : /* b not a scalar (not point in trying to optimize for this case) */
     328              : static GEN
     329           77 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     330              : {
     331              :   GEN da, db;
     332           77 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     333            7 :   da = gel(a,2);
     334            7 :   a = gel(a,1);
     335            7 :   b = Q_remove_denom(b, &db);
     336              :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     337              :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     338            7 :   if (db)
     339              :   {
     340            7 :     GEN d = gcdii(da,db);
     341            7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     342            7 :     if (!is_pm1(db))
     343              :     {
     344            7 :       a = mulii(a, db); /* a B */
     345            7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     346              :     }
     347              :   }
     348            7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     349              : }
     350              : static GEN
     351            7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     352              : {
     353              :   GEN da, db, d, x;
     354            7 :   a = Q_remove_denom(a, &da);
     355            7 :   b = Q_remove_denom(b, &db);
     356            7 :   if (da) b = ZC_Z_mul(b, da);
     357            7 :   if (db) a = ZC_Z_mul(a, db);
     358            7 :   d = mul_denom(da, db);
     359            7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     360            7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     361            7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     362            7 :   return d? RgM_Rg_div(x, d): x;
     363              : }
     364              : static GEN
     365           21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     366              : GEN
     367          413 : idealhnf0(GEN nf, GEN a, GEN b)
     368              : {
     369              :   long ta, tb;
     370              :   pari_sp av;
     371              :   GEN x;
     372          413 :   nf = checknf(nf);
     373          413 :   if (!b) return idealhnf(nf,a);
     374              : 
     375              :   /* HNF of aZ_K+bZ_K */
     376          112 :   av = avma;
     377          112 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     378          112 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     379          105 :   if (ta == t_COL)
     380           14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     381              :   else
     382           91 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     383          105 :   return gc_upto(av, x);
     384              : }
     385              : 
     386              : /*******************************************************************/
     387              : /*                                                                 */
     388              : /*                       TWO-ELEMENT FORM                          */
     389              : /*                                                                 */
     390              : /*******************************************************************/
     391              : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     392              : 
     393              : static int
     394       226198 : ok_elt(GEN x, GEN xZ, GEN y)
     395              : {
     396       226198 :   pari_sp av = avma;
     397       226198 :   return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
     398              : }
     399              : 
     400              : /* a + s * b, a and b ZM, s integer */
     401              : static GEN
     402        66617 : addmul_mat(GEN a, GEN s, GEN b)
     403              : {
     404        66617 :   if (!signe(s)) return a;
     405        57955 :   if (!equali1(s)) b = ZM_Z_mul(b, s);
     406        57955 :   return a? ZM_add(a, b): b;
     407              : }
     408              : 
     409              : static GEN
     410       118264 : get_random_a(GEN nf, GEN x, GEN xZ)
     411              : {
     412              :   pari_sp av;
     413       118264 :   long i, lm, l = lg(x);
     414              :   GEN z, beta, mul;
     415              : 
     416       118264 :   beta= cgetg(l, t_MAT);
     417       118264 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     418              :   /* look for a in x such that a O/xZ = x O/xZ */
     419       251663 :   for (i = 2; i < l; i++)
     420              :   {
     421       241430 :     GEN xi = gel(x,i);
     422       241430 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     423       241426 :     if (gequal0(t)) continue;
     424       197913 :     if (ok_elt(x,xZ, t)) return xi;
     425        89885 :     gel(beta,lm) = xi;
     426              :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     427        89885 :     gel(mul,lm) = t; lm++;
     428              :   }
     429        10233 :   setlg(mul, lm);
     430        10233 :   setlg(beta,lm); z = cgetg(lm, t_VEC);
     431        30142 :   for(av = avma;; set_avma(av))
     432        19909 :   {
     433        30142 :     GEN a = NULL;
     434        96759 :     for (i = 1; i < lm; i++)
     435              :     {
     436        66617 :       gel(z,i) = randomi(xZ);
     437        66617 :       a = addmul_mat(a, gel(z,i), gel(mul,i));
     438              :     }
     439              :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     440        30142 :     if (a && ok_elt(x,xZ, a)) break;
     441              :   }
     442        10233 :   return ZM_ZC_mul(beta, z);
     443              : }
     444              : 
     445              : /* x square matrix, assume it is HNF */
     446              : static GEN
     447       251101 : mat_ideal_two_elt(GEN nf, GEN x)
     448              : {
     449              :   GEN y, a, cx, xZ;
     450       251101 :   long N = nf_get_degree(nf);
     451              :   pari_sp av, tetpil;
     452              : 
     453       251101 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     454       251087 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     455              : 
     456       136846 :   y = cgetg(3,t_VEC); av = avma;
     457       136845 :   cx = Q_content(x);
     458       136846 :   xZ = gcoeff(x,1,1);
     459       136846 :   if (gequal(xZ, cx)) /* x = (cx) */
     460              :   {
     461         7566 :     gel(y,1) = cx;
     462         7566 :     gel(y,2) = gen_0; return y;
     463              :   }
     464       129280 :   if (equali1(cx)) cx = NULL;
     465              :   else
     466              :   {
     467         1026 :     x = Q_div_to_int(x, cx);
     468         1026 :     xZ = gcoeff(x,1,1);
     469              :   }
     470       129280 :   if (N < 6)
     471       109813 :     a = get_random_a(nf, x, xZ);
     472              :   else
     473              :   {
     474        19467 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     475              :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     476              :     };
     477        19467 :     GEN P, E, a1 = Z_lsmoothen(xZ, (GEN)FB, &P, &E);
     478        19467 :     if (!a1) /* factors completely */
     479        11016 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     480         8451 :     else if (lg(P) == 1) /* no small factors */
     481         2604 :       a = get_random_a(nf, x, xZ);
     482              :     else /* general case */
     483              :     {
     484              :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     485         5847 :       a0 = diviiexact(xZ, a1);
     486         5847 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     487         5847 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     488         5847 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     489         5847 :       pi1 = get_random_a(nf, A1, a1);
     490         5847 :       (void)bezout(a0, a1, &v0,&v1);
     491         5847 :       u0 = mulii(a0, v0);
     492         5847 :       u1 = mulii(a1, v1);
     493         5847 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     494              :       else
     495         5847 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     496         5847 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     497         5847 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     498              :     }
     499              :   }
     500       129280 :   if (cx)
     501              :   {
     502         1026 :     a = centermod(a, xZ);
     503         1026 :     tetpil = avma;
     504         1026 :     if (typ(cx) == t_INT)
     505              :     {
     506           91 :       gel(y,1) = mulii(xZ, cx);
     507           91 :       gel(y,2) = ZC_Z_mul(a, cx);
     508              :     }
     509              :     else
     510              :     {
     511          935 :       gel(y,1) = gmul(xZ, cx);
     512          935 :       gel(y,2) = RgC_Rg_mul(a, cx);
     513              :     }
     514              :   }
     515              :   else
     516              :   {
     517       128254 :     tetpil = avma;
     518       128254 :     gel(y,1) = icopy(xZ);
     519       128254 :     gel(y,2) = centermod(a, xZ);
     520              :   }
     521       129280 :   gc_slice_unsafe(av,tetpil,y+1,2); return y;
     522              : }
     523              : 
     524              : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     525              :  * x = a Z_K + alpha Z_K, alpha in K^*
     526              :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     527              :  * x is principal. */
     528              : GEN
     529        97948 : idealtwoelt(GEN nf, GEN x)
     530              : {
     531              :   pari_sp av;
     532        97948 :   long tx = idealtyp(&x, NULL);
     533        97940 :   nf = checknf(nf);
     534        97940 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     535          735 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     536              :   /* id_PRINCIPAL */
     537          714 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     538         1232 :   return gc_GEN(av, typ(x)==t_COL? mkvec2(gen_0,x):
     539          609 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     540              : }
     541              : 
     542              : /*******************************************************************/
     543              : /*                                                                 */
     544              : /*                         FACTORIZATION                           */
     545              : /*                                                                 */
     546              : /*******************************************************************/
     547              : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     548              : static long
     549      4247787 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     550              : {
     551      4247787 :   long i, v = Zval, l = lg(x);
     552     32401857 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     553      4247816 :   return v;
     554              : }
     555              : 
     556              : /* x integral in HNF, f0 = partial factorization of a multiple of
     557              :  * x[1,1] = x\cap Z */
     558              : GEN
     559       293983 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     560              : {
     561       293983 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     562              :   long i, l;
     563       294002 :   P = gel(f,1); l = lg(P);
     564       294002 :   E = gel(f,2);
     565       294002 :   *pvN = vN = cgetg(l, t_VECSMALL);
     566       294009 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     567       734746 :   for (i = 1; i < l; i++)
     568              :   {
     569       440736 :     GEN p = gel(P,i);
     570       440736 :     vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
     571       440735 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     572              :   }
     573       294010 :   return P;
     574              : }
     575              : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     576              :  * x integral in HNF */
     577              : GEN
     578            0 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     579            0 : { return idealHNF_Z_factor_i(x, NULL, pvN, pvZ); }
     580              : 
     581              : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     582              :  * Return v_P(A) */
     583              : static long
     584      4385032 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     585              : {
     586      4385032 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     587              :   GEN mul, B, a, y, r, p, pk, cx, vals;
     588              :   pari_sp av;
     589              : 
     590      4385032 :   if (Nval < f) return 0;
     591      4381862 :   p = pr_get_p(P);
     592      4381866 :   e = pr_get_e(P);
     593              :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     594      4381869 :   vmax = minss(Zval * e, Nval / f);
     595      4381866 :   mul = pr_get_tau(P);
     596      4381864 :   l = lg(mul);
     597      4381864 :   B = cgetg(l,t_MAT);
     598              :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     599      4387739 :   gel(B,1) = gen_0; /* dummy */
     600     23790019 :   for (j = 2; j < l; j++)
     601              :   {
     602     21318912 :     GEN x = gel(A,j);
     603     21318912 :     gel(B,j) = y = cgetg(l, t_COL);
     604    229569838 :     for (i = 1; i < l; i++)
     605              :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     606    210167558 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     607   1470715622 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     608              :       /* p | a ? */
     609    210160056 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     610              :     }
     611              :   }
     612      2471107 :   vals = cgetg(l, t_VECSMALL);
     613              :   /* vals[1] not needed */
     614     16968903 :   for (j = 2; j < l; j++)
     615              :   {
     616     14497617 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     617     14497674 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     618              :   }
     619      2471286 :   pk = powiu(p, ceildivuu(vmax, e));
     620      2471229 :   av = avma; y = cgetg(l,t_COL);
     621              :   /* can compute mod p^ceil((vmax-v)/e) */
     622      4160506 :   for (v = 1; v < vmax; v++)
     623              :   { /* we know v_pr(Bj) >= v for all j */
     624      1719572 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     625      8525845 :     for (j = 2; j < l; j++)
     626              :     {
     627      6836587 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     628     44680269 :       for (i = 1; i < l; i++)
     629              :       {
     630     40068324 :         pari_sp av2 = avma;
     631     40068324 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     632    521979173 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     633              :         /* a = (x.t_0)_i; p | a ? */
     634     40067684 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     635     40037225 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     636     40037221 :         gel(y,i) = gc_INT(av2, a);
     637              :       }
     638      4611945 :       gel(B,j) = y; y = x;
     639      4611945 :       if (gc_needed(av,3))
     640              :       {
     641            0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     642            0 :         (void)gc_all(av,3, &y,&B,&pk);
     643              :       }
     644              :     }
     645              :   }
     646      2440934 :   return v;
     647              : }
     648              : /* true nf, x != 0 integral ideal in HNF, cx t_INT or NULL,
     649              :  * FA integer factorization matrix or NULL. Return partial factorization of
     650              :  * cx * x above primes in FA (complete factorization if !FA)*/
     651              : static GEN
     652       293983 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     653              : {
     654       293983 :   const long N = lg(x)-1;
     655              :   long i, j, k, l, v;
     656       293983 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     657              : 
     658       294010 :   l = lg(vp);
     659       294010 :   i = cx? expi(cx)+1: 1;
     660       294014 :   vP = cgetg((l+i-2)*N+1, t_COL);
     661       294007 :   vE = cgetg((l+i-2)*N+1, t_COL);
     662       734740 :   for (i = k = 1; i < l; i++)
     663              :   {
     664       440729 :     GEN L, p = gel(vp,i);
     665       440729 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     666       440727 :     if (vc)
     667              :     {
     668        48617 :       L = idealprimedec(nf,p);
     669        48617 :       if (is_pm1(cx)) cx = NULL;
     670              :     }
     671              :     else
     672       392110 :       L = idealprimedec_limit_f(nf,p,Nval);
     673      1018691 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     674              :     {
     675       577961 :       GEN P = gel(L,j);
     676       577961 :       pari_sp av = avma;
     677       577961 :       v = idealHNF_val(x, P, Nval, Zval);
     678       577934 :       set_avma(av);
     679       577939 :       Nval -= v*pr_get_f(P);
     680       577944 :       v += vc * pr_get_e(P); if (!v) continue;
     681       483519 :       gel(vP,k) = P;
     682       483519 :       gel(vE,k) = utoipos(v); k++;
     683              :     }
     684       491659 :     if (vc) for (; j<lg(L); j++)
     685              :     {
     686        50937 :       GEN P = gel(L,j);
     687        50937 :       gel(vP,k) = P;
     688        50937 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     689              :     }
     690              :   }
     691       294011 :   if (cx && !FA)
     692              :   { /* complete factorization */
     693        73365 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     694        73363 :     long lc = lg(cP);
     695       159419 :     for (i=1; i<lc; i++)
     696              :     {
     697        86054 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     698        86056 :       long vc = itos(gel(cE,i));
     699       188317 :       for (j=1; j<lg(L); j++)
     700              :       {
     701       102261 :         GEN P = gel(L,j);
     702       102261 :         gel(vP,k) = P;
     703       102261 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     704              :       }
     705              :     }
     706              :   }
     707       294011 :   setlg(vP, k);
     708       294009 :   setlg(vE, k); return mkmat2(vP, vE);
     709              : }
     710              : /* true nf, x integral ideal */
     711              : static GEN
     712       240109 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     713              : {
     714       240109 :   GEN cx, F = NULL;
     715       240109 :   if (lim)
     716              :   {
     717              :     GEN P, E;
     718              :     long i;
     719              :     /* strict useless because of prime table */
     720           77 :     F = absZ_factor_limit(gcoeff(x,1,1), lim);
     721           77 :     P = gel(F,1);
     722           77 :     E = gel(F,2);
     723              :     /* filter out entries > lim */
     724          126 :     for (i = lg(P)-1; i; i--)
     725          126 :       if (cmpiu(gel(P,i), lim) <= 0) break;
     726           77 :     setlg(P, i+1);
     727           77 :     setlg(E, i+1);
     728              :   }
     729       240109 :   x = Q_primitive_part(x, &cx);
     730       240083 :   return idealHNF_factor_i(nf, x, cx, F);
     731              : }
     732              : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     733              : static GEN
     734        28514 : prV_e_muls(GEN L, long c)
     735              : {
     736        28514 :   long j, l = lg(L);
     737        28514 :   GEN z = cgetg(l, t_COL);
     738        58721 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     739        28498 :   return z;
     740              : }
     741              : /* true nf, y in Q */
     742              : static GEN
     743        28508 : Q_nffactor(GEN nf, GEN y, ulong lim)
     744              : {
     745              :   GEN f, P, E;
     746              :   long l, i;
     747        28508 :   if (typ(y) == t_INT)
     748              :   {
     749        28485 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     750        28471 :     if (is_pm1(y)) return trivial_fact();
     751              :   }
     752        18001 :   y = Q_abs_shallow(y);
     753        18004 :   if (!lim) f = Q_factor(y);
     754              :   else
     755              :   {
     756          188 :     f = Q_factor_limit(y, lim);
     757          189 :     P = gel(f,1);
     758          189 :     E = gel(f,2);
     759          273 :     for (i = lg(P)-1; i > 0; i--)
     760          238 :       if (abscmpiu(gel(P,i), lim) < 0) break;
     761          189 :     setlg(P,i+1); setlg(E,i+1);
     762              :   }
     763        18001 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     764        17966 :   E = gel(f,2);
     765        46500 :   for (i = 1; i < l; i++)
     766              :   {
     767        28540 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     768        28513 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     769              :   }
     770        17960 :   P = shallowconcat1(P); gel(f,1) = P; settyp(P, t_COL);
     771        17972 :   E = shallowconcat1(E); gel(f,2) = E; return f;
     772              : }
     773              : 
     774              : GEN
     775        25515 : idealfactor_partial(GEN nf, GEN x, GEN L)
     776              : {
     777        25515 :   pari_sp av = avma;
     778              :   long i, j, l;
     779              :   GEN P, E;
     780        25515 :   if (!L) return idealfactor(nf, x);
     781        24661 :   if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
     782        24633 :   l = lg(L); if (l == 1) return trivial_fact();
     783        23863 :   P = cgetg(l, t_VEC);
     784        89964 :   for (i = 1; i < l; i++)
     785              :   {
     786        66101 :     GEN p = gel(L,i);
     787        66101 :     gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
     788              :   }
     789        23863 :   P = shallowconcat1(P); settyp(P, t_COL);
     790        23863 :   P = gen_sort_uniq(P, (void*)&cmp_prime_ideal, &cmp_nodata);
     791        23863 :   E = cgetg_copy(P, &l);
     792       114695 :   for (i = j = 1; i < l; i++)
     793              :   {
     794        90832 :     long v = idealval(nf, x, gel(P,i));
     795        90832 :     if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
     796              :   }
     797        23863 :   setlg(P,j);
     798        23863 :   setlg(E,j); return gc_GEN(av, mkmat2(P, E));
     799              : }
     800              : GEN
     801       268741 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     802              : {
     803       268741 :   pari_sp av = avma;
     804              :   GEN fa, y;
     805       268741 :   long tx = idealtyp(&x, NULL);
     806              : 
     807       268717 :   if (tx == id_PRIME)
     808              :   {
     809          119 :     if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
     810          112 :     retmkmat2(mkcolcopy(x), mkcol(gen_1));
     811              :   }
     812       268598 :   nf = checknf(nf);
     813       268596 :   if (tx == id_PRINCIPAL)
     814              :   {
     815        29864 :     y = nf_to_scalar_or_basis(nf, x);
     816        29864 :     if (typ(y) != t_COL) return gc_GEN(av, Q_nffactor(nf, y, lim));
     817              :   }
     818       240083 :   y = idealnumden(nf, x);
     819       240095 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     820       240083 :   if (!isint1(gel(y,2)))
     821           14 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     822       240082 :   fa = gc_GEN(av, fa);
     823       240098 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     824              : }
     825              : GEN
     826       268296 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     827              : GEN
     828          189 : gpidealfactor(GEN nf, GEN x, GEN lim)
     829              : {
     830          189 :   ulong L = 0;
     831          189 :   if (lim)
     832              :   {
     833           70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     834           70 :     L = itou(lim);
     835              :   }
     836          189 :   return idealfactor_limit(nf, x, L);
     837              : }
     838              : 
     839              : static GEN
     840         7749 : ramified_root(GEN nf, GEN R, GEN A, long n)
     841              : {
     842         7749 :   GEN v, P = gel(idealfactor(nf, R), 1);
     843         7749 :   long i, l = lg(P);
     844         7749 :   v = cgetg(l, t_VECSMALL);
     845         8414 :   for (i = 1; i < l; i++)
     846              :   {
     847          672 :     long w = idealval(nf, A, gel(P,i));
     848          672 :     if (w % n) return NULL;
     849          665 :     v[i] = w / n;
     850              :   }
     851         7742 :   return idealfactorback(nf, P, v, 0);
     852              : }
     853              : static int
     854            7 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
     855              : {
     856            7 :   long i, l = lg(v);
     857           21 :   for (i = 1; i < l; i++)
     858              :   {
     859           14 :     long w = v[i] % n;
     860           14 :     if (w)
     861              :     {
     862            7 :       GEN vpr = idealprimedec(nf, gel(P,i));
     863            7 :       long lpr = lg(vpr), j;
     864           14 :       for (j = 1; j < lpr; j++)
     865              :       {
     866            7 :         long e = pr_get_e(gel(vpr,j));
     867            7 :         if ((e * w) % n) return 0;
     868              :       }
     869              :     }
     870              :   }
     871            7 :   return 1;
     872              : }
     873              : /* true nf, n > 1, A a non-zero integral ideal; check whether A is the n-th
     874              :  * power of an ideal and set *pB to its n-th root if so */
     875              : static long
     876         7756 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     877              : {
     878              :   GEN C, root;
     879              :   long i, l;
     880              : 
     881         7756 :   if (typ(A) == t_MAT && ZM_isscalar(A, NULL)) A = gcoeff(A,1,1);
     882         7756 :   if (typ(A) == t_INT) /* > 0 */
     883              :   {
     884         5551 :     GEN P = nf_get_ramified_primes(nf), v, q;
     885         5551 :     l = lg(P); v = cgetg(l, t_VECSMALL);
     886        24962 :     for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
     887         5551 :     C = gen_1;
     888         5551 :     if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
     889         5551 :     if (!pB) return ramified_root_simple(nf, n, P, v);
     890         5544 :     q = factorback2(P, v);
     891         5544 :     root = ramified_root(nf, q, q, n);
     892         5544 :     if (!root) return 0;
     893         5544 :     if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
     894         5544 :     *pB = root; return 1;
     895              :   }
     896              :   /* compute valuations at ramified primes */
     897         2205 :   root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
     898         2205 :   if (!root) return 0;
     899              :   /* remove ramified primes */
     900         2198 :   if (isint1(root))
     901         1834 :     root = matid(nf_get_degree(nf));
     902              :   else
     903          364 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     904         2198 :   A = Q_primitive_part(A, &C);
     905         2198 :   if (C)
     906              :   {
     907            7 :     if (!Z_ispowerall(C,n,&C)) return 0;
     908            0 :     if (pB) root = ZM_Z_mul(root, C);
     909              :   }
     910              : 
     911              :   /* compute final n-th root, at most degree(nf)-1 iterations */
     912         2191 :   for (i = 0;; i++)
     913         2079 :   {
     914         4270 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     915         4270 :     if (is_pm1(a)) break;
     916         2107 :     if (!Z_ispowerall(a,n,&b)) return 0;
     917         2079 :     J = idealadd(nf, b, A);
     918         2079 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     919              :     /* div and not divexact here */
     920         2079 :     if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
     921              :   }
     922         2163 :   if (pB) *pB = root;
     923         2163 :   return 1;
     924              : }
     925              : 
     926              : /* A is assumed to be the n-th power of an ideal in nf
     927              :  returns its n-th root. */
     928              : long
     929         3906 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     930              : {
     931         3906 :   pari_sp av = avma;
     932              :   GEN v, N, D;
     933         3906 :   nf = checknf(nf);
     934         3906 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     935         3906 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     936         3899 :   v = idealnumden(nf,A);
     937         3899 :   if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
     938         3899 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     939         3857 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     940         3857 :   if (pB) *pB = gc_upto(av, idealdiv(nf,N,D)); else set_avma(av);
     941         3857 :   return 1;
     942              : }
     943              : 
     944              : /* x t_INT or integral nonzero ideal in HNF */
     945              : static GEN
     946       118369 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     947              : {
     948              :   GEN cx, y, U, N, F, Q;
     949       118369 :   if (typ(x) == t_INT)
     950              :   {
     951        63644 :     if (!signe(x) || is_pm1(x)) return gen_1;
     952         3549 :     F = Z_factor_limit(x, B);
     953         3549 :     gel(F,2) = gdiventgs(gel(F,2), k);
     954         3549 :     return ginv(factorback(F));
     955              :   }
     956        54725 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     957        53909 :   F = absZ_factor_limit_strict(N, B, &U);
     958        53909 :   if (U)
     959              :   {
     960          147 :     GEN M = powii(gel(U,1), gel(U,2));
     961          147 :     y = hnfmodid(x, M); /* coprime part to B! */
     962          147 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     963          147 :     x = hnfmodid(x, diviiexact(N, M));
     964              :   }
     965              :   /* x = B-smooth part of initial x */
     966        53909 :   x = Q_primitive_part(x, &cx);
     967        53908 :   F = idealHNF_factor_i(nf, x, cx, F);
     968        53909 :   gel(F,2) = gdiventgs(gel(F,2), k);
     969        53909 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     970        53909 :   if (U) Q = idealmul(nf,Q,U);
     971        53909 :   if (typ(Q) == t_INT) return Q;
     972        13153 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     973        13153 :   return gdiv(y, gcoeff(Q,1,1));
     974              : }
     975              : GEN
     976        59192 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     977              : {
     978        59192 :   pari_sp av = avma;
     979              :   GEN a, b;
     980        59192 :   nf = checknf(nf);
     981        59192 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     982        59192 :   x = idealnumden(nf, x);
     983        59192 :   a = gel(x,1);
     984        59192 :   if (isintzero(a)) { set_avma(av); return gen_1; }
     985        59185 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     986        59185 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     987        59185 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     988        59185 :   return gc_GEN(av, a);
     989              : }
     990              : 
     991              : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     992              : long
     993      9806073 : idealval(GEN nf, GEN A, GEN P)
     994              : {
     995      9806073 :   pari_sp av = avma;
     996              :   GEN p, cA;
     997      9806073 :   long vcA, v, Zval, tx = idealtyp(&A, NULL);
     998              : 
     999      9806062 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
    1000      9693442 :   checkprid(P);
    1001      9693714 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
    1002              :   /* id_MAT */
    1003      9693686 :   nf = checknf(nf);
    1004      9693857 :   A = Q_primitive_part(A, &cA);
    1005      9693983 :   p = pr_get_p(P);
    1006      9694015 :   vcA = cA? Q_pval(cA,p): 0;
    1007      9694017 :   if (pr_is_inert(P)) return gc_long(av,vcA);
    1008      9295938 :   Zval = Z_pval(gcoeff(A,1,1), p);
    1009      9295947 :   if (!Zval) v = 0;
    1010              :   else
    1011              :   {
    1012      3806973 :     long Nval = idealHNF_norm_pval(A, p, Zval);
    1013      3807072 :     v = idealHNF_val(A, P, Nval, Zval);
    1014              :   }
    1015      9295877 :   return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
    1016              : }
    1017              : GEN
    1018         7119 : gpidealval(GEN nf, GEN ix, GEN P)
    1019              : {
    1020         7119 :   long v = idealval(nf,ix,P);
    1021         7105 :   return v == LONG_MAX? mkoo(): stoi(v);
    1022              : }
    1023              : 
    1024              : /* gcd and generalized Bezout */
    1025              : 
    1026              : GEN
    1027        83578 : idealadd(GEN nf, GEN x, GEN y)
    1028              : {
    1029        83578 :   pari_sp av = avma;
    1030              :   long tx, ty;
    1031              :   GEN z, a, dx, dy, dz;
    1032              : 
    1033        83578 :   tx = idealtyp(&x, NULL);
    1034        83578 :   ty = idealtyp(&y, NULL); nf = checknf(nf);
    1035        83578 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
    1036        83578 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
    1037        83578 :   if (lg(x) == 1) return gc_GEN(av,y);
    1038        82703 :   if (lg(y) == 1) return gc_GEN(av,x); /* check for 0 ideal */
    1039        82108 :   dx = Q_denom(x);
    1040        82108 :   dy = Q_denom(y); dz = lcmii(dx,dy);
    1041        82108 :   if (is_pm1(dz)) dz = NULL; else {
    1042         5975 :     x = Q_muli_to_int(x, dz);
    1043         5975 :     y = Q_muli_to_int(y, dz);
    1044              :   }
    1045        82108 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
    1046        82108 :   if (is_pm1(a))
    1047              :   {
    1048        22889 :     long N = lg(x)-1;
    1049        22889 :     if (!dz) { set_avma(av); return matid(N); }
    1050         1085 :     return gc_upto(av, scalarmat(ginv(dz), N));
    1051              :   }
    1052        59219 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
    1053        59219 :   if (dz) z = RgM_Rg_div(z,dz);
    1054        59219 :   return gc_upto(av,z);
    1055              : }
    1056              : 
    1057              : static GEN
    1058           28 : trivial_merge(GEN x)
    1059           28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
    1060              : /* true nf */
    1061              : static GEN
    1062       803208 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
    1063              : {
    1064              :   GEN a;
    1065       803208 :   long tx = idealtyp(&x, NULL);
    1066       803185 :   long ty = idealtyp(&y, NULL);
    1067              :   long ea;
    1068       803187 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
    1069       803223 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
    1070       803223 :   if (lg(x) == 1)
    1071           14 :     a = trivial_merge(y);
    1072       803209 :   else if (lg(y) == 1)
    1073           14 :     a = trivial_merge(x);
    1074              :   else
    1075       803195 :     a = hnfmerge_get_1(x, y);
    1076       803189 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
    1077       803175 :   if (red && (ea = gexpo(a)) > 10)
    1078              :   {
    1079         4557 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
    1080         4557 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
    1081         4557 :     if (gexpo(b) < ea) a = b;
    1082              :   }
    1083       803175 :   return a;
    1084              : }
    1085              : /* true nf */
    1086              : GEN
    1087        17633 : idealaddtoone_i(GEN nf, GEN x, GEN y)
    1088        17633 : { return _idealaddtoone(nf, x, y, 1); }
    1089              : /* true nf */
    1090              : GEN
    1091       785580 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
    1092       785580 : { return _idealaddtoone(nf, x, y, 0); }
    1093              : 
    1094              : GEN
    1095           98 : idealaddtoone(GEN nf, GEN x, GEN y)
    1096              : {
    1097           98 :   GEN z = cgetg(3,t_VEC), a;
    1098           98 :   pari_sp av = avma;
    1099           98 :   nf = checknf(nf);
    1100           98 :   a = gc_upto(av, idealaddtoone_i(nf,x,y));
    1101           84 :   gel(z,1) = a;
    1102           84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
    1103           84 :   return z;
    1104              : }
    1105              : 
    1106              : /* assume elements of list are integral ideals */
    1107              : GEN
    1108           35 : idealaddmultoone(GEN nf, GEN list)
    1109              : {
    1110           35 :   pari_sp av = avma;
    1111           35 :   long N, i, l, nz, tx = typ(list);
    1112              :   GEN H, U, perm, L;
    1113              : 
    1114           35 :   nf = checknf(nf); N = nf_get_degree(nf);
    1115           35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
    1116           35 :   l = lg(list);
    1117           35 :   L = cgetg(l, t_VEC);
    1118           35 :   if (l == 1)
    1119            0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1120           35 :   nz = 0; /* number of nonzero ideals in L */
    1121           98 :   for (i=1; i<l; i++)
    1122              :   {
    1123           70 :     GEN I = gel(list,i);
    1124           70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
    1125           70 :     if (lg(I) != 1)
    1126              :     {
    1127           42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
    1128           35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
    1129              :     }
    1130           63 :     gel(L,i) = I;
    1131              :   }
    1132           28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
    1133           28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
    1134            7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1135           49 :   for (i=1; i<=N; i++)
    1136           49 :     if (perm[i] == 1) break;
    1137           21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
    1138           21 :   nz = 0;
    1139           63 :   for (i=1; i<l; i++)
    1140              :   {
    1141           42 :     GEN c = gel(L,i);
    1142           42 :     if (lg(c) == 1)
    1143           14 :       c = gen_0;
    1144              :     else {
    1145           28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
    1146           28 :       nz++;
    1147              :     }
    1148           42 :     gel(L,i) = c;
    1149              :   }
    1150           21 :   return gc_GEN(av, L);
    1151              : }
    1152              : 
    1153              : /* multiplication */
    1154              : 
    1155              : /* x integral ideal (without archimedean component) in HNF form
    1156              :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
    1157              :  * alpha a ZV or a ZM (multiplication table). Multiply them */
    1158              : static GEN
    1159       975146 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1160              : {
    1161       975146 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1162              :   long i, N;
    1163              : 
    1164       975146 :   if (typ(alpha) != t_MAT)
    1165              :   {
    1166       625563 :     alpha = zk_scalar_or_multable(nf, alpha);
    1167       625562 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1168        14279 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1169              :   }
    1170       960866 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1171      3807755 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1172      3806726 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1173       960162 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
    1174              : }
    1175              : 
    1176              : /* Assume x and y are integral in HNF form [NOT extended]. Not memory clean.
    1177              :  * HACK: ideal in y can be of the form [a,b], a in Z, b in Z_K */
    1178              : GEN
    1179       474545 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1180              : {
    1181              :   GEN z;
    1182       474545 :   if (typ(y) == t_VEC)
    1183       301224 :     z = idealHNF_mul_two(nf,x,y);
    1184              :   else
    1185              :   { /* reduce one ideal to two-elt form. The smallest */
    1186       173321 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1187       173321 :     if (cmpii(xZ, yZ) < 0)
    1188              :     {
    1189        35945 :       if (is_pm1(xZ)) return gcopy(y);
    1190        20343 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1191              :     }
    1192              :     else
    1193              :     {
    1194       137376 :       if (is_pm1(yZ)) return gcopy(x);
    1195        36305 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1196              :     }
    1197              :   }
    1198       357887 :   return z;
    1199              : }
    1200              : 
    1201              : /* operations on elements in factored form */
    1202              : 
    1203              : GEN
    1204       224909 : famat_mul_shallow(GEN f, GEN g)
    1205              : {
    1206       224909 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1207       224909 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1208       224909 :   if (lgcols(f) == 1) return g;
    1209       167620 :   if (lgcols(g) == 1) return f;
    1210       165677 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1211       165676 :                 shallowconcat(gel(f,2), gel(g,2)));
    1212              : }
    1213              : GEN
    1214        88347 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1215              : {
    1216        88347 :   if (!signe(e)) return f;
    1217        54585 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1218              : }
    1219              : 
    1220              : GEN
    1221       147668 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1222              : {
    1223       147668 :   if (e==0) return f;
    1224       118740 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1225              : }
    1226              : 
    1227              : GEN
    1228        10311 : famat_div_shallow(GEN f, GEN g)
    1229        10311 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
    1230              : 
    1231              : GEN
    1232       376293 : Z_to_famat(GEN x)
    1233              : {
    1234              :   long k;
    1235       376293 :   if (equali1(x)) return trivial_fact();
    1236       192492 :   k = Z_isanypower(x, &x) ;
    1237       192492 :   return to_famat_shallow(x, k? utoi(k): gen_1);
    1238              : }
    1239              : GEN
    1240       197089 : Q_to_famat(GEN x)
    1241              : {
    1242       197089 :   if (typ(x) == t_INT) return Z_to_famat(x);
    1243       179204 :   return famat_div(Z_to_famat(gel(x,1)), Z_to_famat(gel(x,2)));
    1244              : }
    1245              : GEN
    1246            0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1247              : GEN
    1248      2769796 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1249              : 
    1250              : /* concat the single elt x; not gconcat since x may be a t_COL */
    1251              : static GEN
    1252       155937 : append(GEN v, GEN x)
    1253              : {
    1254       155937 :   long i, l = lg(v);
    1255       155937 :   GEN w = cgetg(l+1, typ(v));
    1256       661454 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1257       155937 :   gel(w,i) = gcopy(x); return w;
    1258              : }
    1259              : /* add x^1 to famat f */
    1260              : static GEN
    1261       162630 : famat_add(GEN f, GEN x)
    1262              : {
    1263       162630 :   GEN h = cgetg(3,t_MAT);
    1264       162630 :   if (lgcols(f) == 1)
    1265              :   {
    1266        13200 :     gel(h,1) = mkcolcopy(x);
    1267        13200 :     gel(h,2) = mkcol(gen_1);
    1268              :   }
    1269              :   else
    1270              :   {
    1271       149430 :     gel(h,1) = append(gel(f,1), x);
    1272       149430 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1273              :   }
    1274       162630 :   return h;
    1275              : }
    1276              : /* add x^-1 to famat f */
    1277              : static GEN
    1278        20914 : famat_sub(GEN f, GEN x)
    1279              : {
    1280        20914 :   GEN h = cgetg(3,t_MAT);
    1281        20914 :   if (lgcols(f) == 1)
    1282              :   {
    1283        14407 :     gel(h,1) = mkcolcopy(x);
    1284        14407 :     gel(h,2) = mkcol(gen_m1);
    1285              :   }
    1286              :   else
    1287              :   {
    1288         6507 :     gel(h,1) = append(gel(f,1), x);
    1289         6507 :     gel(h,2) = gconcat(gel(f,2), gen_m1);
    1290              :   }
    1291        20914 :   return h;
    1292              : }
    1293              : 
    1294              : GEN
    1295       451458 : famat_mul(GEN f, GEN g)
    1296              : {
    1297              :   GEN h;
    1298       451458 :   if (typ(g) != t_MAT) {
    1299        30519 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1300            0 :     h = cgetg(3, t_MAT);
    1301            0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1302            0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1303            0 :     return h;
    1304              :   }
    1305       420939 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1306       288828 :   if (lgcols(f) == 1) return gcopy(g);
    1307       265495 :   if (lgcols(g) == 1) return gcopy(f);
    1308       259501 :   h = cgetg(3,t_MAT);
    1309       259501 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1310       259501 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1311       259501 :   return h;
    1312              : }
    1313              : 
    1314              : GEN
    1315       200125 : famat_div(GEN f, GEN g)
    1316              : {
    1317              :   GEN h;
    1318       200125 :   if (typ(g) != t_MAT) {
    1319        20872 :     if (typ(f) == t_MAT) return famat_sub(f, g);
    1320            0 :     h = cgetg(3, t_MAT);
    1321            0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1322            0 :     gel(h,2) = mkcol2(gen_1, gen_m1);
    1323            0 :     return h;
    1324              :   }
    1325       179253 :   if (typ(f) != t_MAT) return famat_sub(g, f);
    1326       179211 :   if (lgcols(f) == 1) return famat_inv(g);
    1327          260 :   if (lgcols(g) == 1) return gcopy(f);
    1328          260 :   h = cgetg(3,t_MAT);
    1329          260 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1330          260 :   gel(h,2) = gconcat(gel(f,2), gneg(gel(g,2)));
    1331          260 :   return h;
    1332              : }
    1333              : 
    1334              : GEN
    1335        22984 : famat_sqr(GEN f)
    1336              : {
    1337              :   GEN h;
    1338        22984 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1339        22984 :   if (lgcols(f) == 1) return gcopy(f);
    1340        13139 :   h = cgetg(3,t_MAT);
    1341        13139 :   gel(h,1) = gcopy(gel(f,1));
    1342        13139 :   gel(h,2) = gmul2n(gel(f,2),1);
    1343        13139 :   return h;
    1344              : }
    1345              : 
    1346              : GEN
    1347        26947 : famat_inv_shallow(GEN f)
    1348              : {
    1349        26947 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1350        10451 :   if (lgcols(f) == 1) return f;
    1351        10451 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1352              : }
    1353              : GEN
    1354       199358 : famat_inv(GEN f)
    1355              : {
    1356       199358 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1357       199358 :   if (lgcols(f) == 1) return gcopy(f);
    1358       180814 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1359              : }
    1360              : GEN
    1361        60642 : famat_pow(GEN f, GEN n)
    1362              : {
    1363        60642 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1364        60642 :   if (lgcols(f) == 1) return gcopy(f);
    1365        60642 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1366              : }
    1367              : GEN
    1368        62032 : famat_pow_shallow(GEN f, GEN n)
    1369              : {
    1370        62032 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1371        35696 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1372         8016 :   if (lgcols(f) == 1) return f;
    1373         6103 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1374              : }
    1375              : 
    1376              : GEN
    1377       151899 : famat_pows_shallow(GEN f, long n)
    1378              : {
    1379       151899 :   if (n==1) return f;
    1380        35007 :   if (n==-1) return famat_inv_shallow(f);
    1381        35000 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1382        26742 :   if (lgcols(f) == 1) return f;
    1383        26742 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1384              : }
    1385              : 
    1386              : GEN
    1387            0 : famat_Z_gcd(GEN M, GEN n)
    1388              : {
    1389            0 :   pari_sp av=avma;
    1390            0 :   long i, j, l=lgcols(M);
    1391            0 :   GEN F=cgetg(3,t_MAT);
    1392            0 :   gel(F,1)=cgetg(l,t_COL);
    1393            0 :   gel(F,2)=cgetg(l,t_COL);
    1394            0 :   for (i=1, j=1; i<l; i++)
    1395              :   {
    1396            0 :     GEN p = gcoeff(M,i,1);
    1397            0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1398            0 :     if (signe(e))
    1399              :     {
    1400            0 :       gcoeff(F,j,1)=p;
    1401            0 :       gcoeff(F,j,2)=e;
    1402            0 :       j++;
    1403              :     }
    1404              :   }
    1405            0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1406            0 :   return gc_GEN(av,F);
    1407              : }
    1408              : 
    1409              : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1410              :  * the element_* functions. */
    1411              : static GEN
    1412        33946 : ext_sqr(GEN nf, GEN x)
    1413        33946 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1414              : static GEN
    1415        61062 : ext_mul(GEN nf, GEN x, GEN y)
    1416        61062 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1417              : static GEN
    1418        20407 : ext_inv(GEN nf, GEN x)
    1419        20407 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1420              : static GEN
    1421            0 : ext_pow(GEN nf, GEN x, GEN n)
    1422            0 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1423              : 
    1424              : GEN
    1425            0 : famat_to_nf(GEN nf, GEN f)
    1426              : {
    1427              :   GEN t, x, e;
    1428              :   long i;
    1429            0 :   if (lgcols(f) == 1) return gen_1;
    1430            0 :   x = gel(f,1);
    1431            0 :   e = gel(f,2);
    1432            0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1433            0 :   for (i=lg(x)-1; i>1; i--)
    1434            0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1435            0 :   return t;
    1436              : }
    1437              : 
    1438              : GEN
    1439            0 : famat_idealfactor(GEN nf, GEN x)
    1440              : {
    1441              :   long i, l;
    1442            0 :   GEN g = gel(x,1), e = gel(x,2), h = cgetg_copy(g, &l);
    1443            0 :   for (i = 1; i < l; i++) gel(h,i) = idealfactor(nf, gel(g,i));
    1444            0 :   h = famat_reduce(famatV_factorback(h,e));
    1445            0 :   return sort_factor(h, (void*)&cmp_prime_ideal, &cmp_nodata);
    1446              : }
    1447              : 
    1448              : GEN
    1449       308204 : famat_reduce(GEN fa)
    1450              : {
    1451              :   GEN E, G, L, g, e;
    1452              :   long i, k, l;
    1453              : 
    1454       308204 :   if (typ(fa) != t_MAT || lgcols(fa) == 1) return fa;
    1455       297261 :   g = gel(fa,1); l = lg(g);
    1456       297261 :   e = gel(fa,2);
    1457       297261 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1458       297262 :   G = cgetg(l, t_COL);
    1459       297262 :   E = cgetg(l, t_COL);
    1460              :   /* merge */
    1461      2366633 :   for (k=i=1; i<l; i++,k++)
    1462              :   {
    1463      2069370 :     gel(G,k) = gel(g,L[i]);
    1464      2069370 :     gel(E,k) = gel(e,L[i]);
    1465      2069370 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1466              :     {
    1467       839993 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1468       839994 :       k--;
    1469              :     }
    1470              :   }
    1471              :   /* kill 0 exponents */
    1472       297263 :   l = k;
    1473      1526641 :   for (k=i=1; i<l; i++)
    1474      1229378 :     if (!gequal0(gel(E,i)))
    1475              :     {
    1476      1204612 :       gel(G,k) = gel(G,i);
    1477      1204612 :       gel(E,k) = gel(E,i); k++;
    1478              :     }
    1479       297263 :   setlg(G, k);
    1480       297263 :   setlg(E, k); return mkmat2(G,E);
    1481              : }
    1482              : GEN
    1483           77 : matreduce(GEN f)
    1484           77 : { pari_sp av = avma;
    1485           77 :   switch(typ(f))
    1486              :   {
    1487           35 :     case t_VEC: case t_COL:
    1488              :     {
    1489           35 :       GEN e; f = vec_reduce(f, &e); settyp(f, t_COL);
    1490           35 :       return gc_GEN(av, mkmat2(f, zc_to_ZC(e)));
    1491              :     }
    1492           35 :     case t_MAT:
    1493           35 :       if (lg(f) == 3) break;
    1494              :     default:
    1495           14 :       pari_err_TYPE("matreduce", f);
    1496              :   }
    1497           28 :   if (typ(gel(f,1)) == t_VECSMALL)
    1498            0 :     f = famatsmall_reduce(f);
    1499              :   else
    1500              :   {
    1501           28 :     if (!RgV_is_ZV(gel(f,2))) pari_err_TYPE("matreduce",f);
    1502           21 :     f = famat_reduce(f);
    1503              :   }
    1504           21 :   return gc_GEN(av, f);
    1505              : }
    1506              : 
    1507              : GEN
    1508       185892 : famatsmall_reduce(GEN fa)
    1509              : {
    1510              :   GEN E, G, L, g, e;
    1511              :   long i, k, l;
    1512       185892 :   if (lgcols(fa) == 1) return fa;
    1513       185892 :   g = gel(fa,1); l = lg(g);
    1514       185892 :   e = gel(fa,2);
    1515       185892 :   L = vecsmall_indexsort(g);
    1516       185893 :   G = cgetg(l, t_VECSMALL);
    1517       185893 :   E = cgetg(l, t_VECSMALL);
    1518              :   /* merge */
    1519       504731 :   for (k=i=1; i<l; i++,k++)
    1520              :   {
    1521       318838 :     G[k] = g[L[i]];
    1522       318838 :     E[k] = e[L[i]];
    1523       318838 :     if (k > 1 && G[k] == G[k-1])
    1524              :     {
    1525         7974 :       E[k-1] += E[k];
    1526         7974 :       k--;
    1527              :     }
    1528              :   }
    1529              :   /* kill 0 exponents */
    1530       185893 :   l = k;
    1531       496757 :   for (k=i=1; i<l; i++)
    1532       310864 :     if (E[i])
    1533              :     {
    1534       307219 :       G[k] = G[i];
    1535       307219 :       E[k] = E[i]; k++;
    1536              :     }
    1537       185893 :   setlg(G, k);
    1538       185893 :   setlg(E, k); return mkmat2(G,E);
    1539              : }
    1540              : 
    1541              : GEN
    1542        67007 : famat_remove_trivial(GEN fa)
    1543              : {
    1544        67007 :   GEN P, E, p = gel(fa,1), e = gel(fa,2);
    1545        67007 :   long j, k, l = lg(p);
    1546        67007 :   P = cgetg(l, t_COL);
    1547        67007 :   E = cgetg(l, t_COL);
    1548      1826705 :   for (j = k = 1; j < l; j++)
    1549      1759698 :     if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
    1550        67007 :   setlg(P, k); setlg(E, k); return mkmat2(P,E);
    1551              : }
    1552              : 
    1553              : GEN
    1554        13814 : famatV_factorback(GEN v, GEN e)
    1555              : {
    1556        13814 :   long i, l = lg(e);
    1557              :   GEN V;
    1558        13814 :   if (l == 1) return trivial_fact();
    1559        13429 :   V = signe(gel(e,1))? famat_pow_shallow(gel(v,1), gel(e,1)): trivial_fact();
    1560        56788 :   for (i = 2; i < l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
    1561        13429 :   return V;
    1562              : }
    1563              : 
    1564              : GEN
    1565        56273 : famatV_zv_factorback(GEN v, GEN e)
    1566              : {
    1567        56273 :   long i, l = lg(e);
    1568              :   GEN V;
    1569        56273 :   if (l == 1) return trivial_fact();
    1570        53816 :   V = uel(e,1)? famat_pows_shallow(gel(v,1), uel(e,1)): trivial_fact();
    1571       177813 :   for (i = 2; i < l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
    1572        53815 :   return V;
    1573              : }
    1574              : 
    1575              : GEN
    1576       568151 : ZM_famat_limit(GEN fa, GEN limit)
    1577              : {
    1578              :   pari_sp av;
    1579              :   GEN E, G, g, e, r;
    1580              :   long i, k, l, n, lG;
    1581              : 
    1582       568151 :   if (lgcols(fa) == 1) return fa;
    1583       568144 :   g = gel(fa,1); l = lg(g);
    1584       568144 :   e = gel(fa,2);
    1585      1137435 :   for(n=0, i=1; i<l; i++)
    1586       569292 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1587       568143 :   lG = n<l-1 ? n+2 : n+1;
    1588       568143 :   G = cgetg(lG, t_COL);
    1589       568143 :   E = cgetg(lG, t_COL);
    1590       568143 :   av = avma;
    1591      1137435 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1592              :   {
    1593       569291 :     if (cmpii(gel(g,i),limit)<=0)
    1594              :     {
    1595       569159 :       gel(G,k) = gel(g,i);
    1596       569159 :       gel(E,k) = gel(e,i);
    1597       569159 :       k++;
    1598          133 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1599              :   }
    1600       568144 :   if (k<i)
    1601              :   {
    1602          133 :     gel(G, k) = gc_INT(av, r);
    1603          133 :     gel(E, k) = gen_1;
    1604              :   }
    1605       568144 :   return mkmat2(G,E);
    1606              : }
    1607              : 
    1608              : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1609              : static GEN
    1610       122255 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1611              : {
    1612       122255 :   GEN d, r, p = modpr_get_p(modpr);
    1613       122255 :   x = nf_to_scalar_or_basis(nf,x);
    1614       122255 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1615       121016 :   x = Q_remove_denom(x, &d);
    1616       121016 :   r = zk_to_Fq(x, modpr);
    1617       121015 :   if (d) r = Fp_div(r, d, p);
    1618       121015 :   return r;
    1619              : }
    1620              : 
    1621              : /* pr coprime to all denominators occurring in x */
    1622              : static GEN
    1623          665 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1624              : {
    1625          665 :   GEN p = modpr_get_p(modpr);
    1626          665 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1627          665 :   long i, l = lg(g);
    1628         3808 :   for (i = 1; i < l; i++)
    1629              :   {
    1630         3143 :     GEN n = modii(gel(e,i), q);
    1631         3143 :     if (signe(n))
    1632              :     {
    1633         3122 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1634         3122 :       h = Fp_pow(h, n, p);
    1635         3122 :       t = t? Fp_mul(t, h, p): h;
    1636              :     }
    1637              :   }
    1638          665 :   return t? modii(t, p): gen_1;
    1639              : }
    1640              : 
    1641              : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1642              : GEN
    1643       119798 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1644              : {
    1645          665 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1646       120463 :                       : to_Fp_coprime(nf, x, modpr);
    1647              : }
    1648              : 
    1649              : static long
    1650      4331903 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1651      4331903 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1652              : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1653              :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1654              : static GEN
    1655      4377720 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1656              : {
    1657              :   long vcx;
    1658              :   GEN dx;
    1659      4377720 :   x = nf_to_scalar_or_basis(nf, x);
    1660      4377721 :   x = Q_remove_denom(x, &dx);
    1661      4377731 :   if (dx)
    1662              :   {
    1663        61229 :     vcx = - Z_pvalrem(dx, p, &dx);
    1664        61229 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1665        61229 :     if (isint1(dx)) dx = NULL;
    1666              :   }
    1667              :   else
    1668              :   {
    1669      4316502 :     vcx = zk_pvalrem(x, p, &x);
    1670      4316492 :     dx = NULL;
    1671              :   }
    1672      4377721 :   *pv = vcx;
    1673      4377721 :   *pdx = dx; return x;
    1674              : }
    1675              : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1676              :  * if p inert (instead of 1) */
    1677              : static GEN
    1678        95075 : p_makecoprime(GEN pr)
    1679              : {
    1680        95075 :   GEN B = pr_get_tau(pr), b;
    1681              :   long i, e;
    1682              : 
    1683        95075 :   if (typ(B) == t_INT) return NULL;
    1684        72318 :   b = gel(B,1); /* B = multiplication table by b */
    1685        72318 :   e = pr_get_e(pr);
    1686        72318 :   if (e == 1) return b;
    1687              :   /* one could also divide (exactly) by p in each iteration */
    1688        45383 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1689        22178 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1690              : }
    1691              : 
    1692              : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1693              :  * Method: modify each g[i] so that it becomes coprime to pr,
    1694              :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1695              :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1696              :  * Optimizations:
    1697              :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1698              :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1699              :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1700              :  *
    1701              :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1702              :  * case the e[i] are large */
    1703              : GEN
    1704      2220947 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1705              : {
    1706      2220947 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1707      2220949 :   long i, l = lg(g);
    1708              : 
    1709      2220949 :   G = cgetg(l+1, t_VEC);
    1710      2220958 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1711      6598677 :   for (i=1; i < l; i++)
    1712              :   {
    1713              :     long vcx;
    1714      4377721 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1715      4377712 :     if (vcx) /* = v_p(content(g[i])) */
    1716              :     {
    1717       142267 :       GEN a = mulsi(vcx, gel(e,i));
    1718       142283 :       vp = vp? addii(vp, a): a;
    1719              :     }
    1720              :     /* x integral, content coprime to p; dx coprime to p */
    1721      4377727 :     if (typ(x) == t_INT)
    1722              :     { /* x coprime to p, hence to pr */
    1723      1136100 :       x = modii(x, prkZ);
    1724      1136099 :       if (dx) x = Fp_div(x, dx, prkZ);
    1725              :     }
    1726              :     else
    1727              :     {
    1728      3241627 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1729      3241594 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1730      3241628 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1731              :     }
    1732      4377711 :     gel(G,i) = x;
    1733      4377711 :     gel(E,i) = gel(e,i);
    1734              :   }
    1735              : 
    1736      2220956 :   t = vp? p_makecoprime(pr): NULL;
    1737      2220958 :   if (!t)
    1738              :   { /* no need for extra generator */
    1739      2148717 :     setlg(G,l);
    1740      2148716 :     setlg(E,l);
    1741              :   }
    1742              :   else
    1743              :   {
    1744        72241 :     gel(G,i) = FpC_red(t, prkZ);
    1745        72241 :     gel(E,i) = vp;
    1746              :   }
    1747      2220957 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1748              : }
    1749              : 
    1750              : /* simplified version of famat_makecoprime for X = SUnits[1] */
    1751              : GEN
    1752           98 : sunits_makecoprime(GEN X, GEN pr, GEN prk)
    1753              : {
    1754           98 :   GEN G, p = pr_get_p(pr), prkZ = gcoeff(prk,1,1);
    1755           98 :   long i, l = lg(X);
    1756              : 
    1757           98 :   G = cgetg(l, t_VEC);
    1758         9205 :   for (i = 1; i < l; i++)
    1759              :   {
    1760         9107 :     GEN x = gel(X,i);
    1761         9107 :     if (typ(x) == t_INT) /* a prime */
    1762         1491 :       x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
    1763              :     else
    1764              :     {
    1765         7616 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1766         7616 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1767              :     }
    1768         9107 :     gel(G,i) = x;
    1769              :   }
    1770           98 :   return G;
    1771              : }
    1772              : 
    1773              : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
    1774              : GEN
    1775        18606 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1776              : {
    1777        18606 :   GEN t, cyc = bid_get_cyc(bid);
    1778        18606 :   if (lg(cyc) == 1)
    1779            0 :     t = gen_1;
    1780              :   else
    1781        18606 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
    1782              :                                      cyc_get_expo(cyc));
    1783        18606 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1784              : }
    1785              : 
    1786              : GEN
    1787     16015377 : vecmul(GEN x, GEN y)
    1788              : {
    1789     16015377 :   if (!is_vec_t(typ(x))) return gmul(x,y);
    1790      3529110 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1791              : }
    1792              : 
    1793              : GEN
    1794       185983 : vecsqr(GEN x)
    1795              : {
    1796       185983 :   if (!is_vec_t(typ(x))) return gsqr(x);
    1797        46606 :   pari_APPLY_same(vecsqr(gel(x,i)))
    1798              : }
    1799              : 
    1800              : GEN
    1801          826 : vecinv(GEN x)
    1802              : {
    1803          826 :   if (!is_vec_t(typ(x))) return ginv(x);
    1804           56 :   pari_APPLY_same(vecinv(gel(x,i)))
    1805              : }
    1806              : 
    1807              : GEN
    1808            0 : vecpow(GEN x, GEN n)
    1809              : {
    1810            0 :   if (!is_vec_t(typ(x))) return powgi(x,n);
    1811            0 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1812              : }
    1813              : 
    1814              : GEN
    1815          903 : vecdiv(GEN x, GEN y)
    1816              : {
    1817          903 :   if (!is_vec_t(typ(x))) return gdiv(x,y);
    1818          903 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1819              : }
    1820              : 
    1821              : /* A ideal as a square t_MAT */
    1822              : static GEN
    1823       266228 : idealmulelt(GEN nf, GEN x, GEN A)
    1824              : {
    1825              :   long i, lx;
    1826              :   GEN dx, dA, D;
    1827       266228 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1828       266228 :   x = nf_to_scalar_or_basis(nf,x);
    1829       266228 :   if (typ(x) != t_COL)
    1830        71769 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1831       194459 :   x = Q_remove_denom(x, &dx);
    1832       194459 :   A = Q_remove_denom(A, &dA);
    1833       194459 :   x = zk_multable(nf, x);
    1834       194459 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1835       194459 :   x = zkC_multable_mul(A, x);
    1836       194459 :   settyp(x, t_MAT); lx = lg(x);
    1837              :   /* x may contain scalars (at most 1 since the ideal is nonzero)*/
    1838       751432 :   for (i=1; i<lx; i++)
    1839       566226 :     if (typ(gel(x,i)) == t_INT)
    1840              :     {
    1841         9253 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1842         9253 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1843         9253 :       break;
    1844              :     }
    1845       194459 :   x = ZM_hnfmodid(x, D);
    1846       194459 :   dx = mul_denom(dx,dA);
    1847       194459 :   return dx? gdiv(x,dx): x;
    1848              : }
    1849              : 
    1850              : /* nf a true nf, tx <= ty */
    1851              : static GEN
    1852       512055 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1853              : {
    1854              :   GEN z, cx, cy;
    1855       512055 :   switch(tx)
    1856              :   {
    1857       304262 :     case id_PRINCIPAL:
    1858       304262 :       switch(ty)
    1859              :       {
    1860        37586 :         case id_PRINCIPAL:
    1861        37586 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1862          448 :         case id_PRIME:
    1863              :         {
    1864          448 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1865          448 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1866              : 
    1867          217 :           x = nf_to_scalar_or_basis(nf, x);
    1868          217 :           switch(typ(x))
    1869              :           {
    1870          203 :             case t_INT:
    1871          203 :               if (!signe(x)) return cgetg(1,t_MAT);
    1872          203 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1873            7 :             case t_FRAC:
    1874            7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1875              :           }
    1876              :           /* t_COL */
    1877            7 :           x = Q_primitive_part(x, &cx);
    1878            7 :           x = zk_multable(nf, x);
    1879            7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1880            7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1881            7 :           return cx? ZM_Q_mul(z, cx): z;
    1882              :         }
    1883       266228 :         default: /* id_MAT */
    1884       266228 :           return idealmulelt(nf, x,y);
    1885              :       }
    1886        42850 :     case id_PRIME:
    1887        42850 :       if (ty==id_PRIME)
    1888         4347 :       { y = pr_hnf(nf,y); cy = NULL; }
    1889              :       else
    1890        38503 :         y = Q_primitive_part(y, &cy);
    1891        42850 :       y = idealHNF_mul_two(nf,y,x);
    1892        42851 :       return cy? ZM_Q_mul(y,cy): y;
    1893              : 
    1894       164943 :     default: /* id_MAT */
    1895              :     {
    1896       164943 :       long N = nf_get_degree(nf);
    1897       164943 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1898       164929 :       x = Q_primitive_part(x, &cx);
    1899       164929 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1900       164929 :       y = idealHNF_mul(nf,x,y);
    1901       164929 :       return cx? ZM_Q_mul(y,cx): y;
    1902              :     }
    1903              :   }
    1904              : }
    1905              : 
    1906              : /* output the ideal product x.y */
    1907              : GEN
    1908       512055 : idealmul(GEN nf, GEN x, GEN y)
    1909              : {
    1910              :   pari_sp av;
    1911              :   GEN res, ax, ay, z;
    1912       512055 :   long tx = idealtyp(&x,&ax);
    1913       512055 :   long ty = idealtyp(&y,&ay), f;
    1914       512055 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1915       512055 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1916       512055 :   av = avma;
    1917       512055 :   z = gc_upto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1918       512042 :   if (!f) return z;
    1919        28039 :   if (ax && ay)
    1920        26541 :     ax = ext_mul(nf, ax, ay);
    1921              :   else
    1922         1498 :     ax = gcopy(ax? ax: ay);
    1923        28039 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1924              : }
    1925              : 
    1926              : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1927              :  * nf = true nf */
    1928              : static GEN
    1929       317550 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1930              : {
    1931       317550 :   GEN p = pr_get_p(pr), q, gen;
    1932       317550 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1933              : 
    1934       317553 :   q = (e == 1)? sqri(p): p;
    1935       317549 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1936              :   { /* pr^e = (p) */
    1937        45692 :     *pc = q;
    1938        45692 :     return mkvec2(gen_1,gen_0);
    1939              :   }
    1940       271856 :   gen = nfsqr(nf, pr_get_gen(pr));
    1941       271854 :   gen = FpC_red(gen, q);
    1942       271842 :   *pc = NULL;
    1943       271842 :   return mkvec2(q, gen);
    1944              : }
    1945              : /* cf idealpow_aux */
    1946              : static GEN
    1947        39245 : idealsqr_aux(GEN nf, GEN x, long tx)
    1948              : {
    1949        39245 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1950        39245 :   long N = degpol(T);
    1951        39245 :   switch(tx)
    1952              :   {
    1953          385 :     case id_PRINCIPAL:
    1954          385 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1955        10785 :     case id_PRIME:
    1956        10785 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1957        10617 :       x = idealsqrprime(nf, x, &cx);
    1958        10617 :       x = idealhnf_two(nf,x);
    1959        10617 :       return cx? ZM_Z_mul(x, cx): x;
    1960        28075 :     default:
    1961        28075 :       x = Q_primitive_part(x, &cx);
    1962        28075 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1963        28075 :       alpha = nfsqr(nf,alpha);
    1964        28075 :       m = zk_scalar_or_multable(nf, alpha);
    1965        28075 :       if (typ(m) == t_INT) {
    1966         1642 :         x = gcdii(sqri(a), m);
    1967         1642 :         if (cx) x = gmul(x, gsqr(cx));
    1968         1642 :         x = scalarmat(x, N);
    1969              :       }
    1970              :       else
    1971              :       { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
    1972        26433 :         x = ZM_hnfmodid(m, sqri(a));
    1973        26433 :         if (cx) cx = gsqr(cx);
    1974        26433 :         if (cx) x = ZM_Q_mul(x, cx);
    1975              :       }
    1976        28075 :       return x;
    1977              :   }
    1978              : }
    1979              : GEN
    1980        39245 : idealsqr(GEN nf, GEN x)
    1981              : {
    1982              :   pari_sp av;
    1983              :   GEN res, ax, z;
    1984        39245 :   long tx = idealtyp(&x,&ax);
    1985        39245 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1986        39245 :   av = avma;
    1987        39245 :   z = gc_upto(av, idealsqr_aux(checknf(nf), x, tx));
    1988        39245 :   if (!ax) return z;
    1989        33946 :   gel(res,1) = z;
    1990        33946 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1991              : }
    1992              : 
    1993              : /* norm of an ideal */
    1994              : GEN
    1995       106058 : idealnorm(GEN nf, GEN x)
    1996              : {
    1997              :   pari_sp av;
    1998              :   long tx;
    1999              : 
    2000       106058 :   switch(idealtyp(&x, NULL))
    2001              :   {
    2002         4935 :     case id_PRIME: return pr_norm(x);
    2003        11179 :     case id_MAT: return RgM_det_triangular(x);
    2004              :   }
    2005              :   /* id_PRINCIPAL */
    2006        89944 :   nf = checknf(nf); av = avma;
    2007        89944 :   x = nfnorm(nf, x);
    2008        89944 :   tx = typ(x);
    2009        89944 :   if (tx == t_INT) return gc_INT(av, absi(x));
    2010          420 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    2011          420 :   return gc_upto(av, Q_abs(x));
    2012              : }
    2013              : 
    2014              : /* x \cap Z */
    2015              : GEN
    2016         3031 : idealdown(GEN nf, GEN x)
    2017              : {
    2018         3031 :   pari_sp av = avma;
    2019              :   GEN y, c;
    2020         3031 :   switch(idealtyp(&x, NULL))
    2021              :   {
    2022            7 :     case id_PRIME: return icopy(pr_get_p(x));
    2023         2121 :     case id_MAT: return gcopy(gcoeff(x,1,1));
    2024              :   }
    2025              :   /* id_PRINCIPAL */
    2026          903 :   nf = checknf(nf); av = avma;
    2027          903 :   x = nf_to_scalar_or_basis(nf, x);
    2028          903 :   if (is_rational_t(typ(x))) return Q_abs(x);
    2029           14 :   x = Q_primitive_part(x, &c);
    2030           14 :   y = zkmultable_capZ(zk_multable(nf, x));
    2031           14 :   return gc_GEN(av, mul_content(c, y));
    2032              : }
    2033              : 
    2034              : /* true nf */
    2035              : static GEN
    2036           42 : idealismaximal_int(GEN nf, GEN p)
    2037              : {
    2038              :   GEN L;
    2039           42 :   if (!BPSW_psp(p)) return NULL;
    2040           77 :   if (!dvdii(nf_get_index(nf), p) &&
    2041           49 :       !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
    2042           28 :   L = idealprimedec(nf, p);
    2043           28 :   return (lg(L) == 2 && pr_get_e(gel(L,1)) == 1)? gel(L,1): NULL;
    2044              : }
    2045              : /* true nf */
    2046              : static GEN
    2047           21 : idealismaximal_mat(GEN nf, GEN x)
    2048              : {
    2049              :   GEN p, c, L;
    2050              :   long i, l, f;
    2051           21 :   x = Q_primitive_part(x, &c);
    2052           21 :   p = gcoeff(x,1,1);
    2053           21 :   if (c)
    2054              :   {
    2055            7 :     if (typ(c) == t_FRAC || !equali1(p)) return NULL;
    2056            7 :     return idealismaximal_int(nf, c);
    2057              :   }
    2058           14 :   if (!BPSW_psp(p)) return NULL;
    2059           14 :   l = lg(x); f = 1;
    2060           35 :   for (i = 2; i < l; i++)
    2061              :   {
    2062           21 :     c = gcoeff(x,i,i);
    2063           21 :     if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
    2064              :   }
    2065           14 :   L = idealprimedec_limit_f(nf, p, f);
    2066           28 :   for (i = lg(L)-1; i; i--)
    2067              :   {
    2068           28 :     GEN pr = gel(L,i);
    2069           28 :     if (pr_get_f(pr) != f) break;
    2070           28 :     if (idealval(nf, x, pr) == 1) return pr;
    2071              :   }
    2072            0 :   return NULL;
    2073              : }
    2074              : /* true nf */
    2075              : static GEN
    2076           77 : idealismaximal_i(GEN nf, GEN x)
    2077              : {
    2078              :   GEN L, p, pr, c;
    2079              :   long i, l;
    2080           77 :   switch(idealtyp(&x, NULL))
    2081              :   {
    2082            7 :     case id_PRIME: return x;
    2083           21 :     case id_MAT: return idealismaximal_mat(nf, x);
    2084              :   }
    2085              :   /* id_PRINCIPAL */
    2086           49 :   x = nf_to_scalar_or_basis(nf, x);
    2087           49 :   switch(typ(x))
    2088              :   {
    2089           35 :     case t_INT: return idealismaximal_int(nf, absi_shallow(x));
    2090            0 :     case t_FRAC: return NULL;
    2091              :   }
    2092           14 :   x = Q_primitive_part(x, &c);
    2093           14 :   if (c) return NULL;
    2094           14 :   p = zkmultable_capZ(zk_multable(nf, x));
    2095           14 :   if (!BPSW_psp(p)) return NULL;
    2096            7 :   L = idealprimedec(nf, p); l = lg(L); pr = NULL;
    2097           21 :   for (i = 1; i < l; i++)
    2098              :   {
    2099           14 :     long v = ZC_nfval(x, gel(L,i));
    2100           14 :     if (v > 1 || (v && pr)) return NULL;
    2101           14 :     pr = gel(L,i);
    2102              :   }
    2103            7 :   return pr;
    2104              : }
    2105              : GEN
    2106           77 : idealismaximal(GEN nf, GEN x)
    2107              : {
    2108           77 :   pari_sp av = avma;
    2109           77 :   x = idealismaximal_i(checknf(nf), x);
    2110           77 :   if (!x) { set_avma(av); return gen_0; }
    2111           49 :   return gc_GEN(av, x);
    2112              : }
    2113              : 
    2114              : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    2115              :  *
    2116              :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    2117              :  * nf[5][7] = same in 2-elt form.
    2118              :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    2119              : GEN
    2120       212260 : idealHNF_inv_Z(GEN nf, GEN I)
    2121              : {
    2122       212260 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    2123       212260 :   if (isint1(IZ)) return matid(lg(I)-1);
    2124       197797 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    2125              :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    2126              :   * missing content cancels while solving the linear equation */
    2127       197799 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    2128       197799 :   return ZM_hnfmodid(dual, IZ);
    2129              : }
    2130              : /* I HNF with rational coefficients (denominator d). */
    2131              : GEN
    2132        69867 : idealHNF_inv(GEN nf, GEN I)
    2133              : {
    2134        69867 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    2135        69867 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    2136        69867 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    2137              : }
    2138              : 
    2139              : /* return p * P^(-1)  [integral] */
    2140              : GEN
    2141        38687 : pr_inv_p(GEN pr)
    2142              : {
    2143        38687 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    2144        38015 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    2145              : }
    2146              : GEN
    2147        17864 : pr_inv(GEN pr)
    2148              : {
    2149        17864 :   GEN p = pr_get_p(pr);
    2150        17864 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    2151        17591 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    2152              : }
    2153              : 
    2154              : GEN
    2155       115839 : idealinv(GEN nf, GEN x)
    2156              : {
    2157              :   GEN res, ax;
    2158              :   pari_sp av;
    2159       115839 :   long tx = idealtyp(&x,&ax), N;
    2160              : 
    2161       115839 :   res = ax? cgetg(3,t_VEC): NULL;
    2162       115839 :   nf = checknf(nf); av = avma;
    2163       115839 :   N = nf_get_degree(nf);
    2164       115839 :   switch (tx)
    2165              :   {
    2166        62967 :     case id_MAT:
    2167        62967 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    2168        62967 :       x = idealHNF_inv(nf,x); break;
    2169        35840 :     case id_PRINCIPAL:
    2170        35840 :       x = nf_to_scalar_or_basis(nf, x);
    2171        35840 :       if (typ(x) != t_COL)
    2172        35791 :         x = idealhnf_principal(nf,ginv(x));
    2173              :       else
    2174              :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    2175              :         GEN c, d;
    2176           49 :         x = Q_remove_denom(x, &c);
    2177           49 :         x = zk_inv(nf, x);
    2178           49 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    2179           49 :         if (!d) /* x and x^(-1) integral => x a unit */
    2180           14 :           x = c? scalarmat(c, N): matid(N);
    2181              :         else
    2182              :         {
    2183           35 :           c = c? gdiv(c,d): ginv(d);
    2184           35 :           x = zk_multable(nf, x);
    2185           35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    2186              :         }
    2187              :       }
    2188        35840 :       break;
    2189        17032 :     case id_PRIME:
    2190        17032 :       x = pr_inv(x); break;
    2191              :   }
    2192       115839 :   x = gc_upto(av,x); if (!ax) return x;
    2193        20407 :   gel(res,1) = x;
    2194        20407 :   gel(res,2) = ext_inv(nf, ax); return res;
    2195              : }
    2196              : 
    2197              : /* write x = A/B, A,B coprime integral ideals */
    2198              : GEN
    2199       389740 : idealnumden(GEN nf, GEN x)
    2200              : {
    2201       389740 :   pari_sp av = avma;
    2202              :   GEN x0, c, d, A, B, J;
    2203       389740 :   long tx = idealtyp(&x, NULL);
    2204       389740 :   nf = checknf(nf);
    2205       389744 :   switch (tx)
    2206              :   {
    2207            7 :     case id_PRIME:
    2208            7 :       retmkvec2(idealhnf(nf, x), gen_1);
    2209       148183 :     case id_PRINCIPAL:
    2210              :     {
    2211              :       GEN xZ, mx;
    2212       148183 :       x = nf_to_scalar_or_basis(nf, x);
    2213       148183 :       switch(typ(x))
    2214              :       {
    2215        88060 :         case t_INT: return gc_GEN(av, mkvec2(absi_shallow(x),gen_1));
    2216         2639 :         case t_FRAC:return gc_GEN(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    2217              :       }
    2218              :       /* t_COL */
    2219        57484 :       x = Q_remove_denom(x, &d);
    2220        57484 :       if (!d) return gc_GEN(av, mkvec2(idealhnf_shallow(nf, x), gen_1));
    2221          105 :       mx = zk_multable(nf, x);
    2222          105 :       xZ = zkmultable_capZ(mx);
    2223          105 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    2224          105 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    2225          105 :       break;
    2226              :     }
    2227       241554 :     default: /* id_MAT */
    2228              :     {
    2229       241554 :       long n = lg(x)-1;
    2230       241554 :       if (n == 0) return mkvec2(gen_0, gen_1);
    2231       241554 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    2232       241554 :       x0 = x = Q_remove_denom(x, &d);
    2233       241551 :       if (!d) return gc_GEN(av, mkvec2(x, gen_1));
    2234           21 :       break;
    2235              :     }
    2236              :   }
    2237          126 :   J = hnfmodid(x, d); /* = d/B */
    2238          126 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    2239          126 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    2240          126 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    2241          126 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    2242          126 :   A = ZM_Z_divexact(A, d); /* = A ! */
    2243          126 :   return gc_GEN(av, mkvec2(A, B));
    2244              : }
    2245              : 
    2246              : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    2247              :  * nf = true nf */
    2248              : static GEN
    2249      1309169 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    2250              : {
    2251      1309169 :   GEN p = pr_get_p(pr), q, gen;
    2252              : 
    2253      1309154 :   *pc = NULL;
    2254      1309154 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    2255              :   {
    2256       636176 :     q = p;
    2257       636176 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    2258              :     {
    2259            0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    2260            0 :       return mkvec2(gen_1,gen_0);
    2261              :     }
    2262       636158 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    2263              :     else
    2264              :     {
    2265       170800 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    2266       170817 :       *pc = ginv(p);
    2267              :     }
    2268              :   }
    2269       673052 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    2270              :   else
    2271              :   {
    2272       366119 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    2273       366125 :     GEN r, m = truedvmdis(n, e, &r);
    2274       366098 :     if (e * f == nf_get_degree(nf))
    2275              :     { /* pr^e = (p) */
    2276        76661 :       if (signe(m)) *pc = powii(p,m);
    2277        76662 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    2278        36563 :       q = p;
    2279        36563 :       gen = nfpow(nf, pr_get_gen(pr), r);
    2280              :     }
    2281              :     else
    2282              :     {
    2283       289449 :       m = absi_shallow(m);
    2284       289452 :       if (signe(r)) m = addiu(m,1);
    2285       289452 :       q = powii(p,m); /* m = ceil(|n|/e) */
    2286       289459 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    2287              :       else
    2288              :       {
    2289        43151 :         gen = pr_get_tau(pr);
    2290        43151 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    2291        43151 :         n = negi(n);
    2292        43151 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    2293        43148 :         *pc = ginv(q);
    2294              :       }
    2295              :     }
    2296       326026 :     gen = FpC_red(gen, q);
    2297              :   }
    2298       962172 :   return mkvec2(q, gen);
    2299              : }
    2300              : 
    2301              : /* True nf. x * pr^n. Assume x in HNF or scalar (possibly nonintegral) */
    2302              : GEN
    2303       828044 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2304              : {
    2305              :   GEN c, cx, y;
    2306       828044 :   long N = nf_get_degree(nf);
    2307              : 
    2308       828031 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    2309              : 
    2310              :   /* inert, special cased for efficiency */
    2311       828024 :   if (pr_is_inert(pr))
    2312              :   {
    2313        76993 :     GEN q = powii(pr_get_p(pr), n);
    2314        74941 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    2315       151931 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    2316              :   }
    2317              : 
    2318       751037 :   y = idealpowprime(nf, pr, n, &c);
    2319       751012 :   if (typ(x) == t_MAT)
    2320       748669 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    2321              :   else
    2322         2343 :   { cx = x; x = NULL; }
    2323       750812 :   cx = mul_content(c,cx);
    2324       750837 :   if (x)
    2325       574409 :     x = idealHNF_mul_two(nf,x,y);
    2326              :   else
    2327       176428 :     x = idealhnf_two(nf,y);
    2328       751221 :   if (cx) x = ZM_Q_mul(x,cx);
    2329       750910 :   return x;
    2330              : }
    2331              : GEN
    2332        13007 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2333              : {
    2334        13007 :   return idealmulpowprime(nf,x,pr, negi(n));
    2335              : }
    2336              : 
    2337              : /* nf = true nf */
    2338              : static GEN
    2339       942139 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    2340              : {
    2341       942139 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    2342       942139 :   long N = degpol(T), s = signe(n);
    2343       942140 :   if (!s) return matid(N);
    2344       927137 :   switch(tx)
    2345              :   {
    2346        75528 :     case id_PRINCIPAL:
    2347        75528 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    2348       656634 :     case id_PRIME:
    2349       656634 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    2350       558113 :       x = idealpowprime(nf, x, n, &cx);
    2351       558101 :       x = idealhnf_two(nf,x);
    2352       558120 :       return cx? ZM_Q_mul(x, cx): x;
    2353       194975 :     default:
    2354       194975 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    2355        69173 :       n1 = (s < 0)? negi(n): n;
    2356              : 
    2357        69173 :       x = Q_primitive_part(x, &cx);
    2358        69173 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    2359        69173 :       alpha = nfpow(nf,alpha,n1);
    2360        69173 :       m = zk_scalar_or_multable(nf, alpha);
    2361        69173 :       if (typ(m) == t_INT) {
    2362          553 :         x = gcdii(powii(a,n1), m);
    2363          553 :         if (s<0) x = ginv(x);
    2364          553 :         if (cx) x = gmul(x, powgi(cx,n));
    2365          553 :         x = scalarmat(x, N);
    2366              :       }
    2367              :       else
    2368              :       { /* could use gcdii(powii(a,n1), zkmultable_capZ(m)), but costly */
    2369        68620 :         x = ZM_hnfmodid(m, powii(a,n1));
    2370        68620 :         if (cx) cx = powgi(cx,n);
    2371        68620 :         if (s<0) {
    2372            7 :           GEN xZ = gcoeff(x,1,1);
    2373            7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    2374            7 :           x = idealHNF_inv_Z(nf,x);
    2375              :         }
    2376        68620 :         if (cx) x = ZM_Q_mul(x, cx);
    2377              :       }
    2378        69173 :       return x;
    2379              :   }
    2380              : }
    2381              : 
    2382              : /* raise the ideal x to the power n (in Z) */
    2383              : GEN
    2384       942140 : idealpow(GEN nf, GEN x, GEN n)
    2385              : {
    2386              :   pari_sp av;
    2387              :   long tx;
    2388              :   GEN res, ax;
    2389              : 
    2390       942140 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2391       942140 :   tx = idealtyp(&x,&ax);
    2392       942141 :   res = ax? cgetg(3,t_VEC): NULL;
    2393       942141 :   av = avma;
    2394       942141 :   x = gc_upto(av, idealpow_aux(checknf(nf), x, tx, n));
    2395       942140 :   if (!ax) return x;
    2396            0 :   gel(res,1) = x;
    2397            0 :   gel(res,2) = ext_pow(nf, ax, n);
    2398            0 :   return res;
    2399              : }
    2400              : 
    2401              : /* Return ideal^e in number field nf. e is a C integer. */
    2402              : GEN
    2403       313385 : idealpows(GEN nf, GEN ideal, long e)
    2404              : {
    2405       313385 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2406       313385 :   affsi(e,court); return idealpow(nf,ideal,court);
    2407              : }
    2408              : 
    2409              : static GEN
    2410        28606 : _idealmulred(GEN nf, GEN x, GEN y)
    2411        28606 : { return idealred(nf,idealmul(nf,x,y)); }
    2412              : static GEN
    2413        35759 : _idealsqrred(GEN nf, GEN x)
    2414        35759 : { return idealred(nf,idealsqr(nf,x)); }
    2415              : static GEN
    2416        11385 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2417              : static GEN
    2418        35759 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2419              : 
    2420              : /* compute x^n (x ideal, n integer), reducing along the way */
    2421              : GEN
    2422        80385 : idealpowred(GEN nf, GEN x, GEN n)
    2423              : {
    2424        80385 :   pari_sp av = avma, av2;
    2425              :   long s;
    2426              :   GEN y;
    2427              : 
    2428        80385 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2429        80385 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2430        80385 :   y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
    2431        80385 :   av2 = avma;
    2432        80385 :   if (s < 0) y = idealinv(nf,y);
    2433        80385 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2434        80386 :   return avma == av2? gc_GEN(av,y): gc_upto(av,y);
    2435              : }
    2436              : 
    2437              : GEN
    2438        17221 : idealmulred(GEN nf, GEN x, GEN y)
    2439              : {
    2440        17221 :   pari_sp av = avma;
    2441        17221 :   return gc_upto(av, _idealmulred(nf,x,y));
    2442              : }
    2443              : 
    2444              : long
    2445           91 : isideal(GEN nf,GEN x)
    2446              : {
    2447           91 :   long N, i, j, lx, tx = typ(x);
    2448              :   pari_sp av;
    2449              :   GEN T, xZ;
    2450              : 
    2451           91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2452           91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2453           91 :   switch(tx)
    2454              :   {
    2455           14 :     case t_INT: case t_FRAC: return 1;
    2456            7 :     case t_POL: return varn(x) == varn(T);
    2457            7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2458           14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2459           42 :     case t_MAT: break;
    2460            7 :     default: return 0;
    2461              :   }
    2462           42 :   N = degpol(T);
    2463           42 :   if (lx-1 != N) return (lx == 1);
    2464           28 :   if (nbrows(x) != N) return 0;
    2465              : 
    2466           28 :   av = avma; x = Q_primpart(x);
    2467           28 :   if (!ZM_ishnf(x)) return 0;
    2468           14 :   xZ = gcoeff(x,1,1);
    2469           21 :   for (j=2; j<=N; j++)
    2470           14 :     if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
    2471           14 :   for (i=2; i<=N; i++)
    2472           14 :     for (j=2; j<=N; j++)
    2473            7 :        if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
    2474            7 :   return gc_long(av,1);
    2475              : }
    2476              : 
    2477              : GEN
    2478        39744 : idealdiv(GEN nf, GEN x, GEN y)
    2479              : {
    2480        39744 :   pari_sp av = avma;
    2481        39744 :   return gc_upto(av, idealmul(nf, x, idealinv(nf,y)));
    2482              : }
    2483              : 
    2484              : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2485              :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2486              :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2487              :  *
    2488              :  *   x + (Nx/Nz)    x
    2489              :  *   ----------- = ---
    2490              :  *   y + (Ny/Nz)    y
    2491              :  *
    2492              :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2493              :  *
    2494              :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2495              :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2496              :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2497              :  * assumed integral and its norm N(x/y) is coprime to p.
    2498              :  *
    2499              :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2500              :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2501              :  *
    2502              :  *                Peter Montgomery.  July, 1994. */
    2503              : static void
    2504            7 : err_divexact(GEN x, GEN y)
    2505            7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2506            0 :                   gen_1,mkvec2(x,y)); }
    2507              : GEN
    2508         5263 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2509              : {
    2510         5263 :   pari_sp av = avma;
    2511              :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2512              : 
    2513         5263 :   nf = checknf(nf);
    2514         5263 :   x = idealhnf_shallow(nf, x0);
    2515         5263 :   y = idealhnf_shallow(nf, y0);
    2516         5263 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2517         5256 :   if (lg(x) == 1) retgc_const(av, cgetg(1, t_MAT)); /* numerator is zero */
    2518         5256 :   y = Q_primitive_part(y, &cy);
    2519         5256 :   if (cy) x = RgM_Rg_div(x,cy);
    2520         5256 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2521         5249 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gc_GEN(av, x);
    2522         2870 :   Nx = idealnorm(nf,x);
    2523         2870 :   Ny = idealnorm(nf,y);
    2524         2870 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2525         2870 :   q = dvmdii(Nx,Ny, &r);
    2526         2870 :   if (signe(r)) err_divexact(x,y);
    2527         2870 :   if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
    2528              :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2529          616 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2530          533 :   {
    2531         1149 :     GEN p1 = gcdii(Nz, q);
    2532         1149 :     if (is_pm1(p1)) break;
    2533          533 :     Nz = diviiexact(Nz,p1);
    2534          533 :     q = mulii(q,p1);
    2535              :   }
    2536          616 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2537          616 :   if (!equalii(xZ,q))
    2538              :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2539          468 :     x = ZM_hnfmodid(x, q);
    2540              :     /* y reduced to unit ideal ? */
    2541          468 :     if (Nz == Ny) return gc_upto(av, x);
    2542              : 
    2543          146 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2544          146 :     y = ZM_hnfmodid(y, q);
    2545              :   }
    2546          294 :   yZ = gcoeff(y,1,1);
    2547          294 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2548          294 :   return gc_upto(av, ZM_Z_divexact(y, yZ));
    2549              : }
    2550              : 
    2551              : GEN
    2552           21 : idealintersect(GEN nf, GEN x, GEN y)
    2553              : {
    2554           21 :   pari_sp av = avma;
    2555              :   GEN z, dx, dy;
    2556              : 
    2557           21 :   nf = checknf(nf);
    2558           21 :   x = idealhnf_shallow(nf,x);
    2559           21 :   y = idealhnf_shallow(nf,y);
    2560           21 :   if (lg(x) == 1 || lg(y) == 1) retgc_const(av, cgetg(1, t_MAT));
    2561           14 :   x = Q_remove_denom(x, &dx);
    2562           14 :   y = Q_remove_denom(y, &dy);
    2563           14 :   if (dx) y = ZM_Z_mul(y, dx);
    2564           14 :   if (dy) x = ZM_Z_mul(x, dy);
    2565           14 :   dx = mul_denom(dx,dy);
    2566           14 :   z = ZM_hnfintersectmod(x,y, lcmii(gcoeff(x,1,1), gcoeff(y,1,1)));
    2567           14 :   if (dx) z = RgM_Rg_div(z,dx);
    2568           14 :   return gc_upto(av,z);
    2569              : }
    2570              : 
    2571              : /*******************************************************************/
    2572              : /*                                                                 */
    2573              : /*                      T2-IDEAL REDUCTION                         */
    2574              : /*                                                                 */
    2575              : /*******************************************************************/
    2576              : 
    2577              : static GEN
    2578           21 : chk_vdir(GEN nf, GEN vdir)
    2579              : {
    2580           21 :   long i, l = lg(vdir);
    2581              :   GEN v;
    2582           21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2583           14 :   switch(typ(vdir))
    2584              :   {
    2585            0 :     case t_VECSMALL: return vdir;
    2586           14 :     case t_VEC: break;
    2587            0 :     default: pari_err_TYPE("idealred",vdir);
    2588              :   }
    2589           14 :   v = cgetg(l, t_VECSMALL);
    2590           56 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2591           14 :   return v;
    2592              : }
    2593              : 
    2594              : static void
    2595        12709 : twistG(GEN G, long r1, long i, long v)
    2596              : {
    2597        12709 :   long j, lG = lg(G);
    2598        12709 :   if (i <= r1) {
    2599        37275 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2600              :   } else {
    2601          648 :     long k = (i<<1) - r1;
    2602         4640 :     for (j=1; j<lG; j++)
    2603              :     {
    2604         3992 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2605         3992 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2606              :     }
    2607              :   }
    2608        12709 : }
    2609              : 
    2610              : GEN
    2611       139185 : nf_get_Gtwist(GEN nf, GEN vdir)
    2612              : {
    2613              :   long i, l, v, r1;
    2614              :   GEN G;
    2615              : 
    2616       139185 :   if (!vdir) return nf_get_roundG(nf);
    2617           21 :   if (typ(vdir) == t_MAT)
    2618              :   {
    2619            0 :     long N = nf_get_degree(nf);
    2620            0 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2621            0 :     return vdir;
    2622              :   }
    2623           21 :   vdir = chk_vdir(nf, vdir);
    2624           14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2625           14 :   r1 = nf_get_r1(nf);
    2626           14 :   l = lg(vdir);
    2627           56 :   for (i=1; i<l; i++)
    2628              :   {
    2629           42 :     v = vdir[i]; if (!v) continue;
    2630           42 :     twistG(G, r1, i, v);
    2631              :   }
    2632           14 :   return RM_round_maxrank(G);
    2633              : }
    2634              : GEN
    2635        12667 : nf_get_Gtwist1(GEN nf, long i)
    2636              : {
    2637        12667 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2638        12667 :   long r1 = nf_get_r1(nf);
    2639        12667 :   twistG(G, r1, i, 10);
    2640        12667 :   return RM_round_maxrank(G);
    2641              : }
    2642              : 
    2643              : GEN
    2644        98529 : RM_round_maxrank(GEN G0)
    2645              : {
    2646        98529 :   long e, r = lg(G0)-1;
    2647        98529 :   pari_sp av = avma;
    2648        98529 :   for (e = 4; ; e <<= 1, set_avma(av))
    2649            0 :   {
    2650        98529 :     GEN G = gmul2n(G0, e), H = ground(G);
    2651        98528 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2652              :   }
    2653              : }
    2654              : 
    2655              : GEN
    2656       139178 : idealred0(GEN nf, GEN I, GEN vdir)
    2657              : {
    2658       139178 :   pari_sp av = avma;
    2659       139178 :   GEN G, aI, IZ, J, y, my, dyi, yi, c1 = NULL;
    2660              :   long N;
    2661              : 
    2662       139178 :   nf = checknf(nf);
    2663       139178 :   N = nf_get_degree(nf);
    2664              :   /* put first for sanity checks, unused when I obviously principal */
    2665       139178 :   G = nf_get_Gtwist(nf, vdir);
    2666       139171 :   switch (idealtyp(&I,&aI))
    2667              :   {
    2668        37341 :     case id_PRIME:
    2669        37341 :       if (pr_is_inert(I)) {
    2670          585 :         if (!aI) { set_avma(av); return matid(N); }
    2671          585 :         c1 = gel(I,1); I = matid(N);
    2672          585 :         goto END;
    2673              :       }
    2674        36756 :       IZ = pr_get_p(I);
    2675        36756 :       J = pr_inv_p(I);
    2676        36757 :       I = idealhnf_two(nf,I);
    2677        36757 :       break;
    2678       101802 :     case id_MAT:
    2679       101802 :       if (lg(I)-1 != N) pari_err_DIM("idealred");
    2680       101795 :       I = Q_primitive_part(I, &c1);
    2681       101795 :       IZ = gcoeff(I,1,1);
    2682       101795 :       if (is_pm1(IZ))
    2683              :       {
    2684         9083 :         if (!aI) { set_avma(av); return matid(N); }
    2685         8999 :         goto END;
    2686              :       }
    2687        92712 :       J = idealHNF_inv_Z(nf, I);
    2688        92712 :       break;
    2689           21 :     default: /* id_PRINCIPAL, silly case */
    2690           21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2691           21 :       if (!aI) return I;
    2692           14 :       goto END;
    2693              :   }
    2694              :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2695       129469 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2696       129469 :   if (ZV_isscalar(y))
    2697              :   { /* already reduced */
    2698        71085 :     if (!aI) return gc_GEN(av, I);
    2699        67907 :     goto END;
    2700              :   }
    2701              : 
    2702        58384 :   my = zk_multable(nf, y);
    2703        58383 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2704        58382 :   c1 = mul_content(c1, IZ);
    2705        58382 :   if (equali1(c1)) c1 = NULL; /* can be simplified with IZ */
    2706        58382 :   yi = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2707        58384 :   dyi = Q_denom(yi); /* generates (y) \cap Z */
    2708        58384 :   I = hnfmodid(I, dyi);
    2709        58384 :   if (!aI) return gc_upto(av, I);
    2710        56389 :   if (typ(aI) == t_MAT)
    2711              :   {
    2712        39309 :     GEN nyi = Q_muli_to_int(yi, dyi);
    2713        39309 :     if (gexpo(nyi) >= gexpo(y))
    2714        20830 :       aI = famat_div(aI, y); /* yi "larger" than y, keep the latter */
    2715              :     else
    2716              :     { /* use yi */
    2717        18479 :       aI = famat_mul(aI, nyi);
    2718        18479 :       c1 = div_content(c1, dyi);
    2719              :     }
    2720        39309 :     if (c1) { aI = famat_mul(aI, Q_to_famat(c1)); c1 = NULL; }
    2721              :   }
    2722              :   else
    2723        17080 :     c1 = c1? RgC_Rg_mul(yi, c1): yi;
    2724       133894 : END:
    2725       133894 :   if (c1) aI = ext_mul(nf, aI,c1);
    2726       133892 :   return gc_GEN(av, mkvec2(I, aI));
    2727              : }
    2728              : 
    2729              : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
    2730              :  * T2 matrix. Reduce I wrt G */
    2731              : GEN
    2732      1346022 : idealpseudored(GEN I, GEN G)
    2733      1346022 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
    2734              : 
    2735              : /* Same I, G; m in I with T2(m) small */
    2736              : GEN
    2737       142664 : idealpseudomin(GEN I, GEN G)
    2738              : {
    2739       142664 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2740       142662 :   return ZM_ZC_mul(I, gel(u,1));
    2741              : }
    2742              : /* Same I,G; irrational m in I with T2(m) small */
    2743              : GEN
    2744            0 : idealpseudomin_nonscalar(GEN I, GEN G)
    2745              : {
    2746            0 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2747            0 :   GEN m = ZM_ZC_mul(I, gel(u,1));
    2748            0 :   if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
    2749            0 :   return m;
    2750              : }
    2751              : /* Same I,G; t_VEC of irrational m in I with T2(m) small */
    2752              : GEN
    2753      1254115 : idealpseudominvec(GEN I, GEN G)
    2754              : {
    2755      1254115 :   long i, j, k, n = lg(I)-1;
    2756      1254115 :   GEN x, L, b = idealpseudored(I, G);
    2757      1254114 :   L = cgetg(1 + (n*(n+1))/2, t_VEC);
    2758      4418490 :   for (i = k = 1; i <= n; i++)
    2759              :   {
    2760      3164376 :     x = gel(b,i);
    2761      3164376 :     if (!ZV_isscalar(x)) gel(L,k++) = x;
    2762              :   }
    2763      3164375 :   for (i = 2; i <= n; i++)
    2764              :   {
    2765      1910261 :     long J = minss(i, 4);
    2766      4733589 :     for (j = 1; j < J; j++)
    2767              :     {
    2768      2823328 :       x = ZC_add(gel(b,i),gel(b,j));
    2769      2823328 :       if (!ZV_isscalar(x)) gel(L,k++) = x;
    2770              :     }
    2771              :   }
    2772      1254114 :   setlg(L,k); return L;
    2773              : }
    2774              : 
    2775              : GEN
    2776        13188 : idealred_elt(GEN nf, GEN I)
    2777              : {
    2778        13188 :   pari_sp av = avma;
    2779        13188 :   GEN u = idealpseudomin(I, nf_get_roundG(nf));
    2780        13188 :   return gc_upto(av, u);
    2781              : }
    2782              : 
    2783              : GEN
    2784            7 : idealmin(GEN nf, GEN x, GEN vdir)
    2785              : {
    2786            7 :   pari_sp av = avma;
    2787              :   GEN y, dx;
    2788            7 :   nf = checknf(nf);
    2789            7 :   switch( idealtyp(&x, NULL) )
    2790              :   {
    2791            0 :     case id_PRINCIPAL: return gcopy(x);
    2792            0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2793            7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2794              :   }
    2795            7 :   x = Q_remove_denom(x, &dx);
    2796            7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2797            7 :   if (dx) y = RgC_Rg_div(y, dx);
    2798            7 :   return gc_upto(av, y);
    2799              : }
    2800              : 
    2801              : /*******************************************************************/
    2802              : /*                                                                 */
    2803              : /*                   APPROXIMATION THEOREM                         */
    2804              : /*                                                                 */
    2805              : /*******************************************************************/
    2806              : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2807              :  * and ppo(a,b) = Z_ppo(a,b) */
    2808              : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2809              : GEN
    2810       986167 : Z_ppio(GEN a, GEN b)
    2811              : {
    2812       986167 :   GEN x, y, d = gcdii(a,b);
    2813       986167 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2814       757533 :   x = d; y = diviiexact(a,d);
    2815              :   for(;;)
    2816       131117 :   {
    2817       888650 :     GEN g = gcdii(x,y);
    2818       888650 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2819       131117 :     x = mulii(x,g); y = diviiexact(y,g);
    2820              :   }
    2821              : }
    2822              : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2823              :  * and pple all others */
    2824              : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2825              : GEN
    2826            0 : Z_ppgle(GEN a, GEN b)
    2827              : {
    2828            0 :   GEN x, y, g, d = gcdii(a,b);
    2829            0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2830            0 :   x = diviiexact(a,d); y = d;
    2831              :   for(;;)
    2832              :   {
    2833            0 :     g = gcdii(x,y);
    2834            0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2835            0 :     x = mulii(x,g); y = diviiexact(y,g);
    2836              :   }
    2837              : }
    2838              : static void
    2839            0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2840              : {
    2841              :   GEN x, r, v, g, h, c, c0;
    2842              :   long n;
    2843            0 :   if (is_pm1(b)) {
    2844            0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2845            0 :     return;
    2846              :   }
    2847            0 :   v = Z_ppio(a,b);
    2848            0 :   a = gel(v,2);
    2849            0 :   r = gel(v,3);
    2850            0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2851            0 :   v = Z_ppgle(a,b);
    2852            0 :   g = gel(v,1);
    2853            0 :   h = gel(v,2);
    2854            0 :   x = c0 = gel(v,3);
    2855            0 :   for (n = 1; !is_pm1(h); n++)
    2856              :   {
    2857              :     GEN d, y;
    2858              :     long i;
    2859            0 :     v = Z_ppgle(h,sqri(g));
    2860            0 :     g = gel(v,1);
    2861            0 :     h = gel(v,2);
    2862            0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2863            0 :     d = gcdii(c,b);
    2864            0 :     x = mulii(x,d);
    2865            0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2866            0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2867              :   }
    2868            0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2869              : }
    2870              : static GEN
    2871      6828640 : Z_cba_rec(GEN L, GEN a, GEN b)
    2872              : {
    2873              :   GEN g;
    2874              :   /* a few naive steps before switching to dcba */
    2875      6828640 :   if (lg(L) > 10) { Z_dcba_rec(L, a, b); return veclast(L); }
    2876      6828640 :   if (is_pm1(a)) return b;
    2877      4062408 :   g = gcdii(a,b);
    2878      4062408 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2879      3035634 :   a = diviiexact(a,g);
    2880      3035634 :   b = diviiexact(b,g);
    2881      3035634 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2882              : }
    2883              : GEN
    2884       757372 : Z_cba(GEN a, GEN b)
    2885              : {
    2886       757372 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2887       757372 :   GEN t = Z_cba_rec(L, a, b);
    2888       757372 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2889       757372 :   return L;
    2890              : }
    2891              : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2892              : GEN
    2893           49 : ZV_cba_extend(GEN P, GEN b)
    2894              : {
    2895           49 :   long i, l = lg(P);
    2896           49 :   GEN w = cgetg(l+1, t_VEC);
    2897          175 :   for (i = 1; i < l; i++)
    2898              :   {
    2899          126 :     GEN v = Z_cba(gel(P,i), b);
    2900          126 :     long nv = lg(v)-1;
    2901          126 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2902          126 :     b = gel(v,nv);
    2903              :   }
    2904           49 :   gel(w,l) = b; return shallowconcat1(w);
    2905              : }
    2906              : GEN
    2907           28 : ZV_cba(GEN v)
    2908              : {
    2909           28 :   long i, l = lg(v);
    2910              :   GEN P;
    2911           28 :   if (l <= 2) return v;
    2912           14 :   P = Z_cba(gel(v,1), gel(v,2));
    2913           42 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2914           14 :   return P;
    2915              : }
    2916              : 
    2917              : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2918              : GEN
    2919     19280335 : Z_ppo(GEN x, GEN f)
    2920              : {
    2921     19280335 :   (void)Z_pvalrem(x, f, &x);
    2922              :   for (;;)
    2923              :   {
    2924     58205170 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2925     38925241 :     x = diviiexact(x, f);
    2926              :   }
    2927     19279967 :   return x;
    2928              : }
    2929              : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2930              : ulong
    2931     70518377 : u_ppo(ulong x, ulong f)
    2932              : {
    2933              :   for (;;)
    2934              :   {
    2935     70518377 :     f = ugcd(x, f); if (f == 1) break;
    2936     16212790 :     x /= f;
    2937              :   }
    2938     54305543 :   return x;
    2939              : }
    2940              : 
    2941              : /* result known to be representable as an ulong */
    2942              : static ulong
    2943      1645036 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
    2944              : 
    2945              : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
    2946              :  * set *pd = gcd(x,N) */
    2947              : ulong
    2948      5915420 : Fl_invgen(ulong x, ulong N, ulong *pd)
    2949              : {
    2950              :   ulong d, d0, e, v, v1;
    2951              :   long s;
    2952      5915420 :   *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
    2953      5916274 :   if (s > 0) v = N - v;
    2954      5916274 :   if (d == 1) return v;
    2955              :   /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
    2956      2758066 :   e = N / d;
    2957      2758066 :   d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
    2958      2758190 :   if (d0 == 1) return v;
    2959      1644994 :   e = lcmuu(e, d / d0);
    2960      1645036 :   return u_chinese_coprime(v, 1, e, d0, e*d0);
    2961              : }
    2962              : 
    2963              : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2964              : static GEN
    2965          126 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2966              : {
    2967          126 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2968              :   GEN x1, x2, ex;
    2969              : 
    2970              : #if 0 /*1) via many gcds. Expensive ! */
    2971              :   GEN f = idealprodprime(nf, listpr);
    2972              :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2973              :   x = scalarmat(x, N);
    2974              :   for (;;)
    2975              :   {
    2976              :     if (gequal1(gcoeff(f,1,1))) break;
    2977              :     x = idealdivexact(nf, x, f);
    2978              :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2979              :   }
    2980              :   x2 = x;
    2981              : #else /*2) from prime decomposition */
    2982          126 :   x1 = NULL;
    2983          350 :   for (j=1; j<lp; j++)
    2984              :   {
    2985          224 :     GEN pr = gel(listpr,j);
    2986          224 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2987              : 
    2988          126 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2989          126 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2990          126 :            : idealpow(nf, pr, ex);
    2991              :   }
    2992          126 :   x = scalarmat(x, N);
    2993          126 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2994              : #endif
    2995          126 :   return x2;
    2996              : }
    2997              : 
    2998              : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2999              : GEN
    3000        10920 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    3001              : {
    3002              :   GEN fZ, t, L, D2, d1, d2, d;
    3003              : 
    3004        10920 :   L = Q_remove_denom(L0, &d);
    3005        10920 :   if (!d) return L0;
    3006              : 
    3007              :   /* L0 = L / d, L integral */
    3008          518 :   fZ = gcoeff(f,1,1);
    3009          518 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    3010              :   /* Kill denom part coprime to fZ */
    3011          126 :   d2 = Z_ppo(d, fZ);
    3012          126 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    3013          126 :   if (equalii(d, d2)) return L;
    3014              : 
    3015          126 :   d1 = diviiexact(d, d2);
    3016              :   /* L0 = (L / d1) mod f. d1 not coprime to f
    3017              :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    3018          126 :   D2 = nf_coprime_part(nf, d1, listpr);
    3019          126 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    3020          126 :   L = nfmuli(nf,t,L);
    3021              : 
    3022              :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    3023          126 :   return Q_div_to_int(L, d1); /* exact division */
    3024              : }
    3025              : 
    3026              : /* assume L is a list of prime ideals. Return the product */
    3027              : GEN
    3028          666 : idealprodprime(GEN nf, GEN L)
    3029              : {
    3030          666 :   long l = lg(L), i;
    3031              :   GEN z;
    3032          666 :   if (l == 1) return matid(nf_get_degree(nf));
    3033          666 :   z = pr_hnf(nf, gel(L,1));
    3034          694 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    3035          666 :   return z;
    3036              : }
    3037              : 
    3038              : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    3039              : GEN
    3040         1064 : idealprod(GEN nf, GEN I)
    3041              : {
    3042         1064 :   long i, l = lg(I);
    3043              :   GEN z;
    3044         2450 :   for (i = 1; i < l; i++)
    3045         2443 :     if (!equali1(gel(I,i))) break;
    3046         1064 :   if (i == l) return gen_1;
    3047         1057 :   z = gel(I,i);
    3048         1855 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    3049         1057 :   return z;
    3050              : }
    3051              : 
    3052              : /* v_pr(idealprod(nf,I)) */
    3053              : long
    3054         1917 : idealprodval(GEN nf, GEN I, GEN pr)
    3055              : {
    3056         1917 :   long i, l = lg(I), v = 0;
    3057        10371 :   for (i = 1; i < l; i++)
    3058         8454 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    3059         1917 :   return v;
    3060              : }
    3061              : 
    3062              : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    3063              : GEN
    3064        75625 : factorbackprime(GEN nf, GEN L, GEN e)
    3065              : {
    3066        75625 :   long l = lg(L), i;
    3067              :   GEN z;
    3068              : 
    3069        75625 :   if (l == 1) return matid(nf_get_degree(nf));
    3070        61002 :   z = idealpow(nf, gel(L,1), gel(e,1));
    3071       113931 :   for (i=2; i<l; i++)
    3072        52929 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    3073        61002 :   return z;
    3074              : }
    3075              : 
    3076              : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    3077              :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    3078              : GEN
    3079        58441 : pr_uniformizer(GEN pr, GEN F)
    3080              : {
    3081        58441 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    3082        58441 :   if (!equalii(F, p))
    3083              :   {
    3084        36884 :     long e = pr_get_e(pr);
    3085        36884 :     GEN u, v, q = (e == 1)? sqri(p): p;
    3086        36884 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    3087        36884 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    3088        36884 :     if (pr_is_inert(pr))
    3089           28 :       t = addii(mulii(p, v), u);
    3090              :     else
    3091              :     {
    3092        36856 :       t = ZC_Z_mul(t, v);
    3093        36856 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    3094              :     }
    3095              :   }
    3096        58441 :   return t;
    3097              : }
    3098              : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    3099              : GEN
    3100        81194 : prV_lcm_capZ(GEN L)
    3101              : {
    3102        81194 :   long i, r = lg(L);
    3103              :   GEN F;
    3104        81194 :   if (r == 1) return gen_1;
    3105        68460 :   F = pr_get_p(gel(L,1));
    3106       121852 :   for (i = 2; i < r; i++)
    3107              :   {
    3108        53392 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    3109        53392 :     if (!dvdii(F, p)) F = mulii(F,p);
    3110              :   }
    3111        68460 :   return F;
    3112              : }
    3113              : /* v vector of prid. Return underlying list of rational primes */
    3114              : GEN
    3115        66234 : prV_primes(GEN v)
    3116              : {
    3117        66234 :   long i, l = lg(v);
    3118        66234 :   GEN w = cgetg(l,t_VEC);
    3119       219768 :   for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
    3120        66234 :   return ZV_sort_uniq(w);
    3121              : }
    3122              : 
    3123              : /* Given a prime ideal factorization with possibly zero or negative
    3124              :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    3125              :  * and v_pr(b) >= 0 for all other pr.
    3126              :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    3127              :  * but no support for this yet. If nored, do not reduce result. */
    3128              : static GEN
    3129        54089 : idealapprfact_i(GEN nf, GEN x, int nored)
    3130              : {
    3131        54089 :   GEN d = NULL, z, L, e, e2, F;
    3132              :   long i, r;
    3133        54089 :   int hasden = 0;
    3134              : 
    3135        54089 :   nf = checknf(nf);
    3136        54089 :   L = gel(x,1);
    3137        54089 :   e = gel(x,2);
    3138        54089 :   F = prV_lcm_capZ(L);
    3139        54089 :   z = NULL; r = lg(e);
    3140       136923 :   for (i = 1; i < r; i++)
    3141              :   {
    3142        82834 :     long s = signe(gel(e,i));
    3143              :     GEN pi, q;
    3144        82834 :     if (!s) continue;
    3145        54073 :     if (s < 0) hasden = 1;
    3146        54073 :     pi = pr_uniformizer(gel(L,i), F);
    3147        54073 :     q = nfpow(nf, pi, gel(e,i));
    3148        54073 :     z = z? nfmul(nf, z, q): q;
    3149              :   }
    3150        54089 :   if (!z) return gen_1;
    3151        26994 :   if (hasden) /* denominator */
    3152              :   {
    3153        10103 :     z = Q_remove_denom(z, &d);
    3154        10103 :     d = diviiexact(d, Z_ppo(d, F));
    3155              :   }
    3156        26994 :   if (nored || typ(z) != t_COL) return d? gdiv(z, d): z;
    3157        10103 :   e2 = cgetg(r, t_VEC);
    3158        28673 :   for (i = 1; i < r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    3159        10103 :   x = factorbackprime(nf, L, e2);
    3160        10103 :   if (d) x = RgM_Rg_mul(x, d);
    3161        10103 :   z = ZC_reducemodlll(z, x);
    3162        10103 :   return d? RgC_Rg_div(z,d): z;
    3163              : }
    3164              : 
    3165              : GEN
    3166            0 : idealapprfact(GEN nf, GEN x) {
    3167            0 :   pari_sp av = avma;
    3168            0 :   return gc_upto(av, idealapprfact_i(nf, x, 0));
    3169              : }
    3170              : GEN
    3171           14 : idealappr(GEN nf, GEN x) {
    3172           14 :   pari_sp av = avma;
    3173           14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    3174           14 :   return gc_upto(av, idealapprfact_i(nf, x, 0));
    3175              : }
    3176              : 
    3177              : /* OBSOLETE */
    3178              : GEN
    3179           14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    3180              : 
    3181              : static GEN
    3182           21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    3183              : {
    3184           21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    3185           21 :   long i, r = lg(E);
    3186           84 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    3187           21 :   return idealapprfact_i(nf,F,1);
    3188              : }
    3189              : 
    3190              : static void
    3191           14 : not_in_ideal(GEN a) {
    3192           14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    3193            0 : }
    3194              : /* x integral in HNF, a an 'nf' */
    3195              : static int
    3196           28 : in_ideal(GEN x, GEN a)
    3197              : {
    3198           28 :   switch(typ(a))
    3199              :   {
    3200           14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    3201            7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    3202            7 :     default: return 0;
    3203              :   }
    3204              : }
    3205              : 
    3206              : /* Given an integral ideal x and a in x, gives a b such that
    3207              :  * x = aZ_K + bZ_K using the approximation theorem */
    3208              : GEN
    3209           42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    3210              : {
    3211           42 :   pari_sp av = avma;
    3212              :   GEN cx, b;
    3213              : 
    3214           42 :   nf = checknf(nf);
    3215           42 :   a = nf_to_scalar_or_basis(nf, a);
    3216           42 :   x = idealhnf_shallow(nf,x);
    3217           42 :   if (lg(x) == 1)
    3218              :   {
    3219           14 :     if (!isintzero(a)) not_in_ideal(a);
    3220            7 :     set_avma(av); return gen_0;
    3221              :   }
    3222           28 :   x = Q_primitive_part(x, &cx);
    3223           28 :   if (cx) a = gdiv(a, cx);
    3224           28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    3225           21 :   b = mat_ideal_two_elt2(nf, x, a);
    3226           21 :   if (typ(b) == t_COL)
    3227              :   {
    3228           14 :     GEN mod = idealhnf_principal(nf,a);
    3229           14 :     b = ZC_hnfrem(b,mod);
    3230           14 :     if (ZV_isscalar(b)) b = gel(b,1);
    3231              :   }
    3232              :   else
    3233              :   {
    3234            7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    3235            7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    3236              :   }
    3237           21 :   b = cx? gmul(b,cx): gcopy(b);
    3238           21 :   return gc_upto(av, b);
    3239              : }
    3240              : 
    3241              : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    3242              :  * beta * x is an integral ideal coprime to y */
    3243              : GEN
    3244        37191 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    3245              : {
    3246        37191 :   GEN L = gel(fy,1), e;
    3247        37191 :   long i, r = lg(L);
    3248              : 
    3249        37191 :   e = cgetg(r, t_COL);
    3250        76055 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    3251        37191 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    3252              : }
    3253              : GEN
    3254           84 : idealcoprime(GEN nf, GEN x, GEN y)
    3255              : {
    3256           84 :   pari_sp av = avma;
    3257           84 :   return gc_upto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    3258              : }
    3259              : 
    3260              : GEN
    3261            7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3262              : {
    3263            7 :   pari_sp av = avma;
    3264            7 :   GEN z, p, pr = modpr, T;
    3265              : 
    3266            7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3267            0 :   x = nf_to_Fq(nf,x,modpr);
    3268            0 :   y = nf_to_Fq(nf,y,modpr);
    3269            0 :   z = Fq_mul(x,y,T,p);
    3270            0 :   return gc_upto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3271              : }
    3272              : 
    3273              : GEN
    3274            0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3275              : {
    3276            0 :   pari_sp av = avma;
    3277            0 :   nf = checknf(nf);
    3278            0 :   return gc_upto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    3279              : }
    3280              : 
    3281              : GEN
    3282            0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    3283              : {
    3284            0 :   pari_sp av=avma;
    3285            0 :   GEN z, T, p, pr = modpr;
    3286              : 
    3287            0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3288            0 :   z = nf_to_Fq(nf,x,modpr);
    3289            0 :   z = Fq_pow(z,k,T,p);
    3290            0 :   return gc_upto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3291              : }
    3292              : 
    3293              : GEN
    3294            0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    3295              : {
    3296            0 :   pari_sp av = avma;
    3297            0 :   GEN T, p, pr = modpr;
    3298              : 
    3299            0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3300            0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    3301            0 :   x = nfM_to_FqM(x, nf, modpr);
    3302            0 :   return gc_GEN(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    3303              : }
    3304              : 
    3305              : GEN
    3306            0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    3307              : {
    3308            0 :   const char *f = "nfsolvemodpr";
    3309            0 :   pari_sp av = avma;
    3310              :   GEN T, p, modpr;
    3311              : 
    3312            0 :   nf = checknf(nf);
    3313            0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3314            0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    3315            0 :   a = nfM_to_FqM(a, nf, modpr);
    3316            0 :   switch(typ(b))
    3317              :   {
    3318            0 :     case t_MAT:
    3319            0 :       b = nfM_to_FqM(b, nf, modpr);
    3320            0 :       b = FqM_gauss(a,b,T,p);
    3321            0 :       if (!b) pari_err_INV(f,a);
    3322            0 :       a = FqM_to_nfM(b, modpr);
    3323            0 :       break;
    3324            0 :     case t_COL:
    3325            0 :       b = nfV_to_FqV(b, nf, modpr);
    3326            0 :       b = FqM_FqC_gauss(a,b,T,p);
    3327            0 :       if (!b) pari_err_INV(f,a);
    3328            0 :       a = FqV_to_nfV(b, modpr);
    3329            0 :       break;
    3330            0 :     default: pari_err_TYPE(f,b);
    3331              :   }
    3332            0 :   return gc_GEN(av, a);
    3333              : }
        

Generated by: LCOV version 2.0-1