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

Generated by: LCOV version 1.13