Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13