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 - base4.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21342-bb34613) Lines: 1303 1448 90.0 %
Date: 2017-11-18 06:21:14 Functions: 130 143 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                       BASIC NF OPERATIONS                       */
      17             : /*                           (continued)                           */
      18             : /*                                                                 */
      19             : /*******************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /*******************************************************************/
      24             : /*                                                                 */
      25             : /*                     IDEAL OPERATIONS                            */
      26             : /*                                                                 */
      27             : /*******************************************************************/
      28             : 
      29             : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
      30             :  * on the integer basis in HNF.
      31             :  * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
      32             :  * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
      33             :  * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
      34             :  *
      35             :  * An extended ideal is a couple [I,F] where I is a valid ideal and F is
      36             :  * either an algebraic number, or a factorization matrix attached to an
      37             :  * algebraic number. All routines work with either extended ideals or ideals
      38             :  * (an omitted F is assumed to be [;] <-> 1).
      39             :  * All ideals are output in HNF form. */
      40             : 
      41             : /* types and conversions */
      42             : 
      43             : long
      44     3118787 : idealtyp(GEN *ideal, GEN *arch)
      45             : {
      46     3118787 :   GEN x = *ideal;
      47     3118787 :   long t,lx,tx = typ(x);
      48             : 
      49     3118787 :   if (tx==t_VEC && lg(x)==3)
      50      328220 :   { *arch = gel(x,2); x = gel(x,1); tx = typ(x); }
      51             :   else
      52     2790567 :     *arch = NULL;
      53     3118787 :   switch(tx)
      54             :   {
      55     1569862 :     case t_MAT: lx = lg(x);
      56     1569862 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      57     1569785 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      58     1569778 :       t = id_MAT;
      59     1569778 :       break;
      60             : 
      61     1150624 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      62     1150610 :       t = id_PRIME; break;
      63             : 
      64             :     case t_POL: case t_POLMOD: case t_COL:
      65             :     case t_INT: case t_FRAC:
      66      398301 :       t = id_PRINCIPAL; break;
      67             :     default:
      68           0 :       pari_err_TYPE("idealtyp",x);
      69             :       return 0; /*LCOV_EXCL_LINE*/
      70             :   }
      71     3118766 :   *ideal = x; return t;
      72             : }
      73             : 
      74             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      75             : GEN
      76      107843 : idealhnf_two(GEN nf, GEN v)
      77             : {
      78      107843 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      79      107843 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      80       93633 :   return ZM_hnfmodid(m, p);
      81             : }
      82             : /* true nf */
      83             : GEN
      84     1161567 : pr_hnf(GEN nf, GEN pr)
      85             : {
      86     1161567 :   GEN p = pr_get_p(pr), m;
      87     1161567 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      88      904163 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      89      904163 :   return ZM_hnfmodprime(m, p);
      90             : }
      91             : 
      92             : GEN
      93      267622 : idealhnf_principal(GEN nf, GEN x)
      94             : {
      95             :   GEN cx;
      96      267622 :   x = nf_to_scalar_or_basis(nf, x);
      97      267622 :   switch(typ(x))
      98             :   {
      99      153077 :     case t_COL: break;
     100       90893 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     101       90508 :       return scalarmat(absi(x), nf_get_degree(nf));
     102             :     case t_FRAC:
     103       23652 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     104           0 :     default: pari_err_TYPE("idealhnf",x);
     105             :   }
     106      153077 :   x = Q_primitive_part(x, &cx);
     107      153077 :   RgV_check_ZV(x, "idealhnf");
     108      153077 :   x = zk_multable(nf, x);
     109      153077 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     110      153077 :   return cx? ZM_Q_mul(x,cx): x;
     111             : }
     112             : 
     113             : /* x integral ideal in t_MAT form, nx columns */
     114             : static GEN
     115           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     116             : {
     117           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     118             :   long i, j, k;
     119          21 :   for (i=k=1; i<=nx; i++)
     120          14 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     121           7 :   return m;
     122             : }
     123             : /* true nf */
     124             : GEN
     125      320864 : idealhnf_shallow(GEN nf, GEN x)
     126             : {
     127      320864 :   long tx = typ(x), lx = lg(x), N;
     128             : 
     129             :   /* cannot use idealtyp because here we allow non-square matrices */
     130      320864 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     131      320864 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     132      221529 :   switch(tx)
     133             :   {
     134             :     case t_MAT:
     135             :     {
     136             :       GEN cx;
     137       46823 :       long nx = lx-1;
     138       46823 :       N = nf_get_degree(nf);
     139       46823 :       if (nx == 0) return cgetg(1, t_MAT);
     140       46802 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     141       46795 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     142             : 
     143       45605 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     144       22330 :       x = Q_primitive_part(x, &cx);
     145       22330 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     146       22330 :       x = ZM_hnfmod(x, ZM_detmult(x));
     147       22330 :       return cx? ZM_Q_mul(x,cx): x;
     148             :     }
     149             :     case t_QFI:
     150             :     case t_QFR:
     151             :     {
     152          14 :       pari_sp av = avma;
     153          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     154          14 :       GEN A = gel(x,1), B = gel(x,2);
     155          14 :       N = nf_get_degree(nf);
     156          14 :       if (N != 2)
     157           0 :         pari_err_TYPE("idealhnf [Qfb for non-quadratic fields]", x);
     158          14 :       if (!equalii(qfb_disc(x), D))
     159           7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     160             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     161             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     162             :          => t = (-u + sqrt(D) f)/2
     163             :          => sqrt(D)/2 = (t + u/2)/f */
     164           7 :       u = gel(T,3);
     165           7 :       B = deg1pol_shallow(ginv(f),
     166             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     167           7 :                           varn(T));
     168           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     169             :     }
     170      174692 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     171             :   }
     172             : }
     173             : GEN
     174        3094 : idealhnf(GEN nf, GEN x)
     175             : {
     176        3094 :   pari_sp av = avma;
     177        3094 :   GEN y = idealhnf_shallow(checknf(nf), x);
     178        3080 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     179             : }
     180             : 
     181             : /* GP functions */
     182             : 
     183             : GEN
     184          63 : idealtwoelt0(GEN nf, GEN x, GEN a)
     185             : {
     186          63 :   if (!a) return idealtwoelt(nf,x);
     187          42 :   return idealtwoelt2(nf,x,a);
     188             : }
     189             : 
     190             : GEN
     191          42 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     192             : {
     193          42 :   if (flag) return idealpowred(nf,x,n);
     194          35 :   return idealpow(nf,x,n);
     195             : }
     196             : 
     197             : GEN
     198          56 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     199             : {
     200          56 :   if (flag) return idealmulred(nf,x,y);
     201          49 :   return idealmul(nf,x,y);
     202             : }
     203             : 
     204             : GEN
     205          42 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     206             : {
     207          42 :   switch(flag)
     208             :   {
     209          21 :     case 0: return idealdiv(nf,x,y);
     210          21 :     case 1: return idealdivexact(nf,x,y);
     211           0 :     default: pari_err_FLAG("idealdiv");
     212             :   }
     213             :   return NULL; /* LCOV_EXCL_LINE */
     214             : }
     215             : 
     216             : GEN
     217          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     218             : {
     219          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     220          35 :   return idealaddtoone(nf,arg1,arg2);
     221             : }
     222             : 
     223             : /* b not a scalar */
     224             : static GEN
     225          28 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     226             : /* b not a scalar */
     227             : static GEN
     228          21 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     229             : {
     230             :   GEN db;
     231          21 :   b = Q_remove_denom(b, &db);
     232          21 :   if (db) a = mulii(a, db);
     233          21 :   b = hnf_Z_ZC(nf,a,b);
     234          21 :   return db? RgM_Rg_div(b, db): b;
     235             : }
     236             : /* b not a scalar (not point in trying to optimize for this case) */
     237             : static GEN
     238          28 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     239             : {
     240             :   GEN da, db;
     241          28 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     242           7 :   da = gel(a,2);
     243           7 :   a = gel(a,1);
     244           7 :   b = Q_remove_denom(b, &db);
     245             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     246             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     247           7 :   if (db)
     248             :   {
     249           7 :     GEN d = gcdii(da,db);
     250           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     251           7 :     if (!is_pm1(db))
     252             :     {
     253           7 :       a = mulii(a, db); /* a B */
     254           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     255             :     }
     256             :   }
     257           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     258             : }
     259             : static GEN
     260           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     261             : {
     262             :   GEN da, db, d, x;
     263           7 :   a = Q_remove_denom(a, &da);
     264           7 :   b = Q_remove_denom(b, &db);
     265           7 :   if (da) b = ZC_Z_mul(b, da);
     266           7 :   if (db) a = ZC_Z_mul(a, db);
     267           7 :   d = mul_denom(da, db);
     268           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     269           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     270           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     271           7 :   return d? RgM_Rg_div(x, d): x;
     272             : }
     273             : static GEN
     274          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     275             : GEN
     276         119 : idealhnf0(GEN nf, GEN a, GEN b)
     277             : {
     278             :   long ta, tb;
     279             :   pari_sp av;
     280             :   GEN x;
     281         119 :   if (!b) return idealhnf(nf,a);
     282             : 
     283             :   /* HNF of aZ_K+bZ_K */
     284          56 :   av = avma; nf = checknf(nf);
     285          56 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     286          56 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     287          56 :   if (ta == t_COL)
     288          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     289             :   else
     290          42 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     291          56 :   return gerepileupto(av, x);
     292             : }
     293             : 
     294             : /*******************************************************************/
     295             : /*                                                                 */
     296             : /*                       TWO-ELEMENT FORM                          */
     297             : /*                                                                 */
     298             : /*******************************************************************/
     299             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     300             : 
     301             : static int
     302      148023 : ok_elt(GEN x, GEN xZ, GEN y)
     303             : {
     304      148023 :   pari_sp av = avma;
     305      148023 :   int r = ZM_equal(x, ZM_hnfmodid(y, xZ));
     306      148023 :   avma = av; return r;
     307             : }
     308             : 
     309             : static GEN
     310       51962 : addmul_col(GEN a, long s, GEN b)
     311             : {
     312             :   long i,l;
     313       51962 :   if (!s) return a? leafcopy(a): a;
     314       51795 :   if (!a) return gmulsg(s,b);
     315       48723 :   l = lg(a);
     316      260551 :   for (i=1; i<l; i++)
     317      211828 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     318       48723 :   return a;
     319             : }
     320             : 
     321             : /* a <-- a + s * b, all coeffs integers */
     322             : static GEN
     323       22967 : addmul_mat(GEN a, long s, GEN b)
     324             : {
     325             :   long j,l;
     326             :   /* copy otherwise next call corrupts a */
     327       22967 :   if (!s) return a? RgM_shallowcopy(a): a;
     328       21384 :   if (!a) return gmulsg(s,b);
     329       11529 :   l = lg(a);
     330       55709 :   for (j=1; j<l; j++)
     331       44180 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     332       11529 :   return a;
     333             : }
     334             : 
     335             : static GEN
     336       77323 : get_random_a(GEN nf, GEN x, GEN xZ)
     337             : {
     338             :   pari_sp av;
     339       77323 :   long i, lm, l = lg(x);
     340             :   GEN a, z, beta, mul;
     341             : 
     342       77323 :   beta= cgetg(l, t_VEC);
     343       77323 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     344             :   /* look for a in x such that a O/xZ = x O/xZ */
     345      151696 :   for (i = 2; i < l; i++)
     346             :   {
     347      148624 :     GEN xi = gel(x,i);
     348      148624 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     349      148624 :     if (gequal0(t)) continue;
     350      138168 :     if (ok_elt(x,xZ, t)) return xi;
     351       63917 :     gel(beta,lm) = xi;
     352             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     353       63917 :     gel(mul,lm) = t; lm++;
     354             :   }
     355        3072 :   setlg(mul, lm);
     356        3072 :   setlg(beta,lm);
     357        3072 :   z = cgetg(lm, t_VECSMALL);
     358        9904 :   for(av = avma;; avma = av)
     359             :   {
     360       32871 :     for (a=NULL,i=1; i<lm; i++)
     361             :     {
     362       22967 :       long t = random_bits(4) - 7; /* in [-7,8] */
     363       22967 :       z[i] = t;
     364       22967 :       a = addmul_mat(a, t, gel(mul,i));
     365             :     }
     366             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     367        9904 :     if (a && ok_elt(x,xZ, a)) break;
     368        6832 :   }
     369       10854 :   for (a=NULL,i=1; i<lm; i++)
     370        7782 :     a = addmul_col(a, z[i], gel(beta,i));
     371        3072 :   return a;
     372             : }
     373             : 
     374             : /* x square matrix, assume it is HNF */
     375             : static GEN
     376      194556 : mat_ideal_two_elt(GEN nf, GEN x)
     377             : {
     378             :   GEN y, a, cx, xZ;
     379      194556 :   long N = nf_get_degree(nf);
     380             :   pari_sp av, tetpil;
     381             : 
     382      194556 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     383      194542 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     384             : 
     385       87127 :   y = cgetg(3,t_VEC); av = avma;
     386       87127 :   cx = Q_content(x);
     387       87127 :   xZ = gcoeff(x,1,1);
     388       87127 :   if (gequal(xZ, cx)) /* x = (cx) */
     389             :   {
     390        3171 :     gel(y,1) = cx;
     391        3171 :     gel(y,2) = gen_0; return y;
     392             :   }
     393       83956 :   if (equali1(cx)) cx = NULL;
     394             :   else
     395             :   {
     396        1393 :     x = Q_div_to_int(x, cx);
     397        1393 :     xZ = gcoeff(x,1,1);
     398             :   }
     399       83956 :   if (N < 6)
     400       73162 :     a = get_random_a(nf, x, xZ);
     401             :   else
     402             :   {
     403       10794 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     404             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     405             :     };
     406       10794 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     407       10794 :     if (!a1) /* factors completely */
     408        6633 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     409        4161 :     else if (lg(P) == 1) /* no small factors */
     410        2954 :       a = get_random_a(nf, x, xZ);
     411             :     else /* general case */
     412             :     {
     413             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     414        1207 :       a0 = diviiexact(xZ, a1);
     415        1207 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     416        1207 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     417        1207 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     418        1207 :       pi1 = get_random_a(nf, A1, a1);
     419        1207 :       (void)bezout(a0, a1, &v0,&v1);
     420        1207 :       u0 = mulii(a0, v0);
     421        1207 :       u1 = mulii(a1, v1);
     422        1207 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     423             :       else
     424        1207 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     425        1207 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     426        1207 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     427             :     }
     428             :   }
     429       83956 :   if (cx)
     430             :   {
     431        1393 :     a = centermod(a, xZ);
     432        1393 :     tetpil = avma;
     433        1393 :     if (typ(cx) == t_INT)
     434             :     {
     435         273 :       gel(y,1) = mulii(xZ, cx);
     436         273 :       gel(y,2) = ZC_Z_mul(a, cx);
     437             :     }
     438             :     else
     439             :     {
     440        1120 :       gel(y,1) = gmul(xZ, cx);
     441        1120 :       gel(y,2) = RgC_Rg_mul(a, cx);
     442             :     }
     443             :   }
     444             :   else
     445             :   {
     446       82563 :     tetpil = avma;
     447       82563 :     gel(y,1) = icopy(xZ);
     448       82563 :     gel(y,2) = centermod(a, xZ);
     449             :   }
     450       83956 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     451             : }
     452             : 
     453             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     454             :  * x = a Z_K + alpha Z_K, alpha in K^*
     455             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     456             :  * x is principal. */
     457             : GEN
     458       38151 : idealtwoelt(GEN nf, GEN x)
     459             : {
     460             :   pari_sp av;
     461             :   GEN z;
     462       38151 :   long tx = idealtyp(&x,&z);
     463       38144 :   nf = checknf(nf);
     464       38144 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     465        1694 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     466             :   /* id_PRINCIPAL */
     467         875 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     468        1554 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     469         770 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     470             : }
     471             : 
     472             : /*******************************************************************/
     473             : /*                                                                 */
     474             : /*                         FACTORIZATION                           */
     475             : /*                                                                 */
     476             : /*******************************************************************/
     477             : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     478             : static long
     479      202067 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     480             : {
     481      202067 :   long i, v = Zval, l = lg(x);
     482      202067 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     483      202067 :   return v;
     484             : }
     485             : 
     486             : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     487             :  * x integral in HNF */
     488             : GEN
     489       36792 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     490             : {
     491       36792 :   GEN xZ = gcoeff(x,1,1), f, P, E, vN, vZ;
     492             :   long i, l;
     493       36792 :   if (typ(xZ) != t_INT) pari_err_TYPE("idealfactor",x);
     494       36792 :   f = Z_factor(xZ);
     495       36792 :   P = gel(f,1); l = lg(P);
     496       36792 :   E = gel(f,2);
     497       36792 :   *pvN = vN = cgetg(l, t_VECSMALL);
     498       36792 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     499       69333 :   for (i = 1; i < l; i++)
     500             :   {
     501       32541 :     vZ[i] = itou(gel(E,i));
     502       32541 :     vN[i] = idealHNF_norm_pval(x,gel(P,i), vZ[i]);
     503             :   }
     504       36792 :   return P;
     505             : }
     506             : 
     507             : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     508             :  * Return v_P(A) */
     509             : static long
     510      218816 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     511             : {
     512      218816 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     513             :   GEN mul, B, a, y, r, p, pk, cx, vals;
     514             :   pari_sp av;
     515             : 
     516      218816 :   if (Nval < f) return 0;
     517      218753 :   p = pr_get_p(P);
     518      218753 :   e = pr_get_e(P);
     519             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     520      218753 :   vmax = minss(Zval * e, Nval / f);
     521      218753 :   mul = pr_get_tau(P);
     522      218753 :   l = lg(mul);
     523      218753 :   B = cgetg(l,t_MAT);
     524             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     525      218753 :   gel(B,1) = gen_0; /* dummy */
     526      670186 :   for (j = 2; j < l; j++)
     527             :   {
     528      523198 :     GEN x = gel(A,j);
     529      523198 :     gel(B,j) = y = cgetg(l, t_COL);
     530     4219360 :     for (i = 1; i < l; i++)
     531             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     532     3767927 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     533     3767927 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     534             :       /* p | a ? */
     535     3767927 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     536             :     }
     537             :   }
     538      146988 :   vals = cgetg(l, t_VECSMALL);
     539             :   /* vals[1] not needed */
     540      534212 :   for (j = 2; j < l; j++)
     541             :   {
     542      387224 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     543      387224 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     544             :   }
     545      146988 :   pk = powiu(p, ceildivuu(vmax, e));
     546      146988 :   av = avma; y = cgetg(l,t_COL);
     547             :   /* can compute mod p^ceil((vmax-v)/e) */
     548      207492 :   for (v = 1; v < vmax; v++)
     549             :   { /* we know v_pr(Bj) >= v for all j */
     550       63737 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     551      494480 :     for (j = 2; j < l; j++)
     552             :     {
     553      433976 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     554     4380409 :       for (i = 1; i < l; i++)
     555             :       {
     556     4066828 :         pari_sp av2 = avma;
     557     4066828 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     558     4066828 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     559             :         /* a = (x.t_0)_i; p | a ? */
     560     4066828 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     561     4063595 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     562     4063595 :         gel(y,i) = gerepileuptoint(av2, a);
     563             :       }
     564      313581 :       gel(B,j) = y; y = x;
     565      313581 :       if (gc_needed(av,3))
     566             :       {
     567           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     568           0 :         gerepileall(av,3, &y,&B,&pk);
     569             :       }
     570             :     }
     571             :   }
     572      143755 :   return v;
     573             : }
     574             : /* true nf, x integral ideal */
     575             : static GEN
     576       36792 : idealHNF_factor(GEN nf, GEN x)
     577             : {
     578       36792 :   const long N = lg(x)-1;
     579             :   long i, j, k, l, v;
     580             :   GEN vp, vN, vZ, vP, vE, cx;
     581             : 
     582       36792 :   x = Q_primitive_part(x, &cx);
     583       36792 :   vp = idealHNF_Z_factor(x, &vN,&vZ);
     584       36792 :   l = lg(vp);
     585       36792 :   i = cx? expi(cx)+1: 1;
     586       36792 :   vP = cgetg((l+i-2)*N+1, t_COL);
     587       36792 :   vE = cgetg((l+i-2)*N+1, t_COL);
     588       69333 :   for (i = k = 1; i < l; i++)
     589             :   {
     590       32541 :     GEN L, p = gel(vp,i);
     591       32541 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     592       32541 :     if (vc)
     593             :     {
     594        1757 :       L = idealprimedec(nf,p);
     595        1757 :       if (is_pm1(cx)) cx = NULL;
     596             :     }
     597             :     else
     598       30784 :       L = idealprimedec_limit_f(nf,p,Nval);
     599       49297 :     for (j = 1; j < lg(L); j++)
     600             :     {
     601       49290 :       GEN P = gel(L,j);
     602       49290 :       pari_sp av = avma;
     603       49290 :       v = idealHNF_val(x, P, Nval, Zval);
     604       49290 :       avma = av;
     605       49290 :       Nval -= v*pr_get_f(P);
     606       49290 :       v += vc * pr_get_e(P); if (!v) continue;
     607       36834 :       gel(vP,k) = P;
     608       36834 :       gel(vE,k) = utoipos(v); k++;
     609       36834 :       if (!Nval) break; /* now only the content contributes */
     610             :     }
     611       33256 :     if (vc) for (j++; j<lg(L); j++)
     612             :     {
     613         715 :       GEN P = gel(L,j);
     614         715 :       gel(vP,k) = P;
     615         715 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     616             :     }
     617             :   }
     618       36792 :   if (cx)
     619             :   {
     620        7476 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     621        7476 :     long lc = lg(cP);
     622       15701 :     for (i=1; i<lc; i++)
     623             :     {
     624        8225 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     625        8225 :       long vc = itos(gel(cE,i));
     626       18228 :       for (j=1; j<lg(L); j++)
     627             :       {
     628       10003 :         GEN P = gel(L,j);
     629       10003 :         gel(vP,k) = P;
     630       10003 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     631             :       }
     632             :     }
     633             :   }
     634       36792 :   setlg(vP, k);
     635       36792 :   setlg(vE, k); return mkmat2(vP, vE);
     636             : }
     637             : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     638             : static GEN
     639        3073 : prV_e_muls(GEN L, long c)
     640             : {
     641        3073 :   long j, l = lg(L);
     642        3073 :   GEN z = cgetg(l, t_COL);
     643        3073 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     644        3073 :   return z;
     645             : }
     646             : /* true nf, y in Q */
     647             : static GEN
     648        3101 : Q_nffactor(GEN nf, GEN y)
     649             : {
     650             :   GEN f, P, E;
     651             :   long lfa, i;
     652        3101 :   if (typ(y) == t_INT)
     653             :   {
     654        3087 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     655        3066 :     if (is_pm1(y)) return trivial_fact();
     656             :   }
     657        2317 :   f = factor(Q_abs_shallow(y));
     658        2317 :   P = gel(f,1); lfa = lg(P);
     659        2317 :   E = gel(f,2);
     660        5390 :   for (i = 1; i < lfa; i++)
     661             :   {
     662        3073 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     663        3073 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     664             :   }
     665        2317 :   settyp(P,t_VEC); P = shallowconcat1(P);
     666        2317 :   settyp(E,t_VEC); E = shallowconcat1(E);
     667        2317 :   gel(f,1) = P; settyp(P, t_COL);
     668        2317 :   gel(f,2) = E; return f;
     669             : }
     670             : 
     671             : GEN
     672       39921 : idealfactor(GEN nf, GEN x)
     673             : {
     674       39921 :   pari_sp av = avma;
     675             :   GEN fa, y;
     676       39921 :   long tx = idealtyp(&x,&y);
     677             : 
     678       39921 :   nf = checknf(nf);
     679       39921 :   if (tx == id_PRIME) retmkmat2(mkcolcopy(x), mkcol(gen_1));
     680       39886 :   if (tx == id_PRINCIPAL)
     681             :   {
     682        5026 :     y = nf_to_scalar_or_basis(nf, x);
     683        5026 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y));
     684             :   }
     685       36785 :   y = idealnumden(nf, x);
     686       36785 :   fa = idealHNF_factor(nf, gel(y,1));
     687       36785 :   if (!isint1(gel(y,2)))
     688             :   {
     689           7 :     GEN F = idealHNF_factor(nf, gel(y,2));
     690           7 :     fa = famat_mul_shallow(fa, famat_inv_shallow(F));
     691             :   }
     692       36785 :   fa = gerepilecopy(av, fa);
     693       36785 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     694             : }
     695             : 
     696             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     697             : long
     698      488507 : idealval(GEN nf, GEN A, GEN P)
     699             : {
     700      488507 :   pari_sp av = avma;
     701             :   GEN a, p, cA;
     702      488507 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     703             : 
     704      488507 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     705      484076 :   checkprid(P);
     706      484076 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     707             :   /* id_MAT */
     708      484048 :   nf = checknf(nf);
     709      484048 :   A = Q_primitive_part(A, &cA);
     710      484048 :   p = pr_get_p(P);
     711      484048 :   vcA = cA? Q_pval(cA,p): 0;
     712      484048 :   if (pr_is_inert(P)) { avma = av; return vcA; }
     713      476558 :   Zval = Z_pval(gcoeff(A,1,1), p);
     714      476558 :   if (!Zval) v = 0;
     715             :   else
     716             :   {
     717      169526 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     718      169526 :     v = idealHNF_val(A, P, Nval, Zval);
     719             :   }
     720      476558 :   avma = av; return vcA? v + vcA*pr_get_e(P): v;
     721             : }
     722             : GEN
     723        6573 : gpidealval(GEN nf, GEN ix, GEN P)
     724             : {
     725        6573 :   long v = idealval(nf,ix,P);
     726        6573 :   return v == LONG_MAX? mkoo(): stoi(v);
     727             : }
     728             : 
     729             : /* gcd and generalized Bezout */
     730             : 
     731             : GEN
     732       59416 : idealadd(GEN nf, GEN x, GEN y)
     733             : {
     734       59416 :   pari_sp av = avma;
     735             :   long tx, ty;
     736             :   GEN z, a, dx, dy, dz;
     737             : 
     738       59416 :   tx = idealtyp(&x,&z);
     739       59416 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     740       59416 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     741       59416 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     742       59416 :   if (lg(x) == 1) return gerepilecopy(av,y);
     743       59409 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     744       59129 :   dx = Q_denom(x);
     745       59129 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     746       59129 :   if (is_pm1(dz)) dz = NULL; else {
     747       12453 :     x = Q_muli_to_int(x, dz);
     748       12453 :     y = Q_muli_to_int(y, dz);
     749             :   }
     750       59129 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     751       59129 :   if (is_pm1(a))
     752             :   {
     753       27663 :     long N = lg(x)-1;
     754       27663 :     if (!dz) { avma = av; return matid(N); }
     755        3611 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     756             :   }
     757       31466 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     758       31466 :   if (dz) z = RgM_Rg_div(z,dz);
     759       31466 :   return gerepileupto(av,z);
     760             : }
     761             : 
     762             : static GEN
     763          28 : trivial_merge(GEN x)
     764          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     765             : /* true nf */
     766             : static GEN
     767      120421 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     768             : {
     769             :   GEN a;
     770      120421 :   long tx = idealtyp(&x, &a/*junk*/);
     771      120421 :   long ty = idealtyp(&y, &a/*junk*/);
     772             :   long ea;
     773      120421 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
     774      120421 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
     775      120421 :   if (lg(x) == 1)
     776          14 :     a = trivial_merge(y);
     777      120407 :   else if (lg(y) == 1)
     778          14 :     a = trivial_merge(x);
     779             :   else
     780      120393 :     a = hnfmerge_get_1(x, y);
     781      120421 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
     782      120407 :   if (red && (ea = gexpo(a)) > 10)
     783             :   {
     784        6736 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
     785        6736 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
     786        6736 :     if (gexpo(b) < ea) a = b;
     787             :   }
     788      120407 :   return a;
     789             : }
     790             : /* true nf */
     791             : GEN
     792       12201 : idealaddtoone_i(GEN nf, GEN x, GEN y)
     793       12201 : { return _idealaddtoone(nf, x, y, 1); }
     794             : /* true nf */
     795             : GEN
     796      108220 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
     797      108220 : { return _idealaddtoone(nf, x, y, 0); }
     798             : 
     799             : GEN
     800          98 : idealaddtoone(GEN nf, GEN x, GEN y)
     801             : {
     802          98 :   GEN z = cgetg(3,t_VEC), a;
     803          98 :   pari_sp av = avma;
     804          98 :   nf = checknf(nf);
     805          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
     806          84 :   gel(z,1) = a;
     807          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
     808          84 :   return z;
     809             : }
     810             : 
     811             : /* assume elements of list are integral ideals */
     812             : GEN
     813          35 : idealaddmultoone(GEN nf, GEN list)
     814             : {
     815          35 :   pari_sp av = avma;
     816          35 :   long N, i, l, nz, tx = typ(list);
     817             :   GEN H, U, perm, L;
     818             : 
     819          35 :   nf = checknf(nf); N = nf_get_degree(nf);
     820          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
     821          35 :   l = lg(list);
     822          35 :   L = cgetg(l, t_VEC);
     823          35 :   if (l == 1)
     824           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     825          35 :   nz = 0; /* number of non-zero ideals in L */
     826          98 :   for (i=1; i<l; i++)
     827             :   {
     828          70 :     GEN I = gel(list,i);
     829          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
     830          70 :     if (lg(I) != 1)
     831             :     {
     832          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
     833          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
     834             :     }
     835          63 :     gel(L,i) = I;
     836             :   }
     837          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
     838          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
     839           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     840          49 :   for (i=1; i<=N; i++)
     841          49 :     if (perm[i] == 1) break;
     842          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
     843          21 :   nz = 0;
     844          63 :   for (i=1; i<l; i++)
     845             :   {
     846          42 :     GEN c = gel(L,i);
     847          42 :     if (lg(c) == 1)
     848          14 :       c = gen_0;
     849             :     else {
     850          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
     851          28 :       nz++;
     852             :     }
     853          42 :     gel(L,i) = c;
     854             :   }
     855          21 :   return gerepilecopy(av, L);
     856             : }
     857             : 
     858             : /* multiplication */
     859             : 
     860             : /* x integral ideal (without archimedean component) in HNF form
     861             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
     862             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
     863             : static GEN
     864      630134 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
     865             : {
     866      630134 :   GEN m, a = gel(y,1), alpha = gel(y,2);
     867             :   long i, N;
     868             : 
     869      630134 :   if (typ(alpha) != t_MAT)
     870             :   {
     871      427180 :     alpha = zk_scalar_or_multable(nf, alpha);
     872      427180 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
     873        2912 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
     874             :   }
     875      627222 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
     876      627222 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
     877      627222 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
     878      627222 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
     879             : }
     880             : 
     881             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
     882             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
     883             : GEN
     884      308988 : idealHNF_mul(GEN nf, GEN x, GEN y)
     885             : {
     886             :   GEN z;
     887      308988 :   if (typ(y) == t_VEC)
     888      211084 :     z = idealHNF_mul_two(nf,x,y);
     889             :   else
     890             :   { /* reduce one ideal to two-elt form. The smallest */
     891       97904 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
     892       97904 :     if (cmpii(xZ, yZ) < 0)
     893             :     {
     894       34865 :       if (is_pm1(xZ)) return gcopy(y);
     895       24546 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
     896             :     }
     897             :     else
     898             :     {
     899       63039 :       if (is_pm1(yZ)) return gcopy(x);
     900       39050 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
     901             :     }
     902             :   }
     903      274680 :   return z;
     904             : }
     905             : 
     906             : /* operations on elements in factored form */
     907             : 
     908             : GEN
     909       93859 : famat_mul_shallow(GEN f, GEN g)
     910             : {
     911       93859 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
     912       93859 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
     913       93859 :   if (lg(f) == 1) return g;
     914       74369 :   if (lg(g) == 1) return f;
     915      146010 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
     916      146010 :                 shallowconcat(gel(f,2), gel(g,2)));
     917             : }
     918             : GEN
     919       63700 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
     920             : {
     921       63700 :   if (!signe(e)) return f;
     922       61915 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
     923             : }
     924             : 
     925             : GEN
     926           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
     927             : GEN
     928      804384 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
     929             : 
     930             : /* concat the single elt x; not gconcat since x may be a t_COL */
     931             : static GEN
     932       59742 : append(GEN v, GEN x)
     933             : {
     934       59742 :   long i, l = lg(v);
     935       59742 :   GEN w = cgetg(l+1, typ(v));
     936       59742 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
     937       59742 :   gel(w,i) = gcopy(x); return w;
     938             : }
     939             : /* add x^1 to famat f */
     940             : static GEN
     941       86402 : famat_add(GEN f, GEN x)
     942             : {
     943       86402 :   GEN h = cgetg(3,t_MAT);
     944       86402 :   if (lg(f) == 1)
     945             :   {
     946       26660 :     gel(h,1) = mkcolcopy(x);
     947       26660 :     gel(h,2) = mkcol(gen_1);
     948             :   }
     949             :   else
     950             :   {
     951       59742 :     gel(h,1) = append(gel(f,1), x);
     952       59742 :     gel(h,2) = gconcat(gel(f,2), gen_1);
     953             :   }
     954       86402 :   return h;
     955             : }
     956             : 
     957             : GEN
     958      110525 : famat_mul(GEN f, GEN g)
     959             : {
     960             :   GEN h;
     961      110525 :   if (typ(g) != t_MAT) {
     962       86402 :     if (typ(f) == t_MAT) return famat_add(f, g);
     963           0 :     h = cgetg(3, t_MAT);
     964           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
     965           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
     966             :   }
     967       24123 :   if (typ(f) != t_MAT) return famat_add(g, f);
     968       24123 :   if (lg(f) == 1) return gcopy(g);
     969        4382 :   if (lg(g) == 1) return gcopy(f);
     970        1897 :   h = cgetg(3,t_MAT);
     971        1897 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
     972        1897 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
     973        1897 :   return h;
     974             : }
     975             : 
     976             : GEN
     977       51020 : famat_sqr(GEN f)
     978             : {
     979             :   GEN h;
     980       51020 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     981       25409 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
     982       25409 :   h = cgetg(3,t_MAT);
     983       25409 :   gel(h,1) = gcopy(gel(f,1));
     984       25409 :   gel(h,2) = gmul2n(gel(f,2),1);
     985       25409 :   return h;
     986             : }
     987             : 
     988             : GEN
     989       27020 : famat_inv_shallow(GEN f)
     990             : {
     991       27020 :   if (lg(f) == 1) return f;
     992       27020 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
     993          14 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
     994             : }
     995             : GEN
     996       11070 : famat_inv(GEN f)
     997             : {
     998       11070 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     999        4156 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1000        4156 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1001             : }
    1002             : GEN
    1003        1177 : famat_pow(GEN f, GEN n)
    1004             : {
    1005        1177 :   if (lg(f) == 1) return cgetg(1,t_MAT);
    1006           0 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1007           0 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1008             : }
    1009             : GEN
    1010       61915 : famat_pow_shallow(GEN f, GEN n)
    1011             : {
    1012       61915 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1013       31507 :   if (lg(f) == 1) return f;
    1014       31507 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1015        1274 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1016             : }
    1017             : 
    1018             : GEN
    1019           0 : famat_Z_gcd(GEN M, GEN n)
    1020             : {
    1021           0 :   pari_sp av=avma;
    1022           0 :   long i, j, l=lgcols(M);
    1023           0 :   GEN F=cgetg(3,t_MAT);
    1024           0 :   gel(F,1)=cgetg(l,t_COL);
    1025           0 :   gel(F,2)=cgetg(l,t_COL);
    1026           0 :   for (i=1, j=1; i<l; i++)
    1027             :   {
    1028           0 :     GEN p = gcoeff(M,i,1);
    1029           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1030           0 :     if (signe(e))
    1031             :     {
    1032           0 :       gcoeff(F,j,1)=p;
    1033           0 :       gcoeff(F,j,2)=e;
    1034           0 :       j++;
    1035             :     }
    1036             :   }
    1037           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1038           0 :   return gerepilecopy(av,F);
    1039             : }
    1040             : 
    1041             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1042             :  * the element_* functions. */
    1043             : static GEN
    1044       61590 : ext_sqr(GEN nf, GEN x)
    1045       61590 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1046             : static GEN
    1047      145231 : ext_mul(GEN nf, GEN x, GEN y)
    1048      145231 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1049             : static GEN
    1050       10930 : ext_inv(GEN nf, GEN x)
    1051       10930 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1052             : static GEN
    1053        1177 : ext_pow(GEN nf, GEN x, GEN n)
    1054        1177 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1055             : 
    1056             : GEN
    1057           0 : famat_to_nf(GEN nf, GEN f)
    1058             : {
    1059             :   GEN t, x, e;
    1060             :   long i;
    1061           0 :   if (lg(f) == 1) return gen_1;
    1062             : 
    1063           0 :   x = gel(f,1);
    1064           0 :   e = gel(f,2);
    1065           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1066           0 :   for (i=lg(x)-1; i>1; i--)
    1067           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1068           0 :   return t;
    1069             : }
    1070             : 
    1071             : GEN
    1072       17983 : famat_reduce(GEN fa)
    1073             : {
    1074             :   GEN E, G, L, g, e;
    1075             :   long i, k, l;
    1076             : 
    1077       17983 :   if (lg(fa) == 1) return fa;
    1078       15428 :   g = gel(fa,1); l = lg(g);
    1079       15428 :   e = gel(fa,2);
    1080       15428 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1081       15428 :   G = cgetg(l, t_COL);
    1082       15428 :   E = cgetg(l, t_COL);
    1083             :   /* merge */
    1084       37842 :   for (k=i=1; i<l; i++,k++)
    1085             :   {
    1086       22414 :     gel(G,k) = gel(g,L[i]);
    1087       22414 :     gel(E,k) = gel(e,L[i]);
    1088       22414 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1089             :     {
    1090         749 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1091         749 :       k--;
    1092             :     }
    1093             :   }
    1094             :   /* kill 0 exponents */
    1095       15428 :   l = k;
    1096       37093 :   for (k=i=1; i<l; i++)
    1097       21665 :     if (!gequal0(gel(E,i)))
    1098             :     {
    1099       20965 :       gel(G,k) = gel(G,i);
    1100       20965 :       gel(E,k) = gel(E,i); k++;
    1101             :     }
    1102       15428 :   setlg(G, k);
    1103       15428 :   setlg(E, k); return mkmat2(G,E);
    1104             : }
    1105             : 
    1106             : GEN
    1107       12724 : famatsmall_reduce(GEN fa)
    1108             : {
    1109             :   GEN E, G, L, g, e;
    1110             :   long i, k, l;
    1111       12724 :   if (lg(fa) == 1) return fa;
    1112       12724 :   g = gel(fa,1); l = lg(g);
    1113       12724 :   e = gel(fa,2);
    1114       12724 :   L = vecsmall_indexsort(g);
    1115       12724 :   G = cgetg(l, t_VECSMALL);
    1116       12724 :   E = cgetg(l, t_VECSMALL);
    1117             :   /* merge */
    1118      113886 :   for (k=i=1; i<l; i++,k++)
    1119             :   {
    1120      101162 :     G[k] = g[L[i]];
    1121      101162 :     E[k] = e[L[i]];
    1122      101162 :     if (k > 1 && G[k] == G[k-1])
    1123             :     {
    1124        5919 :       E[k-1] += E[k];
    1125        5919 :       k--;
    1126             :     }
    1127             :   }
    1128             :   /* kill 0 exponents */
    1129       12724 :   l = k;
    1130      107966 :   for (k=i=1; i<l; i++)
    1131       95242 :     if (E[i])
    1132             :     {
    1133       92298 :       G[k] = G[i];
    1134       92298 :       E[k] = E[i]; k++;
    1135             :     }
    1136       12724 :   setlg(G, k);
    1137       12724 :   setlg(E, k); return mkmat2(G,E);
    1138             : }
    1139             : 
    1140             : GEN
    1141       55951 : ZM_famat_limit(GEN fa, GEN limit)
    1142             : {
    1143             :   pari_sp av;
    1144             :   GEN E, G, g, e, r;
    1145             :   long i, k, l, n, lG;
    1146             : 
    1147       55951 :   if (lg(fa) == 1) return fa;
    1148       55951 :   g = gel(fa,1); l = lg(g);
    1149       55951 :   e = gel(fa,2);
    1150      124194 :   for(n=0, i=1; i<l; i++)
    1151       68243 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1152       55951 :   lG = n<l-1 ? n+2 : n+1;
    1153       55951 :   G = cgetg(lG, t_COL);
    1154       55951 :   E = cgetg(lG, t_COL);
    1155       55951 :   av = avma;
    1156      124194 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1157             :   {
    1158       68243 :     if (cmpii(gel(g,i),limit)<=0)
    1159             :     {
    1160       68159 :       gel(G,k) = gel(g,i);
    1161       68159 :       gel(E,k) = gel(e,i);
    1162       68159 :       k++;
    1163          84 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1164             :   }
    1165       55951 :   if (k<i)
    1166             :   {
    1167          84 :     gel(G, k) = gerepileuptoint(av, r);
    1168          84 :     gel(E, k) = gen_1;
    1169             :   }
    1170       55951 :   return mkmat2(G,E);
    1171             : }
    1172             : 
    1173             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1174             : static GEN
    1175        5099 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1176             : {
    1177        5099 :   GEN d, r, p = modpr_get_p(modpr);
    1178        5099 :   x = nf_to_scalar_or_basis(nf,x);
    1179        5099 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1180        4735 :   x = Q_remove_denom(x, &d);
    1181        4735 :   r = zk_to_Fq(x, modpr);
    1182        4735 :   if (d) r = Fp_div(r, d, p);
    1183        4735 :   return r;
    1184             : }
    1185             : 
    1186             : /* pr coprime to all denominators occurring in x */
    1187             : static GEN
    1188         790 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1189             : {
    1190         790 :   GEN p = modpr_get_p(modpr);
    1191         790 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1192         790 :   long i, l = lg(g);
    1193        2425 :   for (i = 1; i < l; i++)
    1194             :   {
    1195        1635 :     GEN n = modii(gel(e,i), q);
    1196        1635 :     if (signe(n))
    1197             :     {
    1198        1635 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1199        1635 :       h = Fp_pow(h, n, p);
    1200        1635 :       t = t? Fp_mul(t, h, p): h;
    1201             :     }
    1202             :   }
    1203         790 :   return t? modii(t, p): gen_1;
    1204             : }
    1205             : 
    1206             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1207             : GEN
    1208        4254 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1209             : {
    1210        8508 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1211        4254 :                       : to_Fp_coprime(nf, x, modpr);
    1212             : }
    1213             : 
    1214             : static long
    1215      135997 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1216      135997 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1217             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1218             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1219             : static GEN
    1220      263600 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1221             : {
    1222             :   long vcx;
    1223             :   GEN dx;
    1224      263600 :   x = nf_to_scalar_or_basis(nf, x);
    1225      263600 :   x = Q_remove_denom(x, &dx);
    1226      263600 :   if (dx)
    1227             :   {
    1228      170940 :     vcx = - Z_pvalrem(dx, p, &dx);
    1229      170940 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1230      170940 :     if (isint1(dx)) dx = NULL;
    1231             :   }
    1232             :   else
    1233             :   {
    1234       92660 :     vcx = zk_pvalrem(x, p, &x);
    1235       92660 :     dx = NULL;
    1236             :   }
    1237      263600 :   *pv = vcx;
    1238      263600 :   *pdx = dx; return x;
    1239             : }
    1240             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1241             :  * if p inert (instead of 1) */
    1242             : static GEN
    1243       62069 : p_makecoprime(GEN pr)
    1244             : {
    1245       62069 :   GEN B = pr_get_tau(pr), b;
    1246             :   long i, e;
    1247             : 
    1248       62069 :   if (typ(B) == t_INT) return NULL;
    1249       61929 :   b = gel(B,1); /* B = multiplication table by b */
    1250       61929 :   e = pr_get_e(pr);
    1251       61929 :   if (e == 1) return b;
    1252             :   /* one could also divide (exactly) by p in each iteration */
    1253       17150 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1254       17150 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1255             : }
    1256             : 
    1257             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1258             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1259             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1260             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1261             :  * Optimizations:
    1262             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1263             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1264             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1265             :  *
    1266             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1267             :  * case the e[i] are large */
    1268             : GEN
    1269      111640 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1270             : {
    1271      111640 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1272      111640 :   long i, l = lg(g);
    1273             : 
    1274      111640 :   G = cgetg(l+1, t_VEC);
    1275      111640 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1276      375240 :   for (i=1; i < l; i++)
    1277             :   {
    1278             :     long vcx;
    1279      263600 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1280      263600 :     if (vcx) /* = v_p(content(g[i])) */
    1281             :     {
    1282      129262 :       GEN a = mulsi(vcx, gel(e,i));
    1283      129262 :       vp = vp? addii(vp, a): a;
    1284             :     }
    1285             :     /* x integral, content coprime to p; dx coprime to p */
    1286      263600 :     if (typ(x) == t_INT)
    1287             :     { /* x coprime to p, hence to pr */
    1288       38602 :       x = modii(x, prkZ);
    1289       38602 :       if (dx) x = Fp_div(x, dx, prkZ);
    1290             :     }
    1291             :     else
    1292             :     {
    1293      224998 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1294      224998 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1295      224998 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1296             :     }
    1297      263600 :     gel(G,i) = x;
    1298      263600 :     gel(E,i) = gel(e,i);
    1299             :   }
    1300             : 
    1301      111640 :   t = vp? p_makecoprime(pr): NULL;
    1302      111640 :   if (!t)
    1303             :   { /* no need for extra generator */
    1304       49711 :     setlg(G,l);
    1305       49711 :     setlg(E,l);
    1306             :   }
    1307             :   else
    1308             :   {
    1309       61929 :     gel(G,i) = FpC_red(t, prkZ);
    1310       61929 :     gel(E,i) = vp;
    1311             :   }
    1312      111640 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1313             : }
    1314             : 
    1315             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 */
    1316             : GEN
    1317       10983 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1318             : {
    1319             :   GEN t, cyc;
    1320       10983 :   if (lg(g) == 1) return gen_1;
    1321       10983 :   cyc = bid_get_cyc(bid);
    1322       10983 :   if (lg(cyc) == 1)
    1323           0 :     t = gen_1;
    1324             :   else
    1325       10983 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid), gel(cyc,1));
    1326       10983 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1327             : }
    1328             : 
    1329             : GEN
    1330      185605 : vecmul(GEN x, GEN y)
    1331             : {
    1332      185605 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1333       16310 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1334             : }
    1335             : 
    1336             : GEN
    1337           0 : vecinv(GEN x)
    1338             : {
    1339           0 :   if (is_scalar_t(typ(x))) return ginv(x);
    1340           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1341             : }
    1342             : 
    1343             : GEN
    1344       15729 : vecpow(GEN x, GEN n)
    1345             : {
    1346       15729 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1347        4270 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1348             : }
    1349             : 
    1350             : GEN
    1351         903 : vecdiv(GEN x, GEN y)
    1352             : {
    1353         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1354         301 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1355             : }
    1356             : 
    1357             : /* A ideal as a square t_MAT */
    1358             : static GEN
    1359      196571 : idealmulelt(GEN nf, GEN x, GEN A)
    1360             : {
    1361             :   long i, lx;
    1362             :   GEN dx, dA, D;
    1363      196571 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1364      196571 :   x = nf_to_scalar_or_basis(nf,x);
    1365      196571 :   if (typ(x) != t_COL)
    1366       67416 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1367      129155 :   x = Q_remove_denom(x, &dx);
    1368      129155 :   A = Q_remove_denom(A, &dA);
    1369      129155 :   x = zk_multable(nf, x);
    1370      129155 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1371      129155 :   x = zkC_multable_mul(A, x);
    1372      129155 :   settyp(x, t_MAT); lx = lg(x);
    1373             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1374      446469 :   for (i=1; i<lx; i++)
    1375      325797 :     if (typ(gel(x,i)) == t_INT)
    1376             :     {
    1377        8483 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1378        8483 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1379        8483 :       break;
    1380             :     }
    1381      129155 :   x = ZM_hnfmodid(x, D);
    1382      129155 :   dx = mul_denom(dx,dA);
    1383      129155 :   return dx? gdiv(x,dx): x;
    1384             : }
    1385             : 
    1386             : /* nf a true nf, tx <= ty */
    1387             : static GEN
    1388      640858 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1389             : {
    1390             :   GEN z, cx, cy;
    1391      640858 :   switch(tx)
    1392             :   {
    1393             :     case id_PRINCIPAL:
    1394      245787 :       switch(ty)
    1395             :       {
    1396             :         case id_PRINCIPAL:
    1397       49090 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1398             :         case id_PRIME:
    1399             :         {
    1400         126 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1401         126 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1402             : 
    1403          42 :           x = nf_to_scalar_or_basis(nf, x);
    1404          42 :           switch(typ(x))
    1405             :           {
    1406             :             case t_INT:
    1407          28 :               if (!signe(x)) return cgetg(1,t_MAT);
    1408          28 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1409             :             case t_FRAC:
    1410           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1411             :           }
    1412             :           /* t_COL */
    1413           7 :           x = Q_primitive_part(x, &cx);
    1414           7 :           x = zk_multable(nf, x);
    1415           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1416           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1417           7 :           return cx? ZM_Q_mul(z, cx): z;
    1418             :         }
    1419             :         default: /* id_MAT */
    1420      196571 :           return idealmulelt(nf, x,y);
    1421             :       }
    1422             :     case id_PRIME:
    1423      321385 :       if (ty==id_PRIME)
    1424      299409 :       { y = pr_hnf(nf,y); cy = NULL; }
    1425             :       else
    1426       21976 :         y = Q_primitive_part(y, &cy);
    1427      321385 :       y = idealHNF_mul_two(nf,y,x);
    1428      321385 :       return cy? RgM_Rg_mul(y,cy): y;
    1429             : 
    1430             :     default: /* id_MAT */
    1431             :     {
    1432       73686 :       long N = nf_get_degree(nf);
    1433       73686 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1434       73672 :       x = Q_primitive_part(x, &cx);
    1435       73672 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1436       73672 :       y = idealHNF_mul(nf,x,y);
    1437       73672 :       return cx? ZM_Q_mul(y,cx): y;
    1438             :     }
    1439             :   }
    1440             : }
    1441             : 
    1442             : /* output the ideal product ix.iy */
    1443             : GEN
    1444      640858 : idealmul(GEN nf, GEN x, GEN y)
    1445             : {
    1446             :   pari_sp av;
    1447             :   GEN res, ax, ay, z;
    1448      640858 :   long tx = idealtyp(&x,&ax);
    1449      640858 :   long ty = idealtyp(&y,&ay), f;
    1450      640858 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1451      640858 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1452      640858 :   av = avma;
    1453      640858 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1454      640844 :   if (!f) return z;
    1455       42696 :   if (ax && ay)
    1456       41021 :     ax = ext_mul(nf, ax, ay);
    1457             :   else
    1458        1675 :     ax = gcopy(ax? ax: ay);
    1459       42696 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1460             : }
    1461             : 
    1462             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1463             :  * nf = true nf */
    1464             : static GEN
    1465       44641 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1466             : {
    1467       44641 :   GEN p = pr_get_p(pr), q, gen;
    1468       44641 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1469             : 
    1470       44641 :   q = (e == 1)? sqri(p): p;
    1471       44641 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1472             :   { /* pr^e = (p) */
    1473        9947 :     *pc = q;
    1474        9947 :     return mkvec2(gen_1,gen_0);
    1475             :   }
    1476       34694 :   gen = nfsqr(nf, pr_get_gen(pr));
    1477       34694 :   gen = FpC_red(gen, q);
    1478       34694 :   *pc = NULL;
    1479       34694 :   return mkvec2(q, gen);
    1480             : }
    1481             : /* cf idealpow_aux */
    1482             : static GEN
    1483       62353 : idealsqr_aux(GEN nf, GEN x, long tx)
    1484             : {
    1485       62353 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1486       62353 :   long N = degpol(T);
    1487       62353 :   switch(tx)
    1488             :   {
    1489             :     case id_PRINCIPAL:
    1490          77 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1491             :     case id_PRIME:
    1492       23474 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1493       23306 :       x = idealsqrprime(nf, x, &cx);
    1494       23306 :       x = idealhnf_two(nf,x);
    1495       23306 :       return cx? ZM_Z_mul(x, cx): x;
    1496             :     default:
    1497       38802 :       x = Q_primitive_part(x, &cx);
    1498       38802 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1499       38802 :       alpha = nfsqr(nf,alpha);
    1500       38802 :       m = zk_scalar_or_multable(nf, alpha);
    1501       38802 :       if (typ(m) == t_INT) {
    1502        1456 :         x = gcdii(sqri(a), m);
    1503        1456 :         if (cx) x = gmul(x, gsqr(cx));
    1504        1456 :         x = scalarmat(x, N);
    1505             :       }
    1506             :       else
    1507             :       {
    1508       37346 :         x = ZM_hnfmodid(m, gcdii(sqri(a), zkmultable_capZ(m)));
    1509       37346 :         if (cx) cx = gsqr(cx);
    1510       37346 :         if (cx) x = RgM_Rg_mul(x, cx);
    1511             :       }
    1512       38802 :       return x;
    1513             :   }
    1514             : }
    1515             : GEN
    1516       62353 : idealsqr(GEN nf, GEN x)
    1517             : {
    1518             :   pari_sp av;
    1519             :   GEN res, ax, z;
    1520       62353 :   long tx = idealtyp(&x,&ax);
    1521       62353 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1522       62353 :   av = avma;
    1523       62353 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1524       62353 :   if (!ax) return z;
    1525       61590 :   gel(res,1) = z;
    1526       61590 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1527             : }
    1528             : 
    1529             : /* norm of an ideal */
    1530             : GEN
    1531        6328 : idealnorm(GEN nf, GEN x)
    1532             : {
    1533             :   pari_sp av;
    1534             :   GEN y, T;
    1535             :   long tx;
    1536             : 
    1537        6328 :   switch(idealtyp(&x,&y))
    1538             :   {
    1539         175 :     case id_PRIME: return pr_norm(x);
    1540        4095 :     case id_MAT: return RgM_det_triangular(x);
    1541             :   }
    1542             :   /* id_PRINCIPAL */
    1543        2058 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1544        2058 :   x = nf_to_scalar_or_alg(nf, x);
    1545        2058 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1546        2058 :   tx = typ(x);
    1547        2058 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1548         532 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1549         532 :   return gerepileupto(av, Q_abs(x));
    1550             : }
    1551             : 
    1552             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    1553             :  *
    1554             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    1555             :  * nf[5][7] = same in 2-elt form.
    1556             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    1557             : GEN
    1558      191564 : idealHNF_inv_Z(GEN nf, GEN I)
    1559             : {
    1560      191564 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1561      191564 :   if (isint1(IZ)) return matid(lg(I)-1);
    1562      180490 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    1563             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    1564             :   * missing content cancels while solving the linear equation */
    1565      180490 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1566      180490 :   return ZM_hnfmodid(dual, IZ);
    1567             : }
    1568             : /* I HNF with rational coefficients (denominator d). */
    1569             : GEN
    1570       57873 : idealHNF_inv(GEN nf, GEN I)
    1571             : {
    1572       57873 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1573       57873 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1574       57873 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1575             : }
    1576             : 
    1577             : /* return p * P^(-1)  [integral] */
    1578             : GEN
    1579       24459 : pr_inv_p(GEN pr)
    1580             : {
    1581       24459 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    1582       23969 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    1583             : }
    1584             : GEN
    1585        3584 : pr_inv(GEN pr)
    1586             : {
    1587        3584 :   GEN p = pr_get_p(pr);
    1588        3584 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    1589        3234 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    1590             : }
    1591             : 
    1592             : GEN
    1593       97611 : idealinv(GEN nf, GEN x)
    1594             : {
    1595             :   GEN res, ax;
    1596             :   pari_sp av;
    1597       97611 :   long tx = idealtyp(&x,&ax), N;
    1598             : 
    1599       97611 :   res = ax? cgetg(3,t_VEC): NULL;
    1600       97611 :   nf = checknf(nf); av = avma;
    1601       97611 :   N = nf_get_degree(nf);
    1602       97611 :   switch (tx)
    1603             :   {
    1604             :     case id_MAT:
    1605       52441 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1606       52441 :       x = idealHNF_inv(nf,x); break;
    1607             :     case id_PRINCIPAL:
    1608       42440 :       x = nf_to_scalar_or_basis(nf, x);
    1609       42440 :       if (typ(x) != t_COL)
    1610       42398 :         x = idealhnf_principal(nf,ginv(x));
    1611             :       else
    1612             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1613             :         GEN c, d;
    1614          42 :         x = Q_remove_denom(x, &c);
    1615          42 :         x = zk_inv(nf, x);
    1616          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1617          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    1618           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    1619             :         else
    1620             :         {
    1621          35 :           c = c? gdiv(c,d): ginv(d);
    1622          35 :           x = zk_multable(nf, x);
    1623          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1624             :         }
    1625             :       }
    1626       42440 :       break;
    1627             :     case id_PRIME:
    1628        2730 :       x = pr_inv(x); break;
    1629             :   }
    1630       97611 :   x = gerepileupto(av,x); if (!ax) return x;
    1631       10930 :   gel(res,1) = x;
    1632       10930 :   gel(res,2) = ext_inv(nf, ax); return res;
    1633             : }
    1634             : 
    1635             : /* write x = A/B, A,B coprime integral ideals */
    1636             : GEN
    1637       37009 : idealnumden(GEN nf, GEN x)
    1638             : {
    1639       37009 :   pari_sp av = avma;
    1640             :   GEN x0, ax, c, d, A, B, J;
    1641       37009 :   long tx = idealtyp(&x,&ax);
    1642       37009 :   nf = checknf(nf);
    1643       37009 :   switch (tx)
    1644             :   {
    1645             :     case id_PRIME:
    1646           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    1647             :     case id_PRINCIPAL:
    1648             :     {
    1649             :       GEN xZ, mx;
    1650        2135 :       x = nf_to_scalar_or_basis(nf, x);
    1651        2135 :       switch(typ(x))
    1652             :       {
    1653          77 :         case t_INT: return gerepilecopy(av, mkvec2(absi(x),gen_1));
    1654          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi(gel(x,1)), gel(x,2)));
    1655             :       }
    1656             :       /* t_COL */
    1657        2044 :       x = Q_remove_denom(x, &d);
    1658        2044 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    1659          21 :       mx = zk_multable(nf, x);
    1660          21 :       xZ = zkmultable_capZ(mx);
    1661          21 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    1662          21 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    1663          21 :       break;
    1664             :     }
    1665             :     default: /* id_MAT */
    1666             :     {
    1667       34867 :       long n = lg(x)-1;
    1668       34867 :       if (n == 0) return mkvec2(gen_0, gen_1);
    1669       34867 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    1670       34867 :       x0 = x = Q_remove_denom(x, &d);
    1671       34867 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    1672          14 :       break;
    1673             :     }
    1674             :   }
    1675          35 :   J = hnfmodid(x, d); /* = d/B */
    1676          35 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    1677          35 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    1678          35 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    1679          35 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    1680          35 :   A = ZM_Z_divexact(A, d); /* = A ! */
    1681          35 :   return gerepilecopy(av, mkvec2(A, B));
    1682             : }
    1683             : 
    1684             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    1685             :  * nf = true nf */
    1686             : static GEN
    1687       88903 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    1688             : {
    1689       88903 :   GEN p = pr_get_p(pr), q, gen;
    1690             : 
    1691       88903 :   *pc = NULL;
    1692       88903 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    1693             :   {
    1694       50724 :     q = p;
    1695       50724 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    1696             :     {
    1697           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    1698           0 :       return mkvec2(gen_1,gen_0);
    1699             :     }
    1700       50724 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    1701             :     else
    1702             :     {
    1703        8127 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    1704        8127 :       *pc = ginv(p);
    1705             :     }
    1706             :   }
    1707       38179 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    1708             :   else
    1709             :   {
    1710       16844 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    1711       16844 :     GEN r, m = truedvmdis(n, e, &r);
    1712       16844 :     if (e * f == nf_get_degree(nf))
    1713             :     { /* pr^e = (p) */
    1714        7770 :       if (signe(m)) *pc = powii(p,m);
    1715        7770 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    1716        3171 :       q = p;
    1717        3171 :       gen = nfpow(nf, pr_get_gen(pr), r);
    1718             :     }
    1719             :     else
    1720             :     {
    1721        9074 :       m = absi(m);
    1722        9074 :       if (signe(r)) m = addiu(m,1);
    1723        9074 :       q = powii(p,m); /* m = ceil(|n|/e) */
    1724        9074 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    1725             :       else
    1726             :       {
    1727        2219 :         gen = pr_get_tau(pr);
    1728        2219 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    1729        2219 :         n = negi(n);
    1730        2219 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    1731        2219 :         *pc = ginv(q);
    1732             :       }
    1733             :     }
    1734       12245 :     gen = FpC_red(gen, q);
    1735             :   }
    1736       62969 :   return mkvec2(q, gen);
    1737             : }
    1738             : 
    1739             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    1740             : GEN
    1741       67620 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1742             : {
    1743             :   GEN c, cx, y;
    1744             :   long N;
    1745             : 
    1746       67620 :   nf = checknf(nf);
    1747       67620 :   N = nf_get_degree(nf);
    1748       67620 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    1749             : 
    1750             :   /* inert, special cased for efficiency */
    1751       67508 :   if (pr_is_inert(pr))
    1752             :   {
    1753        5551 :     GEN q = powii(pr_get_p(pr), n);
    1754        5551 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q): scalarmat_shallow(gmul(x,q), N);
    1755             :   }
    1756             : 
    1757       61957 :   y = idealpowprime(nf, pr, n, &c);
    1758       61957 :   if (typ(x) == t_MAT)
    1759       60634 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    1760             :   else
    1761        1323 :   { cx = x; x = NULL; }
    1762       61957 :   cx = mul_content(c,cx);
    1763       61957 :   if (x)
    1764       34048 :     x = idealHNF_mul_two(nf,x,y);
    1765             :   else
    1766       27909 :     x = idealhnf_two(nf,y);
    1767       61957 :   if (cx) x = RgM_Rg_mul(x,cx);
    1768       61957 :   return x;
    1769             : }
    1770             : GEN
    1771       13482 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1772             : {
    1773       13482 :   return idealmulpowprime(nf,x,pr, negi(n));
    1774             : }
    1775             : 
    1776             : /* nf = true nf */
    1777             : static GEN
    1778      178396 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    1779             : {
    1780      178396 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    1781      178396 :   long N = degpol(T), s = signe(n);
    1782      178396 :   if (!s) return matid(N);
    1783      172837 :   switch(tx)
    1784             :   {
    1785             :     case id_PRINCIPAL:
    1786           0 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    1787             :     case id_PRIME:
    1788       70955 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    1789       26946 :       x = idealpowprime(nf, x, n, &cx);
    1790       26946 :       x = idealhnf_two(nf,x);
    1791       26946 :       return cx? RgM_Rg_mul(x, cx): x;
    1792             :     default:
    1793      101882 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    1794       55708 :       n1 = (s < 0)? negi(n): n;
    1795             : 
    1796       55708 :       x = Q_primitive_part(x, &cx);
    1797       55708 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1798       55708 :       alpha = nfpow(nf,alpha,n1);
    1799       55708 :       m = zk_scalar_or_multable(nf, alpha);
    1800       55708 :       if (typ(m) == t_INT) {
    1801         189 :         x = gcdii(powii(a,n1), m);
    1802         189 :         if (s<0) x = ginv(x);
    1803         189 :         if (cx) x = gmul(x, powgi(cx,n));
    1804         189 :         x = scalarmat(x, N);
    1805             :       }
    1806             :       else
    1807             :       {
    1808       55519 :         x = ZM_hnfmodid(m, gcdii(powii(a,n1), zkmultable_capZ(m)));
    1809       55519 :         if (cx) cx = powgi(cx,n);
    1810       55519 :         if (s<0) {
    1811           7 :           GEN xZ = gcoeff(x,1,1);
    1812           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    1813           7 :           x = idealHNF_inv_Z(nf,x);
    1814             :         }
    1815       55519 :         if (cx) x = RgM_Rg_mul(x, cx);
    1816             :       }
    1817       55708 :       return x;
    1818             :   }
    1819             : }
    1820             : 
    1821             : /* raise the ideal x to the power n (in Z) */
    1822             : GEN
    1823      178396 : idealpow(GEN nf, GEN x, GEN n)
    1824             : {
    1825             :   pari_sp av;
    1826             :   long tx;
    1827             :   GEN res, ax;
    1828             : 
    1829      178396 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    1830      178396 :   tx = idealtyp(&x,&ax);
    1831      178396 :   res = ax? cgetg(3,t_VEC): NULL;
    1832      178396 :   av = avma;
    1833      178396 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    1834      178396 :   if (!ax) return x;
    1835        1177 :   ax = ext_pow(nf, ax, n);
    1836        1177 :   gel(res,1) = x;
    1837        1177 :   gel(res,2) = ax;
    1838        1177 :   return res;
    1839             : }
    1840             : 
    1841             : /* Return ideal^e in number field nf. e is a C integer. */
    1842             : GEN
    1843       21196 : idealpows(GEN nf, GEN ideal, long e)
    1844             : {
    1845       21196 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    1846       21196 :   affsi(e,court); return idealpow(nf,ideal,court);
    1847             : }
    1848             : 
    1849             : static GEN
    1850       42745 : _idealmulred(GEN nf, GEN x, GEN y)
    1851       42745 : { return idealred(nf,idealmul(nf,x,y)); }
    1852             : static GEN
    1853       61646 : _idealsqrred(GEN nf, GEN x)
    1854       61646 : { return idealred(nf,idealsqr(nf,x)); }
    1855             : static GEN
    1856       26305 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    1857             : static GEN
    1858       61646 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    1859             : 
    1860             : /* compute x^n (x ideal, n integer), reducing along the way */
    1861             : GEN
    1862       59092 : idealpowred(GEN nf, GEN x, GEN n)
    1863             : {
    1864       59092 :   pari_sp av = avma;
    1865             :   long s;
    1866             :   GEN y;
    1867             : 
    1868       59092 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    1869       59092 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    1870       57915 :   y = gen_pow(x, n, (void*)nf, &_sqr, &_mul);
    1871             : 
    1872       57915 :   if (s < 0) y = idealinv(nf,y);
    1873       57915 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    1874       57915 :   return gerepileupto(av,y);
    1875             : }
    1876             : 
    1877             : GEN
    1878       16440 : idealmulred(GEN nf, GEN x, GEN y)
    1879             : {
    1880       16440 :   pari_sp av = avma;
    1881       16440 :   return gerepileupto(av, _idealmulred(nf,x,y));
    1882             : }
    1883             : 
    1884             : long
    1885          91 : isideal(GEN nf,GEN x)
    1886             : {
    1887          91 :   long N, i, j, lx, tx = typ(x);
    1888             :   pari_sp av;
    1889             :   GEN T, xZ;
    1890             : 
    1891          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    1892          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    1893          91 :   switch(tx)
    1894             :   {
    1895          14 :     case t_INT: case t_FRAC: return 1;
    1896           7 :     case t_POL: return varn(x) == varn(T);
    1897           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    1898          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    1899          42 :     case t_MAT: break;
    1900           7 :     default: return 0;
    1901             :   }
    1902          42 :   N = degpol(T);
    1903          42 :   if (lx-1 != N) return (lx == 1);
    1904          28 :   if (nbrows(x) != N) return 0;
    1905             : 
    1906          28 :   av = avma; x = Q_primpart(x);
    1907          28 :   if (!ZM_ishnf(x)) return 0;
    1908          14 :   xZ = gcoeff(x,1,1);
    1909          21 :   for (j=2; j<=N; j++)
    1910          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) { avma = av; return 0; }
    1911          14 :   for (i=2; i<=N; i++)
    1912          14 :     for (j=2; j<=N; j++)
    1913           7 :       if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) { avma = av; return 0; }
    1914           7 :   avma=av; return 1;
    1915             : }
    1916             : 
    1917             : GEN
    1918       20804 : idealdiv(GEN nf, GEN x, GEN y)
    1919             : {
    1920       20804 :   pari_sp av = avma, tetpil;
    1921       20804 :   GEN z = idealinv(nf,y);
    1922       20804 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    1923             : }
    1924             : 
    1925             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    1926             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    1927             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    1928             :  *
    1929             :  *   x + (Nx/Nz)    x
    1930             :  *   ----------- = ---
    1931             :  *   y + (Ny/Nz)    y
    1932             :  *
    1933             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    1934             :  *
    1935             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    1936             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    1937             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    1938             :  * assumed integral and its norm N(x/y) is coprime to p.
    1939             :  *
    1940             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    1941             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    1942             :  *
    1943             :  *                Peter Montgomery.  July, 1994. */
    1944             : static void
    1945           7 : err_divexact(GEN x, GEN y)
    1946           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    1947           0 :                   gen_1,mkvec2(x,y)); }
    1948             : GEN
    1949         777 : idealdivexact(GEN nf, GEN x0, GEN y0)
    1950             : {
    1951         777 :   pari_sp av = avma;
    1952             :   GEN x, y, yZ, Nx, Ny, Nz, cy, q, r;
    1953             : 
    1954         777 :   nf = checknf(nf);
    1955         777 :   x = idealhnf_shallow(nf, x0);
    1956         777 :   y = idealhnf_shallow(nf, y0);
    1957         777 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    1958         770 :   if (lg(x) == 1) { avma = av; return cgetg(1, t_MAT); } /* numerator is zero */
    1959         770 :   y = Q_primitive_part(y, &cy);
    1960         770 :   if (cy) x = RgM_Rg_div(x,cy);
    1961         770 :   Nx = idealnorm(nf,x);
    1962         770 :   Ny = idealnorm(nf,y);
    1963         770 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    1964         763 :   q = dvmdii(Nx,Ny, &r);
    1965         763 :   if (signe(r)) err_divexact(x,y);
    1966         763 :   if (is_pm1(q)) { avma = av; return matid(nf_get_degree(nf)); }
    1967             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    1968         539 :   for (Nz = Ny;;) /* q = Nx/Nz */
    1969             :   {
    1970         693 :     GEN p1 = gcdii(Nz, q);
    1971         693 :     if (is_pm1(p1)) break;
    1972         154 :     Nz = diviiexact(Nz,p1);
    1973         154 :     q = mulii(q,p1);
    1974         154 :   }
    1975             :   /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    1976         539 :   x = ZM_hnfmodid(x, q);
    1977             :   /* y reduced to unit ideal ? */
    1978         539 :   if (Nz == Ny) return gerepileupto(av, x);
    1979             : 
    1980         154 :   y = ZM_hnfmodid(y, diviiexact(Ny,Nz));
    1981         154 :   yZ = gcoeff(y,1,1);
    1982         154 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    1983         154 :   return gerepileupto(av, RgM_Rg_div(y, yZ));
    1984             : }
    1985             : 
    1986             : GEN
    1987          21 : idealintersect(GEN nf, GEN x, GEN y)
    1988             : {
    1989          21 :   pari_sp av = avma;
    1990             :   long lz, lx, i;
    1991             :   GEN z, dx, dy, xZ, yZ;;
    1992             : 
    1993          21 :   nf = checknf(nf);
    1994          21 :   x = idealhnf_shallow(nf,x);
    1995          21 :   y = idealhnf_shallow(nf,y);
    1996          21 :   if (lg(x) == 1 || lg(y) == 1) { avma = av; return cgetg(1,t_MAT); }
    1997          14 :   x = Q_remove_denom(x, &dx);
    1998          14 :   y = Q_remove_denom(y, &dy);
    1999          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2000          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2001          14 :   xZ = gcoeff(x,1,1);
    2002          14 :   yZ = gcoeff(y,1,1);
    2003          14 :   dx = mul_denom(dx,dy);
    2004          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2005          14 :   lx = lg(x);
    2006          14 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2007          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2008          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2009          14 :   return gerepileupto(av,z);
    2010             : }
    2011             : 
    2012             : /*******************************************************************/
    2013             : /*                                                                 */
    2014             : /*                      T2-IDEAL REDUCTION                         */
    2015             : /*                                                                 */
    2016             : /*******************************************************************/
    2017             : 
    2018             : static GEN
    2019          21 : chk_vdir(GEN nf, GEN vdir)
    2020             : {
    2021          21 :   long i, l = lg(vdir);
    2022             :   GEN v;
    2023          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2024          14 :   switch(typ(vdir))
    2025             :   {
    2026           0 :     case t_VECSMALL: return vdir;
    2027          14 :     case t_VEC: break;
    2028           0 :     default: pari_err_TYPE("idealred",vdir);
    2029             :   }
    2030          14 :   v = cgetg(l, t_VECSMALL);
    2031          14 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2032          14 :   return v;
    2033             : }
    2034             : 
    2035             : static void
    2036       26913 : twistG(GEN G, long r1, long i, long v)
    2037             : {
    2038       26913 :   long j, lG = lg(G);
    2039       26913 :   if (i <= r1) {
    2040       23560 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2041             :   } else {
    2042        3353 :     long k = (i<<1) - r1;
    2043       17871 :     for (j=1; j<lG; j++)
    2044             :     {
    2045       14518 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2046       14518 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2047             :     }
    2048             :   }
    2049       26913 : }
    2050             : 
    2051             : GEN
    2052      164744 : nf_get_Gtwist(GEN nf, GEN vdir)
    2053             : {
    2054             :   long i, l, v, r1;
    2055             :   GEN G;
    2056             : 
    2057      164744 :   if (!vdir) return nf_get_roundG(nf);
    2058        2820 :   if (typ(vdir) == t_MAT)
    2059             :   {
    2060        2799 :     long N = nf_get_degree(nf);
    2061        2799 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2062        2799 :     return vdir;
    2063             :   }
    2064          21 :   vdir = chk_vdir(nf, vdir);
    2065          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2066          14 :   r1 = nf_get_r1(nf);
    2067          14 :   l = lg(vdir);
    2068          56 :   for (i=1; i<l; i++)
    2069             :   {
    2070          42 :     v = vdir[i]; if (!v) continue;
    2071          42 :     twistG(G, r1, i, v);
    2072             :   }
    2073          14 :   return RM_round_maxrank(G);
    2074             : }
    2075             : GEN
    2076       26871 : nf_get_Gtwist1(GEN nf, long i)
    2077             : {
    2078       26871 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2079       26871 :   long r1 = nf_get_r1(nf);
    2080       26871 :   twistG(G, r1, i, 10);
    2081       26871 :   return RM_round_maxrank(G);
    2082             : }
    2083             : 
    2084             : GEN
    2085       39527 : RM_round_maxrank(GEN G0)
    2086             : {
    2087       39527 :   long e, r = lg(G0)-1;
    2088       39527 :   pari_sp av = avma;
    2089       39527 :   GEN G = G0;
    2090       39527 :   for (e = 4; ; e <<= 1)
    2091             :   {
    2092       39527 :     GEN H = ground(G);
    2093       79054 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2094           0 :     avma = av;
    2095           0 :     G = gmul2n(G0, e);
    2096           0 :   }
    2097             : }
    2098             : 
    2099             : GEN
    2100      164737 : idealred0(GEN nf, GEN I, GEN vdir)
    2101             : {
    2102      164737 :   pari_sp av = avma;
    2103      164737 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2104             :   long N;
    2105             : 
    2106      164737 :   nf = checknf(nf);
    2107      164737 :   N = nf_get_degree(nf);
    2108             :   /* put first for sanity checks, unused when I obviously principal */
    2109      164737 :   G = nf_get_Gtwist(nf, vdir);
    2110      164730 :   switch (idealtyp(&I,&aI))
    2111             :   {
    2112             :     case id_PRIME:
    2113       23227 :       if (pr_is_inert(I)) {
    2114         581 :         if (!aI) { avma = av; return matid(N); }
    2115         581 :         c1 = gel(I,1); I = matid(N);
    2116         581 :         goto END;
    2117             :       }
    2118       22646 :       IZ = pr_get_p(I);
    2119       22646 :       J = pr_inv_p(I);
    2120       22646 :       I = idealhnf_two(nf,I);
    2121       22646 :       break;
    2122             :     case id_MAT:
    2123      141489 :       I = Q_primitive_part(I, &c1);
    2124      141489 :       IZ = gcoeff(I,1,1);
    2125      141489 :       if (is_pm1(IZ))
    2126             :       {
    2127        7994 :         if (!aI) { avma = av; return matid(N); }
    2128        7938 :         goto END;
    2129             :       }
    2130      133495 :       J = idealHNF_inv_Z(nf, I);
    2131      133495 :       break;
    2132             :     default: /* id_PRINCIPAL, silly case */
    2133          14 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2134          14 :       if (!aI) return I;
    2135           7 :       goto END;
    2136             :   }
    2137             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2138      156141 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2139      156141 :   if (ZV_isscalar(y))
    2140             :   { /* already reduced */
    2141       65851 :     if (!aI) return gerepilecopy(av, I);
    2142       65452 :     goto END;
    2143             :   }
    2144             : 
    2145       90290 :   my = zk_multable(nf, y);
    2146       90290 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2147       90290 :   c1 = mul_content(c1, IZ);
    2148       90290 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2149       90290 :   yZ = Q_denom(my); /* (y) \cap Z */
    2150       90290 :   I = hnfmodid(I, yZ);
    2151       90290 :   if (!aI) return gerepileupto(av, I);
    2152       90024 :   c1 = RgC_Rg_mul(my, c1);
    2153             : END:
    2154      164002 :   if (c1) aI = ext_mul(nf, aI,c1);
    2155      164002 :   return gerepilecopy(av, mkvec2(I, aI));
    2156             : }
    2157             : 
    2158             : GEN
    2159           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2160             : {
    2161           7 :   pari_sp av = avma;
    2162             :   GEN y, dx;
    2163           7 :   nf = checknf(nf);
    2164           7 :   switch( idealtyp(&x,&y) )
    2165             :   {
    2166           0 :     case id_PRINCIPAL: return gcopy(x);
    2167           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2168           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2169             :   }
    2170           7 :   x = Q_remove_denom(x, &dx);
    2171           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2172           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2173           7 :   return gerepileupto(av, y);
    2174             : }
    2175             : 
    2176             : /*******************************************************************/
    2177             : /*                                                                 */
    2178             : /*                   APPROXIMATION THEOREM                         */
    2179             : /*                                                                 */
    2180             : /*******************************************************************/
    2181             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2182             :  * and ppo(a,b) = Z_ppo(a,b) */
    2183             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2184             : GEN
    2185      452893 : Z_ppio(GEN a, GEN b)
    2186             : {
    2187      452893 :   GEN x, y, d = gcdii(a,b);
    2188      452893 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2189      344596 :   x = d; y = diviiexact(a,d);
    2190             :   for(;;)
    2191             :   {
    2192      407316 :     GEN g = gcdii(x,y);
    2193      407316 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2194       62720 :     x = mulii(x,g); y = diviiexact(y,g);
    2195       62720 :   }
    2196             : }
    2197             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2198             :  * and pple all others */
    2199             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2200             : GEN
    2201           0 : Z_ppgle(GEN a, GEN b)
    2202             : {
    2203           0 :   GEN x, y, g, d = gcdii(a,b);
    2204           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2205           0 :   x = diviiexact(a,d); y = d;
    2206             :   for(;;)
    2207             :   {
    2208           0 :     g = gcdii(x,y);
    2209           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2210           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2211           0 :   }
    2212             : }
    2213             : static void
    2214           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2215             : {
    2216             :   GEN x, r, v, g, h, c, c0;
    2217             :   long n;
    2218           0 :   if (is_pm1(b)) {
    2219           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2220           0 :     return;
    2221             :   }
    2222           0 :   v = Z_ppio(a,b);
    2223           0 :   a = gel(v,2);
    2224           0 :   r = gel(v,3);
    2225           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2226           0 :   v = Z_ppgle(a,b);
    2227           0 :   g = gel(v,1);
    2228           0 :   h = gel(v,2);
    2229           0 :   x = c0 = gel(v,3);
    2230           0 :   for (n = 1; !is_pm1(h); n++)
    2231             :   {
    2232             :     GEN d, y;
    2233             :     long i;
    2234           0 :     v = Z_ppgle(h,sqri(g));
    2235           0 :     g = gel(v,1);
    2236           0 :     h = gel(v,2);
    2237           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2238           0 :     d = gcdii(c,b);
    2239           0 :     x = mulii(x,d);
    2240           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2241           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2242             :   }
    2243           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2244             : }
    2245             : static GEN
    2246     3069115 : Z_cba_rec(GEN L, GEN a, GEN b)
    2247             : {
    2248             :   GEN g;
    2249     3069115 :   if (lg(L) > 10)
    2250             :   { /* a few naive steps before switching to dcba */
    2251           0 :     Z_dcba_rec(L, a, b);
    2252           0 :     return gel(L, lg(L)-1);
    2253             :   }
    2254     3069115 :   if (is_pm1(a)) return b;
    2255     1823640 :   g = gcdii(a,b);
    2256     1823640 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2257     1362235 :   a = diviiexact(a,g);
    2258     1362235 :   b = diviiexact(b,g);
    2259     1362235 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2260             : }
    2261             : GEN
    2262      344645 : Z_cba(GEN a, GEN b)
    2263             : {
    2264      344645 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2265      344645 :   GEN t = Z_cba_rec(L, a, b);
    2266      344645 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2267      344645 :   return L;
    2268             : }
    2269             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2270             : GEN
    2271           0 : ZV_cba_extend(GEN P, GEN b)
    2272             : {
    2273           0 :   long i, l = lg(P);
    2274           0 :   GEN w = cgetg(l+1, t_VEC);
    2275           0 :   for (i = 1; i < l; i++)
    2276             :   {
    2277           0 :     GEN v = Z_cba(gel(P,i), b);
    2278           0 :     long nv = lg(v)-1;
    2279           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2280           0 :     b = gel(v,nv);
    2281             :   }
    2282           0 :   gel(w,l) = b; return shallowconcat1(w);
    2283             : }
    2284             : GEN
    2285           0 : ZV_cba(GEN v)
    2286             : {
    2287           0 :   long i, l = lg(v);
    2288             :   GEN P;
    2289           0 :   if (l <= 2) return v;
    2290           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2291           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2292           0 :   return P;
    2293             : }
    2294             : 
    2295             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2296             : GEN
    2297     1112781 : Z_ppo(GEN x, GEN f)
    2298             : {
    2299             :   for (;;)
    2300             :   {
    2301     1112781 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2302      756524 :     x = diviiexact(x, f);
    2303      756524 :   }
    2304      356257 :   return x;
    2305             : }
    2306             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2307             : ulong
    2308    37990225 : u_ppo(ulong x, ulong f)
    2309             : {
    2310             :   for (;;)
    2311             :   {
    2312    37990225 :     f = ugcd(x, f); if (f == 1) break;
    2313     7310016 :     x /= f;
    2314     7310016 :   }
    2315    30680209 :   return x;
    2316             : }
    2317             : 
    2318             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2319             : static GEN
    2320         140 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2321             : {
    2322         140 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2323             :   GEN x1, x2, ex;
    2324             : 
    2325             : #if 0 /*1) via many gcds. Expensive ! */
    2326             :   GEN f = idealprodprime(nf, listpr);
    2327             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2328             :   x = scalarmat(x, N);
    2329             :   for (;;)
    2330             :   {
    2331             :     if (gequal1(gcoeff(f,1,1))) break;
    2332             :     x = idealdivexact(nf, x, f);
    2333             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2334             :   }
    2335             :   x2 = x;
    2336             : #else /*2) from prime decomposition */
    2337         140 :   x1 = NULL;
    2338         399 :   for (j=1; j<lp; j++)
    2339             :   {
    2340         259 :     GEN pr = gel(listpr,j);
    2341         259 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2342             : 
    2343         140 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2344         140 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2345         140 :            : idealpow(nf, pr, ex);
    2346             :   }
    2347         140 :   x = scalarmat(x, N);
    2348         140 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2349             : #endif
    2350         140 :   return x2;
    2351             : }
    2352             : 
    2353             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2354             : GEN
    2355        5621 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2356             : {
    2357             :   GEN fZ, t, L, D2, d1, d2, d;
    2358             : 
    2359        5621 :   L = Q_remove_denom(L0, &d);
    2360        5621 :   if (!d) return L0;
    2361             : 
    2362             :   /* L0 = L / d, L integral */
    2363        2170 :   fZ = gcoeff(f,1,1);
    2364        2170 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2365             :   /* Kill denom part coprime to fZ */
    2366        1939 :   d2 = Z_ppo(d, fZ);
    2367        1939 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2368        1939 :   if (equalii(d, d2)) return L;
    2369             : 
    2370         140 :   d1 = diviiexact(d, d2);
    2371             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2372             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2373         140 :   D2 = nf_coprime_part(nf, d1, listpr);
    2374         140 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2375         140 :   L = nfmuli(nf,t,L);
    2376             : 
    2377             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2378         140 :   return Q_div_to_int(L, d1); /* exact division */
    2379             : }
    2380             : 
    2381             : /* assume L is a list of prime ideals. Return the product */
    2382             : GEN
    2383         126 : idealprodprime(GEN nf, GEN L)
    2384             : {
    2385         126 :   long l = lg(L), i;
    2386             :   GEN z;
    2387         126 :   if (l == 1) return matid(nf_get_degree(nf));
    2388         126 :   z = pr_hnf(nf, gel(L,1));
    2389         126 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2390         126 :   return z;
    2391             : }
    2392             : 
    2393             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2394             : GEN
    2395        1470 : idealprod(GEN nf, GEN I)
    2396             : {
    2397        1470 :   long i, l = lg(I);
    2398             :   GEN z;
    2399        2541 :   for (i = 1; i < l; i++)
    2400        2520 :     if (!equali1(gel(I,i))) break;
    2401        1470 :   if (i == l) return gen_1;
    2402        1449 :   z = gel(I,i);
    2403        1449 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2404        1449 :   return z;
    2405             : }
    2406             : 
    2407             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2408             : GEN
    2409        7203 : factorbackprime(GEN nf, GEN L, GEN e)
    2410             : {
    2411        7203 :   long l = lg(L), i;
    2412             :   GEN z;
    2413             : 
    2414        7203 :   if (l == 1) return matid(nf_get_degree(nf));
    2415        7189 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2416       11032 :   for (i=2; i<l; i++)
    2417        3843 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2418        7189 :   return z;
    2419             : }
    2420             : 
    2421             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2422             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2423             : GEN
    2424       17501 : pr_uniformizer(GEN pr, GEN F)
    2425             : {
    2426       17501 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2427       17501 :   if (!equalii(F, p))
    2428             :   {
    2429        7410 :     long e = pr_get_e(pr);
    2430        7410 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2431        7410 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2432        7410 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2433        7410 :     if (pr_is_inert(pr))
    2434           0 :       t = addii(mulii(p, v), u);
    2435             :     else
    2436             :     {
    2437        7410 :       t = ZC_Z_mul(t, v);
    2438        7410 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2439             :     }
    2440             :   }
    2441       17501 :   return t;
    2442             : }
    2443             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2444             : GEN
    2445       34811 : prV_lcm_capZ(GEN L)
    2446             : {
    2447       34811 :   long i, r = lg(L);
    2448             :   GEN F;
    2449       34811 :   if (r == 1) return gen_1;
    2450       29414 :   F = pr_get_p(gel(L,1));
    2451       43835 :   for (i = 2; i < r; i++)
    2452             :   {
    2453       14421 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2454       14421 :     if (!dvdii(F, p)) F = mulii(F,p);
    2455             :   }
    2456       29414 :   return F;
    2457             : }
    2458             : 
    2459             : /* Given a prime ideal factorization with possibly zero or negative
    2460             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2461             :  * and v_pr(b) >= 0 for all other pr.
    2462             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2463             :  * but no support for this yet.
    2464             :  *
    2465             :  * If nored, do not reduce result.
    2466             :  * No garbage collecting */
    2467             : static GEN
    2468       20412 : idealapprfact_i(GEN nf, GEN x, int nored)
    2469             : {
    2470             :   GEN z, d, L, e, e2, F;
    2471             :   long i, r;
    2472             :   int flagden;
    2473             : 
    2474       20412 :   nf = checknf(nf);
    2475       20412 :   L = gel(x,1);
    2476       20412 :   e = gel(x,2);
    2477       20412 :   F = prV_lcm_capZ(L);
    2478       20412 :   flagden = 0;
    2479       20412 :   z = NULL; r = lg(e);
    2480       43226 :   for (i = 1; i < r; i++)
    2481             :   {
    2482       22814 :     long s = signe(gel(e,i));
    2483             :     GEN pi, q;
    2484       22814 :     if (!s) continue;
    2485       15338 :     if (s < 0) flagden = 1;
    2486       15338 :     pi = pr_uniformizer(gel(L,i), F);
    2487       15338 :     q = nfpow(nf, pi, gel(e,i));
    2488       15338 :     z = z? nfmul(nf, z, q): q;
    2489             :   }
    2490       20412 :   if (!z) return gen_1;
    2491       10570 :   if (nored || typ(z) != t_COL) return z;
    2492        2716 :   e2 = cgetg(r, t_VEC);
    2493        2716 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2494        2716 :   x = factorbackprime(nf, L,e2);
    2495        2716 :   if (flagden) /* denominator */
    2496             :   {
    2497        2702 :     z = Q_remove_denom(z, &d);
    2498        2702 :     d = diviiexact(d, Z_ppo(d, F));
    2499        2702 :     x = RgM_Rg_mul(x, d);
    2500             :   }
    2501             :   else
    2502          14 :     d = NULL;
    2503        2716 :   z = ZC_reducemodlll(z, x);
    2504        2716 :   return d? RgC_Rg_div(z,d): z;
    2505             : }
    2506             : 
    2507             : GEN
    2508           0 : idealapprfact(GEN nf, GEN x) {
    2509           0 :   pari_sp av = avma;
    2510           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2511             : }
    2512             : GEN
    2513          14 : idealappr(GEN nf, GEN x) {
    2514          14 :   pari_sp av = avma;
    2515          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2516          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2517             : }
    2518             : 
    2519             : /* OBSOLETE */
    2520             : GEN
    2521          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2522             : 
    2523             : static GEN
    2524          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2525             : {
    2526          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    2527          21 :   long i, r = lg(E);
    2528          21 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    2529          21 :   return idealapprfact_i(nf,F,1);
    2530             : }
    2531             : 
    2532             : static void
    2533          14 : not_in_ideal(GEN a) {
    2534          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2535           0 : }
    2536             : /* x integral in HNF, a an 'nf' */
    2537             : static int
    2538          28 : in_ideal(GEN x, GEN a)
    2539             : {
    2540          28 :   switch(typ(a))
    2541             :   {
    2542          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    2543           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    2544           7 :     default: return 0;
    2545             :   }
    2546             : }
    2547             : 
    2548             : /* Given an integral ideal x and a in x, gives a b such that
    2549             :  * x = aZ_K + bZ_K using the approximation theorem */
    2550             : GEN
    2551          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2552             : {
    2553          42 :   pari_sp av = avma;
    2554             :   GEN cx, b;
    2555             : 
    2556          42 :   nf = checknf(nf);
    2557          42 :   a = nf_to_scalar_or_basis(nf, a);
    2558          42 :   x = idealhnf_shallow(nf,x);
    2559          42 :   if (lg(x) == 1)
    2560             :   {
    2561          14 :     if (!isintzero(a)) not_in_ideal(a);
    2562           7 :     avma = av; return gen_0;
    2563             :   }
    2564          28 :   x = Q_primitive_part(x, &cx);
    2565          28 :   if (cx) a = gdiv(a, cx);
    2566          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    2567          21 :   b = mat_ideal_two_elt2(nf, x, a);
    2568          21 :   if (typ(b) == t_COL)
    2569             :   {
    2570          14 :     GEN mod = idealhnf_principal(nf,a);
    2571          14 :     b = ZC_hnfrem(b,mod);
    2572          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    2573             :   }
    2574             :   else
    2575             :   {
    2576           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    2577           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    2578             :   }
    2579          21 :   b = cx? gmul(b,cx): gcopy(b);
    2580          21 :   return gerepileupto(av, b);
    2581             : }
    2582             : 
    2583             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2584             :  * beta * x is an integral ideal coprime to y */
    2585             : GEN
    2586       12537 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2587             : {
    2588       12537 :   GEN L = gel(fy,1), e;
    2589       12537 :   long i, r = lg(L);
    2590             : 
    2591       12537 :   e = cgetg(r, t_COL);
    2592       12537 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2593       12537 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2594             : }
    2595             : GEN
    2596          70 : idealcoprime(GEN nf, GEN x, GEN y)
    2597             : {
    2598          70 :   pari_sp av = avma;
    2599          70 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2600             : }
    2601             : 
    2602             : GEN
    2603           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2604             : {
    2605           7 :   pari_sp av = avma;
    2606           7 :   GEN z, p, pr = modpr, T;
    2607             : 
    2608           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2609           0 :   x = nf_to_Fq(nf,x,modpr);
    2610           0 :   y = nf_to_Fq(nf,y,modpr);
    2611           0 :   z = Fq_mul(x,y,T,p);
    2612           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2613             : }
    2614             : 
    2615             : GEN
    2616           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2617             : {
    2618           0 :   pari_sp av = avma;
    2619           0 :   nf = checknf(nf);
    2620           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2621             : }
    2622             : 
    2623             : GEN
    2624           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2625             : {
    2626           0 :   pari_sp av=avma;
    2627           0 :   GEN z, T, p, pr = modpr;
    2628             : 
    2629           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2630           0 :   z = nf_to_Fq(nf,x,modpr);
    2631           0 :   z = Fq_pow(z,k,T,p);
    2632           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2633             : }
    2634             : 
    2635             : GEN
    2636           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    2637             : {
    2638           0 :   pari_sp av = avma;
    2639           0 :   GEN T, p, pr = modpr;
    2640             : 
    2641           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2642           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    2643           0 :   x = nfM_to_FqM(x, nf, modpr);
    2644           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    2645             : }
    2646             : 
    2647             : GEN
    2648           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    2649             : {
    2650           0 :   const char *f = "nfsolvemodpr";
    2651           0 :   pari_sp av = avma;
    2652             :   GEN T, p, modpr;
    2653             : 
    2654           0 :   nf = checknf(nf);
    2655           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2656           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    2657           0 :   a = nfM_to_FqM(a, nf, modpr);
    2658           0 :   switch(typ(b))
    2659             :   {
    2660             :     case t_MAT:
    2661           0 :       b = nfM_to_FqM(b, nf, modpr);
    2662           0 :       b = FqM_gauss(a,b,T,p);
    2663           0 :       if (!b) pari_err_INV(f,a);
    2664           0 :       a = FqM_to_nfM(b, modpr);
    2665           0 :       break;
    2666             :     case t_COL:
    2667           0 :       b = nfV_to_FqV(b, nf, modpr);
    2668           0 :       b = FqM_FqC_gauss(a,b,T,p);
    2669           0 :       if (!b) pari_err_INV(f,a);
    2670           0 :       a = FqV_to_nfV(b, modpr);
    2671           0 :       break;
    2672           0 :     default: pari_err_TYPE(f,b);
    2673             :   }
    2674           0 :   return gerepilecopy(av, a);
    2675             : }

Generated by: LCOV version 1.11