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

Generated by: LCOV version 1.11