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; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 : #include "pari.h"
15 : #include "paripriv.h"
16 :
17 : #define DEBUGLEVEL DEBUGLEVEL_alg
18 :
19 : #define dbg_printf(lvl) if (DEBUGLEVEL >= (lvl) + 3) err_printf
20 :
21 : /********************************************************************/
22 : /** **/
23 : /** ASSOCIATIVE ALGEBRAS, CENTRAL SIMPLE ALGEBRAS **/
24 : /** contributed by Aurel Page (2014) **/
25 : /** **/
26 : /********************************************************************/
27 : static GEN alg_subalg(GEN al, GEN basis);
28 : static GEN alg_maximal_primes(GEN al, GEN P);
29 : static GEN algnatmultable(GEN al, long D);
30 : static GEN _tablemul_ej(GEN mt, GEN x, long j);
31 : static GEN _tablemul_ej_Fp(GEN mt, GEN x, long j, GEN p);
32 : static GEN _tablemul_ej_Fl(GEN mt, GEN x, long j, ulong p);
33 : static ulong algtracei(GEN mt, ulong p, ulong expo, ulong modu);
34 : static GEN alg_pmaximal(GEN al, GEN p);
35 : static GEN alg_maximal(GEN al);
36 : static GEN algtracematrix(GEN al);
37 : static GEN algtableinit_i(GEN mt0, GEN p);
38 : static GEN algbasisrightmultable(GEN al, GEN x);
39 : static GEN algabstrace(GEN al, GEN x);
40 : static GEN algbasismul(GEN al, GEN x, GEN y);
41 : static GEN algbasismultable(GEN al, GEN x);
42 : static GEN algbasismultable_Flm(GEN mt, GEN x, ulong m);
43 :
44 : static void H_compo(GEN x, GEN* a, GEN* b, GEN* c, GEN* d);
45 : static GEN H_add(GEN x, GEN y);
46 : static GEN H_charpoly(GEN x, long v, long abs);
47 : static GEN H_divl_i(GEN x, GEN y);
48 : static GEN H_inv(GEN x);
49 : static GEN H_mul(GEN x, GEN y);
50 : static GEN H_neg(GEN x);
51 : static GEN H_norm(GEN x, long abs);
52 : static GEN H_random(GEN b);
53 : static GEN H_sqr(GEN x);
54 : static GEN H_tomatrix(GEN x, long abs);
55 : static GEN H_trace(GEN x, long abs);
56 : static GEN mk_C();
57 :
58 : static int
59 826996 : checkalg_i(GEN al)
60 : {
61 : GEN mt, rnf;
62 : long t;
63 826996 : if (typ(al) != t_VEC || lg(al) != 12) return 0;
64 826779 : mt = alg_get_multable(al);
65 826779 : if (typ(mt) != t_VEC || lg(mt) == 1 || typ(gel(mt,1)) != t_MAT) return 0;
66 826758 : rnf = alg_get_splittingfield(al);
67 826758 : if (isintzero(rnf) || !gequal0(alg_get_char(al)))
68 362952 : return 1;
69 463806 : if (typ(gel(al,2)) != t_VEC || lg(gel(al,2)) == 1) return 0;
70 : /* not checkrnf_i: beware placeholder from alg_csa_table */
71 463799 : t = typ(rnf);
72 463799 : return t==t_COMPLEX || t==t_REAL || (t==t_VEC && lg(rnf)==13);
73 : }
74 : void
75 956349 : checkalg(GEN al)
76 : {
77 956349 : if (al && !checkalg_i(al))
78 112 : pari_err_TYPE("checkalg [please apply alginit()]",al);
79 956237 : }
80 :
81 : static int
82 180992 : checklat_i(GEN al, GEN lat)
83 : {
84 : long N,i,j;
85 : GEN m,t,c;
86 180992 : if (typ(lat)!=t_VEC || lg(lat) != 3) return 0;
87 180992 : t = gel(lat,2);
88 180992 : if (typ(t) != t_INT && typ(t) != t_FRAC) return 0;
89 180992 : if (gsigne(t)<=0) return 0;
90 180992 : m = gel(lat,1);
91 180992 : if (typ(m) != t_MAT) return 0;
92 180992 : N = alg_get_absdim(al);
93 180992 : if (lg(m)-1 != N || lg(gel(m,1))-1 != N) return 0;
94 1628886 : for (i=1; i<=N; i++)
95 13031067 : for (j=1; j<=N; j++) {
96 11583173 : c = gcoeff(m,i,j);
97 11583173 : if (typ(c) != t_INT) return 0;
98 11583173 : if (j<i && signe(gcoeff(m,i,j))) return 0;
99 11583173 : if (i==j && !signe(gcoeff(m,i,j))) return 0;
100 : }
101 180985 : return 1;
102 : }
103 180992 : void checklat(GEN al, GEN lat)
104 180992 : { if (!checklat_i(al,lat)) pari_err_TYPE("checklat [please apply alglathnf()]", lat); }
105 :
106 : /** ACCESSORS **/
107 : long
108 5630505 : alg_type(GEN al)
109 : {
110 : long t;
111 5630505 : if (!al) return al_REAL;
112 5500354 : t = typ(alg_get_splittingfield(al));
113 5500354 : if (t==t_REAL || t==t_COMPLEX) return al_REAL;
114 5496938 : if (isintzero(alg_get_splittingfield(al)) || !gequal0(alg_get_char(al))) return al_TABLE;
115 3924464 : switch(typ(gmael(al,2,1))) {
116 932974 : case t_MAT: return al_CSA;
117 2991455 : case t_INT:
118 : case t_FRAC:
119 : case t_POL:
120 2991455 : case t_POLMOD: return al_CYCLIC;
121 35 : default: return al_NULL;
122 : }
123 : return -1; /*LCOV_EXCL_LINE*/
124 : }
125 : long
126 224 : algtype(GEN al)
127 224 : { return checkalg_i(al)? alg_type(al): al_NULL; }
128 :
129 : /* absdim == dim for al_TABLE. */
130 : static long
131 238 : algreal_dim(GEN al)
132 : {
133 238 : switch(lg(alg_get_multable(al))) {
134 154 : case 2: case 3: return 1;
135 77 : case 5: return 4;
136 7 : default: pari_err_TYPE("algreal_dim", al);
137 : }
138 : return -1; /*LCOV_EXCL_LINE*/
139 : }
140 : long
141 225155 : alg_get_dim(GEN al)
142 : {
143 : long d;
144 225155 : if (!al) return 4;
145 225155 : switch(alg_type(al)) {
146 10787 : case al_TABLE: return lg(alg_get_multable(al))-1;
147 214256 : case al_CSA: return lg(alg_get_relmultable(al))-1;
148 77 : case al_CYCLIC: d = alg_get_degree(al); return d*d;
149 28 : case al_REAL: return algreal_dim(al);
150 7 : default: pari_err_TYPE("alg_get_dim", al);
151 : }
152 : return -1; /*LCOV_EXCL_LINE*/
153 : }
154 :
155 : long
156 1592841 : alg_get_absdim(GEN al)
157 : {
158 1592841 : if (!al) return 4;
159 1546123 : switch(alg_type(al)) {
160 669422 : case al_TABLE: case al_REAL: return lg(alg_get_multable(al))-1;
161 113323 : case al_CSA: return alg_get_dim(al)*nf_get_degree(alg_get_center(al));
162 763371 : case al_CYCLIC:
163 763371 : return rnf_get_absdegree(alg_get_splittingfield(al))*alg_get_degree(al);
164 7 : default: pari_err_TYPE("alg_get_absdim", al);
165 : }
166 : return -1;/*LCOV_EXCL_LINE*/
167 : }
168 :
169 : long
170 1778 : algdim(GEN al, long abs)
171 : {
172 1778 : checkalg(al);
173 1757 : if (abs) return alg_get_absdim(al);
174 1526 : return alg_get_dim(al);
175 : }
176 :
177 : /* only cyclic */
178 : GEN
179 13818 : alg_get_auts(GEN al)
180 : {
181 13818 : long ta = alg_type(al);
182 13818 : if (ta != al_CYCLIC && ta != al_REAL)
183 0 : pari_err_TYPE("alg_get_auts [noncyclic algebra]", al);
184 13818 : return gel(al,2);
185 : }
186 : GEN
187 112 : alg_get_aut(GEN al)
188 : {
189 112 : long ta = alg_type(al);
190 112 : if (ta != al_CYCLIC && ta != al_REAL)
191 7 : pari_err_TYPE("alg_get_aut [noncyclic algebra]", al);
192 105 : return gel(alg_get_auts(al),1);
193 : }
194 : GEN
195 42 : algaut(GEN al) { checkalg(al); return alg_get_aut(al); }
196 : GEN
197 13839 : alg_get_b(GEN al)
198 : {
199 13839 : long ta = alg_type(al);
200 13839 : if (ta != al_CYCLIC && ta != al_REAL)
201 7 : pari_err_TYPE("alg_get_b [noncyclic algebra]", al);
202 13832 : return gel(al,3);
203 : }
204 : GEN
205 56 : algb(GEN al) { checkalg(al); return alg_get_b(al); }
206 :
207 : /* only CSA */
208 : GEN
209 216356 : alg_get_relmultable(GEN al)
210 : {
211 216356 : if (alg_type(al) != al_CSA)
212 14 : pari_err_TYPE("alg_get_relmultable [algebra not given via mult. table]", al);
213 216342 : return gel(al,2);
214 : }
215 : GEN
216 49 : algrelmultable(GEN al) { checkalg(al); return alg_get_relmultable(al); }
217 : GEN
218 56 : alg_get_splittingdata(GEN al)
219 : {
220 56 : if (alg_type(al) != al_CSA)
221 14 : pari_err_TYPE("alg_get_splittingdata [algebra not given via mult. table]",al);
222 42 : return gel(al,3);
223 : }
224 : GEN
225 56 : algsplittingdata(GEN al) { checkalg(al); return alg_get_splittingdata(al); }
226 : GEN
227 4102 : alg_get_splittingbasis(GEN al)
228 : {
229 4102 : if (alg_type(al) != al_CSA)
230 0 : pari_err_TYPE("alg_get_splittingbasis [algebra not given via mult. table]",al);
231 4102 : return gmael(al,3,2);
232 : }
233 : GEN
234 4102 : alg_get_splittingbasisinv(GEN al)
235 : {
236 4102 : if (alg_type(al) != al_CSA)
237 0 : pari_err_TYPE("alg_get_splittingbasisinv [algebra not given via mult. table]",al);
238 4102 : return gmael(al,3,3);
239 : }
240 :
241 : /* only cyclic and CSA */
242 : GEN
243 14252565 : alg_get_splittingfield(GEN al) { return gel(al,1); }
244 : GEN
245 119 : algsplittingfield(GEN al)
246 : {
247 : long ta;
248 119 : checkalg(al);
249 119 : ta = alg_type(al);
250 119 : if (ta != al_CYCLIC && ta != al_CSA && ta != al_REAL)
251 7 : pari_err_TYPE("alg_get_splittingfield [use alginit]",al);
252 112 : return alg_get_splittingfield(al);
253 : }
254 : long
255 1207605 : alg_get_degree(GEN al)
256 : {
257 : long ta;
258 1207605 : ta = alg_type(al);
259 1207605 : if (ta == al_REAL) return algreal_dim(al)==1? 1 : 2;
260 1207521 : if (ta != al_CYCLIC && ta != al_CSA)
261 21 : pari_err_TYPE("alg_get_degree [use alginit]",al);
262 1207500 : return rnf_get_degree(alg_get_splittingfield(al));
263 : }
264 : long
265 322 : algdegree(GEN al)
266 : {
267 322 : checkalg(al);
268 315 : return alg_get_degree(al);
269 : }
270 :
271 : GEN
272 298765 : alg_get_center(GEN al)
273 : {
274 : long ta;
275 298765 : ta = alg_type(al);
276 298765 : if (ta == al_REAL)
277 : {
278 21 : if (algreal_dim(al) != 4) return alg_get_splittingfield(al);
279 7 : return stor(1,3);
280 : }
281 298744 : if (ta != al_CSA && ta != al_CYCLIC)
282 7 : pari_err_TYPE("alg_get_center [use alginit]",al);
283 298737 : return rnf_get_nf(alg_get_splittingfield(al));
284 : }
285 : GEN
286 70 : alg_get_splitpol(GEN al)
287 : {
288 70 : long ta = alg_type(al);
289 70 : if (ta != al_CYCLIC && ta != al_CSA)
290 0 : pari_err_TYPE("alg_get_splitpol [use alginit]",al);
291 70 : return rnf_get_pol(alg_get_splittingfield(al));
292 : }
293 : GEN
294 69916 : alg_get_abssplitting(GEN al)
295 : {
296 69916 : long ta = alg_type(al), prec;
297 69916 : if (ta != al_CYCLIC && ta != al_CSA)
298 0 : pari_err_TYPE("alg_get_abssplitting [use alginit]",al);
299 69916 : prec = nf_get_prec(alg_get_center(al));
300 69916 : return rnf_build_nfabs(alg_get_splittingfield(al), prec);
301 : }
302 : GEN
303 1169 : alg_get_hasse_i(GEN al)
304 : {
305 1169 : long ta = alg_type(al);
306 1169 : if (ta != al_CYCLIC && ta != al_CSA && ta != al_REAL)
307 7 : pari_err_TYPE("alg_get_hasse_i [use alginit]",al);
308 1162 : if (ta == al_CSA) pari_err_IMPL("computation of Hasse invariants over table CSA");
309 1155 : return gel(al,4);
310 : }
311 : GEN
312 231 : alghassei(GEN al) { checkalg(al); return alg_get_hasse_i(al); }
313 : GEN
314 1911 : alg_get_hasse_f(GEN al)
315 : {
316 1911 : long ta = alg_type(al);
317 1911 : if (ta != al_CYCLIC && ta != al_CSA)
318 7 : pari_err_TYPE("alg_get_hasse_f [use alginit]",al);
319 1904 : if (ta == al_CSA) pari_err_IMPL("computation of Hasse invariants over table CSA");
320 1897 : return gel(al,5);
321 : }
322 : GEN
323 329 : alghassef(GEN al) { checkalg(al); return alg_get_hasse_f(al); }
324 :
325 : /* all types */
326 : GEN
327 2716 : alg_get_basis(GEN al) { return gel(al,7); }
328 : GEN
329 63 : algbasis(GEN al) { checkalg(al); return alg_get_basis(al); }
330 : GEN
331 60683 : alg_get_invbasis(GEN al) { return gel(al,8); }
332 : GEN
333 63 : alginvbasis(GEN al) { checkalg(al); return alg_get_invbasis(al); }
334 : GEN
335 2261379 : alg_get_multable(GEN al) { return gel(al,9); }
336 : GEN
337 245 : algmultable(GEN al) { checkalg(al); return alg_get_multable(al); }
338 : GEN
339 5936143 : alg_get_char(GEN al) { if (!al) return gen_0; return gel(al,10); }
340 : GEN
341 112 : algchar(GEN al) { checkalg(al); return alg_get_char(al); }
342 : GEN
343 236466 : alg_get_tracebasis(GEN al) { return gel(al,11); }
344 :
345 : /* lattices */
346 : GEN
347 244314 : alglat_get_primbasis(GEN lat) { return gel(lat,1); }
348 : GEN
349 289905 : alglat_get_scalar(GEN lat) { return gel(lat,2); }
350 :
351 : /** ADDITIONAL **/
352 :
353 : /* is N=smooth*prime? */
354 1018 : static int Z_easyfactor(GEN N, ulong lim)
355 : {
356 : GEN fa;
357 1018 : if (lgefint(N) <= 3) return 1;
358 91 : fa = absZ_factor_limit(N, lim);
359 91 : return BPSW_psp(veclast(gel(fa,1)));
360 : }
361 :
362 : /* no garbage collection */
363 : static GEN
364 840 : backtrackfacto(GEN y0, long n, GEN red, GEN pl, GEN nf, GEN data, int (*test)(GEN,GEN), GEN* fa, GEN N, GEN I)
365 : {
366 : long b, i;
367 840 : ulong lim = 1UL << 17;
368 840 : long *v = new_chunk(n+1);
369 840 : pari_sp av = avma;
370 840 : for (b = 0;; b += (2*b)/(3*n) + 1)
371 42 : {
372 : GEN ny, y1, y2;
373 882 : set_avma(av);
374 2758 : for (i = 1; i <= n; i++) v[i] = -b;
375 882 : v[n]--;
376 : for(;;)
377 : {
378 1127 : i = n;
379 1425 : while (i > 0)
380 1383 : { if (v[i] == b) v[i--] = -b; else { v[i]++; break; } }
381 1127 : if (i==0) break;
382 :
383 1085 : y1 = y0;
384 4576 : for (i = 1; i <= n; i++) y1 = nfadd(nf, y1, ZC_z_mul(gel(red,i), v[i]));
385 1085 : if (!nfchecksigns(nf, y1, pl)) continue;
386 :
387 1018 : ny = absi_shallow(nfnorm(nf, y1));
388 1018 : if (!signe(ny)) continue;
389 1018 : ny = diviiexact(ny, gcdii(ny, N));
390 1018 : if (!Z_easyfactor(ny, lim)) continue;
391 :
392 962 : y2 = idealdivexact(nf, y1, idealadd(nf,y1,I));
393 962 : *fa = idealfactor(nf, y2);
394 962 : if (!data || test(data,*fa)) return y1;
395 : }
396 : }
397 : }
398 :
399 : /* if data == NULL, the test is skipped */
400 : /* in the test, the factorization does not contain the known factors */
401 : static GEN
402 840 : factoredextchinesetest(GEN nf, GEN x, GEN y, GEN pl, GEN* fa, GEN data, int (*test)(GEN,GEN))
403 : {
404 840 : pari_sp av = avma;
405 : long n,i;
406 840 : GEN x1, y0, y1, red, N, I, P = gel(x,1), E = gel(x,2);
407 840 : n = nf_get_degree(nf);
408 840 : x = idealchineseinit(nf, mkvec2(x,pl));
409 840 : x1 = gel(x,1);
410 840 : red = lg(x1) == 1? matid(n): gmael(x1,1,1);
411 840 : y0 = idealchinese(nf, x, y);
412 :
413 840 : E = shallowcopy(E);
414 840 : if (!gequal0(y0))
415 2372 : for (i=1; i<lg(E); i++)
416 : {
417 1532 : long v = nfval(nf,y0,gel(P,i));
418 1532 : if (cmpsi(v, gel(E,i)) < 0) gel(E,i) = stoi(v);
419 : }
420 : /* N and I : known factors */
421 840 : I = factorbackprime(nf, P, E);
422 840 : N = idealnorm(nf,I);
423 :
424 840 : y1 = backtrackfacto(y0, n, red, pl, nf, data, test, fa, N, I);
425 :
426 : /* restore known factors */
427 2372 : for (i=1; i<lg(E); i++) gel(E,i) = stoi(nfval(nf,y1,gel(P,i)));
428 840 : *fa = famat_reduce(famat_mul_shallow(*fa, mkmat2(P, E)));
429 840 : return gc_all(av, 2, &y1, fa);
430 : }
431 :
432 : static GEN
433 581 : factoredextchinese(GEN nf, GEN x, GEN y, GEN pl, GEN* fa)
434 581 : { return factoredextchinesetest(nf,x,y,pl,fa,NULL,NULL); }
435 :
436 : /** OPERATIONS ON ASSOCIATIVE ALGEBRAS algebras.c **/
437 :
438 : /*
439 : Convention:
440 : (K/F,sigma,b) = sum_{i=0..n-1} u^i*K
441 : t*u = u*sigma(t)
442 :
443 : Natural basis:
444 : 1<=i<=d*n^2
445 : b_i = u^((i-1)/(dn))*ZKabs.((i-1)%(dn)+1)
446 :
447 : Integral basis:
448 : Basis of some order.
449 :
450 : al:
451 : 1- rnf of the cyclic splitting field of degree n over the center nf of degree d
452 : 2- VEC of aut^i 1<=i<=n if n>1, or i=0 if n=1
453 : 3- b in nf
454 : 4- infinite hasse invariants (mod n) : VECSMALL of size r1, values only 0 or n/2 (if integral)
455 : 5- finite hasse invariants (mod n) : VEC[VEC of primes, VECSMALL of hasse inv mod n]
456 : 6- nf of the splitting field (absolute)
457 : 7* dn^2*dn^2 matrix expressing the integral basis in terms of the natural basis
458 : 8* dn^2*dn^2 matrix expressing the natural basis in terms of the integral basis
459 : 9* VEC of dn^2 matrices giving the dn^2*dn^2 left multiplication tables of the integral basis
460 : 10* characteristic of the base field (used only for algebras given by a multiplication table)
461 : 11* trace of basis elements
462 :
463 : If al is given by a multiplication table (al_TABLE), only the * fields are present.
464 : */
465 :
466 : /* assumes same center and same variable */
467 : /* currently only works for coprime degrees */
468 : GEN
469 84 : algtensor(GEN al1, GEN al2, long maxord) {
470 84 : pari_sp av = avma;
471 : long v, k, d1, d2;
472 : GEN nf, P1, P2, aut1, aut2, b1, b2, C, rnf, aut, b, x1, x2, al;
473 :
474 84 : checkalg(al1);
475 70 : checkalg(al2);
476 63 : if (alg_type(al1) != al_CYCLIC || alg_type(al2) != al_CYCLIC)
477 21 : pari_err_IMPL("tensor of noncyclic algebras"); /* TODO: do it. */
478 :
479 42 : nf = alg_get_center(al1);
480 42 : if (!gequal(alg_get_center(al2),nf))
481 7 : pari_err_OP("tensor product [not the same center]", al1, al2);
482 :
483 35 : P1=alg_get_splitpol(al1); aut1=alg_get_aut(al1); b1=alg_get_b(al1);
484 35 : P2=alg_get_splitpol(al2); aut2=alg_get_aut(al2); b2=alg_get_b(al2);
485 35 : v=varn(P1);
486 :
487 35 : d1=alg_get_degree(al1);
488 35 : d2=alg_get_degree(al2);
489 35 : if (ugcd(d1,d2) != 1)
490 7 : pari_err_IMPL("tensor of cyclic algebras of noncoprime degrees"); /* TODO */
491 :
492 28 : if (d1==1) return gcopy(al2);
493 21 : if (d2==1) return gcopy(al1);
494 :
495 14 : C = nfcompositum(nf, P1, P2, 3);
496 14 : rnf = rnfinit(nf,gel(C,1));
497 14 : x1 = gel(C,2);
498 14 : x2 = gel(C,3);
499 14 : k = itos(gel(C,4));
500 14 : aut = gadd(gsubst(aut2,v,x2),gmulsg(k,gsubst(aut1,v,x1)));
501 14 : b = nfmul(nf,nfpow_u(nf,b1,d2),nfpow_u(nf,b2,d1));
502 14 : al = alg_cyclic(rnf,aut,b,maxord);
503 14 : return gerepilecopy(av,al);
504 : }
505 :
506 : /* M an n x d Flm of rank d, n >= d. Initialize Mx = y solver */
507 : static GEN
508 4383 : Flm_invimage_init(GEN M, ulong p)
509 : {
510 4383 : GEN v = Flm_indexrank(M, p), perm = gel(v,1);
511 4383 : GEN MM = rowpermute(M, perm); /* square invertible */
512 4383 : return mkvec2(Flm_inv(MM,p), perm);
513 : }
514 : /* assume Mx = y has a solution, v = Flm_invimage_init(M,p); return x */
515 : static GEN
516 244362 : Flm_invimage_pre(GEN v, GEN y, ulong p)
517 : {
518 244362 : GEN inv = gel(v,1), perm = gel(v,2);
519 244362 : return Flm_Flc_mul(inv, vecsmallpermute(y, perm), p);
520 : }
521 :
522 : GEN
523 5747 : algradical(GEN al)
524 : {
525 5747 : pari_sp av = avma;
526 : GEN I, x, traces, K, MT, P, mt;
527 : long l,i,ni, n;
528 : ulong modu, expo, p;
529 5747 : checkalg(al);
530 5747 : if (alg_type(al) != al_TABLE) return gen_0;
531 5656 : P = alg_get_char(al);
532 5656 : mt = alg_get_multable(al);
533 5656 : n = alg_get_absdim(al);
534 5656 : dbg_printf(1)("algradical: char=%Ps, dim=%d\n", P, n);
535 5656 : traces = algtracematrix(al);
536 5656 : if (!signe(P))
537 : {
538 518 : dbg_printf(2)(" char 0, computing kernel...\n");
539 518 : K = ker(traces);
540 518 : dbg_printf(2)(" ...done.\n");
541 518 : ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
542 70 : return gerepileupto(av, K);
543 : }
544 5138 : dbg_printf(2)(" char>0, computing kernel...\n");
545 5138 : K = FpM_ker(traces, P);
546 5138 : dbg_printf(2)(" ...done.\n");
547 5138 : ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
548 2999 : if (abscmpiu(P,n)>0) return gerepileupto(av, K);
549 :
550 : /* tough case, p <= n. Ronyai's algorithm */
551 2382 : p = P[2]; l = 1;
552 2382 : expo = p; modu = p*p;
553 2382 : dbg_printf(2)(" char>0, hard case.\n");
554 4810 : while (modu<=(ulong)n) { l++; modu *= p; }
555 2382 : MT = ZMV_to_FlmV(mt, modu);
556 2382 : I = ZM_to_Flm(K,p); /* I_0 */
557 6436 : for (i=1; i<=l; i++) {/*compute I_i, expo = p^i, modu = p^(l+1) > n*/
558 : long j, lig,col;
559 4383 : GEN v = cgetg(ni+1, t_VECSMALL);
560 4383 : GEN invI = Flm_invimage_init(I, p);
561 4383 : dbg_printf(2)(" computing I_%d:\n", i);
562 4383 : traces = cgetg(ni+1,t_MAT);
563 29022 : for (j = 1; j <= ni; j++)
564 : {
565 24639 : GEN M = algbasismultable_Flm(MT, gel(I,j), modu);
566 24639 : uel(v,j) = algtracei(M, p,expo,modu);
567 : }
568 29022 : for (col=1; col<=ni; col++)
569 : {
570 24639 : GEN t = cgetg(n+1,t_VECSMALL); gel(traces,col) = t;
571 24639 : x = gel(I, col); /*col-th basis vector of I_{i-1}*/
572 269001 : for (lig=1; lig<=n; lig++)
573 : {
574 244362 : GEN y = _tablemul_ej_Fl(MT,x,lig,p);
575 244362 : GEN z = Flm_invimage_pre(invI, y, p);
576 244362 : uel(t,lig) = Flv_dotproduct(v, z, p);
577 : }
578 : }
579 4383 : dbg_printf(2)(" computing kernel...\n");
580 4383 : K = Flm_ker(traces, p);
581 4383 : dbg_printf(2)(" ...done.\n");
582 4383 : ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
583 4054 : I = Flm_mul(I,K,p);
584 4054 : expo *= p;
585 : }
586 2053 : return Flm_to_ZM(I);
587 : }
588 :
589 : /* compute the multiplication table of the element x, where mt is a
590 : * multiplication table in an arbitrary ring */
591 : static GEN
592 427 : Rgmultable(GEN mt, GEN x)
593 : {
594 427 : long i, l = lg(x);
595 427 : GEN z = NULL;
596 5796 : for (i = 1; i < l; i++)
597 : {
598 5369 : GEN c = gel(x,i);
599 5369 : if (!gequal0(c))
600 : {
601 644 : GEN M = RgM_Rg_mul(gel(mt,i),c);
602 644 : z = z? RgM_add(z, M): M;
603 : }
604 : }
605 427 : return z;
606 : }
607 :
608 : static GEN
609 49 : change_Rgmultable(GEN mt, GEN P, GEN Pi)
610 : {
611 : GEN mt2;
612 49 : long lmt = lg(mt), i;
613 49 : mt2 = cgetg(lmt,t_VEC);
614 476 : for (i=1;i<lmt;i++) {
615 427 : GEN mti = Rgmultable(mt,gel(P,i));
616 427 : gel(mt2,i) = RgM_mul(Pi, RgM_mul(mti,P));
617 : }
618 49 : return mt2;
619 : }
620 :
621 : static GEN
622 21091 : alg_quotient0(GEN al, GEN S, GEN Si, long nq, GEN p, long maps)
623 : {
624 21091 : GEN mt = cgetg(nq+1,t_VEC), P, Pi, d;
625 : long i;
626 21091 : dbg_printf(3)(" alg_quotient0: char=%Ps, dim=%d, dim I=%d\n", p, alg_get_absdim(al), lg(S)-1);
627 85062 : for (i=1; i<=nq; i++) {
628 63971 : GEN mti = algbasismultable(al,gel(S,i));
629 63971 : if (signe(p)) gel(mt,i) = FpM_mul(Si, FpM_mul(mti,S,p), p);
630 5390 : else gel(mt,i) = RgM_mul(Si, RgM_mul(mti,S));
631 : }
632 21091 : if (!signe(p) && !isint1(Q_denom(mt))) {
633 35 : dbg_printf(3)(" bad case: denominator=%Ps\n", Q_denom(mt));
634 35 : P = Q_remove_denom(Si,&d);
635 35 : P = ZM_hnf(P);
636 35 : P = RgM_Rg_div(P,d);
637 35 : Pi = RgM_inv(P);
638 35 : mt = change_Rgmultable(mt,P,Pi);
639 35 : Si = RgM_mul(P,Si);
640 35 : S = RgM_mul(S,Pi);
641 : }
642 21091 : al = algtableinit_i(mt,p);
643 21091 : if (maps) al = mkvec3(al,Si,S); /* algebra, proj, lift */
644 21091 : return al;
645 : }
646 :
647 : /* quotient of an algebra by a nontrivial two-sided ideal */
648 : GEN
649 2799 : alg_quotient(GEN al, GEN I, long maps)
650 : {
651 2799 : pari_sp av = avma;
652 : GEN p, IS, ISi, S, Si;
653 : long n, ni;
654 :
655 2799 : checkalg(al);
656 2799 : if (alg_type(al) != al_TABLE) pari_err_TYPE("alg_quotient [not a table algebra]", al);
657 2792 : p = alg_get_char(al);
658 2792 : n = alg_get_absdim(al);
659 2792 : ni = lg(I)-1;
660 :
661 : /* force first vector of complement to be the identity */
662 2792 : IS = shallowconcat(I, gcoeff(alg_get_multable(al),1,1));
663 2792 : if (signe(p)) {
664 2764 : IS = FpM_suppl(IS,p);
665 2764 : ISi = FpM_inv(IS,p);
666 : }
667 : else {
668 28 : IS = suppl(IS);
669 28 : ISi = RgM_inv(IS);
670 : }
671 2792 : S = vecslice(IS, ni+1, n);
672 2792 : Si = rowslice(ISi, ni+1, n);
673 2792 : return gerepilecopy(av, alg_quotient0(al, S, Si, n-ni, p, maps));
674 : }
675 :
676 : static GEN
677 28491 : image_keep_first(GEN m, GEN p) /* assume first column is nonzero or m==0, no GC */
678 : {
679 : GEN ir, icol, irow, M, c, x;
680 : long i;
681 28491 : if (gequal0(gel(m,1))) return zeromat(nbrows(m),0);
682 :
683 28477 : if (signe(p)) ir = FpM_indexrank(m,p);
684 1498 : else ir = indexrank(m);
685 :
686 28477 : icol = gel(ir,2);
687 28477 : if (icol[1]==1) return extract0(m,icol,NULL);
688 :
689 10 : irow = gel(ir,1);
690 10 : M = extract0(m, irow, icol);
691 10 : c = extract0(gel(m,1), irow, NULL);
692 10 : if (signe(p)) x = FpM_FpC_invimage(M,c,p);
693 0 : else x = inverseimage(M,c); /* TODO modulo a small prime */
694 :
695 10 : for (i=1; i<lg(x); i++)
696 : {
697 10 : if (!gequal0(gel(x,i)))
698 : {
699 10 : icol[i] = 1;
700 10 : vecsmall_sort(icol);
701 10 : return extract0(m,icol,NULL);
702 : }
703 : }
704 :
705 : return NULL; /* LCOV_EXCL_LINE */
706 : }
707 :
708 : /* z[1],...z[nz] central elements such that z[1]A + z[2]A + ... + z[nz]A = A
709 : * is a direct sum. idempotents ==> first basis element is identity */
710 : GEN
711 8653 : alg_centralproj(GEN al, GEN z, long maps)
712 : {
713 8653 : pari_sp av = avma;
714 : GEN S, U, Ui, alq, p;
715 8653 : long i, iu, lz = lg(z), ta;
716 :
717 8653 : checkalg(al);
718 8653 : ta = alg_type(al);
719 8653 : if (ta != al_TABLE) pari_err_TYPE("algcentralproj [not a table algebra]", al);
720 8646 : if (typ(z) != t_VEC) pari_err_TYPE("alcentralproj",z);
721 8639 : p = alg_get_char(al);
722 8639 : dbg_printf(3)(" alg_centralproj: char=%Ps, dim=%d, #z=%d\n", p, alg_get_absdim(al), lz-1);
723 8639 : S = cgetg(lz,t_VEC); /* S[i] = Im(z_i) */
724 26952 : for (i=1; i<lz; i++)
725 : {
726 18313 : GEN mti = algbasismultable(al, gel(z,i));
727 18313 : gel(S,i) = image_keep_first(mti,p);
728 : }
729 8639 : U = shallowconcat1(S); /* U = [Im(z_1)|Im(z_2)|...|Im(z_nz)], n x n */
730 8639 : if (lg(U)-1 < alg_get_absdim(al)) pari_err_TYPE("alcentralproj [z[i]'s not surjective]",z);
731 8632 : if (signe(p)) Ui = FpM_inv(U,p);
732 749 : else Ui = RgM_inv(U);
733 : if (!Ui) pari_err_BUG("alcentralproj"); /*LCOV_EXCL_LINE*/
734 :
735 8632 : alq = cgetg(lz,t_VEC);
736 26931 : for (iu=0,i=1; i<lz; i++)
737 : {
738 18299 : long nq = lg(gel(S,i))-1, ju = iu + nq;
739 18299 : GEN Si = rowslice(Ui, iu+1, ju);
740 18299 : gel(alq, i) = alg_quotient0(al,gel(S,i),Si,nq,p,maps);
741 18299 : iu = ju;
742 : }
743 8632 : return gerepilecopy(av, alq);
744 : }
745 :
746 : /* al is an al_TABLE */
747 : static GEN
748 18988 : algtablecenter(GEN al)
749 : {
750 18988 : pari_sp av = avma;
751 : long n, i, j, k, ic;
752 : GEN C, cij, mt, p;
753 :
754 18988 : n = alg_get_absdim(al);
755 18988 : mt = alg_get_multable(al);
756 18988 : p = alg_get_char(al);
757 18988 : C = cgetg(n+1,t_MAT);
758 92137 : for (j=1; j<=n; j++)
759 : {
760 73149 : gel(C,j) = cgetg(n*n-n+1,t_COL);
761 73149 : ic = 1;
762 594117 : for (i=2; i<=n; i++) {
763 520968 : if (signe(p)) cij = FpC_sub(gmael(mt,i,j),gmael(mt,j,i),p);
764 52318 : else cij = RgC_sub(gmael(mt,i,j),gmael(mt,j,i));
765 7284828 : for (k=1; k<=n; k++, ic++) gcoeff(C,ic,j) = gel(cij, k);
766 : }
767 : }
768 18988 : if (signe(p)) return gerepileupto(av, FpM_ker(C,p));
769 1645 : else return gerepileupto(av, ker(C));
770 : }
771 :
772 : GEN
773 4886 : algcenter(GEN al)
774 : {
775 4886 : checkalg(al);
776 4886 : if (alg_type(al)==al_TABLE) return algtablecenter(al);
777 49 : return alg_get_center(al);
778 : }
779 :
780 : /* Only in positive characteristic. Assumes that al is semisimple. */
781 : GEN
782 4414 : algprimesubalg(GEN al)
783 : {
784 4414 : pari_sp av = avma;
785 : GEN p, Z, F, K;
786 : long nz, i;
787 4414 : checkalg(al);
788 4414 : p = alg_get_char(al);
789 4414 : if (!signe(p)) pari_err_DOMAIN("algprimesubalg","characteristic","=",gen_0,p);
790 :
791 4400 : Z = algtablecenter(al);
792 4400 : nz = lg(Z)-1;
793 4400 : if (nz==1) return Z;
794 :
795 2839 : F = cgetg(nz+1, t_MAT);
796 14734 : for (i=1; i<=nz; i++) {
797 11895 : GEN zi = gel(Z,i);
798 11895 : gel(F,i) = FpC_sub(algpow(al,zi,p),zi,p);
799 : }
800 2839 : K = FpM_ker(F,p);
801 2839 : return gerepileupto(av, FpM_mul(Z,K,p));
802 : }
803 :
804 : static GEN
805 14777 : out_decompose(GEN t, GEN Z, GEN P, GEN p)
806 : {
807 14777 : GEN ali = gel(t,1), projm = gel(t,2), liftm = gel(t,3), pZ;
808 14777 : if (signe(p)) pZ = FpM_image(FpM_mul(projm,Z,p),p);
809 1407 : else pZ = image(RgM_mul(projm,Z));
810 14777 : return mkvec5(ali, projm, liftm, pZ, P);
811 : }
812 : /* fa factorization of charpol(x) */
813 : static GEN
814 7427 : alg_decompose_from_facto(GEN al, GEN x, GEN fa, GEN Z, long mini)
815 : {
816 7427 : long k = lgcols(fa)-1, k2 = mini? 1: k/2;
817 7427 : GEN v1 = rowslice(fa,1,k2);
818 7427 : GEN v2 = rowslice(fa,k2+1,k);
819 7427 : GEN alq, P, Q, p = alg_get_char(al);
820 7427 : dbg_printf(3)(" alg_decompose_from_facto\n");
821 7427 : if (signe(p)) {
822 6706 : P = FpXV_factorback(gel(v1,1), gel(v1,2), p, 0);
823 6706 : Q = FpXV_factorback(gel(v2,1), gel(v2,2), p, 0);
824 6706 : P = FpX_mul(P, FpXQ_inv(P,Q,p), p);
825 : }
826 : else {
827 721 : P = factorback(v1);
828 721 : Q = factorback(v2);
829 721 : P = RgX_mul(P, RgXQ_inv(P,Q));
830 : }
831 7427 : P = algpoleval(al, P, x);
832 7427 : if (signe(p)) Q = FpC_sub(col_ei(lg(P)-1,1), P, p);
833 721 : else Q = gsub(gen_1, P);
834 7427 : if (gequal0(P) || gequal0(Q)) return NULL;
835 7427 : alq = alg_centralproj(al, mkvec2(P,Q), 1);
836 :
837 7427 : P = out_decompose(gel(alq,1), Z, P, p); if (mini) return P;
838 7350 : Q = out_decompose(gel(alq,2), Z, Q, p);
839 7350 : return mkvec2(P,Q);
840 : }
841 :
842 : static GEN
843 11886 : random_pm1(long n)
844 : {
845 11886 : GEN z = cgetg(n+1,t_VECSMALL);
846 : long i;
847 52135 : for (i = 1; i <= n; i++) z[i] = random_bits(5)%3 - 1;
848 11886 : return z;
849 : }
850 :
851 : static GEN alg_decompose(GEN al, GEN Z, long mini, GEN* pt_primelt);
852 : /* Try to split al using x's charpoly. Return gen_0 if simple, NULL if failure.
853 : * And a splitting otherwise
854 : * If pt_primelt!=NULL, compute a primitive element of the center when simple */
855 : static GEN
856 13853 : try_fact(GEN al, GEN x, GEN zx, GEN Z, GEN Zal, long mini, GEN* pt_primelt)
857 : {
858 13853 : GEN z, dec0, dec1, cp = algcharpoly(Zal,zx,0,1), fa, p = alg_get_char(al);
859 : long nfa, e;
860 13853 : dbg_printf(3)(" try_fact: zx=%Ps\n", zx);
861 13853 : if (signe(p)) fa = FpX_factor(cp,p);
862 1281 : else fa = factor(cp);
863 13853 : dbg_printf(3)(" charpoly=%Ps\n", fa);
864 13853 : nfa = nbrows(fa);
865 13853 : if (nfa == 1) {
866 6426 : if (signe(p)) e = gel(fa,2)[1];
867 560 : else e = itos(gcoeff(fa,1,2));
868 6426 : if (e == 1) {
869 3689 : if (pt_primelt != NULL) *pt_primelt = mkvec2(x, cp);
870 3689 : return gen_0;
871 : }
872 2737 : else return NULL;
873 : }
874 7427 : dec0 = alg_decompose_from_facto(al, x, fa, Z, mini);
875 7427 : if (!dec0) return NULL;
876 7427 : if (!mini) return dec0;
877 77 : dec1 = alg_decompose(gel(dec0,1), gel(dec0,4), 1, pt_primelt);
878 77 : z = gel(dec0,5);
879 77 : if (!isintzero(dec1)) {
880 7 : if (signe(p)) z = FpM_FpC_mul(gel(dec0,3),dec1,p);
881 7 : else z = RgM_RgC_mul(gel(dec0,3),dec1);
882 : }
883 77 : return z;
884 : }
885 : static GEN
886 7 : randcol(long n, GEN b)
887 : {
888 7 : GEN N = addiu(shifti(b,1), 1);
889 : long i;
890 7 : GEN res = cgetg(n+1,t_COL);
891 63 : for (i=1; i<=n; i++)
892 : {
893 56 : pari_sp av = avma;
894 56 : gel(res,i) = gerepileuptoint(av, subii(randomi(N),b));
895 : }
896 7 : return res;
897 : }
898 : /* Return gen_0 if already simple. mini: only returns a central idempotent
899 : * corresponding to one simple factor
900 : * if pt_primelt!=NULL, sets it to a primitive element of the center when simple */
901 : static GEN
902 20363 : alg_decompose(GEN al, GEN Z, long mini, GEN* pt_primelt)
903 : {
904 : pari_sp av;
905 : GEN Zal, x, zx, rand, dec0, B, p;
906 20363 : long i, nz = lg(Z)-1;
907 :
908 20363 : if (nz == 1) {
909 9247 : if (pt_primelt != 0) *pt_primelt = mkvec2(zerocol(alg_get_dim(al)), pol_x(0));
910 9247 : return gen_0;
911 : }
912 11116 : p = alg_get_char(al);
913 11116 : dbg_printf(2)(" alg_decompose: char=%Ps, dim=%d, dim Z=%d\n", p, alg_get_absdim(al), nz);
914 11116 : Zal = alg_subalg(al,Z);
915 11116 : Z = gel(Zal,2);
916 11116 : Zal = gel(Zal,1);
917 11116 : av = avma;
918 :
919 11116 : rand = random_pm1(nz);
920 11116 : zx = zc_to_ZC(rand);
921 11116 : if (signe(p)) {
922 10143 : zx = FpC_red(zx,p);
923 10143 : x = ZM_zc_mul(Z,rand);
924 10143 : x = FpC_red(x,p);
925 : }
926 973 : else x = RgM_zc_mul(Z,rand);
927 11116 : dec0 = try_fact(al,x,zx,Z,Zal,mini,pt_primelt);
928 11116 : if (dec0) return dec0;
929 2681 : set_avma(av);
930 :
931 2737 : for (i=2; i<=nz; i++)
932 : {
933 2730 : dec0 = try_fact(al,gel(Z,i),col_ei(nz,i),Z,Zal,mini,pt_primelt);
934 2730 : if (dec0) return dec0;
935 56 : set_avma(av);
936 : }
937 7 : B = int2n(10);
938 : for (;;)
939 0 : {
940 7 : GEN x = randcol(nz,B), zx = ZM_ZC_mul(Z,x);
941 7 : dec0 = try_fact(al,x,zx,Z,Zal,mini,pt_primelt);
942 7 : if (dec0) return dec0;
943 0 : set_avma(av);
944 : }
945 : }
946 :
947 : static GEN
948 16765 : alg_decompose_total(GEN al, GEN Z, long maps)
949 : {
950 : GEN dec, sc, p;
951 : long i;
952 :
953 16765 : dec = alg_decompose(al, Z, 0, NULL);
954 16765 : if (isintzero(dec))
955 : {
956 9415 : if (maps) {
957 6783 : long n = alg_get_absdim(al);
958 6783 : al = mkvec3(al, matid(n), matid(n));
959 : }
960 9415 : return mkvec(al);
961 : }
962 7350 : p = alg_get_char(al); if (!signe(p)) p = NULL;
963 7350 : sc = cgetg(lg(dec), t_VEC);
964 22050 : for (i=1; i<lg(sc); i++) {
965 14700 : GEN D = gel(dec,i), a = gel(D,1), Za = gel(D,4);
966 14700 : GEN S = alg_decompose_total(a, Za, maps);
967 14700 : gel(sc,i) = S;
968 14700 : if (maps)
969 : {
970 10444 : GEN projm = gel(D,2), liftm = gel(D,3);
971 10444 : long j, lS = lg(S);
972 28335 : for (j=1; j<lS; j++)
973 : {
974 17891 : GEN Sj = gel(S,j), p2 = gel(Sj,2), l2 = gel(Sj,3);
975 17891 : if (p) p2 = FpM_mul(p2, projm, p);
976 49 : else p2 = RgM_mul(p2, projm);
977 17891 : if (p) l2 = FpM_mul(liftm, l2, p);
978 49 : else l2 = RgM_mul(liftm, l2);
979 17891 : gel(Sj,2) = p2;
980 17891 : gel(Sj,3) = l2;
981 : }
982 : }
983 : }
984 7350 : return shallowconcat1(sc);
985 : }
986 :
987 : static GEN
988 11172 : alg_subalg(GEN al, GEN basis)
989 : {
990 11172 : GEN invbasis, mt, p = alg_get_char(al);
991 11172 : long i, j, n = lg(basis)-1;
992 :
993 11172 : if (!signe(p)) p = NULL;
994 11172 : basis = shallowmatconcat(mkvec2(col_ei(n,1), basis));
995 11172 : if (p)
996 : {
997 10178 : basis = image_keep_first(basis,p);
998 10178 : invbasis = FpM_inv(basis,p);
999 : }
1000 : else
1001 : { /* FIXME use an integral variant of image_keep_first */
1002 994 : basis = QM_ImQ_hnf(basis);
1003 994 : invbasis = RgM_inv(basis);
1004 : }
1005 11172 : mt = cgetg(n+1,t_VEC);
1006 11172 : gel(mt,1) = matid(n);
1007 37568 : for (i = 2; i <= n; i++)
1008 : {
1009 26396 : GEN mtx = cgetg(n+1,t_MAT), x = gel(basis,i);
1010 26396 : gel(mtx,1) = col_ei(n,i);
1011 167652 : for (j = 2; j <= n; j++)
1012 : {
1013 141256 : GEN xy = algmul(al, x, gel(basis,j));
1014 141256 : if (p) gel(mtx,j) = FpM_FpC_mul(invbasis, xy, p);
1015 29701 : else gel(mtx,j) = RgM_RgC_mul(invbasis, xy);
1016 : }
1017 26396 : gel(mt,i) = mtx;
1018 : }
1019 11172 : return mkvec2(algtableinit_i(mt,p), basis);
1020 : }
1021 :
1022 : GEN
1023 70 : algsubalg(GEN al, GEN basis)
1024 : {
1025 70 : pari_sp av = avma;
1026 : GEN p;
1027 70 : checkalg(al);
1028 70 : if (alg_type(al) == al_REAL) pari_err_TYPE("algsubalg [real algebra]", al);
1029 63 : if (typ(basis) != t_MAT) pari_err_TYPE("algsubalg",basis);
1030 56 : p = alg_get_char(al);
1031 56 : if (signe(p)) basis = RgM_to_FpM(basis,p);
1032 56 : return gerepilecopy(av, alg_subalg(al,basis));
1033 : }
1034 :
1035 : static int
1036 11921 : cmp_algebra(GEN x, GEN y)
1037 : {
1038 : long d;
1039 11921 : d = gel(x,1)[1] - gel(y,1)[1]; if (d) return d < 0? -1: 1;
1040 10724 : d = gel(x,1)[2] - gel(y,1)[2]; if (d) return d < 0? -1: 1;
1041 10724 : return cmp_universal(gel(x,2), gel(y,2));
1042 : }
1043 :
1044 : GEN
1045 4512 : algsimpledec_ss(GEN al, long maps)
1046 : {
1047 4512 : pari_sp av = avma;
1048 : GEN Z, p, r, res, perm;
1049 : long i, l, n;
1050 4512 : checkalg(al);
1051 4512 : p = alg_get_char(al);
1052 4512 : dbg_printf(1)("algsimpledec_ss: char=%Ps, dim=%d\n", p, alg_get_absdim(al));
1053 4512 : if (signe(p)) Z = algprimesubalg(al);
1054 266 : else if (alg_type(al)!=al_TABLE) Z = gen_0;
1055 245 : else Z = algtablecenter(al);
1056 :
1057 4512 : if (lg(Z) == 2) {/* dim Z = 1 */
1058 2447 : n = alg_get_absdim(al);
1059 2447 : set_avma(av);
1060 2447 : if (!maps) return mkveccopy(al);
1061 2300 : retmkvec(mkvec3(gcopy(al), matid(n), matid(n)));
1062 : }
1063 2065 : res = alg_decompose_total(al, Z, maps);
1064 2065 : l = lg(res); r = cgetg(l, t_VEC);
1065 11480 : for (i = 1; i < l; i++)
1066 : {
1067 9415 : GEN A = maps? gmael(res,i,1): gel(res,i);
1068 9415 : gel(r,i) = mkvec2(mkvecsmall2(alg_get_dim(A), lg(algtablecenter(A))),
1069 : alg_get_multable(A));
1070 : }
1071 2065 : perm = gen_indexsort(r, (void*)cmp_algebra, &cmp_nodata);
1072 2065 : return gerepilecopy(av, vecpermute(res, perm));
1073 : }
1074 :
1075 : GEN
1076 777 : algsimpledec(GEN al, long maps)
1077 : {
1078 777 : pari_sp av = avma;
1079 : int ss;
1080 777 : GEN rad, dec, res, proj=NULL, lift=NULL;
1081 777 : rad = algradical(al);
1082 777 : ss = gequal0(rad);
1083 777 : if (!ss)
1084 : {
1085 42 : al = alg_quotient(al, rad, maps);
1086 42 : if (maps) {
1087 14 : proj = gel(al,2);
1088 14 : lift = gel(al,3);
1089 14 : al = gel(al,1);
1090 : }
1091 : }
1092 777 : dec = algsimpledec_ss(al, maps);
1093 777 : if (!ss && maps) /* update maps */
1094 : {
1095 14 : GEN p = alg_get_char(al);
1096 : long i;
1097 42 : for (i=1; i<lg(dec); i++)
1098 : {
1099 28 : if (signe(p))
1100 : {
1101 14 : gmael(dec,i,2) = FpM_mul(gmael(dec,i,2), proj, p);
1102 14 : gmael(dec,i,3) = FpM_mul(lift, gmael(dec,i,3), p);
1103 : }
1104 : else
1105 : {
1106 14 : gmael(dec,i,2) = RgM_mul(gmael(dec,i,2), proj);
1107 14 : gmael(dec,i,3) = RgM_mul(lift, gmael(dec,i,3));
1108 : }
1109 : }
1110 : }
1111 777 : res = mkvec2(rad, dec);
1112 777 : return gerepilecopy(av,res);
1113 : }
1114 :
1115 : static GEN alg_idempotent(GEN al, long n, long d);
1116 : static GEN
1117 6482 : try_split(GEN al, GEN x, long n, long d)
1118 : {
1119 6482 : GEN cp, p = alg_get_char(al), fa, e, pol, exp, P, Q, U, u, mx, mte, ire;
1120 6482 : long nfa, i, smalldim = alg_get_absdim(al)+1, dim, smalli = 0;
1121 6482 : cp = algcharpoly(al,x,0,1);
1122 6482 : fa = FpX_factor(cp,p);
1123 6482 : nfa = nbrows(fa);
1124 6482 : if (nfa == 1) return NULL;
1125 3052 : pol = gel(fa,1);
1126 3052 : exp = gel(fa,2);
1127 :
1128 : /* charpoly is always a d-th power */
1129 9254 : for (i=1; i<lg(exp); i++) {
1130 6209 : if (exp[i]%d) pari_err(e_MISC, "the algebra must be simple (try_split 1)");
1131 6202 : exp[i] /= d;
1132 : }
1133 3045 : cp = FpXV_factorback(gel(fa,1), gel(fa,2), p, 0);
1134 :
1135 : /* find smallest Fp-dimension of a characteristic space */
1136 9247 : for (i=1; i<lg(pol); i++) {
1137 6202 : dim = degree(gel(pol,i))*exp[i];
1138 6202 : if (dim < smalldim) {
1139 3115 : smalldim = dim;
1140 3115 : smalli = i;
1141 : }
1142 : }
1143 3045 : i = smalli;
1144 3045 : if (smalldim != n) return NULL;
1145 : /* We could also compute e*al*e and try again with this smaller algebra */
1146 : /* Fq-rank 1 = Fp-rank n idempotent: success */
1147 :
1148 : /* construct idempotent */
1149 3031 : mx = algbasismultable(al,x);
1150 3031 : P = gel(pol,i);
1151 3031 : P = FpX_powu(P, exp[i], p);
1152 3031 : Q = FpX_div(cp, P, p);
1153 3031 : e = algpoleval(al, Q, mkvec2(x,mx));
1154 3031 : U = FpXQ_inv(Q, P, p);
1155 3031 : u = algpoleval(al, U, mkvec2(x,mx));
1156 3031 : e = algbasismul(al, e, u);
1157 3031 : mte = algbasisrightmultable(al,e);
1158 3031 : ire = FpM_indexrank(mte,p);
1159 3031 : if (lg(gel(ire,1))-1 != smalldim*d) pari_err(e_MISC, "the algebra must be simple (try_split 2)");
1160 :
1161 3024 : return mkvec3(e,mte,ire);
1162 : }
1163 :
1164 : /*
1165 : * Given a simple algebra al of dimension d^2 over its center of degree n,
1166 : * find an idempotent e in al with rank n (which is minimal).
1167 : */
1168 : static GEN
1169 3038 : alg_idempotent(GEN al, long n, long d)
1170 : {
1171 3038 : pari_sp av = avma;
1172 3038 : long i, N = alg_get_absdim(al);
1173 3038 : GEN e, p = alg_get_char(al), x;
1174 6377 : for(i=2; i<=N; i++) {
1175 6321 : x = col_ei(N,i);
1176 6321 : e = try_split(al, x, n, d);
1177 6307 : if (e) return e;
1178 3339 : set_avma(av);
1179 : }
1180 : for(;;) {
1181 161 : x = random_FpC(N,p);
1182 161 : e = try_split(al, x, n, d);
1183 161 : if (e) return e;
1184 105 : set_avma(av);
1185 : }
1186 : }
1187 :
1188 : static GEN
1189 3857 : try_descend(GEN M, GEN B, GEN p, long m, long n, long d)
1190 : {
1191 3857 : GEN B2 = cgetg(m+1,t_MAT), b;
1192 3857 : long i, j, k=0;
1193 11011 : for (i=1; i<=d; i++)
1194 : {
1195 7154 : k++;
1196 7154 : b = gel(B,i);
1197 7154 : gel(B2,k) = b;
1198 17248 : for (j=1; j<n; j++)
1199 : {
1200 10094 : k++;
1201 10094 : b = FpM_FpC_mul(M,b,p);
1202 10094 : gel(B2,k) = b;
1203 : }
1204 : }
1205 3857 : if (!signe(FpM_det(B2,p))) return NULL;
1206 3437 : return FpM_inv(B2,p);
1207 : }
1208 :
1209 : /* Given an m*m matrix M with irreducible charpoly over F of degree n,
1210 : * let K = F(M), which is a field, and write m=d*n.
1211 : * Compute the d-dimensional K-vector space structure on V=F^m induced by M.
1212 : * Return [B,C] where:
1213 : * - B is m*d matrix over F giving a K-basis b_1,...,b_d of V
1214 : * - C is d*m matrix over F[x] expressing the canonical F-basis of V on the b_i
1215 : * Currently F = Fp TODO extend this. */
1216 : static GEN
1217 3437 : descend_i(GEN M, long n, GEN p)
1218 : {
1219 : GEN B, C;
1220 : long m,d,i;
1221 : pari_sp av;
1222 3437 : m = lg(M)-1;
1223 3437 : d = m/n;
1224 3437 : B = cgetg(d+1,t_MAT);
1225 3437 : av = avma;
1226 :
1227 : /* try a subset of the canonical basis */
1228 9751 : for (i=1; i<=d; i++)
1229 6314 : gel(B,i) = col_ei(m,n*(i-1)+1);
1230 3437 : C = try_descend(M,B,p,m,n,d);
1231 3437 : if (C) return mkvec2(B,C);
1232 385 : set_avma(av);
1233 :
1234 : /* try smallish elements */
1235 1155 : for (i=1; i<=d; i++)
1236 770 : gel(B,i) = FpC_red(zc_to_ZC(random_pm1(m)),p);
1237 385 : C = try_descend(M,B,p,m,n,d);
1238 385 : if (C) return mkvec2(B,C);
1239 35 : set_avma(av);
1240 :
1241 : /* try random elements */
1242 : for (;;)
1243 : {
1244 105 : for (i=1; i<=d; i++)
1245 70 : gel(B,i) = random_FpC(m,p);
1246 35 : C = try_descend(M,B,p,m,n,d);
1247 35 : if (C) return mkvec2(B,C);
1248 0 : set_avma(av);
1249 : }
1250 : }
1251 : static GEN
1252 15568 : RgC_contract(GEN C, long n, long v) /* n>1 */
1253 : {
1254 : GEN C2, P;
1255 : long m, d, i, j;
1256 15568 : m = lg(C)-1;
1257 15568 : d = m/n;
1258 15568 : C2 = cgetg(d+1,t_COL);
1259 43344 : for (i=1; i<=d; i++)
1260 : {
1261 27776 : P = pol_xn(n-1,v);
1262 105728 : for (j=1; j<=n; j++)
1263 77952 : gel(P,j+1) = gel(C,n*(i-1)+j);
1264 27776 : P = normalizepol(P);
1265 27776 : gel(C2,i) = P;
1266 : }
1267 15568 : return C2;
1268 : }
1269 : static GEN
1270 3437 : RgM_contract(GEN A, long n, long v) /* n>1 */
1271 : {
1272 3437 : GEN A2 = cgetg(lg(A),t_MAT);
1273 : long i;
1274 19005 : for (i=1; i<lg(A2); i++)
1275 15568 : gel(A2,i) = RgC_contract(gel(A,i),n,v);
1276 3437 : return A2;
1277 : }
1278 : static GEN
1279 3437 : descend(GEN M, long n, GEN p, long v)
1280 : {
1281 3437 : GEN res = descend_i(M,n,p);
1282 3437 : gel(res,2) = RgM_contract(gel(res,2),n,v);
1283 3437 : return res;
1284 : }
1285 :
1286 : /* isomorphism of Fp-vector spaces M_d(F_p^n) -> (F_p)^(d^2*n) */
1287 : static GEN
1288 29939 : Fq_mat2col(GEN M, long d, long n)
1289 : {
1290 29939 : long N = d*d*n, i, j, k;
1291 29939 : GEN C = cgetg(N+1, t_COL);
1292 90160 : for (i=1; i<=d; i++)
1293 191632 : for (j=1; j<=d; j++)
1294 400526 : for (k=0; k<n; k++)
1295 269115 : gel(C,n*(d*(i-1)+j-1)+k+1) = polcoef_i(gcoeff(M,i,j),k,-1);
1296 29939 : return C;
1297 : }
1298 :
1299 : static GEN
1300 3752 : alg_finite_csa_split(GEN al, long v)
1301 : {
1302 : GEN Z, e, mte, ire, primelt, b, T, M, proje, lifte, extre, p, B, C, mt, mx, map, mapi, T2, ro;
1303 3752 : long n, d, N = alg_get_absdim(al), i;
1304 3752 : p = alg_get_char(al);
1305 : /* compute the center */
1306 3752 : Z = algcenter(al);
1307 : /* TODO option to give the center as input instead of computing it */
1308 3752 : n = lg(Z)-1;
1309 :
1310 : /* compute a minimal rank idempotent e */
1311 3752 : if (n==N) {
1312 707 : d = 1;
1313 707 : e = col_ei(N,1);
1314 707 : mte = matid(N);
1315 707 : ire = mkvec2(identity_perm(n),identity_perm(n));
1316 : }
1317 : else {
1318 3045 : d = usqrt(N/n);
1319 3045 : if (d*d*n != N) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 1)");
1320 3038 : e = alg_idempotent(al,n,d);
1321 3024 : mte = gel(e,2);
1322 3024 : ire = gel(e,3);
1323 3024 : e = gel(e,1);
1324 : }
1325 :
1326 : /* identify the center */
1327 3731 : if (n==1)
1328 : {
1329 287 : T = pol_x(v);
1330 287 : primelt = gen_0;
1331 : }
1332 : else
1333 : {
1334 3444 : b = alg_decompose(al, Z, 1, &primelt);
1335 3444 : if (!gequal0(b)) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 2)");
1336 3437 : T = gel(primelt,2);
1337 3437 : primelt = gel(primelt,1);
1338 3437 : setvarn(T,v);
1339 : }
1340 :
1341 : /* use the ffinit polynomial */
1342 3724 : if (n>1)
1343 : {
1344 3437 : T2 = init_Fq(p,n,v);
1345 3437 : setvarn(T,fetch_var_higher());
1346 3437 : ro = FpXQX_roots(T2,T,p);
1347 3437 : ro = gel(ro,1);
1348 3437 : primelt = algpoleval(al,ro,primelt);
1349 3437 : T = T2;
1350 : }
1351 :
1352 : /* descend al*e to a vector space over the center */
1353 : /* lifte: al*e -> al ; proje: al*e -> al */
1354 3724 : lifte = shallowextract(mte,gel(ire,2));
1355 3724 : extre = shallowmatextract(mte,gel(ire,1),gel(ire,2));
1356 3724 : extre = FpM_inv(extre,p);
1357 3724 : proje = rowpermute(mte,gel(ire,1));
1358 3724 : proje = FpM_mul(extre,proje,p);
1359 3724 : if (n==1)
1360 : {
1361 287 : B = lifte;
1362 287 : C = proje;
1363 : }
1364 : else
1365 : {
1366 3437 : M = algbasismultable(al,primelt);
1367 3437 : M = FpM_mul(M,lifte,p);
1368 3437 : M = FpM_mul(proje,M,p);
1369 3437 : B = descend(M,n,p,v);
1370 3437 : C = gel(B,2);
1371 3437 : B = gel(B,1);
1372 3437 : B = FpM_mul(lifte,B,p);
1373 3437 : C = FqM_mul(C,proje,T,p);
1374 : }
1375 :
1376 : /* compute the isomorphism */
1377 3724 : mt = alg_get_multable(al);
1378 3724 : map = cgetg(N+1,t_VEC);
1379 3724 : M = cgetg(N+1,t_MAT);
1380 33663 : for (i=1; i<=N; i++)
1381 : {
1382 29939 : mx = gel(mt,i);
1383 29939 : mx = FpM_mul(mx,B,p);
1384 29939 : mx = FqM_mul(C,mx,T,p);
1385 29939 : gel(map,i) = mx;
1386 29939 : gel(M,i) = Fq_mat2col(mx,d,n);
1387 : }
1388 3724 : mapi = FpM_inv(M,p);
1389 3724 : if (!mapi) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 3)");
1390 3717 : return mkvec3(T,map,mapi);
1391 : }
1392 :
1393 : GEN
1394 3766 : algsplit(GEN al, long v)
1395 : {
1396 3766 : pari_sp av = avma;
1397 : GEN res, T, map, mapi, ff, p;
1398 : long i,j,k,li,lj;
1399 3766 : checkalg(al);
1400 3759 : p = alg_get_char(al);
1401 3759 : if (gequal0(p))
1402 7 : pari_err_IMPL("splitting a characteristic 0 algebra over its center");
1403 3752 : res = alg_finite_csa_split(al, v);
1404 3717 : T = gel(res,1);
1405 3717 : map = gel(res,2);
1406 3717 : mapi = gel(res,3);
1407 3717 : ff = Tp_to_FF(T,p);
1408 33593 : for (i=1; i<lg(map); i++)
1409 : {
1410 29876 : li = lg(gel(map,i));
1411 89908 : for (j=1; j<li; j++)
1412 : {
1413 60032 : lj = lg(gmael(map,i,j));
1414 190876 : for (k=1; k<lj; k++)
1415 130844 : gmael3(map,i,j,k) = Fq_to_FF(gmael3(map,i,j,k),ff);
1416 : }
1417 : }
1418 :
1419 3717 : return gerepilecopy(av, mkvec2(map,mapi));
1420 : }
1421 :
1422 : /* multiplication table sanity checks */
1423 : static GEN
1424 38129 : check_mt_noid(GEN mt, GEN p)
1425 : {
1426 : long i, l;
1427 38129 : GEN MT = cgetg_copy(mt, &l);
1428 38129 : if (typ(MT) != t_VEC || l == 1) return NULL;
1429 185525 : for (i = 1; i < l; i++)
1430 : {
1431 147445 : GEN M = gel(mt,i);
1432 147445 : if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
1433 147417 : if (p) M = RgM_to_FpM(M,p);
1434 147417 : gel(MT,i) = M;
1435 : }
1436 38080 : return MT;
1437 : }
1438 : static GEN
1439 37625 : check_mt(GEN mt, GEN p)
1440 : {
1441 : long i;
1442 : GEN MT;
1443 37625 : MT = check_mt_noid(mt, p);
1444 37625 : if (!MT || !ZM_isidentity(gel(MT,1))) return NULL;
1445 144274 : for (i=2; i<lg(MT); i++)
1446 106677 : if (ZC_is_ei(gmael(MT,i,1)) != i) return NULL;
1447 37597 : return MT;
1448 : }
1449 :
1450 : static GEN
1451 168 : check_relmt(GEN nf, GEN mt)
1452 : {
1453 168 : long i, l = lg(mt), j, k;
1454 168 : GEN MT = gcopy(mt), a, b, d;
1455 168 : if (typ(MT) != t_VEC || l == 1) return NULL;
1456 658 : for (i = 1; i < l; i++)
1457 : {
1458 511 : GEN M = gel(MT,i);
1459 511 : if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
1460 2618 : for (k = 1; k < l; k++)
1461 13083 : for (j = 1; j < l; j++)
1462 : {
1463 10976 : a = gcoeff(M,j,k);
1464 10976 : if (typ(a)==t_INT) continue;
1465 1771 : b = algtobasis(nf,a);
1466 1771 : d = Q_denom(b);
1467 1771 : if (!isint1(d))
1468 14 : pari_err_DOMAIN("alg_csa_table", "denominator(mt)", "!=", gen_1, mt);
1469 1757 : gcoeff(M,j,k) = lift(basistoalg(nf,b));
1470 : }
1471 497 : if (i > 1 && RgC_is_ei(gel(M,1)) != i) return NULL; /* i = 1 checked at end */
1472 490 : gel(MT,i) = M;
1473 : }
1474 147 : if (!RgM_isidentity(gel(MT,1))) return NULL;
1475 147 : return MT;
1476 : }
1477 :
1478 : int
1479 511 : algisassociative(GEN mt0, GEN p)
1480 : {
1481 511 : pari_sp av = avma;
1482 : long i, j, k, n;
1483 : GEN M, mt;
1484 :
1485 511 : if (checkalg_i(mt0)) { p = alg_get_char(mt0); mt0 = alg_get_multable(mt0); }
1486 511 : if (typ(p) != t_INT) pari_err_TYPE("algisassociative",p);
1487 504 : mt = check_mt_noid(mt0, isintzero(p)? NULL: p);
1488 504 : if (!mt) pari_err_TYPE("algisassociative (mult. table)", mt0);
1489 469 : if (!ZM_isidentity(gel(mt,1))) return gc_bool(av,0);
1490 455 : n = lg(mt)-1;
1491 455 : M = cgetg(n+1,t_MAT);
1492 3542 : for (j=1; j<=n; j++) gel(M,j) = cgetg(n+1,t_COL);
1493 3542 : for (i=1; i<=n; i++)
1494 : {
1495 3087 : GEN mi = gel(mt,i);
1496 35182 : for (j=1; j<=n; j++) gcoeff(M,i,j) = gel(mi,j); /* ei.ej */
1497 : }
1498 3073 : for (i=2; i<=n; i++) {
1499 2625 : GEN mi = gel(mt,i);
1500 28973 : for (j=2; j<=n; j++) {
1501 368291 : for (k=2; k<=n; k++) {
1502 : GEN x, y;
1503 341943 : if (signe(p)) {
1504 242039 : x = _tablemul_ej_Fp(mt,gcoeff(M,i,j),k,p);
1505 242039 : y = FpM_FpC_mul(mi,gcoeff(M,j,k),p);
1506 : }
1507 : else {
1508 99904 : x = _tablemul_ej(mt,gcoeff(M,i,j),k);
1509 99904 : y = RgM_RgC_mul(mi,gcoeff(M,j,k));
1510 : }
1511 : /* not cmp_universal: must not fail on 0 == Mod(0,2) for instance */
1512 341943 : if (!gequal(x,y)) return gc_bool(av,0);
1513 : }
1514 : }
1515 : }
1516 448 : return gc_bool(av,1);
1517 : }
1518 :
1519 : int
1520 371 : algiscommutative(GEN al) /* assumes e_1 = 1 */
1521 : {
1522 : long i,j,k,N,sp;
1523 : GEN mt,a,b,p;
1524 371 : checkalg(al);
1525 371 : if (alg_type(al) != al_TABLE) return alg_get_degree(al)==1;
1526 308 : N = alg_get_absdim(al);
1527 308 : mt = alg_get_multable(al);
1528 308 : p = alg_get_char(al);
1529 308 : sp = signe(p);
1530 1449 : for (i=2; i<=N; i++)
1531 9464 : for (j=2; j<=N; j++)
1532 85820 : for (k=1; k<=N; k++) {
1533 77553 : a = gcoeff(gel(mt,i),k,j);
1534 77553 : b = gcoeff(gel(mt,j),k,i);
1535 77553 : if (sp) {
1536 73423 : if (cmpii(Fp_red(a,p), Fp_red(b,p))) return 0;
1537 : }
1538 4130 : else if (gcmp(a,b)) return 0;
1539 : }
1540 252 : return 1;
1541 : }
1542 :
1543 : int
1544 371 : algissemisimple(GEN al)
1545 : {
1546 371 : pari_sp av = avma;
1547 : GEN rad;
1548 371 : checkalg(al);
1549 371 : if (alg_type(al) != al_TABLE) return 1;
1550 308 : rad = algradical(al);
1551 308 : set_avma(av);
1552 308 : return gequal0(rad);
1553 : }
1554 :
1555 : /* ss : known to be semisimple */
1556 : int
1557 280 : algissimple(GEN al, long ss)
1558 : {
1559 280 : pari_sp av = avma;
1560 : GEN Z, dec, p;
1561 280 : checkalg(al);
1562 280 : if (alg_type(al) != al_TABLE) return 1;
1563 224 : if (!ss && !algissemisimple(al)) return 0;
1564 :
1565 182 : p = alg_get_char(al);
1566 182 : if (signe(p)) Z = algprimesubalg(al);
1567 91 : else Z = algtablecenter(al);
1568 :
1569 182 : if (lg(Z) == 2) {/* dim Z = 1 */
1570 105 : set_avma(av);
1571 105 : return 1;
1572 : }
1573 77 : dec = alg_decompose(al, Z, 1, NULL);
1574 77 : set_avma(av);
1575 77 : return gequal0(dec);
1576 : }
1577 :
1578 : static long
1579 329 : is_place_emb(GEN nf, GEN pl)
1580 : {
1581 : long r, r1, r2;
1582 329 : if (typ(pl) != t_INT) pari_err_TYPE("is_place_emb", pl);
1583 315 : if (signe(pl)<=0) pari_err_DOMAIN("is_place_emb", "pl", "<=", gen_0, pl);
1584 308 : nf_get_sign(nf,&r1,&r2); r = r1+r2;
1585 308 : if (cmpiu(pl,r)>0) pari_err_DOMAIN("is_place_emb", "pl", ">", utoi(r), pl);
1586 294 : return itou(pl);
1587 : }
1588 :
1589 : static long
1590 294 : alghasse_emb(GEN al, long emb)
1591 : {
1592 294 : GEN nf = alg_get_center(al);
1593 294 : long r1 = nf_get_r1(nf);
1594 294 : return (emb <= r1)? alg_get_hasse_i(al)[emb]: 0;
1595 : }
1596 :
1597 : static long
1598 399 : alghasse_pr(GEN al, GEN pr)
1599 : {
1600 399 : GEN hf = alg_get_hasse_f(al);
1601 399 : long i = tablesearch(gel(hf,1), pr, &cmp_prime_ideal);
1602 399 : return i? gel(hf,2)[i]: 0;
1603 : }
1604 :
1605 : static long
1606 763 : alghasse_0(GEN al, GEN pl)
1607 : {
1608 : long ta;
1609 : GEN pr, nf;
1610 763 : ta = alg_type(al);
1611 763 : if (ta == al_REAL) return algreal_dim(al)!=1;
1612 742 : if (!pl)
1613 7 : pari_err(e_MISC, "must provide a place pl");
1614 735 : if (ta == al_CSA)
1615 7 : pari_err_IMPL("computation of Hasse invariants over table CSA");
1616 728 : if ((pr = get_prid(pl))) return alghasse_pr(al, pr);
1617 329 : nf = alg_get_center(al);
1618 329 : return alghasse_emb(al, is_place_emb(nf, pl));
1619 : }
1620 : GEN
1621 238 : alghasse(GEN al, GEN pl)
1622 : {
1623 : long h;
1624 238 : checkalg(al);
1625 238 : if (alg_type(al) == al_TABLE) pari_err_TYPE("alghasse [use alginit]",al);
1626 231 : h = alghasse_0(al,pl);
1627 182 : return sstoQ(h, alg_get_degree(al));
1628 : }
1629 :
1630 : /* h >= 0, d >= 0 */
1631 : static long
1632 812 : indexfromhasse(long h, long d) { return d/ugcd(h,d); }
1633 :
1634 : long
1635 812 : algindex(GEN al, GEN pl)
1636 : {
1637 : long d, res, i, l, ta;
1638 : GEN hi, hf;
1639 :
1640 812 : checkalg(al);
1641 805 : ta = alg_type(al);
1642 805 : if (ta == al_TABLE) pari_err_TYPE("algindex [use alginit]",al);
1643 798 : if (ta == al_REAL) return algreal_dim(al)==1 ? 1 : 2;
1644 714 : d = alg_get_degree(al);
1645 714 : if (pl) return indexfromhasse(alghasse_0(al,pl), d);
1646 :
1647 : /* else : global index */
1648 182 : res = 1;
1649 182 : hi = alg_get_hasse_i(al); l = lg(hi);
1650 308 : for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hi[i],d));
1651 182 : hf = gel(alg_get_hasse_f(al), 2); l = lg(hf);
1652 336 : for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hf[i],d));
1653 182 : return res;
1654 : }
1655 :
1656 : int
1657 224 : algisdivision(GEN al, GEN pl)
1658 : {
1659 224 : checkalg(al);
1660 224 : if (alg_type(al) == al_TABLE) {
1661 21 : if (!algissimple(al,0)) return 0;
1662 14 : if (algiscommutative(al)) return 1;
1663 7 : pari_err_IMPL("algisdivision for table algebras");
1664 : }
1665 203 : return algindex(al,pl) == alg_get_degree(al);
1666 : }
1667 :
1668 : int
1669 406 : algissplit(GEN al, GEN pl)
1670 : {
1671 406 : checkalg(al);
1672 406 : if (alg_type(al) == al_TABLE) pari_err_TYPE("algissplit [use alginit]", al);
1673 392 : return algindex(al,pl) == 1;
1674 : }
1675 :
1676 : int
1677 203 : algisramified(GEN al, GEN pl) { return !algissplit(al,pl); }
1678 :
1679 : GEN
1680 98 : algramifiedplaces(GEN al)
1681 : {
1682 98 : pari_sp av = avma;
1683 : GEN ram, hf, hi, Lpr;
1684 : long r1, count, i, ta;
1685 98 : checkalg(al);
1686 98 : ta = alg_type(al);
1687 98 : if (ta != al_CSA && ta != al_CYCLIC)
1688 14 : pari_err_TYPE("algramifiedplaces [not a central simple algebra"
1689 : " over a number field]", al);
1690 84 : r1 = nf_get_r1(alg_get_center(al));
1691 84 : hi = alg_get_hasse_i(al);
1692 84 : hf = alg_get_hasse_f(al);
1693 84 : Lpr = gel(hf,1);
1694 84 : hf = gel(hf,2);
1695 84 : ram = cgetg(r1+lg(Lpr), t_VEC);
1696 84 : count = 0;
1697 280 : for (i=1; i<=r1; i++)
1698 196 : if (hi[i]) {
1699 91 : count++;
1700 91 : gel(ram,count) = stoi(i);
1701 : }
1702 286 : for (i=1; i<lg(Lpr); i++)
1703 202 : if (hf[i]) {
1704 77 : count++;
1705 77 : gel(ram,count) = gel(Lpr,i);
1706 : }
1707 84 : setlg(ram, count+1);
1708 84 : return gerepilecopy(av, ram);
1709 : }
1710 :
1711 : /** OPERATIONS ON ELEMENTS operations.c **/
1712 :
1713 : static long
1714 1037796 : alg_model0(GEN al, GEN x)
1715 : {
1716 1037796 : long t, N = alg_get_absdim(al), lx = lg(x), d, n, D, i;
1717 1037796 : if (typ(x) == t_MAT) return al_MATRIX;
1718 991673 : if (typ(x) != t_COL) return al_INVALID;
1719 991610 : if (N == 1) {
1720 2842 : if (lx != 2) return al_INVALID;
1721 2821 : switch(typ(gel(x,1)))
1722 : {
1723 1799 : case t_INT: case t_FRAC: return al_TRIVIAL; /* cannot distinguish basis and alg from size */
1724 1015 : case t_POL: case t_POLMOD: return al_ALGEBRAIC;
1725 7 : default: return al_INVALID;
1726 : }
1727 : }
1728 :
1729 988768 : switch(alg_type(al)) {
1730 557358 : case al_TABLE:
1731 557358 : if (lx != N+1) return al_INVALID;
1732 557337 : return al_BASIS;
1733 345338 : case al_CYCLIC:
1734 345338 : d = alg_get_degree(al);
1735 345338 : if (lx == N+1) return al_BASIS;
1736 93674 : if (lx == d+1) return al_ALGEBRAIC;
1737 14 : return al_INVALID;
1738 86072 : case al_CSA:
1739 86072 : D = alg_get_dim(al);
1740 86072 : n = nf_get_degree(alg_get_center(al));
1741 86072 : if (n == 1) {
1742 1316 : if (lx != D+1) return al_INVALID;
1743 4109 : for (i=1; i<=D; i++) {
1744 3409 : t = typ(gel(x,i));
1745 3409 : if (t == t_POL || t == t_POLMOD) return al_ALGEBRAIC;
1746 : /* TODO t_COL for coefficients in basis form ? */
1747 : }
1748 700 : return al_BASIS;
1749 : }
1750 : else {
1751 84756 : if (lx == N+1) return al_BASIS;
1752 22582 : if (lx == D+1) return al_ALGEBRAIC;
1753 7 : return al_INVALID;
1754 : }
1755 : }
1756 : return al_INVALID; /* LCOV_EXCL_LINE */
1757 : }
1758 :
1759 : static void
1760 1037656 : checkalgx(GEN x, long model)
1761 : {
1762 : long t, i;
1763 1037656 : switch(model) {
1764 871875 : case al_BASIS:
1765 9085260 : for (i=1; i<lg(x); i++) {
1766 8213392 : t = typ(gel(x,i));
1767 8213392 : if (t != t_INT && t != t_FRAC)
1768 7 : pari_err_TYPE("checkalgx", gel(x,i));
1769 : }
1770 871868 : return;
1771 119658 : case al_TRIVIAL:
1772 : case al_ALGEBRAIC:
1773 405601 : for (i=1; i<lg(x); i++) {
1774 285950 : t = typ(gel(x,i));
1775 285950 : if (t != t_INT && t != t_FRAC && t != t_POL && t != t_POLMOD)
1776 : /* TODO t_COL ? */
1777 7 : pari_err_TYPE("checkalgx", gel(x,i));
1778 : }
1779 119651 : return;
1780 : }
1781 : }
1782 :
1783 : long
1784 1037796 : alg_model(GEN al, GEN x)
1785 : {
1786 1037796 : long res = alg_model0(al, x);
1787 1037796 : if (res == al_INVALID) pari_err_TYPE("alg_model", x);
1788 1037656 : checkalgx(x, res); return res;
1789 : }
1790 :
1791 : static long
1792 462630 : H_model0(GEN x)
1793 : {
1794 : long i;
1795 462630 : switch(typ(x))
1796 : {
1797 15218 : case t_INT:
1798 : case t_FRAC:
1799 : case t_REAL:
1800 : case t_COMPLEX:
1801 15218 : return H_SCALAR;
1802 10157 : case t_MAT:
1803 10157 : return H_MATRIX;
1804 437143 : case t_COL:
1805 437143 : if (lg(x)!=5) return H_INVALID;
1806 2185603 : for (i=1; i<=4; i++) if (!is_real_t(typ(gel(x,i)))) return H_INVALID;
1807 437115 : return H_QUATERNION;
1808 112 : default:
1809 112 : return al_INVALID;
1810 : }
1811 : }
1812 :
1813 : static long
1814 462630 : H_model(GEN x)
1815 : {
1816 462630 : long res = H_model0(x);
1817 462630 : if (res == H_INVALID) pari_err_TYPE("H_model", x);
1818 462490 : return res;
1819 : }
1820 :
1821 : static GEN
1822 756 : alC_add_i(GEN al, GEN x, GEN y, long lx)
1823 : {
1824 756 : GEN A = cgetg(lx, t_COL);
1825 : long i;
1826 2296 : for (i=1; i<lx; i++) gel(A,i) = algadd(al, gel(x,i), gel(y,i));
1827 749 : return A;
1828 : }
1829 : static GEN
1830 406 : alM_add(GEN al, GEN x, GEN y)
1831 : {
1832 406 : long lx = lg(x), l, j;
1833 : GEN z;
1834 406 : if (lg(y) != lx) pari_err_DIM("alM_add (rows)");
1835 392 : if (lx == 1) return cgetg(1, t_MAT);
1836 385 : z = cgetg(lx, t_MAT); l = lgcols(x);
1837 385 : if (lgcols(y) != l) pari_err_DIM("alM_add (columns)");
1838 1127 : for (j = 1; j < lx; j++) gel(z,j) = alC_add_i(al, gel(x,j), gel(y,j), l);
1839 371 : return z;
1840 : }
1841 : static GEN
1842 17745 : H_add(GEN x, GEN y)
1843 : {
1844 17745 : long tx = H_model(x), ty = H_model(y);
1845 17724 : if ((tx==H_MATRIX) ^ (ty==H_MATRIX)) pari_err_TYPE2("H_add", x, y);
1846 17710 : if (tx>ty) { swap(x,y); lswap(tx,ty); }
1847 17710 : switch (tx)
1848 : {
1849 105 : case H_MATRIX: /* both H_MATRIX */ return alM_add(NULL, x, y);
1850 16681 : case H_QUATERNION: /* both H_QUATERNION */ return gadd(x,y);
1851 924 : case H_SCALAR:
1852 924 : if (ty == H_SCALAR) return gadd(x,y);
1853 : else /* ty == H_QUATERNION */
1854 : {
1855 217 : pari_sp av = avma;
1856 217 : GEN res = gcopy(y), im;
1857 217 : gel(res,1) = gadd(gel(res,1), real_i(x));
1858 217 : im = imag_i(x);
1859 217 : if (im != gen_0) gel(res,2) = gadd(gel(res,2), im);
1860 217 : return gerepileupto(av, res);
1861 : }
1862 : }
1863 : return NULL; /*LCOV_EXCL_LINE*/
1864 : }
1865 : GEN
1866 54845 : algadd(GEN al, GEN x, GEN y)
1867 : {
1868 54845 : pari_sp av = avma;
1869 : long tx, ty;
1870 : GEN p;
1871 54845 : checkalg(al);
1872 54845 : if (alg_type(al)==al_REAL) return H_add(x,y);
1873 37100 : tx = alg_model(al,x);
1874 37093 : ty = alg_model(al,y);
1875 37093 : p = alg_get_char(al);
1876 37093 : if (signe(p)) return FpC_add(x,y,p);
1877 36960 : if (tx==ty) {
1878 36078 : if (tx!=al_MATRIX) return gadd(x,y);
1879 301 : return gerepilecopy(av, alM_add(al,x,y));
1880 : }
1881 882 : if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
1882 882 : if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
1883 882 : return gerepileupto(av, gadd(x,y));
1884 : }
1885 :
1886 : static GEN
1887 98 : H_neg(GEN x)
1888 : {
1889 98 : (void)H_model(x);
1890 70 : return gneg(x);
1891 : }
1892 :
1893 : GEN
1894 245 : algneg(GEN al, GEN x)
1895 : {
1896 245 : checkalg(al);
1897 245 : if (alg_type(al)==al_REAL) return H_neg(x);
1898 147 : (void)alg_model(al,x);
1899 140 : return gneg(x);
1900 : }
1901 :
1902 : static GEN
1903 210 : alC_sub_i(GEN al, GEN x, GEN y, long lx)
1904 : {
1905 : long i;
1906 210 : GEN A = cgetg(lx, t_COL);
1907 630 : for (i=1; i<lx; i++) gel(A,i) = algsub(al, gel(x,i), gel(y,i));
1908 210 : return A;
1909 : }
1910 : static GEN
1911 126 : alM_sub(GEN al, GEN x, GEN y)
1912 : {
1913 126 : long lx = lg(x), l, j;
1914 : GEN z;
1915 126 : if (lg(y) != lx) pari_err_DIM("alM_sub (rows)");
1916 119 : if (lx == 1) return cgetg(1, t_MAT);
1917 112 : z = cgetg(lx, t_MAT); l = lgcols(x);
1918 112 : if (lgcols(y) != l) pari_err_DIM("alM_sub (columns)");
1919 315 : for (j = 1; j < lx; j++) gel(z,j) = alC_sub_i(al, gel(x,j), gel(y,j), l);
1920 105 : return z;
1921 : }
1922 : GEN
1923 1120 : algsub(GEN al, GEN x, GEN y)
1924 : {
1925 : long tx, ty;
1926 1120 : pari_sp av = avma;
1927 : GEN p;
1928 1120 : checkalg(al);
1929 1120 : if (alg_type(al)==al_REAL) return gerepileupto(av, algadd(NULL,x,gneg(y)));
1930 966 : tx = alg_model(al,x);
1931 959 : ty = alg_model(al,y);
1932 959 : p = alg_get_char(al);
1933 959 : if (signe(p)) return FpC_sub(x,y,p);
1934 868 : if (tx==ty) {
1935 546 : if (tx != al_MATRIX) return gsub(x,y);
1936 126 : return gerepilecopy(av, alM_sub(al,x,y));
1937 : }
1938 322 : if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
1939 322 : if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
1940 322 : return gerepileupto(av, gsub(x,y));
1941 : }
1942 :
1943 : static GEN
1944 1659 : algalgmul_cyc(GEN al, GEN x, GEN y)
1945 : {
1946 1659 : pari_sp av = avma;
1947 1659 : long n = alg_get_degree(al), i, k;
1948 : GEN xalg, yalg, res, rnf, auts, sum, b, prod, autx;
1949 1659 : rnf = alg_get_splittingfield(al);
1950 1659 : auts = alg_get_auts(al);
1951 1659 : b = alg_get_b(al);
1952 :
1953 1659 : xalg = cgetg(n+1, t_COL);
1954 4935 : for (i=0; i<n; i++)
1955 3276 : gel(xalg,i+1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
1956 :
1957 1659 : yalg = cgetg(n+1, t_COL);
1958 4935 : for (i=0; i<n; i++) gel(yalg,i+1) = rnfbasistoalg(rnf,gel(y,i+1));
1959 :
1960 1659 : res = cgetg(n+1,t_COL);
1961 4935 : for (k=0; k<n; k++) {
1962 3276 : gel(res,k+1) = gmul(gel(xalg,k+1),gel(yalg,1));
1963 5166 : for (i=1; i<=k; i++) {
1964 1890 : autx = poleval(gel(xalg,k-i+1),gel(auts,i));
1965 1890 : prod = gmul(autx,gel(yalg,i+1));
1966 1890 : gel(res,k+1) = gadd(gel(res,k+1), prod);
1967 : }
1968 :
1969 3276 : sum = gen_0;
1970 5166 : for (; i<n; i++) {
1971 1890 : autx = poleval(gel(xalg,k+n-i+1),gel(auts,i));
1972 1890 : prod = gmul(autx,gel(yalg,i+1));
1973 1890 : sum = gadd(sum,prod);
1974 : }
1975 3276 : sum = gmul(b,sum);
1976 :
1977 3276 : gel(res,k+1) = gadd(gel(res,k+1),sum);
1978 : }
1979 :
1980 1659 : return gerepilecopy(av, res);
1981 : }
1982 :
1983 : static GEN
1984 205394 : _tablemul(GEN mt, GEN x, GEN y)
1985 : {
1986 205394 : pari_sp av = avma;
1987 205394 : long D = lg(mt)-1, i;
1988 205394 : GEN res = NULL;
1989 1930922 : for (i=1; i<=D; i++) {
1990 1725528 : GEN c = gel(x,i);
1991 1725528 : if (!gequal0(c)) {
1992 990444 : GEN My = RgM_RgC_mul(gel(mt,i),y);
1993 990444 : GEN t = RgC_Rg_mul(My,c);
1994 990444 : res = res? RgC_add(res,t): t;
1995 : }
1996 : }
1997 205394 : if (!res) { set_avma(av); return zerocol(D); }
1998 204491 : return gerepileupto(av, res);
1999 : }
2000 :
2001 : static GEN
2002 192758 : _tablemul_Fp(GEN mt, GEN x, GEN y, GEN p)
2003 : {
2004 192758 : pari_sp av = avma;
2005 192758 : long D = lg(mt)-1, i;
2006 192758 : GEN res = NULL;
2007 2254758 : for (i=1; i<=D; i++) {
2008 2062000 : GEN c = gel(x,i);
2009 2062000 : if (signe(c)) {
2010 329401 : GEN My = FpM_FpC_mul(gel(mt,i),y,p);
2011 329401 : GEN t = FpC_Fp_mul(My,c,p);
2012 329401 : res = res? FpC_add(res,t,p): t;
2013 : }
2014 : }
2015 192758 : if (!res) { set_avma(av); return zerocol(D); }
2016 192219 : return gerepileupto(av, res);
2017 : }
2018 :
2019 : /* x*ej */
2020 : static GEN
2021 99904 : _tablemul_ej(GEN mt, GEN x, long j)
2022 : {
2023 99904 : pari_sp av = avma;
2024 99904 : long D = lg(mt)-1, i;
2025 99904 : GEN res = NULL;
2026 1563793 : for (i=1; i<=D; i++) {
2027 1463889 : GEN c = gel(x,i);
2028 1463889 : if (!gequal0(c)) {
2029 114415 : GEN My = gel(gel(mt,i),j);
2030 114415 : GEN t = RgC_Rg_mul(My,c);
2031 114415 : res = res? RgC_add(res,t): t;
2032 : }
2033 : }
2034 99904 : if (!res) { set_avma(av); return zerocol(D); }
2035 99764 : return gerepileupto(av, res);
2036 : }
2037 : static GEN
2038 242039 : _tablemul_ej_Fp(GEN mt, GEN x, long j, GEN p)
2039 : {
2040 242039 : pari_sp av = avma;
2041 242039 : long D = lg(mt)-1, i;
2042 242039 : GEN res = NULL;
2043 4364787 : for (i=1; i<=D; i++) {
2044 4122748 : GEN c = gel(x,i);
2045 4122748 : if (!gequal0(c)) {
2046 289954 : GEN My = gel(gel(mt,i),j);
2047 289954 : GEN t = FpC_Fp_mul(My,c,p);
2048 289954 : res = res? FpC_add(res,t,p): t;
2049 : }
2050 : }
2051 242039 : if (!res) { set_avma(av); return zerocol(D); }
2052 241927 : return gerepileupto(av, res);
2053 : }
2054 :
2055 : static GEN
2056 244362 : _tablemul_ej_Fl(GEN mt, GEN x, long j, ulong p)
2057 : {
2058 244362 : pari_sp av = avma;
2059 244362 : long D = lg(mt)-1, i;
2060 244362 : GEN res = NULL;
2061 3944134 : for (i=1; i<=D; i++) {
2062 3699772 : ulong c = x[i];
2063 3699772 : if (c) {
2064 384594 : GEN My = gel(gel(mt,i),j);
2065 384594 : GEN t = Flv_Fl_mul(My,c, p);
2066 384594 : res = res? Flv_add(res,t, p): t;
2067 : }
2068 : }
2069 244362 : if (!res) { set_avma(av); return zero_Flv(D); }
2070 244362 : return gerepileupto(av, res);
2071 : }
2072 :
2073 : static GEN
2074 686 : algalgmul_csa(GEN al, GEN x, GEN y)
2075 : {
2076 686 : GEN z, nf = alg_get_center(al);
2077 : long i;
2078 686 : z = _tablemul(alg_get_relmultable(al), x, y);
2079 2485 : for (i=1; i<lg(z); i++)
2080 1799 : gel(z,i) = basistoalg(nf,gel(z,i));
2081 686 : return z;
2082 : }
2083 :
2084 : /* assumes x and y in algebraic form */
2085 : static GEN
2086 2345 : algalgmul(GEN al, GEN x, GEN y)
2087 : {
2088 2345 : switch(alg_type(al))
2089 : {
2090 1659 : case al_CYCLIC: return algalgmul_cyc(al, x, y);
2091 686 : case al_CSA: return algalgmul_csa(al, x, y);
2092 : }
2093 : return NULL; /*LCOV_EXCL_LINE*/
2094 : }
2095 :
2096 : static GEN
2097 397466 : algbasismul(GEN al, GEN x, GEN y)
2098 : {
2099 397466 : GEN mt = alg_get_multable(al), p = alg_get_char(al);
2100 397466 : if (signe(p)) return _tablemul_Fp(mt, x, y, p);
2101 204708 : return _tablemul(mt, x, y);
2102 : }
2103 :
2104 : /* x[i,]*y. Assume lg(x) > 1 and 0 < i < lgcols(x) */
2105 : static GEN
2106 119651 : alMrow_alC_mul_i(GEN al, GEN x, GEN y, long i, long lx)
2107 : {
2108 119651 : pari_sp av = avma;
2109 119651 : GEN c = algmul(al,gcoeff(x,i,1),gel(y,1)), ZERO;
2110 : long k;
2111 119651 : ZERO = zerocol(alg_get_absdim(al));
2112 273308 : for (k = 2; k < lx; k++)
2113 : {
2114 153657 : GEN t = algmul(al, gcoeff(x,i,k), gel(y,k));
2115 153657 : if (!gequal(t,ZERO)) c = algadd(al, c, t);
2116 : }
2117 119651 : return gerepilecopy(av, c);
2118 : }
2119 : /* return x * y, 1 < lx = lg(x), l = lgcols(x) */
2120 : static GEN
2121 54502 : alM_alC_mul_i(GEN al, GEN x, GEN y, long lx, long l)
2122 : {
2123 54502 : GEN z = cgetg(l,t_COL);
2124 : long i;
2125 174153 : for (i=1; i<l; i++) gel(z,i) = alMrow_alC_mul_i(al,x,y,i,lx);
2126 54502 : return z;
2127 : }
2128 : static GEN
2129 25627 : alM_mul(GEN al, GEN x, GEN y)
2130 : {
2131 25627 : long j, l, lx=lg(x), ly=lg(y);
2132 : GEN z;
2133 25627 : if (ly==1) return cgetg(1,t_MAT);
2134 25529 : if (lx != lgcols(y)) pari_err_DIM("alM_mul");
2135 25508 : if (lx==1) return zeromat(0, ly-1);
2136 25501 : l = lgcols(x); z = cgetg(ly,t_MAT);
2137 80003 : for (j=1; j<ly; j++) gel(z,j) = alM_alC_mul_i(al,x,gel(y,j),lx,l);
2138 25501 : return z;
2139 : }
2140 :
2141 : static void
2142 205583 : H_compo(GEN x, GEN* a, GEN* b, GEN* c, GEN* d)
2143 : {
2144 205583 : switch(H_model(x))
2145 : {
2146 5173 : case H_SCALAR:
2147 5173 : *a = real_i(x);
2148 5173 : *b = imag_i(x);
2149 5173 : *c = gen_0;
2150 5173 : *d = gen_0;
2151 5173 : return;
2152 200410 : case H_QUATERNION:
2153 200410 : *a = gel(x,1);
2154 200410 : *b = gel(x,2);
2155 200410 : *c = gel(x,3);
2156 200410 : *d = gel(x,4);
2157 200410 : return;
2158 : default: *a = *b = *c = *d = NULL; return; /*LCOV_EXCL_LINE*/
2159 : }
2160 : }
2161 : static GEN
2162 108101 : H_mul(GEN x, GEN y)
2163 : {
2164 108101 : pari_sp av = avma;
2165 : GEN a,b,c,d,u,v,w,z;
2166 108101 : long tx = H_model(x), ty = H_model(y);
2167 108087 : if ((tx==H_MATRIX) ^ (ty==H_MATRIX)) pari_err_TYPE2("H_mul", x, y);
2168 108080 : if (tx == H_MATRIX) /* both H_MATRIX */ return alM_mul(NULL, x, y);
2169 103789 : if (tx == H_SCALAR && ty == H_SCALAR) return gmul(x,y);
2170 102592 : H_compo(x,&a,&b,&c,&d);
2171 102592 : H_compo(y,&u,&v,&w,&z);
2172 102592 : return gerepilecopy(av,mkcol4(
2173 : gsub(gmul(a,u), gadd(gadd(gmul(b,v),gmul(c,w)),gmul(d,z))),
2174 : gsub(gadd(gmul(a,v),gadd(gmul(b,u),gmul(c,z))), gmul(d,w)),
2175 : gsub(gadd(gmul(a,w),gadd(gmul(c,u),gmul(d,v))), gmul(b,z)),
2176 : gsub(gadd(gmul(a,z),gadd(gmul(b,w),gmul(d,u))), gmul(c,v))
2177 : ));
2178 : }
2179 :
2180 : GEN
2181 475676 : algmul(GEN al, GEN x, GEN y)
2182 : {
2183 475676 : pari_sp av = avma;
2184 : long tx, ty;
2185 475676 : checkalg(al);
2186 475676 : if (alg_type(al)==al_REAL) return H_mul(x,y);
2187 367827 : tx = alg_model(al,x);
2188 367813 : ty = alg_model(al,y);
2189 367813 : if (tx==al_MATRIX) {
2190 20832 : if (ty==al_MATRIX) return alM_mul(al,x,y);
2191 7 : pari_err_TYPE("algmul", y);
2192 : }
2193 346981 : if (signe(alg_get_char(al))) return algbasismul(al,x,y);
2194 205135 : if (tx==al_TRIVIAL) retmkcol(gmul(gel(x,1),gel(y,1)));
2195 205030 : if (tx==al_ALGEBRAIC && ty==al_ALGEBRAIC) return algalgmul(al,x,y);
2196 203504 : if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
2197 203504 : if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
2198 203504 : return gerepileupto(av,algbasismul(al,x,y));
2199 : }
2200 :
2201 : static GEN
2202 329 : H_sqr(GEN x)
2203 : {
2204 329 : pari_sp av = avma;
2205 329 : long tx = H_model(x);
2206 : GEN a,b,c,d;
2207 308 : if (tx == H_SCALAR) return gsqr(x);
2208 224 : if (tx == H_MATRIX) return H_mul(x,x);
2209 119 : H_compo(x,&a,&b,&c,&d);
2210 119 : return gerepilecopy(av, mkcol4(
2211 : gsub(gsqr(a), gadd(gsqr(b),gadd(gsqr(c),gsqr(d)))),
2212 : gshift(gmul(a,b),1),
2213 : gshift(gmul(a,c),1),
2214 : gshift(gmul(a,d),1)
2215 : ));
2216 : }
2217 :
2218 : GEN
2219 51031 : algsqr(GEN al, GEN x)
2220 : {
2221 51031 : pari_sp av = avma;
2222 : long tx;
2223 51031 : checkalg(al);
2224 50996 : if (alg_type(al)==al_REAL) return H_sqr(x);
2225 50667 : tx = alg_model(al,x);
2226 50597 : if (tx==al_MATRIX) return gerepilecopy(av,alM_mul(al,x,x));
2227 50086 : if (signe(alg_get_char(al))) return algbasismul(al,x,x);
2228 2205 : if (tx==al_TRIVIAL) retmkcol(gsqr(gel(x,1)));
2229 2023 : if (tx==al_ALGEBRAIC) return algalgmul(al,x,x);
2230 1204 : return gerepileupto(av,algbasismul(al,x,x));
2231 : }
2232 :
2233 : static GEN
2234 8946 : algmtK2Z_cyc(GEN al, GEN m)
2235 : {
2236 8946 : pari_sp av = avma;
2237 8946 : GEN nf = alg_get_abssplitting(al), res, mt, rnf = alg_get_splittingfield(al), c, dc;
2238 8946 : long n = alg_get_degree(al), N = nf_get_degree(nf), Nn, i, j, i1, j1;
2239 8946 : Nn = N*n;
2240 8946 : res = zeromatcopy(Nn,Nn);
2241 40936 : for (i=0; i<n; i++)
2242 192808 : for (j=0; j<n; j++) {
2243 160818 : c = gcoeff(m,i+1,j+1);
2244 160818 : if (!gequal0(c)) {
2245 31990 : c = rnfeltreltoabs(rnf,c);
2246 31990 : c = algtobasis(nf,c);
2247 31990 : c = Q_remove_denom(c,&dc);
2248 31990 : mt = zk_multable(nf,c);
2249 31990 : if (dc) mt = ZM_Z_div(mt,dc);
2250 297220 : for (i1=1; i1<=N; i1++)
2251 2923228 : for (j1=1; j1<=N; j1++)
2252 2657998 : gcoeff(res,i*N+i1,j*N+j1) = gcoeff(mt,i1,j1);
2253 : }
2254 : }
2255 8946 : return gerepilecopy(av,res);
2256 : }
2257 :
2258 : static GEN
2259 917 : algmtK2Z_csa(GEN al, GEN m)
2260 : {
2261 917 : pari_sp av = avma;
2262 917 : GEN nf = alg_get_center(al), res, mt, c, dc;
2263 917 : long d2 = alg_get_dim(al), n = nf_get_degree(nf), D, i, j, i1, j1;
2264 917 : D = d2*n;
2265 917 : res = zeromatcopy(D,D);
2266 5362 : for (i=0; i<d2; i++)
2267 30562 : for (j=0; j<d2; j++) {
2268 26117 : c = gcoeff(m,i+1,j+1);
2269 26117 : if (!gequal0(c)) {
2270 3794 : c = algtobasis(nf,c);
2271 3794 : c = Q_remove_denom(c,&dc);
2272 3794 : mt = zk_multable(nf,c);
2273 3794 : if (dc) mt = ZM_Z_div(mt,dc);
2274 12852 : for (i1=1; i1<=n; i1++)
2275 32340 : for (j1=1; j1<=n; j1++)
2276 23282 : gcoeff(res,i*n+i1,j*n+j1) = gcoeff(mt,i1,j1);
2277 : }
2278 : }
2279 917 : return gerepilecopy(av,res);
2280 : }
2281 :
2282 : /* assumes al is a CSA or CYCLIC */
2283 : static GEN
2284 9863 : algmtK2Z(GEN al, GEN m)
2285 : {
2286 9863 : switch(alg_type(al))
2287 : {
2288 8946 : case al_CYCLIC: return algmtK2Z_cyc(al, m);
2289 917 : case al_CSA: return algmtK2Z_csa(al, m);
2290 : }
2291 : return NULL; /*LCOV_EXCL_LINE*/
2292 : }
2293 :
2294 : /* left multiplication table, as a vector space of dimension n over the splitting field (by right multiplication) */
2295 : static GEN
2296 11564 : algalgmultable_cyc(GEN al, GEN x)
2297 : {
2298 11564 : pari_sp av = avma;
2299 11564 : long n = alg_get_degree(al), i, j;
2300 : GEN res, rnf, auts, b, pol;
2301 11564 : rnf = alg_get_splittingfield(al);
2302 11564 : auts = alg_get_auts(al);
2303 11564 : b = alg_get_b(al);
2304 11564 : pol = rnf_get_pol(rnf);
2305 :
2306 11564 : res = zeromatcopy(n,n);
2307 48860 : for (i=0; i<n; i++)
2308 37296 : gcoeff(res,i+1,1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
2309 :
2310 48860 : for (i=0; i<n; i++) {
2311 104706 : for (j=1; j<=i; j++)
2312 67410 : gcoeff(res,i+1,j+1) = gmodulo(poleval(gcoeff(res,i-j+1,1),gel(auts,j)),pol);
2313 104706 : for (; j<n; j++)
2314 67410 : gcoeff(res,i+1,j+1) = gmodulo(gmul(b,poleval(gcoeff(res,n+i-j+1,1),gel(auts,j))), pol);
2315 : }
2316 :
2317 48860 : for (i=0; i<n; i++)
2318 37296 : gcoeff(res,i+1,1) = gmodulo(gcoeff(res,i+1,1),pol);
2319 :
2320 11564 : return gerepilecopy(av, res);
2321 : }
2322 :
2323 : static GEN
2324 1365 : elementmultable(GEN mt, GEN x)
2325 : {
2326 1365 : pari_sp av = avma;
2327 1365 : long D = lg(mt)-1, i;
2328 1365 : GEN z = NULL;
2329 7308 : for (i=1; i<=D; i++)
2330 : {
2331 5943 : GEN c = gel(x,i);
2332 5943 : if (!gequal0(c))
2333 : {
2334 2135 : GEN M = RgM_Rg_mul(gel(mt,i),c);
2335 2135 : z = z? RgM_add(z, M): M;
2336 : }
2337 : }
2338 1365 : if (!z) { set_avma(av); return zeromatcopy(D,D); }
2339 1365 : return gerepileupto(av, z);
2340 : }
2341 : /* mt a t_VEC of Flm modulo m */
2342 : static GEN
2343 24639 : algbasismultable_Flm(GEN mt, GEN x, ulong m)
2344 : {
2345 24639 : pari_sp av = avma;
2346 24639 : long D = lg(gel(mt,1))-1, i;
2347 24639 : GEN z = NULL;
2348 269001 : for (i=1; i<=D; i++)
2349 : {
2350 244362 : ulong c = x[i];
2351 244362 : if (c)
2352 : {
2353 33625 : GEN M = Flm_Fl_mul(gel(mt,i),c, m);
2354 33625 : z = z? Flm_add(z, M, m): M;
2355 : }
2356 : }
2357 24639 : if (!z) { set_avma(av); return zero_Flm(D,D); }
2358 24639 : return gerepileupto(av, z);
2359 : }
2360 : static GEN
2361 226640 : elementabsmultable_Z(GEN mt, GEN x)
2362 : {
2363 226640 : long i, l = lg(x);
2364 226640 : GEN z = NULL;
2365 2410471 : for (i = 1; i < l; i++)
2366 : {
2367 2183831 : GEN c = gel(x,i);
2368 2183831 : if (signe(c))
2369 : {
2370 852014 : GEN M = ZM_Z_mul(gel(mt,i),c);
2371 852014 : z = z? ZM_add(z, M): M;
2372 : }
2373 : }
2374 226640 : return z;
2375 : }
2376 : static GEN
2377 115040 : elementabsmultable(GEN mt, GEN x)
2378 : {
2379 115040 : GEN d, z = elementabsmultable_Z(mt, Q_remove_denom(x,&d));
2380 115040 : return (z && d)? ZM_Z_div(z, d): z;
2381 : }
2382 : static GEN
2383 111600 : elementabsmultable_Fp(GEN mt, GEN x, GEN p)
2384 : {
2385 111600 : GEN z = elementabsmultable_Z(mt, x);
2386 111600 : return z? FpM_red(z, p): z;
2387 : }
2388 : static GEN
2389 226640 : algbasismultable(GEN al, GEN x)
2390 : {
2391 226640 : pari_sp av = avma;
2392 226640 : GEN z, p = alg_get_char(al), mt = alg_get_multable(al);
2393 226640 : z = signe(p)? elementabsmultable_Fp(mt, x, p): elementabsmultable(mt, x);
2394 226640 : if (!z)
2395 : {
2396 710 : long D = lg(mt)-1;
2397 710 : set_avma(av); return zeromat(D,D);
2398 : }
2399 225930 : return gerepileupto(av, z);
2400 : }
2401 :
2402 : static GEN
2403 1365 : algalgmultable_csa(GEN al, GEN x)
2404 : {
2405 1365 : GEN nf = alg_get_center(al), m;
2406 : long i,j;
2407 1365 : m = elementmultable(alg_get_relmultable(al), x);
2408 7308 : for (i=1; i<lg(m); i++)
2409 37758 : for(j=1; j<lg(m); j++)
2410 31815 : gcoeff(m,i,j) = basistoalg(nf,gcoeff(m,i,j));
2411 1365 : return m;
2412 : }
2413 :
2414 : /* assumes x in algebraic form */
2415 : static GEN
2416 12635 : algalgmultable(GEN al, GEN x)
2417 : {
2418 12635 : switch(alg_type(al))
2419 : {
2420 11564 : case al_CYCLIC: return algalgmultable_cyc(al, x);
2421 1071 : case al_CSA: return algalgmultable_csa(al, x);
2422 : }
2423 : return NULL; /*LCOV_EXCL_LINE*/
2424 : }
2425 :
2426 : /* on the natural basis */
2427 : /* assumes x in algebraic form */
2428 : static GEN
2429 9863 : algZmultable(GEN al, GEN x) {
2430 9863 : pari_sp av = avma;
2431 9863 : return gerepileupto(av, algmtK2Z(al,algalgmultable(al,x)));
2432 : }
2433 :
2434 : /* x integral */
2435 : static GEN
2436 36568 : algbasisrightmultable(GEN al, GEN x)
2437 : {
2438 36568 : long N = alg_get_absdim(al), i,j,k;
2439 36568 : GEN res = zeromatcopy(N,N), c, mt = alg_get_multable(al), p = alg_get_char(al);
2440 36568 : if (gequal0(p)) p = NULL;
2441 330925 : for (i=1; i<=N; i++) {
2442 294357 : c = gel(x,i);
2443 294357 : if (!gequal0(c)) {
2444 872263 : for (j=1; j<=N; j++)
2445 7418194 : for(k=1; k<=N; k++) {
2446 6640130 : if (p) gcoeff(res,k,j) = Fp_add(gcoeff(res,k,j), Fp_mul(c, gcoeff(gel(mt,j),k,i), p), p);
2447 5015262 : else gcoeff(res,k,j) = addii(gcoeff(res,k,j), mulii(c, gcoeff(gel(mt,j),k,i)));
2448 : }
2449 : }
2450 : }
2451 36568 : return res;
2452 : }
2453 :
2454 : /* basis for matrices : 1, E_{i,j} for (i,j)!=(1,1) */
2455 : /* index : ijk = ((i-1)*N+j-1)*n + k */
2456 : /* square matrices only, coefficients in basis form, shallow function */
2457 : static GEN
2458 23961 : algmat2basis(GEN al, GEN M)
2459 : {
2460 23961 : long n = alg_get_absdim(al), N = lg(M)-1, i, j, k, ij, ijk;
2461 : GEN res, x;
2462 23961 : res = zerocol(N*N*n);
2463 75131 : for (i=1; i<=N; i++) {
2464 163310 : for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
2465 112140 : x = gcoeff(M,i,j);
2466 819532 : for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
2467 707392 : gel(res, ijk) = gel(x, k);
2468 707392 : if (i>1 && i==j) gel(res, ijk) = gsub(gel(res,ijk), gel(res,k));
2469 : }
2470 : }
2471 : }
2472 :
2473 23961 : return res;
2474 : }
2475 :
2476 : static GEN
2477 294 : algbasis2mat(GEN al, GEN M, long N)
2478 : {
2479 294 : long n = alg_get_absdim(al), i, j, k, ij, ijk;
2480 : GEN res, x;
2481 294 : res = zeromatcopy(N,N);
2482 882 : for (i=1; i<=N; i++)
2483 1764 : for (j=1; j<=N; j++)
2484 1176 : gcoeff(res,i,j) = zerocol(n);
2485 :
2486 882 : for (i=1; i<=N; i++) {
2487 1764 : for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
2488 1176 : x = gcoeff(res,i,j);
2489 9240 : for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
2490 8064 : gel(x,k) = gel(M,ijk);
2491 8064 : if (i>1 && i==j) gel(x,k) = gadd(gel(x,k), gel(M,k));
2492 : }
2493 : }
2494 : }
2495 :
2496 294 : return res;
2497 : }
2498 :
2499 : static GEN
2500 23884 : algmatbasis_ei(GEN al, long ijk, long N)
2501 : {
2502 23884 : long n = alg_get_absdim(al), i, j, k, ij;
2503 : GEN res;
2504 :
2505 23884 : res = zeromatcopy(N,N);
2506 74900 : for (i=1; i<=N; i++)
2507 162848 : for (j=1; j<=N; j++)
2508 111832 : gcoeff(res,i,j) = zerocol(n);
2509 :
2510 23884 : k = ijk%n;
2511 23884 : if (k==0) k=n;
2512 23884 : ij = (ijk-k)/n+1;
2513 :
2514 23884 : if (ij==1) {
2515 16947 : for (i=1; i<=N; i++)
2516 11410 : gcoeff(res,i,i) = col_ei(n,k);
2517 5537 : return res;
2518 : }
2519 :
2520 18347 : j = ij%N;
2521 18347 : if (j==0) j=N;
2522 18347 : i = (ij-j)/N+1;
2523 :
2524 18347 : gcoeff(res,i,j) = col_ei(n,k);
2525 18347 : return res;
2526 : }
2527 :
2528 : /* FIXME lazy implementation! */
2529 : static GEN
2530 910 : algleftmultable_mat(GEN al, GEN M)
2531 : {
2532 910 : long N = lg(M)-1, n = alg_get_absdim(al), D = N*N*n, j;
2533 : GEN res, x, Mx;
2534 910 : if (N == 0) return cgetg(1, t_MAT);
2535 903 : if (N != nbrows(M)) pari_err_DIM("algleftmultable_mat (nonsquare)");
2536 882 : res = cgetg(D+1, t_MAT);
2537 24766 : for (j=1; j<=D; j++) {
2538 23884 : x = algmatbasis_ei(al, j, N);
2539 23884 : Mx = algmul(al, M, x);
2540 23884 : gel(res, j) = algmat2basis(al, Mx);
2541 : }
2542 882 : return res;
2543 : }
2544 :
2545 : /* left multiplication table on integral basis */
2546 : static GEN
2547 6951 : algleftmultable(GEN al, GEN x)
2548 : {
2549 6951 : pari_sp av = avma;
2550 : long tx;
2551 : GEN res;
2552 :
2553 6951 : checkalg(al);
2554 6951 : tx = alg_model(al,x);
2555 6944 : switch(tx) {
2556 98 : case al_TRIVIAL : res = mkmatcopy(mkcol(gel(x,1))); break;
2557 196 : case al_ALGEBRAIC : x = algalgtobasis(al,x);
2558 6328 : case al_BASIS : res = algbasismultable(al,x); break;
2559 518 : case al_MATRIX : res = algleftmultable_mat(al,x); break;
2560 : default : return NULL; /* LCOV_EXCL_LINE */
2561 : }
2562 6937 : return gerepileupto(av,res);
2563 : }
2564 :
2565 : static GEN
2566 4102 : algbasissplittingmatrix_csa(GEN al, GEN x)
2567 : {
2568 4102 : long d = alg_get_degree(al), i, j;
2569 4102 : GEN rnf = alg_get_splittingfield(al), splba = alg_get_splittingbasis(al), splbainv = alg_get_splittingbasisinv(al), M;
2570 4102 : M = algbasismultable(al,x);
2571 4102 : M = RgM_mul(M, splba); /* TODO best order ? big matrix /Q vs small matrix /nf */
2572 4102 : M = RgM_mul(splbainv, M);
2573 12131 : for (i=1; i<=d; i++)
2574 23912 : for (j=1; j<=d; j++)
2575 15883 : gcoeff(M,i,j) = rnfeltabstorel(rnf, gcoeff(M,i,j));
2576 4102 : return M;
2577 : }
2578 :
2579 : static GEN
2580 728 : algmat_tomatrix(GEN al, GEN x) /* abs = 0 */
2581 : {
2582 : GEN res;
2583 : long i,j;
2584 728 : if (lg(x) == 1) return cgetg(1, t_MAT);
2585 700 : res = zeromatcopy(nbrows(x),lg(x)-1);
2586 2212 : for (j=1; j<lg(x); j++)
2587 4879 : for (i=1; i<lgcols(x); i++)
2588 3367 : gcoeff(res,i,j) = algtomatrix(al,gcoeff(x,i,j),0);
2589 700 : return shallowmatconcat(res);
2590 : }
2591 :
2592 : static GEN
2593 42 : R_tomatrix(GEN x)
2594 : {
2595 42 : long t = H_model(x);
2596 42 : if (t == H_QUATERNION) pari_err_TYPE("R_tomatrix", x);
2597 35 : if (t == H_MATRIX) return x;
2598 21 : return mkmat(mkcol(x));
2599 : }
2600 : static GEN
2601 84 : C_tomatrix(GEN z, long abs)
2602 : {
2603 : GEN x,y;
2604 84 : long t = H_model(z), nrows, ncols;
2605 84 : if (t == H_QUATERNION) pari_err_TYPE("C_tomatrix", z);
2606 77 : if (!abs)
2607 : {
2608 14 : if (t == H_MATRIX) return z;
2609 7 : return mkmat(mkcol(z));
2610 : }
2611 63 : if (t == H_MATRIX)
2612 : {
2613 : /* Warning: this is not the same choice of basis as for other algebras */
2614 : GEN res, a, b;
2615 : long i,j;
2616 56 : RgM_dimensions(z,&nrows,&ncols);
2617 56 : res = zeromatcopy(2*nrows,2*ncols);
2618 168 : for (i=1; i<=nrows; i++)
2619 336 : for (j=1; j<=ncols; j++)
2620 : {
2621 224 : a = real_i(gcoeff(z,i,j));
2622 224 : b = imag_i(gcoeff(z,i,j));
2623 224 : gcoeff(res,2*i-1,2*j-1) = a;
2624 224 : gcoeff(res,2*i,2*j) = a;
2625 224 : gcoeff(res,2*i-1,2*j) = gneg(b);
2626 224 : gcoeff(res,2*i,2*j-1) = b;
2627 : }
2628 56 : return res;
2629 : }
2630 7 : x = real_i(z);
2631 7 : y = imag_i(z);
2632 7 : return mkmat22(x,gneg(y),y,x);
2633 : }
2634 : static GEN
2635 2331 : H_tomatrix(GEN x, long abs)
2636 : {
2637 2331 : long tx = H_model(x);
2638 2324 : GEN a = NULL, b =NULL, c = NULL, d = NULL, md = NULL, M = NULL;
2639 2324 : if (abs) {
2640 287 : if (tx == H_MATRIX) return algleftmultable_mat(NULL,x);
2641 154 : switch(tx)
2642 : {
2643 35 : case H_SCALAR:
2644 35 : a = real_i(x);
2645 35 : b = imag_i(x);
2646 35 : c = gen_0;
2647 35 : d = gen_0;
2648 35 : break;
2649 119 : case H_QUATERNION:
2650 119 : a = gel(x,1);
2651 119 : b = gel(x,2);
2652 119 : c = gel(x,3);
2653 119 : d = gel(x,4);
2654 119 : break;
2655 : }
2656 154 : M = scalarmat(a,4);
2657 154 : gcoeff(M,2,1) = gcoeff(M,4,3) = b;
2658 154 : gcoeff(M,1,2) = gcoeff(M,3,4) = gneg(b);
2659 154 : gcoeff(M,3,1) = gcoeff(M,2,4) = c;
2660 154 : gcoeff(M,4,2) = gcoeff(M,1,3) = gneg(c);
2661 154 : gcoeff(M,4,1) = gcoeff(M,3,2) = d;
2662 154 : gcoeff(M,2,3) = gcoeff(M,1,4) = gneg(d);
2663 : }
2664 : else /* abs == 0 */
2665 : {
2666 2037 : if (tx == H_MATRIX) return algmat_tomatrix(NULL,x);
2667 1778 : switch(tx)
2668 : {
2669 273 : case H_SCALAR:
2670 273 : M = mkmat22(
2671 : x, gen_0,
2672 : gen_0, conj_i(x)
2673 : );
2674 273 : break;
2675 1505 : case H_QUATERNION:
2676 1505 : a = gel(x,1);
2677 1505 : b = gel(x,2);
2678 1505 : c = gel(x,3);
2679 1505 : md = gneg(gel(x,4));
2680 1505 : M = mkmat22(
2681 : mkcomplex(a,b), mkcomplex(gneg(c),md),
2682 : mkcomplex(c,md), mkcomplex(a,gneg(b))
2683 : );
2684 : }
2685 1932 : }
2686 1932 : return M;
2687 : }
2688 :
2689 : GEN
2690 9667 : algtomatrix(GEN al, GEN x, long abs)
2691 : {
2692 9667 : pari_sp av = avma;
2693 9667 : GEN res = NULL;
2694 : long ta, tx;
2695 9667 : checkalg(al);
2696 9667 : ta = alg_type(al);
2697 9667 : if (ta==al_REAL)
2698 : {
2699 2268 : switch(alg_get_absdim(al)) {
2700 42 : case 1: res = R_tomatrix(x); break;
2701 84 : case 2: res = C_tomatrix(x,abs); break;
2702 2135 : case 4: res = H_tomatrix(x,abs); break;
2703 7 : default: pari_err_TYPE("algtomatrix [apply alginit]", al);
2704 : }
2705 2240 : return gerepilecopy(av, res);
2706 : }
2707 7399 : if (abs || ta==al_TABLE) return algleftmultable(al,x);
2708 6622 : tx = alg_model(al,x);
2709 6622 : if (tx == al_MATRIX) res = algmat_tomatrix(al,x);
2710 6153 : else switch (alg_type(al))
2711 : {
2712 2051 : case al_CYCLIC:
2713 2051 : if (tx==al_BASIS) x = algbasistoalg(al,x);
2714 2051 : res = algalgmultable(al,x);
2715 2051 : break;
2716 4102 : case al_CSA:
2717 4102 : if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
2718 4102 : res = algbasissplittingmatrix_csa(al,x);
2719 4102 : break;
2720 : default: return NULL; /*LCOV_EXCL_LINE*/
2721 : }
2722 6622 : return gerepilecopy(av,res);
2723 : }
2724 :
2725 : /* x^(-1)*y, NULL if no solution */
2726 : static GEN
2727 112 : C_divl_i(GEN x, GEN y)
2728 : {
2729 112 : long tx = H_model(x), ty = H_model(y);
2730 112 : if (tx != ty) pari_err_TYPE2("C_divl", x, y);
2731 105 : switch (tx) {
2732 42 : case H_SCALAR:
2733 42 : if (gequal0(x)) return gequal0(y) ? gen_0 : NULL;
2734 14 : else return gdiv(y,x);
2735 56 : case H_MATRIX:
2736 56 : if ((lg(x)>1 && lg(x) != lgcols(x)) || (lg(y)>1 && lg(y) != lgcols(y)))
2737 7 : pari_err_DIM("C_divl (nonsquare)");
2738 49 : if (lg(x) != lg(y)) pari_err_DIM("C_divl");
2739 42 : if (lg(y) == 1) return cgetg(1, t_MAT);
2740 42 : return RgM_invimage(x, y);
2741 7 : default: pari_err_TYPE("C_divl", x); return NULL;
2742 : }
2743 : }
2744 : /* H^k -> C^2k */
2745 : static GEN
2746 140 : HC_to_CC(GEN v)
2747 : {
2748 140 : long l = lg(v), i;
2749 140 : GEN w = cgetg(2*l-1, t_COL), a, b, c, d;
2750 420 : for (i=1; i<l; i++)
2751 : {
2752 280 : H_compo(gel(v,i),&a,&b,&c,&d);
2753 280 : gel(w,2*i-1) = mkcomplex(a,b);
2754 280 : gel(w,2*i) = mkcomplex(c,gneg(d));
2755 : }
2756 140 : return w;
2757 : }
2758 : /* C^2k -> H^k */
2759 : static GEN
2760 98 : CC_to_HC(GEN w)
2761 : {
2762 98 : long l = lg(w), i, lv = (l+1)/2;
2763 98 : GEN v = cgetg(lv, t_COL), ab, cd;
2764 294 : for (i=1; i<lv; i++)
2765 : {
2766 196 : ab = gel(w,2*i-1);
2767 196 : cd = gel(w,2*i);
2768 196 : gel(v,i) = mkcol4(real_i(ab),imag_i(ab),real_i(cd),gneg(imag_i(cd)));
2769 : }
2770 98 : return v;
2771 : }
2772 : /* M_{k,n}(H) -> M_{2k,n}(C) */
2773 : static GEN
2774 210 : HM_to_CM(GEN x) pari_APPLY_same(HC_to_CC(gel(x,i)));
2775 : /* M_{2k,n}(C) -> M_{k,n}(H) */
2776 : static GEN
2777 147 : CM_to_HM(GEN x) pari_APPLY_same(CC_to_HC(gel(x,i)));
2778 : /* x^(-1)*y, NULL if no solution */
2779 : static GEN
2780 203 : H_divl_i(GEN x, GEN y)
2781 : {
2782 203 : pari_sp av = avma;
2783 203 : long tx = H_model(x), ty = H_model(y);
2784 189 : if ((tx==H_MATRIX) ^ (ty==H_MATRIX)) pari_err_TYPE2("H_divl", x, y);
2785 168 : if (tx==H_MATRIX)
2786 : {
2787 : GEN mx, my, mxdivy;
2788 98 : if ((lg(x)>1 && lg(x) != lgcols(x)) || (lg(y)>1 && lg(y) != lgcols(y)))
2789 14 : pari_err_DIM("H_divl (nonsquare)");
2790 84 : if (lg(x) != lg(y)) pari_err_DIM("H_divl");
2791 77 : if (lg(y) == 1) return cgetg(1, t_MAT);
2792 70 : mx = H_tomatrix(x,0);
2793 70 : my = HM_to_CM(y);
2794 70 : mxdivy = RgM_invimage(mx, my);
2795 70 : if (!mxdivy) return gc_NULL(av);
2796 49 : return gerepilecopy(av,CM_to_HM(mxdivy));
2797 : }
2798 70 : if (gequal0(y)) return gen_0;
2799 56 : if (gequal0(x)) return NULL;
2800 42 : return gerepilecopy(av,H_mul(H_inv(x),y));
2801 : }
2802 : /* x^(-1)*y, NULL if no solution */
2803 : static GEN
2804 1715 : algdivl_i(GEN al, GEN x, GEN y, long tx, long ty) {
2805 1715 : pari_sp av = avma;
2806 1715 : GEN res, p = alg_get_char(al), mtx;
2807 1715 : if (tx != ty) {
2808 343 : if (tx==al_ALGEBRAIC) { x = algalgtobasis(al,x); tx=al_BASIS; }
2809 343 : if (ty==al_ALGEBRAIC) { y = algalgtobasis(al,y); ty=al_BASIS; }
2810 : }
2811 1715 : if (ty == al_MATRIX)
2812 : {
2813 77 : if (alg_type(al) != al_TABLE) y = algalgtobasis(al,y);
2814 77 : y = algmat2basis(al,y);
2815 : }
2816 1715 : if (signe(p)) res = FpM_FpC_invimage(algbasismultable(al,x),y,p);
2817 : else
2818 : {
2819 1526 : if (ty==al_ALGEBRAIC) mtx = algalgmultable(al,x);
2820 819 : else mtx = algleftmultable(al,x);
2821 1526 : res = inverseimage(mtx,y);
2822 : }
2823 1715 : if (!res || lg(res)==1) return gc_NULL(av);
2824 1687 : if (tx == al_MATRIX) {
2825 294 : res = algbasis2mat(al, res, lg(x)-1);
2826 294 : return gerepilecopy(av,res);
2827 : }
2828 1393 : return gerepileupto(av,res);
2829 : }
2830 : static GEN
2831 1001 : algdivl_i2(GEN al, GEN x, GEN y)
2832 : {
2833 : long tx, ty;
2834 1001 : checkalg(al);
2835 1001 : if (alg_type(al)==al_REAL) switch(alg_get_absdim(al)) {
2836 112 : case 1: case 2: return C_divl_i(x,y);
2837 147 : case 4: return H_divl_i(x,y);
2838 : }
2839 742 : tx = alg_model(al,x);
2840 735 : ty = alg_model(al,y);
2841 735 : if (tx == al_MATRIX) {
2842 140 : if (ty != al_MATRIX) pari_err_TYPE2("\\", x, y);
2843 133 : if ((lg(x)>1 && lg(x) != lgcols(x)) || (lg(y)>1 && lg(y) != lgcols(y)))
2844 28 : pari_err_DIM("algdivl (nonsquare)");
2845 105 : if (lg(x) != lg(y)) pari_err_DIM("algdivl");
2846 84 : if (lg(y) == 1) return cgetg(1, t_MAT);
2847 : }
2848 672 : return algdivl_i(al,x,y,tx,ty);
2849 : }
2850 :
2851 875 : GEN algdivl(GEN al, GEN x, GEN y)
2852 : {
2853 : GEN z;
2854 875 : z = algdivl_i2(al,x,y);
2855 728 : if (!z) pari_err_INV("algdivl", x);
2856 714 : return z;
2857 : }
2858 :
2859 : int
2860 126 : algisdivl(GEN al, GEN x, GEN y, GEN* ptz)
2861 : {
2862 126 : pari_sp av = avma;
2863 126 : GEN z = algdivl_i2(al,x,y);
2864 126 : if (!z) return gc_bool(av,0);
2865 84 : if (ptz != NULL) *ptz = z;
2866 84 : return 1;
2867 : }
2868 :
2869 : static GEN
2870 140 : C_inv(GEN x)
2871 : {
2872 140 : switch (H_model(x))
2873 : {
2874 63 : case H_SCALAR: return gequal0(x) ? NULL : ginv(x);
2875 70 : case H_MATRIX: return RgM_inv(x);
2876 7 : default: pari_err_TYPE("alginv_i", x);
2877 : }
2878 : return NULL; /*LCOV_EXCL_LINE*/
2879 : }
2880 : static GEN
2881 259 : H_inv(GEN x)
2882 : {
2883 259 : pari_sp av = avma;
2884 : GEN nm, xi;
2885 : long i;
2886 259 : switch (H_model(x))
2887 : {
2888 28 : case H_SCALAR:
2889 28 : if (gequal0(x)) return NULL;
2890 14 : return ginv(x);
2891 161 : case H_QUATERNION:
2892 161 : if (gequal0(x)) return NULL;
2893 154 : nm = H_norm(x, 0);
2894 154 : xi = gdiv(x,nm);
2895 616 : for(i=2; i<=4; i++) gel(xi,i) = gneg(gel(xi,i));
2896 154 : return gerepilecopy(av,xi);
2897 63 : case H_MATRIX:
2898 63 : if (lg(x)==1) return cgetg(1,t_MAT);
2899 56 : return H_divl_i(x, matid(lg(x)-1));
2900 : }
2901 : return NULL; /*LCOV_EXCL_LINE*/
2902 : }
2903 : static GEN
2904 1512 : alginv_i(GEN al, GEN x)
2905 : {
2906 1512 : pari_sp av = avma;
2907 1512 : GEN res = NULL, p = alg_get_char(al);
2908 : long tx, n, ta;
2909 1512 : ta = alg_type(al);
2910 1512 : if (ta==al_REAL) switch(alg_get_absdim(al)) {
2911 140 : case 1: case 2: return C_inv(x);
2912 217 : case 4: return H_inv(x);
2913 7 : default: pari_err_TYPE("alginv_i [apply alginit]", al);
2914 : }
2915 1148 : tx = alg_model(al,x);
2916 1127 : switch(tx) {
2917 63 : case al_TRIVIAL :
2918 63 : if (signe(p)) { res = mkcol(Fp_inv(gel(x,1),p)); break; }
2919 49 : else { res = mkcol(ginv(gel(x,1))); break; }
2920 455 : case al_ALGEBRAIC :
2921 : switch(ta) {
2922 350 : case al_CYCLIC: n = alg_get_degree(al); break;
2923 105 : case al_CSA: n = alg_get_dim(al); break;
2924 : default: return NULL; /* LCOV_EXCL_LINE */
2925 : }
2926 455 : res = algdivl_i(al, x, col_ei(n,1), tx, al_ALGEBRAIC); break;
2927 371 : case al_BASIS : res = algdivl_i(al, x, col_ei(alg_get_absdim(al),1), tx,
2928 371 : al_BASIS); break;
2929 238 : case al_MATRIX :
2930 238 : n = lg(x)-1;
2931 238 : if (n==0) return cgetg(1, t_MAT);
2932 224 : if (n != nbrows(x)) pari_err_DIM("alginv_i (nonsquare)");
2933 217 : res = algdivl_i(al, x, col_ei(n*n*alg_get_absdim(al),1), tx, al_BASIS);
2934 : /* cheat on type because wrong dimension */
2935 : }
2936 1106 : if (!res) return gc_NULL(av);
2937 1092 : return gerepilecopy(av,res);
2938 : }
2939 : GEN
2940 1323 : alginv(GEN al, GEN x)
2941 : {
2942 : GEN z;
2943 1323 : checkalg(al);
2944 1323 : z = alginv_i(al,x);
2945 1274 : if (!z) pari_err_INV("alginv", x);
2946 1239 : return z;
2947 : }
2948 :
2949 : int
2950 189 : algisinv(GEN al, GEN x, GEN* ptix)
2951 : {
2952 189 : pari_sp av = avma;
2953 : GEN ix;
2954 189 : if (al) checkalg(al);
2955 189 : ix = alginv_i(al,x);
2956 189 : if (!ix) return gc_bool(av,0);
2957 133 : if (ptix != NULL) *ptix = ix;
2958 133 : return 1;
2959 : }
2960 :
2961 : /* x*y^(-1) */
2962 : GEN
2963 469 : algdivr(GEN al, GEN x, GEN y) { return algmul(al, x, alginv(al, y)); }
2964 :
2965 26049 : static GEN _mul(void* data, GEN x, GEN y) { return algmul((GEN)data,x,y); }
2966 49799 : static GEN _sqr(void* data, GEN x) { return algsqr((GEN)data,x); }
2967 :
2968 : static GEN
2969 21 : algmatid(GEN al, long N)
2970 : {
2971 21 : long n = alg_get_absdim(al), i, j;
2972 : GEN res, one, zero;
2973 :
2974 21 : res = zeromatcopy(N,N);
2975 21 : one = col_ei(n,1);
2976 21 : zero = zerocol(n);
2977 49 : for (i=1; i<=N; i++)
2978 84 : for (j=1; j<=N; j++)
2979 56 : gcoeff(res,i,j) = i==j ? one : zero;
2980 21 : return res;
2981 : }
2982 :
2983 : GEN
2984 12882 : algpow(GEN al, GEN x, GEN n)
2985 : {
2986 12882 : pari_sp av = avma;
2987 : GEN res;
2988 12882 : long s = signe(n);
2989 12882 : checkalg(al);
2990 12882 : if (!s && alg_type(al)==al_REAL)
2991 : {
2992 63 : if (H_model(x) == H_MATRIX) return matid(lg(x)-1);
2993 35 : else return gen_1;
2994 : }
2995 12819 : switch (s) {
2996 28 : case 0:
2997 28 : if (alg_model(al,x) == al_MATRIX)
2998 21 : res = algmatid(al,lg(x)-1);
2999 : else
3000 7 : res = col_ei(alg_get_absdim(al),1);
3001 28 : return res;
3002 12644 : case 1:
3003 12644 : res = gen_pow_i(x, n, (void*)al, _sqr, _mul); break;
3004 147 : default: /* -1 */
3005 147 : res = gen_pow_i(alginv(al,x), gneg(n), (void*)al, _sqr, _mul);
3006 : }
3007 12777 : return gerepilecopy(av,res);
3008 : }
3009 :
3010 : static GEN
3011 378 : algredcharpoly_i(GEN al, GEN x, long v)
3012 : {
3013 378 : GEN rnf = alg_get_splittingfield(al);
3014 378 : GEN cp = charpoly(algtomatrix(al,x,0),v);
3015 371 : long i, m = lg(cp);
3016 1540 : for (i=2; i<m; i++) gel(cp,i) = rnfeltdown(rnf, gel(cp,i));
3017 371 : return cp;
3018 : }
3019 :
3020 : /* assumes al is CSA or CYCLIC */
3021 : static GEN
3022 385 : algredcharpoly(GEN al, GEN x, long v)
3023 : {
3024 385 : pari_sp av = avma;
3025 385 : long w = gvar(rnf_get_pol(alg_get_center(al)));
3026 385 : if (varncmp(v,w)>=0) pari_err_PRIORITY("algredcharpoly",pol_x(v),">=",w);
3027 378 : switch(alg_type(al))
3028 : {
3029 378 : case al_CYCLIC:
3030 : case al_CSA:
3031 378 : return gerepileupto(av, algredcharpoly_i(al, x, v));
3032 : }
3033 : return NULL; /*LCOV_EXCL_LINE*/
3034 : }
3035 :
3036 : static GEN
3037 21077 : algbasischarpoly(GEN al, GEN x, long v)
3038 : {
3039 21077 : pari_sp av = avma;
3040 21077 : GEN p = alg_get_char(al), mx;
3041 21077 : if (alg_model(al,x) == al_MATRIX) mx = algleftmultable_mat(al,x);
3042 20986 : else mx = algbasismultable(al,x);
3043 21070 : if (signe(p)) {
3044 19173 : GEN res = FpM_charpoly(mx,p);
3045 19173 : setvarn(res,v);
3046 19173 : return gerepileupto(av, res);
3047 : }
3048 1897 : return gerepileupto(av, charpoly(mx,v));
3049 : }
3050 :
3051 : static GEN
3052 35 : R_charpoly(GEN x, long v, long abs)
3053 : {
3054 35 : pari_sp av = avma;
3055 35 : GEN res = NULL;
3056 35 : switch (H_model(x))
3057 : {
3058 14 : case H_SCALAR: res = mkpoln(2, gen_1, gneg(x)); break;
3059 14 : case H_MATRIX:
3060 14 : res = charpoly(x,v);
3061 14 : if (abs) res = gpowgs(res,nbrows(x));
3062 14 : break;
3063 7 : default: pari_err_TYPE("R_charpoly", x);
3064 : }
3065 28 : if (v) setvarn(res, v);
3066 28 : return gerepilecopy(av, res);
3067 : }
3068 : static GEN
3069 35 : C_charpoly(GEN x, long v, long abs)
3070 : {
3071 35 : pari_sp av = avma;
3072 35 : GEN res = NULL;
3073 35 : switch (H_model(x))
3074 : {
3075 14 : case H_SCALAR:
3076 14 : if (abs) res = mkpoln(3, gen_1, gneg(gshift(real_i(x),1)), cxnorm(x));
3077 7 : else res = mkpoln(2, gen_1, gneg(x));
3078 14 : break;
3079 14 : case H_MATRIX:
3080 14 : res = charpoly(x,v);
3081 14 : if (abs) res = gpowgs(real_i(gmul(res,gconj(res))),nbrows(x));
3082 14 : break;
3083 7 : default: pari_err_TYPE("C_charpoly", x);
3084 : }
3085 28 : if (v) setvarn(res, v);
3086 28 : return gerepilecopy(av, res);
3087 : }
3088 : static GEN
3089 98 : H_charpoly(GEN x, long v, long abs)
3090 : {
3091 98 : pari_sp av = avma;
3092 : GEN res;
3093 98 : if (H_model(x) == H_MATRIX) return greal(charpoly(H_tomatrix(x,abs),v));
3094 70 : res = mkpoln(3, gen_1, gneg(H_trace(x,0)), H_norm(x,0));
3095 70 : if (v) setvarn(res, v);
3096 70 : if (abs) res = gsqr(res);
3097 70 : return gerepilecopy(av, res);
3098 : }
3099 :
3100 : GEN
3101 21280 : algcharpoly(GEN al, GEN x, long v, long abs)
3102 : {
3103 : long ta;
3104 21280 : if (v<0) v=0;
3105 21280 : checkalg(al);
3106 21280 : ta = alg_type(al);
3107 21280 : if (ta == al_REAL) switch (alg_get_absdim(al)) {
3108 35 : case 1: return R_charpoly(x, v, abs);
3109 35 : case 2: return C_charpoly(x, v, abs);
3110 98 : case 4: return H_charpoly(x, v, abs);
3111 7 : default: pari_err_TYPE("algcharpoly [apply alginit]", al);
3112 : }
3113 :
3114 : /* gneg(x[1]) left on stack */
3115 21105 : if (alg_model(al,x) == al_TRIVIAL) {
3116 56 : GEN p = alg_get_char(al);
3117 56 : if (signe(p)) return deg1pol(gen_1,Fp_neg(gel(x,1),p),v);
3118 42 : return deg1pol(gen_1,gneg(gel(x,1)),v);
3119 : }
3120 :
3121 21042 : switch(ta) {
3122 490 : case al_CYCLIC: case al_CSA:
3123 490 : if (abs)
3124 : {
3125 105 : if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
3126 : }
3127 385 : else return algredcharpoly(al,x,v);
3128 20657 : case al_TABLE: return algbasischarpoly(al,x,v);
3129 : default : return NULL; /* LCOV_EXCL_LINE */
3130 : }
3131 : }
3132 :
3133 : /* assumes x in basis form */
3134 : static GEN
3135 236620 : algabstrace(GEN al, GEN x)
3136 : {
3137 236620 : pari_sp av = avma;
3138 236620 : GEN res = NULL, p = alg_get_char(al);
3139 236620 : if (signe(p)) return FpV_dotproduct(x, alg_get_tracebasis(al), p);
3140 36435 : switch(alg_model(al,x)) {
3141 154 : case al_TRIVIAL: return gcopy(gel(x,1)); break;
3142 36281 : case al_BASIS: res = RgV_dotproduct(x, alg_get_tracebasis(al)); break;
3143 : }
3144 36281 : return gerepileupto(av,res);
3145 : }
3146 :
3147 : static GEN
3148 1372 : algredtrace(GEN al, GEN x)
3149 : {
3150 1372 : pari_sp av = avma;
3151 1372 : GEN res = NULL;
3152 1372 : switch(alg_model(al,x)) {
3153 35 : case al_TRIVIAL: return gcopy(gel(x,1)); break;
3154 490 : case al_BASIS: return algredtrace(al, algbasistoalg(al,x));
3155 : /* TODO precompute too? */
3156 847 : case al_ALGEBRAIC:
3157 847 : switch(alg_type(al))
3158 : {
3159 553 : case al_CYCLIC:
3160 553 : res = rnfelttrace(alg_get_splittingfield(al),gel(x,1));
3161 553 : break;
3162 294 : case al_CSA:
3163 294 : res = gtrace(algalgmultable_csa(al,x));
3164 294 : res = gdiv(res, stoi(alg_get_degree(al)));
3165 294 : break;
3166 : default: return NULL; /* LCOV_EXCL_LINE */
3167 : }
3168 847 : }
3169 847 : return gerepileupto(av,res);
3170 : }
3171 :
3172 : static GEN
3173 469 : algtrace_mat(GEN al, GEN M, long abs) {
3174 469 : pari_sp av = avma;
3175 469 : long N = lg(M)-1, i;
3176 469 : GEN res, p = alg_get_char(al);
3177 469 : if (N == 0) return gen_0;
3178 448 : if (N != nbrows(M)) pari_err_DIM("algtrace_mat (nonsquare)");
3179 :
3180 434 : if (!signe(p)) p = NULL;
3181 434 : if (alg_type(al) == al_TABLE) abs = 1;
3182 434 : res = algtrace(al, gcoeff(M,1,1), abs);
3183 896 : for (i=2; i<=N; i++) {
3184 462 : if (p) res = Fp_add(res, algtrace(al,gcoeff(M,i,i),abs), p);
3185 455 : else res = gadd(res, algtrace(al,gcoeff(M,i,i),abs));
3186 : }
3187 434 : if (abs) res = gmulgu(res, N); /* absolute trace */
3188 434 : return gerepileupto(av, res);
3189 : }
3190 :
3191 : static GEN
3192 35 : R_trace(GEN x, long abs)
3193 : {
3194 35 : pari_sp av = avma;
3195 35 : GEN res = NULL;
3196 35 : switch (H_model(x))
3197 : {
3198 14 : case H_SCALAR: res = gcopy(x); break;
3199 14 : case H_MATRIX: res = abs? mulrs(gtrace(x),nbrows(x)) : gtrace(x); break;
3200 7 : default: pari_err_TYPE("R_trace", x);
3201 : }
3202 28 : return gerepilecopy(av, res);
3203 : }
3204 : static GEN
3205 35 : C_trace(GEN x, long abs)
3206 : {
3207 35 : pari_sp av = avma;
3208 35 : GEN res = NULL;
3209 35 : switch (H_model(x))
3210 : {
3211 14 : case H_SCALAR: res = abs ? gshift(real_i(x),1) : x; break;
3212 14 : case H_MATRIX:
3213 14 : res = abs ? mulrs(real_i(gtrace(x)),2*nbrows(x)) : gtrace(x); break;
3214 7 : default: pari_err_TYPE("C_trace", x);
3215 : }
3216 28 : return gerepilecopy(av, res);
3217 : }
3218 : static GEN
3219 567 : H_trace(GEN x, long abs)
3220 : {
3221 567 : long s = abs? 2 : 1;
3222 567 : switch (H_model(x))
3223 : {
3224 154 : case H_SCALAR: return gshift(real_i(x),s);
3225 329 : case H_QUATERNION: return gshift(gel(x,1),s);
3226 77 : case H_MATRIX:
3227 77 : return algtrace_mat(NULL, x, abs);
3228 : }
3229 : return NULL; /*LCOV_EXCL_LINE*/
3230 : }
3231 :
3232 : GEN
3233 2632 : algtrace(GEN al, GEN x, long abs)
3234 : {
3235 : long ta;
3236 2632 : checkalg(al);
3237 2632 : ta = alg_type(al);
3238 2632 : if (ta==al_REAL) switch (alg_get_absdim(al)) {
3239 35 : case 1: return R_trace(x,abs);
3240 35 : case 2: return C_trace(x,abs);
3241 497 : case 4: return H_trace(x,abs);
3242 7 : default: pari_err_TYPE("algtrace [apply alginit]", al);
3243 : }
3244 2058 : if (alg_model(al,x) == al_MATRIX) return algtrace_mat(al,x,abs);
3245 1666 : switch(ta) {
3246 1526 : case al_CYCLIC: case al_CSA:
3247 1526 : if (!abs) return algredtrace(al,x);
3248 644 : if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
3249 784 : case al_TABLE: return algabstrace(al,x);
3250 : default : return NULL; /* LCOV_EXCL_LINE */
3251 : }
3252 : }
3253 :
3254 : static GEN
3255 42288 : ZM_trace(GEN x)
3256 : {
3257 42288 : long i, lx = lg(x);
3258 : GEN t;
3259 42288 : if (lx < 3) return lx == 1? gen_0: gcopy(gcoeff(x,1,1));
3260 41483 : t = gcoeff(x,1,1);
3261 705373 : for (i = 2; i < lx; i++) t = addii(t, gcoeff(x,i,i));
3262 41483 : return t;
3263 : }
3264 : static GEN
3265 131590 : FpM_trace(GEN x, GEN p)
3266 : {
3267 131590 : long i, lx = lg(x);
3268 : GEN t;
3269 131590 : if (lx < 3) return lx == 1? gen_0: gcopy(gcoeff(x,1,1));
3270 123674 : t = gcoeff(x,1,1);
3271 911260 : for (i = 2; i < lx; i++) t = Fp_add(t, gcoeff(x,i,i), p);
3272 123674 : return t;
3273 : }
3274 :
3275 : static GEN
3276 40224 : algtracebasis(GEN al)
3277 : {
3278 40224 : pari_sp av = avma;
3279 40224 : GEN mt = alg_get_multable(al), p = alg_get_char(al);
3280 40224 : long i, l = lg(mt);
3281 40224 : GEN v = cgetg(l, t_VEC);
3282 171814 : if (signe(p)) for (i=1; i < l; i++) gel(v,i) = FpM_trace(gel(mt,i), p);
3283 47834 : else for (i=1; i < l; i++) gel(v,i) = ZM_trace(gel(mt,i));
3284 40224 : return gerepileupto(av,v);
3285 : }
3286 :
3287 : /* Assume: i > 0, expo := p^i <= absdim, x contained in I_{i-1} given by mult
3288 : * table modulo modu=p^(i+1). Return Tr(x^(p^i)) mod modu */
3289 : static ulong
3290 24639 : algtracei(GEN mt, ulong p, ulong expo, ulong modu)
3291 : {
3292 24639 : pari_sp av = avma;
3293 24639 : long j, l = lg(mt);
3294 24639 : ulong tr = 0;
3295 24639 : mt = Flm_powu(mt,expo,modu);
3296 269001 : for (j=1; j<l; j++) tr += ucoeff(mt,j,j);
3297 24639 : return gc_ulong(av, (tr/expo) % p);
3298 : }
3299 :
3300 : static GEN
3301 42 : R_norm(GEN x, long abs)
3302 : {
3303 42 : pari_sp av = avma;
3304 42 : GEN res = NULL;
3305 42 : switch (H_model(x))
3306 : {
3307 14 : case H_SCALAR: res = gcopy(x); break;
3308 21 : case H_MATRIX: res = abs ? powrs(det(x),nbrows(x)) : det(x); break;
3309 7 : default: pari_err_TYPE("R_norm", x);
3310 : }
3311 35 : return gerepilecopy(av,res);
3312 : }
3313 : static GEN
3314 42 : C_norm(GEN x, long abs)
3315 : {
3316 42 : pari_sp av = avma;
3317 42 : GEN res = NULL;
3318 42 : switch (H_model(x))
3319 : {
3320 14 : case H_SCALAR: res = abs ? cxnorm(x) : x; break;
3321 21 : case H_MATRIX: res = abs ? powrs(cxnorm(det(x)),nbrows(x)) : det(x); break;
3322 7 : default: pari_err_TYPE("C_norm", x);
3323 : }
3324 35 : return gerepilecopy(av,res);
3325 : }
3326 : static GEN
3327 434 : H_norm(GEN x, long abs)
3328 : {
3329 434 : pari_sp av = avma;
3330 434 : switch (H_model(x))
3331 : {
3332 42 : case H_SCALAR:
3333 42 : if (abs) return gerepilecopy(av,gsqr(gnorm(x)));
3334 35 : else return gnorm(x);
3335 322 : case H_QUATERNION:
3336 322 : if (abs) return gerepilecopy(av,gsqr(gnorml2(x)));
3337 294 : else return gnorml2(x);
3338 63 : case H_MATRIX:
3339 63 : return gerepilecopy(av,real_i(det(H_tomatrix(x,abs))));
3340 : }
3341 : return NULL; /*LCOV_EXCL_LINE*/
3342 : }
3343 :
3344 : GEN
3345 1253 : algnorm(GEN al, GEN x, long abs)
3346 : {
3347 1253 : pari_sp av = avma;
3348 : long tx, ta;
3349 : GEN p, rnf, res, mx;
3350 1253 : checkalg(al);
3351 1253 : ta = alg_type(al);
3352 1253 : if (ta==al_REAL) switch (alg_get_absdim(al)) {
3353 42 : case 1: return R_norm(x,abs);
3354 42 : case 2: return C_norm(x,abs);
3355 210 : case 4: return H_norm(x,abs);
3356 7 : default: pari_err_TYPE("algnorm [apply alginit]", al);
3357 : }
3358 952 : p = alg_get_char(al);
3359 952 : tx = alg_model(al,x);
3360 952 : if (signe(p)) {
3361 21 : if (tx == al_MATRIX) mx = algleftmultable_mat(al,x);
3362 14 : else mx = algbasismultable(al,x);
3363 21 : return gerepileupto(av, FpM_det(mx,p));
3364 : }
3365 931 : if (tx == al_TRIVIAL) return gcopy(gel(x,1));
3366 :
3367 889 : switch(ta) {
3368 819 : case al_CYCLIC: case al_CSA:
3369 819 : if (abs)
3370 : {
3371 196 : if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
3372 : }
3373 : else
3374 : {
3375 623 : rnf = alg_get_splittingfield(al);
3376 623 : res = rnfeltdown(rnf, det(algtomatrix(al,x,0)));
3377 616 : break;
3378 : }
3379 : case al_TABLE:
3380 266 : if (tx == al_MATRIX) mx = algleftmultable_mat(al,x);
3381 105 : else mx = algbasismultable(al,x);
3382 259 : res = det(mx);
3383 259 : break;
3384 : default: return NULL; /* LCOV_EXCL_LINE */
3385 : }
3386 875 : return gerepileupto(av, res);
3387 : }
3388 :
3389 : static GEN
3390 49245 : algalgtonat_cyc(GEN al, GEN x)
3391 : {
3392 49245 : pari_sp av = avma;
3393 49245 : GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
3394 49245 : long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
3395 49245 : res = zerocol(N*n);
3396 150941 : for (i=0; i<n; i++) {
3397 101696 : c = gel(x,i+1);
3398 101696 : c = rnfeltreltoabs(rnf,c);
3399 101696 : if (!gequal0(c)) {
3400 76958 : c = algtobasis(nf,c);
3401 415870 : for (i1=1; i1<=N; i1++) gel(res,i*N+i1) = gel(c,i1);
3402 : }
3403 : }
3404 49245 : return gerepilecopy(av, res);
3405 : }
3406 :
3407 : static GEN
3408 11375 : algalgtonat_csa(GEN al, GEN x)
3409 : {
3410 11375 : pari_sp av = avma;
3411 11375 : GEN nf = alg_get_center(al), res, c;
3412 11375 : long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
3413 11375 : res = zerocol(d2*n);
3414 56644 : for (i=0; i<d2; i++) {
3415 45269 : c = gel(x,i+1);
3416 45269 : if (!gequal0(c)) {
3417 31563 : c = algtobasis(nf,c);
3418 95095 : for (i1=1; i1<=n; i1++) gel(res,i*n+i1) = gel(c,i1);
3419 : }
3420 : }
3421 11375 : return gerepilecopy(av, res);
3422 : }
3423 :
3424 : /* assumes al CSA or CYCLIC */
3425 : static GEN
3426 60620 : algalgtonat(GEN al, GEN x)
3427 : {
3428 60620 : switch(alg_type(al))
3429 : {
3430 49245 : case al_CYCLIC: return algalgtonat_cyc(al, x);
3431 11375 : case al_CSA: return algalgtonat_csa(al, x);
3432 : }
3433 : return NULL; /*LCOV_EXCL_LINE*/
3434 : }
3435 :
3436 : static GEN
3437 11235 : algnattoalg_cyc(GEN al, GEN x)
3438 : {
3439 11235 : pari_sp av = avma;
3440 11235 : GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
3441 11235 : long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
3442 11235 : res = zerocol(n);
3443 11235 : c = zerocol(N);
3444 47726 : for (i=0; i<n; i++) {
3445 319585 : for (i1=1; i1<=N; i1++) gel(c,i1) = gel(x,i*N+i1);
3446 36491 : gel(res,i+1) = rnfeltabstorel(rnf,basistoalg(nf,c));
3447 : }
3448 11235 : return gerepilecopy(av, res);
3449 : }
3450 :
3451 : static GEN
3452 1281 : algnattoalg_csa(GEN al, GEN x)
3453 : {
3454 1281 : pari_sp av = avma;
3455 1281 : GEN nf = alg_get_center(al), res, c;
3456 1281 : long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
3457 1281 : res = zerocol(d2);
3458 1281 : c = zerocol(n);
3459 6888 : for (i=0; i<d2; i++) {
3460 19166 : for (i1=1; i1<=n; i1++) gel(c,i1) = gel(x,i*n+i1);
3461 5607 : gel(res,i+1) = basistoalg(nf,c);
3462 : }
3463 1281 : return gerepilecopy(av, res);
3464 : }
3465 :
3466 : /* assumes al CSA or CYCLIC */
3467 : static GEN
3468 12516 : algnattoalg(GEN al, GEN x)
3469 : {
3470 12516 : switch(alg_type(al))
3471 : {
3472 11235 : case al_CYCLIC: return algnattoalg_cyc(al, x);
3473 1281 : case al_CSA: return algnattoalg_csa(al, x);
3474 : }
3475 : return NULL; /*LCOV_EXCL_LINE*/
3476 : }
3477 :
3478 : static GEN
3479 182 : algalgtobasis_mat(GEN al, GEN x) /* componentwise */
3480 : {
3481 182 : pari_sp av = avma;
3482 : long lx, lxj, i, j;
3483 : GEN res;
3484 182 : lx = lg(x);
3485 182 : res = cgetg(lx, t_MAT);
3486 546 : for (j=1; j<lx; j++) {
3487 364 : lxj = lg(gel(x,j));
3488 364 : gel(res,j) = cgetg(lxj, t_COL);
3489 1092 : for (i=1; i<lxj; i++)
3490 728 : gcoeff(res,i,j) = algalgtobasis(al,gcoeff(x,i,j));
3491 : }
3492 182 : return gerepilecopy(av,res);
3493 : }
3494 : GEN
3495 61082 : algalgtobasis(GEN al, GEN x)
3496 : {
3497 : pari_sp av;
3498 : long tx, ta;
3499 61082 : checkalg(al);
3500 61082 : ta = alg_type(al);
3501 61082 : if (ta != al_CYCLIC && ta != al_CSA) pari_err_TYPE("algalgtobasis [use alginit]", al);
3502 61061 : tx = alg_model(al,x);
3503 61061 : if (tx==al_BASIS) return gcopy(x);
3504 60802 : if (tx==al_MATRIX) return algalgtobasis_mat(al,x);
3505 60620 : av = avma;
3506 60620 : x = algalgtonat(al,x);
3507 60620 : x = RgM_RgC_mul(alg_get_invbasis(al),x);
3508 60620 : return gerepileupto(av, x);
3509 : }
3510 :
3511 : static GEN
3512 119 : algbasistoalg_mat(GEN al, GEN x) /* componentwise */
3513 : {
3514 119 : long j, lx = lg(x);
3515 119 : GEN res = cgetg(lx, t_MAT);
3516 357 : for (j=1; j<lx; j++) {
3517 238 : long i, lxj = lg(gel(x,j));
3518 238 : gel(res,j) = cgetg(lxj, t_COL);
3519 714 : for (i=1; i<lxj; i++) gcoeff(res,i,j) = algbasistoalg(al,gcoeff(x,i,j));
3520 : }
3521 119 : return res;
3522 : }
3523 : GEN
3524 2926 : algbasistoalg(GEN al, GEN x)
3525 : {
3526 : pari_sp av;
3527 : long tx, ta;
3528 2926 : checkalg(al);
3529 2926 : ta = alg_type(al);
3530 2926 : if (ta != al_CYCLIC && ta != al_CSA) pari_err_TYPE("algbasistoalg [use alginit]", al);
3531 2905 : tx = alg_model(al,x);
3532 2905 : if (tx==al_ALGEBRAIC) return gcopy(x);
3533 2772 : if (tx==al_MATRIX) return algbasistoalg_mat(al,x);
3534 2653 : av = avma;
3535 2653 : x = RgM_RgC_mul(alg_get_basis(al),x);
3536 2653 : x = algnattoalg(al,x);
3537 2653 : return gerepileupto(av, x);
3538 : }
3539 :
3540 : static GEN
3541 4466 : R_random(GEN b)
3542 : {
3543 4466 : pari_sp av = avma;
3544 4466 : long prec = realprec(b);
3545 4466 : GEN z = randomr(prec); shiftr_inplace(z, 1);
3546 4466 : return gerepileuptoleaf(av, mulrr(b,addsr(-1, z)));
3547 : }
3548 : static GEN
3549 182 : C_random(GEN b)
3550 : {
3551 182 : retmkcomplex(R_random(b), R_random(b));
3552 : }
3553 : static GEN
3554 980 : H_random(GEN b)
3555 : {
3556 980 : GEN res = cgetg(5, t_COL);
3557 : long i;
3558 4900 : for (i=1; i<=4; i++) gel(res,i) = R_random(b);
3559 980 : return res;
3560 : }
3561 : GEN
3562 19677 : algrandom(GEN al, GEN b)
3563 : {
3564 19677 : GEN res = NULL, p, N;
3565 : long i, n;
3566 19677 : checkalg(al);
3567 19663 : if (alg_type(al)==al_REAL)
3568 : {
3569 1365 : if (typ(b) != t_REAL) pari_err_TYPE("algrandom",b);
3570 1358 : if (signe(b) < 0) pari_err_DOMAIN("algrandom", "b", "<", gen_0, b);
3571 1351 : switch(alg_get_absdim(al))
3572 : {
3573 182 : case 1: res = R_random(b); break;
3574 182 : case 2: res = C_random(b); break;
3575 980 : case 4: res = H_random(b); break;
3576 7 : default: pari_err_TYPE("algrandom [apply alginit]", al);
3577 : }
3578 1344 : return res;
3579 : }
3580 18298 : if (typ(b) != t_INT) pari_err_TYPE("algrandom",b);
3581 18291 : if (signe(b) < 0) pari_err_DOMAIN("algrandom", "b", "<", gen_0, b);
3582 18284 : n = alg_get_absdim(al);
3583 18284 : N = addiu(shifti(b,1), 1); /* left on stack */
3584 18284 : p = alg_get_char(al); if (!signe(p)) p = NULL;
3585 18284 : res = cgetg(n+1,t_COL);
3586 163828 : for (i = 1; i <= n; i++)
3587 : {
3588 145544 : pari_sp av = avma;
3589 145544 : GEN t = subii(randomi(N),b);
3590 145544 : if (p) t = modii(t, p);
3591 145544 : gel(res,i) = gerepileuptoint(av, t);
3592 : }
3593 18284 : return res;
3594 : }
3595 :
3596 : static GEN
3597 77 : H_poleval(GEN pol, GEN x)
3598 : {
3599 77 : pari_sp av = avma;
3600 : GEN res;
3601 : long i;
3602 77 : switch (H_model(x))
3603 : {
3604 21 : case H_SCALAR: return RgX_cxeval(pol, x, NULL);
3605 42 : case H_QUATERNION: break;
3606 7 : default: pari_err_TYPE("H_poleval", x);
3607 : }
3608 :
3609 42 : res = zerocol(4);
3610 189 : for (i=lg(pol)-1; i>1; i--)
3611 : {
3612 147 : gel(res,1) = gadd(gel(res,1), gel(pol,i));
3613 147 : if (i>2) res = H_mul(x, res);
3614 : }
3615 :
3616 42 : return gerepilecopy(av,res);
3617 : }
3618 :
3619 : /* Assumes pol has coefficients in the same ring as the COL x; x either
3620 : * in basis or algebraic form or [x,mx] where mx is the mult. table of x.
3621 : TODO more general version: pol with coeffs in center and x in basis form */
3622 : GEN
3623 17276 : algpoleval(GEN al, GEN pol, GEN x)
3624 : {
3625 17276 : pari_sp av = avma;
3626 17276 : GEN p, mx = NULL, res;
3627 : long i;
3628 17276 : if (typ(pol) != t_POL) pari_err_TYPE("algpoleval", pol);
3629 17262 : checkalg(al);
3630 17262 : if (alg_type(al)==al_REAL) return H_poleval(pol,x);
3631 17185 : p = alg_get_char(al);
3632 17185 : if (typ(x) == t_VEC)
3633 : {
3634 6097 : if (lg(x) != 3) pari_err_TYPE("algpoleval [vector must be of length 2]", x);
3635 6090 : mx = gel(x,2);
3636 6090 : x = gel(x,1);
3637 6090 : if (typ(mx)!=t_MAT || !gequal(x,gel(mx,1)))
3638 21 : pari_err_TYPE("algpoleval [mx must be the multiplication table of x]", mx);
3639 : }
3640 : else
3641 : {
3642 11088 : switch(alg_model(al,x))
3643 : {
3644 14 : case al_ALGEBRAIC: mx = algalgmultable(al,x); break;
3645 11046 : case al_BASIS: if (!RgX_is_QX(pol))
3646 7 : pari_err_IMPL("algpoleval with x in basis form and pol not in Q[x]");
3647 11053 : case al_TRIVIAL: mx = algbasismultable(al,x); break;
3648 7 : default: pari_err_TYPE("algpoleval", x);
3649 : }
3650 : }
3651 17136 : res = zerocol(lg(mx)-1);
3652 17136 : if (signe(p)) {
3653 64335 : for (i=lg(pol)-1; i>1; i--)
3654 : {
3655 47997 : gel(res,1) = Fp_add(gel(res,1), gel(pol,i), p);
3656 47997 : if (i>2) res = FpM_FpC_mul(mx, res, p);
3657 : }
3658 : }
3659 : else {
3660 4879 : for (i=lg(pol)-1; i>1; i--)
3661 : {
3662 4081 : gel(res,1) = gadd(gel(res,1), gel(pol,i));
3663 4081 : if (i>2) res = RgM_RgC_mul(mx, res);
3664 : }
3665 : }
3666 17136 : return gerepileupto(av, res);
3667 : }
3668 :
3669 : /** GRUNWALD-WANG **/
3670 : /*
3671 : Song Wang's PhD thesis (pdf pages)
3672 : p.25 definition of chi_b. K^Ker(chi_b) = K(b^(1/m))
3673 : p.26 bound on the conductor (also Cohen adv. GTM 193 p.166)
3674 : p.21 & p.34 description special case, also on wikipedia:
3675 : http://en.wikipedia.org/wiki/Grunwald%E2%80%93Wang_theorem#Special_fields
3676 : p.77 Kummer case
3677 : */
3678 :
3679 : /* n > 0. Is n = 2^k ? */
3680 : static int
3681 329 : uispow2(ulong n) { return !(n &(n-1)); }
3682 :
3683 : static GEN
3684 378 : get_phi0(GEN bnr, GEN Lpr, GEN Ld, GEN pl, long *pr, long *pn)
3685 : {
3686 378 : const long NTRY = 10; /* FIXME: magic constant */
3687 378 : const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
3688 378 : GEN S = bnr_get_cyc(bnr);
3689 : GEN Sst, G, globGmod, loc, X, Rglob, Rloc, H, U, Lconj;
3690 : long i, j, r, nbfrob, nbloc, nz, t;
3691 :
3692 378 : *pn = n;
3693 378 : *pr = r = lg(S)-1;
3694 378 : if (!r) return NULL;
3695 329 : Sst = cgetg(r+1, t_VECSMALL); /* Z/n-dual */
3696 1589 : for (i=1; i<=r; i++) Sst[i] = ugcdiu(gel(S,i), n);
3697 329 : if (Sst[1] != n) return NULL;
3698 329 : Lconj = NULL;
3699 329 : nbloc = nbfrob = lg(Lpr)-1;
3700 329 : if (uispow2(n))
3701 : {
3702 259 : long l = lg(pl), k = 0;
3703 259 : GEN real = cgetg(l, t_VECSMALL);
3704 973 : for (i = 1; i < l; i++)
3705 714 : if (pl[i] == -1) real[++k] = i;
3706 259 : if (k)
3707 : {
3708 259 : GEN nf = bnr_get_nf(bnr), I = bid_get_fact(bnr_get_bid(bnr));
3709 259 : GEN v, y, C = idealchineseinit(bnr, I);
3710 259 : long r1 = nf_get_r1(nf), n = nbrows(I);
3711 259 : nbloc += k;
3712 259 : Lconj = cgetg(k+1, t_VEC);
3713 259 : v = const_vecsmall(r1, 1);
3714 259 : y = const_vec(n, gen_1);
3715 707 : for (i = 1; i <= k; i++)
3716 : {
3717 448 : v[real[i]] = -1; gel(Lconj,i) = idealchinese(nf, mkvec2(C,v), y);
3718 448 : v[real[i]] = 1;
3719 : }
3720 : }
3721 : }
3722 329 : globGmod = cgetg(r+1,t_MAT);
3723 329 : G = cgetg(r+1,t_VECSMALL);
3724 1589 : for (i = 1; i <= r; i++)
3725 : {
3726 1260 : G[i] = n / Sst[i]; /* pairing between S and Sst */
3727 1260 : gel(globGmod,i) = cgetg(nbloc+1,t_VECSMALL);
3728 : }
3729 :
3730 : /* compute images of Frobenius elements (and complex conjugation) */
3731 329 : loc = cgetg(nbloc+1,t_VECSMALL);
3732 700 : for (i = 1; i <= nbloc; i++)
3733 : {
3734 : long L;
3735 539 : if (i <= nbfrob)
3736 : {
3737 224 : X = gel(Lpr, i);
3738 224 : L = Ld[i];
3739 : }
3740 : else
3741 : { /* X = 1 (mod f), sigma_i(x) < 0, positive at all other real places */
3742 315 : X = gel(Lconj, i-nbfrob);
3743 315 : L = 2;
3744 : }
3745 539 : X = ZV_to_Flv(isprincipalray(bnr,X), n);
3746 2275 : for (nz=0,j=1; j<=r; j++)
3747 : {
3748 1736 : ulong c = (X[j] * G[j]) % L;
3749 1736 : ucoeff(globGmod,i,j) = c;
3750 1736 : if (c) nz = 1;
3751 : }
3752 539 : if (!nz) return NULL;
3753 371 : loc[i] = L;
3754 : }
3755 :
3756 : /* try some random elements in the dual */
3757 161 : Rglob = cgetg(r+1,t_VECSMALL);
3758 466 : for (t=0; t<NTRY; t++) {
3759 1783 : for (j = 1; j <= r; j++) Rglob[j] = random_Fl(Sst[j]);
3760 459 : Rloc = zm_zc_mul(globGmod,Rglob);
3761 996 : for (i = 1; i <= nbloc; i++)
3762 842 : if (Rloc[i] % loc[i] == 0) break;
3763 459 : if (i > nbloc) return zv_to_ZV(Rglob);
3764 : }
3765 :
3766 : /* try to realize some random elements of the product of the local duals */
3767 7 : H = ZM_hnfall_i(shallowconcat(zm_to_ZM(globGmod),
3768 : diagonal_shallow(zv_to_ZV(loc))), &U, 2);
3769 : /* H,U nbloc x nbloc */
3770 7 : Rloc = cgetg(nbloc+1,t_COL);
3771 77 : for (t = 0; t < NTRY; t++)
3772 : { /* nonzero random coordinate */ /* TODO add special case ? */
3773 560 : for (i = 1; i <= nbloc; i++) gel(Rloc,i) = stoi(1 + random_Fl(loc[i]-1));
3774 70 : Rglob = hnf_invimage(H, Rloc);
3775 70 : if (Rglob)
3776 : {
3777 0 : Rglob = ZM_ZC_mul(U,Rglob);
3778 0 : return vecslice(Rglob,1,r);
3779 : }
3780 : }
3781 7 : return NULL;
3782 : }
3783 :
3784 : static GEN
3785 378 : bnrgwsearch(GEN bnr, GEN Lpr, GEN Ld, GEN pl)
3786 : {
3787 378 : pari_sp av = avma;
3788 : long n, r;
3789 378 : GEN phi0 = get_phi0(bnr,Lpr,Ld,pl, &r,&n), gn, v, H,U;
3790 378 : if (!phi0) return gc_const(av, gen_0);
3791 154 : gn = stoi(n);
3792 : /* compute kernel of phi0 */
3793 154 : v = ZV_extgcd(vec_append(phi0, gn));
3794 154 : U = vecslice(gel(v,2), 1,r);
3795 154 : H = ZM_hnfmodid(rowslice(U, 1,r), gn);
3796 154 : return gerepileupto(av, H);
3797 : }
3798 :
3799 : GEN
3800 154 : bnfgwgeneric(GEN bnf, GEN Lpr, GEN Ld, GEN pl, long var)
3801 : {
3802 154 : pari_sp av = avma;
3803 154 : const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
3804 : forprime_t S;
3805 154 : GEN bnr = NULL, ideal = gen_1, nf, dec, H = gen_0, finf, pol;
3806 : ulong ell, p;
3807 : long deg, i, degell;
3808 154 : (void)uisprimepower(n, &ell);
3809 154 : nf = bnf_get_nf(bnf);
3810 154 : deg = nf_get_degree(nf);
3811 154 : degell = ugcd(deg,ell-1);
3812 154 : finf = cgetg(lg(pl),t_VEC);
3813 427 : for (i=1; i<lg(pl); i++) gel(finf,i) = pl[i]==-1 ? gen_1 : gen_0;
3814 :
3815 154 : u_forprime_init(&S, 2, ULONG_MAX);
3816 679 : while ((p = u_forprime_next(&S))) {
3817 679 : if (Fl_powu(p % ell, degell, ell) != 1) continue; /* ell | p^deg-1 ? */
3818 364 : dec = idealprimedec(nf, utoipos(p));
3819 700 : for (i=1; i<lg(dec); i++) {
3820 490 : GEN pp = gel(dec,i);
3821 490 : if (RgV_isin(Lpr,pp)) continue;
3822 : /* TODO also accept the prime ideals at which there is a condition
3823 : * (use local Artin)? */
3824 434 : if (smodis(idealnorm(nf,pp),ell) != 1) continue; /* ell | N(pp)-1 ? */
3825 378 : ideal = idealmul(bnf,ideal,pp);
3826 : /* TODO: give factorization ? */
3827 378 : bnr = Buchray(bnf, mkvec2(ideal,finf), nf_INIT);
3828 378 : H = bnrgwsearch(bnr,Lpr,Ld,pl);
3829 378 : if (H != gen_0)
3830 : {
3831 154 : pol = rnfkummer(bnr,H,nf_get_prec(nf));
3832 154 : setvarn(pol, var);
3833 154 : return gerepileupto(av,pol);
3834 : }
3835 : }
3836 : }
3837 : pari_err_BUG("bnfgwgeneric (no suitable p)"); /*LCOV_EXCL_LINE*/
3838 : return NULL;/*LCOV_EXCL_LINE*/
3839 : }
3840 :
3841 : /* pr.p != ell */
3842 : static GEN
3843 168 : localextdeg(GEN nf, GEN pr, long d, ulong ell, long n)
3844 : {
3845 : GEN modpr, T, p, gen, k;
3846 168 : if (d == 1) return gen_1;
3847 154 : k = powuu(ell, Z_lval(subiu(pr_norm(pr),1), ell));
3848 154 : k = divis(k, n / d);
3849 154 : modpr = nf_to_Fq_init(nf, &pr, &T, &p);
3850 154 : (void)Fq_sqrtn(gen_1, k, T, p, &gen);
3851 154 : return Fq_to_nf(gen, modpr);
3852 : }
3853 : /* pr.p = ell */
3854 : static GEN
3855 119 : localextdegell(GEN nf, GEN pr, GEN E, long d, long n)
3856 : {
3857 : GEN x;
3858 119 : if (d == 1) return gen_1;
3859 112 : x = nfadd(nf, gen_1, pr_get_gen(pr));
3860 112 : return nfpowmodideal(nf, x, stoi(n / d), idealpow(nf, pr, E));
3861 : }
3862 :
3863 : /* Ld[i] must be nontrivial powers of the same prime ell */
3864 : /* pl : -1 at real places at which the extension must ramify, 0 elsewhere */
3865 : GEN
3866 196 : nfgwkummer(GEN nf, GEN Lpr, GEN Ld, GEN pl, long var)
3867 : {
3868 196 : const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
3869 : ulong ell;
3870 196 : long i, l = lg(Lpr), v = uisprimepower(n, &ell);
3871 196 : GEN E = cgetg(l, t_COL), y = cgetg(l, t_VEC);
3872 :
3873 483 : for (i = 1; i < l; i++)
3874 : {
3875 287 : GEN pr = gel(Lpr,i), p = pr_get_p(pr);
3876 287 : if (!absequalui(ell, p))
3877 : {
3878 168 : gel(E, i) = gen_1;
3879 168 : gel(y, i) = localextdeg(nf, pr, Ld[i], ell, n);
3880 : }
3881 : else
3882 : {
3883 119 : long e = pr_get_e(pr);
3884 119 : gel(E, i) = addui(1 + v*e, divsi(e, subiu(p,1)));
3885 119 : gel(y, i) = localextdegell(nf, pr, gel(E,i), Ld[i], n);
3886 : }
3887 : }
3888 : /* TODO use a factoredextchinese to ease computations afterwards ? */
3889 196 : y = idealchinese(nf, mkvec2(mkmat2(Lpr,E), pl), y);
3890 196 : return gsub(gpowgs(pol_x(var),n), basistoalg(nf, y));
3891 : }
3892 :
3893 : static GEN
3894 805 : get_vecsmall(GEN v)
3895 : {
3896 805 : switch(typ(v))
3897 : {
3898 679 : case t_VECSMALL: return v;
3899 119 : case t_VEC: if (RgV_is_ZV(v)) return ZV_to_zv(v);
3900 : }
3901 7 : pari_err_TYPE("nfgrunwaldwang",v);
3902 : return NULL;/*LCOV_EXCL_LINE*/
3903 : }
3904 : GEN
3905 448 : nfgrunwaldwang(GEN nf0, GEN Lpr, GEN Ld, GEN pl, long var)
3906 : {
3907 : ulong n, ell, ell2;
3908 448 : pari_sp av = avma;
3909 : GEN nf, bnf;
3910 : long t, w, i, vnf;
3911 :
3912 448 : if (var < 0) var = 0;
3913 448 : nf = get_nf(nf0,&t);
3914 448 : if (!nf) pari_err_TYPE("nfgrunwaldwang",nf0);
3915 448 : vnf = nf_get_varn(nf);
3916 448 : if (varncmp(var, vnf) >= 0)
3917 7 : pari_err_PRIORITY("nfgrunwaldwang", pol_x(var), ">=", vnf);
3918 441 : if (typ(Lpr) != t_VEC) pari_err_TYPE("nfgrunwaldwang",Lpr);
3919 427 : if (lg(Lpr) != lg(Ld)) pari_err_DIM("nfgrunwaldwang [#Lpr != #Ld]");
3920 420 : if (nf_get_degree(nf)==1) Lpr = shallowcopy(Lpr);
3921 945 : for (i=1; i<lg(Lpr); i++) {
3922 532 : GEN pr = gel(Lpr,i);
3923 532 : if (nf_get_degree(nf)==1 && typ(pr)==t_INT)
3924 77 : gel(Lpr,i) = gel(idealprimedec(nf,pr), 1);
3925 455 : else checkprid(pr);
3926 : }
3927 413 : if (lg(pl)-1 != nf_get_r1(nf))
3928 7 : pari_err_DOMAIN("nfgrunwaldwang [pl should have r1 components]", "#pl",
3929 7 : "!=", stoi(nf_get_r1(nf)), stoi(lg(pl)-1));
3930 :
3931 406 : Ld = get_vecsmall(Ld);
3932 399 : pl = get_vecsmall(pl);
3933 399 : bnf = get_bnf(nf0,&t);
3934 399 : n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
3935 :
3936 399 : if (!uisprimepower(n, &ell))
3937 7 : pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (a)");
3938 882 : for (i=1; i<lg(Ld); i++)
3939 497 : if (Ld[i]!=1 && (!uisprimepower(Ld[i],&ell2) || ell2!=ell))
3940 7 : pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (b)");
3941 1015 : for (i=1; i<lg(pl); i++)
3942 637 : if (pl[i]==-1 && ell%2)
3943 7 : pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (c)");
3944 :
3945 378 : w = bnf? bnf_get_tuN(bnf): itos(gel(nfrootsof1(nf),1));
3946 :
3947 : /* TODO choice between kummer and generic ? Let user choose between speed
3948 : * and size */
3949 378 : if (w%n==0 && lg(Ld)>1)
3950 196 : return gerepileupto(av, nfgwkummer(nf,Lpr,Ld,pl,var));
3951 182 : if (ell==n)
3952 : {
3953 154 : if (!bnf) bnf = Buchall(nf, nf_FORCE, 0);
3954 154 : return gerepileupto(av, bnfgwgeneric(bnf,Lpr,Ld,pl,var));
3955 : }
3956 28 : pari_err_IMPL("nfgrunwaldwang for nonprime degree");
3957 : return NULL; /*LCOV_EXCL_LINE*/
3958 : }
3959 :
3960 : /** HASSE INVARIANTS **/
3961 :
3962 : /* TODO long -> ulong + uel */
3963 : static GEN
3964 1022 : hasseconvert(GEN H, long n)
3965 : {
3966 : GEN h, c;
3967 : long i, l;
3968 1022 : switch(typ(H)) {
3969 952 : case t_VEC:
3970 952 : l = lg(H); h = cgetg(l,t_VECSMALL);
3971 952 : if (l == 1) return h;
3972 840 : c = gel(H,1);
3973 840 : if (typ(c) == t_VEC && l == 3)
3974 322 : return mkvec2(gel(H,1),hasseconvert(gel(H,2),n));
3975 1449 : for (i=1; i<l; i++)
3976 : {
3977 959 : c = gel(H,i);
3978 959 : switch(typ(c)) {
3979 714 : case t_INT: break;
3980 7 : case t_INTMOD:
3981 7 : c = gel(c,2); break;
3982 217 : case t_FRAC :
3983 217 : c = gmulgs(c,n);
3984 217 : if (typ(c) == t_INT) break;
3985 7 : pari_err_DOMAIN("hasseconvert [degree should be a denominator of the invariant]", "denom(h)", "ndiv", stoi(n), Q_denom(gel(H,i)));
3986 21 : default : pari_err_TYPE("Hasse invariant", c);
3987 : }
3988 931 : h[i] = smodis(c,n);
3989 : }
3990 490 : return h;
3991 63 : case t_VECSMALL: return H;
3992 : }
3993 7 : pari_err_TYPE("Hasse invariant", H);
3994 : return NULL;/*LCOV_EXCL_LINE*/
3995 : }
3996 :
3997 : /* assume f >= 2 */
3998 : static long
3999 420 : cyclicrelfrob0(GEN nf, GEN aut, GEN pr, GEN q, long f, long g)
4000 : {
4001 420 : GEN T, p, a, b, modpr = nf_to_Fq_init(nf,&pr,&T,&p);
4002 : long s;
4003 :
4004 420 : a = pol_x(nf_get_varn(nf));
4005 420 : b = galoisapply(nf, aut, modpr_genFq(modpr));
4006 420 : b = nf_to_Fq(nf, b, modpr);
4007 1323 : for (s = 0; !ZX_equal(a, b); s++) a = Fq_pow(a, q, T, p);
4008 420 : return g * Fl_inv(s, f); /* < n */
4009 : }
4010 :
4011 : static long
4012 1008 : cyclicrelfrob(GEN rnf, GEN auts, GEN pr)
4013 : {
4014 1008 : pari_sp av = avma;
4015 1008 : long f,g,frob, n = rnf_get_degree(rnf);
4016 1008 : GEN P = rnfidealprimedec(rnf, pr);
4017 :
4018 1008 : if (pr_get_e(gel(P,1)) > pr_get_e(pr))
4019 0 : pari_err_DOMAIN("cyclicrelfrob","e(PR/pr)",">",gen_1,pr);
4020 1008 : g = lg(P) - 1;
4021 1008 : f = n / g;
4022 :
4023 1008 : if (f <= 2) frob = g % n;
4024 : else {
4025 420 : GEN nf2, PR = gel(P,1);
4026 420 : GEN autabs = rnfeltreltoabs(rnf,gel(auts,g));
4027 420 : nf2 = obj_check(rnf,rnf_NFABS);
4028 420 : autabs = nfadd(nf2, autabs, gmul(rnf_get_k(rnf), rnf_get_alpha(rnf)));
4029 420 : frob = cyclicrelfrob0(nf2, autabs, PR, pr_norm(pr), f, g);
4030 : }
4031 1008 : return gc_long(av, frob);
4032 : }
4033 :
4034 : static long
4035 581 : localhasse(GEN rnf, GEN cnd, GEN pl, GEN auts, GEN b, long k)
4036 : {
4037 581 : pari_sp av = avma;
4038 : long v, m, h, lfa, frob, n, i;
4039 : GEN previous, y, pr, nf, q, fa;
4040 581 : nf = rnf_get_nf(rnf);
4041 581 : n = rnf_get_degree(rnf);
4042 581 : pr = gcoeff(cnd,k,1);
4043 581 : v = nfval(nf, b, pr);
4044 581 : m = lg(cnd)>1 ? nbrows(cnd) : 0;
4045 :
4046 : /* add the valuation of b to the conductor... */
4047 581 : previous = gcoeff(cnd,k,2);
4048 581 : gcoeff(cnd,k,2) = addis(previous, v);
4049 :
4050 581 : y = const_vec(m, gen_1);
4051 581 : gel(y,k) = b;
4052 : /* find a factored element y congruent to b mod pr^(vpr(b)+vpr(cnd)) and to 1 mod the conductor. */
4053 581 : y = factoredextchinese(nf, cnd, y, pl, &fa);
4054 581 : h = 0;
4055 581 : lfa = nbrows(fa);
4056 : /* sum of all Hasse invariants of (rnf/nf,aut,y) is 0, Hasse invariants at q!=pr are easy, Hasse invariant at pr is the same as for al=(rnf/nf,aut,b). */
4057 1120 : for (i=1; i<=lfa; i++) {
4058 539 : q = gcoeff(fa,i,1);
4059 539 : if (cmp_prime_ideal(pr,q)) {
4060 504 : frob = cyclicrelfrob(rnf, auts, q);
4061 504 : frob = Fl_mul(frob,umodiu(gcoeff(fa,i,2),n),n);
4062 504 : h = Fl_add(h,frob,n);
4063 : }
4064 : }
4065 : /* ...then restore it. */
4066 581 : gcoeff(cnd,k,2) = previous;
4067 581 : return gc_long(av, Fl_neg(h,n));
4068 : }
4069 :
4070 : static GEN
4071 749 : allauts(GEN rnf, GEN aut)
4072 : {
4073 749 : long n = rnf_get_degree(rnf), i;
4074 749 : GEN pol = rnf_get_pol(rnf), vaut;
4075 749 : if (n==1) n=2;
4076 749 : vaut = cgetg(n,t_VEC);
4077 749 : aut = lift_shallow(rnfbasistoalg(rnf,aut));
4078 749 : if (typ(aut) != t_POL || varn(pol) != varn(aut))
4079 0 : pari_err_TYPE("alg_cyclic", aut);
4080 749 : gel(vaut,1) = aut;
4081 1064 : for (i=1; i<n-1; i++)
4082 315 : gel(vaut,i+1) = RgX_rem(poleval(gel(vaut,i), aut), pol);
4083 749 : return vaut;
4084 : }
4085 :
4086 : static GEN
4087 259 : clean_factor(GEN fa)
4088 : {
4089 259 : GEN P2,E2, P = gel(fa,1), E = gel(fa,2);
4090 259 : long l = lg(P), i, j = 1;
4091 259 : P2 = cgetg(l, t_COL);
4092 259 : E2 = cgetg(l, t_COL);
4093 902 : for (i = 1;i < l; i++)
4094 643 : if (signe(gel(E,i))) {
4095 489 : gel(P2,j) = gel(P,i);
4096 489 : gel(E2,j) = gel(E,i); j++;
4097 : }
4098 259 : setlg(P2,j);
4099 259 : setlg(E2,j); return mkmat2(P2,E2);
4100 : }
4101 :
4102 : /* shallow concat x[1],...x[nx],y[1], ... y[ny], returning a t_COL. To be
4103 : * used when we do not know whether x,y are t_VEC or t_COL */
4104 : static GEN
4105 518 : colconcat(GEN x, GEN y)
4106 : {
4107 518 : long i, lx = lg(x), ly = lg(y);
4108 518 : GEN z=cgetg(lx+ly-1, t_COL);
4109 868 : for (i=1; i<lx; i++) z[i] = x[i];
4110 1454 : for (i=1; i<ly; i++) z[lx+i-1]= y[i];
4111 518 : return z;
4112 : }
4113 :
4114 : /* return v(x) at all primes in listpr, replace x by cofactor */
4115 : static GEN
4116 1008 : nfmakecoprime(GEN nf, GEN *px, GEN listpr)
4117 : {
4118 1008 : long j, l = lg(listpr);
4119 1008 : GEN x1, x = *px, L = cgetg(l, t_COL);
4120 :
4121 1008 : if (typ(x) != t_MAT)
4122 : { /* scalar, divide at the end (fast valuation) */
4123 868 : x1 = NULL;
4124 2015 : for (j=1; j<l; j++)
4125 : {
4126 1147 : GEN pr = gel(listpr,j), e;
4127 1147 : long v = nfval(nf, x, pr);
4128 1147 : e = stoi(v); gel(L,j) = e;
4129 1336 : if (v) x1 = x1? idealmulpowprime(nf, x1, pr, e)
4130 189 : : idealpow(nf, pr, e);
4131 : }
4132 868 : if (x1) x = idealdivexact(nf, idealhnf(nf,x), x1);
4133 : }
4134 : else
4135 : { /* HNF, divide as we proceed (reduce size) */
4136 196 : for (j=1; j<l; j++)
4137 : {
4138 56 : GEN pr = gel(listpr,j);
4139 56 : long v = idealval(nf, x, pr);
4140 56 : gel(L,j) = stoi(v);
4141 56 : if (v) x = idealmulpowprime(nf, x, pr, stoi(-v));
4142 : }
4143 : }
4144 1008 : *px = x; return L;
4145 : }
4146 :
4147 : /* Caveat: factorizations are not sorted wrt cmp_prime_ideal: Lpr comes first */
4148 : static GEN
4149 259 : computecnd(GEN rnf, GEN Lpr)
4150 : {
4151 : GEN id, nf, fa, Le, P,E;
4152 259 : long n = rnf_get_degree(rnf);
4153 :
4154 259 : nf = rnf_get_nf(rnf);
4155 259 : id = rnf_get_idealdisc(rnf);
4156 259 : Le = nfmakecoprime(nf, &id, Lpr);
4157 259 : fa = idealfactor(nf, id); /* part of D_{L/K} coprime with Lpr */
4158 259 : P = colconcat(Lpr,gel(fa,1));
4159 259 : E = colconcat(Le, gel(fa,2));
4160 259 : fa = mkmat2(P, gdiventgs(E, eulerphiu(n)));
4161 259 : return mkvec2(fa, clean_factor(fa));
4162 : }
4163 :
4164 : /* h >= 0 */
4165 : static void
4166 21 : nextgen(GEN gene, long h, GEN* gens, GEN* hgens, long* ngens, long* curgcd) {
4167 21 : long nextgcd = ugcd(h,*curgcd);
4168 21 : if (nextgcd == *curgcd) return;
4169 21 : (*ngens)++;
4170 21 : gel(*gens,*ngens) = gene;
4171 21 : gel(*hgens,*ngens) = utoi(h);
4172 21 : *curgcd = nextgcd;
4173 21 : return;
4174 : }
4175 :
4176 : static int
4177 35 : dividesmod(long d, long h, long n) { return !(h%cgcd(d,n)); }
4178 :
4179 : /* ramified prime with nontrivial Hasse invariant */
4180 : static GEN
4181 21 : localcomplete(GEN rnf, GEN pl, GEN cnd, GEN auts, long j, long n, long h, long* v)
4182 : {
4183 : GEN nf, gens, hgens, pr, modpr, T, p, sol, U, b, gene, randg, pu;
4184 : long ngens, i, d, np, d1, d2, hg, dnf, vcnd, curgcd;
4185 21 : nf = rnf_get_nf(rnf);
4186 21 : pr = gcoeff(cnd,j,1);
4187 21 : np = umodiu(pr_norm(pr), n);
4188 21 : dnf = nf_get_degree(nf);
4189 21 : vcnd = itos(gcoeff(cnd,j,2));
4190 21 : ngens = 13+dnf;
4191 21 : gens = zerovec(ngens);
4192 21 : hgens = zerovec(ngens);
4193 21 : *v = 0;
4194 21 : curgcd = 0;
4195 21 : ngens = 0;
4196 :
4197 21 : if (!uisprime(n)) {
4198 0 : gene = pr_get_gen(pr);
4199 0 : hg = localhasse(rnf, cnd, pl, auts, gene, j);
4200 0 : nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
4201 : }
4202 :
4203 21 : if (ugcd(np,n) != 1) { /* GCD(Np,n) != 1 */
4204 21 : pu = idealprincipalunits(nf,pr,vcnd);
4205 21 : pu = abgrp_get_gen(pu);
4206 42 : for (i=1; i<lg(pu) && !dividesmod(curgcd,h,n); i++) {
4207 21 : gene = gel(pu,i);
4208 21 : hg = localhasse(rnf, cnd, pl, auts, gene, j);
4209 21 : nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
4210 : }
4211 : }
4212 :
4213 21 : d = ugcd(np-1,n);
4214 21 : if (d != 1) { /* GCD(Np-1,n) != 1 */
4215 7 : modpr = nf_to_Fq_init(nf, &pr, &T, &p);
4216 7 : while (!dividesmod(curgcd,h,n)) { /* TODO gener_FpXQ_local */
4217 0 : if (T==NULL) randg = randomi(p);
4218 0 : else randg = random_FpX(degpol(T), varn(T),p);
4219 :
4220 0 : if (!gequal0(randg) && !gequal1(randg)) {
4221 0 : gene = Fq_to_nf(randg, modpr);
4222 0 : hg = localhasse(rnf, cnd, pl, auts, gene, j);
4223 0 : nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
4224 : }
4225 : }
4226 : }
4227 :
4228 21 : setlg(gens,ngens+1);
4229 21 : setlg(hgens,ngens+1);
4230 :
4231 21 : sol = ZV_extgcd(hgens);
4232 21 : U = ZV_to_Flv(gmael(sol,2,ngens), n);
4233 21 : d = itou(gel(sol,1));
4234 21 : d1 = ugcd(d, n);
4235 21 : d2 = d / d1;
4236 21 : d = Fl_mul(h / d1, Fl_inv(d2,n), n);
4237 21 : if (d != 1) U = Flv_Fl_mul(U, d, n);
4238 42 : for (i = 1, b = gen_1; i <= ngens; i++)
4239 21 : if (U[i]) b = nfmul(nf, b, nfpow_u(nf, gel(gens,i), U[i]));
4240 21 : *v = U[1]; return b;
4241 : }
4242 :
4243 : static int
4244 381 : testsplits(GEN data, GEN fa)
4245 : {
4246 381 : GEN rnf = gel(data,1), forbid = gel(data,2), P = gel(fa,1), E = gel(fa,2);
4247 381 : long i, n, l = lg(P);
4248 :
4249 777 : for (i = 1; i < l; i++)
4250 : {
4251 415 : GEN pr = gel(P,i);
4252 415 : if (tablesearch(forbid, pr, &cmp_prime_ideal)) return 0;
4253 : }
4254 362 : n = rnf_get_degree(rnf);
4255 507 : for (i = 1; i < l; i++)
4256 : {
4257 248 : long e = itos(gel(E,i)) % n;
4258 248 : if (e)
4259 : {
4260 234 : GEN L = rnfidealprimedec(rnf, gel(P,i));
4261 234 : long g = lg(L) - 1;
4262 234 : if ((e * g) % n) return 0;
4263 : }
4264 : }
4265 259 : return 1;
4266 : }
4267 :
4268 : /* remove entries with Hasse invariant 0 */
4269 : static GEN
4270 546 : hassereduce(GEN hf)
4271 : {
4272 546 : GEN pr,h, PR = gel(hf,1), H = gel(hf,2);
4273 546 : long i, j, l = lg(PR);
4274 :
4275 546 : pr= cgetg(l, t_VEC);
4276 546 : h = cgetg(l, t_VECSMALL);
4277 1253 : for (i = j = 1; i < l; i++)
4278 707 : if (H[i]) {
4279 378 : gel(pr,j) = gel(PR,i);
4280 378 : h[j] = H[i]; j++;
4281 : }
4282 546 : setlg(pr,j);
4283 546 : setlg(h,j); return mkvec2(pr,h);
4284 : }
4285 :
4286 : /* rnf complete */
4287 : static GEN
4288 259 : alg_complete0(GEN rnf, GEN aut, GEN hf, GEN hi, long maxord)
4289 : {
4290 259 : pari_sp av = avma;
4291 : GEN nf, pl, pl2, cnd, prcnd, cnds, y, Lpr, auts, b, fa, data, hfe;
4292 : GEN forbid, al, ind;
4293 : long D, n, d, i, j, l;
4294 259 : nf = rnf_get_nf(rnf);
4295 259 : n = rnf_get_degree(rnf);
4296 259 : d = nf_get_degree(nf);
4297 259 : D = d*n*n;
4298 259 : checkhasse(nf,hf,hi,n);
4299 259 : hf = hassereduce(hf);
4300 259 : Lpr = gel(hf,1);
4301 259 : hfe = gel(hf,2);
4302 :
4303 259 : auts = allauts(rnf,aut);
4304 :
4305 259 : pl = leafcopy(hi); /* conditions on the final b */
4306 259 : pl2 = leafcopy(hi); /* conditions for computing local Hasse invariants */
4307 259 : l = lg(pl); ind = cgetg(l, t_VECSMALL);
4308 658 : for (i = j = 1; i < l; i++)
4309 399 : if (hi[i]) { pl[i] = -1; pl2[i] = 1; } else ind[j++] = i;
4310 259 : setlg(ind, j);
4311 259 : y = nfpolsturm(nf, rnf_get_pol(rnf), ind);
4312 483 : for (i = 1; i < j; i++)
4313 224 : if (!signe(gel(y,i))) { pl[ind[i]] = 1; pl2[ind[i]] = 1; }
4314 :
4315 259 : cnds = computecnd(rnf,Lpr);
4316 259 : prcnd = gel(cnds,1);
4317 259 : cnd = gel(cnds,2);
4318 259 : y = cgetg(lgcols(prcnd),t_VEC);
4319 259 : forbid = vectrunc_init(lg(Lpr));
4320 434 : for (i=j=1; i<lg(Lpr); i++)
4321 : {
4322 175 : GEN pr = gcoeff(prcnd,i,1), yi;
4323 175 : long v, e = itou( gcoeff(prcnd,i,2) );
4324 175 : if (!e) {
4325 154 : long frob = cyclicrelfrob(rnf,auts,pr), f1 = ugcd(frob,n);
4326 154 : vectrunc_append(forbid, pr);
4327 154 : yi = gen_0;
4328 154 : v = ((hfe[i]/f1) * Fl_inv(frob/f1,n)) % n;
4329 : }
4330 : else
4331 21 : yi = localcomplete(rnf, pl2, cnd, auts, j++, n, hfe[i], &v);
4332 175 : gel(y,i) = yi;
4333 175 : gcoeff(prcnd,i,2) = stoi(e + v);
4334 : }
4335 727 : for (; i<lgcols(prcnd); i++) gel(y,i) = gen_1;
4336 259 : gen_sort_inplace(forbid, (void*)&cmp_prime_ideal, &cmp_nodata, NULL);
4337 259 : data = mkvec2(rnf,forbid);
4338 259 : b = factoredextchinesetest(nf,prcnd,y,pl,&fa,data,testsplits);
4339 :
4340 259 : al = cgetg(12, t_VEC);
4341 259 : gel(al,10)= gen_0; /* must be set first */
4342 259 : gel(al,1) = rnf;
4343 259 : gel(al,2) = auts;
4344 259 : gel(al,3) = basistoalg(nf,b);
4345 259 : gel(al,4) = hi;
4346 : /* add primes | disc or b with trivial Hasse invariant to hf */
4347 259 : Lpr = gel(prcnd,1); y = b;
4348 259 : (void)nfmakecoprime(nf, &y, Lpr);
4349 259 : Lpr = shallowconcat(Lpr, gel(idealfactor(nf,y), 1));
4350 259 : settyp(Lpr,t_VEC);
4351 259 : hf = mkvec2(Lpr, shallowconcat(hfe, const_vecsmall(lg(Lpr)-lg(hfe), 0)));
4352 259 : gel(al,5) = hf;
4353 259 : gel(al,6) = gen_0;
4354 259 : gel(al,7) = matid(D);
4355 259 : gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
4356 259 : gel(al,9) = algnatmultable(al,D);
4357 259 : gel(al,11)= algtracebasis(al);
4358 259 : if (maxord) al = alg_maximal_primes(al, prV_primes(Lpr));
4359 259 : return gerepilecopy(av, al);
4360 : }
4361 :
4362 : GEN
4363 0 : alg_complete(GEN rnf, GEN aut, GEN hf, GEN hi, long maxord)
4364 : {
4365 0 : long n = rnf_get_degree(rnf);
4366 0 : rnfcomplete(rnf);
4367 0 : return alg_complete0(rnf,aut,hasseconvert(hf,n),hasseconvert(hi,n), maxord);
4368 : }
4369 :
4370 : void
4371 1358 : checkhasse(GEN nf, GEN hf, GEN hi, long n)
4372 : {
4373 : GEN Lpr, Lh;
4374 : long i, sum;
4375 1358 : if (typ(hf) != t_VEC || lg(hf) != 3) pari_err_TYPE("checkhasse [hf]", hf);
4376 1351 : Lpr = gel(hf,1);
4377 1351 : Lh = gel(hf,2);
4378 1351 : if (typ(Lpr) != t_VEC) pari_err_TYPE("checkhasse [Lpr]", Lpr);
4379 1351 : if (typ(Lh) != t_VECSMALL) pari_err_TYPE("checkhasse [Lh]", Lh);
4380 1351 : if (typ(hi) != t_VECSMALL) pari_err_TYPE("checkhasse [hi]", hi);
4381 1351 : if ((nf && lg(hi) != nf_get_r1(nf)+1))
4382 7 : pari_err_DOMAIN("checkhasse [hi should have r1 components]","#hi","!=",stoi(nf_get_r1(nf)),stoi(lg(hi)-1));
4383 1344 : if (lg(Lpr) != lg(Lh))
4384 7 : pari_err_DIM("checkhasse [Lpr and Lh should have same length]");
4385 3150 : for (i=1; i<lg(Lpr); i++) checkprid(gel(Lpr,i));
4386 1337 : if (lg(gen_sort_uniq(Lpr, (void*)cmp_prime_ideal, cmp_nodata)) < lg(Lpr))
4387 7 : pari_err(e_MISC, "error in checkhasse [duplicate prime ideal]");
4388 1330 : sum = 0;
4389 3129 : for (i=1; i<lg(Lh); i++) sum = (sum+Lh[i])%n;
4390 3122 : for (i=1; i<lg(hi); i++) {
4391 1806 : if (hi[i] && 2*hi[i] != n) pari_err_DOMAIN("checkhasse", "Hasse invariant at real place [must be 0 or 1/2]", "!=", n%2? gen_0 : stoi(n/2), stoi(hi[i]));
4392 1792 : sum = (sum+hi[i])%n;
4393 : }
4394 1316 : if (sum<0) sum = n+sum;
4395 1316 : if (sum != 0)
4396 7 : pari_err_DOMAIN("checkhasse","sum(Hasse invariants)","!=",gen_0,Lh);
4397 1309 : }
4398 :
4399 : static GEN
4400 357 : hassecoprime(GEN hf, GEN hi, long n)
4401 : {
4402 357 : pari_sp av = avma;
4403 : long l, i, j, lk, inv;
4404 : GEN fa, P,E, res, hil, hfl;
4405 357 : hi = hasseconvert(hi, n);
4406 343 : hf = hasseconvert(hf, n);
4407 322 : checkhasse(NULL,hf,hi,n);
4408 280 : fa = factoru(n);
4409 280 : P = gel(fa,1); l = lg(P);
4410 280 : E = gel(fa,2);
4411 280 : res = cgetg(l,t_VEC);
4412 567 : for (i=1; i<l; i++) {
4413 287 : lk = upowuu(P[i],E[i]);
4414 287 : inv = Fl_invsafe((n/lk)%lk, lk);
4415 287 : hil = gcopy(hi);
4416 287 : hfl = gcopy(hf);
4417 :
4418 287 : if (P[i] == 2)
4419 623 : for (j=1; j<lg(hil); j++) hil[j] = hi[j]==0 ? 0 : lk/2;
4420 : else
4421 98 : for (j=1; j<lg(hil); j++) hil[j] = 0;
4422 819 : for (j=1; j<lgcols(hfl); j++) gel(hfl,2)[j] = (gel(hf,2)[j]*inv)%lk;
4423 287 : hfl = hassereduce(hfl);
4424 287 : gel(res,i) = mkvec3(hfl,hil,utoi(lk));
4425 : }
4426 :
4427 280 : return gerepilecopy(av, res);
4428 : }
4429 :
4430 : /* no garbage collection */
4431 : static GEN
4432 77 : genefrob(GEN nf, GEN gal, GEN r)
4433 : {
4434 : long i;
4435 77 : GEN g = identity_perm(nf_get_degree(nf)), fa = Z_factor(r), p, pr, frob;
4436 126 : for (i=1; i<lgcols(fa); i++) {
4437 49 : p = gcoeff(fa,i,1);
4438 49 : pr = idealprimedec(nf, p);
4439 49 : pr = gel(pr,1);
4440 49 : frob = idealfrobenius(nf, gal, pr);
4441 49 : g = perm_mul(g, perm_pow(frob, gcoeff(fa,i,2)));
4442 : }
4443 77 : return g;
4444 : }
4445 :
4446 : static GEN
4447 259 : rnfcycaut(GEN rnf)
4448 : {
4449 259 : GEN nf2 = obj_check(rnf, rnf_NFABS);
4450 : GEN L, alpha, pol, salpha, s, sj, polabs, k, X, pol0, nf;
4451 : long i, d, j;
4452 259 : d = rnf_get_degree(rnf);
4453 259 : L = galoisconj(nf2,NULL);
4454 259 : alpha = lift_shallow(rnf_get_alpha(rnf));
4455 259 : pol = rnf_get_pol(rnf);
4456 259 : k = rnf_get_k(rnf);
4457 259 : polabs = rnf_get_polabs(rnf);
4458 259 : nf = rnf_get_nf(rnf);
4459 259 : pol0 = nf_get_pol(nf);
4460 259 : X = RgX_rem(pol_x(varn(pol0)), pol0);
4461 :
4462 : /* TODO check mod prime of degree 1 */
4463 364 : for (i=1; i<lg(L); i++) {
4464 364 : s = gel(L,i);
4465 364 : salpha = RgX_RgXQ_eval(alpha,s,polabs);
4466 364 : if (!gequal(alpha,salpha)) continue;
4467 :
4468 322 : s = lift_shallow(rnfeltabstorel(rnf,s));
4469 322 : sj = s = gsub(s, gmul(k,X));
4470 623 : for (j=1; !gequal0(gsub(sj,pol_x(varn(s)))); j++)
4471 301 : sj = RgX_RgXQ_eval(sj,s,pol);
4472 322 : if (j<d) continue;
4473 259 : return s;
4474 : }
4475 : return NULL; /*LCOV_EXCL_LINE*/
4476 : }
4477 :
4478 : /* returns the smallest prime not in P */
4479 : static GEN
4480 84 : extraprime(GEN P)
4481 : {
4482 : forprime_t T;
4483 : GEN p;
4484 84 : forprime_init(&T, gen_2, NULL);
4485 98 : while ((p = forprime_next(&T))) if (!ZV_search(P, p)) break;
4486 84 : return p;
4487 : }
4488 :
4489 : /* true nf */
4490 : GEN
4491 371 : alg_hasse(GEN nf, long n, GEN hf, GEN hi, long var, long maxord)
4492 : {
4493 371 : pari_sp av = avma;
4494 371 : GEN primary, al = gen_0, al2, rnf, hil, hfl, Ld, pl, pol, Lpr, aut, Lpr2, Ld2;
4495 : long i, lk, j, maxdeg;
4496 371 : dbg_printf(1)("alg_hasse\n");
4497 371 : if (n<=1) pari_err_DOMAIN("alg_hasse", "degree", "<=", gen_1, stoi(n));
4498 357 : primary = hassecoprime(hf, hi, n);
4499 546 : for (i=1; i<lg(primary); i++) {
4500 287 : lk = itos(gmael(primary,i,3));
4501 287 : hfl = gmael(primary,i,1);
4502 287 : hil = gmael(primary,i,2);
4503 287 : checkhasse(nf, hfl, hil, lk);
4504 280 : dbg_printf(1)("alg_hasse: i=%d hf=%Ps hi=%Ps lk=%d\n", i, hfl, hil, lk);
4505 :
4506 280 : if (lg(gel(hfl,1))>1 || lk%2==0) {
4507 273 : maxdeg = 1;
4508 273 : Lpr = gel(hfl,1);
4509 273 : Ld = gcopy(gel(hfl,2));
4510 462 : for (j=1; j<lg(Ld); j++)
4511 : {
4512 189 : Ld[j] = lk/ugcd(lk,Ld[j]);
4513 189 : maxdeg = maxss(Ld[j],maxdeg);
4514 : }
4515 273 : pl = leafcopy(hil);
4516 686 : for (j=1; j<lg(pl); j++) if(pl[j])
4517 : {
4518 175 : pl[j] = -1;
4519 175 : maxdeg = maxss(maxdeg,2);
4520 : }
4521 :
4522 273 : Lpr2 = Lpr;
4523 273 : Ld2 = Ld;
4524 273 : if (maxdeg<lk)
4525 : {
4526 154 : if (maxdeg==1 && lk==2 && lg(pl)>1) pl[1] = -1;
4527 : else
4528 : {
4529 84 : GEN p = extraprime(prV_primes(Lpr));
4530 84 : Lpr2 = vec_append(Lpr2, idealprimedec_galois(nf, p));
4531 84 : Ld2 = vecsmall_append(Ld2, lk);
4532 : }
4533 : }
4534 :
4535 273 : dbg_printf(2)("alg_hasse: calling nfgrunwaldwang Lpr=%Ps Pd=%Ps pl=%Ps\n",
4536 : Lpr, Ld, pl);
4537 273 : pol = nfgrunwaldwang(nf, Lpr2, Ld2, pl, var);
4538 259 : dbg_printf(2)("alg_hasse: calling rnfinit(%Ps)\n", pol);
4539 259 : rnf = rnfinit0(nf,pol,1);
4540 259 : dbg_printf(2)("alg_hasse: computing automorphism\n");
4541 259 : aut = rnfcycaut(rnf);
4542 259 : dbg_printf(2)("alg_hasse: calling alg_complete\n");
4543 259 : al2 = alg_complete0(rnf,aut,hfl,hil,maxord);
4544 : }
4545 7 : else al2 = alg_matrix(nf, lk, var, maxord);
4546 :
4547 266 : if (i==1) al = al2;
4548 7 : else al = algtensor(al,al2,maxord);
4549 : }
4550 259 : return gerepilecopy(av,al);
4551 : }
4552 :
4553 : /** CYCLIC ALGEBRA WITH GIVEN HASSE INVARIANTS **/
4554 :
4555 : /* no garbage collection */
4556 : static GEN
4557 77 : subcycloindep(GEN nf, long n, long v, GEN *pr)
4558 : {
4559 : pari_sp av;
4560 : forprime_t S;
4561 : ulong p;
4562 77 : u_forprime_arith_init(&S, 1, ULONG_MAX, 1, n);
4563 77 : av = avma;
4564 84 : while ((p = u_forprime_next(&S)))
4565 : {
4566 84 : ulong r = pgener_Fl(p);
4567 84 : GEN pol = galoissubcyclo(utoipos(p), utoipos(Fl_powu(r,n,p)), 0, v);
4568 84 : GEN fa = nffactor(nf, pol);
4569 84 : if (lgcols(fa) == 2) { *pr = utoipos(r); return pol; }
4570 7 : set_avma(av);
4571 : }
4572 : pari_err_BUG("subcycloindep (no suitable prime = 1(mod n))"); /*LCOV_EXCL_LINE*/
4573 : *pr = NULL; return NULL; /*LCOV_EXCL_LINE*/
4574 : }
4575 :
4576 : GEN
4577 84 : alg_matrix(GEN nf, long n, long v, long maxord)
4578 : {
4579 84 : pari_sp av = avma;
4580 : GEN pol, gal, rnf, cyclo, g, r, aut;
4581 84 : dbg_printf(1)("alg_matrix\n");
4582 84 : if (n<=0) pari_err_DOMAIN("alg_matrix", "n", "<=", gen_0, stoi(n));
4583 77 : pol = subcycloindep(nf, n, v, &r);
4584 77 : rnf = rnfinit(nf, pol);
4585 77 : cyclo = nfinit(pol, nf_get_prec(nf));
4586 77 : gal = galoisinit(cyclo, NULL);
4587 77 : g = genefrob(cyclo,gal,r);
4588 77 : aut = galoispermtopol(gal,g);
4589 77 : return gerepileupto(av, alg_cyclic(rnf, aut, gen_1, maxord));
4590 : }
4591 :
4592 : GEN
4593 280 : alg_hilbert(GEN nf, GEN a, GEN b, long v, long maxord)
4594 : {
4595 280 : pari_sp av = avma;
4596 : GEN rnf, aut;
4597 280 : dbg_printf(1)("alg_hilbert\n");
4598 280 : if (!isint1(Q_denom(a)))
4599 7 : pari_err_DOMAIN("alg_hilbert", "denominator(a)", "!=", gen_1,a);
4600 273 : if (!isint1(Q_denom(b)))
4601 7 : pari_err_DOMAIN("alg_hilbert", "denominator(b)", "!=", gen_1,b);
4602 :
4603 266 : if (v < 0) v = 0;
4604 266 : rnf = rnfinit(nf, deg2pol_shallow(gen_1, gen_0, gneg(a), v));
4605 259 : aut = gneg(pol_x(v));
4606 259 : return gerepileupto(av, alg_cyclic(rnf, aut, b, maxord));
4607 : }
4608 :
4609 : /* return a structure representing the algebra of real numbers */
4610 : static GEN
4611 14 : mk_R()
4612 : {
4613 14 : pari_sp av = avma;
4614 : GEN al;
4615 14 : al = zerovec(11);
4616 14 : gel(al,1) = stor(1,3);
4617 14 : gel(al,2) = mkvec(gel(al,1));
4618 14 : gel(al,3) = gen_1;
4619 14 : gel(al,4) = mkvecsmall(0);
4620 14 : gel(al,8) = gel(al,7) = matid(1);
4621 14 : gel(al,9) = mkvec(matid(1));
4622 14 : return gerepilecopy(av,al);
4623 : }
4624 : /* return a structure representing the algebra of complex numbers */
4625 : static GEN
4626 14 : mk_C()
4627 : {
4628 14 : pari_sp av = avma;
4629 : GEN al, I;
4630 14 : al = zerovec(11);
4631 14 : I = gen_I();
4632 14 : gel(al,1) = I;
4633 14 : gel(al,2) = mkvec(I);
4634 14 : gel(al,3) = gen_1;
4635 14 : gel(al,4) = cgetg(1,t_VECSMALL);
4636 14 : gel(al,8) = gel(al,7) = matid(2);
4637 14 : gel(al,9) = mkvec2(
4638 : matid(2),
4639 : mkmat22(gen_0,gen_m1,gen_1,gen_0)
4640 : );
4641 14 : return gerepilecopy(av,al);
4642 : }
4643 : /* return a structure representing the Hamilton quaternion algebra */
4644 : static GEN
4645 14 : mk_H()
4646 : {
4647 14 : pari_sp av = avma;
4648 : GEN al, I;
4649 14 : al = zerovec(11);
4650 14 : I = gen_I();
4651 14 : gel(al,1) = I;
4652 14 : gel(al,2) = mkvec(gconj(I));
4653 14 : gel(al,3) = gen_m1;
4654 14 : gel(al,4) = mkvecsmall(1);
4655 14 : gel(al,8) = gel(al,7) = matid(4);
4656 14 : gel(al,9) = mkvec4(
4657 : matid(4),
4658 : H_tomatrix(I,1),
4659 : H_tomatrix(mkcol4(gen_0,gen_0,gen_1,gen_0),1),
4660 : H_tomatrix(mkcol4(gen_0,gen_0,gen_0,gen_1),1)
4661 : );
4662 14 : return gerepilecopy(av,al);
4663 : }
4664 :
4665 : GEN
4666 1162 : alginit(GEN A, GEN B, long v, long maxord)
4667 : {
4668 : long w;
4669 1162 : if (typ(A) == t_COMPLEX) return mk_C();
4670 1148 : if (typ(A) == t_REAL)
4671 : {
4672 35 : if (is_scalar_t(typ(B)) && gequal0(B)) return mk_R();
4673 21 : if (typ(B) == t_FRAC && gequal(B, mkfrac(gen_1,gen_2))) return mk_H();
4674 7 : pari_err_DOMAIN("alginit", "real Hasse invariant [must be 0 or 1/2]", "", NULL, B);
4675 : }
4676 1113 : switch(nftyp(A))
4677 : {
4678 931 : case typ_NF:
4679 931 : if (v<0) v=0;
4680 931 : w = gvar(nf_get_pol(A));
4681 931 : if (varncmp(v,w)>=0) pari_err_PRIORITY("alginit", pol_x(v), ">=", w);
4682 917 : switch(typ(B))
4683 : {
4684 : long nB;
4685 77 : case t_INT: return alg_matrix(A, itos(B), v, maxord);
4686 833 : case t_VEC:
4687 833 : nB = lg(B)-1;
4688 833 : if (nB && typ(gel(B,1)) == t_MAT) return alg_csa_table(A,B,v,maxord);
4689 : switch(nB)
4690 : {
4691 280 : case 2: return alg_hilbert(A, gel(B,1), gel(B,2), v, maxord);
4692 378 : case 3:
4693 378 : if (typ(gel(B,1))!=t_INT)
4694 7 : pari_err_TYPE("alginit [degree should be an integer]", gel(B,1));
4695 371 : return alg_hasse(A, itos(gel(B,1)), gel(B,2), gel(B,3), v,
4696 : maxord);
4697 : }
4698 : }
4699 14 : pari_err_TYPE("alginit", B); break;
4700 :
4701 168 : case typ_RNF:
4702 168 : if (typ(B) != t_VEC || lg(B) != 3) pari_err_TYPE("alginit", B);
4703 154 : return alg_cyclic(A, gel(B,1), gel(B,2), maxord);
4704 : }
4705 14 : pari_err_TYPE("alginit", A);
4706 : return NULL;/*LCOV_EXCL_LINE*/
4707 : }
4708 :
4709 : /* assumes al CSA or CYCLIC */
4710 : static GEN
4711 889 : algnatmultable(GEN al, long D)
4712 : {
4713 : GEN res, x;
4714 : long i;
4715 889 : res = cgetg(D+1,t_VEC);
4716 10752 : for (i=1; i<=D; i++) {
4717 9863 : x = algnattoalg(al,col_ei(D,i));
4718 9863 : gel(res,i) = algZmultable(al,x);
4719 : }
4720 889 : return res;
4721 : }
4722 :
4723 : /* no garbage collection */
4724 : static void
4725 490 : algcomputehasse(GEN al)
4726 : {
4727 : long r1, k, n, m, m1, m2, m3, i, m23, m123;
4728 : GEN rnf, nf, b, fab, disc2, cnd, fad, auts, pr, pl, perm, y, hi, PH, H, L;
4729 :
4730 490 : rnf = alg_get_splittingfield(al);
4731 490 : n = rnf_get_degree(rnf);
4732 490 : nf = rnf_get_nf(rnf);
4733 490 : b = alg_get_b(al);
4734 490 : r1 = nf_get_r1(nf);
4735 490 : auts = alg_get_auts(al);
4736 490 : (void)alg_get_abssplitting(al);
4737 :
4738 490 : y = nfpolsturm(nf, rnf_get_pol(rnf), NULL);
4739 490 : pl = cgetg(r1+1, t_VECSMALL);
4740 : /* real places where rnf/nf ramifies */
4741 1029 : for (k = 1; k <= r1; k++) pl[k] = !signe(gel(y,k));
4742 :
4743 : /* infinite Hasse invariants */
4744 490 : if (odd(n)) hi = const_vecsmall(r1, 0);
4745 : else
4746 : {
4747 413 : GEN s = nfsign(nf, b);
4748 413 : hi = cgetg(r1+1, t_VECSMALL);
4749 896 : for (k = 1; k<=r1; k++) hi[k] = (s[k] && pl[k]) ? (n/2) : 0;
4750 : }
4751 :
4752 490 : fab = idealfactor(nf, b);
4753 490 : disc2 = rnf_get_idealdisc(rnf);
4754 490 : L = nfmakecoprime(nf, &disc2, gel(fab,1));
4755 490 : m = lg(L)-1;
4756 : /* m1 = #{pr|b: pr \nmid disc}, m3 = #{pr|b: pr | disc} */
4757 490 : perm = cgetg(m+1, t_VECSMALL);
4758 875 : for (i=1, m1=m, k=1; k<=m; k++)
4759 385 : if (signe(gel(L,k))) perm[m1--] = k; else perm[i++] = k;
4760 490 : m3 = m - m1;
4761 :
4762 : /* disc2 : factor of disc coprime to b */
4763 490 : fad = idealfactor(nf, disc2);
4764 : /* m2 : number of prime factors of disc not dividing b */
4765 490 : m2 = nbrows(fad);
4766 490 : m23 = m2+m3;
4767 490 : m123 = m1+m2+m3;
4768 :
4769 : /* initialize the possibly ramified primes (hasse) and the factored conductor of rnf/nf (cnd) */
4770 490 : cnd = zeromatcopy(m23,2);
4771 490 : PH = cgetg(m123+1, t_VEC); /* ramified primes */
4772 490 : H = cgetg(m123+1, t_VECSMALL); /* Hasse invariant */
4773 : /* compute Hasse invariant at primes that are unramified in rnf/nf */
4774 840 : for (k=1; k<=m1; k++) {/* pr | b, pr \nmid disc */
4775 350 : long frob, e, j = perm[k];
4776 350 : pr = gcoeff(fab,j,1);
4777 350 : e = itos(gcoeff(fab,j,2));
4778 350 : frob = cyclicrelfrob(rnf, auts, pr);
4779 350 : gel(PH,k) = pr;
4780 350 : H[k] = Fl_mul(frob, e, n);
4781 : }
4782 : /* compute Hasse invariant at primes that are ramified in rnf/nf */
4783 1015 : for (k=1; k<=m2; k++) {/* pr \nmid b, pr | disc */
4784 525 : pr = gcoeff(fad,k,1);
4785 525 : gel(PH,k+m1) = pr;
4786 525 : gcoeff(cnd,k,1) = pr;
4787 525 : gcoeff(cnd,k,2) = gcoeff(fad,k,2);
4788 : }
4789 525 : for (k=1; k<=m3; k++) { /* pr | (b, disc) */
4790 35 : long j = perm[k+m1];
4791 35 : pr = gcoeff(fab,j,1);
4792 35 : gel(PH,k+m1+m2) = pr;
4793 35 : gcoeff(cnd,k+m2,1) = pr;
4794 35 : gcoeff(cnd,k+m2,2) = gel(L,j);
4795 : }
4796 490 : gel(cnd,2) = gdiventgs(gel(cnd,2), eulerphiu(n));
4797 1050 : for (k=1; k<=m23; k++) H[k+m1] = localhasse(rnf, cnd, pl, auts, b, k);
4798 490 : gel(al,4) = hi;
4799 490 : perm = gen_indexsort(PH, (void*)&cmp_prime_ideal, &cmp_nodata);
4800 490 : gel(al,5) = mkvec2(vecpermute(PH,perm),vecsmallpermute(H,perm));
4801 490 : checkhasse(nf,alg_get_hasse_f(al),alg_get_hasse_i(al),n);
4802 490 : }
4803 :
4804 : static GEN
4805 777 : alg_maximal_primes(GEN al, GEN P)
4806 : {
4807 777 : pari_sp av = avma;
4808 777 : long l = lg(P), i;
4809 2091 : for (i=1; i<l; i++)
4810 : {
4811 1314 : if (i != 1) al = gerepilecopy(av, al);
4812 1314 : al = alg_pmaximal(al,gel(P,i));
4813 : }
4814 777 : return al;
4815 : }
4816 :
4817 : GEN
4818 504 : alg_cyclic(GEN rnf, GEN aut, GEN b, long maxord)
4819 : {
4820 504 : pari_sp av = avma;
4821 : GEN al, nf;
4822 : long D, n, d;
4823 504 : dbg_printf(1)("alg_cyclic\n");
4824 504 : checkrnf(rnf); nf = rnf_get_nf(rnf);
4825 504 : b = nf_to_scalar_or_basis(nf, b);
4826 497 : if (typ(b) == t_FRAC || (typ(b) == t_COL && !RgV_is_ZV(b)))
4827 7 : pari_err_DOMAIN("alg_cyclic", "denominator(b)", "!=", gen_1,b);
4828 :
4829 490 : n = rnf_get_degree(rnf);
4830 490 : d = nf_get_degree(nf);
4831 490 : D = d*n*n;
4832 :
4833 490 : al = cgetg(12,t_VEC);
4834 490 : gel(al,10)= gen_0; /* must be set first */
4835 490 : gel(al,1) = rnf;
4836 490 : gel(al,2) = allauts(rnf, aut);
4837 490 : gel(al,3) = basistoalg(nf,b);
4838 490 : rnf_build_nfabs(rnf, nf_get_prec(nf));
4839 490 : gel(al,6) = gen_0;
4840 490 : gel(al,7) = matid(D);
4841 490 : gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
4842 490 : gel(al,9) = algnatmultable(al,D);
4843 490 : gel(al,11)= algtracebasis(al);
4844 :
4845 490 : algcomputehasse(al);
4846 :
4847 490 : if (maxord) {
4848 427 : GEN hf = alg_get_hasse_f(al), pr = gel(hf,1);
4849 427 : al = alg_maximal_primes(al, prV_primes(pr));
4850 : }
4851 490 : return gerepilecopy(av, al);
4852 : }
4853 :
4854 : static int
4855 420 : ismaximalsubfield(GEN al, GEN x, GEN d, long v, GEN *pt_minpol)
4856 : {
4857 420 : GEN cp = algbasischarpoly(al, x, v), lead;
4858 420 : if (!ispower(cp, d, pt_minpol)) return 0;
4859 420 : lead = leading_coeff(*pt_minpol);
4860 420 : if (isintm1(lead)) *pt_minpol = gneg(*pt_minpol);
4861 420 : return ZX_is_irred(*pt_minpol);
4862 : }
4863 :
4864 : static GEN
4865 140 : findmaximalsubfield(GEN al, GEN d, long v)
4866 : {
4867 140 : long count, nb=2, i, N = alg_get_absdim(al), n = nf_get_degree(alg_get_center(al));
4868 140 : GEN x, minpol, maxc = gen_1;
4869 :
4870 231 : for (i=n+1; i<=N; i+=n) {
4871 392 : for (count=0; count<2 && i+count<=N; count++) {
4872 301 : x = col_ei(N,i+count);
4873 301 : if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
4874 : }
4875 : }
4876 :
4877 : while(1) {
4878 119 : x = zerocol(N);
4879 504 : for (count=0; count<nb; count++)
4880 : {
4881 385 : i = random_Fl(N)+1;
4882 385 : gel(x,i) = addiu(randomi(maxc),1);
4883 385 : if (random_bits(1)) gel(x,i) = negi(gel(x,i));
4884 : }
4885 119 : if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
4886 56 : if (!random_bits(3)) maxc = addiu(maxc,1);
4887 56 : if (nb<N) nb++;
4888 : }
4889 :
4890 : return NULL; /* LCOV_EXCL_LINE */
4891 : }
4892 :
4893 : static GEN
4894 140 : frobeniusform(GEN al, GEN x)
4895 : {
4896 : GEN M, FP, P, Pi;
4897 :
4898 : /* /!\ has to be the *right* multiplication table */
4899 140 : M = algbasisrightmultable(al, x);
4900 :
4901 140 : FP = matfrobenius(M,2,0); /* M = P^(-1)*F*P */
4902 140 : P = gel(FP,2);
4903 140 : Pi = RgM_inv(P);
4904 140 : return mkvec2(P, Pi);
4905 : }
4906 :
4907 : static void
4908 140 : computesplitting(GEN al, long d, long v)
4909 : {
4910 140 : GEN subf, x, pol, polabs, basis, P, Pi, nf = alg_get_center(al), rnf, Lbasis, Lbasisinv, Q, pows;
4911 140 : long i, n = nf_get_degree(nf), nd = n*d, N = alg_get_absdim(al), j, j2;
4912 :
4913 140 : subf = findmaximalsubfield(al, utoipos(d), v);
4914 140 : x = gel(subf, 1);
4915 140 : polabs = gel(subf, 2);
4916 :
4917 : /* Frobenius form to obtain L-vector space structure */
4918 140 : basis = frobeniusform(al, x);
4919 140 : P = gel(basis, 1);
4920 140 : Pi = gel(basis, 2);
4921 :
4922 : /* construct rnf of splitting field */
4923 140 : pol = nffactor(nf,polabs);
4924 140 : pol = gcoeff(pol,1,1);
4925 140 : gel(al,1) = rnf = rnfinit(nf, pol);
4926 : /* since pol is irreducible over Q, we have k=0 in rnf. */
4927 140 : if (!gequal0(rnf_get_k(rnf)))
4928 : pari_err_BUG("computesplitting (k!=0)"); /*LCOV_EXCL_LINE*/
4929 140 : gel(al,6) = gen_0;
4930 140 : rnf_build_nfabs(rnf, nf_get_prec(nf));
4931 :
4932 : /* construct splitting data */
4933 140 : Lbasis = cgetg(d+1, t_MAT);
4934 378 : for (j=j2=1; j<=d; j++, j2+=nd)
4935 238 : gel(Lbasis,j) = gel(Pi,j2);
4936 :
4937 140 : Q = zeromatcopy(d,N);
4938 140 : pows = pol_x_powers(nd,v);
4939 378 : for (i=j=1; j<=N; j+=nd, i++)
4940 1155 : for (j2=0; j2<nd; j2++)
4941 917 : gcoeff(Q,i,j+j2) = mkpolmod(gel(pows,j2+1),polabs);
4942 140 : Lbasisinv = RgM_mul(Q,P);
4943 :
4944 140 : gel(al,3) = mkvec3(x,Lbasis,Lbasisinv);
4945 140 : }
4946 :
4947 : /* assumes that mt defines a central simple algebra over nf */
4948 : GEN
4949 168 : alg_csa_table(GEN nf, GEN mt0, long v, long maxord)
4950 : {
4951 168 : pari_sp av = avma;
4952 : GEN al, mt;
4953 168 : long n, D, d2 = lg(mt0)-1, d = usqrt(d2);
4954 168 : dbg_printf(1)("alg_csa_table\n");
4955 :
4956 168 : mt = check_relmt(nf,mt0);
4957 154 : if (!mt) pari_err_TYPE("alg_csa_table", mt0);
4958 147 : n = nf_get_degree(nf);
4959 147 : D = n*d2;
4960 147 : if (d*d != d2)
4961 7 : pari_err_DOMAIN("alg_csa_table","(nonsquare) dimension","!=",stoi(d*d),mt);
4962 :
4963 140 : al = cgetg(12, t_VEC);
4964 140 : gel(al,10) = gen_0; /* must be set first */
4965 140 : gel(al,1) = zerovec(12); gmael(al,1,10) = nf;
4966 140 : gmael(al,1,1) = gpowgs(pol_x(0), d); /* placeholder before splitting field */
4967 140 : gel(al,2) = mt;
4968 140 : gel(al,3) = gen_0; /* placeholder */
4969 140 : gel(al,4) = gel(al,5) = gen_0; /* TODO Hasse invariants */
4970 140 : gel(al,5) = gel(al,6) = gen_0; /* placeholder */
4971 140 : gel(al,7) = matid(D);
4972 140 : gel(al,8) = matid(D);
4973 140 : gel(al,9) = algnatmultable(al,D);
4974 140 : gel(al,11)= algtracebasis(al);
4975 140 : if (maxord) al = alg_maximal(al);
4976 140 : computesplitting(al, d, v);
4977 140 : return gerepilecopy(av, al);
4978 : }
4979 :
4980 : static GEN
4981 37583 : algtableinit_i(GEN mt0, GEN p)
4982 : {
4983 : GEN al, mt;
4984 : long i, n;
4985 :
4986 37583 : if (p && !signe(p)) p = NULL;
4987 37583 : mt = check_mt(mt0,p);
4988 37583 : if (!mt) pari_err_TYPE("algtableinit", mt0);
4989 37576 : if (!p && !isint1(Q_denom(mt0)))
4990 7 : pari_err_DOMAIN("algtableinit", "denominator(mt)", "!=", gen_1, mt0);
4991 37569 : n = lg(mt)-1;
4992 37569 : al = cgetg(12, t_VEC);
4993 262983 : for (i=1; i<=6; i++) gel(al,i) = gen_0;
4994 37569 : gel(al,7) = matid(n);
4995 37569 : gel(al,8) = matid(n);
4996 37569 : gel(al,9) = mt;
4997 37569 : gel(al,10) = p? p: gen_0;
4998 37569 : gel(al,11)= algtracebasis(al);
4999 37569 : return al;
5000 : }
5001 : GEN
5002 4200 : algtableinit(GEN mt0, GEN p)
5003 : {
5004 4200 : pari_sp av = avma;
5005 4200 : if (p)
5006 : {
5007 4074 : if (typ(p) != t_INT) pari_err_TYPE("algtableinit",p);
5008 4067 : if (signe(p) && !BPSW_psp(p)) pari_err_PRIME("algtableinit",p);
5009 : }
5010 4179 : return gerepilecopy(av, algtableinit_i(mt0, p));
5011 : }
5012 :
5013 : /** REPRESENTATIONS OF GROUPS **/
5014 :
5015 : static GEN
5016 294 : list_to_regular_rep(GEN elts, long n)
5017 : {
5018 : GEN reg, elts2, g;
5019 : long i,j;
5020 294 : elts = shallowcopy(elts);
5021 294 : gen_sort_inplace(elts, (void*)&vecsmall_lexcmp, &cmp_nodata, NULL);
5022 294 : reg = cgetg(n+1, t_VEC);
5023 294 : gel(reg,1) = identity_perm(n);
5024 3857 : for (i=2; i<=n; i++) {
5025 3563 : g = perm_inv(gel(elts,i));
5026 3563 : elts2 = cgetg(n+1, t_VEC);
5027 74543 : for (j=1; j<=n; j++) gel(elts2,j) = perm_mul(g,gel(elts,j));
5028 3563 : gen_sort_inplace(elts2, (void*)&vecsmall_lexcmp, &cmp_nodata, &gel(reg,i));
5029 : }
5030 294 : return reg;
5031 : }
5032 :
5033 : static GEN
5034 3857 : matrix_perm(GEN perm, long n)
5035 : {
5036 : GEN m;
5037 : long j;
5038 3857 : m = cgetg(n+1, t_MAT);
5039 78694 : for (j=1; j<=n; j++) {
5040 74837 : gel(m,j) = col_ei(n,perm[j]);
5041 : }
5042 3857 : return m;
5043 : }
5044 :
5045 : GEN
5046 847 : conjclasses_algcenter(GEN cc, GEN p)
5047 : {
5048 847 : GEN mt, elts = gel(cc,1), conjclass = gel(cc,2), rep = gel(cc,3), card;
5049 847 : long i, nbcl = lg(rep)-1, n = lg(elts)-1;
5050 : pari_sp av;
5051 :
5052 847 : card = zero_Flv(nbcl);
5053 14819 : for (i=1; i<=n; i++) card[conjclass[i]]++;
5054 :
5055 : /* multiplication table of the center of Z[G] (class functions) */
5056 847 : mt = cgetg(nbcl+1,t_VEC);
5057 7217 : for (i=1;i<=nbcl;i++) gel(mt,i) = zero_Flm_copy(nbcl,nbcl);
5058 847 : av = avma;
5059 7217 : for (i=1;i<=nbcl;i++)
5060 : {
5061 6370 : GEN xi = gel(elts,rep[i]), mi = gel(mt,i);
5062 : long j,k;
5063 132244 : for (j=1;j<=n;j++)
5064 : {
5065 125874 : GEN xj = gel(elts,j);
5066 125874 : k = vecsearch(elts, perm_mul(xi,xj), NULL);
5067 125874 : ucoeff(mi, conjclass[k], conjclass[j])++;
5068 : }
5069 70238 : for (k=1; k<=nbcl; k++)
5070 852362 : for (j=1; j<=nbcl; j++)
5071 : {
5072 788494 : ucoeff(mi,k,j) *= card[i];
5073 788494 : ucoeff(mi,k,j) /= card[k];
5074 : }
5075 6370 : set_avma(av);
5076 : }
5077 7217 : for (i=1;i<=nbcl;i++) gel(mt,i) = Flm_to_ZM(gel(mt,i));
5078 847 : return algtableinit_i(mt,p);
5079 : }
5080 :
5081 : GEN
5082 329 : alggroupcenter(GEN G, GEN p, GEN *pcc)
5083 : {
5084 329 : pari_sp av = avma;
5085 329 : GEN cc = group_to_cc(G), al = conjclasses_algcenter(cc, p);
5086 315 : if (!pcc) return gerepilecopy(av,al);
5087 7 : *pcc = cc; return gc_all(av, 2, &al, pcc);
5088 : }
5089 :
5090 : static GEN
5091 294 : groupelts_algebra(GEN elts, GEN p)
5092 : {
5093 294 : pari_sp av = avma;
5094 : GEN mt;
5095 294 : long i, n = lg(elts)-1;
5096 294 : elts = list_to_regular_rep(elts,n);
5097 294 : mt = cgetg(n+1, t_VEC);
5098 4151 : for (i=1; i<=n; i++) gel(mt,i) = matrix_perm(gel(elts,i),n);
5099 294 : return gerepilecopy(av, algtableinit_i(mt,p));
5100 : }
5101 :
5102 : GEN
5103 329 : alggroup(GEN gal, GEN p)
5104 : {
5105 329 : GEN elts = checkgroupelts(gal);
5106 294 : return groupelts_algebra(elts, p);
5107 : }
5108 :
5109 : /** MAXIMAL ORDER **/
5110 :
5111 : static GEN
5112 55305 : mattocol(GEN M, long n)
5113 : {
5114 55305 : GEN C = cgetg(n*n+1, t_COL);
5115 : long i,j,ic;
5116 55305 : ic = 1;
5117 1121740 : for (i=1; i<=n; i++)
5118 27481792 : for (j=1; j<=n; j++, ic++) gel(C,ic) = gcoeff(M,i,j);
5119 55305 : return C;
5120 : }
5121 :
5122 : /* Ip is a lift of a left O/pO-ideal where O is the integral basis of al */
5123 : static GEN
5124 4196 : algleftordermodp(GEN al, GEN Ip, GEN p)
5125 : {
5126 4196 : pari_sp av = avma;
5127 : GEN I, Ii, M, mt, K, imi, p2;
5128 : long n, i;
5129 4196 : n = alg_get_absdim(al);
5130 4196 : mt = alg_get_multable(al);
5131 4196 : p2 = sqri(p);
5132 :
5133 4196 : I = ZM_hnfmodid(Ip, p);
5134 4196 : Ii = ZM_inv(I,NULL);
5135 :
5136 4196 : M = cgetg(n+1, t_MAT);
5137 59501 : for (i=1; i<=n; i++) {
5138 55305 : imi = FpM_mul(Ii, FpM_mul(gel(mt,i), I, p2), p2);
5139 55305 : imi = ZM_Z_divexact(imi, p);
5140 55305 : gel(M,i) = mattocol(imi, n);
5141 : }
5142 4196 : K = FpM_ker(M, p);
5143 4196 : if (lg(K)==1) { set_avma(av); return matid(n); }
5144 1766 : K = ZM_hnfmodid(K,p);
5145 :
5146 1766 : return gerepileupto(av, ZM_Z_div(K,p));
5147 : }
5148 :
5149 : static GEN
5150 5240 : alg_ordermodp(GEN al, GEN p)
5151 : {
5152 : GEN alp;
5153 5240 : long i, N = alg_get_absdim(al);
5154 5240 : alp = cgetg(12, t_VEC);
5155 47160 : for (i=1; i<=8; i++) gel(alp,i) = gen_0;
5156 5240 : gel(alp,9) = cgetg(N+1, t_VEC);
5157 63118 : for (i=1; i<=N; i++) gmael(alp,9,i) = FpM_red(gmael(al,9,i), p);
5158 5240 : gel(alp,10) = p;
5159 5240 : gel(alp,11) = cgetg(N+1, t_VEC);
5160 63118 : for (i=1; i<=N; i++) gmael(alp,11,i) = Fp_red(gmael(al,11,i), p);
5161 :
5162 5240 : return alp;
5163 : }
5164 :
5165 : static GEN
5166 3080 : algpradical_i(GEN al, GEN p, GEN zprad, GEN projs)
5167 : {
5168 3080 : pari_sp av = avma;
5169 3080 : GEN alp = alg_ordermodp(al, p), liftrad, projrad, alq, alrad, res, Lalp, radq;
5170 : long i;
5171 3080 : if (lg(zprad)==1) {
5172 2057 : liftrad = NULL;
5173 2057 : projrad = NULL;
5174 : }
5175 : else {
5176 1023 : alq = alg_quotient(alp, zprad, 1);
5177 1023 : alp = gel(alq,1);
5178 1023 : projrad = gel(alq,2);
5179 1023 : liftrad = gel(alq,3);
5180 : }
5181 :
5182 3080 : if (projs) {
5183 606 : if (projrad) {
5184 40 : projs = gcopy(projs);
5185 120 : for (i=1; i<lg(projs); i++)
5186 80 : gel(projs,i) = FpM_FpC_mul(projrad, gel(projs,i), p);
5187 : }
5188 606 : Lalp = alg_centralproj(alp, projs, 1);
5189 :
5190 606 : alrad = cgetg(lg(Lalp),t_VEC);
5191 2346 : for (i=1; i<lg(Lalp); i++) {
5192 1740 : alq = gel(Lalp,i);
5193 1740 : radq = algradical(gel(alq,1));
5194 1740 : if (gequal0(radq))
5195 1096 : gel(alrad,i) = cgetg(1,t_MAT);
5196 : else {
5197 644 : radq = FpM_mul(gel(alq,3),radq,p);
5198 644 : gel(alrad,i) = radq;
5199 : }
5200 : }
5201 606 : alrad = shallowmatconcat(alrad);
5202 606 : alrad = FpM_image(alrad,p);
5203 : }
5204 2474 : else alrad = algradical(alp);
5205 :
5206 3080 : if (!gequal0(alrad)) {
5207 2305 : if (liftrad) alrad = FpM_mul(liftrad, alrad, p);
5208 2305 : res = shallowmatconcat(mkvec2(alrad, zprad));
5209 2305 : res = FpM_image(res,p);
5210 : }
5211 775 : else res = lg(zprad)==1 ? gen_0 : zprad;
5212 3080 : return gerepilecopy(av, res);
5213 : }
5214 :
5215 : static GEN
5216 2160 : algpdecompose0(GEN al, GEN prad, GEN p, GEN projs)
5217 : {
5218 2160 : pari_sp av = avma;
5219 2160 : GEN alp, quo, ss, liftm = NULL, projm = NULL, dec, res, I, Lss, deci;
5220 : long i, j;
5221 :
5222 2160 : alp = alg_ordermodp(al, p);
5223 2160 : if (!gequal0(prad)) {
5224 1664 : quo = alg_quotient(alp, prad, 1);
5225 1664 : ss = gel(quo,1);
5226 1664 : projm = gel(quo,2);
5227 1664 : liftm = gel(quo,3);
5228 : }
5229 496 : else ss = alp;
5230 :
5231 2160 : if (projs) {
5232 536 : if (projm) {
5233 1395 : for (i=1; i<lg(projs); i++)
5234 1026 : gel(projs,i) = FpM_FpC_mul(projm, gel(projs,i), p);
5235 : }
5236 536 : Lss = alg_centralproj(ss, projs, 1);
5237 :
5238 536 : dec = cgetg(lg(Lss),t_VEC);
5239 2115 : for (i=1; i<lg(Lss); i++) {
5240 1579 : gel(dec,i) = algsimpledec_ss(gmael(Lss,i,1), 1);
5241 1579 : deci = gel(dec,i);
5242 3522 : for (j=1; j<lg(deci); j++)
5243 1943 : gmael(deci,j,3) = FpM_mul(gmael(Lss,i,3), gmael(deci,j,3), p);
5244 : }
5245 536 : dec = shallowconcat1(dec);
5246 : }
5247 1624 : else dec = algsimpledec_ss(ss,1);
5248 :
5249 2160 : res = cgetg(lg(dec),t_VEC);
5250 6434 : for (i=1; i<lg(dec); i++) {
5251 4274 : I = gmael(dec,i,3);
5252 4274 : if (liftm) I = FpM_mul(liftm,I,p);
5253 4274 : I = shallowmatconcat(mkvec2(I,prad));
5254 4274 : gel(res,i) = I;
5255 : }
5256 :
5257 2160 : return gerepilecopy(av, res);
5258 : }
5259 :
5260 : /* finds a nontrivial ideal of O/prad or gen_0 if there is none. */
5261 : static GEN
5262 846 : algpdecompose_i(GEN al, GEN p, GEN zprad, GEN projs)
5263 : {
5264 846 : pari_sp av = avma;
5265 846 : GEN prad = algpradical_i(al,p,zprad,projs);
5266 846 : return gerepileupto(av, algpdecompose0(al, prad, p, projs));
5267 : }
5268 :
5269 : /* ord is assumed to be in hnf wrt the integral basis of al. */
5270 : /* assumes that alg_get_invbasis(al) is integral. */
5271 : static GEN
5272 1766 : alg_change_overorder_shallow(GEN al, GEN ord)
5273 : {
5274 : GEN al2, mt, iord, mtx, den, den2, div;
5275 : long i, n;
5276 1766 : n = alg_get_absdim(al);
5277 :
5278 1766 : iord = QM_inv(ord);
5279 1766 : al2 = shallowcopy(al);
5280 1766 : ord = Q_remove_denom(ord,&den);
5281 :
5282 1766 : gel(al2,7) = Q_remove_denom(gel(al,7), &den2);
5283 1766 : if (den2) div = mulii(den,den2);
5284 665 : else div = den;
5285 1766 : gel(al2,7) = ZM_Z_div(ZM_mul(gel(al2,7), ord), div);
5286 :
5287 1766 : gel(al2,8) = ZM_mul(iord, gel(al,8));
5288 :
5289 1766 : mt = cgetg(n+1,t_VEC);
5290 1766 : gel(mt,1) = matid(n);
5291 1766 : div = sqri(den);
5292 19832 : for (i=2; i<=n; i++) {
5293 18066 : mtx = algbasismultable(al,gel(ord,i));
5294 18066 : gel(mt,i) = ZM_mul(iord, ZM_mul(mtx, ord));
5295 18066 : gel(mt,i) = ZM_Z_divexact(gel(mt,i), div);
5296 : }
5297 1766 : gel(al2,9) = mt;
5298 :
5299 1766 : gel(al2,11) = algtracebasis(al2);
5300 :
5301 1766 : return al2;
5302 : }
5303 :
5304 : static GEN
5305 11011 : algfromcenter(GEN al, GEN x)
5306 : {
5307 11011 : GEN nf = alg_get_center(al);
5308 : long n;
5309 11011 : switch(alg_type(al)) {
5310 9877 : case al_CYCLIC:
5311 9877 : n = alg_get_degree(al);
5312 9877 : break;
5313 1134 : case al_CSA:
5314 1134 : n = alg_get_dim(al);
5315 1134 : break;
5316 : default: return NULL; /*LCOV_EXCL_LINE*/
5317 : }
5318 11011 : return algalgtobasis(al, scalarcol(basistoalg(nf, x), n));
5319 : }
5320 :
5321 : /* x is an ideal of the center in hnf form */
5322 : static GEN
5323 3080 : algfromcenterhnf(GEN al, GEN x)
5324 : {
5325 : GEN res;
5326 : long i;
5327 3080 : res = cgetg(lg(x), t_MAT);
5328 9877 : for (i=1; i<lg(x); i++) gel(res,i) = algfromcenter(al, gel(x,i));
5329 3080 : return res;
5330 : }
5331 :
5332 : /* assumes al is CSA or CYCLIC */
5333 : static GEN
5334 1314 : algcenter_precompute(GEN al, GEN p)
5335 : {
5336 1314 : GEN fa, pdec, nfprad, projs, nf = alg_get_center(al);
5337 : long i, np;
5338 :
5339 1314 : pdec = idealprimedec(nf, p);
5340 1314 : settyp(pdec, t_COL);
5341 1314 : np = lg(pdec)-1;
5342 1314 : fa = mkmat2(pdec, const_col(np, gen_1));
5343 1314 : if (dvdii(nf_get_disc(nf), p))
5344 342 : nfprad = idealprodprime(nf, pdec);
5345 : else
5346 972 : nfprad = scalarmat_shallow(p, nf_get_degree(nf));
5347 1314 : fa = idealchineseinit(nf, fa);
5348 1314 : projs = cgetg(np+1, t_VEC);
5349 3160 : for (i=1; i<=np; i++) gel(projs, i) = idealchinese(nf, fa, vec_ei(np,i));
5350 1314 : return mkvec2(nfprad, projs);
5351 : }
5352 :
5353 : static GEN
5354 3080 : algcenter_prad(GEN al, GEN p, GEN pre)
5355 : {
5356 : GEN nfprad, zprad, mtprad;
5357 : long i;
5358 3080 : nfprad = gel(pre,1);
5359 3080 : zprad = algfromcenterhnf(al, nfprad);
5360 3080 : zprad = FpM_image(zprad, p);
5361 3080 : mtprad = cgetg(lg(zprad), t_VEC);
5362 4672 : for (i=1; i<lg(zprad); i++) gel(mtprad, i) = algbasismultable(al, gel(zprad,i));
5363 3080 : mtprad = shallowmatconcat(mtprad);
5364 3080 : zprad = FpM_image(mtprad, p);
5365 3080 : return zprad;
5366 : }
5367 :
5368 : static GEN
5369 3080 : algcenter_p_projs(GEN al, GEN p, GEN pre)
5370 : {
5371 : GEN projs, zprojs;
5372 : long i;
5373 3080 : projs = gel(pre,2);
5374 3080 : zprojs = cgetg(lg(projs), t_VEC);
5375 7294 : for (i=1; i<lg(projs); i++) gel(zprojs,i) = FpC_red(algfromcenter(al, gel(projs,i)),p);
5376 3080 : return zprojs;
5377 : }
5378 :
5379 : /* al is assumed to be simple */
5380 : static GEN
5381 1314 : alg_pmaximal(GEN al, GEN p)
5382 : {
5383 : pari_sp av;
5384 1314 : long n = alg_get_absdim(al);
5385 1314 : GEN id = matid(n), al2 = al, prad, lord = gen_0, dec, zprad, projs, pre;
5386 :
5387 1314 : dbg_printf(0)("Round 2 (noncommutative) at p=%Ps, dim=%d\n", p, n);
5388 1314 : pre = algcenter_precompute(al,p); av = avma;
5389 : while (1) {
5390 2234 : zprad = algcenter_prad(al2, p, pre);
5391 2234 : projs = algcenter_p_projs(al2, p, pre);
5392 2234 : if (lg(projs) == 2) projs = NULL;
5393 2234 : prad = algpradical_i(al2,p,zprad,projs);
5394 2234 : if (typ(prad) == t_INT) break;
5395 2213 : lord = algleftordermodp(al2,prad,p);
5396 2213 : if (!cmp_universal(lord,id)) break;
5397 920 : al2 = gerepilecopy(av, alg_change_overorder_shallow(al2,lord));
5398 : }
5399 :
5400 1314 : dec = algpdecompose0(al2,prad,p,projs); av = avma;
5401 2160 : while (lg(dec) > 2) {
5402 : long i;
5403 2282 : for (i = 1; i < lg(dec); i++) {
5404 1983 : GEN I = gel(dec,i);
5405 1983 : lord = algleftordermodp(al2,I,p);
5406 1983 : if (cmp_universal(lord,id)) break;
5407 : }
5408 1145 : if (i==lg(dec)) break;
5409 846 : al2 = gerepilecopy(av, alg_change_overorder_shallow(al2,lord));
5410 846 : zprad = algcenter_prad(al2, p, pre);
5411 846 : projs = algcenter_p_projs(al2, p, pre);
5412 846 : if (lg(projs) == 2) projs = NULL;
5413 846 : dec = algpdecompose_i(al2,p,zprad,projs);
5414 : }
5415 1314 : return al2;
5416 : }
5417 :
5418 : static GEN
5419 5796 : algtracematrix(GEN al)
5420 : {
5421 : GEN M, mt;
5422 : long n, i, j;
5423 5796 : n = alg_get_absdim(al);
5424 5796 : mt = alg_get_multable(al);
5425 5796 : M = cgetg(n+1, t_MAT);
5426 44795 : for (i=1; i<=n; i++)
5427 : {
5428 38999 : gel(M,i) = cgetg(n+1,t_MAT);
5429 274835 : for (j=1; j<=i; j++)
5430 235836 : gcoeff(M,j,i) = gcoeff(M,i,j) = algabstrace(al,gmael(mt,i,j));
5431 : }
5432 5796 : return M;
5433 : }
5434 : static GEN
5435 140 : algdisc_i(GEN al) { return ZM_det(algtracematrix(al)); }
5436 : GEN
5437 28 : algdisc(GEN al)
5438 : {
5439 28 : pari_sp av = avma;
5440 28 : checkalg(al);
5441 28 : if (alg_type(al) == al_REAL) pari_err_TYPE("algdisc [real algebra]", al);
5442 7 : return gerepileuptoint(av, algdisc_i(al));
5443 : }
5444 : static GEN
5445 133 : alg_maximal(GEN al)
5446 : {
5447 133 : GEN fa = absZ_factor(algdisc_i(al));
5448 133 : return alg_maximal_primes(al, gel(fa,1));
5449 : }
5450 :
5451 : /** LATTICES **/
5452 :
5453 : /*
5454 : Convention: lattice = [I,t] representing t*I, where
5455 : - I integral nonsingular upper-triangular matrix representing a lattice over
5456 : the integral basis of the algebra, and
5457 : - t>0 either an integer or a rational number.
5458 :
5459 : Recommended and returned by the functions below:
5460 : - I HNF and primitive
5461 : */
5462 :
5463 : /* TODO use hnfmodid whenever possible using a*O <= I <= O
5464 : * for instance a = ZM_det_triangular(I) */
5465 :
5466 : static GEN
5467 63343 : primlat(GEN lat)
5468 : {
5469 : GEN m, t, c;
5470 63343 : m = alglat_get_primbasis(lat);
5471 63343 : t = alglat_get_scalar(lat);
5472 63343 : m = Q_primitive_part(m,&c);
5473 63343 : if (c) return mkvec2(m,gmul(t,c));
5474 53760 : return lat;
5475 : }
5476 :
5477 : /* assumes the lattice contains d * integral basis, d=0 allowed */
5478 : GEN
5479 51072 : alglathnf(GEN al, GEN m, GEN d)
5480 : {
5481 51072 : pari_sp av = avma;
5482 : long N,i,j;
5483 : GEN m2, c;
5484 51072 : checkalg(al);
5485 51072 : if (alg_type(al) == al_REAL) pari_err_TYPE("alglathnf [real algebra]", al);
5486 51065 : N = alg_get_absdim(al);
5487 51065 : if (!d) d = gen_0;
5488 51065 : if (typ(m) == t_VEC) m = matconcat(m);
5489 51065 : if (typ(m) == t_COL) m = algleftmultable(al,m);
5490 51065 : if (typ(m) != t_MAT) pari_err_TYPE("alglathnf",m);
5491 51058 : if (typ(d) != t_FRAC && typ(d) != t_INT) pari_err_TYPE("alglathnf",d);
5492 51058 : if (lg(m)-1 < N || lg(gel(m,1))-1 != N) pari_err_DIM("alglathnf");
5493 459242 : for (i=1; i<=N; i++)
5494 6820758 : for (j=1; j<lg(m); j++)
5495 6412546 : if (typ(gcoeff(m,i,j)) != t_FRAC && typ(gcoeff(m,i,j)) != t_INT)
5496 7 : pari_err_TYPE("alglathnf", gcoeff(m,i,j));
5497 51023 : m2 = Q_primitive_part(m,&c);
5498 51023 : if (!c) c = gen_1;
5499 51023 : if (!signe(d)) d = detint(m2);
5500 45593 : else d = gdiv(d,c); /* should be an integer */
5501 51023 : if (!signe(d)) pari_err_INV("alglathnf [m does not have full rank]", m2);
5502 51009 : m2 = ZM_hnfmodid(m2,d);
5503 51009 : return gerepilecopy(av, mkvec2(m2,c));
5504 : }
5505 :
5506 : static GEN
5507 10689 : prepare_multipliers(GEN *a, GEN *b)
5508 : {
5509 : GEN na, nb, da, db, d;
5510 10689 : na = numer_i(*a); da = denom_i(*a);
5511 10689 : nb = numer_i(*b); db = denom_i(*b);
5512 10689 : na = mulii(na,db);
5513 10689 : nb = mulii(nb,da);
5514 10689 : d = gcdii(na,nb);
5515 10689 : *a = diviiexact(na,d);
5516 10689 : *b = diviiexact(nb,d);
5517 10689 : return gdiv(d, mulii(da,db));
5518 : }
5519 :
5520 : static GEN
5521 10689 : prepare_lat(GEN m1, GEN t1, GEN m2, GEN t2)
5522 : {
5523 10689 : GEN d = prepare_multipliers(&t1, &t2);
5524 10689 : m1 = ZM_Z_mul(m1,t1);
5525 10689 : m2 = ZM_Z_mul(m2,t2);
5526 10689 : return mkvec3(m1,m2,d);
5527 : }
5528 :
5529 : static GEN
5530 10703 : alglataddinter(GEN al, GEN lat1, GEN lat2, GEN *sum, GEN *inter)
5531 : {
5532 : GEN d, m1, m2, t1, t2, M, prep, d1, d2, ds, di, K;
5533 10703 : checkalg(al);
5534 10703 : if (alg_type(al) == al_REAL)
5535 14 : pari_err_TYPE("alglataddinter [real algebra]", al);
5536 10689 : checklat(al,lat1);
5537 10689 : checklat(al,lat2);
5538 :
5539 10689 : m1 = alglat_get_primbasis(lat1);
5540 10689 : t1 = alglat_get_scalar(lat1);
5541 10689 : m2 = alglat_get_primbasis(lat2);
5542 10689 : t2 = alglat_get_scalar(lat2);
5543 10689 : prep = prepare_lat(m1, t1, m2, t2);
5544 10689 : m1 = gel(prep,1);
5545 10689 : m2 = gel(prep,2);
5546 10689 : d = gel(prep,3);
5547 10689 : M = matconcat(mkvec2(m1,m2));
5548 10689 : d1 = ZM_det_triangular(m1);
5549 10689 : d2 = ZM_det_triangular(m2);
5550 10689 : ds = gcdii(d1,d2);
5551 10689 : if (inter)
5552 : {
5553 7112 : di = diviiexact(mulii(d1,d2),ds);
5554 7112 : K = matkermod(M,di,sum);
5555 7112 : K = rowslice(K,1,lg(m1));
5556 7112 : *inter = hnfmodid(FpM_mul(m1,K,di),di);
5557 7112 : if (sum) *sum = hnfmodid(*sum,ds);
5558 : }
5559 3577 : else *sum = hnfmodid(M,ds);
5560 10689 : return d;
5561 : }
5562 :
5563 : GEN
5564 3605 : alglatinter(GEN al, GEN lat1, GEN lat2, GEN* psum)
5565 : {
5566 3605 : pari_sp av = avma;
5567 : GEN inter, d;
5568 3605 : d = alglataddinter(al, lat1, lat2, psum, &inter);
5569 3598 : inter = primlat(mkvec2(inter, d));
5570 3598 : if (!psum) return gerepilecopy(av, inter);
5571 14 : *psum = primlat(mkvec2(*psum,d));
5572 14 : return gc_all(av, 2, &inter, psum);
5573 : }
5574 :
5575 : GEN
5576 7098 : alglatadd(GEN al, GEN lat1, GEN lat2, GEN* pinter)
5577 : {
5578 7098 : pari_sp av = avma;
5579 : GEN sum, d;
5580 7098 : d = alglataddinter(al, lat1, lat2, &sum, pinter);
5581 7091 : sum = primlat(mkvec2(sum, d));
5582 7091 : if (!pinter) return gerepilecopy(av, sum);
5583 3514 : *pinter = primlat(mkvec2(*pinter,d));
5584 3514 : return gc_all(av, 2, &sum, pinter);
5585 : }
5586 :
5587 : /* TODO version that returns the quotient as abelian group? */
5588 : /* return matrices to convert coordinates from one to other? */
5589 : int
5590 31556 : alglatsubset(GEN al, GEN lat1, GEN lat2, GEN* pindex)
5591 : {
5592 31556 : pari_sp av = avma;
5593 : int res;
5594 : GEN m1, m2, m2i, m, t;
5595 31556 : checkalg(al);
5596 31556 : if (alg_type(al) == al_REAL) pari_err_TYPE("alglatsubset [real algebra]", al);
5597 31549 : checklat(al,lat1);
5598 31549 : checklat(al,lat2);
5599 31549 : m1 = alglat_get_primbasis(lat1);
5600 31549 : m2 = alglat_get_primbasis(lat2);
5601 31549 : m2i = RgM_inv_upper(m2);
5602 31549 : t = gdiv(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
5603 31549 : m = RgM_Rg_mul(RgM_mul(m2i,m1), t);
5604 31549 : res = RgM_is_ZM(m);
5605 31549 : if (!res || !pindex) return gc_int(av, res);
5606 1757 : *pindex = gerepileuptoint(av, mpabs(ZM_det_triangular(m)));
5607 1757 : return 1;
5608 : }
5609 :
5610 : GEN
5611 5271 : alglatindex(GEN al, GEN lat1, GEN lat2)
5612 : {
5613 5271 : pari_sp av = avma;
5614 : long N;
5615 : GEN res;
5616 5271 : checkalg(al);
5617 5271 : if (alg_type(al) == al_REAL) pari_err_TYPE("alglatindex [real algebra]", al);
5618 5264 : checklat(al,lat1);
5619 5264 : checklat(al,lat2);
5620 5264 : N = alg_get_absdim(al);
5621 5264 : res = alglat_get_scalar(lat1);
5622 5264 : res = gdiv(res, alglat_get_scalar(lat2));
5623 5264 : res = gpowgs(res, N);
5624 5264 : res = gmul(res,RgM_det_triangular(alglat_get_primbasis(lat1)));
5625 5264 : res = gdiv(res, RgM_det_triangular(alglat_get_primbasis(lat2)));
5626 5264 : res = gabs(res,0);
5627 5264 : return gerepilecopy(av, res);
5628 : }
5629 :
5630 : GEN
5631 45612 : alglatmul(GEN al, GEN lat1, GEN lat2)
5632 : {
5633 45612 : pari_sp av = avma;
5634 : long N,i;
5635 : GEN m1, m2, m, V, lat, t, d, dp;
5636 45612 : checkalg(al);
5637 45612 : if (alg_type(al) == al_REAL) pari_err_TYPE("alglatmul [real algebra]", al);
5638 45605 : if (typ(lat1)==t_COL)
5639 : {
5640 19292 : if (typ(lat2)==t_COL)
5641 7 : pari_err_TYPE("alglatmul [one of lat1, lat2 has to be a lattice]", lat2);
5642 19285 : checklat(al,lat2);
5643 19285 : lat1 = Q_remove_denom(lat1,&d);
5644 19285 : m = algbasismultable(al,lat1);
5645 19285 : m2 = alglat_get_primbasis(lat2);
5646 19285 : dp = mulii(detint(m),ZM_det_triangular(m2));
5647 19285 : m = ZM_mul(m,m2);
5648 19285 : t = alglat_get_scalar(lat2);
5649 19285 : if (d) t = gdiv(t,d);
5650 : }
5651 : else /* typ(lat1)!=t_COL */
5652 : {
5653 26313 : checklat(al,lat1);
5654 26313 : if (typ(lat2)==t_COL)
5655 : {
5656 19285 : lat2 = Q_remove_denom(lat2,&d);
5657 19285 : m = algbasisrightmultable(al,lat2);
5658 19285 : m1 = alglat_get_primbasis(lat1);
5659 19285 : dp = mulii(detint(m),ZM_det_triangular(m1));
5660 19285 : m = ZM_mul(m,m1);
5661 19285 : t = alglat_get_scalar(lat1);
5662 19285 : if (d) t = gdiv(t,d);
5663 : }
5664 : else /* typ(lat2)!=t_COL */
5665 : {
5666 7028 : checklat(al,lat2);
5667 7021 : N = alg_get_absdim(al);
5668 7021 : m1 = alglat_get_primbasis(lat1);
5669 7021 : m2 = alglat_get_primbasis(lat2);
5670 7021 : dp = mulii(ZM_det_triangular(m1), ZM_det_triangular(m2));
5671 7021 : V = cgetg(N+1,t_VEC);
5672 63189 : for (i=1; i<=N; i++) {
5673 56168 : gel(V,i) = algbasismultable(al,gel(m1,i));
5674 56168 : gel(V,i) = ZM_mul(gel(V,i),m2);
5675 : }
5676 7021 : m = matconcat(V);
5677 7021 : t = gmul(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
5678 : }
5679 : }
5680 :
5681 45591 : lat = alglathnf(al,m,dp);
5682 45591 : gel(lat,2) = gmul(alglat_get_scalar(lat), t);
5683 45591 : lat = primlat(lat);
5684 45591 : return gerepilecopy(av, lat);
5685 : }
5686 :
5687 : int
5688 17528 : alglatcontains(GEN al, GEN lat, GEN x, GEN *ptc)
5689 : {
5690 17528 : pari_sp av = avma;
5691 : GEN m, t, sol;
5692 17528 : checkalg(al);
5693 17528 : if (alg_type(al) == al_REAL)
5694 7 : pari_err_TYPE("alglatcontains [real algebra]", al);
5695 17521 : checklat(al,lat);
5696 17521 : m = alglat_get_primbasis(lat);
5697 17521 : t = alglat_get_scalar(lat);
5698 17521 : x = RgC_Rg_div(x,t);
5699 17521 : if (!RgV_is_ZV(x)) return gc_bool(av,0);
5700 17521 : sol = hnf_solve(m,x);
5701 17521 : if (!sol) return gc_bool(av,0);
5702 8771 : if (!ptc) return gc_bool(av,1);
5703 8764 : *ptc = gerepilecopy(av, sol); return 1;
5704 : }
5705 :
5706 : GEN
5707 8778 : alglatelement(GEN al, GEN lat, GEN c)
5708 : {
5709 8778 : pari_sp av = avma;
5710 : GEN res;
5711 8778 : checkalg(al);
5712 8778 : if (alg_type(al) == al_REAL)
5713 7 : pari_err_TYPE("alglatelement [real algebra]", al);
5714 8771 : checklat(al,lat);
5715 8771 : if (typ(c)!=t_COL) pari_err_TYPE("alglatelement", c);
5716 8764 : res = ZM_ZC_mul(alglat_get_primbasis(lat),c);
5717 8764 : res = RgC_Rg_mul(res, alglat_get_scalar(lat));
5718 8764 : return gerepilecopy(av,res);
5719 : }
5720 :
5721 : /* idem QM_invimZ, knowing result is contained in 1/c*Z^n */
5722 : static GEN
5723 3535 : QM_invimZ_mod(GEN m, GEN c)
5724 : {
5725 : GEN d, m0, K;
5726 3535 : m0 = Q_remove_denom(m, &d);
5727 3535 : if (d) d = mulii(d,c);
5728 21 : else d = c;
5729 3535 : K = matkermod(m0, d, NULL);
5730 3535 : if (lg(K)==1) K = scalarmat(d, lg(m)-1);
5731 3521 : else K = hnfmodid(K, d);
5732 3535 : return RgM_Rg_div(K,c);
5733 : }
5734 :
5735 : /* If m is injective, computes a Z-basis of the submodule of elements whose
5736 : * image under m is integral */
5737 : static GEN
5738 14 : QM_invimZ(GEN m)
5739 : {
5740 14 : return RgM_invimage(m, QM_ImQ_hnf(m));
5741 : }
5742 :
5743 : /* An isomorphism of R-modules M_{m,n}(R) -> R^{m*n} */
5744 : static GEN
5745 28322 : mat2col(GEN M, long m, long n)
5746 : {
5747 : long i,j,k,p;
5748 : GEN C;
5749 28322 : p = m*n;
5750 28322 : C = cgetg(p+1,t_COL);
5751 254702 : for (i=1,k=1;i<=m;i++)
5752 2036804 : for (j=1;j<=n;j++,k++)
5753 1810424 : gel(C,k) = gcoeff(M,i,j);
5754 28322 : return C;
5755 : }
5756 :
5757 : static GEN
5758 3535 : alglattransporter_i(GEN al, GEN lat1, GEN lat2, long right)
5759 : {
5760 : GEN m1, m2, m2i, M, MT, mt, t1, t2, T, c;
5761 : long N, i;
5762 3535 : N = alg_get_absdim(al);
5763 3535 : m1 = alglat_get_primbasis(lat1);
5764 3535 : m2 = alglat_get_primbasis(lat2);
5765 3535 : m2i = RgM_inv_upper(m2);
5766 3535 : c = detint(m1);
5767 3535 : t1 = alglat_get_scalar(lat1);
5768 3535 : m1 = RgM_Rg_mul(m1,t1);
5769 3535 : t2 = alglat_get_scalar(lat2);
5770 3535 : m2i = RgM_Rg_div(m2i,t2);
5771 :
5772 3535 : MT = right? NULL: alg_get_multable(al);
5773 3535 : M = cgetg(N+1, t_MAT);
5774 31815 : for (i=1; i<=N; i++) {
5775 28280 : if (right) mt = algbasisrightmultable(al, vec_ei(N,i));
5776 14168 : else mt = gel(MT,i);
5777 28280 : mt = RgM_mul(m2i,mt);
5778 28280 : mt = RgM_mul(mt,m1);
5779 28280 : gel(M,i) = mat2col(mt, N, N);
5780 : }
5781 :
5782 3535 : c = gdiv(t2,gmul(c,t1));
5783 3535 : c = denom_i(c);
5784 3535 : T = QM_invimZ_mod(M,c);
5785 3535 : return primlat(mkvec2(T,gen_1));
5786 : }
5787 :
5788 : /*
5789 : { x in al | x*lat1 subset lat2}
5790 : */
5791 : GEN
5792 1778 : alglatlefttransporter(GEN al, GEN lat1, GEN lat2)
5793 : {
5794 1778 : pari_sp av = avma;
5795 1778 : checkalg(al);
5796 1778 : if (alg_type(al) == al_REAL)
5797 7 : pari_err_TYPE("alglatlefttransporter [real algebra]", al);
5798 1771 : checklat(al,lat1);
5799 1771 : checklat(al,lat2);
5800 1771 : return gerepilecopy(av, alglattransporter_i(al,lat1,lat2,0));
5801 : }
5802 :
5803 : /*
5804 : { x in al | lat1*x subset lat2}
5805 : */
5806 : GEN
5807 1771 : alglatrighttransporter(GEN al, GEN lat1, GEN lat2)
5808 : {
5809 1771 : pari_sp av = avma;
5810 1771 : checkalg(al);
5811 1771 : if (alg_type(al) == al_REAL)
5812 7 : pari_err_TYPE("alglatrighttransporter [real algebra]", al);
5813 1764 : checklat(al,lat1);
5814 1764 : checklat(al,lat2);
5815 1764 : return gerepilecopy(av, alglattransporter_i(al,lat1,lat2,1));
5816 : }
5817 :
5818 : GEN
5819 42 : algmakeintegral(GEN mt0, long maps)
5820 : {
5821 42 : pari_sp av = avma;
5822 : long n,i;
5823 : GEN m,P,Pi,mt2,mt;
5824 42 : n = lg(mt0)-1;
5825 42 : mt = check_mt(mt0,NULL);
5826 42 : if (!mt) pari_err_TYPE("algmakeintegral", mt0);
5827 21 : if (isint1(Q_denom(mt0))) {
5828 7 : if (maps) mt = mkvec3(mt,matid(n),matid(n));
5829 7 : return gerepilecopy(av,mt);
5830 : }
5831 14 : dbg_printf(2)(" algmakeintegral: dim=%d, denom=%Ps\n", n, Q_denom(mt0));
5832 14 : m = cgetg(n+1,t_MAT);
5833 56 : for (i=1;i<=n;i++)
5834 42 : gel(m,i) = mat2col(gel(mt,i),n,n);
5835 14 : dbg_printf(2)(" computing order, dims m = %d x %d...\n", nbrows(m), lg(m)-1);
5836 14 : P = QM_invimZ(m);
5837 14 : dbg_printf(2)(" ...done.\n");
5838 14 : P = shallowmatconcat(mkvec2(col_ei(n,1),P));
5839 14 : P = hnf(P);
5840 14 : Pi = RgM_inv(P);
5841 14 : mt2 = change_Rgmultable(mt,P,Pi);
5842 14 : if (maps) mt2 = mkvec3(mt2,Pi,P); /* mt2, mt->mt2, mt2->mt */
5843 14 : return gerepilecopy(av,mt2);
5844 : }
5845 :
5846 : /** ORDERS **/
5847 :
5848 : /** IDEALS **/
5849 :
|