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

Generated by: LCOV version 1.11