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