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 - base5.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25406-bf255ab81b) Lines: 1080 1163 92.9 %
Date: 2020-06-04 05:59:24 Functions: 75 79 94.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                     RNF STRUCTURE AND OPERATIONS                */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : /* eq is an rnfeq; must return a t_POL */
      23             : GEN
      24       84651 : eltreltoabs(GEN eq, GEN x)
      25             : {
      26       84651 :   GEN Pabs = gel(eq,1), a = gel(eq,2), k = gel(eq,3), T = gel(eq,4), b, s;
      27       84651 :   long i, v = varn(Pabs);
      28       84651 :   pari_sp av = avma;
      29             : 
      30       84651 :   if (varncmp(gvar(x), v) > 0) x = scalarpol(x,v);
      31       84651 :   x = RgX_nffix("eltreltoabs", T, x, 1);
      32             :   /* Mod(X - k a, Pabs(X)) is a root of the relative polynomial */
      33       84644 :   if (signe(k))
      34       14511 :     x = RgXQX_translate(x, deg1pol_shallow(negi(k), gen_0, varn(T)), T);
      35       84644 :   b = pol_x(v);
      36       84644 :   s = gen_0;
      37      251020 :   for (i=lg(x)-1; i>1; i--)
      38             :   {
      39      166376 :     GEN c = gel(x,i);
      40      166376 :     if (typ(c) == t_POL) c = RgX_RgXQ_eval(c, a, Pabs);
      41      166376 :     s = RgX_rem(gadd(c, gmul(b,s)), Pabs);
      42             :   }
      43       84644 :   return gerepileupto(av, s);
      44             : }
      45             : GEN
      46      130417 : rnfeltreltoabs(GEN rnf,GEN x)
      47             : {
      48      130417 :   const char *f = "rnfeltreltoabs";
      49             :   GEN pol;
      50      130417 :   checkrnf(rnf);
      51      130417 :   pol = rnf_get_polabs(rnf);
      52      130417 :   switch(typ(x))
      53             :   {
      54       23555 :     case t_INT: return icopy(x);
      55        1869 :     case t_FRAC: return gcopy(x);
      56      101612 :     case t_POLMOD:
      57      101612 :       if (RgX_equal_var(gel(x,1), pol))
      58             :       { /* already in 'abs' form, unless possibly if nf = Q */
      59       14728 :         if (rnf_get_nfdegree(rnf) == 1)
      60             :         {
      61       14707 :           GEN y = gel(x,2);
      62       14707 :           pari_sp av = avma;
      63       14707 :           y = simplify_shallow(liftpol_shallow(y));
      64       14707 :           return gerepilecopy(av, mkpolmod(y, pol));
      65             :         }
      66          21 :         return gcopy(x);
      67             :       }
      68       86884 :       x = polmod_nffix(f,rnf,x,0);
      69       86870 :       if (typ(x) == t_POLMOD) return rnfeltup(rnf,x);
      70       79030 :       retmkpolmod(eltreltoabs(rnf_get_map(rnf), x), ZX_copy(pol));
      71        3332 :     case t_POL:
      72        3332 :       if (varn(x) == rnf_get_nfvarn(rnf)) return rnfeltup(rnf,x);
      73         581 :       retmkpolmod(eltreltoabs(rnf_get_map(rnf), x), ZX_copy(pol));
      74             :   }
      75          49 :   pari_err_TYPE(f,x);
      76             :   return NULL;/*LCOV_EXCL_LINE*/
      77             : }
      78             : 
      79             : GEN
      80       27307 : eltabstorel_lift(GEN rnfeq, GEN P)
      81             : {
      82       27307 :   GEN k, T = gel(rnfeq,4), R = gel(rnfeq,5);
      83       27307 :   if (is_scalar_t(typ(P))) return P;
      84       26481 :   k = gel(rnfeq,3);
      85       26481 :   P = lift_shallow(P);
      86       26481 :   if (signe(k))
      87        6230 :     P = RgXQX_translate(P, deg1pol_shallow(k, gen_0, varn(T)), T);
      88       26481 :   P = RgXQX_rem(P, R, T);
      89       26481 :   return QXQX_to_mod_shallow(P, T);
      90             : }
      91             : /* rnfeq = [pol,a,k,T,R], P a t_POL or scalar
      92             :  * Return Mod(P(x + k Mod(y, T(y))), pol(x)) */
      93             : GEN
      94       25046 : eltabstorel(GEN rnfeq, GEN P)
      95             : {
      96       25046 :   GEN T = gel(rnfeq,4), R = gel(rnfeq,5);
      97       25046 :   return mkpolmod(eltabstorel_lift(rnfeq,P), QXQX_to_mod_shallow(R,T));
      98             : }
      99             : GEN
     100       52843 : rnfeltabstorel(GEN rnf,GEN x)
     101             : {
     102       52843 :   const char *f = "rnfeltabstorel";
     103       52843 :   pari_sp av = avma;
     104             :   GEN pol, T, P, NF;
     105       52843 :   checkrnf(rnf);
     106       52843 :   T = rnf_get_nfpol(rnf);
     107       52843 :   P = rnf_get_pol(rnf);
     108       52843 :   pol = rnf_get_polabs(rnf);
     109       52843 :   switch(typ(x))
     110             :   {
     111        4914 :     case t_INT: return icopy(x);
     112          63 :     case t_FRAC: return gcopy(x);
     113       46389 :     case t_POLMOD:
     114       46389 :       if (RgX_equal_var(P, gel(x,1)))
     115             :       {
     116       14861 :         x = polmod_nffix(f, rnf, x, 0);
     117       14861 :         P = QXQX_to_mod_shallow(P,T);
     118       14861 :         return gerepilecopy(av, mkpolmod(x,P));
     119             :       }
     120       31528 :       if (RgX_equal_var(T, gel(x,1))) { x = Rg_nffix(f, T, x, 0); goto END; }
     121       31444 :       if (!RgX_equal_var(pol, gel(x,1))) pari_err_MODULUS(f, gel(x,1),pol);
     122       31402 :       x = gel(x,2); break;
     123        1036 :     case t_POL: break;
     124         441 :     case t_COL:
     125         441 :       NF = obj_check(rnf, rnf_NFABS);
     126         441 :       if (!NF) pari_err_TYPE("rnfeltabstorel, apply nfinit(rnf)",x);
     127         294 :       x = nf_to_scalar_or_alg(NF,x); break;
     128           0 :     default:
     129           0 :       pari_err_TYPE(f,x);
     130             :       return NULL;/*LCOV_EXCL_LINE*/
     131             :   }
     132       32732 :   switch(typ(x))
     133             :   {
     134       11886 :     case t_INT: return icopy(x);
     135          70 :     case t_FRAC: return gcopy(x);
     136       20776 :     case t_POL: break;
     137           0 :     default: pari_err_TYPE(f, x);
     138             :   }
     139       20776 :   RgX_check_QX(x,f);
     140       20699 :   if (varn(x) != varn(pol))
     141             :   {
     142          70 :     if (varn(x) == varn(T)) { x = Rg_nffix(f,T,x,0); goto END; }
     143          28 :     pari_err_VAR(f, x,pol);
     144             :   }
     145       20629 :   switch(lg(x))
     146             :   {
     147           0 :     case 2: set_avma(av); return gen_0;
     148          70 :     case 3: return gerepilecopy(av, gel(x,2));
     149             :   }
     150       20685 : END:
     151       20685 :   return gerepilecopy(av, eltabstorel(rnf_get_map(rnf), x));
     152             : }
     153             : 
     154             : /* x a t_VEC of rnf elements in 'alg' form (t_POL). Assume maximal rank or 0 */
     155             : static GEN
     156        2023 : modulereltoabs(GEN rnf, GEN x)
     157             : {
     158        2023 :   GEN W=gel(x,1), I=gel(x,2), rnfeq = rnf_get_map(rnf), polabs = gel(rnfeq,1);
     159        2023 :   long i, j, k, m, N = lg(W)-1;
     160             :   GEN zknf, dzknf, M;
     161             : 
     162        2023 :   if (!N) return cgetg(1, t_VEC);
     163        1960 :   zknf = rnf_get_nfzk(rnf);
     164        1960 :   dzknf = gel(zknf,1);
     165        1960 :   m = rnf_get_nfdegree(rnf);
     166        1960 :   M = cgetg(N*m+1, t_VEC);
     167        7049 :   for (k=i=1; i<=N; i++)
     168             :   {
     169        5089 :     GEN c0, cid, w = gel(W,i), id = gel(I,i);
     170             : 
     171        5089 :     if (lg(id) == 1) continue; /* must be a t_MAT */
     172        5040 :     id = Q_primitive_part(id, &cid);
     173        5040 :     w = Q_primitive_part(eltreltoabs(rnfeq,w), &c0);
     174        5040 :     c0 = mul_content(c0, mul_content(cid,inv_content(dzknf)));
     175        5040 :     if (typ(id) == t_INT)
     176       10346 :       for (j=1; j<=m; j++)
     177             :       {
     178        6776 :         GEN z = RgX_rem(gmul(w, gel(zknf,j)), polabs);
     179        6776 :         if (c0) z = RgX_Rg_mul(z, c0);
     180        6776 :         gel(M,k++) = z;
     181             :       }
     182             :     else
     183        5082 :       for (j=1; j<=m; j++)
     184             :       {
     185        3612 :         GEN c, z = Q_primitive_part(RgV_RgC_mul(zknf,gel(id,j)), &c);
     186        3612 :         z = RgX_rem(gmul(w, z), polabs);
     187        3612 :         c = mul_content(c, c0); if (c) z = RgX_Rg_mul(z, c);
     188        3612 :         gel(M,k++) = z;
     189             :       }
     190             :   }
     191        1960 :   setlg(M, k); return M;
     192             : }
     193             : 
     194             : /* Z-basis for absolute maximal order: [NF.pol, NF.zk] */
     195             : GEN
     196        1568 : rnf_zkabs(GEN rnf)
     197             : {
     198        1568 :   GEN d, v, M = modulereltoabs(rnf, rnf_get_zk(rnf));
     199        1568 :   GEN T = rnf_get_polabs(rnf);
     200        1568 :   long n = degpol(T);
     201        1568 :   M = Q_remove_denom(M, &d); /* t_VEC of t_POL */
     202        1568 :   if (d)
     203             :   {
     204        1064 :     M = RgXV_to_RgM(M,n);
     205        1064 :     M = ZM_hnfmodall(M, d, hnf_MODID|hnf_CENTER);
     206        1064 :     M = RgM_Rg_div(M, d);
     207             :   }
     208             :   else
     209         504 :     M = matid(n);
     210        1568 :   v = rnf_get_ramified_primes(rnf);
     211        1568 :   if (lg(v) == 1)
     212             :   {
     213         112 :     GEN D = gel(rnf_get_disc(rnf),1);
     214         112 :     if (!isint1(D)) pari_err_TYPE("rnf_zkabs (old style rnf)", rnf);
     215             :   }
     216        1568 :   v = shallowconcat(nf_get_ramified_primes(rnf_get_nf(rnf)), v);
     217        1568 :   return mkvec3(T, RgM_to_RgXV(M, varn(T)), ZV_sort_uniq(v));
     218             : }
     219             : 
     220             : static GEN
     221        1422 : mknfabs(GEN rnf, long prec)
     222             : {
     223             :   GEN NF;
     224        1422 :   if ((NF = obj_check(rnf,rnf_NFABS)))
     225           1 :   { if (nf_get_prec(NF) < prec) NF = nfnewprec_shallow(NF,prec); }
     226             :   else
     227        1421 :     NF = nfinit(rnf_zkabs(rnf), prec);
     228        1422 :   return NF;
     229             : }
     230             : 
     231             : static GEN
     232        1421 : mkupdown(GEN rnf)
     233             : {
     234        1421 :   GEN NF = obj_check(rnf, rnf_NFABS), M, zknf, dzknf;
     235             :   long i, l;
     236        1421 :   zknf = rnf_get_nfzk(rnf);
     237        1421 :   dzknf = gel(zknf,1); if (gequal1(dzknf)) dzknf = NULL;
     238        1421 :   l = lg(zknf); M = cgetg(l, t_MAT);
     239        1421 :   gel(M,1) = vec_ei(nf_get_degree(NF), 1);
     240        2653 :   for (i = 2; i < l; i++)
     241             :   {
     242        1232 :     GEN c = poltobasis(NF, gel(zknf,i));
     243        1232 :     if (dzknf) c = gdiv(c, dzknf);
     244        1232 :     gel(M,i) = c;
     245             :   }
     246        1421 :   return Qevproj_init(M);
     247             : }
     248             : GEN
     249       92421 : rnf_build_nfabs(GEN rnf, long prec)
     250             : {
     251       92421 :   GEN NF = obj_checkbuild_prec(rnf, rnf_NFABS, &mknfabs, &nf_get_prec, prec);
     252       92421 :   (void)obj_checkbuild(rnf, rnf_MAPS, &mkupdown);
     253       92421 :   return NF;
     254             : }
     255             : 
     256             : void
     257       24129 : rnfcomplete(GEN rnf)
     258       24129 : { (void)rnf_build_nfabs(rnf, nf_get_prec(rnf_get_nf(rnf))); }
     259             : 
     260             : GEN
     261        1792 : nf_nfzk(GEN nf, GEN rnfeq)
     262             : {
     263        1792 :   GEN pol = gel(rnfeq,1), a = gel(rnfeq,2);
     264        1792 :   return Q_primpart(QXV_QXQ_eval(nf_get_zkprimpart(nf), a, pol));
     265             : }
     266             : 
     267             : static GEN
     268        2737 : rnfdisc_get_T_i(GEN P, GEN *lim)
     269             : {
     270        2737 :   *lim = NULL;
     271        2737 :   if (typ(P) == t_VEC && lg(P) == 3)
     272             :   {
     273         308 :     GEN L = gel(P,2);
     274             :     long i, l;
     275         308 :     *lim = L;
     276         308 :     switch(typ(L))
     277             :     {
     278          56 :       case t_INT:
     279          56 :         if (signe(L) <= 0) return NULL;
     280          56 :         break;
     281         252 :       case t_VEC: case t_COL:
     282         252 :         l = lg(L);
     283         763 :         for (i = 1; i < l; i++)
     284             :         {
     285         511 :           GEN p = gel(L,i);
     286         511 :           if (typ(p) == t_INT)
     287         497 :           { if (signe(p) <= 0) return NULL; }
     288          14 :           else checkprid(p);
     289             :         }
     290         252 :         break;
     291           0 :       default: return NULL;
     292             :     }
     293         308 :     P = gel(P,1);
     294             :   }
     295        2737 :   return (typ(P) == t_POL)? P: NULL;
     296             : }
     297             : /* true nf */
     298             : GEN
     299        2737 : rnfdisc_get_T(GEN nf, GEN P, GEN *lim)
     300             : {
     301        2737 :   GEN T = rnfdisc_get_T_i(P, lim);
     302        2737 :   if (!T) pari_err_TYPE("rnfdisc",P);
     303        2737 :   return RgX_nffix("rnfdisc", nf_get_pol(nf), T, 1);
     304             : }
     305             : 
     306             : GEN
     307         119 : rnfpseudobasis(GEN nf, GEN pol)
     308             : {
     309         119 :   pari_sp av = avma;
     310             :   GEN D, z, lim;
     311         119 :   nf = checknf(nf);
     312         119 :   pol = rnfdisc_get_T(nf, pol, &lim);
     313         119 :   z = rnfallbase(nf, pol, lim, NULL, &D, NULL, NULL);
     314         105 :   return gerepilecopy(av, shallowconcat(z,D));
     315             : }
     316             : 
     317             : GEN
     318        1743 : rnfinit0(GEN nf, GEN T, long flag)
     319             : {
     320        1743 :   pari_sp av = avma;
     321        1743 :   GEN lim, bas, D, f, B, DKP, rnfeq, rnf = obj_init(11, 2);
     322        1743 :   nf = checknf(nf);
     323        1743 :   T = rnfdisc_get_T(nf, T, &lim);
     324        1743 :   gel(rnf,11) = rnfeq = nf_rnfeq(nf,T);
     325        1736 :   gel(rnf,2) = nf_nfzk(nf, rnfeq);
     326        1736 :   bas = rnfallbase(nf, T, lim, rnf, &D, &f, &DKP);
     327        1736 :   B = matbasistoalg(nf,gel(bas,1));
     328        1736 :   gel(bas,1) = lift_if_rational( RgM_to_RgXV(B,varn(T)) );
     329        1736 :   gel(rnf,1) = T;
     330        1736 :   gel(rnf,3) = D;
     331        1736 :   gel(rnf,4) = f;
     332        1736 :   gel(rnf,5) = DKP;
     333        1736 :   gel(rnf,6) = cgetg(1, t_VEC); /* dummy */
     334        1736 :   gel(rnf,7) = bas;
     335        1736 :   gel(rnf,8) = lift_if_rational( RgM_inv_upper(B) );
     336        1176 :   gel(rnf,9) = typ(f) == t_INT? powiu(f, nf_get_degree(nf))
     337        1736 :                               : RgM_det_triangular(f);
     338        1736 :   gel(rnf,10)= nf;
     339        1736 :   rnf = gerepilecopy(av, rnf);
     340        1736 :   if (flag) rnfcomplete(rnf);
     341        1736 :   return rnf;
     342             : }
     343             : GEN
     344         623 : rnfinit(GEN nf, GEN T) { return rnfinit0(nf,T,0); }
     345             : 
     346             : GEN
     347       22162 : rnfeltup0(GEN rnf, GEN x, long flag)
     348             : {
     349       22162 :   pari_sp av = avma;
     350             :   GEN zknf, nf, NF, POL;
     351       22162 :   long tx = typ(x);
     352       22162 :   checkrnf(rnf);
     353       22162 :   if (flag) rnfcomplete(rnf);
     354       22162 :   NF = obj_check(rnf,rnf_NFABS);
     355       22162 :   POL = rnf_get_polabs(rnf);
     356       22162 :   if (tx == t_POLMOD && RgX_equal_var(gel(x,1), POL))
     357             :   {
     358          42 :     if (flag) x = nf_to_scalar_or_basis(NF,x);
     359          42 :     return gerepilecopy(av, x);
     360             :   }
     361       22120 :   nf = rnf_get_nf(rnf);
     362       22120 :   if (NF && tx == t_COL && lg(x)-1 == degpol(POL) && nf_get_degree(rnf) > 1)
     363             :   {
     364           0 :     x = flag? nf_to_scalar_or_basis(NF,x)
     365           0 :             : mkpolmod(nf_to_scalar_or_alg(NF,x), POL);
     366           0 :     return gerepilecopy(av, x);
     367             :   }
     368       22120 :   if (NF)
     369             :   {
     370             :     GEN d, proj;
     371       21903 :     x = nf_to_scalar_or_basis(nf, x);
     372       21903 :     if (typ(x) != t_COL) return gerepilecopy(av, x);
     373       21147 :     proj = obj_check(rnf,rnf_MAPS);
     374       21147 :     x = Q_remove_denom(x,&d);
     375       21147 :     x = ZM_ZC_mul(gel(proj,1), x);
     376       21147 :     if (d) x = gdiv(x,d);
     377       21147 :     if (!flag) x = basistoalg(NF,x);
     378             :   }
     379             :   else
     380             :   {
     381         217 :     zknf = rnf_get_nfzk(rnf);
     382         217 :     x = nfeltup(nf, x, zknf);
     383         112 :     if (typ(x) == t_POL) x = mkpolmod(x, POL);
     384             :   }
     385       21259 :   return gerepilecopy(av, x);
     386             : }
     387             : GEN
     388       12201 : rnfeltup(GEN rnf, GEN x) { return rnfeltup0(rnf,x,0); }
     389             : 
     390             : GEN
     391         245 : nfeltup(GEN nf, GEN x, GEN zknf)
     392             : {
     393         245 :   GEN c, dzknf = gel(zknf,1);
     394         245 :   x = nf_to_scalar_or_basis(nf, x);
     395         140 :   if (typ(x) != t_COL) return x;
     396          42 :   x = Q_primitive_part(x, &c);
     397          42 :   if (!RgV_is_ZV(x)) pari_err_TYPE("rnfeltup", x);
     398          42 :   if (gequal1(dzknf)) dzknf = NULL;
     399          42 :   c = mul_content(c, inv_content(dzknf));
     400          42 :   x = RgV_RgC_mul(zknf, x); if (c) x = RgX_Rg_mul(x, c);
     401          42 :   return x;
     402             : }
     403             : 
     404             : static void
     405          49 : fail(const char *f, GEN x)
     406          49 : { pari_err_DOMAIN(f,"element","not in", strtoGENstr("the base field"),x); }
     407             : /* x t_COL of length degabs */
     408             : static GEN
     409           0 : eltdown(GEN rnf, GEN x, long flag)
     410             : {
     411           0 :   GEN z,y, d, proj = obj_check(rnf,rnf_MAPS);
     412           0 :   GEN M= gel(proj,1), iM=gel(proj,2), diM=gel(proj,3), perm=gel(proj,4);
     413           0 :   x = Q_remove_denom(x,&d);
     414           0 :   if (!RgV_is_ZV(x)) pari_err_TYPE("rnfeltdown", x);
     415           0 :   y = ZM_ZC_mul(iM, vecpermute(x, perm));
     416           0 :   z = ZM_ZC_mul(M,y);
     417           0 :   if (!isint1(diM)) z = ZC_Z_mul(z,diM);
     418           0 :   if (!ZV_equal(z,x)) fail("rnfeltdown",x);
     419             : 
     420           0 :   d = mul_denom(d, diM);
     421           0 :   if (d) y = gdiv(y,d);
     422           0 :   if (!flag) y = basistoalg(rnf_get_nf(rnf), y);
     423           0 :   return y;
     424             : }
     425             : GEN
     426        2716 : rnfeltdown0(GEN rnf, GEN x, long flag)
     427             : {
     428        2716 :   const char *f = "rnfeltdown";
     429        2716 :   pari_sp av = avma;
     430             :   GEN z, T, NF, nf;
     431             :   long v;
     432             : 
     433        2716 :   checkrnf(rnf);
     434        2716 :   NF = obj_check(rnf,rnf_NFABS);
     435        2716 :   nf = rnf_get_nf(rnf);
     436        2716 :   T = nf_get_pol(nf);
     437        2716 :   v = varn(T);
     438        2716 :   switch(typ(x))
     439             :   { /* directly belonging to base field ? */
     440         567 :     case t_INT: return icopy(x);
     441          63 :     case t_FRAC:return gcopy(x);
     442        1904 :     case t_POLMOD:
     443        1904 :       if (RgX_equal_var(gel(x,1), rnf_get_polabs(rnf)))
     444             :       {
     445         210 :         if (degpol(T) == 1)
     446             :         {
     447         182 :           x = simplify_shallow(liftpol_shallow(gel(x,2)));
     448         182 :           if (typ(x) != t_POL) return gerepilecopy(av,x);
     449             :         }
     450          49 :         break;
     451             :       }
     452        1694 :       x = polmod_nffix(f,rnf,x,0);
     453             :       /* x was defined mod the relative polynomial & non constant => fail */
     454        1680 :       if (typ(x) == t_POL) fail(f,x);
     455        1673 :       if (flag) x = nf_to_scalar_or_basis(nf,x);
     456        1673 :       return gerepilecopy(av, x);
     457             : 
     458         133 :     case t_POL:
     459         133 :       if (varn(x) != v) break;
     460          91 :       x = Rg_nffix(f,T,x,0);
     461          84 :       if (flag) x = nf_to_scalar_or_basis(nf,x);
     462          84 :       return gerepilecopy(av, x);
     463          49 :     case t_COL:
     464             :     {
     465          49 :       long n = lg(x)-1;
     466          49 :       if (n == degpol(T) && RgV_is_QV(x))
     467             :       {
     468           7 :         if (RgV_isscalar(x)) return gcopy(gel(x,1));
     469           0 :         if (!flag) return gcopy(x);
     470           0 :         return basistoalg(nf,x);
     471             :       }
     472          42 :       if (NF) break;
     473             :     }
     474          42 :     default: pari_err_TYPE(f, x);
     475             :   }
     476             :   /* x defined mod the absolute equation */
     477          91 :   if (NF)
     478             :   {
     479           0 :     x = nf_to_scalar_or_basis(NF, x);
     480           0 :     if (typ(x) == t_COL) x = eltdown(rnf,x,flag);
     481           0 :     return gerepilecopy(av, x);
     482             :   }
     483          91 :   z = rnfeltabstorel(rnf,x);
     484          70 :   switch(typ(z))
     485             :   {
     486          14 :     case t_INT:
     487          14 :     case t_FRAC: return z;
     488             :   }
     489             :   /* typ(z) = t_POLMOD, varn of both components is rnf_get_varn(rnf) */
     490          56 :   z = gel(z,2);
     491          56 :   if (typ(z) == t_POL)
     492             :   {
     493          56 :     if (lg(z) != 3) fail(f,x);
     494          14 :     z = gel(z,2);
     495             :   }
     496          14 :   return gerepilecopy(av, z);
     497             : }
     498             : GEN
     499        2471 : rnfeltdown(GEN rnf, GEN x) { return rnfeltdown0(rnf,x,0); }
     500             : 
     501             : /* vector of rnf elt -> matrix of nf elts */
     502             : static GEN
     503         483 : rnfV_to_nfM(GEN rnf, GEN x)
     504             : {
     505         483 :   long i, l = lg(x);
     506         483 :   GEN y = cgetg(l, t_MAT);
     507        1463 :   for (i = 1; i < l; i++) gel(y,i) = rnfalgtobasis(rnf,gel(x,i));
     508         483 :   return y;
     509             : }
     510             : 
     511             : static GEN
     512         770 : rnfprincipaltohnf(GEN rnf,GEN x)
     513             : {
     514         770 :   pari_sp av = avma;
     515         770 :   GEN bas = rnf_get_zk(rnf), nf = rnf_get_nf(rnf);
     516         770 :   x = rnfbasistoalg(rnf,x);
     517         434 :   x = gmul(x, gmodulo(gel(bas,1), rnf_get_pol(rnf)));
     518         434 :   return gerepileupto(av, nfhnf(nf, mkvec2(rnfV_to_nfM(rnf,x), gel(bas,2))));
     519             : }
     520             : 
     521             : /* pseudo-basis for the 0 ideal */
     522             : static GEN
     523         154 : rnfideal0(void) { retmkvec2(cgetg(1,t_MAT),cgetg(1,t_VEC)); }
     524             : 
     525             : GEN
     526        1330 : rnfidealhnf(GEN rnf, GEN x)
     527             : {
     528             :   GEN z, nf, bas;
     529             : 
     530        1330 :   checkrnf(rnf); nf = rnf_get_nf(rnf);
     531        1330 :   switch(typ(x))
     532             :   {
     533         182 :     case t_INT: case t_FRAC:
     534         182 :       if (isintzero(x)) return rnfideal0();
     535         126 :       bas = rnf_get_zk(rnf); z = cgetg(3,t_VEC);
     536         126 :       gel(z,1) = matid(rnf_get_degree(rnf));
     537         126 :       gel(z,2) = gmul(x, gel(bas,2)); return z;
     538             : 
     539         266 :     case t_VEC:
     540         266 :       if (lg(x) == 3 && typ(gel(x,1)) == t_MAT) return nfhnf(nf, x);
     541             :     case t_MAT:
     542         252 :       return rnfidealabstorel(rnf, x);
     543             : 
     544         770 :     case t_POLMOD: case t_POL: case t_COL:
     545         770 :       return rnfprincipaltohnf(rnf,x);
     546             :   }
     547           0 :   pari_err_TYPE("rnfidealhnf",x);
     548             :   return NULL; /* LCOV_EXCL_LINE */
     549             : }
     550             : 
     551             : static GEN
     552         105 : prodidnorm(GEN nf, GEN I)
     553             : {
     554         105 :   long i, l = lg(I);
     555             :   GEN z;
     556         105 :   if (l == 1) return gen_1;
     557         105 :   z = idealnorm(nf, gel(I,1));
     558         210 :   for (i=2; i<l; i++) z = gmul(z, idealnorm(nf, gel(I,i)));
     559         105 :   return z;
     560             : }
     561             : 
     562             : GEN
     563         196 : rnfidealnormrel(GEN rnf, GEN id)
     564             : {
     565         196 :   pari_sp av = avma;
     566         196 :   GEN nf, z = gel(rnfidealhnf(rnf,id), 2);
     567         126 :   if (lg(z) == 1) return cgetg(1, t_MAT);
     568          98 :   nf = rnf_get_nf(rnf); z = idealprod(nf, z);
     569          98 :   return gerepileupto(av, idealmul(nf,z, rnf_get_index(rnf)));
     570             : }
     571             : 
     572             : GEN
     573         203 : rnfidealnormabs(GEN rnf, GEN id)
     574             : {
     575         203 :   pari_sp av = avma;
     576         203 :   GEN nf, z = gel(rnfidealhnf(rnf,id), 2);
     577         133 :   if (lg(z) == 1) return gen_0;
     578         105 :   nf = rnf_get_nf(rnf); z = prodidnorm(nf, z);
     579         105 :   return gerepileupto(av, gmul(z, gel(rnf,9)));
     580             : }
     581             : 
     582             : static GEN
     583         497 : rnfidealreltoabs_i(GEN rnf, GEN x)
     584             : {
     585             :   long i, l;
     586             :   GEN w;
     587         497 :   x = rnfidealhnf(rnf,x);
     588         357 :   w = gel(x,1); l = lg(w); settyp(w, t_VEC);
     589         966 :   for (i=1; i<l; i++) gel(w,i) = lift_shallow( rnfbasistoalg(rnf, gel(w,i)) );
     590         357 :   return modulereltoabs(rnf, x);
     591             : }
     592             : GEN
     593           0 : rnfidealreltoabs(GEN rnf, GEN x)
     594             : {
     595           0 :   pari_sp av = avma;
     596           0 :   return gerepilecopy(av, rnfidealreltoabs_i(rnf,x));
     597             : }
     598             : GEN
     599         238 : rnfidealreltoabs0(GEN rnf, GEN x, long flag)
     600             : {
     601         238 :   pari_sp av = avma;
     602             :   long i, l;
     603             :   GEN NF;
     604             : 
     605         238 :   x = rnfidealreltoabs_i(rnf, x);
     606         168 :   if (!flag) return gerepilecopy(av,x);
     607          35 :   rnfcomplete(rnf);
     608          35 :   NF = obj_check(rnf,rnf_NFABS);
     609          35 :   l = lg(x); settyp(x, t_MAT);
     610         245 :   for (i=1; i<l; i++) gel(x,i) = algtobasis(NF, gel(x,i));
     611          35 :   return gerepileupto(av, idealhnf(NF,x));
     612             : }
     613             : 
     614             : GEN
     615         455 : rnfidealabstorel(GEN rnf, GEN x)
     616             : {
     617         455 :   long n, N, j, tx = typ(x);
     618         455 :   pari_sp av = avma;
     619             :   GEN A, I, invbas;
     620             : 
     621         455 :   checkrnf(rnf);
     622         455 :   invbas = rnf_get_invzk(rnf);
     623         455 :   if (tx != t_VEC && tx != t_MAT) pari_err_TYPE("rnfidealabstorel",x);
     624         315 :   N = lg(x)-1;
     625         315 :   if (N != rnf_get_absdegree(rnf))
     626             :   {
     627         196 :     if (!N) return rnfideal0();
     628         105 :     pari_err_DIM("rnfidealabstorel");
     629             :   }
     630         119 :   n = rnf_get_degree(rnf);
     631         119 :   A = cgetg(N+1,t_MAT);
     632         119 :   I = cgetg(N+1,t_VEC);
     633         833 :   for (j=1; j<=N; j++)
     634             :   {
     635         714 :     GEN t = lift_shallow( rnfeltabstorel(rnf, gel(x,j)) );
     636         714 :     if (typ(t) == t_POL)
     637         595 :       t = RgM_RgX_mul(invbas, t);
     638             :     else
     639         119 :       t = scalarcol_shallow(t, n);
     640         714 :     gel(A,j) = t;
     641         714 :     gel(I,j) = gen_1;
     642             :   }
     643         119 :   return gerepileupto(av, nfhnf(rnf_get_nf(rnf), mkvec2(A,I)));
     644             : }
     645             : 
     646             : GEN
     647         217 : rnfidealdown(GEN rnf,GEN x)
     648             : {
     649         217 :   pari_sp av = avma;
     650             :   GEN I;
     651         217 :   if (typ(x) == t_MAT)
     652             :   {
     653             :     GEN d;
     654          28 :     x = Q_remove_denom(x,&d);
     655          28 :     if (RgM_is_ZM(x))
     656             :     {
     657          28 :       GEN NF = obj_check(rnf,rnf_NFABS);
     658          28 :       if (NF)
     659             :       {
     660          28 :         GEN z, proj = obj_check(rnf,rnf_MAPS), ZK = gel(proj,1);
     661             :         long i, lz, l;
     662          28 :         x = idealhnf(NF,x);
     663          35 :         if (lg(x) == 1) { set_avma(av); return cgetg(1,t_MAT); }
     664          14 :         z = ZM_lll(shallowconcat(ZK,x), 0.99, LLL_KER);
     665          14 :         lz = lg(z); l = lg(ZK);
     666          56 :         for (i = 1; i < lz; i++) setlg(gel(z,i), l);
     667          14 :         z = ZM_hnfmodid(z, gcoeff(x,1,1));
     668          14 :         if (d) z = gdiv(z,d);
     669          14 :         return gerepileupto(av, z);
     670             :       }
     671             :     }
     672             :   }
     673         189 :   x = rnfidealhnf(rnf,x); I = gel(x,2);
     674         126 :   if (lg(I) == 1) { set_avma(av); return cgetg(1,t_MAT); }
     675         105 :   return gerepilecopy(av, gel(I,1));
     676             : }
     677             : 
     678             : /* lift ideal x to the relative extension, returns a Z-basis */
     679             : GEN
     680         217 : rnfidealup(GEN rnf,GEN x)
     681             : {
     682         217 :   pari_sp av = avma;
     683             :   long i, n;
     684             :   GEN nf, bas, bas2, I, x2;
     685             : 
     686         217 :   checkrnf(rnf); nf = rnf_get_nf(rnf);
     687         217 :   n = rnf_get_degree(rnf);
     688         217 :   bas = rnf_get_zk(rnf); bas2 = gel(bas,2);
     689             : 
     690         217 :   (void)idealtyp(&x, &I); /* I is junk */
     691         203 :   x2 = idealtwoelt(nf,x);
     692          98 :   I = cgetg(n+1,t_VEC);
     693         287 :   for (i=1; i<=n; i++)
     694             :   {
     695         189 :     GEN c = gel(bas2,i), d;
     696         189 :     if (typ(c) == t_MAT)
     697             :     {
     698           0 :       c = Q_remove_denom(c,&d);
     699           0 :       c = idealHNF_mul(nf,c,x2);
     700           0 :       if (d) c = gdiv(c,d);
     701             :     }
     702             :     else
     703         189 :       c = idealmul(nf,c,x);
     704         189 :     gel(I,i) = c;
     705             :   }
     706          98 :   return gerepilecopy(av, modulereltoabs(rnf, mkvec2(gel(bas,1), I)));
     707             : }
     708             : GEN
     709        1260 : rnfidealup0(GEN rnf,GEN x, long flag)
     710             : {
     711        1260 :   pari_sp av = avma;
     712             :   GEN NF, nf, proj, d, x2;
     713             : 
     714        1260 :   if (!flag) return rnfidealup(rnf,x);
     715        1043 :   checkrnf(rnf); nf = rnf_get_nf(rnf);
     716        1043 :   rnfcomplete(rnf);
     717        1043 :   proj = obj_check(rnf,rnf_MAPS);
     718        1043 :   NF = obj_check(rnf,rnf_NFABS);
     719             : 
     720        1043 :   (void)idealtyp(&x, &d); /* d is junk */
     721        1043 :   x2 = idealtwoelt(nf,x);
     722        1043 :   x2 = Q_remove_denom(x2,&d);
     723        1043 :   if (typ(gel(x2,2)) == t_COL) gel(x2,2) = ZM_ZC_mul(gel(proj,1),gel(x2,2));
     724        1043 :   x2 = idealhnf_two(NF, x2);
     725        1043 :   if (d) x2 = gdiv(x2,d);
     726        1043 :   return gerepileupto(av, x2);
     727             : }
     728             : 
     729             : /* x a relative HNF => vector of 2 generators (relative polmods) */
     730             : GEN
     731         259 : rnfidealtwoelement(GEN rnf, GEN x)
     732             : {
     733         259 :   pari_sp av = avma;
     734             :   GEN y, cy, z, NF;
     735             : 
     736         259 :   y = rnfidealreltoabs_i(rnf,x);
     737         189 :   rnfcomplete(rnf);
     738         189 :   NF = obj_check(rnf,rnf_NFABS);
     739         189 :   y = matalgtobasis(NF, y); settyp(y, t_MAT);
     740         189 :   y = Q_primitive_part(y, &cy);
     741         189 :   y = ZM_hnf(y);
     742         189 :   if (lg(y) == 1) { set_avma(av); return mkvec2(gen_0, gen_0); }
     743         154 :   y = idealtwoelt(NF, y);
     744         147 :   if (cy) y = RgV_Rg_mul(y, cy);
     745         147 :   z = gel(y,2);
     746         147 :   if (typ(z) == t_COL) z = rnfeltabstorel(rnf, nf_to_scalar_or_alg(NF, z));
     747         147 :   return gerepilecopy(av, mkvec2(gel(y,1), z));
     748             : }
     749             : 
     750             : GEN
     751          56 : rnfidealmul(GEN rnf,GEN x,GEN y)
     752             : {
     753          56 :   pari_sp av = avma;
     754             :   GEN nf, z, x1, x2, p1, p2, bas;
     755             : 
     756          56 :   y = rnfidealtwoelement(rnf,y);
     757          56 :   if (isintzero(gel(y,1))) { set_avma(av); return rnfideal0(); }
     758          49 :   nf = rnf_get_nf(rnf);
     759          49 :   bas = rnf_get_zk(rnf);
     760          49 :   x = rnfidealhnf(rnf,x);
     761          49 :   x1 = gmodulo(gmul(gel(bas,1), matbasistoalg(nf,gel(x,1))), rnf_get_pol(rnf));
     762          49 :   x2 = gel(x,2);
     763          49 :   p1 = gmul(gel(y,1), gel(x,1));
     764          49 :   p2 = rnfV_to_nfM(rnf, gmul(gel(y,2), x1));
     765          49 :   z = mkvec2(shallowconcat(p1, p2), shallowconcat(x2, x2));
     766          49 :   return gerepileupto(av, nfhnf(nf,z));
     767             : }
     768             : 
     769             : /* prK wrt NF ~ Q[x]/(polabs) */
     770             : static GEN
     771       12334 : rnfidealprimedec_1(GEN rnf, GEN SL, GEN prK)
     772             : {
     773       12334 :   GEN v, piL, piK = pr_get_gen(prK);
     774             :   long i, c, l;
     775       12334 :   if (pr_is_inert(prK)) return SL;
     776        9730 :   piL = rnfeltup0(rnf, piK, 1);
     777        9730 :   v = cgetg_copy(SL, &l);
     778       42504 :   for (i = c = 1; i < l; i++)
     779             :   {
     780       32774 :     GEN P = gel(SL,i);
     781       32774 :     if (ZC_prdvd(piL, P)) gel(v,c++) = P;
     782             :   }
     783        9730 :   setlg(v, c); return v;
     784             : }
     785             : GEN
     786       12334 : rnfidealprimedec(GEN rnf, GEN pr)
     787             : {
     788       12334 :   pari_sp av = avma;
     789             :   GEN p, z, NF, nf, SL;
     790       12334 :   checkrnf(rnf);
     791       12334 :   rnfcomplete(rnf);
     792       12334 :   NF = obj_check(rnf,rnf_NFABS);
     793       12334 :   nf = rnf_get_nf(rnf);
     794       12334 :   if (typ(pr) == t_INT)
     795             :   {
     796          28 :     p = pr;
     797          28 :     pr = NULL;
     798             :   }
     799             :   else
     800             :   {
     801       12306 :     checkprid(pr);
     802       12306 :     p = pr_get_p(pr);
     803             :   }
     804       12334 :   SL = idealprimedec(NF, p);
     805       12334 :   if (pr) z = rnfidealprimedec_1(rnf, SL, pr);
     806             :   else
     807             :   {
     808          28 :     GEN vK = idealprimedec(nf, p), vL;
     809          28 :     long l = lg(vK), i;
     810          28 :     vL = cgetg(l, t_VEC);
     811          56 :     for (i = 1; i < l; i++) gel(vL,i) = rnfidealprimedec_1(rnf, SL, gel(vK,i));
     812          28 :     z = mkvec2(vK, vL);
     813             :   }
     814       12334 :   return gerepilecopy(av, z);
     815             : }
     816             : 
     817             : GEN
     818          35 : rnfidealfactor(GEN rnf, GEN x)
     819             : {
     820          35 :   pari_sp av = avma;
     821             :   GEN NF;
     822          35 :   checkrnf(rnf);
     823          35 :   rnfcomplete(rnf);
     824          35 :   NF = obj_check(rnf,rnf_NFABS);
     825          35 :   return gerepileupto(av, idealfactor(NF, rnfidealreltoabs0(rnf, x, 1)));
     826             : }
     827             : 
     828             : GEN
     829        4018 : rnfequationall(GEN A, GEN B, long *pk, GEN *pLPRS)
     830             : {
     831             :   long lA, lB;
     832             :   GEN nf, C;
     833             : 
     834        4018 :   A = get_nfpol(A, &nf); lA = lg(A);
     835        4018 :   if (!nf) {
     836        1379 :     if (lA<=3) pari_err_CONSTPOL("rnfequation");
     837        1379 :     RgX_check_ZX(A,"rnfequation");
     838             :   }
     839        4018 :   B = RgX_nffix("rnfequation", A,B,1); lB = lg(B);
     840        4018 :   if (lB<=3) pari_err_CONSTPOL("rnfequation");
     841        4018 :   B = Q_primpart(B);
     842             : 
     843        4018 :   if (!nfissquarefree(A,B))
     844           7 :     pari_err_DOMAIN("rnfequation","issquarefree(B)","=",gen_0,B);
     845             : 
     846        4011 :   *pk = 0; C = ZX_ZXY_resultant_all(A, B, pk, pLPRS);
     847        4011 :   if (signe(leading_coeff(C)) < 0) C = ZX_neg(C);
     848        4011 :   *pk = -*pk; return Q_primpart(C);
     849             : }
     850             : 
     851             : GEN
     852        3696 : rnfequation0(GEN A, GEN B, long flall)
     853             : {
     854        3696 :   pari_sp av = avma;
     855             :   GEN LPRS, C;
     856             :   long k;
     857             : 
     858        3696 :   C = rnfequationall(A, B, &k, flall? &LPRS: NULL);
     859        3689 :   if (flall)
     860             :   { /* a,b,c root of A,B,C = compositum, c = b + k a */
     861        3192 :     GEN a, mH0 = RgX_neg(gel(LPRS,1)), H1 = gel(LPRS,2);
     862        3192 :     a = QXQ_div(mH0, H1, C);
     863        3192 :     C = mkvec3(C, mkpolmod(a, C), stoi(k));
     864             :   }
     865        3689 :   return gerepilecopy(av, C);
     866             : }
     867             : GEN
     868         483 : rnfequation(GEN nf, GEN pol) { return rnfequation0(nf,pol,0); }
     869             : GEN
     870        3087 : rnfequation2(GEN nf, GEN pol) { return rnfequation0(nf,pol,1); }
     871             : GEN
     872        1869 : nf_rnfeq(GEN nf, GEN R)
     873             : {
     874             :   GEN pol, a, k, junk, eq;
     875        1869 :   R = liftpol_shallow(R);
     876        1869 :   eq = rnfequation2(nf, R);
     877        1862 :   pol = gel(eq,1);
     878        1862 :   a = gel(eq,2); if (typ(a) == t_POLMOD) a = gel(a,2);
     879        1862 :   k = gel(eq,3);
     880        1862 :   return mkvec5(pol,a,k,get_nfpol(nf, &junk),R);
     881             : }
     882             : /* only allow abstorel */
     883             : GEN
     884         322 : nf_rnfeqsimple(GEN nf, GEN R)
     885             : {
     886             :   long sa;
     887             :   GEN junk, pol;
     888         322 :   R = liftpol_shallow(R);
     889         322 :   pol = rnfequationall(nf, R, &sa, NULL);
     890         322 :   return mkvec5(pol,gen_0/*dummy*/,stoi(sa),get_nfpol(nf, &junk),R);
     891             : }
     892             : 
     893             : /*******************************************************************/
     894             : /*                                                                 */
     895             : /*                            RELATIVE LLL                         */
     896             : /*                                                                 */
     897             : /*******************************************************************/
     898             : static GEN
     899         196 : nftau(long r1, GEN x)
     900             : {
     901         196 :   long i, l = lg(x);
     902         196 :   GEN s = r1? gel(x,1): gmul2n(real_i(gel(x,1)),1);
     903         392 :   for (i=2; i<=r1; i++) s = gadd(s, gel(x,i));
     904         196 :   for (   ; i < l; i++) s = gadd(s, gmul2n(real_i(gel(x,i)),1));
     905         196 :   return s;
     906             : }
     907             : 
     908             : static GEN
     909          28 : initmat(long l)
     910             : {
     911          28 :   GEN x = cgetg(l, t_MAT);
     912             :   long i;
     913         196 :   for (i = 1; i < l; i++) gel(x,i) = cgetg(l, t_COL);
     914          28 :   return x;
     915             : }
     916             : 
     917             : static GEN
     918        1022 : nftocomplex(GEN nf, GEN x)
     919             : {
     920        1022 :   GEN M = nf_get_M(nf);
     921        1022 :   x = nf_to_scalar_or_basis(nf,x);
     922        1022 :   if (typ(x) != t_COL) return const_col(nbrows(M), x);
     923         161 :   return RgM_RgC_mul(M, x);
     924             : }
     925             : /* assume x a square t_MAT, return a t_VEC of embeddings of its columns */
     926             : static GEN
     927          14 : mattocomplex(GEN nf, GEN x)
     928             : {
     929          14 :   long i,j, l = lg(x);
     930          14 :   GEN v = cgetg(l, t_VEC);
     931          98 :   for (j=1; j<l; j++)
     932             :   {
     933          84 :     GEN c = gel(x,j), b = cgetg(l, t_MAT);
     934         714 :     for (i=1; i<l; i++) gel(b,i) = nftocomplex(nf, gel(c,i));
     935          84 :     b = shallowtrans(b); settyp(b, t_COL);
     936          84 :     gel(v,j) = b;
     937             :   }
     938          14 :   return v;
     939             : }
     940             : 
     941             : static GEN
     942          14 : nf_all_roots(GEN nf, GEN x, long prec)
     943             : {
     944          14 :   long i, j, l = lg(x), ru = lg(nf_get_roots(nf));
     945          14 :   GEN y = cgetg(l, t_POL), v, z;
     946             : 
     947          14 :   x = RgX_to_nfX(nf, x);
     948          14 :   y[1] = x[1];
     949         112 :   for (i=2; i<l; i++) gel(y,i) = nftocomplex(nf, gel(x,i));
     950          14 :   i = gprecision(y); if (i && i <= 3) return NULL;
     951             : 
     952          14 :   v = cgetg(ru, t_VEC);
     953          14 :   z = cgetg(l, t_POL); z[1] = x[1];
     954          42 :   for (i=1; i<ru; i++)
     955             :   {
     956         224 :     for (j = 2; j < l; j++) gel(z,j) = gmael(y,j,i);
     957          28 :     gel(v,i) = cleanroots(z, prec);
     958             :   }
     959          14 :   return v;
     960             : }
     961             : 
     962             : static GEN
     963         357 : rnfscal(GEN m, GEN x, GEN y)
     964             : {
     965         357 :   long i, l = lg(m);
     966         357 :   GEN z = cgetg(l, t_COL);
     967        1071 :   for (i = 1; i < l; i++)
     968         714 :     gel(z,i) = gmul(conj_i(shallowtrans(gel(x,i))), gmul(gel(m,i), gel(y,i)));
     969         357 :   return z;
     970             : }
     971             : 
     972             : /* x ideal in HNF */
     973             : static GEN
     974         364 : findmin(GEN nf, GEN x, GEN muf)
     975             : {
     976         364 :   pari_sp av = avma;
     977             :   long e;
     978         364 :   GEN cx, y, m, M = nf_get_M(nf);
     979             : 
     980         364 :   x = Q_primitive_part(x, &cx);
     981         364 :   if (gequal1(gcoeff(x,1,1))) y = M;
     982             :   else
     983             :   {
     984         210 :     GEN G = nf_get_G(nf);
     985         210 :     m = lllfp(RgM_mul(G,x), 0.75, 0);
     986         210 :     if (typ(m) != t_MAT)
     987             :     {
     988           0 :       x = ZM_lll(x, 0.75, LLL_INPLACE);
     989           0 :       m = lllfp(RgM_mul(G,x), 0.75, 0);
     990           0 :       if (typ(m) != t_MAT) pari_err_PREC("rnflllgram");
     991             :     }
     992         210 :     x = ZM_mul(x, m);
     993         210 :     y = RgM_mul(M, x);
     994             :   }
     995         364 :   m = RgM_solve_realimag(y, muf);
     996         364 :   if (!m) return NULL; /* precision problem */
     997         364 :   if (cx) m = RgC_Rg_div(m, cx);
     998         364 :   m = grndtoi(m, &e);
     999         364 :   if (e >= 0) return NULL; /* precision problem */
    1000         364 :   m = ZM_ZC_mul(x, m);
    1001         364 :   if (cx) m = ZC_Q_mul(m, cx);
    1002         364 :   return gerepileupto(av, m);
    1003             : }
    1004             : 
    1005             : static int
    1006         364 : RED(long k, long l, GEN U, GEN mu, GEN MC, GEN nf, GEN I, GEN *Ik_inv)
    1007             : {
    1008             :   GEN x, xc, ideal;
    1009             :   long i;
    1010             : 
    1011         364 :   if (!*Ik_inv) *Ik_inv = idealinv(nf, gel(I,k));
    1012         364 :   ideal = idealmul(nf,gel(I,l), *Ik_inv);
    1013         364 :   x = findmin(nf, ideal, gcoeff(mu,k,l));
    1014         364 :   if (!x) return 0;
    1015         364 :   if (gequal0(x)) return 1;
    1016             : 
    1017         294 :   xc = nftocomplex(nf,x);
    1018         294 :   gel(MC,k) = gsub(gel(MC,k), vecmul(xc,gel(MC,l)));
    1019         294 :   gel(U,k) = gsub(gel(U,k), gmul(coltoalg(nf,x), gel(U,l)));
    1020         294 :   gcoeff(mu,k,l) = gsub(gcoeff(mu,k,l), xc);
    1021        1029 :   for (i=1; i<l; i++)
    1022         735 :     gcoeff(mu,k,i) = gsub(gcoeff(mu,k,i), vecmul(xc,gcoeff(mu,l,i)));
    1023         294 :   return 1;
    1024             : }
    1025             : 
    1026             : static int
    1027          84 : check_0(GEN B)
    1028             : {
    1029          84 :   long i, l = lg(B);
    1030         252 :   for (i = 1; i < l; i++)
    1031         168 :     if (gsigne(gel(B,i)) <= 0) return 1;
    1032          84 :   return 0;
    1033             : }
    1034             : 
    1035             : static int
    1036          98 : do_SWAP(GEN I, GEN MC, GEN MCS, GEN h, GEN mu, GEN B, long kmax, long k,
    1037             :         const long alpha, long r1)
    1038             : {
    1039             :   GEN p1, p2, muf, mufc, Bf, temp;
    1040             :   long i, j;
    1041             : 
    1042          98 :   p1 = nftau(r1, gadd(gel(B,k),
    1043          98 :                       gmul(gnorml2(gcoeff(mu,k,k-1)), gel(B,k-1))));
    1044          98 :   p2 = nftau(r1, gel(B,k-1));
    1045          98 :   if (gcmp(gmulsg(alpha,p1), gmulsg(alpha-1,p2)) > 0) return 0;
    1046             : 
    1047          14 :   swap(gel(MC,k-1),gel(MC,k));
    1048          14 :   swap(gel(h,k-1), gel(h,k));
    1049          14 :   swap(gel(I,k-1), gel(I,k));
    1050          91 :   for (j=1; j<=k-2; j++) swap(gcoeff(mu,k-1,j),gcoeff(mu,k,j));
    1051          14 :   muf = gcoeff(mu,k,k-1);
    1052          14 :   mufc = conj_i(muf);
    1053          14 :   Bf = gadd(gel(B,k), vecmul(real_i(vecmul(muf,mufc)), gel(B,k-1)));
    1054          14 :   if (check_0(Bf)) return 1; /* precision problem */
    1055             : 
    1056          14 :   p1 = vecdiv(gel(B,k-1),Bf);
    1057          14 :   gcoeff(mu,k,k-1) = vecmul(mufc,p1);
    1058          14 :   temp = gel(MCS,k-1);
    1059          14 :   gel(MCS,k-1) = gadd(gel(MCS,k), vecmul(muf,gel(MCS,k-1)));
    1060          14 :   gel(MCS,k) = gsub(vecmul(vecdiv(gel(B,k),Bf), temp),
    1061          14 :                     vecmul(gcoeff(mu,k,k-1), gel(MCS,k)));
    1062          14 :   gel(B,k) = vecmul(gel(B,k),p1);
    1063          14 :   gel(B,k-1) = Bf;
    1064          14 :   for (i=k+1; i<=kmax; i++)
    1065             :   {
    1066           0 :     temp = gcoeff(mu,i,k);
    1067           0 :     gcoeff(mu,i,k) = gsub(gcoeff(mu,i,k-1), vecmul(muf, gcoeff(mu,i,k)));
    1068           0 :     gcoeff(mu,i,k-1) = gadd(temp, vecmul(gcoeff(mu,k,k-1),gcoeff(mu,i,k)));
    1069             :   }
    1070          14 :   return 1;
    1071             : }
    1072             : 
    1073             : static GEN
    1074          14 : rel_T2(GEN nf, GEN pol, long lx, long prec)
    1075             : {
    1076             :   long ru, i, j, k, l;
    1077             :   GEN T2, s, unro, roorder, powreorder;
    1078             : 
    1079          14 :   roorder = nf_all_roots(nf, pol, prec);
    1080          14 :   if (!roorder) return NULL;
    1081          14 :   ru = lg(roorder);
    1082          98 :   unro = cgetg(lx,t_COL); for (i=1; i<lx; i++) gel(unro,i) = gen_1;
    1083          14 :   powreorder = cgetg(lx,t_MAT); gel(powreorder,1) = unro;
    1084          14 :   T2 = cgetg(ru, t_VEC);
    1085          42 :   for (i = 1; i < ru; i++)
    1086             :   {
    1087          28 :     GEN ro = gel(roorder,i);
    1088          28 :     GEN m = initmat(lx);
    1089         168 :     for (k=2; k<lx; k++)
    1090             :     {
    1091         140 :       GEN c = cgetg(lx, t_COL); gel(powreorder,k) = c;
    1092        1232 :       for (j=1; j < lx; j++)
    1093        1092 :         gel(c,j) = gmul(gel(ro,j), gmael(powreorder,k-1,j));
    1094             :     }
    1095         196 :     for (l = 1; l < lx; l++)
    1096         882 :       for (k = 1; k <= l; k++)
    1097             :       {
    1098         714 :         s = gen_0;
    1099        6636 :         for (j = 1; j < lx; j++)
    1100        5922 :           s = gadd(s, gmul(conj_i(gmael(powreorder,k,j)),
    1101        5922 :                                   gmael(powreorder,l,j)));
    1102         714 :         if (l == k)
    1103         168 :           gcoeff(m, l, l) = real_i(s);
    1104             :         else
    1105             :         {
    1106         546 :           gcoeff(m, k, l) = s;
    1107         546 :           gcoeff(m, l, k) = conj_i(s);
    1108             :         }
    1109             :       }
    1110          28 :     gel(T2,i) = m;
    1111             :   }
    1112          14 :   return T2;
    1113             : }
    1114             : 
    1115             : /* given a base field nf (e.g main variable y), a polynomial pol with
    1116             :  * coefficients in nf    (e.g main variable x), and an order as output
    1117             :  * by rnfpseudobasis, outputs a reduced order. */
    1118             : GEN
    1119          14 : rnflllgram(GEN nf, GEN pol, GEN order,long prec)
    1120             : {
    1121          14 :   pari_sp av = avma;
    1122          14 :   long j, k, l, kmax, r1, lx, count = 0;
    1123             :   GEN M, I, h, H, mth, MC, MPOL, MCS, B, mu;
    1124          14 :   const long alpha = 10, MAX_COUNT = 4;
    1125             : 
    1126          14 :   nf = checknf(nf); r1 = nf_get_r1(nf);
    1127          14 :   check_ZKmodule(order, "rnflllgram");
    1128          14 :   M = gel(order,1);
    1129          14 :   I = gel(order,2); lx = lg(I);
    1130          14 :   if (lx < 3) return gcopy(order);
    1131          14 :   if (lx-1 != degpol(pol)) pari_err_DIM("rnflllgram");
    1132          14 :   I = leafcopy(I);
    1133          14 :   H = NULL;
    1134          14 :   MPOL = matbasistoalg(nf, M);
    1135          14 :   MCS = matid(lx-1); /* dummy for gerepile */
    1136          14 : PRECNF:
    1137          14 :   if (count == MAX_COUNT)
    1138             :   {
    1139           0 :     prec = precdbl(prec); count = 0;
    1140           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"rnflllgram",prec);
    1141           0 :     nf = nfnewprec_shallow(nf,prec);
    1142             :   }
    1143          14 :   mth = rel_T2(nf, pol, lx, prec);
    1144          14 :   if (!mth) { count = MAX_COUNT; goto PRECNF; }
    1145          14 :   h = NULL;
    1146          14 : PRECPB:
    1147          14 :   if (h)
    1148             :   { /* precision problem, recompute. If no progress, increase nf precision */
    1149           0 :     if (++count == MAX_COUNT || RgM_isidentity(h)) {count = MAX_COUNT; goto PRECNF;}
    1150           0 :     H = H? gmul(H, h): h;
    1151           0 :     MPOL = gmul(MPOL, h);
    1152             :   }
    1153          14 :   h = matid(lx-1);
    1154          14 :   MC = mattocomplex(nf, MPOL);
    1155          14 :   mu = cgetg(lx,t_MAT);
    1156          14 :   B  = cgetg(lx,t_COL);
    1157          98 :   for (j=1; j<lx; j++)
    1158             :   {
    1159          84 :     gel(mu,j) = zerocol(lx - 1);
    1160          84 :     gel(B,j) = gen_0;
    1161             :   }
    1162          14 :   if (DEBUGLEVEL) err_printf("k = ");
    1163          14 :   gel(B,1) = real_i(rnfscal(mth,gel(MC,1),gel(MC,1)));
    1164          14 :   gel(MCS,1) = gel(MC,1);
    1165          14 :   kmax = 1; k = 2;
    1166             :   do
    1167             :   {
    1168          98 :     GEN Ik_inv = NULL;
    1169          98 :     if (DEBUGLEVEL) err_printf("%ld ",k);
    1170          98 :     if (k > kmax)
    1171             :     { /* Incremental Gram-Schmidt */
    1172          70 :       kmax = k; gel(MCS,k) = gel(MC,k);
    1173         343 :       for (j=1; j<k; j++)
    1174             :       {
    1175         273 :         gcoeff(mu,k,j) = vecdiv(rnfscal(mth,gel(MCS,j),gel(MC,k)),
    1176         273 :                                 gel(B,j));
    1177         273 :         gel(MCS,k) = gsub(gel(MCS,k), vecmul(gcoeff(mu,k,j),gel(MCS,j)));
    1178             :       }
    1179          70 :       gel(B,k) = real_i(rnfscal(mth,gel(MCS,k),gel(MCS,k)));
    1180          70 :       if (check_0(gel(B,k))) goto PRECPB;
    1181             :     }
    1182          98 :     if (!RED(k, k-1, h, mu, MC, nf, I, &Ik_inv)) goto PRECPB;
    1183          98 :     if (do_SWAP(I,MC,MCS,h,mu,B,kmax,k,alpha, r1))
    1184             :     {
    1185          14 :       if (!B[k]) goto PRECPB;
    1186          14 :       if (k > 2) k--;
    1187             :     }
    1188             :     else
    1189             :     {
    1190         350 :       for (l=k-2; l; l--)
    1191         266 :         if (!RED(k, l, h, mu, MC, nf, I, &Ik_inv)) goto PRECPB;
    1192          84 :       k++;
    1193             :     }
    1194          98 :     if (gc_needed(av,2))
    1195             :     {
    1196           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"rnflllgram");
    1197           0 :       gerepileall(av, H?10:9, &nf,&mth,&h,&MPOL,&B,&MC,&MCS,&mu,&I,&H);
    1198             :     }
    1199             :   }
    1200          98 :   while (k < lx);
    1201          14 :   MPOL = gmul(MPOL,h);
    1202          14 :   if (H) h = gmul(H, h);
    1203          14 :   if (DEBUGLEVEL) err_printf("\n");
    1204          14 :   MPOL = RgM_to_nfM(nf,MPOL);
    1205          14 :   h = RgM_to_nfM(nf,h);
    1206          14 :   return gerepilecopy(av, mkvec2(mkvec2(MPOL,I), h));
    1207             : }
    1208             : 
    1209             : GEN
    1210           7 : rnfpolred(GEN nf, GEN pol, long prec)
    1211             : {
    1212           7 :   pari_sp av = avma;
    1213           7 :   long i, j, n, v = varn(pol);
    1214             :   GEN id, w, I, O, bnf, nfpol;
    1215             : 
    1216           7 :   if (typ(pol)!=t_POL) pari_err_TYPE("rnfpolred",pol);
    1217           7 :   bnf = nf; nf = checknf(bnf);
    1218           7 :   bnf = (nf == bnf)? NULL: checkbnf(bnf);
    1219           7 :   if (degpol(pol) <= 1) { w = cgetg(2, t_VEC); gel(w,1) = pol_x(v); return w; }
    1220           7 :   nfpol = nf_get_pol(nf);
    1221             : 
    1222           7 :   id = rnfpseudobasis(nf,pol);
    1223           7 :   if (bnf && is_pm1( bnf_get_no(bnf) )) /* if bnf is principal */
    1224             :   {
    1225             :     GEN newI, newO;
    1226           0 :     O = gel(id,1);
    1227           0 :     I = gel(id,2); n = lg(I)-1;
    1228           0 :     newI = cgetg(n+1,t_VEC);
    1229           0 :     newO = cgetg(n+1,t_MAT);
    1230           0 :     for (j=1; j<=n; j++)
    1231             :     {
    1232           0 :       GEN al = gen_if_principal(bnf,gel(I,j));
    1233           0 :       gel(newI,j) = gen_1;
    1234           0 :       gel(newO,j) = nfC_nf_mul(nf, gel(O,j), al);
    1235             :     }
    1236           0 :     id = mkvec2(newO, newI);
    1237             :   }
    1238             : 
    1239           7 :   id = gel(rnflllgram(nf,pol,id,prec),1);
    1240           7 :   O = gel(id,1);
    1241           7 :   I = gel(id,2); n = lg(I)-1;
    1242           7 :   w = cgetg(n+1,t_VEC);
    1243           7 :   pol = lift_shallow(pol);
    1244          70 :   for (j=1; j<=n; j++)
    1245             :   {
    1246          63 :     GEN newpol, L, a, Ij = gel(I,j);
    1247          63 :     a = RgC_Rg_mul(gel(O,j), (typ(Ij) == t_MAT)? gcoeff(Ij,1,1): Ij);
    1248         630 :     for (i=n; i; i--) gel(a,i) = nf_to_scalar_or_alg(nf, gel(a,i));
    1249          63 :     a = RgV_to_RgX(a, v);
    1250          63 :     newpol = RgXQX_red(RgXQ_charpoly(a, pol, v), nfpol);
    1251          63 :     newpol = Q_primpart(newpol);
    1252             : 
    1253          63 :     (void)nfgcd_all(newpol, RgX_deriv(newpol), nfpol, nf_get_index(nf), &newpol);
    1254          63 :     L = leading_coeff(newpol);
    1255          63 :     gel(w,j) = (typ(L) == t_POL)? RgXQX_div(newpol, L, nfpol)
    1256          63 :                                 : RgX_Rg_div(newpol, L);
    1257             :   }
    1258           7 :   return gerepilecopy(av,w);
    1259             : }
    1260             : 
    1261             : /*******************************************************************/
    1262             : /*                                                                 */
    1263             : /*                  LINEAR ALGEBRA OVER Z_K  (HNF,SNF)             */
    1264             : /*                                                                 */
    1265             : /*******************************************************************/
    1266             : /* A torsion-free module M over Z_K is given by [A,I].
    1267             :  * I=[a_1,...,a_k] is a row vector of k fractional ideals given in HNF.
    1268             :  * A is an n x k matrix (same k) such that if A_j is the j-th column of A then
    1269             :  * M=a_1 A_1+...+a_k A_k. We say that [A,I] is a pseudo-basis if k=n */
    1270             : 
    1271             : /* Given an element x and an ideal I in HNF, gives an r such that x-r is in H
    1272             :  * and r is small */
    1273             : GEN
    1274           7 : nfreduce(GEN nf, GEN x, GEN I)
    1275             : {
    1276           7 :   pari_sp av = avma;
    1277             :   GEN aI;
    1278           7 :   x = nf_to_scalar_or_basis(checknf(nf), x);
    1279           7 :   if (idealtyp(&I,&aI) != id_MAT || lg(I)==1) pari_err_TYPE("nfreduce",I);
    1280           7 :   if (typ(x) != t_COL) x = scalarcol( gmod(x, gcoeff(I,1,1)), lg(I)-1 );
    1281           7 :   else x = reducemodinvertible(x, I);
    1282           7 :   return gerepileupto(av, x);
    1283             : }
    1284             : /* Given an element x and an ideal in HNF, gives an a in ideal such that
    1285             :  * x-a is small. No checks */
    1286             : static GEN
    1287       34013 : element_close(GEN nf, GEN x, GEN ideal)
    1288             : {
    1289       34013 :   pari_sp av = avma;
    1290       34013 :   GEN y = gcoeff(ideal,1,1);
    1291       34013 :   x = nf_to_scalar_or_basis(nf, x);
    1292       34013 :   if (typ(y) == t_INT && is_pm1(y)) return ground(x);
    1293       31787 :   if (typ(x) == t_COL)
    1294       13496 :     x = closemodinvertible(x, ideal);
    1295             :   else
    1296       18291 :     x = gmul(y, gdivround(x,y));
    1297       31787 :   return gerepileupto(av, x);
    1298             : }
    1299             : 
    1300             : /* A + v B */
    1301             : static GEN
    1302      116452 : colcomb1(GEN nf, GEN v, GEN A, GEN B)
    1303             : {
    1304      116452 :   if (isintzero(v)) return A;
    1305       77266 :   return RgC_to_nfC(nf, RgC_add(A, nfC_nf_mul(nf,B,v)));
    1306             : }
    1307             : /* u A + v B */
    1308             : static GEN
    1309       96740 : colcomb(GEN nf, GEN u, GEN v, GEN A, GEN B)
    1310             : {
    1311       96740 :   if (isintzero(u)) return nfC_nf_mul(nf,B,v);
    1312       82418 :   if (u != gen_1) A = nfC_nf_mul(nf,A,u);
    1313       82418 :   return colcomb1(nf, v, A, B);
    1314             : }
    1315             : 
    1316             : /* return m[i,1..lim] * x */
    1317             : static GEN
    1318         231 : element_mulvecrow(GEN nf, GEN x, GEN m, long i, long lim)
    1319             : {
    1320         231 :   long j, l = minss(lg(m), lim+1);
    1321         231 :   GEN dx, y = cgetg(l, t_VEC);
    1322         231 :   x = nf_to_scalar_or_basis(nf, x);
    1323         231 :   if (typ(x) == t_COL)
    1324             :   {
    1325          91 :     x = zk_multable(nf, Q_remove_denom(x, &dx));
    1326         350 :     for (j=1; j<l; j++)
    1327             :     {
    1328         259 :       GEN t = gcoeff(m,i,j);
    1329         259 :       if (!isintzero(t))
    1330             :       {
    1331         112 :         if (typ(t) == t_COL)
    1332          28 :           t = RgM_RgC_mul(x, t);
    1333             :         else
    1334          84 :           t = ZC_Q_mul(gel(x,1), t);
    1335         112 :         if (dx) t = gdiv(t, dx);
    1336         112 :         t = nf_to_scalar_or_basis(nf,t);
    1337             :       }
    1338         259 :       gel(y,j) = t;
    1339             :     }
    1340             :   }
    1341             :   else
    1342             :   {
    1343         532 :     for (j=1; j<l; j++) gel(y,j) = gmul(x, gcoeff(m,i,j));
    1344             :   }
    1345         231 :   return y;
    1346             : }
    1347             : 
    1348             : /* u Z[s,] + v Z[t,], limitied to the first lim entries */
    1349             : static GEN
    1350         154 : rowcomb(GEN nf, GEN u, GEN v, long s, long t, GEN Z, long lim)
    1351             : {
    1352             :   GEN z;
    1353         154 :   if (gequal0(u))
    1354           7 :     z = element_mulvecrow(nf,v,Z,t, lim);
    1355             :   else
    1356             :   {
    1357         147 :     z = element_mulvecrow(nf,u,Z,s, lim);
    1358         147 :     if (!gequal0(v)) z = gadd(z, element_mulvecrow(nf,v,Z,t, lim));
    1359             :   }
    1360         154 :   return z;
    1361             : }
    1362             : 
    1363             : /* nfbezout(0,b,A,B). Either bB = NULL or b*B */
    1364             : static GEN
    1365       56077 : zero_nfbezout(GEN nf,GEN bB, GEN b, GEN A,GEN B,GEN *u,GEN *v,GEN *w,GEN *di)
    1366             : {
    1367             :   GEN d;
    1368       56077 :   if (isint1(b))
    1369             :   {
    1370       54796 :     *v = gen_1;
    1371       54796 :     *w = A;
    1372       54796 :     d = B;
    1373       54796 :     *di = idealinv(nf,d);
    1374             :   }
    1375             :   else
    1376             :   {
    1377        1281 :     *v = nfinv(nf,b);
    1378        1281 :     *w = idealmul(nf,A,*v);
    1379        1281 :     d = bB? bB: idealmul(nf,b,B);
    1380        1281 :     *di = idealHNF_inv(nf,d);
    1381             :   }
    1382       56077 :   *u = gen_0; return d;
    1383             : }
    1384             : 
    1385             : /* Given elements a,b and ideals A, B, outputs d = a.A+b.B and gives
    1386             :  * di=d^-1, w=A.B.di, u, v such that au+bv=1 and u in A.di, v in B.di.
    1387             :  * Assume A, B non-zero, but a or b can be zero (not both) */
    1388             : static GEN
    1389       59857 : nfbezout(GEN nf,GEN a,GEN b, GEN A,GEN B, GEN *pu,GEN *pv,GEN *pw,GEN *pdi,
    1390             :          int red)
    1391             : {
    1392             :   GEN w, u, v, d, di, aA, bB;
    1393             : 
    1394       59857 :   if (isintzero(a)) return zero_nfbezout(nf,NULL,b,A,B,pu,pv,pw,pdi);
    1395       59857 :   if (isintzero(b)) return zero_nfbezout(nf,NULL,a,B,A,pv,pu,pw,pdi);
    1396             : 
    1397       59857 :   if (a != gen_1) /* frequently called with a = gen_1 */
    1398             :   {
    1399       34531 :     a = nf_to_scalar_or_basis(nf,a);
    1400       34531 :     if (isint1(a)) a = gen_1;
    1401             :   }
    1402       59857 :   aA = (a == gen_1)? idealhnf_shallow(nf,A): idealmul(nf,a,A);
    1403       59857 :   bB = idealmul(nf,b,B);
    1404       59857 :   d = idealadd(nf,aA,bB);
    1405       59857 :   if (gequal(aA, d)) return zero_nfbezout(nf,d, a,B,A,pv,pu,pw,pdi);
    1406       27195 :   if (gequal(bB, d)) return zero_nfbezout(nf,d, b,A,B,pu,pv,pw,pdi);
    1407             :   /* general case is slow */
    1408        3780 :   di = idealHNF_inv(nf,d);
    1409        3780 :   aA = idealmul(nf,aA,di); /* integral */
    1410        3780 :   bB = idealmul(nf,bB,di); /* integral */
    1411             : 
    1412        3780 :   u = red? idealaddtoone_i(nf, aA, bB): idealaddtoone_raw(nf, aA, bB);
    1413        3780 :   w = idealmul(nf,aA,B);
    1414        3780 :   v = nfdiv(nf, nfsub(nf, gen_1, u), b);
    1415        3780 :   if (a != gen_1)
    1416             :   {
    1417        1190 :     GEN inva = nfinv(nf, a);
    1418        1190 :     u =  nfmul(nf,u,inva);
    1419        1190 :     w = idealmul(nf, inva, w); /* AB/d */
    1420             :   }
    1421        3780 :   *pu = u; *pv = v; *pw = w; *pdi = di; return d;
    1422             : }
    1423             : /* v a vector of ideals, simplify in place the ones generated by elts of Q */
    1424             : static void
    1425        8351 : idV_simplify(GEN v)
    1426             : {
    1427        8351 :   long i, l = lg(v);
    1428       48622 :   for (i = 1; i < l; i++)
    1429             :   {
    1430       40271 :     GEN M = gel(v,i);
    1431       40271 :     if (typ(M)==t_MAT && RgM_isscalar(M,NULL))
    1432       12929 :       gel(v,i) = Q_abs_shallow(gcoeff(M,1,1));
    1433             :   }
    1434        8351 : }
    1435             : /* Given a torsion-free module x outputs a pseudo-basis for x in HNF */
    1436             : GEN
    1437        5971 : nfhnf0(GEN nf, GEN x, long flag)
    1438             : {
    1439             :   long i, j, def, idef, m, n;
    1440        5971 :   pari_sp av0 = avma, av;
    1441             :   GEN y, A, I, J, U;
    1442             : 
    1443        5971 :   nf = checknf(nf);
    1444        5971 :   check_ZKmodule(x, "nfhnf");
    1445        5971 :   A = gel(x,1); RgM_dimensions(A, &m, &n);
    1446        5971 :   I = gel(x,2);
    1447        5971 :   if (!n) {
    1448          49 :     if (!flag) return gcopy(x);
    1449           0 :     retmkvec2(gcopy(x), cgetg(1,t_MAT));
    1450             :   }
    1451        5922 :   U = flag? matid(n): NULL;
    1452        5922 :   idef = (n < m)? m-n : 0;
    1453        5922 :   av = avma;
    1454        5922 :   A = RgM_to_nfM(nf,A);
    1455        5922 :   I = leafcopy(I);
    1456        5922 :   J = zerovec(n); def = n;
    1457       34706 :   for (i=m; i>idef; i--)
    1458             :   {
    1459       28784 :     GEN d, di = NULL;
    1460             : 
    1461       29029 :     j=def; while (j>=1 && isintzero(gcoeff(A,i,j))) j--;
    1462       28784 :     if (!j)
    1463             :     { /* no pivot on line i */
    1464           7 :       if (idef) idef--;
    1465           7 :       continue;
    1466             :     }
    1467       28777 :     if (j==def) j--;
    1468             :     else {
    1469          63 :       swap(gel(A,j), gel(A,def));
    1470          63 :       swap(gel(I,j), gel(I,def));
    1471          63 :       if (U) swap(gel(U,j), gel(U,def));
    1472             :     }
    1473      172725 :     for (  ; j; j--)
    1474             :     {
    1475      143948 :       GEN a,b, u,v,w, S, T, S0, T0 = gel(A,j);
    1476      143948 :       b = gel(T0,i); if (isintzero(b)) continue;
    1477             : 
    1478       36414 :       S0 = gel(A,def); a = gel(S0,i);
    1479       36414 :       d = nfbezout(nf, a,b, gel(I,def),gel(I,j), &u,&v,&w,&di,1);
    1480       36414 :       S = colcomb(nf, u,v, S0,T0);
    1481       36414 :       T = colcomb(nf, a,gneg(b), T0,S0);
    1482       36414 :       gel(A,def) = S; gel(A,j) = T;
    1483       36414 :       gel(I,def) = d; gel(I,j) = w;
    1484       36414 :       if (U)
    1485             :       {
    1486          42 :         S0 = gel(U,def);
    1487          42 :         T0 = gel(U,j);
    1488          42 :         gel(U,def) = colcomb(nf, u,v, S0,T0);
    1489          42 :         gel(U,j) = colcomb(nf, a,gneg(b), T0,S0);
    1490             :       }
    1491             :     }
    1492       28777 :     y = gcoeff(A,i,def);
    1493       28777 :     if (!isint1(y))
    1494             :     {
    1495         637 :       GEN yi = nfinv(nf,y);
    1496         637 :       gel(A,def) = nfC_nf_mul(nf, gel(A,def), yi);
    1497         637 :       gel(I,def) = idealmul(nf, y, gel(I,def));
    1498         637 :       if (U) gel(U,def) = nfC_nf_mul(nf, gel(U,def), yi);
    1499         637 :       di = NULL;
    1500             :     }
    1501       28777 :     if (!di) di = idealinv(nf,gel(I,def));
    1502       28777 :     d = gel(I,def);
    1503       28777 :     gel(J,def) = di;
    1504       95095 :     for (j=def+1; j<=n; j++)
    1505             :     {
    1506       66318 :       GEN mc, c = gcoeff(A,i,j); if (isintzero(c)) continue;
    1507       22967 :       c = element_close(nf, c, idealmul(nf,d,gel(J,j)));
    1508       22967 :       mc = gneg(c);
    1509       22967 :       gel(A,j) = colcomb1(nf, mc, gel(A,j),gel(A,def));
    1510       22967 :       if (U) gel(U,j) = colcomb1(nf, mc, gel(U,j),gel(U,def));
    1511             :     }
    1512       28777 :     def--;
    1513       28777 :     if (gc_needed(av,2))
    1514             :     {
    1515           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"nfhnf, i = %ld", i);
    1516           0 :       gerepileall(av,U?4:3, &A,&I,&J,&U);
    1517             :     }
    1518             :   }
    1519        5922 :   n -= def;
    1520        5922 :   A += def; A[0] = evaltyp(t_MAT)|evallg(n+1);
    1521        5922 :   I += def; I[0] = evaltyp(t_VEC)|evallg(n+1);
    1522        5922 :   idV_simplify(I);
    1523        5922 :   x = mkvec2(A,I);
    1524        5922 :   if (U) x = mkvec2(x,U);
    1525        5922 :   return gerepilecopy(av0, x);
    1526             : }
    1527             : 
    1528             : GEN
    1529        5957 : nfhnf(GEN nf, GEN x) { return nfhnf0(nf, x, 0); }
    1530             : 
    1531             : static GEN
    1532           0 : RgV_find_denom(GEN x)
    1533             : {
    1534           0 :   long i, l = lg(x);
    1535           0 :   for (i = 1; i < l; i++)
    1536           0 :     if (Q_denom(gel(x,i)) != gen_1) return gel(x,i);
    1537           0 :   return NULL;
    1538             : }
    1539             : /* A torsion module M over Z_K will be given by a row vector [A,I,J] with
    1540             :  * three components. I=[b_1,...,b_n] is a row vector of n fractional ideals
    1541             :  * given in HNF, J=[a_1,...,a_n] is a row vector of n fractional ideals in
    1542             :  * HNF. A is an nxn matrix (same n) such that if A_j is the j-th column of A
    1543             :  * and e_n is the canonical basis of K^n, then
    1544             :  * M=(b_1e_1+...+b_ne_n)/(a_1A_1+...a_nA_n) */
    1545             : 
    1546             : /* x=[A,I,J] a torsion module as above. Output the
    1547             :  * smith normal form as K=[c_1,...,c_n] such that x = Z_K/c_1+...+Z_K/c_n */
    1548             : GEN
    1549          21 : nfsnf0(GEN nf, GEN x, long flag)
    1550             : {
    1551             :   long i, j, k, l, n, m;
    1552             :   pari_sp av;
    1553             :   GEN z,u,v,w,d,dinv,A,I,J, U,V;
    1554             : 
    1555          21 :   nf = checknf(nf);
    1556          21 :   if (typ(x)!=t_VEC || lg(x)!=4) pari_err_TYPE("nfsnf",x);
    1557          21 :   A = gel(x,1);
    1558          21 :   I = gel(x,2);
    1559          21 :   J = gel(x,3);
    1560          21 :   if (typ(A)!=t_MAT) pari_err_TYPE("nfsnf",A);
    1561          21 :   n = lg(A)-1;
    1562          21 :   if (typ(I)!=t_VEC) pari_err_TYPE("nfsnf",I);
    1563          21 :   if (typ(J)!=t_VEC) pari_err_TYPE("nfsnf",J);
    1564          21 :   if (lg(I)!=n+1 || lg(J)!=n+1) pari_err_DIM("nfsnf");
    1565          21 :   RgM_dimensions(A, &m, &n);
    1566          21 :   if (!n || n != m) pari_err_IMPL("nfsnf for empty or non square matrices");
    1567             : 
    1568          21 :   av = avma;
    1569          21 :   if (!flag) U = V = NULL;
    1570             :   else
    1571             :   {
    1572           7 :     U = matid(m);
    1573           7 :     V = matid(n);
    1574             :   }
    1575          21 :   A = RgM_to_nfM(nf, A);
    1576          21 :   I = leafcopy(I);
    1577          21 :   J = leafcopy(J);
    1578          84 :   for (i = 1; i <= n; i++) gel(J,i) = idealinv(nf, gel(J,i));
    1579          21 :   z = zerovec(n);
    1580         126 :   for (i=n; i>=1; i--)
    1581             :   {
    1582             :     GEN Aii, a, b, db;
    1583         105 :     long c = 0;
    1584         238 :     for (j=i-1; j>=1; j--)
    1585             :     {
    1586         133 :       GEN S, T, S0, T0 = gel(A,j);
    1587         133 :       b = gel(T0,i); if (gequal0(b)) continue;
    1588             : 
    1589          49 :       S0 = gel(A,i); a = gel(S0,i);
    1590          49 :       d = nfbezout(nf, a,b, gel(J,i),gel(J,j), &u,&v,&w,&dinv,1);
    1591          49 :       S = colcomb(nf, u,v, S0,T0);
    1592          49 :       T = colcomb(nf, a,gneg(b), T0,S0);
    1593          49 :       gel(A,i) = S; gel(A,j) = T;
    1594          49 :       gel(J,i) = d; gel(J,j) = w;
    1595          49 :       if (V)
    1596             :       {
    1597          21 :         T0 = gel(V,j);
    1598          21 :         S0 = gel(V,i);
    1599          21 :         gel(V,i) = colcomb(nf, u,v, S0,T0);
    1600          21 :         gel(V,j) = colcomb(nf, a,gneg(b), T0,S0);
    1601             :       }
    1602             :     }
    1603         238 :     for (j=i-1; j>=1; j--)
    1604             :     {
    1605             :       GEN ri, rj;
    1606         133 :       b = gcoeff(A,j,i); if (gequal0(b)) continue;
    1607             : 
    1608          56 :       a = gcoeff(A,i,i);
    1609          56 :       d = nfbezout(nf, a,b, gel(I,i),gel(I,j), &u,&v,&w,&dinv,1);
    1610          56 :       ri = rowcomb(nf, u,v,       i,j, A, i);
    1611          56 :       rj = rowcomb(nf, a,gneg(b), j,i, A, i);
    1612         210 :       for (k=1; k<=i; k++) {
    1613         154 :         gcoeff(A,j,k) = gel(rj,k);
    1614         154 :         gcoeff(A,i,k) = gel(ri,k);
    1615             :       }
    1616          56 :       if (U)
    1617             :       {
    1618          21 :         ri = rowcomb(nf, u,v,       i,j, U, m);
    1619          21 :         rj = rowcomb(nf, a,gneg(b), j,i, U, m);
    1620          84 :         for (k=1; k<=m; k++) {
    1621          63 :           gcoeff(U,j,k) = gel(rj,k);
    1622          63 :           gcoeff(U,i,k) = gel(ri,k);
    1623             :         }
    1624             :       }
    1625          56 :       gel(I,i) = d; gel(I,j) = w; c = 1;
    1626             :     }
    1627         105 :     if (c) { i++; continue; }
    1628             : 
    1629          63 :     Aii = gcoeff(A,i,i); if (gequal0(Aii)) continue;
    1630          63 :     gel(J,i) = idealmul(nf, gel(J,i), Aii);
    1631          63 :     gcoeff(A,i,i) = gen_1;
    1632          63 :     if (V) gel(V,i) = nfC_nf_mul(nf, gel(V,i), nfinv(nf,Aii));
    1633          63 :     gel(z,i) = idealmul(nf,gel(J,i),gel(I,i));
    1634          63 :     b = Q_remove_denom(gel(z,i), &db);
    1635         126 :     for (k=1; k<i; k++)
    1636         168 :       for (l=1; l<i; l++)
    1637             :       {
    1638         105 :         GEN d, D, p1, p2, p3, Akl = gcoeff(A,k,l);
    1639             :         long t;
    1640         105 :         if (gequal0(Akl)) continue;
    1641             : 
    1642          91 :         p1 = idealmul(nf,Akl,gel(J,l));
    1643          91 :         p3 = idealmul(nf, p1, gel(I,k));
    1644          91 :         if (db) p3 = RgM_Rg_mul(p3, db);
    1645          91 :         if (RgM_is_ZM(p3) && hnfdivide(b, p3)) continue;
    1646             : 
    1647             :         /* find d in D = I[k]/I[i] not in J[i]/(A[k,l] J[l]) */
    1648           0 :         D = idealdiv(nf,gel(I,k),gel(I,i));
    1649           0 :         p2 = idealdiv(nf,gel(J,i), p1);
    1650           0 :         d = RgV_find_denom(QM_gauss(p2, D));
    1651           0 :         if (!d) pari_err_BUG("nfsnf");
    1652           0 :         p1 = element_mulvecrow(nf,d,A,k,i);
    1653           0 :         for (t=1; t<=i; t++) gcoeff(A,i,t) = gadd(gcoeff(A,i,t),gel(p1,t));
    1654           0 :         if (U)
    1655             :         {
    1656           0 :           p1 = element_mulvecrow(nf,d,U,k,i);
    1657           0 :           for (t=1; t<=i; t++) gcoeff(U,i,t) = gadd(gcoeff(U,i,t),gel(p1,t));
    1658             :         }
    1659             : 
    1660           0 :         k = i; c = 1; break;
    1661             :       }
    1662          63 :     if (gc_needed(av,1))
    1663             :     {
    1664           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"nfsnf");
    1665           0 :       gerepileall(av,U?6:4, &A,&I,&J,&z,&U,&V);
    1666             :     }
    1667          63 :     if (c) i++; /* iterate on row/column i */
    1668             :   }
    1669          21 :   if (U) z = mkvec3(z,U,V);
    1670          21 :   return gerepilecopy(av, z);
    1671             : }
    1672             : GEN
    1673           0 : nfsnf(GEN nf, GEN x) { return nfsnf0(nf,x,0); }
    1674             : 
    1675             : /* Given a pseudo-basis x, outputs a multiple of its ideal determinant */
    1676             : GEN
    1677         280 : nfdetint(GEN nf, GEN x)
    1678             : {
    1679             :   GEN pass,c,v,det1,piv,pivprec,vi,p1,A,I,id,idprod;
    1680         280 :   long i, j, k, rg, n, m, m1, cm=0, N;
    1681         280 :   pari_sp av = avma, av1;
    1682             : 
    1683         280 :   nf = checknf(nf); N = nf_get_degree(nf);
    1684         280 :   check_ZKmodule(x, "nfdetint");
    1685         280 :   A = gel(x,1);
    1686         280 :   I = gel(x,2);
    1687         280 :   n = lg(A)-1; if (!n) return gen_1;
    1688             : 
    1689         280 :   m1 = lgcols(A); m = m1-1;
    1690         280 :   id = matid(N);
    1691        1267 :   c = new_chunk(m1); for (k=1; k<=m; k++) c[k] = 0;
    1692         280 :   piv = pivprec = gen_1;
    1693             : 
    1694         280 :   av1 = avma;
    1695         280 :   det1 = idprod = gen_0; /* dummy for gerepileall */
    1696         280 :   pass = cgetg(m1,t_MAT);
    1697         280 :   v = cgetg(m1,t_COL);
    1698        1267 :   for (j=1; j<=m; j++)
    1699             :   {
    1700         987 :     gel(pass,j) = zerocol(m);
    1701         987 :     gel(v,j) = gen_0; /* dummy */
    1702             :   }
    1703        1617 :   for (rg=0,k=1; k<=n; k++)
    1704             :   {
    1705        1337 :     long t = 0;
    1706        8827 :     for (i=1; i<=m; i++)
    1707        7490 :       if (!c[i])
    1708             :       {
    1709        3857 :         vi=nfmul(nf,piv,gcoeff(A,i,k));
    1710       37996 :         for (j=1; j<=m; j++)
    1711       34139 :           if (c[j]) vi=gadd(vi,nfmul(nf,gcoeff(pass,i,j),gcoeff(A,j,k)));
    1712        3857 :         gel(v,i) = vi; if (!t && !gequal0(vi)) t=i;
    1713             :       }
    1714        1337 :     if (t)
    1715             :     {
    1716        1337 :       pivprec = piv;
    1717        1337 :       if (rg == m-1)
    1718             :       {
    1719         630 :         if (!cm)
    1720             :         {
    1721         280 :           cm=1; idprod = id;
    1722        1267 :           for (i=1; i<=m; i++)
    1723         987 :             if (i!=t)
    1724         707 :               idprod = (idprod==id)? gel(I,c[i])
    1725         707 :                                    : idealmul(nf,idprod,gel(I,c[i]));
    1726             :         }
    1727         630 :         p1 = idealmul(nf,gel(v,t),gel(I,k)); c[t]=0;
    1728         630 :         det1 = (typ(det1)==t_INT)? p1: idealadd(nf,p1,det1);
    1729             :       }
    1730             :       else
    1731             :       {
    1732         707 :         rg++; piv=gel(v,t); c[t]=k;
    1733        5747 :         for (i=1; i<=m; i++)
    1734        5040 :           if (!c[i])
    1735             :           {
    1736       29169 :             for (j=1; j<=m; j++)
    1737       26649 :               if (c[j] && j!=t)
    1738             :               {
    1739        7203 :                 p1 = gsub(nfmul(nf,piv,gcoeff(pass,i,j)),
    1740        7203 :                           nfmul(nf,gel(v,i),gcoeff(pass,t,j)));
    1741        7203 :                 gcoeff(pass,i,j) = rg>1? nfdiv(nf,p1,pivprec)
    1742        7203 :                                        : p1;
    1743             :               }
    1744        2520 :             gcoeff(pass,i,t) = gneg(gel(v,i));
    1745             :           }
    1746             :       }
    1747             :     }
    1748        1337 :     if (gc_needed(av1,1))
    1749             :     {
    1750           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"nfdetint");
    1751           0 :       gerepileall(av1,6, &det1,&piv,&pivprec,&pass,&v,&idprod);
    1752             :     }
    1753             :   }
    1754         280 :   if (!cm) { set_avma(av); return cgetg(1,t_MAT); }
    1755         280 :   return gerepileupto(av, idealmul(nf,idprod,det1));
    1756             : }
    1757             : 
    1758             : /* reduce in place components of x[1..lim] mod D (destroy x). D in HNF */
    1759             : static void
    1760       21826 : nfcleanmod(GEN nf, GEN x, long lim, GEN D)
    1761             : {
    1762             :   GEN DZ, DZ2, dD;
    1763             :   long i;
    1764       21826 :   D = Q_remove_denom(D, &dD);
    1765       21826 :   DZ = gcoeff(D,1,1); DZ2 = shifti(DZ, -1);
    1766      107569 :   for (i = 1; i <= lim; i++)
    1767             :   {
    1768       85743 :     GEN c = nf_to_scalar_or_basis(nf, gel(x,i));
    1769       85743 :     switch(typ(c)) /* c = centermod(c, D) */
    1770             :     {
    1771       75915 :       case t_INT:
    1772       75915 :         if (!signe(c)) break;
    1773       41734 :         if (dD) c = mulii(c, dD);
    1774       41734 :         c = centermodii(c, DZ, DZ2);
    1775       41734 :         if (dD) c = Qdivii(c,dD);
    1776       41734 :         break;
    1777         217 :       case t_FRAC: {
    1778         217 :         GEN dc = gel(c,2), nc = gel(c,1), N = mulii(DZ, dc);
    1779         217 :         if (dD) nc = mulii(nc, dD);
    1780         217 :         c = centermodii(nc, N, shifti(N,-1));
    1781         217 :         c = Qdivii(c, dD ? mulii(dc,dD): dc);
    1782         217 :         break;
    1783             :       }
    1784        9611 :       case t_COL: {
    1785             :         GEN dc;
    1786        9611 :         c = Q_remove_denom(c, &dc);
    1787        9611 :         if (dD) c = ZC_Z_mul(c, dD);
    1788        9611 :         c = ZC_hnfrem(c, dc? ZM_Z_mul(D,dc): D);
    1789        9611 :         dc = mul_content(dc, dD);
    1790        9611 :         if (ZV_isscalar(c))
    1791             :         {
    1792         161 :           c = gel(c,1);
    1793         161 :           if (dc) c = Qdivii(c,dc);
    1794             :         }
    1795             :         else
    1796        9450 :           if (dc) c = RgC_Rg_div(c, dc);
    1797        9611 :         break;
    1798             :       }
    1799             :     }
    1800       85743 :     gel(x,i) = c;
    1801             :   }
    1802       21826 : }
    1803             : 
    1804             : GEN
    1805        2429 : nfhnfmod(GEN nf, GEN x, GEN D)
    1806             : {
    1807             :   long li, co, i, j, def, ldef;
    1808        2429 :   pari_sp av0=avma, av;
    1809             :   GEN dA, dI, d0, w, p1, d, u, v, A, I, J, di;
    1810             : 
    1811        2429 :   nf = checknf(nf);
    1812        2429 :   check_ZKmodule(x, "nfhnfmod");
    1813        2429 :   A = gel(x,1);
    1814        2429 :   I = gel(x,2);
    1815        2429 :   co = lg(A); if (co==1) return cgetg(1,t_MAT);
    1816             : 
    1817        2429 :   li = lgcols(A);
    1818        2429 :   if (typ(D)!=t_MAT) D = idealhnf_shallow(nf, D);
    1819        2429 :   D = Q_remove_denom(D, NULL);
    1820        2429 :   RgM_check_ZM(D, "nfhnfmod");
    1821             : 
    1822        2429 :   av = avma;
    1823        2429 :   A = RgM_to_nfM(nf, A);
    1824        2429 :   A = Q_remove_denom(A, &dA);
    1825        2429 :   I = Q_remove_denom(leafcopy(I), &dI);
    1826        2429 :   dA = mul_denom(dA,dI);
    1827        2429 :   if (dA) D = ZM_Z_mul(D, powiu(dA, minss(li,co)));
    1828             : 
    1829        2429 :   def = co; ldef = (li>co)? li-co+1: 1;
    1830       13923 :   for (i=li-1; i>=ldef; i--)
    1831             :   {
    1832       13363 :     def--; j=def; while (j>=1 && isintzero(gcoeff(A,i,j))) j--;
    1833       11494 :     if (!j) continue;
    1834       11494 :     if (j==def) j--;
    1835             :     else {
    1836        1624 :       swap(gel(A,j), gel(A,def));
    1837        1624 :       swap(gel(I,j), gel(I,def));
    1838             :     }
    1839       54663 :     for (  ; j; j--)
    1840             :     {
    1841       43169 :       GEN a, b, S, T, S0, T0 = gel(A,j);
    1842       43169 :       b = gel(T0,i); if (isintzero(b)) continue;
    1843             : 
    1844       11844 :       S0 = gel(A,def); a = gel(S0,i);
    1845       11844 :       d = nfbezout(nf, a,b, gel(I,def),gel(I,j), &u,&v,&w,&di,0);
    1846       11844 :       S = colcomb(nf, u,v, S0,T0);
    1847       11844 :       T = colcomb(nf, a,gneg(b), T0,S0);
    1848       11844 :       if (u != gen_0 && v != gen_0) /* already reduced otherwise */
    1849         917 :         nfcleanmod(nf, S, i, idealmul(nf,D,di));
    1850       11844 :       nfcleanmod(nf, T, i, idealdiv(nf,D,w));
    1851       11844 :       gel(A,def) = S; gel(A,j) = T;
    1852       11844 :       gel(I,def) = d; gel(I,j) = w;
    1853             :     }
    1854       11494 :     if (gc_needed(av,2))
    1855             :     {
    1856           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"[1]: nfhnfmod, i = %ld", i);
    1857           0 :       gerepileall(av,dA? 4: 3, &A,&I,&D,&dA);
    1858             :     }
    1859             :   }
    1860        2429 :   def--; d0 = D;
    1861        2429 :   A += def; A[0] = evaltyp(t_MAT)|evallg(li);
    1862        2429 :   I += def; I[0] = evaltyp(t_VEC)|evallg(li);
    1863        2429 :   J = cgetg(li,t_VEC);
    1864       13923 :   for (i=li-1; i>=1; i--)
    1865             :   {
    1866       11494 :     GEN b = gcoeff(A,i,i);
    1867       11494 :     d = nfbezout(nf, gen_1,b, d0,gel(I,i), &u,&v,&w,&di,0);
    1868       11494 :     p1 = nfC_nf_mul(nf,gel(A,i),v);
    1869       11494 :     if (i > 1)
    1870             :     {
    1871        9065 :       d0 = idealmul(nf,d0,di);
    1872        9065 :       nfcleanmod(nf, p1, i, d0);
    1873             :     }
    1874       11494 :     gel(A,i) = p1; gel(p1,i) = gen_1;
    1875       11494 :     gel(I,i) = d;
    1876       11494 :     gel(J,i) = di;
    1877             :   }
    1878       11494 :   for (i=li-2; i>=1; i--)
    1879             :   {
    1880        9065 :     d = gel(I,i);
    1881       37919 :     for (j=i+1; j<li; j++)
    1882             :     {
    1883       28854 :       GEN c = gcoeff(A,i,j); if (isintzero(c)) continue;
    1884       11046 :       c = element_close(nf, c, idealmul(nf,d,gel(J,j)));
    1885       11046 :       gel(A,j) = colcomb1(nf, gneg(c), gel(A,j),gel(A,i));
    1886             :     }
    1887        9065 :     if (gc_needed(av,2))
    1888             :     {
    1889           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"[2]: nfhnfmod, i = %ld", i);
    1890           0 :       gerepileall(av,dA? 4: 3, &A,&I,&J,&dA);
    1891             :     }
    1892             :   }
    1893        2429 :   idV_simplify(I);
    1894        2429 :   if (dA) I = gdiv(I,dA);
    1895        2429 :   return gerepilecopy(av0, mkvec2(A, I));
    1896             : }

Generated by: LCOV version 1.13