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 - char.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21353-12523aa) Lines: 776 798 97.2 %
Date: 2017-11-24 06:20:58 Functions: 63 63 100.0 %
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             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : 
      16             : /*********************************************************************/
      17             : /**                                                                 **/
      18             : /**                  GENERIC ABELIAN CHARACTERS                     **/
      19             : /**                                                                 **/
      20             : /*********************************************************************/
      21             : /* check whether G is a znstar */
      22             : int
      23      972384 : checkznstar_i(GEN G)
      24             : {
      25     2916935 :   return (typ(G) == t_VEC && lg(G) == 6
      26      972167 :       && typ(znstar_get_faN(G)) == t_VEC
      27     1944551 :       && typ(gel(G,1)) == t_VEC && lg(gel(G,1)) == 3);
      28             : }
      29             : 
      30             : int
      31       40229 : char_check(GEN cyc, GEN chi)
      32       40229 : { return typ(chi) == t_VEC && lg(chi) == lg(cyc) && RgV_is_ZV(chi); }
      33             : 
      34             : /* Shallow; return [ d[1],  d[1]/d[2],...,d[1]/d[n] ] */
      35             : GEN
      36      152761 : cyc_normalize(GEN d)
      37             : {
      38      152761 :   long i, l = lg(d);
      39             :   GEN C, D;
      40      152761 :   if (l == 1) return mkvec(gen_1);
      41      152747 :   D = cgetg(l, t_VEC); gel(D,1) = C = gel(d,1);
      42      152747 :   for (i = 2; i < l; i++) gel(D,i) = diviiexact(C, gel(d,i));
      43      152747 :   return D;
      44             : }
      45             : 
      46             : /* chi character [D,C] given by chi(g_i) = \zeta_D^C[i] for all i, return
      47             :  * [d,c] such that chi(g_i) = \zeta_d^c[i] for all i and d minimal */
      48             : GEN
      49      154959 : char_simplify(GEN D, GEN C)
      50             : {
      51      154959 :   GEN d = D;
      52      154959 :   if (lg(C) == 1) d = gen_1;
      53             :   else
      54             :   {
      55      154581 :     GEN t = gcdii(d, ZV_content(C));
      56      154581 :     if (!equali1(t))
      57             :     {
      58      111881 :       C = ZC_Z_divexact(C, t);
      59      111881 :       d = diviiexact(d, t);
      60             :     }
      61             :   }
      62      154959 :   return mkvec2(d,C);
      63             : }
      64             : 
      65             : /* Shallow; ncyc from cyc_normalize(): ncyc[1] = cyc[1],
      66             :  * ncyc[i] = cyc[i]/cyc[1] for i > 1; chi character on G ~ cyc.
      67             :  * Return [d,c] such that: chi( g_i ) = e(chi[i] / cyc[i]) = e(c[i]/ d) */
      68             : GEN
      69      154238 : char_normalize(GEN chi, GEN ncyc)
      70             : {
      71      154238 :   long i, l = lg(chi);
      72      154238 :   GEN c = cgetg(l, t_VEC);
      73      154238 :   if (l > 1) {
      74      154224 :     gel(c,1) = gel(chi,1);
      75      154224 :     for (i = 2; i < l; i++) gel(c,i) = mulii(gel(chi,i), gel(ncyc,i));
      76             :   }
      77      154238 :   return char_simplify(gel(ncyc,1), c);
      78             : }
      79             : 
      80             : /* Called by function 's'. x is a group object affording ".cyc" method, and
      81             :  * chi an abelian character. Return NULL if the group is (Z/nZ)^* [special
      82             :  * case more character types allowed] and x.cyc otherwise */
      83             : static GEN
      84         483 : get_cyc(GEN x, GEN chi, const char *s)
      85             : {
      86         483 :   if (nftyp(x) == typ_BIDZ)
      87             :   {
      88         448 :     if (!zncharcheck(x, chi)) pari_err_TYPE(s, chi);
      89         448 :     return NULL;
      90             :   }
      91             :   else
      92             :   {
      93          35 :     if (typ(x) != t_VEC || !RgV_is_ZV(x)) x = member_cyc(x);
      94          35 :     if (!char_check(x, chi)) pari_err_TYPE(s, chi);
      95          35 :     return x;
      96             :   }
      97             : }
      98             : 
      99             : /* conjugate character [ZV/ZC] */
     100             : GEN
     101        2212 : charconj(GEN cyc, GEN chi)
     102             : {
     103             :   long i, l;
     104        2212 :   GEN z = cgetg_copy(chi, &l);
     105        5152 :   for (i = 1; i < l; i++)
     106             :   {
     107        2940 :     GEN c = gel(chi,i);
     108        2940 :     gel(z,i) = signe(c)? subii(gel(cyc,i), c): gen_0;
     109             :   }
     110        2212 :   return z;
     111             : }
     112             : GEN
     113          28 : charconj0(GEN x, GEN chi)
     114             : {
     115          28 :   GEN cyc = get_cyc(x, chi, "charconj");
     116          28 :   return cyc? charconj(cyc, chi): zncharconj(x, chi);
     117             : }
     118             : 
     119             : GEN
     120      371546 : charorder(GEN cyc, GEN x)
     121             : {
     122      371546 :   pari_sp av = avma;
     123      371546 :   long i, l = lg(cyc);
     124      371546 :   GEN f = gen_1;
     125     1595923 :   for (i = 1; i < l; i++)
     126     1224377 :     if (signe(gel(x,i)))
     127             :     {
     128      789432 :       GEN c, o = gel(cyc,i);
     129      789432 :       c = gcdii(o, gel(x,i));
     130      789432 :       if (!is_pm1(c)) o = diviiexact(o,c);
     131      789432 :       f = lcmii(f, o);
     132             :     }
     133      371546 :   return gerepileuptoint(av, f);
     134             : }
     135             : GEN
     136         168 : charorder0(GEN x, GEN chi)
     137             : {
     138         168 :   GEN cyc = get_cyc(x, chi, "charorder");
     139         168 :   return cyc? charorder(cyc, chi): zncharorder(x, chi);
     140             : }
     141             : 
     142             : /* chi character of abelian G: chi[i] = chi(z_i), where G = \oplus Z/cyc[i] z_i.
     143             :  * Return Ker chi */
     144             : GEN
     145       39886 : charker(GEN cyc, GEN chi)
     146             : {
     147       39886 :   long i, l = lg(cyc);
     148             :   GEN nchi, ncyc, m, U;
     149             : 
     150       39886 :   if (l == 1) return cgetg(1,t_MAT); /* trivial subgroup */
     151       39879 :   ncyc = cyc_normalize(cyc);
     152       39879 :   nchi = char_normalize(chi, ncyc);
     153       39879 :   m = shallowconcat(gel(nchi,2), gel(nchi,1));
     154       39879 :   U = gel(ZV_extgcd(m), 2); setlg(U,l);
     155       39879 :   for (i = 1; i < l; i++) setlg(U[i], l);
     156       39879 :   return hnfmodid(U, gel(ncyc,1));
     157             : }
     158             : GEN
     159          28 : charker0(GEN x, GEN chi)
     160             : {
     161          28 :   GEN cyc = get_cyc(x, chi, "charker");
     162          28 :   return cyc? charker(cyc, chi): zncharker(x, chi);
     163             : }
     164             : 
     165             : GEN
     166         105 : charpow(GEN cyc, GEN a, GEN N)
     167             : {
     168             :   long i, l;
     169         105 :   GEN v = cgetg_copy(a, &l);
     170         105 :   for (i = 1; i < l; i++) gel(v,i) = Fp_mul(gel(a,i), N, gel(cyc,i));
     171         105 :   return v;
     172             : }
     173             : GEN
     174      301133 : charmul(GEN cyc, GEN a, GEN b)
     175             : {
     176             :   long i, l;
     177      301133 :   GEN v = cgetg_copy(a, &l);
     178      301133 :   for (i = 1; i < l; i++) gel(v,i) = Fp_add(gel(a,i), gel(b,i), gel(cyc,i));
     179      301133 :   return v;
     180             : }
     181             : GEN
     182        6510 : chardiv(GEN cyc, GEN a, GEN b)
     183             : {
     184             :   long i, l;
     185        6510 :   GEN v = cgetg_copy(a, &l);
     186        6510 :   for (i = 1; i < l; i++) gel(v,i) = Fp_sub(gel(a,i), gel(b,i), gel(cyc,i));
     187        6510 :   return v;
     188             : }
     189             : GEN
     190          63 : charpow0(GEN x, GEN a, GEN N)
     191             : {
     192          63 :   GEN cyc = get_cyc(x, a, "charpow");
     193          63 :   return cyc? charpow(cyc, a, N): zncharpow(x, a, N);
     194             : }
     195             : GEN
     196         154 : charmul0(GEN x, GEN a, GEN b)
     197             : {
     198         154 :   const char *s = "charmul";
     199         154 :   GEN cyc = get_cyc(x, a, s);
     200         154 :   if (!cyc)
     201             :   {
     202         154 :     if (!zncharcheck(x, b)) pari_err_TYPE(s, b);
     203         154 :     return zncharmul(x, a, b);
     204             :   }
     205             :   else
     206             :   {
     207           0 :     if (!char_check(cyc, b)) pari_err_TYPE(s, b);
     208           0 :     return charmul(cyc, a, b);
     209             :   }
     210             : }
     211             : GEN
     212          42 : chardiv0(GEN x, GEN a, GEN b)
     213             : {
     214          42 :   const char *s = "chardiv";
     215          42 :   GEN cyc = get_cyc(x, a, s);
     216          42 :   if (!cyc)
     217             :   {
     218          42 :     if (!zncharcheck(x, b)) pari_err_TYPE(s, b);
     219          42 :     return znchardiv(x, a, b);
     220             :   }
     221             :   else
     222             :   {
     223           0 :     if (!char_check(cyc, b)) pari_err_TYPE(s, b);
     224           0 :     return chardiv(cyc, a, b);
     225             :   }
     226             : }
     227             : 
     228             : static GEN
     229      237657 : chareval_i(GEN nchi, GEN dlog, GEN z)
     230             : {
     231      237657 :   GEN o, q, r, b = gel(nchi,1);
     232      237657 :   GEN a = FpV_dotproduct(gel(nchi,2), dlog, b);
     233             :   /* image is a/b in Q/Z */
     234      237657 :   if (!z) return gdiv(a,b);
     235      237279 :   if (typ(z) == t_INT)
     236             :   {
     237      235907 :     q = dvmdii(z, b, &r);
     238      235907 :     if (signe(r)) pari_err_TYPE("chareval", z);
     239      235907 :     return mulii(a, q);
     240             :   }
     241             :   /* return z^(a*o/b), assuming z^o = 1 and b | o */
     242        1372 :   if (typ(z) != t_VEC || lg(z) != 3) pari_err_TYPE("chareval", z);
     243        1372 :   o = gel(z,2); if (typ(o) != t_INT) pari_err_TYPE("chareval", z);
     244        1372 :   q = dvmdii(o, b, &r); if (signe(r)) pari_err_TYPE("chareval", z);
     245        1372 :   q = mulii(a, q); /* in [0, o[ since a is reduced mod b */
     246        1372 :   z = gel(z,1);
     247        1372 :   if (typ(z) == t_VEC)
     248             :   {
     249          35 :     if (itos_or_0(o) != lg(z)-1) pari_err_TYPE("chareval", z);
     250          35 :     return gcopy(gel(z, itos(q)+1));
     251             :   }
     252             :   else
     253        1337 :     return gpow(z, q, DEFAULTPREC);
     254             : }
     255             : 
     256             : static GEN
     257        1855 : not_coprime(GEN z)
     258        1855 : { return (!z || typ(z) == t_INT)? gen_m1: gen_0; }
     259             : 
     260             : static GEN
     261          35 : get_chi(GEN cyc, GEN chi)
     262             : {
     263          35 :   if (!char_check(cyc,chi)) pari_err_TYPE("chareval", chi);
     264          35 :   return char_normalize(chi, cyc_normalize(cyc));
     265             : }
     266             : /* G a bnr.  FIXME: horribly inefficient to check that (x,N)=1, what to do ? */
     267             : static int
     268          42 : bnr_coprime(GEN G, GEN x)
     269             : {
     270          42 :   GEN t, N = gel(bnr_get_mod(G), 1);
     271          42 :   if (typ(x) == t_INT) /* shortcut */
     272             :   {
     273          14 :     t = gcdii(gcoeff(N,1,1), x);
     274          14 :     if (equali1(t)) return 1;
     275           0 :     t = idealadd(G, N, x);
     276           0 :     return equali1(gcoeff(t,1,1));
     277             :   }
     278          28 :   x = idealnumden(G, x);
     279          28 :   t = idealadd(G, N, gel(x,1));
     280          28 :   if (!equali1(gcoeff(t,1,1))) return 0;
     281          21 :   t = idealadd(G, N, gel(x,2));
     282          21 :   return equali1(gcoeff(t,1,1));
     283             : }
     284             : GEN
     285        3598 : chareval(GEN G, GEN chi, GEN x, GEN z)
     286             : {
     287        3598 :   pari_sp av = avma;
     288             :   GEN nchi, L;
     289             : 
     290        3598 :   switch(nftyp(G))
     291             :   {
     292             :     case typ_BNR:
     293          42 :       if (!bnr_coprime(G, x)) return not_coprime(z);
     294          28 :       L = isprincipalray(G, x);
     295          28 :       nchi = get_chi(bnr_get_cyc(G), chi);
     296          28 :       break;
     297             :     case typ_BNF:
     298           7 :       L = isprincipal(G, x);
     299           7 :       nchi = get_chi(bnf_get_cyc(G), chi);
     300           7 :       break;
     301             :     case typ_BIDZ:
     302        3542 :       if (checkznstar_i(G)) return gerepileupto(av, znchareval(G, chi, x, z));
     303             :       /* don't implement chars on general bid: need an nf... */
     304             :     default:
     305           7 :       pari_err_TYPE("chareval", G);
     306             :       return NULL;/* LCOV_EXCL_LINE */
     307             :   }
     308          35 :   return gerepileupto(av, chareval_i(nchi, L, z));
     309             : }
     310             : 
     311             : /* nchi = [ord,D] a quasi-normalized character (ord may be a multiple of
     312             :  * the character order); return v such that v[n] = -1 if (n,N) > 1 else
     313             :  * chi(n) = e(v[n]/ord), 1 <= n <= N */
     314             : GEN
     315       28574 : ncharvecexpo(GEN G, GEN nchi)
     316             : {
     317       28574 :   long N = itou(znstar_get_N(G)), ord = itou(gel(nchi,1)), i, j, l;
     318             :   GEN cyc, gen, d, t, t1, t2, t3, e, u, u1, u2, u3;
     319       28574 :   GEN D = gel(nchi,2), v = const_vecsmall(N,-1);
     320       28574 :   pari_sp av = avma;
     321       28574 :   if (typ(D) == t_COL) {
     322       28574 :     cyc = znstar_get_conreycyc(G);
     323       28574 :     gen = znstar_get_conreygen(G);
     324             :   } else {
     325           0 :     cyc = znstar_get_cyc(G);
     326           0 :     gen = znstar_get_gen(G);
     327             :   }
     328       28574 :   l = lg(cyc);
     329       28574 :   e = u = cgetg(N+1,t_VECSMALL);
     330       28574 :   d = t = cgetg(N+1,t_VECSMALL);
     331       28574 :   *++d = 1;
     332       28574 :   *++e = 0; v[*d] = *e;
     333       51282 :   for (i = 1; i < l; i++)
     334             :   {
     335       22708 :     ulong g = itou(gel(gen,i)), c = itou(gel(cyc,i)), x = itou(gel(D,i));
     336      239806 :     for (t1=t,u1=u,j=c-1; j; j--,t1=t2,u1=u2)
     337      666085 :       for (t2=d,u2=e, t3=t1,u3=u1; t3<t2; )
     338             :       {
     339      231889 :         *++d = Fl_mul(*++t3, g, N);
     340      231889 :         *++e = Fl_add(*++u3, x, ord); v[*d] = *e;
     341             :       }
     342             :   }
     343       28574 :   avma = av; return v;
     344             : }
     345             : 
     346             : /*****************************************************************************/
     347             : 
     348             : static ulong
     349      189742 : lcmuu(ulong a, ulong b) { return (a/ugcd(a,b)) * b; }
     350             : static ulong
     351       77665 : zv_charorder(GEN cyc, GEN x)
     352             : {
     353       77665 :   long i, l = lg(cyc);
     354       77665 :   ulong f = 1;
     355      243026 :   for (i = 1; i < l; i++)
     356      165361 :     if (x[i])
     357             :     {
     358      112077 :       ulong o = cyc[i];
     359      112077 :       f = lcmuu(f, o / ugcd(o, x[i]));
     360             :     }
     361       77665 :   return f;
     362             : }
     363             : 
     364             : /* N > 0 */
     365             : GEN
     366       58191 : coprimes_zv(ulong N)
     367             : {
     368       58191 :   GEN v = cgetg(N+1, t_VECSMALL);
     369             :   ulong i;
     370       58191 :   v[1] = 1; for (i = 2; i <= N; i++) v[i] = (ugcd(N,i)==1);
     371       58191 :   return v;
     372             : }
     373             : /* cf zv_cyc_minimal: return k such that g*k is minimal (wrt lex) */
     374             : long
     375       27965 : zv_cyc_minimize(GEN cyc, GEN g, GEN coprime)
     376             : {
     377       27965 :   pari_sp av = avma;
     378       27965 :   long d, k, e, i, k0, bestk, l = lg(g), o = lg(coprime)-1;
     379             :   GEN best, gk, gd;
     380             :   ulong t;
     381       27965 :   if (o == 1) return 1;
     382       32620 :   for (i = 1; i < l; i++)
     383       32620 :     if (g[i]) break;
     384       27965 :   if (g[i] == 1) return 1;
     385       22155 :   k0 = Fl_invgen(g[i], cyc[i], &t);
     386       22155 :   d = cyc[i] / (long)t;
     387       22155 :   if (k0 > 1) g = vecmoduu(Flv_Fl_mul(g, k0, cyc[i]), cyc);
     388       29176 :   for (i++; i < l; i++)
     389       25193 :     if (g[i]) break;
     390       22155 :   if (i == l || d >= cyc[i]) return k0;
     391        2604 :   cyc = vecslice(cyc,i,l-1);
     392        2604 :   g   = vecslice(g,  i,l-1);
     393        2604 :   e = cyc[1];
     394        2604 :   gd = Flv_Fl_mul(g, d, e);
     395        2604 :   bestk = 1; best = g;
     396        6657 :   for (gk = g, k = d+1; k < e; k += d)
     397             :   {
     398        4053 :     long ko = k % o;
     399        4053 :     gk = Flv_add(gk, gd, e); if (!ko || !coprime[ko]) continue;
     400        3122 :     gk = vecmoduu(gk, cyc);
     401        3122 :     if (vecsmall_lexcmp(gk, best) < 0) { best = gk; bestk = k; }
     402             :   }
     403        2604 :   avma = av; return bestk == 1? k0: Fl_mul(k0, bestk, o);
     404             : }
     405             : /* g of order o in abelian group G attached to cyc. Is g a minimal generator
     406             :  * [wrt lex order] of the cyclic subgroup it generates;
     407             :  * coprime = coprimes_zv(o) */
     408             : long
     409       77539 : zv_cyc_minimal(GEN cyc, GEN g, GEN coprime)
     410             : {
     411       77539 :   pari_sp av = avma;
     412       77539 :   long i, d, k, e, l = lg(g), o = lg(coprime)-1; /* elt order */
     413             :   GEN gd, gk;
     414       77539 :   if (o == 1) return 1;
     415       82530 :   for (k = 1; k < l; k++)
     416       82530 :     if (g[k]) break;
     417       77539 :   if (g[k] == 1) return 1;
     418       59962 :   if (cyc[k] % g[k]) return 0;
     419       59962 :   d = cyc[k] / g[k]; /* > 1 */
     420       75306 :   for (k++; k < l; k++) /* skip following 0s */
     421       75306 :     if (g[k]) break;
     422       59962 :   if (k == l) return 1;
     423       59962 :   cyc = vecslice(cyc,k,l-1);
     424       59962 :   g   = vecslice(g,  k,l-1);
     425       59962 :   e = cyc[1];
     426             :   /* find k in (Z/e)^* such that g*k mod cyc is lexicographically minimal,
     427             :    * k = 1 mod d to fix the first non-zero entry */
     428       59962 :   gd = Flv_Fl_mul(g, d, e);
     429      169981 :   for (gk = g, k = d+1, i = 1; i < e; i++, k += d)
     430             :   {
     431      114268 :     long ko = k % o;
     432      114268 :     gk = Flv_add(gk, gd, e); if (!coprime[ko]) continue;
     433       88578 :     gk = vecmoduu(gk, cyc);
     434       88578 :     if (vecsmall_lexcmp(gk, g) < 0) { avma = av; return 0; }
     435             :   }
     436       55713 :   avma = av; return 1;
     437             : }
     438             : 
     439             : static GEN
     440        4872 : coprime_tables(long N)
     441             : {
     442        4872 :   GEN D = divisorsu(N), v = const_vec(N, NULL);
     443        4872 :   long i, l = lg(D);
     444        4872 :   for (i = 1; i < l; i++) gel(v, D[i]) = coprimes_zv(D[i]);
     445        4872 :   return v;
     446             : }
     447             : /* enumerate all group elements, modulo (Z/cyc[1])^* */
     448             : static GEN
     449        4872 : cyc2elts_normal(GEN cyc, long maxord, GEN ORD)
     450             : {
     451        4872 :   long i, n, o, N, j = 1;
     452             :   GEN z, vcoprime;
     453             : 
     454        4872 :   if (typ(cyc) != t_VECSMALL) cyc = gtovecsmall(cyc);
     455        4872 :   n = lg(cyc)-1;
     456        4872 :   if (n == 0) return cgetg(1, t_VEC);
     457        4872 :   N = zv_prod(cyc);
     458        4872 :   z = cgetg(N+1, t_VEC);
     459        4872 :   if (1 <= maxord && (!ORD|| zv_search(ORD,1)))
     460        4501 :     gel(z,j++) = zero_zv(n);
     461        4872 :   vcoprime = coprime_tables(cyc[1]);
     462       16548 :   for (i = n; i > 0; i--)
     463             :   {
     464       11676 :     GEN cyc0 = vecslice(cyc,i+1,n), pre = zero_zv(i);
     465       11676 :     GEN D = divisorsu(cyc[i]), C = cyc2elts(cyc0);
     466       11676 :     long s, t, lD = lg(D), nC = lg(C)-1; /* remove last element */
     467       43715 :     for (s = 1; s < lD-1; s++)
     468             :     {
     469       32039 :       long o0 = D[lD-s]; /* cyc[i] / D[s] */
     470       32039 :       if (o0 > maxord) continue;
     471       30583 :       pre[i] = D[s];
     472       30583 :       if (!ORD || zv_search(ORD,o0))
     473             :       {
     474       30380 :         GEN c = vecsmall_concat(pre, zero_zv(n-i));
     475       30380 :         gel(z,j++) = c;
     476             :       }
     477      108248 :       for (t = 1; t < nC; t++)
     478             :       {
     479       77665 :         GEN chi0 = gel(C,t);
     480       77665 :         o = lcmuu(o0, zv_charorder(cyc0,chi0));
     481       77665 :         if (o <= maxord && (!ORD || zv_search(ORD,o)))
     482             :         {
     483       77539 :           GEN c = vecsmall_concat(pre, chi0);
     484       77539 :           if (zv_cyc_minimal(cyc, c, gel(vcoprime,o))) gel(z,j++) = c;
     485             :         }
     486             :       }
     487             :     }
     488             :   }
     489        4872 :   setlg(z,j); return z;
     490             : }
     491             : 
     492             : GEN
     493        4886 : chargalois(GEN G, GEN ORD)
     494             : {
     495        4886 :   pari_sp av = avma;
     496             :   long maxord, i, l;
     497        4886 :   GEN v, cyc = (typ(G) == t_VEC && RgV_is_ZVpos(G))? G: member_cyc(G);
     498        4886 :   if (lg(cyc) == 1) retmkvec(cgetg(1,t_VEC));
     499        4872 :   maxord = itou(gel(cyc,1));
     500        4872 :   if (ORD && gequal0(ORD)) ORD = NULL;
     501        4872 :   if (ORD)
     502         399 :     switch(typ(ORD))
     503             :     {
     504             :       long l;
     505             :       case t_VEC:
     506          14 :         ORD = ZV_to_zv(ORD);
     507             :       case t_VECSMALL:
     508         378 :         ORD = leafcopy(ORD);
     509         378 :         vecsmall_sort(ORD);
     510         378 :         l = lg(ORD);
     511         378 :         if (l == 1) return cgetg(1, t_VECSMALL);
     512         378 :         maxord = minss(maxord, ORD[l-1]);
     513         378 :         break;
     514             :       case t_INT:
     515          21 :         maxord = minss(maxord, itos(ORD));
     516          21 :         ORD = NULL;
     517          21 :         break;
     518           0 :       default: pari_err_TYPE("chargalois", ORD);
     519             :     }
     520        4872 :   v = cyc2elts_normal(cyc, maxord, ORD); l = lg(v);
     521        4872 :   for(i = 1; i < l; i++) gel(v,i) = zv_to_ZV(gel(v,i));
     522        4872 :   return gerepileupto(av, v);
     523             : }
     524             : 
     525             : /*********************************************************************/
     526             : /**                                                                 **/
     527             : /**                  (Z/NZ)^* AND DIRICHLET CHARACTERS              **/
     528             : /**                                                                 **/
     529             : /*********************************************************************/
     530             : 
     531             : GEN
     532       49672 : znstar0(GEN N, long flag)
     533             : {
     534       49672 :   GEN F = NULL, P, E, cyc, gen, mod, G;
     535             :   long i, i0, l, nbprimes;
     536       49672 :   pari_sp av = avma;
     537             : 
     538       49672 :   if (flag && flag != 1) pari_err_FLAG("znstar");
     539       49672 :   if ((F = check_arith_all(N,"znstar")))
     540             :   {
     541        7609 :     F = clean_Z_factor(F);
     542        7609 :     N = typ(N) == t_VEC? gel(N,1): factorback(F);
     543             :   }
     544       49672 :   if (!signe(N))
     545             :   {
     546          21 :     if (flag) pari_err_IMPL("znstar(0,1)");
     547          14 :     avma = av;
     548          14 :     retmkvec3(gen_2, mkvec(gen_2), mkvec(gen_m1));
     549             :   }
     550       49651 :   if (signe(N) < 0) N = absi(N);
     551       49651 :   if (abscmpiu(N,2) <= 0)
     552             :   {
     553        5152 :     G = mkvec3(gen_1, cgetg(1,t_VEC), cgetg(1,t_VEC));
     554        5152 :     if (flag)
     555             :     {
     556        5124 :       GEN v = const_vec(6,cgetg(1,t_VEC));
     557        5124 :       gel(v,3) = cgetg(1,t_MAT);
     558       15316 :       F = equali1(N)? mkvec2(cgetg(1,t_COL),cgetg(1,t_VECSMALL))
     559       10192 :                     : mkvec2(mkcol(gen_2), mkvecsmall(1));
     560        5124 :       G = mkvec5(mkvec2(N,mkvec(gen_0)), G, F, v, cgetg(1,t_MAT));
     561             :     }
     562        5152 :     return gerepilecopy(av,G);
     563             :   }
     564       44499 :   if (!F) F = Z_factor(N);
     565       44499 :   P = gel(F,1); nbprimes = lg(P)-1;
     566       44499 :   E = ZV_to_nv( gel(F,2) );
     567       44499 :   switch(mod8(N))
     568             :   {
     569             :     case 0:
     570       11942 :       P = shallowconcat(gen_2,P);
     571       11942 :       E = vecsmall_prepend(E, E[1]); /* add a copy of p=2 row */
     572       11942 :       i = 2; /* 2 generators at 2 */
     573       11942 :       break;
     574             :     case 4:
     575        9114 :       i = 1; /* 1 generator at 2 */
     576        9114 :       break;
     577             :     case 2: case 6:
     578        4361 :       P = vecsplice(P,1);
     579        4361 :       E = vecsplice(E,1); /* remove 2 */
     580        4361 :       i = 0; /* no generator at 2 */
     581        4361 :       break;
     582             :     default:
     583       19082 :       i = 0; /* no generator at 2 */
     584       19082 :       break;
     585             :   }
     586       44499 :   l = lg(P);
     587       44499 :   cyc = cgetg(l,t_VEC);
     588       44499 :   gen = cgetg(l,t_VEC);
     589       44499 :   mod = cgetg(l,t_VEC);
     590             :   /* treat p=2 first */
     591       44499 :   if (i == 2)
     592             :   {
     593       11942 :     long v2 = E[1];
     594       11942 :     GEN q = int2n(v2);
     595       11942 :     gel(cyc,1) = gen_2;
     596       11942 :     gel(gen,1) = subiu(q,1); /* -1 */
     597       11942 :     gel(mod,1) = q;
     598       11942 :     gel(cyc,2) = int2n(v2-2);
     599       11942 :     gel(gen,2) = utoipos(5); /* Conrey normalization */
     600       11942 :     gel(mod,2) = q;
     601       11942 :     i0 = 3;
     602             :   }
     603       32557 :   else if (i == 1)
     604             :   {
     605        9114 :     gel(cyc,1) = gen_2;
     606        9114 :     gel(gen,1) = utoipos(3);
     607        9114 :     gel(mod,1) = utoipos(4);
     608        9114 :     i0 = 2;
     609             :   }
     610             :   else
     611       23443 :     i0 = 1;
     612             :   /* odd primes, fill remaining entries */
     613      109319 :   for (i = i0; i < l; i++)
     614             :   {
     615       64820 :     long e = E[i];
     616       64820 :     GEN p = gel(P,i), q = powiu(p, e-1), Q = mulii(p, q);
     617       64820 :     gel(cyc,i) = subii(Q, q); /* phi(p^e) */
     618       64820 :     gel(gen,i) = pgener_Zp(p);/* Conrey normalization, for e = 1 also */
     619       64820 :     gel(mod,i) = Q;
     620             :   }
     621             :   /* gen[i] has order cyc[i] and generates (Z/mod[i]Z)^* */
     622       44499 :   if (nbprimes > 1) /* lift generators to (Z/NZ)^*, = 1 mod N/mod[i] */
     623      113099 :     for (i=1; i<l; i++)
     624             :     {
     625       82222 :       GEN Q = gel(mod,i), g = gel(gen,i), qinv = Fp_inv(Q, diviiexact(N,Q));
     626       82222 :       g = addii(g, mulii(mulii(subsi(1,g),qinv),Q));
     627       82222 :       gel(gen,i) = modii(g, N);
     628             :     }
     629             : 
     630             :   /* cyc[i] > 1 and remain so in the loop, gen[i] = 1 mod (N/mod[i]) */
     631       44499 :   if (!flag)
     632             :   { /* update generators in place; about twice faster */
     633        4396 :     G = gen;
     634        4662 :     for (i=l-1; i>=2; i--)
     635             :     {
     636         266 :       GEN ci = gel(cyc,i), gi = gel(G,i);
     637             :       long j;
     638         602 :       for (j=i-1; j>=1; j--) /* we want cyc[i] | cyc[j] */
     639             :       {
     640         336 :         GEN cj = gel(cyc,j), gj, qj, v, d;
     641             : 
     642         336 :         d = bezout(ci,cj,NULL,&v); /* > 1 */
     643         651 :         if (absequalii(ci, d)) continue; /* ci | cj */
     644         147 :         if (absequalii(cj, d)) { /* cj | ci */
     645         126 :           swap(gel(G,j),gel(G,i));
     646         126 :           gi = gel(G,i);
     647         126 :           swap(gel(cyc,j),gel(cyc,i));
     648         126 :           ci = gel(cyc,i); continue;
     649             :         }
     650             : 
     651          21 :         qj = diviiexact(cj,d);
     652          21 :         gel(cyc,j) = mulii(ci,qj);
     653          21 :         gel(cyc,i) = d;
     654             : 
     655             :         /* [1,v*cj/d; 0,1]*[1,0;-1,1]*diag(cj,ci)*[ci/d,-v; cj/d,u]
     656             :          * = diag(lcm,gcd), with u ci + v cj = d */
     657          21 :         gj = gel(G,j);
     658             :         /* (gj, gi) *= [1,0; -1,1]^-1 */
     659          21 :         gj = Fp_mul(gj, gi, N); /* order ci*qj = lcm(ci,cj) */
     660             :         /* (gj,gi) *= [1,v*qj; 0,1]^-1 */
     661          21 :         togglesign_safe(&v);
     662          21 :         if (signe(v) < 0) v = modii(v,ci); /* >= 0 to avoid inversions */
     663          21 :         gel(G,i) = gi = Fp_mul(gi, Fp_pow(gj, mulii(qj, v), N), N);
     664          21 :         gel(G,j) = gj;
     665          21 :         ci = d; if (absequaliu(ci, 2)) break;
     666             :       }
     667             :     }
     668        4396 :     G = mkvec3(ZV_prod(cyc), cyc, FpV_to_mod(G,N));
     669             :   }
     670             :   else
     671             :   { /* keep matrices between generators, return an 'init' structure */
     672       40103 :     GEN D, U, Ui, fao = cgetg(l, t_VEC), lo = cgetg(l, t_VEC);
     673       40103 :     F = mkvec2(P, E);
     674       40103 :     D = ZV_snf_group(cyc,&U,&Ui);
     675      133259 :     for (i = 1; i < l; i++)
     676             :     {
     677       93156 :       GEN t = gen_0, p = gel(P,i), p_1 = subiu(p,1);
     678       93156 :       long e = E[i];
     679       93156 :       gel(fao,i) = get_arith_ZZM(p_1);
     680       93156 :       if (e >= 2 && !absequaliu(p,2))
     681             :       {
     682        7392 :         GEN q = gel(mod,i), g = Fp_pow(gel(gen,i),p_1,q);
     683        7392 :         if (e == 2)
     684        6041 :           t = Fp_inv(diviiexact(subiu(g,1), p), p);
     685             :         else
     686        1351 :           t = ginv(Qp_log(cvtop(g,p,e)));
     687             :       }
     688       93156 :       gel(lo,i) = t;
     689             :     }
     690       40103 :     G = cgetg(l, t_VEC);
     691       40103 :     for (i = 1; i < l; i++) gel(G,i) = FpV_factorback(gen, gel(Ui,i), N);
     692       40103 :     G = mkvec3(ZV_prod(D), D, G);
     693       40103 :     G = mkvec5(mkvec2(N,mkvec(gen_0)), G, F,
     694             :                mkvecn(6,mod,fao,Ui,gen,cyc,lo), U);
     695             :   }
     696       44499 :   return gerepilecopy(av, G);
     697             : }
     698             : GEN
     699        4368 : znstar(GEN N) { return znstar0(N, 0); }
     700             : 
     701             : /* g has order 2^(e-2), g,h = 1 (mod 4); return x s.t. g^x = h (mod 2^e) */
     702             : static GEN
     703       28357 : Zideallog_2k(GEN h, GEN g, long e, GEN pe)
     704             : {
     705       28357 :   GEN a = Fp_log(h, g, int2n(e-2), pe);
     706       28357 :   if (typ(a) != t_INT) return NULL;
     707       28357 :   return a;
     708             : }
     709             : 
     710             : /* ord = get_arith_ZZM(p-1), simplified form of znlog_rec: g is known
     711             :  * to be a primitive root mod p^e; lo = 1/log_p(g^(p-1)) */
     712             : static GEN
     713      213353 : Zideallog_pk(GEN h, GEN g, GEN p, long e, GEN pe, GEN ord, GEN lo)
     714             : {
     715      213353 :   GEN gp = (e == 1)? g: modii(g, p);
     716      213353 :   GEN hp = (e == 1)? h: modii(h, p);
     717      213353 :   GEN a = Fp_log(hp, gp, ord, p);
     718      213353 :   if (typ(a) != t_INT) return NULL;
     719      213346 :   if (e > 1)
     720             :   { /* find a s.t. g^a = h (mod p^e), p odd prime, e > 0, (h,p) = 1 */
     721             :     /* use p-adic log: O(log p + e) mul*/
     722       10542 :     GEN b, p_1 = gel(ord,1);
     723       10542 :     h = Fp_mul(h, Fp_pow(g, negi(a), pe), pe);
     724             :     /* g,h = 1 mod p; compute b s.t. h = g^b */
     725       10542 :     if (e == 2) /* simpler */
     726        3633 :       b = Fp_mul(diviiexact(subiu(h,1), p), lo, p);
     727             :     else
     728        6909 :       b = padic_to_Q(gmul(Qp_log(cvtop(h, p, e)), lo));
     729       10542 :     a = addii(a, mulii(p_1, b));
     730             :   }
     731      213346 :   return a;
     732             : }
     733             : 
     734             : int
     735      653653 : znconrey_check(GEN cyc, GEN chi)
     736      653653 : { return typ(chi) == t_COL && lg(chi) == lg(cyc) && RgV_is_ZV(chi); }
     737             : 
     738             : int
     739      183624 : zncharcheck(GEN G, GEN chi)
     740             : {
     741      183624 :   switch(typ(chi))
     742             :   {
     743         875 :     case t_INT: return 1;
     744      182455 :     case t_COL: return znconrey_check(znstar_get_conreycyc(G), chi);
     745         294 :     case t_VEC: return char_check(znstar_get_cyc(G), chi);
     746             :   }
     747           0 :   return 0;
     748             : }
     749             : 
     750             : GEN
     751      108612 : znconreyfromchar_normalized(GEN bid, GEN chi)
     752             : {
     753      108612 :   GEN nchi, U = znstar_get_U(bid);
     754      108612 :   long l = lg(chi);
     755      108612 :   if (l == 1) retmkvec2(gen_1,cgetg(1,t_VEC));
     756      108584 :   if (!RgV_is_ZV(chi) || lgcols(U) != l) pari_err_TYPE("lfunchiZ", chi);
     757      108584 :   nchi = char_normalize(chi, cyc_normalize(znstar_get_cyc(bid)));
     758      108584 :   gel(nchi,2) = ZV_ZM_mul(gel(nchi,2),U); return nchi;
     759             : }
     760             : 
     761             : GEN
     762      108521 : znconreyfromchar(GEN bid, GEN chi)
     763             : {
     764      108521 :   GEN nchi = znconreyfromchar_normalized(bid, chi);
     765      108521 :   GEN v = char_denormalize(znstar_get_conreycyc(bid), gel(nchi,1), gel(nchi,2));
     766      108521 :   settyp(v, t_COL); return v;
     767             : }
     768             : 
     769             : /* discrete log on canonical "primitive root" generators
     770             :  * Allow log(x) instead of x [usual discrete log on bid's generators */
     771             : GEN
     772      252469 : znconreylog(GEN bid, GEN x)
     773             : {
     774      252469 :   pari_sp av = avma;
     775             :   GEN N, L, F, P,E, y, pe, fao, gen, lo, cycg;
     776             :   long i, l;
     777      252469 :   if (!checkznstar_i(bid)) pari_err_TYPE("znconreylog", bid);
     778      252469 :   N = znstar_get_N(bid);
     779      252469 :   if (typ(N) != t_INT) pari_err_TYPE("znconreylog", N);
     780      252462 :   if (cmpiu(N, 2) <= 0) return cgetg(1, t_COL);
     781      250166 :   cycg = znstar_get_conreycyc(bid);
     782      250166 :   switch(typ(x))
     783             :   {
     784             :     GEN Ui;
     785             :     case t_INT:
     786      249193 :       if (!signe(x)) pari_err_COPRIME("znconreylog", x, N);
     787      249186 :       break;
     788             :     case t_COL: /* log_bid(x) */
     789          35 :       Ui = znstar_get_Ui(bid);
     790          35 :       if (!RgV_is_ZV(x) || lg(x) != lg(Ui)) pari_err_TYPE("znconreylog", x);
     791          35 :       return gerepileupto(av, vecmodii(ZM_ZC_mul(Ui,x), cycg));
     792             :     case t_VEC:
     793         938 :       return gerepilecopy(av, znconreyfromchar(bid, x));
     794           0 :     default: pari_err_TYPE("znconreylog", x);
     795             :   }
     796      249186 :   F = znstar_get_faN(bid); /* factor(N) */
     797      249186 :   P = gel(F, 1); /* prime divisors of N */
     798      249186 :   E = gel(F, 2); /* exponents */
     799      249186 :   L = gel(bid,4);
     800      249186 :   pe = znstar_get_pe(bid);
     801      249186 :   fao = gel(L,2);
     802      249186 :   gen = znstar_get_conreygen(bid); /* local generators of (Z/p^k)^* */
     803      249186 :   lo = gel(L,6); /* 1/log_p((g_i)^(p_i-1)) */
     804             : 
     805      249186 :   l = lg(gen); i = 1;
     806      249186 :   y = cgetg(l, t_COL);
     807      249186 :   if (!mod2(N) && !mod2(x)) pari_err_COPRIME("znconreylog", x, N);
     808      249172 :   if (absequaliu(gel(P,1), 2) && E[1] >= 2)
     809             :   {
     810       88907 :     if (E[1] == 2)
     811       60550 :       gel(y,i++) = mod4(x) == 1? gen_0: gen_1;
     812             :     else
     813             :     {
     814       28357 :       GEN a, x2, q2 = gel(pe,1);
     815       28357 :       x2 = modii(x, q2);
     816       28357 :       if (mod4(x) == 1) /* 1 or 5 mod 8*/
     817       19551 :         gel(y,i++) = gen_0;
     818             :       else /* 3 or 7 */
     819        8806 :       { gel(y,i++) = gen_1; x2 = subii(q2, x2); }
     820             :       /* x2 = 5^x mod q */
     821       28357 :       a = Zideallog_2k(x2, gel(gen,i), E[1], q2);
     822       28357 :       if (!a) pari_err_COPRIME("znconreylog", x, N);
     823       28357 :       gel(y, i++) = a;
     824             :     }
     825             :   }
     826      711690 :   while (i < l)
     827             :   {
     828      213353 :     GEN p = gel(P,i), q = gel(pe,i), xpe = modii(x, q);
     829      213353 :     GEN a = Zideallog_pk(xpe, gel(gen,i), p, E[i], q, gel(fao,i), gel(lo,i));
     830      213353 :     if (!a) pari_err_COPRIME("znconreylog", x, N);
     831      213346 :     gel(y, i++) = a;
     832             :   }
     833      249165 :   return gerepilecopy(av, y);
     834             : }
     835             : GEN
     836        6720 : Zideallog(GEN bid, GEN x)
     837             : {
     838        6720 :   pari_sp av = avma;
     839        6720 :   GEN y = znconreylog(bid, x), U = znstar_get_U(bid);
     840        6692 :   return gerepileupto(av, ZM_ZC_mul(U, y));
     841             : }
     842             : GEN
     843         280 : znlog0(GEN h, GEN g, GEN o)
     844             : {
     845         280 :   if (typ(g) == t_VEC)
     846             :   {
     847             :     GEN N;
     848          56 :     if (o) pari_err_TYPE("znlog [with znstar]", o);
     849          56 :     if (!checkznstar_i(g)) pari_err_TYPE("znlog", h);
     850          56 :     N = znstar_get_N(g);
     851          56 :     h = Rg_to_Fp(h,N);
     852          49 :     return Zideallog(g, h);
     853             :   }
     854         224 :   return znlog(h, g, o);
     855             : }
     856             : 
     857             : GEN
     858       47852 : znconreyexp(GEN bid, GEN x)
     859             : {
     860       47852 :   pari_sp av = avma;
     861             :   long i, l;
     862             :   GEN N, pe, gen, cycg, v, vmod;
     863             :   int e2;
     864       47852 :   if (!checkznstar_i(bid)) pari_err_TYPE("znconreyexp", bid);
     865       47852 :   cycg = znstar_get_conreycyc(bid);
     866       47852 :   switch(typ(x))
     867             :   {
     868             :     case t_VEC:
     869          21 :       x = znconreylog(bid, x);
     870          21 :       break;
     871             :     case t_COL:
     872       47831 :       if (RgV_is_ZV(x) && lg(x) == lg(cycg)) break;
     873           7 :     default: pari_err_TYPE("znconreyexp",x);
     874             :   }
     875       47845 :   pe = znstar_get_pe(bid);
     876       47845 :   gen = znstar_get_conreygen(bid); /* local generators of (Z/p^k)^* */
     877       47845 :   cycg = znstar_get_conreycyc(bid);
     878       47845 :   l = lg(x); v = cgetg(l, t_VEC);
     879       47845 :   N = znstar_get_N(bid);
     880       47845 :   e2 = !mod8(N); /* 2 generators at p = 2 */
     881      153657 :   for (i = 1; i < l; i++)
     882             :   {
     883             :     GEN q, g, m;
     884      105812 :     if (i == 1 && e2) { gel(v,1) = NULL; continue; }
     885       95550 :     q = gel(pe,i);
     886       95550 :     g = gel(gen,i);
     887       95550 :     m = modii(gel(x,i), gel(cycg,i));
     888       95550 :     m = Fp_pow(g, m, q);
     889       95550 :     if (i == 2 && e2 && signe(gel(x,1))) m = Fp_neg(m, q);
     890       95550 :     gel(v,i) = mkintmod(m, q);
     891             :   }
     892       47845 :   if (e2) v = vecsplice(v, 1);
     893       47845 :   v = chinese1_coprime_Z(v);
     894       47845 :   vmod = gel(v,1);
     895       47845 :   v = gel(v,2);
     896       47845 :   if (mpodd(v) || mpodd(N)) return gerepilecopy(av, v);
     897             :   /* handle N = 2 mod 4 */
     898         861 :   return gerepileuptoint(av, addii(v, vmod));
     899             : }
     900             : 
     901             : /* Return Dirichlet character \chi_q(m,.), where bid = znstar(q);
     902             :  * m is either a t_INT, or a t_COL [Conrey logarithm] */
     903             : GEN
     904       28098 : znconreychar(GEN bid, GEN m)
     905             : {
     906       28098 :   pari_sp av = avma;
     907             :   GEN c, d, nchi;
     908             : 
     909       28098 :   switch(typ(m))
     910             :   {
     911             :     case t_COL:
     912             :     case t_INT:
     913       28098 :       nchi = znconrey_normalized(bid,m); /* images of primroot gens */
     914       28091 :       break;
     915             :     default:
     916           0 :       pari_err_TYPE("znconreychar",m);
     917             :       return NULL;/*LCOV_EXCL_LINE*/
     918             :   }
     919       28091 :   d = gel(nchi,1);
     920       28091 :   c = ZV_ZM_mul(gel(nchi,2), znstar_get_Ui(bid)); /* images of bid gens */
     921       28091 :   return gerepilecopy(av, char_denormalize(znstar_get_cyc(bid),d,c));
     922             : }
     923             : 
     924             : /* chi a t_INT or Conrey log describing a character. Return conductor, as an
     925             :  * integer if primitive; as a t_VEC [N,factor(N)] if not. Set *pm=m to the
     926             :  * attached primitive character: chi(g_i) = m[i]/ord(g_i)
     927             :  * Caller should use znconreylog_normalize(BID, m), once BID(conductor) is
     928             :  * computed (wasteful to do it here since BID is shared by many characters) */
     929             : GEN
     930      471891 : znconreyconductor(GEN bid, GEN chi, GEN *pm)
     931             : {
     932      471891 :   pari_sp av = avma;
     933             :   GEN q, m, F, P, E;
     934             :   long i, j, l;
     935      471891 :   int e2, primitive = 1;
     936             : 
     937      471891 :   if (!checkznstar_i(bid)) pari_err_TYPE("znconreyconductor", bid);
     938      471891 :   if (typ(chi) == t_COL)
     939             :   {
     940      471198 :     if (!znconrey_check(znstar_get_conreycyc(bid), chi))
     941           0 :       pari_err_TYPE("znconreyconductor",chi);
     942             :   }
     943             :   else
     944         693 :     chi = znconreylog(bid, chi);
     945      471891 :   l = lg(chi);
     946      471891 :   F = znstar_get_faN(bid);
     947      471891 :   P = gel(F,1);
     948      471891 :   E = gel(F,2);
     949      471891 :   if (l == 1)
     950             :   {
     951       83041 :     avma = av;
     952       83041 :     if (pm) *pm = cgetg(1,t_COL);
     953       83041 :     if (lg(P) == 1) return gen_1;
     954          63 :     retmkvec2(gen_1, trivial_fact());
     955             :   }
     956      388850 :   P = leafcopy(P);
     957      388850 :   E = leafcopy(E);
     958      388850 :   m = cgetg(l, t_COL);
     959      388850 :   e2 = (E[1] >= 3 && absequaliu(gel(P,1),2));
     960      388850 :   i = j = 1;
     961      388850 :   if (e2)
     962             :   { /* two generators at p=2 */
     963      262031 :     GEN a1 = gel(chi,1), a = gel(chi,2);
     964      262031 :     i = 3;
     965      262031 :     if (!signe(a))
     966             :     {
     967       92232 :       e2 =  primitive = 0;
     968       92232 :       if (signe(a1))
     969             :       { /* lose one generator */
     970       42483 :         E[1] = 2;
     971       42483 :         gel(m,1) = a1;
     972       42483 :         j = 2;
     973             :       }
     974             :       /* else lose both */
     975             :     }
     976             :     else
     977             :     {
     978      169799 :       long v = Z_pvalrem(a, gen_2, &a);
     979      169799 :       if (v) { E[1] -= v; E[2] = E[1]; primitive = 0; }
     980      169799 :       gel(m,1) = a1;
     981      169799 :       gel(m,2) = a;
     982      169799 :       j = 3;
     983             :     }
     984             :   }
     985      388850 :   l = lg(P);
     986     1127938 :   for (; i < l; i++)
     987             :   {
     988      739088 :     GEN p = gel(P,i), a = gel(chi,i);
     989             :     /* image of g_i in Q/Z is a/cycg[i], cycg[i] = order(g_i) */
     990      739088 :     if (!signe(a)) primitive = 0;
     991             :     else
     992             :     {
     993      531951 :       long v = Z_pvalrem(a, p, &a);
     994      531951 :       E[j] = E[i]; if (v) { E[j] -= v; primitive = 0; }
     995      531951 :       gel(P,j) = gel(P,i);
     996      531951 :       gel(m,j) = a; j++;
     997             :     }
     998             :   }
     999      388850 :   setlg(m,j);
    1000      388850 :   setlg(P,j);
    1001      388850 :   setlg(E,j);
    1002      388850 :   if (pm) *pm = m; /* attached primitive  character */
    1003      388850 :   if (primitive)
    1004             :   {
    1005      112462 :     q = znstar_get_N(bid);
    1006      112462 :     if (mod4(q) == 2) primitive = 0;
    1007             :   }
    1008      388850 :   if (!primitive)
    1009             :   {
    1010      278124 :     if (e2)
    1011             :     { /* remove duplicate p=2 row from factorization */
    1012      109613 :       P = vecsplice(P,1);
    1013      109613 :       E = vecsplice(E,1);
    1014             :     }
    1015      278124 :     E = zv_to_ZV(E);
    1016      278124 :     q = mkvec2(factorback2(P,E), mkmat2(P,E));
    1017             :   }
    1018      388850 :   gerepileall(av, pm? 2: 1, &q, pm);
    1019      388850 :   return q;
    1020             : }
    1021             : 
    1022             : GEN
    1023        9786 : zncharinduce(GEN G, GEN chi, GEN N)
    1024             : {
    1025        9786 :   pari_sp av = avma;
    1026             :   GEN q, faq, P, E, Pq, Eq, CHI;
    1027             :   long i, j, l;
    1028             :   int e2;
    1029             : 
    1030        9786 :   if (!checkznstar_i(G)) pari_err_TYPE("zncharinduce", G);
    1031        9786 :   if (!zncharcheck(G, chi)) pari_err_TYPE("zncharinduce", chi);
    1032        9786 :   q = znstar_get_N(G);
    1033        9786 :   if (typ(chi) != t_COL) chi = znconreylog(G, chi);
    1034        9786 :   if (checkznstar_i(N))
    1035             :   {
    1036        9597 :     GEN faN = znstar_get_faN(N);
    1037        9597 :     P = gel(faN,1); l = lg(P);
    1038        9597 :     E = gel(faN,2);
    1039        9597 :     N = znstar_get_N(N);
    1040        9597 :     if (l > 2 && equalii(gel(P,1),gel(P,2)))
    1041             :     { /* remove duplicate 2 */
    1042        1638 :       l--;
    1043        1638 :       P = vecsplice(P,1);
    1044        1638 :       E = vecsplice(E,1);
    1045             :     }
    1046             :   }
    1047             :   else
    1048             :   {
    1049         189 :     GEN faN = check_arith_pos(N, "zncharinduce");
    1050         189 :     if (!faN) faN = Z_factor(N);
    1051             :     else
    1052           0 :       N = (typ(N) == t_VEC)? gel(N,1): factorback(faN);
    1053         189 :     P = gel(faN,1);
    1054         189 :     E = gel(faN,2);
    1055             :   }
    1056        9786 :   if (!dvdii(N,q)) pari_err_DOMAIN("zncharinduce", "N % q", "!=", gen_0, N);
    1057        9779 :   if (mod4(N) == 2)
    1058             :   { /* remove 2 */
    1059         868 :     if (lg(P) > 1 && absequaliu(gel(P,1), 2))
    1060             :     {
    1061          77 :       P = vecsplice(P,1);
    1062          77 :       E = vecsplice(E,1);
    1063             :     }
    1064         868 :     N = shifti(N,-1);
    1065             :   }
    1066        9779 :   l = lg(P);
    1067             :   /* q = N or q = 2N, N odd */
    1068        9779 :   if (cmpii(N,q) <= 0) return gerepilecopy(av, chi);
    1069             :   /* N > 1 => l > 1*/
    1070        9002 :   if (typ(E) != t_VECSMALL) E = ZV_to_zv(E);
    1071        9002 :   e2 = (E[1] >= 3 && absequaliu(gel(P,1),2)); /* 2 generators at 2 mod N */
    1072        9002 :   if (ZV_equal0(chi))
    1073             :   {
    1074        5761 :     avma = av;
    1075        5761 :     return equali1(N)? cgetg(1, t_COL): zerocol(l+e2 - 1);
    1076             :   }
    1077             : 
    1078        3241 :   faq = znstar_get_faN(G);
    1079        3241 :   Pq = gel(faq,1);
    1080        3241 :   Eq = gel(faq,2);
    1081        3241 :   CHI = cgetg(l+e2, t_COL);
    1082        3241 :   i = j = 1;
    1083        3241 :   if (e2)
    1084             :   {
    1085        1113 :     i = 2; j = 3;
    1086        1113 :     if (absequaliu(gel(Pq,1), 2))
    1087             :     {
    1088         910 :       if (Eq[1] >= 3)
    1089             :       { /* 2 generators at 2 mod q */
    1090         434 :         gel(CHI,1) = gel(chi,1);
    1091         434 :         gel(CHI,2) = shifti(gel(chi,2), E[1]-Eq[1]);
    1092             :       }
    1093         476 :       else if (Eq[1] == 2)
    1094             :       { /* 1 generator at 2 mod q */
    1095         476 :         gel(CHI,1) = gel(chi,1);
    1096         476 :         gel(CHI,2) = gen_0;
    1097             :       }
    1098             :       else
    1099           0 :         gel(CHI,1) = gel(CHI,2) = gen_0;
    1100             :     }
    1101             :     else
    1102         203 :       gel(CHI,1) = gel(CHI,2) = gen_0;
    1103             :   }
    1104        8330 :   for (; i < l; i++,j++)
    1105             :   {
    1106        5089 :     GEN p = gel(P,i);
    1107        5089 :     long k = ZV_search(Pq, p);
    1108        5089 :     gel(CHI,j) = k? mulii(gel(chi,k), powiu(p, E[i]-Eq[k])): gen_0;
    1109             :   }
    1110        3241 :   return gerepilecopy(av, CHI);
    1111             : }
    1112             : 
    1113             : /* m a Conrey log [on the canonical primitive roots], cycg the primitive
    1114             :  * roots orders */
    1115             : GEN
    1116      294077 : znconreylog_normalize(GEN G, GEN m)
    1117             : {
    1118      294077 :   GEN cycg = znstar_get_conreycyc(G);
    1119             :   long i, l;
    1120      294077 :   GEN d, M = cgetg_copy(m, &l);
    1121      294077 :   if (typ(cycg) != t_VEC || lg(cycg) != l)
    1122           0 :     pari_err_TYPE("znconreylog_normalize",mkvec2(m,cycg));
    1123      294077 :   for (i = 1; i < l; i++) gel(M,i) = gdiv(gel(m,i), gel(cycg,i));
    1124             :   /* m[i]: image of primroot generators g_i in Q/Z */
    1125      294077 :   M = Q_remove_denom(M, &d);
    1126      294077 :   return mkvec2(d? d: gen_1, M);
    1127             : }
    1128             : 
    1129             : /* return normalized character on Conrey generators attached to chi: Conrey
    1130             :  * label (t_INT), char on (SNF) G.gen* (t_VEC), or Conrey log (t_COL) */
    1131             : GEN
    1132      290451 : znconrey_normalized(GEN G, GEN chi)
    1133             : {
    1134      290451 :   switch(typ(chi))
    1135             :   {
    1136             :     case t_INT: /* Conrey label */
    1137         420 :       return znconreylog_normalize(G, znconreylog(G, chi));
    1138             :     case t_COL: /* Conrey log */
    1139      289940 :       if (!RgV_is_ZV(chi)) break;
    1140      289940 :       return znconreylog_normalize(G, chi);
    1141             :     case t_VEC: /* char on G.gen */
    1142          91 :       if (!RgV_is_ZV(chi)) break;
    1143          91 :       return znconreyfromchar_normalized(G, chi);
    1144             :   }
    1145           0 :   pari_err_TYPE("znchareval",chi);
    1146             :   return NULL;/* LCOV_EXCL_LINE */
    1147             : }
    1148             : 
    1149             : /* return 1 iff chi(-1) = -1, and 0 otherwise */
    1150             : long
    1151      172606 : zncharisodd(GEN G, GEN chi)
    1152             : {
    1153             :   long i, l, s;
    1154             :   GEN N;
    1155      172606 :   if (!checkznstar_i(G)) pari_err_TYPE("zncharisodd", G);
    1156      172606 :   if (!zncharcheck(G, chi)) pari_err_TYPE("zncharisodd", chi);
    1157      172606 :   if (typ(chi) != t_COL) chi = znconreylog(G, chi);
    1158      172606 :   N = znstar_get_N(G);
    1159      172606 :   l = lg(chi);
    1160      172606 :   s = 0;
    1161      172606 :   if (!mod8(N))
    1162             :   {
    1163       88529 :     s = mpodd(gel(chi,1));
    1164       88529 :     i = 3;
    1165             :   }
    1166             :   else
    1167       84077 :     i = 1;
    1168      172606 :   for (; i < l; i++) s += mpodd(gel(chi,i));
    1169      172606 :   return odd(s);
    1170             : }
    1171             : 
    1172             : GEN
    1173         819 : znchartokronecker(GEN G, GEN chi, long flag)
    1174             : {
    1175         819 :   pari_sp av = avma;
    1176             :   long s;
    1177             :   GEN F, o;
    1178             : 
    1179         819 :   if (flag && flag != 1) pari_err_FLAG("znchartokronecker");
    1180         819 :   s = zncharisodd(G, chi)? -1: 1;
    1181         819 :   if (typ(chi) != t_COL) chi = znconreylog(G, chi);
    1182         819 :   o = zncharorder(G, chi);
    1183         819 :   if (abscmpiu(o,2) > 0) { avma = av; return gen_0; }
    1184         553 :   F = znconreyconductor(G, chi, NULL);
    1185         553 :   if (typ(F) == t_INT)
    1186             :   {
    1187         427 :     if (s < 0) F = negi(F);
    1188         427 :     return gerepileuptoint(av, F);
    1189             :   }
    1190         126 :   F = gel(F,1);
    1191         126 :   F = (s < 0)? negi(F): icopy(F);
    1192         126 :   if (!flag)
    1193             :   {
    1194          49 :     GEN MF = znstar_get_faN(G), P = gel(MF,1);
    1195          49 :     long i, l = lg(P);
    1196         140 :     for (i = 1; i < l; i++)
    1197             :     {
    1198          91 :       GEN p = gel(P,i);
    1199          91 :       if (!dvdii(F,p)) F = mulii(F,sqri(p));
    1200             :     }
    1201             :   }
    1202         126 :   return gerepileuptoint(av, F);
    1203             : }
    1204             : 
    1205             : /* (D/.) as a character mod N; assume |D| divides N and D = 0,1 mod 4*/
    1206             : GEN
    1207      302659 : znchar_quad(GEN G, GEN D)
    1208             : {
    1209      302659 :   GEN cyc = znstar_get_conreycyc(G);
    1210      302659 :   GEN gen = znstar_get_conreygen(G);
    1211      302659 :   long i, l = lg(cyc);
    1212      302659 :   GEN chi = cgetg(l, t_COL);
    1213     1350006 :   for (i = 1; i < l; i++)
    1214             :   {
    1215     1047347 :     long k = kronecker(D, gel(gen,i));
    1216     1047347 :     gel(chi,i) = (k==1)? gen_0: shifti(gel(cyc,i), -1);
    1217             :   }
    1218      302659 :   return chi;
    1219             : }
    1220             : 
    1221             : GEN
    1222        2835 : znchar(GEN D)
    1223             : {
    1224        2835 :   pari_sp av = avma;
    1225             :   GEN G, chi;
    1226        2835 :   switch(typ(D))
    1227             :   {
    1228             :     case t_INT:
    1229        2254 :       if (!signe(D) || Mod4(D) > 1) pari_err_TYPE("znchar", D);
    1230        2233 :       G = znstar0(D,1);
    1231        2233 :       chi = znchar_quad(G,D);
    1232        2233 :       break;
    1233             :     case t_INTMOD:
    1234         518 :       G = znstar0(gel(D,1), 1);
    1235         518 :       chi = znconreylog(G, gel(D,2));
    1236         518 :       break;
    1237             :     case t_VEC:
    1238          49 :       if (checkMF_i(D)) { D = MF_get_CHI(D); G=gel(D,1); chi=gel(D,2); break; }
    1239          42 :       if (lg(D) != 3) pari_err_TYPE("znchar", D);
    1240          35 :       G = gel(D,1);
    1241          35 :       if (!checkznstar_i(G)) pari_err_TYPE("znchar", D);
    1242          28 :       chi = gel(D,2);
    1243          28 :       if (typ(chi) == t_VEC && lg(chi) == 3 && is_vec_t(typ(gel(chi,2))))
    1244             :       { /* normalized character */
    1245           7 :         GEN n = gel(chi,1), chic = gel(chi,2);
    1246           7 :         GEN cyc = typ(chic)==t_VEC? znstar_get_cyc(G): znstar_get_conreycyc(G);
    1247           7 :         if (!char_check(cyc, chic)) pari_err_TYPE("znchar",D);
    1248           7 :         chi = char_denormalize(cyc, n, chic);
    1249             :       }
    1250          28 :       if (!zncharcheck(G, chi)) pari_err_TYPE("znchar", D);
    1251          21 :       break;
    1252             :     default:
    1253          14 :       pari_err_TYPE("znchar", D);
    1254             :       return NULL; /*LCOV_EXCL_LINE*/
    1255             :   }
    1256        2779 :   return gerepilecopy(av, mkvec2(G, chi));
    1257             : }
    1258             : 
    1259             : /* G a znstar, not stack clean */
    1260             : GEN
    1261      239463 : znchareval(GEN G, GEN chi, GEN n, GEN z)
    1262             : {
    1263      239463 :   GEN nchi, N = znstar_get_N(G);
    1264             :   /* avoid division by 0 */
    1265      239463 :   if (typ(n) == t_FRAC && !equali1(gcdii(gel(n,2), N))) return not_coprime(z);
    1266      239456 :   n = Rg_to_Fp(n, N);
    1267      239456 :   if (!equali1(gcdii(n, N))) return not_coprime(z);
    1268             :   /* nchi: normalized character on Conrey generators */
    1269      237622 :   nchi = znconrey_normalized(G, chi);
    1270      237622 :   return chareval_i(nchi, znconreylog(G,n), z);
    1271             : }
    1272             : 
    1273             : /* G is a znstar, chi a Dirichlet character */
    1274             : GEN
    1275         154 : zncharconj(GEN G, GEN chi)
    1276             : {
    1277         154 :   switch(typ(chi))
    1278             :   {
    1279           7 :     case t_INT: chi = znconreylog(G, chi); /* fall through */
    1280         147 :     case t_COL: return charconj(znstar_get_conreycyc(G), chi);
    1281           7 :     case t_VEC: return charconj(znstar_get_cyc(G), chi);
    1282             :   }
    1283           0 :   pari_err_TYPE("zncharconj",chi);
    1284             :   return NULL; /*LCOV_EXCL_LINE*/
    1285             : }
    1286             : 
    1287             : /* G is a znstar, chi a Dirichlet character */
    1288             : GEN
    1289      370013 : zncharorder(GEN G,  GEN chi)
    1290             : {
    1291      370013 :   switch(typ(chi))
    1292             :   {
    1293          21 :     case t_INT: chi = znconreylog(G, chi); /*fall through*/
    1294      369880 :     case t_COL: return charorder(znstar_get_conreycyc(G), chi);
    1295         133 :     case t_VEC: return charorder(znstar_get_cyc(G), chi);
    1296           0 :     default: pari_err_TYPE("zncharorder",chi);
    1297             :              return NULL; /* LCOV_EXCL_LINE */
    1298             :   }
    1299             : }
    1300             : 
    1301             : /* G is a znstar, chi a Dirichlet character */
    1302             : GEN
    1303          21 : zncharker(GEN G, GEN chi)
    1304             : {
    1305          21 :   if (typ(chi) != t_VEC) chi = znconreychar(G, chi);
    1306          21 :   return charker(znstar_get_cyc(G), chi);
    1307             : }
    1308             : 
    1309             : /* G is a znstar, 'a' is a Dirichlet character */
    1310             : GEN
    1311         126 : zncharpow(GEN G, GEN a, GEN n)
    1312             : {
    1313         126 :   switch(typ(a))
    1314             :   {
    1315          21 :     case t_INT: return Fp_pow(a, n, znstar_get_N(G));
    1316          21 :     case t_VEC: return charpow(znstar_get_cyc(G), a, n);
    1317          84 :     case t_COL: return charpow(znstar_get_conreycyc(G), a, n);
    1318           0 :     default: pari_err_TYPE("znchapow",a);
    1319             :              return NULL; /* LCOV_EXCL_LINE */
    1320             :   }
    1321             : }
    1322             : /* G is a znstar, 'a' and 'b' are Dirichlet character */
    1323             : GEN
    1324      301140 : zncharmul(GEN G, GEN a, GEN b)
    1325             : {
    1326      301140 :   long ta = typ(a), tb = typ(b);
    1327      301140 :   if (ta == tb) switch(ta)
    1328             :   {
    1329           7 :     case t_INT: return Fp_mul(a, b, znstar_get_N(G));
    1330           7 :     case t_VEC: return charmul(znstar_get_cyc(G), a, b);
    1331      301105 :     case t_COL: return charmul(znstar_get_conreycyc(G), a, b);
    1332           0 :     default: pari_err_TYPE("zncharmul",a);
    1333             :              return NULL; /* LCOV_EXCL_LINE */
    1334             :   }
    1335          21 :   if (ta != t_COL) a = znconreylog(G, a);
    1336          21 :   if (tb != t_COL) b = znconreylog(G, b);
    1337          21 :   return charmul(znstar_get_conreycyc(G), a, b);
    1338             : }
    1339             : 
    1340             : /* G is a znstar, 'a' and 'b' are Dirichlet character */
    1341             : GEN
    1342        6517 : znchardiv(GEN G, GEN a, GEN b)
    1343             : {
    1344        6517 :   long ta = typ(a), tb = typ(b);
    1345        6517 :   if (ta == tb) switch(ta)
    1346             :   {
    1347           7 :     case t_INT: return Fp_div(a, b, znstar_get_N(G));
    1348           7 :     case t_VEC: return chardiv(znstar_get_cyc(G), a, b);
    1349        6482 :     case t_COL: return chardiv(znstar_get_conreycyc(G), a, b);
    1350           0 :     default: pari_err_TYPE("znchardiv",a);
    1351             :              return NULL; /* LCOV_EXCL_LINE */
    1352             :   }
    1353          21 :   if (ta != t_COL) a = znconreylog(G, a);
    1354          21 :   if (tb != t_COL) b = znconreylog(G, b);
    1355          21 :   return chardiv(znstar_get_conreycyc(G), a, b);
    1356             : }
    1357             : 
    1358             : /* CHI mod N = \prod_p p^e; let CHI = \prod CHI_p, CHI_p mod p^e
    1359             :  * return \prod_{p | (Q,N)} CHI_p. E.g if Q = p, return chi_p */
    1360             : GEN
    1361         784 : znchardecompose(GEN G, GEN chi, GEN Q)
    1362             : {
    1363             :   GEN c, P, E, F;
    1364             :   long l, lP, i;
    1365             : 
    1366         784 :   if (!checkznstar_i(G)) pari_err_TYPE("znchardecompose", G);
    1367         784 :   if (typ(Q) != t_INT) pari_err_TYPE("znchardecompose", Q);
    1368         784 :   if (typ(chi) == t_COL)
    1369         560 :   { if (!zncharcheck(G, chi)) pari_err_TYPE("znchardecompose", chi); }
    1370             :   else
    1371         224 :     chi = znconreylog(G, chi);
    1372         784 :   l = lg(chi);
    1373         784 :   F = znstar_get_faN(G);
    1374         784 :   c = zerocol(l-1);
    1375         784 :   P = gel(F,1); /* prime divisors of N */
    1376         784 :   lP = lg(P);
    1377         784 :   E = gel(F,2); /* exponents */
    1378        2471 :   for (i = 1; i < lP; i++)
    1379             :   {
    1380        1687 :     GEN p = gel(P,i);
    1381        1687 :     if (i == 1 && equaliu(p,2) && E[1] >= 3)
    1382             :     {
    1383         567 :       if (!mpodd(Q))
    1384             :       {
    1385         203 :         gel(c,1) = icopy(gel(chi,1));
    1386         203 :         gel(c,2) = icopy(gel(chi,2));
    1387             :       }
    1388         567 :       i = 2; /* skip P[2] = P[1] = 2 */
    1389             :     }
    1390             :     else
    1391        1120 :       if (dvdii(Q, p)) gel(c,i) = icopy(gel(chi,i));
    1392             :   }
    1393         784 :   return c;
    1394             : }
    1395             : 
    1396             : GEN
    1397          21 : zncharconductor(GEN G, GEN chi)
    1398             : {
    1399          21 :   pari_sp av = avma;
    1400          21 :   GEN F = znconreyconductor(G, chi, NULL);
    1401          21 :   if (typ(F) == t_INT) return F;
    1402          14 :   return gerepilecopy(av, gel(F,1));
    1403             : }
    1404             : GEN
    1405         140 : znchartoprimitive(GEN G, GEN chi)
    1406             : {
    1407         140 :   pari_sp av = avma;
    1408         140 :   GEN chi0, F = znconreyconductor(G, chi, &chi0);
    1409         140 :   if (typ(F) == t_INT)
    1410          98 :     chi = mkvec2(G,chi);
    1411             :   else
    1412          42 :     chi = mkvec2(znstar0(F,1), chi0);
    1413         140 :   return gerepilecopy(av, chi);
    1414             : }

Generated by: LCOV version 1.11