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

Generated by: LCOV version 1.11