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 :
15 : #include "pari.h"
16 : #include "paripriv.h"
17 :
18 : #define DEBUGLEVEL DEBUGLEVEL_mathnf
19 :
20 : /**************************************************************/
21 : /** **/
22 : /** HERMITE NORMAL FORM REDUCTION **/
23 : /** **/
24 : /**************************************************************/
25 : static GEN ZV_hnfgcdext(GEN A);
26 : static GEN
27 21 : hnfallgen(GEN x)
28 : {
29 21 : GEN z = cgetg(3, t_VEC);
30 21 : gel(z,1) = RgM_hnfall(x, (GEN*)(z+2), 1);
31 21 : return z;
32 : }
33 : GEN
34 287 : mathnf0(GEN x, long flag)
35 : {
36 287 : switch(typ(x))
37 : {
38 70 : case t_VEC:
39 70 : if (RgV_is_ZV(x))
40 : switch (flag)
41 : {
42 14 : case 0:
43 14 : if (lg(x) == 1) return cgetg(1, t_MAT);
44 7 : retmkmat(mkcol(ZV_content(x)));
45 21 : case 1:
46 : case 4:
47 21 : return ZV_hnfgcdext(x);
48 : }
49 35 : x = gtomat(x); break;
50 217 : case t_MAT: break;
51 0 : default: pari_err_TYPE("mathnf0",x);
52 : }
53 :
54 252 : switch(flag)
55 : {
56 196 : case 0: case 2: return RgM_is_ZM(x)? ZM_hnf(x): RgM_hnfall(x,NULL,1);
57 35 : case 1: case 3: return RgM_is_ZM(x)? hnfall(x): hnfallgen(x);
58 7 : case 4: RgM_check_ZM(x, "mathnf0"); return hnflll(x);
59 14 : case 5: RgM_check_ZM(x, "mathnf0"); return hnfperm(x);
60 0 : default: pari_err_FLAG("mathnf");
61 : }
62 : return NULL; /* LCOV_EXCL_LINE */
63 : }
64 :
65 : /*******************************************************************/
66 : /* */
67 : /* SPECIAL HNF (FOR INTERNAL USE !!!) */
68 : /* */
69 : /*******************************************************************/
70 : static int
71 7980965 : count(GEN mat, long row, long len, long *firstnonzero)
72 : {
73 7980965 : long j, n = 0;
74 :
75 714423547 : for (j=1; j<=len; j++)
76 : {
77 708156374 : long p = mael(mat,j,row);
78 708156374 : if (p)
79 : {
80 22642683 : if (labs(p)!=1) return -1;
81 20928891 : n++; *firstnonzero=j;
82 : }
83 : }
84 6267173 : return n;
85 : }
86 :
87 : static int
88 409532 : count2(GEN mat, long row, long len)
89 : {
90 : long j;
91 4195834 : for (j=len; j; j--)
92 4071318 : if (labs(mael(mat,j,row)) == 1) return j;
93 124516 : return 0;
94 : }
95 :
96 : static GEN
97 210208 : hnffinal(GEN matgen,GEN perm,GEN* ptdep,GEN* ptB,GEN* ptC)
98 : {
99 : GEN p1,p2,U,H,Hnew,Bnew,Cnew,diagH1;
100 210208 : GEN B = *ptB, C = *ptC, dep = *ptdep, depnew;
101 : pari_sp av;
102 : long i,j,k,s,i1,j1,zc;
103 210208 : long co = lg(C);
104 210208 : long col = lg(matgen)-1;
105 : long lnz, nlze, lig;
106 :
107 210208 : if (col == 0) return matgen;
108 210208 : lnz = nbrows(matgen); /* was called lnz-1 - nr in hnfspec */
109 210208 : nlze = nbrows(dep);
110 210208 : lig = nlze + lnz;
111 : /* H: lnz x lnz [disregarding initial 0 cols], U: col x col */
112 210208 : H = ZM_hnflll(matgen, &U, 0);
113 210208 : H += (lg(H)-1 - lnz); H[0] = evaltyp(t_MAT) | evallg(lnz+1);
114 : /* Only keep the part above the H (above the 0s is 0 since the dep rows
115 : * are dependent from the ones in matgen) */
116 210208 : zc = col - lnz; /* # of 0 columns, correspond to units */
117 210208 : if (nlze) { dep = ZM_mul(dep,U); dep += zc; }
118 :
119 210208 : diagH1 = new_chunk(lnz+1); /* diagH1[i] = 0 iff H[i,i] != 1 (set later) */
120 :
121 210207 : av = avma;
122 210207 : Cnew = cgetg(co, typ(C));
123 210207 : setlg(C, col+1); p1 = gmul(C,U);
124 1861401 : for (j=1; j<=col; j++) gel(Cnew,j) = gel(p1,j);
125 3063989 : for ( ; j<co ; j++) gel(Cnew,j) = gel(C,j);
126 :
127 : /* Clean up B using new H */
128 847853 : for (s=0,i=lnz; i; i--)
129 : {
130 637661 : GEN Di = gel(dep,i), Hi = gel(H,i);
131 637661 : GEN h = gel(Hi,i); /* H[i,i] */
132 637661 : if ( (diagH1[i] = is_pm1(h)) ) { h = NULL; s++; }
133 15478581 : for (j=col+1; j<co; j++)
134 : {
135 14841166 : GEN z = gel(B,j-col);
136 14841166 : p1 = gel(z,i+nlze);
137 14841166 : if (h) p1 = truedivii(p1,h);
138 14839965 : if (!signe(p1)) continue;
139 26294454 : for (k=1; k<=nlze; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Di,k)));
140 130260944 : for ( ; k<=lig; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Hi,k-nlze)));
141 8983837 : gel(Cnew,j) = gsub(gel(Cnew,j), gmul(p1, gel(Cnew,i+zc)));
142 : }
143 637415 : if (gc_needed(av,2))
144 : {
145 741 : if(DEBUGMEM>1) pari_warn(warnmem,"hnffinal, i = %ld",i);
146 741 : gerepileall(av, 2, &Cnew, &B);
147 : }
148 : }
149 210192 : p1 = cgetg(lnz+1,t_VEC); p2 = perm + nlze;
150 847889 : for (i1=0, j1=lnz-s, i=1; i<=lnz; i++) /* push the 1 rows down */
151 637683 : if (diagH1[i])
152 302648 : gel(p1,++j1) = gel(p2,i);
153 : else
154 335035 : gel(p2,++i1) = gel(p2,i);
155 512854 : for (i=i1+1; i<=lnz; i++) gel(p2,i) = gel(p1,i);
156 :
157 : /* s = # extra redundant generators taken from H
158 : * zc col-s co zc = col - lnz
159 : * [ 0 |dep | ] i = nlze + lnz - s = lig - s
160 : * nlze [--------| B' ]
161 : * [ 0 | H' | ] H' = H minus the s rows with a 1 on diagonal
162 : * i [--------|-----] lig-s (= "1-rows")
163 : * [ 0 | Id ]
164 : * [ | ] li */
165 210206 : lig -= s; col -= s; lnz -= s;
166 210206 : Hnew = cgetg(lnz+1,t_MAT);
167 210208 : depnew = cgetg(lnz+1,t_MAT); /* only used if nlze > 0 */
168 210207 : Bnew = cgetg(co-col,t_MAT);
169 210206 : C = shallowcopy(Cnew);
170 847894 : for (j=1,i1=j1=0; j<=lnz+s; j++)
171 : {
172 637687 : GEN z = gel(H,j);
173 637687 : if (diagH1[j])
174 : { /* hit exactly s times */
175 302646 : i1++; C[i1+col] = Cnew[j+zc];
176 302646 : p1 = cgetg(lig+1,t_COL); gel(Bnew,i1) = p1;
177 530698 : for (i=1; i<=nlze; i++) gel(p1,i) = gcoeff(dep,i,j);
178 302646 : p1 += nlze;
179 : }
180 : else
181 : {
182 335041 : j1++; C[j1+zc] = Cnew[j+zc];
183 335041 : p1 = cgetg(lnz+1,t_COL); gel(Hnew,j1) = p1;
184 335041 : depnew[j1] = dep[j];
185 : }
186 2471838 : for (i=k=1; k<=lnz; i++)
187 1834151 : if (!diagH1[i]) p1[k++] = z[i];
188 : }
189 3063925 : for (j=s+1; j<co-col; j++)
190 : {
191 2853730 : GEN z = gel(B,j-s);
192 2853730 : p1 = cgetg(lig+1,t_COL); gel(Bnew,j) = p1;
193 4942116 : for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
194 2853718 : z += nlze; p1 += nlze;
195 12254560 : for (i=k=1; k<=lnz; i++)
196 9400842 : if (!diagH1[i]) gel(p1,k++) = gel(z,i);
197 : }
198 210195 : *ptdep = depnew;
199 210195 : *ptC = C;
200 210195 : *ptB = Bnew; return Hnew;
201 : }
202 :
203 : /* for debugging */
204 : static void
205 0 : p_mat(GEN mat, GEN perm, long k)
206 : {
207 0 : pari_sp av = avma;
208 0 : perm = vecslice(perm, k+1, lg(perm)-1);
209 0 : err_printf("Permutation: %Ps\n",perm);
210 0 : if (DEBUGLEVEL > 6)
211 0 : err_printf("matgen = %Ps\n", zm_to_ZM( rowpermute(mat, perm) ));
212 0 : set_avma(av);
213 0 : }
214 :
215 : static GEN
216 2652659 : col_dup(long l, GEN col)
217 : {
218 2652659 : GEN c = new_chunk(l);
219 2652652 : memcpy(c,col,l * sizeof(long)); return c;
220 : }
221 :
222 : /* permutation giving imagecompl(x') | image(x'), x' = transpose of x */
223 : static GEN
224 210207 : ZM_rowrankprofile(GEN x, long *nlze)
225 : {
226 210207 : pari_sp av = avma;
227 : GEN d, y;
228 : long i, j, k, l, r;
229 :
230 210207 : x = shallowtrans(x); l = lg(x);
231 210208 : (void)new_chunk(l); /* HACK */
232 210208 : d = ZM_pivots(x,&r); set_avma(av);
233 210206 : *nlze = r;
234 210206 : if (!d) return identity_perm(l-1);
235 194606 : y = cgetg(l,t_VECSMALL);
236 839409 : for (i = j = 1, k = r+1; i<l; i++)
237 644804 : if (d[i]) y[k++] = i; else y[j++] = i;
238 194605 : return y;
239 : }
240 :
241 : /* HNF reduce a relation matrix (column operations + row permutation)
242 : ** Input:
243 : ** mat = (li-1) x (co-1) matrix of long
244 : ** C = r x (co-1) matrix of GEN
245 : ** perm= permutation vector (length li-1), indexing the rows of mat: easier
246 : ** to maintain perm than to copy rows. For columns we can do it directly
247 : ** using e.g. swap(mat[i], mat[j])
248 : ** k0 = integer. The k0 first lines of mat are dense, the others are sparse.
249 : ** Output: cf ASCII art in the function body
250 : **
251 : ** row permutations applied to perm
252 : ** column operations applied to C. IN PLACE
253 : **/
254 : GEN
255 133958 : hnfspec_i(GEN mat0, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
256 : {
257 : pari_sp av;
258 : long co, n, s, nlze, lnz, nr, i, j, k, lk0, col, lig, *p;
259 : GEN mat;
260 : GEN p1, p2, matb, matbnew, vmax, matt, T, extramat, B, C, H, dep, permpro;
261 133958 : const long li = lg(perm); /* = lgcols(mat0) */
262 133958 : const long CO = lg(mat0);
263 :
264 133958 : n = 0; /* -Wall */
265 :
266 133958 : C = *ptC; co = CO;
267 133958 : if (co > 300 && co > 1.5 * li)
268 : { /* treat the rest at the end */
269 0 : co = (long)(1.2 * li);
270 0 : setlg(C, co);
271 : }
272 :
273 133958 : if (DEBUGLEVEL>5)
274 : {
275 0 : err_printf("Entering hnfspec\n");
276 0 : p_mat(mat0,perm,0);
277 : }
278 133958 : matt = cgetg(co, t_MAT); /* dense part of mat (top) */
279 133958 : mat = cgetg(co, t_MAT);
280 2786624 : for (j = 1; j < co; j++)
281 : {
282 2652664 : GEN matj = col_dup(li, gel(mat0,j));
283 2652660 : p1 = cgetg(k0+1,t_COL); gel(matt,j) = p1; gel(mat,j) = matj;
284 12337859 : for (i=1; i<=k0; i++) gel(p1,i) = stoi(matj[perm[i]]);
285 : }
286 133960 : av = avma;
287 :
288 133960 : i = lig = li-1; col = co-1; lk0 = k0;
289 133960 : T = (k0 || (lg(C) > 1 && lgcols(C) > 1))? matid(col): NULL;
290 : /* Look for lines with a single nonzero entry, equal to 1 in absolute value */
291 6312757 : while (i > lk0 && col)
292 6178746 : switch( count(mat,perm[i],col,&n) )
293 : {
294 26211 : case 0: /* move zero lines between k0+1 and lk0 */
295 26211 : lk0++; lswap(perm[i], perm[lk0]);
296 26211 : i = lig; continue;
297 :
298 481820 : case 1: /* move trivial generator between lig+1 and li */
299 481820 : lswap(perm[i], perm[lig]);
300 481820 : if (T) swap(gel(T,n), gel(T,col));
301 481820 : swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
302 481820 : if (p[perm[lig]] < 0) /* = -1 */
303 : { /* convert relation -g = 0 to g = 0 */
304 10382641 : for (i=lk0+1; i<lig; i++) p[perm[i]] = -p[perm[i]];
305 435538 : if (T)
306 : {
307 435533 : p1 = gel(T,col);
308 11113417 : for (i=1; ; i++) /* T = permuted identity: single nonzero entry */
309 11113417 : if (signe(gel(p1,i))) { togglesign_safe(&gel(p1,i)); break; }
310 : }
311 : }
312 481838 : lig--; col--; i = lig; continue;
313 :
314 5670748 : default: i--;
315 : }
316 134011 : if (DEBUGLEVEL>5) { err_printf(" after phase1:\n"); p_mat(mat,perm,0); }
317 :
318 : #define absmax(s,z) {long _z; _z = labs(z); if (_z > s) s = _z;}
319 : /* Get rid of all lines containing only 0 and +/- 1, keeping track of column
320 : * operations in T. Leave the rows 1..lk0 alone [up to k0, coefficient
321 : * explosion, between k0+1 and lk0, row is 0] */
322 133960 : s = 0;
323 777626 : while (lig > lk0 && col && s < (long)(HIGHBIT>>1))
324 : {
325 1926913 : for (i=lig; i>lk0; i--)
326 1802353 : if (count(mat,perm[i],col,&n) > 0) break;
327 768341 : if (i == lk0) break;
328 :
329 : /* only 0, +/- 1 entries, at least 2 of them nonzero */
330 643779 : lswap(perm[i], perm[lig]);
331 643779 : swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
332 643779 : if (T) swap(gel(T,n), gel(T,col));
333 643779 : if (p[perm[lig]] < 0)
334 : {
335 7579845 : for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
336 414672 : if (T) ZV_togglesign(gel(T,col));
337 : }
338 20891510 : for (j=1; j<col; j++)
339 : {
340 20247844 : GEN matj = gel(mat,j);
341 : long t;
342 20247844 : if (! (t = matj[perm[lig]]) ) continue;
343 1514714 : if (t == 1) {
344 25321113 : for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= p[perm[i]]);
345 : }
346 : else { /* t = -1 */
347 18318856 : for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] += p[perm[i]]);
348 : }
349 1514714 : if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
350 : }
351 643666 : lig--; col--;
352 643666 : if (gc_needed(av,3))
353 : {
354 0 : if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[1]");
355 0 : if (T) T = gerepilecopy(av, T); else set_avma(av);
356 : }
357 : }
358 : /* As above with lines containing a +/- 1 (no other assumption).
359 : * Stop when single precision becomes dangerous */
360 133853 : vmax = cgetg(co,t_VECSMALL);
361 1661067 : for (j=1; j<=col; j++)
362 : {
363 1527107 : GEN matj = gel(mat,j);
364 6995681 : for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[i]);
365 1527107 : vmax[j] = s;
366 : }
367 418975 : while (lig > lk0 && col)
368 : {
369 435750 : for (i=lig; i>lk0; i--)
370 409532 : if ( (n = count2(mat,perm[i],col)) ) break;
371 311234 : if (i == lk0) break;
372 :
373 285015 : lswap(vmax[n], vmax[col]);
374 285015 : lswap(perm[i], perm[lig]);
375 285015 : swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
376 285015 : if (T) swap(gel(T,n), gel(T,col));
377 285015 : if (p[perm[lig]] < 0)
378 : {
379 571654 : for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
380 99245 : if (T) ZV_togglesign(gel(T,col));
381 : }
382 3868166 : for (j=1; j<col; j++)
383 : {
384 3583216 : GEN matj = gel(mat,j);
385 : long t;
386 3583216 : if (! (t = matj[perm[lig]]) ) continue;
387 1794973 : if (vmax[col] && (ulong)labs(t) >= (HIGHBIT-vmax[j]) / vmax[col])
388 0 : goto END2;
389 :
390 18226733 : for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= t*p[perm[i]]);
391 1794973 : vmax[j] = s;
392 1794973 : if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
393 : }
394 284950 : lig--; col--;
395 284950 : if (gc_needed(av,3))
396 : {
397 0 : if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[2]");
398 0 : gerepileall(av, T? 2: 1, &vmax, &T);
399 : }
400 : }
401 :
402 107741 : END2: /* clean up mat: remove everything to the right of the 1s on diagonal */
403 : /* go multiprecision first */
404 133960 : matb = cgetg(co,t_MAT); /* bottom part (complement of matt) */
405 2786565 : for (j=1; j<co; j++)
406 : {
407 2652705 : GEN matj = gel(mat,j);
408 2652705 : p1 = cgetg(li-k0,t_COL); gel(matb,j) = p1;
409 2652715 : p1 -= k0;
410 80602004 : for (i=k0+1; i<li; i++) gel(p1,i) = stoi(matj[perm[i]]);
411 : }
412 133860 : if (DEBUGLEVEL>5)
413 : {
414 0 : err_printf(" after phase2:\n");
415 0 : p_mat(mat,perm,lk0);
416 : }
417 1411014 : for (i=li-2; i>lig; i--)
418 : {
419 1277055 : long h, i0 = i - k0, k = i + co-li;
420 1277055 : GEN Bk = gel(matb,k);
421 28290461 : for (j=k+1; j<co; j++)
422 : {
423 27013702 : GEN Bj = gel(matb,j), v = gel(Bj,i0);
424 27013702 : s = signe(v); if (!s) continue;
425 :
426 5189322 : gel(Bj,i0) = gen_0;
427 5189322 : if (is_pm1(v))
428 : {
429 3013535 : if (s > 0) /* v = 1 */
430 44214283 : { for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), gel(Bk,h)); }
431 : else /* v = -1 */
432 37787255 : { for (h=1; h<i0; h++) gel(Bj,h) = addii(gel(Bj,h), gel(Bk,h)); }
433 : }
434 : else {
435 49392195 : for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), mulii(v,gel(Bk,h)));
436 : }
437 5188870 : if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,k), negi(v));
438 : }
439 1276759 : if (gc_needed(av,2))
440 : {
441 6 : if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[3], i = %ld", i);
442 1590 : for (h=1; h<co; h++) setlg(matb[h], i0+1); /* bottom can be forgotten */
443 6 : gerepileall(av, T? 2: 1, &matb, &T);
444 : }
445 : }
446 2786686 : for (j=1; j<co; j++) setlg(matb[j], lig-k0+1); /* bottom can be forgotten */
447 133969 : gerepileall(av, T? 2: 1, &matb, &T);
448 133960 : if (DEBUGLEVEL>5) err_printf(" matb cleaned up (using Id block)\n");
449 :
450 133960 : nlze = lk0 - k0; /* # of 0 rows */
451 133960 : lnz = lig-nlze+1; /* 1 + # of nonzero rows (!= 0...0 1 0 ... 0) */
452 133960 : if (T) matt = ZM_mul(matt,T); /* update top rows */
453 133959 : extramat = cgetg(col+1,t_MAT); /* = new C minus the 0 rows */
454 1376052 : for (j=1; j<=col; j++)
455 : {
456 1242094 : GEN z = gel(matt,j);
457 1242094 : GEN t = (gel(matb,j)) + nlze - k0;
458 1242094 : p2=cgetg(lnz,t_COL); gel(extramat,j) = p2;
459 5211804 : for (i=1; i<=k0; i++) gel(p2,i) = gel(z,i); /* top k0 rows */
460 2006605 : for ( ; i<lnz; i++) gel(p2,i) = gel(t,i); /* other nonzero rows */
461 : }
462 133958 : if (!col) {
463 0 : permpro = identity_perm(lnz);
464 0 : nr = lnz;
465 : }
466 : else
467 133958 : permpro = ZM_rowrankprofile(extramat, &nr);
468 : /* lnz = lg(permpro) */
469 133959 : if (nlze)
470 : { /* put the nlze 0 rows (trivial generators) at the top */
471 11544 : p1 = new_chunk(lk0+1);
472 37755 : for (i=1; i<=nlze; i++) p1[i] = perm[i + k0];
473 60042 : for ( ; i<=lk0; i++) p1[i] = perm[i - nlze];
474 86253 : for (i=1; i<=lk0; i++) perm[i] = p1[i];
475 : }
476 : /* sort other rows according to permpro (nr redundant generators first) */
477 133959 : p1 = new_chunk(lnz); p2 = perm + nlze;
478 586628 : for (i=1; i<lnz; i++) p1[i] = p2[permpro[i]];
479 586633 : for (i=1; i<lnz; i++) p2[i] = p1[i];
480 : /* perm indexes the rows of mat
481 : * |_0__|__redund__|__dense__|__too big__|_____done______|
482 : * 0 nlze lig li
483 : * \___nr___/ \___k0__/
484 : * \____________lnz ______________/
485 : *
486 : * col co
487 : * [dep | ]
488 : * i0 [--------| B ] (i0 = nlze + nr)
489 : * [matbnew | ] matbnew has maximal rank = lnz-1 - nr
490 : * mat = [--------|-----] lig
491 : * [ 0 | Id ]
492 : * [ | ] li */
493 :
494 133959 : matbnew = cgetg(col+1,t_MAT); /* dense+toobig, maximal rank. For hnffinal */
495 133959 : dep = cgetg(col+1,t_MAT); /* rows dependent from the ones in matbnew */
496 1376045 : for (j=1; j<=col; j++)
497 : {
498 1242087 : GEN z = gel(extramat,j);
499 1242087 : p1 = cgetg(nlze+nr+1,t_COL); gel(dep,j) = p1;
500 1242091 : p2 = cgetg(lnz-nr,t_COL); gel(matbnew,j) = p2;
501 1586831 : for (i=1; i<=nlze; i++) gel(p1,i) = gen_0;
502 1315762 : p1 += nlze; for (i=1; i<=nr; i++) p1[i] = z[permpro[i]];
503 5902574 : p2 -= nr; for ( ; i<lnz; i++) p2[i] = z[permpro[i]];
504 : }
505 :
506 : /* redundant generators in terms of the genuine generators
507 : * (x_i) = - (g_i) B */
508 133958 : B = cgetg(co-col,t_MAT);
509 1544576 : for (j=col+1; j<co; j++)
510 : {
511 1410620 : GEN y = gel(matt,j);
512 1410620 : GEN z = gel(matb,j);
513 1410620 : p1=cgetg(lig+1,t_COL); gel(B,j-col) = p1;
514 2953563 : for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
515 1410618 : p1 += nlze; z += nlze-k0;
516 9961168 : for (k=1; k<lnz; k++)
517 : {
518 8550550 : i = permpro[k];
519 8550550 : gel(p1,k) = (i <= k0)? gel(y,i): gel(z,i);
520 : }
521 : }
522 133956 : if (T) C = typ(C)==t_MAT? RgM_ZM_mul(C,T): RgV_RgM_mul(C,T);
523 133953 : gerepileall(av, 4, &matbnew, &B, &dep, &C);
524 133960 : *ptdep = dep;
525 133960 : *ptB = B;
526 133960 : H = hnffinal(matbnew, perm, ptdep, ptB, &C);
527 133960 : if (CO > co)
528 : { /* treat the rest, N cols at a time (hnflll slow otherwise) */
529 0 : const long N = 300;
530 0 : long a, L = CO - co, l = minss(L, N); /* L columns to add */
531 0 : GEN CC = *ptC, m0 = mat0;
532 0 : setlg(CC, CO); /* restore */
533 0 : CC += co-1;
534 0 : m0 += co-1;
535 0 : for (a = l;;)
536 0 : {
537 : GEN MAT, emb;
538 0 : gerepileall(av, 4, &H,&C,ptB,ptdep);
539 0 : MAT = cgetg(l + 1, t_MAT);
540 0 : emb = cgetg(l + 1, typ(C));
541 0 : for (j = 1 ; j <= l; j++)
542 : {
543 0 : gel(MAT,j) = gel(m0,j);
544 0 : emb[j] = CC[j];
545 : }
546 0 : H = hnfadd_i(H, perm, ptdep, ptB, &C, MAT, emb);
547 0 : if (a == L) break;
548 0 : CC += l;
549 0 : m0 += l;
550 0 : a += l; if (a > L) { l = L - (a - l); a = L; }
551 : }
552 : }
553 133960 : *ptC = C; return H;
554 : }
555 :
556 : GEN
557 0 : hnfspec(GEN mat, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
558 : {
559 0 : pari_sp av = avma;
560 0 : GEN H = hnfspec_i(mat, perm, ptdep, ptB, ptC, k0);
561 0 : return gc_all(av, 4, &H, ptC, ptdep, ptB);
562 : }
563 :
564 : /* HNF reduce x, apply same transforms to C */
565 : GEN
566 0 : mathnfspec(GEN x, GEN *pperm, GEN *pdep, GEN *pB, GEN *pC)
567 : {
568 0 : long i, j, k, l, n, ly, lx = lg(x);
569 : GEN z, v1, perm;
570 0 : if (lx == 1) return cgetg(1, t_MAT);
571 0 : ly = lgcols(x);
572 0 : *pperm = perm = identity_perm(ly-1);
573 0 : z = cgetg(lx,t_MAT);
574 0 : for (i=1; i<lx; i++)
575 : {
576 0 : GEN C = cgetg(ly,t_COL), D = gel(x,i);
577 0 : gel(z,i) = C;
578 0 : for (j=1; j<ly; j++)
579 : {
580 0 : GEN d = gel(D,j);
581 0 : if (is_bigint(d)) goto TOOLARGE;
582 0 : C[j] = itos(d);
583 : }
584 : }
585 : /* [ dep | ]
586 : * [-----| B ]
587 : * [ H | ]
588 : * [-----|-----]
589 : * [ 0 | Id ] */
590 0 : return hnfspec(z,perm, pdep, pB, pC, 0);
591 :
592 0 : TOOLARGE:
593 0 : if (lg(*pC) > 1 && lgcols(*pC) > 1)
594 0 : pari_err_IMPL("mathnfspec with large entries");
595 0 : x = ZM_hnf(x); lx = lg(x);
596 0 : v1 = cgetg(ly, t_VECSMALL);
597 0 : n = lx - ly;
598 0 : for (i = k = l = 1; i < ly; i++)
599 0 : if (equali1(gcoeff(x,i,i + n))) v1[l++] = i; else perm[k++] = i;
600 0 : setlg(perm, k);
601 0 : setlg(v1, l);
602 0 : x = rowpermute(x, perm); /* upper part */
603 0 : *pperm = vecsmall_concat(perm, v1);
604 0 : *pB = vecslice(x, k+n, lx-1);
605 0 : setlg(x, k);
606 0 : *pdep = rowslice(x, 1, n);
607 0 : return n? rowslice(x, n+1, k-1): x; /* H */
608 : }
609 :
610 : /* add new relations to a matrix treated by hnfspec (extramat / extraC) */
611 : GEN
612 76248 : hnfadd_i(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
613 : GEN extramat,GEN extraC)
614 : {
615 76248 : GEN matb, extratop, Cnew, permpro, B = *ptB, C = *ptC, dep = *ptdep;
616 : long i, lH, lB, li, lig, co, col, nlze;
617 :
618 76248 : if (lg(extramat) == 1) return H;
619 76248 : co = lg(C)-1;
620 76248 : lH = lg(H)-1;
621 76248 : lB = lg(B)-1;
622 76248 : li = lg(perm)-1;
623 76248 : lig = li - lB;
624 76248 : col = co - lB;
625 76248 : nlze = lig - lH;
626 :
627 : /* col co
628 : * [ 0 |dep | ]
629 : * nlze [--------| B ]
630 : * [ 0 | H | ]
631 : * [--------|-----] lig
632 : * [ 0 | Id ]
633 : * [ | ] li */
634 76248 : extratop = zm_to_ZM( rowslicepermute(extramat, perm, 1, lig) );
635 76248 : if (li != lig)
636 : { /* zero out bottom part, using the Id block */
637 76234 : GEN A = vecslice(C, col+1, co);
638 76234 : GEN c = rowslicepermute(extramat, perm, lig+1, li);
639 76234 : extraC = gsub(extraC, typ(A)==t_MAT? RgM_zm_mul(A, c): RgV_zm_mul(A,c));
640 76234 : extratop = ZM_sub(extratop, ZM_zm_mul(B, c));
641 : }
642 :
643 76243 : extramat = shallowconcat(extratop, vconcat(dep, H));
644 76248 : Cnew = shallowconcat(extraC, vecslice(C, col-lH+1, co));
645 76248 : if (DEBUGLEVEL>5) err_printf(" 1st phase done\n");
646 76248 : permpro = ZM_rowrankprofile(extramat, &nlze);
647 76248 : extramat = rowpermute(extramat, permpro);
648 76248 : *ptB = rowpermute(B, permpro);
649 76248 : permpro = vecsmallpermute(perm, permpro);
650 268389 : for (i=1; i<=lig; i++) perm[i] = permpro[i]; /* perm o= permpro */
651 :
652 76248 : *ptdep = rowslice(extramat, 1, nlze);
653 76248 : matb = rowslice(extramat, nlze+1, lig);
654 76248 : if (DEBUGLEVEL>5) err_printf(" 2nd phase done\n");
655 76248 : H = hnffinal(matb,perm,ptdep,ptB,&Cnew);
656 76248 : *ptC = shallowconcat(vecslice(C, 1, col-lH), Cnew);
657 76248 : return H;
658 : }
659 :
660 : GEN
661 0 : hnfadd(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
662 : GEN extramat,GEN extraC)
663 : {
664 0 : pari_sp av = avma;
665 0 : H = hnfadd_i(H, perm, ptdep, ptB, ptC, ZM_to_zm(extramat), extraC);
666 0 : return gc_all(av, 4, &H, ptC, ptdep, ptB);
667 : }
668 :
669 : /* zero aj = Aij (!= 0) using ak = Aik (maybe 0), via linear combination of
670 : * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
671 : static void
672 38519156 : ZC_elem(GEN aj, GEN ak, GEN A, GEN U, long j, long k)
673 : {
674 : GEN p1,u,v,d;
675 :
676 38519156 : if (!signe(ak)) {
677 99072 : swap(gel(A,j), gel(A,k));
678 99072 : if (U) swap(gel(U,j), gel(U,k));
679 34920777 : return;
680 : }
681 38420084 : d = bezout(aj,ak,&u,&v);
682 : /* frequent special case (u,v) = (1,0) or (0,1) */
683 38411488 : if (!signe(u))
684 : { /* ak | aj */
685 18820738 : p1 = diviiexact(aj,ak); togglesign(p1);
686 18821429 : ZC_lincomb1_inplace(gel(A,j), gel(A,k), p1);
687 18830264 : if (U)
688 2268671 : ZC_lincomb1_inplace(gel(U,j), gel(U,k), p1);
689 18830100 : return;
690 : }
691 19590750 : if (!signe(v))
692 : { /* aj | ak */
693 15993299 : p1 = diviiexact(ak,aj); togglesign(p1);
694 15991518 : ZC_lincomb1_inplace(gel(A,k), gel(A,j), p1);
695 15991603 : swap(gel(A,j), gel(A,k));
696 15991603 : if (U) {
697 365801 : ZC_lincomb1_inplace(gel(U,k), gel(U,j), p1);
698 365803 : swap(gel(U,j), gel(U,k));
699 : }
700 15991605 : return;
701 : }
702 :
703 3597451 : if (!is_pm1(d)) { aj = diviiexact(aj, d); ak = diviiexact(ak, d); }
704 3597436 : p1 = gel(A,k); aj = negi(aj); /* NOT togglesign */
705 3597709 : gel(A,k) = ZC_lincomb(u,v, gel(A,j),p1);
706 3597278 : gel(A,j) = ZC_lincomb(aj,ak, p1,gel(A,j));
707 3597395 : if (U)
708 : {
709 747904 : p1 = gel(U,k);
710 747904 : gel(U,k) = ZC_lincomb(u,v, gel(U,j),p1);
711 747959 : gel(U,j) = ZC_lincomb(aj,ak, p1,gel(U,j));
712 : }
713 : }
714 :
715 : INLINE int
716 2590 : is_RgX(GEN a, long v) { return typ(a) == t_POL && varn(a)==v; }
717 : /* set u,v such that au + bv = gcd(a,b), divide a,b by the gcd */
718 : static GEN
719 882 : gbezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv, long vx)
720 : {
721 882 : GEN a = *pa, b = *pb, d;
722 882 : if (gequal0(a))
723 : {
724 0 : *pa = gen_0; *pu = gen_0;
725 0 : *pb = gen_1; *pv = gen_1; return b;
726 : }
727 882 : a = is_RgX(a,vx)? RgX_renormalize(a): scalarpol(a, vx);
728 882 : b = is_RgX(b,vx)? RgX_renormalize(b): scalarpol(b, vx);
729 882 : d = RgX_extgcd(a,b, pu,pv);
730 882 : if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
731 238 : else if (typ(gel(d,2)) == t_REAL && lg(gel(d,2)) <= 3)
732 : #if 1
733 : { /* possible accuracy problem */
734 0 : GEN D = RgX_gcd_simple(a,b);
735 0 : if (degpol(D)) {
736 0 : D = RgX_normalize(D);
737 0 : a = RgX_div(a, D);
738 0 : b = RgX_div(b, D);
739 0 : d = RgX_extgcd(a,b, pu,pv); /* retry now */
740 0 : d = RgX_mul(d, D);
741 : }
742 : }
743 : #else
744 : { /* less stable */
745 : d = RgX_extgcd_simple(a,b, pu,pv);
746 : if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
747 : }
748 : #endif
749 882 : *pa = a;
750 882 : *pb = b; return d;
751 : }
752 : static GEN
753 2694022 : col_mul(GEN x, GEN c)
754 : {
755 2694022 : if (typ(x) == t_INT)
756 : {
757 2692035 : long s = signe(x);
758 2692035 : if (!s) return NULL;
759 2041005 : if (is_pm1(x)) return (s > 0)? c: RgC_neg(c);
760 : }
761 307645 : return RgC_Rg_mul(c, x);
762 : }
763 : static void
764 0 : do_zero(GEN x)
765 : {
766 0 : long i, lx = lg(x);
767 0 : for (i=1; i<lx; i++) gel(x,i) = gen_0;
768 0 : }
769 :
770 : /* (c1, c2) *= [u,-b; v,a] */
771 : static void
772 673508 : update(GEN u, GEN v, GEN a, GEN b, GEN *c1, GEN *c2)
773 : {
774 : GEN p1,p2;
775 :
776 673508 : u = col_mul(u,*c1);
777 673552 : v = col_mul(v,*c2);
778 673565 : if (u) p1 = v? gadd(u,v): u;
779 6516 : else p1 = v? v: NULL;
780 :
781 673565 : a = col_mul(a,*c2);
782 673582 : b = col_mul(gneg_i(b),*c1);
783 673587 : if (a) p2 = b? RgC_add(a,b): a;
784 0 : else p2 = b? b: NULL;
785 :
786 673531 : if (!p1) do_zero(*c1); else *c1 = p1;
787 673536 : if (!p2) do_zero(*c2); else *c2 = p2;
788 673536 : }
789 :
790 : /* zero aj = Aij (!= 0) using ak = Aik (maybe 0), via linear combination of
791 : * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
792 : static void
793 511 : RgC_elem(GEN aj, GEN ak, GEN A, GEN V, long j, long k, long li, long vx)
794 : {
795 511 : GEN u,v, d = gbezout_step(&aj, &ak, &u, &v, vx);
796 : long l;
797 : /* (A[,k], A[,j]) *= [v, -aj; u, ak ] */
798 1778 : for (l = 1; l < li; l++)
799 : {
800 1267 : GEN t = gadd(gmul(u,gcoeff(A,l,j)), gmul(v,gcoeff(A,l,k)));
801 1267 : gcoeff(A,l,j) = gsub(gmul(ak,gcoeff(A,l,j)), gmul(aj,gcoeff(A,l,k)));
802 1267 : gcoeff(A,l,k) = t;
803 : }
804 511 : gcoeff(A,li,j) = gen_0;
805 511 : gcoeff(A,li,k) = d;
806 511 : if (V) update(v,u,ak,aj,(GEN*)(V+k),(GEN*)(V+j));
807 511 : }
808 :
809 : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
810 : static void
811 4124168 : ZM_reduce(GEN A, GEN U, long i, long j0)
812 : {
813 4124168 : long j, lA = lg(A);
814 4124168 : GEN d = gcoeff(A,i,j0);
815 4124168 : if (signe(d) < 0)
816 : {
817 33771 : ZV_neg_inplace(gel(A,j0));
818 33771 : if (U) ZV_togglesign(gel(U,j0));
819 33771 : d = gcoeff(A,i,j0);
820 : }
821 8242737 : for (j=j0+1; j<lA; j++)
822 : {
823 4118615 : GEN q = truedivii(gcoeff(A,i,j), d);
824 4118528 : if (!signe(q)) continue;
825 :
826 256002 : togglesign(q);
827 256110 : ZC_lincomb1_inplace(gel(A,j), gel(A,j0), q);
828 256108 : if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,j0), q);
829 : }
830 4124122 : }
831 :
832 : /* normalize T as if it were a t_POL in variable v */
833 : static GEN
834 364 : normalize_as_RgX(GEN T, long v, GEN *pd)
835 : {
836 : GEN d;
837 364 : if (!is_RgX(T,v)) { *pd = T; return gen_1; }
838 336 : d = leading_coeff(T);
839 336 : while (gequal0(d) || (typ(d) == t_REAL && lg(d) == 3
840 0 : && gexpo(T) - expo(d) > (long)BITS_IN_LONG)) {
841 14 : T = normalizepol_lg(T, lg(T)-1);
842 14 : if (!signe(T)) { *pd = gen_1; return T; }
843 0 : d = leading_coeff(T);
844 : }
845 322 : if (degpol(T)) T = RgX_Rg_div(T,d); else { d = gel(T,2); T = gen_1; }
846 322 : *pd = d; return T;
847 : }
848 : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
849 : static void
850 84 : RgM_reduce(GEN A, GEN U, long i, long j0, long vx)
851 : {
852 84 : long j, lA = lg(A);
853 84 : GEN d, T = normalize_as_RgX(gcoeff(A,i,j0), vx, &d);
854 84 : if (U && !gequal1(d)) gel(U,j0) = RgC_Rg_div(gel(U,j0), d);
855 84 : gcoeff(A,i,j0) = T;
856 :
857 196 : for (j=j0+1; j<lA; j++)
858 : {
859 112 : GEN t = gcoeff(A,i,j), q;
860 112 : if (gequal0(t)) continue;
861 14 : if (T == gen_1)
862 0 : q = t;
863 14 : else if (is_RgX(t,vx))
864 14 : q = RgX_div(t, T);
865 0 : else continue;
866 :
867 14 : if (gequal0(q)) continue;
868 7 : gel(A,j) = RgC_sub(gel(A,j), RgC_Rg_mul(gel(A,j0), q));
869 7 : if (U) gel(U,j) = RgC_sub(gel(U,j), RgC_Rg_mul(gel(U,j0), q));
870 : }
871 84 : }
872 :
873 : /* A,B square integral in upper HNF, of the same dimension > 0. Return Au
874 : * in Z^n (v in Z^n not computed), such that Au + Bv = [1, 0, ..., 0] */
875 : GEN
876 741904 : hnfmerge_get_1(GEN A, GEN B)
877 : {
878 741904 : pari_sp av = avma;
879 741904 : long j, k, l = lg(A), lb;
880 741904 : GEN b, U = cgetg(l + 1, t_MAT), C = cgetg(l + 1, t_VEC);
881 :
882 741930 : b = gcoeff(B,1,1); lb = lgefint(b);
883 1507281 : for (j = 1; j < l; j++)
884 : {
885 : GEN t;
886 1507276 : long c = j+1;
887 1507276 : gel(U,j) = col_ei(l-1, j);
888 1507405 : gel(U,c) = zerocol(l-1); /* dummy */
889 1507426 : gel(C,j) = vecslice(gel(A,j), 1,j);
890 1507464 : gel(C,c) = vecslice(gel(B,j), 1,j);
891 4185132 : for (k = j; k > 0; k--)
892 : {
893 2678135 : t = gcoeff(C,k,c);
894 2678135 : if (gequal0(t)) continue;
895 2474078 : setlg(C[c], k+1);
896 2474059 : ZC_elem(t, gcoeff(C,k,k), C, U, c, k);
897 2473197 : if (lgefint(gcoeff(C,k,k)) > lb) gel(C,k) = FpC_red(gel(C,k), b);
898 2473197 : if (j > 4)
899 : {
900 89296 : GEN u = gel(U,k);
901 : long h;
902 1337037 : for (h=1; h<l; h++)
903 1247741 : if (lgefint(gel(u,h)) > lb) gel(u,h) = remii(gel(u,h), b);
904 : }
905 : }
906 1506997 : if (j == 1)
907 741717 : t = gcoeff(C,1,1);
908 : else
909 : {
910 : GEN u;
911 765280 : t = bezout(gcoeff(C,1,1), b, &u, NULL); /* >= 0 */
912 765623 : if (signe(u) && !equali1(u)) gel(U,1) = ZC_Z_mul(gel(U,1), u);
913 765624 : gcoeff(C,1,1) = t;
914 : }
915 1507341 : if (equali1(t)) break;
916 : }
917 741847 : if (j >= l) return NULL;
918 741847 : b = lcmii(gcoeff(A,1,1),b);
919 741845 : A = FpC_red(ZM_ZC_mul(A,gel(U,1)), b);
920 741703 : return gerepileupto(av, FpC_center(A, b, shifti(b,-1)));
921 : }
922 :
923 : /* remove the first r columns */
924 : static void
925 309548 : remove_0cols(long r, GEN *pA, GEN *pB, long remove)
926 : {
927 309548 : GEN A = *pA, B = *pB;
928 309548 : long l = lg(A);
929 309548 : A += r; A[0] = evaltyp(t_MAT) | evallg(l-r);
930 309551 : if (B && remove == 2) { B += r; B[0] = A[0]; }
931 309551 : *pA = A; *pB = B;
932 309551 : }
933 :
934 : /* Inefficient compared to hnfall. 'remove' = throw away lin.dep columns */
935 : static GEN
936 54373 : hnf_i(GEN A, int remove)
937 : {
938 54373 : pari_sp av0 = avma, av;
939 : long s, n, m, j, k, li, def, ldef;
940 :
941 54373 : RgM_dimensions(A, &m, &n);
942 54373 : if (!n) return cgetg(1,t_MAT);
943 54121 : av = avma;
944 54121 : A = RgM_shallowcopy(A);
945 54121 : def = n; ldef = (m>n)? m-n: 0;
946 146250 : for (li=m; li>ldef; li--)
947 : {
948 380244 : for (j=def-1; j; j--)
949 : {
950 288114 : GEN a = gcoeff(A,li,j);
951 288114 : if (!signe(a)) continue;
952 :
953 : /* zero a = Aij using b = Aik */
954 146976 : k = (j==1)? def: j-1;
955 146976 : ZC_elem(a,gcoeff(A,li,k), A,NULL, j,k);
956 146975 : if (gc_needed(av,1))
957 : {
958 0 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[1]. li=%ld",li);
959 0 : A = gerepilecopy(av, A);
960 : }
961 : }
962 92130 : s = signe(gcoeff(A,li,def));
963 92130 : if (s)
964 : {
965 91531 : if (s < 0) ZV_neg_inplace(gel(A,def));
966 91531 : ZM_reduce(A, NULL, li,def);
967 91530 : def--;
968 : }
969 : else
970 599 : if (ldef) ldef--;
971 92129 : if (gc_needed(av,1))
972 : {
973 0 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[2]. li=%ld",li);
974 0 : A = gerepilecopy(av, A);
975 : }
976 : }
977 : /* rank A = n - def */
978 54119 : if (remove) { GEN B = NULL; remove_0cols(def, &A, &B, remove); }
979 54118 : return gerepileupto(av0, ZM_copy(A));
980 : }
981 :
982 : GEN
983 77276 : ZM_hnf(GEN x) { return lg(x) > 8? ZM_hnfall(x, NULL, 1): hnf_i(x, 1); }
984 :
985 : /* u*z[1..k] mod p, in place */
986 : static void
987 5304003 : FpV_Fp_mul_part_ip(GEN z, GEN u, GEN p, long k)
988 : {
989 : long i;
990 5304003 : if (is_pm1(u)) {
991 192424 : if (signe(u) > 0) {
992 578775 : for (i = 1; i <= k; i++)
993 438288 : if (signe(gel(z,i))) gel(z,i) = modii(gel(z,i), p);
994 : } else {
995 145546 : for (i = 1; i <= k; i++)
996 93617 : if (signe(gel(z,i))) gel(z,i) = modii(negi(gel(z,i)), p);
997 : }
998 : }
999 : else {
1000 19284815 : for (i = 1; i <= k; i++)
1001 14173701 : if (signe(gel(z,i))) gel(z,i) = Fp_mul(u,gel(z,i), p);
1002 : }
1003 5303530 : }
1004 : static void
1005 34323769 : FpV_red_part_ipvec(GEN z, GEN p, long k)
1006 : {
1007 : long i;
1008 103829020 : for (i = 1; i <= k; i++) gel(z,i) = modii(gel(z,i), gel(p,i));
1009 34318792 : }
1010 :
1011 : /* return x * U, in echelon form (mod p^m), where (det(U),p) = 1.
1012 : * If early_abort is set, return NULL as soon as one pivot is 0 (mod p^m) */
1013 : GEN
1014 523873 : ZpM_echelon(GEN x, long early_abort, GEN p, GEN pm)
1015 : {
1016 523873 : pari_sp av0 = avma, av;
1017 : long m, li, co, i, j, k, def, ldef;
1018 :
1019 523873 : co = lg(x); if (co == 1) return cgetg(1,t_MAT);
1020 523873 : li = lgcols(x);
1021 523873 : av = avma;
1022 523873 : x = RgM_shallowcopy(x);
1023 523875 : m = Z_pval(pm, p);
1024 :
1025 523870 : ldef = (li > co)? li - co: 0;
1026 2495602 : for (def = co-1,i = li-1; i > ldef; i--)
1027 : {
1028 1972395 : long vmin = LONG_MAX, kmin = 0;
1029 1972395 : GEN umin = gen_0, pvmin, q;
1030 9617789 : for (k = 1; k <= def; k++)
1031 : {
1032 8108913 : GEN u = gcoeff(x,i,k);
1033 : long v;
1034 8108913 : if (!signe(u)) continue;
1035 3826429 : v = Z_pvalrem(u, p, &u);
1036 3826319 : if (v >= m) gcoeff(x,i,k) = gen_0;
1037 2702281 : else if (v < vmin) {
1038 1392915 : vmin = v; kmin = k; umin = u;
1039 1392915 : if (!vmin) break;
1040 : }
1041 : }
1042 1972285 : if (!kmin)
1043 : {
1044 920450 : if (early_abort) return NULL;
1045 919762 : gcoeff(x,i,def) = gen_0;
1046 919762 : ldef--;
1047 919762 : if (ldef < 0) ldef = 0;
1048 919762 : continue;
1049 : }
1050 1051835 : if (kmin != def) swap(gel(x,def), gel(x,kmin));
1051 1051835 : q = vmin? powiu(p, m-vmin): pm;
1052 : /* pivot has valuation vmin */
1053 1051835 : umin = modii(umin, q);
1054 1051919 : if (!equali1(umin))
1055 527946 : FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(umin,q), pm, i-1);
1056 1051918 : gcoeff(x, i, def) = pvmin = powiu(p, vmin);
1057 7090738 : for (j = def-1; j; j--)
1058 : { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
1059 6038768 : GEN t, a = gcoeff(x,i,j) = modii(gcoeff(x,i,j), pm);
1060 6038336 : if (!signe(a)) continue;
1061 :
1062 2824497 : t = diviiexact(a, pvmin); togglesign(t);
1063 2825049 : ZC_lincomb1_inplace(gel(x,j), gel(x,def), t);
1064 2824964 : if (gc_needed(av,1))
1065 : {
1066 14 : if (DEBUGMEM>1) pari_warn(warnmem,"ZpM_echelon. i=%ld",i);
1067 14 : x = gerepilecopy(av, x); pvmin = gcoeff(x,i,def);
1068 : }
1069 : }
1070 1051970 : def--;
1071 : }
1072 523207 : if (co > li)
1073 : {
1074 0 : x += co - li;
1075 0 : x[0] = evaltyp(t_MAT) | evallg(li);
1076 : }
1077 523207 : return gerepilecopy(av0, x);
1078 : }
1079 : GEN
1080 4650255 : zlm_echelon(GEN x, long early_abort, ulong p, ulong pm)
1081 : {
1082 4650255 : pari_sp av0 = avma;
1083 : long li, co, i, j, k, def, ldef;
1084 : ulong m;
1085 :
1086 4650255 : co = lg(x); if (co == 1) return cgetg(1,t_MAT);
1087 4650255 : li = lgcols(x);
1088 4650371 : x = Flm_copy(x);
1089 4650463 : m = u_lval(pm, p);
1090 :
1091 4650451 : ldef = (li > co)? li - co: 0;
1092 20738334 : for (def = co-1,i = li-1; i > ldef; i--)
1093 : {
1094 16273333 : long vmin = LONG_MAX, kmin = 0;
1095 16273333 : ulong umin = 0, pvmin, q;
1096 49024046 : for (k = 1; k <= def; k++)
1097 : {
1098 41438957 : ulong u = ucoeff(x,i,k);
1099 : long v;
1100 41438957 : if (!u) continue;
1101 22640137 : v = u_lvalrem(u, p, &u);
1102 22640057 : if (v >= (long) m) ucoeff(x,i,k) = 0;
1103 22640057 : else if (v < vmin) {
1104 16317450 : vmin = v; kmin = k; umin = u;
1105 16317450 : if (!vmin) break;
1106 : }
1107 : }
1108 16273253 : if (!kmin)
1109 : {
1110 1011719 : if (early_abort) return NULL;
1111 826928 : ucoeff(x,i,def) = 0;
1112 826928 : ldef--;
1113 826928 : if (ldef < 0) ldef = 0;
1114 826928 : continue;
1115 : }
1116 15261534 : if (kmin != def) swap(gel(x,def), gel(x,kmin));
1117 15261534 : q = vmin? upowuu(p, m-vmin): pm;
1118 : /* pivot has valuation vmin */
1119 15261575 : umin %= q;
1120 15261575 : if (umin != 1)
1121 7720084 : Flv_Fl_mul_part_inplace(gel(x,def), Fl_inv(umin,q), pm, i-1);
1122 15261543 : ucoeff(x, i, def) = pvmin = upowuu(p, vmin);
1123 67493343 : for (j = def-1; j; j--)
1124 : { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
1125 52232388 : ulong t, a = ucoeff(x,i,j);
1126 52232388 : if (!a) continue;
1127 :
1128 29492071 : t = Fl_neg(a / pvmin, q);
1129 29491990 : Flc_lincomb1_inplace(gel(x,j), gel(x,def), t, pm);
1130 : }
1131 15260955 : def--;
1132 : }
1133 4465001 : if (co > li)
1134 : {
1135 0 : x += co - li;
1136 0 : x[0] = evaltyp(t_MAT) | evallg(li);
1137 : }
1138 4465001 : return gerepilecopy(av0, x);
1139 : }
1140 :
1141 : static int
1142 219643 : ZV_allequal(GEN v)
1143 : {
1144 219643 : long i, l = lg(v);
1145 219643 : if (l > 1)
1146 : {
1147 219643 : GEN x = gel(v,1);
1148 388901 : for (i = 2; i < l; i++) if (!equalii(x,gel(v,i))) return 0;
1149 : }
1150 198948 : return 1;
1151 : }
1152 : /* compute optimal D for hnfmod: x upper triangular */
1153 : static GEN
1154 4652756 : optimal_D(GEN x, GEN D)
1155 : {
1156 4652756 : long i, n = nbrows(x);
1157 4653010 : GEN C = shallowcopy(D);
1158 4653998 : gel(C,1) = gcoeff(x,1,1);
1159 5065468 : for (i = 2; i < n; i++)
1160 : {
1161 2455392 : GEN c = mulii(gel(C,i-1), gcoeff(x,i,i));
1162 2455149 : if (signe(c) < 0) togglesign(c);
1163 2455153 : if (cmpii(c, gel(D,i)) >= 0) break;
1164 411470 : gel(C,i) = c;
1165 : }
1166 4653881 : return C;
1167 : }
1168 :
1169 : /* D = multiple of det x (usually detint(x)) or vector of positive moduli
1170 : * (compute hnf(x | D))
1171 : * flag & hnf_MODID: reduce mod D * matid [ otherwise as above ].
1172 : * flag & hnf_PART: don't reduce once diagonal is known
1173 : * flag & hnf_CENTER: centermod HNF (2|x[i,j]| <] x[i,i]) */
1174 : GEN
1175 4655888 : ZM_hnfmodall_i(GEN x, GEN D, long flag)
1176 : {
1177 : pari_sp av;
1178 4655888 : const long center = (flag & hnf_CENTER);
1179 : long moddiag, modsame, nli, li, co, i, j, k, def, ldef;
1180 : GEN u, LDM;
1181 :
1182 4655888 : co = lg(x);
1183 4655888 : if (co == 1)
1184 : {
1185 1561 : if (typ(D) == t_INT || lg(D) == 1) return cgetg(1,t_MAT);
1186 105 : x = diagonal_shallow(D); /* handle flags properly */
1187 105 : co = lg(x);
1188 : }
1189 4654432 : li = lgcols(x);
1190 4654621 : if (li == 1)
1191 : {
1192 120 : if (typ(D) != t_INT && lg(D) != li) pari_err_DIM("ZM_hnfmod");
1193 120 : return cgetg(1,t_MAT);
1194 : }
1195 4654503 : nli = li - 1;
1196 4654503 : modsame = typ(D)==t_INT;
1197 4654503 : if (!modsame)
1198 : {
1199 219657 : if (lg(D) != li) pari_err_DIM("ZM_hnfmod");
1200 219643 : if (ZV_allequal(D)) { modsame = 1; D = gel(D,1); }
1201 : }
1202 4654484 : moddiag = (flag & hnf_MODID) || !modsame;
1203 : /* modsame: triangularize mod fixed d*Id;
1204 : * moddiag: modulo diagonal matrix, else modulo multiple of determinant */
1205 :
1206 4654484 : if (modsame)
1207 : {
1208 4633785 : LDM = const_vecsmall(nli, 2*lgefint(D)-2);
1209 4633869 : D = const_vec(nli,D);
1210 : }
1211 : else
1212 : {
1213 20699 : LDM = cgetg(li, t_VECSMALL);
1214 68039 : for (i=1; i<li; i++) LDM[i] = lgefint(gel(D,i));
1215 : }
1216 4654699 : av = avma;
1217 4654699 : x = RgM_shallowcopy(x);
1218 :
1219 4654804 : ldef = 0;
1220 4654804 : if (li > co)
1221 : {
1222 135730 : ldef = li - co;
1223 135730 : if (!moddiag)
1224 7 : pari_err_DOMAIN("ZM_hnfmod","nb lines",">", strtoGENstr("nb columns"), x);
1225 : }
1226 18485405 : for (def = co-1,i = nli; i > ldef; i--,def--)
1227 : {
1228 13829085 : GEN d = gel(D,i);
1229 13829085 : long add_N = modsame;
1230 60095577 : for (j = 1; j < def; j++)
1231 : {
1232 46264938 : GEN p1, p2, b, a = gcoeff(x,i,j) = remii(gcoeff(x,i,j), d);
1233 58175996 : if (!signe(a)) continue;
1234 :
1235 31797992 : k = j+1;
1236 31797992 : b = gcoeff(x,i,k) = remii(gcoeff(x,i,k), d);
1237 31803191 : if (!signe(b)) { swap(gel(x,j), gel(x,k)); continue; }
1238 19883709 : if (add_N)
1239 : { /* ensure the moving pivot on row i divides d from now on */
1240 7009126 : add_N = 0;
1241 7009126 : if (!equali1(a))
1242 : { /* x[j] *= u; after this, a = x[i,j] | d */
1243 5726812 : GEN u = Fp_invgen(a, d, &a);
1244 : long t;
1245 5727897 : p1 = gel(x,j);
1246 20381385 : for (t = 1; t < i; t++) gel(p1,t) = mulii(gel(p1,t), u);
1247 5726032 : FpV_red_part_ipvec(p1, D, i-1);
1248 5726157 : gel(p1,i) = a;
1249 5726157 : if (2*lg(a) < lg(b))
1250 : { /* reduce x[i,k] mod x[i,j]: helps ZC_elem */
1251 26409 : GEN r, q = dvmdii(b, a, &r);
1252 26409 : togglesign(q);
1253 26409 : ZC_lincomb1_inplace_i(gel(x,k), gel(x,j), q, i-1);
1254 26409 : FpV_red_part_ipvec(gel(x,k), D, i-1);
1255 26409 : gcoeff(x,i,k) = b = r;
1256 : }
1257 : }
1258 : }
1259 19883190 : ZC_elem(a,b, x, NULL, j,k);
1260 19888485 : p1 = gel(x,j);
1261 19888485 : p2 = gel(x,k);
1262 : /* prevent coeffs explosion */
1263 115201369 : for (k = 1; k < i; k++)
1264 : {
1265 95312881 : if (lgefint(gel(p1,k)) > LDM[k]) gel(p1,k) = remii(gel(p1,k),gel(D,k));
1266 95312884 : if (lgefint(gel(p2,k)) > LDM[k]) gel(p2,k) = remii(gel(p2,k),gel(D,k));
1267 : }
1268 : }
1269 13830639 : if (gc_needed(av,2))
1270 : {
1271 24 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[1]. i=%ld",i);
1272 24 : x = gerepilecopy(av, x);
1273 : }
1274 13830639 : if (moddiag && !signe(gcoeff(x,i,def)))
1275 : { /* missing pivot on line i, insert column */
1276 918215 : GEN a = cgetg(co + 1, t_MAT);
1277 4910888 : for (k = 1; k <= def; k++) gel(a,k) = gel(x,k);
1278 918219 : gel(a,k++) = Rg_col_ei(gel(D,i), nli, i);
1279 3898250 : for ( ; k <= co; k++) gel(a,k) = gel(x,k-1);
1280 918219 : ldef--; if (ldef < 0) ldef = 0;
1281 918219 : co++; def++; x = a;
1282 : }
1283 : }
1284 4656320 : if (co < li)
1285 : { /* implies moddiag, add missing diag(D) components */
1286 103806 : GEN a = cgetg(li+1, t_MAT);
1287 242492 : for (k = 1; k <= li-co; k++) gel(a,k) = Rg_col_ei(gel(D,k), nli, k);
1288 284198 : for (i = 1; i < co; i++) gel(a,k-1+i) = gel(x,i);
1289 103806 : gel(a,li) = zerocol(nli); x = a;
1290 : }
1291 : else
1292 : {
1293 4552514 : x += co - li;
1294 4552514 : x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns */
1295 4550718 : if (moddiag) x = shallowconcat(x, zerocol(nli));
1296 : }
1297 4654766 : if (moddiag)
1298 : { /* x[li]: extra column, an accumulator discarded at the end */
1299 : GEN D2;
1300 4499378 : gcoeff(x,1,1) = gcdii(gcoeff(x,1,1), gel(D,1));
1301 4497456 : D2 = optimal_D(x,D);
1302 : /* add up missing diag(D) components */
1303 18034576 : for (i = nli; i > 0; i--)
1304 : {
1305 13535952 : gcoeff(x, i, li) = gel(D,i);
1306 52024141 : for (j = i; j > 0; j--)
1307 : {
1308 38491167 : GEN a = gcoeff(x, j, li);
1309 38491167 : if (!signe(a)) continue;
1310 14296123 : ZC_elem(a, gcoeff(x,j,j), x, NULL, li,j);
1311 14293525 : FpV_red_part_ipvec(gel(x,li), D, j-1);
1312 14294577 : FpV_red_part_ipvec(gel(x,j), D, j-1);
1313 : }
1314 13532974 : if (gc_needed(av,1))
1315 : {
1316 32 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[2]. i=%ld", i);
1317 32 : gerepileall(av, 2, &x, &D2);
1318 : }
1319 : }
1320 4498624 : D = D2;
1321 : }
1322 : else
1323 : {
1324 155388 : GEN b = gel(D,1);
1325 584496 : for (i = nli; i > 0; i--)
1326 : {
1327 429102 : GEN d = bezout(gcoeff(x,i,i),b, &u,NULL);
1328 429110 : gcoeff(x,i,i) = d;
1329 429110 : FpV_Fp_mul_part_ip(gel(x,i), u, b, i-1);
1330 429104 : if (i > 1) b = diviiexact(b,d);
1331 : }
1332 155394 : D = optimal_D(x,D);
1333 : }
1334 4654012 : x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns / discard accumulator */
1335 4654110 : if (flag & hnf_PART) return x;
1336 :
1337 18617923 : for (i = nli; i > 0; i--)
1338 : {
1339 13967684 : GEN diag = gcoeff(x,i,i);
1340 13967684 : if (signe(diag) < 0) { gel(x,i) = ZC_neg(gel(x,i)); diag = gcoeff(x,i,i); }
1341 13967690 : if (i != nli)
1342 25587995 : for (j = 1; j < i; j++) gcoeff(x,j,i) = remii(gcoeff(x,j,i), gel(D,j));
1343 39551998 : for (j = i+1; j < li; j++)
1344 : {
1345 25586342 : GEN b = gcoeff(x,i,j) = remii(gcoeff(x,i,j), gel(D,i));
1346 25584871 : GEN r, q = truedvmdii(b, diag, &r);
1347 : /* ensure -diag/2 <= r < diag/2 */
1348 25583888 : if (center && signe(r) && abscmpii(shifti(r,1),diag) >= 0)
1349 616968 : { r = subii(r,diag); q = addiu(q,1); }
1350 25585916 : if (!signe(q)) continue;
1351 10639964 : togglesign(q);
1352 10640005 : ZC_lincomb1_inplace_i(gel(x,j), gel(x,i), q, i-1);
1353 10638467 : gcoeff(x,i,j) = r;
1354 : }
1355 13965656 : if (gc_needed(av,1))
1356 : {
1357 40 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[3]. i=%ld", i);
1358 40 : gerepileall(av, 2, &x, &D);
1359 : }
1360 : }
1361 4650239 : return x;
1362 : }
1363 : GEN
1364 4606542 : ZM_hnfmodall(GEN x, GEN dm, long flag)
1365 : {
1366 4606542 : pari_sp av = avma;
1367 4606542 : return gerepilecopy(av, ZM_hnfmodall_i(x, dm, flag));
1368 : }
1369 : GEN
1370 155383 : ZM_hnfmod(GEN x, GEN d) { return ZM_hnfmodall(x,d,0); }
1371 : GEN
1372 4052756 : ZM_hnfmodid(GEN x, GEN d)
1373 4052756 : { return ZM_hnfmodall(x,d,hnf_MODID); }
1374 : /* return the column echelon form of x with 1's as pivots,
1375 : * P contains the row indices containing the pivots in increasing order */
1376 : static GEN
1377 3134065 : FpM_echelon(GEN x, GEN *pP, GEN p)
1378 : {
1379 : pari_sp av;
1380 : long iP, li, co, i, j, k, def, ldef;
1381 : GEN P;
1382 :
1383 3134065 : co = lg(x); if (co == 1) { *pP = cgetg(1,t_VECSMALL); return cgetg(1,t_MAT); }
1384 3134065 : li = lgcols(x);
1385 3134052 : iP = 1;
1386 3134052 : *pP = P = cgetg(li, t_VECSMALL);
1387 3134083 : av = avma;
1388 3134083 : x = FpM_red(x, p);
1389 :
1390 3133502 : ldef = (li > co)? li - co: 0;
1391 11918290 : for (def = co-1,i = li-1; i > ldef; i--)
1392 : {
1393 8782931 : GEN u = NULL;
1394 13527205 : for (k = def; k; k--)
1395 : {
1396 10134966 : u = gcoeff(x,i,k);
1397 10134966 : if (signe(u)) break;
1398 : }
1399 8782931 : if (!k)
1400 : {
1401 3393785 : if (--ldef < 0) ldef = 0;
1402 3393785 : continue;
1403 : }
1404 5389146 : P[iP++] = i;
1405 5389146 : if (k != def) swap(gel(x,def), gel(x,k));
1406 5389146 : if (!equali1(u))
1407 4347159 : FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(u,p), p, i-1);
1408 5390602 : gcoeff(x, i, def) = gen_1;
1409 17093048 : for (j = def-1; j; j--)
1410 : { /* zero x[i, 1..def-1] using x[i,def] = 1*/
1411 11702738 : GEN xj = gel(x,j), u = gel(xj,i);
1412 11702738 : if (!signe(u)) continue;
1413 :
1414 7432249 : ZC_lincomb1_inplace(xj, gel(x,def), negi(u));
1415 38424674 : for (k = 1; k < i; k++) gel(xj,k) = modii(gel(xj,k), p);
1416 : }
1417 5390310 : if (gc_needed(av,2))
1418 : {
1419 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FpM_echelon. i=%ld",i);
1420 0 : x = gerepilecopy(av, x);
1421 : }
1422 5391003 : def--;
1423 : }
1424 : /* rank = iP - 1 */
1425 3135359 : setlg(P, iP); vecsmall_sort(P);
1426 3134093 : if (co > iP) x += co - iP;
1427 3134093 : x[0] = evaltyp(t_MAT) | evallg(iP);
1428 3134073 : return x;
1429 : }
1430 : /* given x square of maximal rank with 1 or p on diagonal from hnfmodid
1431 : * (=> a column containing p has its other entries at 0 ), return the HNF */
1432 : static GEN
1433 3134329 : FpM_hnfend(pari_sp av, GEN x, GEN p)
1434 : {
1435 3134329 : long i, l = lgcols(x);
1436 11918560 : for (i = l-1; i > 0; i--)
1437 : {
1438 8785494 : GEN diag = gcoeff(x,i,i);
1439 : long j;
1440 8785494 : if (is_pm1(diag))
1441 11085564 : for (j = i+1; j < l; j++)
1442 : {
1443 5694014 : GEN xj = gel(x,j), b = gel(xj,i);
1444 : long k;
1445 5694014 : if (!signe(b)) continue;
1446 4023096 : ZC_lincomb1_inplace(xj, gel(x,i), negi(b));
1447 19550981 : for (k=1; k<i; k++)
1448 15528074 : if (lgefint(gel(xj,k)) > 3) gel(xj,k) = remii(gel(xj,k), p);
1449 : }
1450 : else
1451 9738447 : for (j = i+1; j < l; j++) gcoeff(x,i,j) = modii(gcoeff(x,i,j), p);
1452 8784970 : if (gc_needed(av,2))
1453 : {
1454 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FpM_hnfend. i=%ld",i);
1455 0 : x = gerepilecopy(av, x);
1456 : }
1457 : }
1458 3133066 : return x;
1459 : }
1460 : GEN
1461 3134067 : ZM_hnfmodprime(GEN x, GEN p)
1462 : {
1463 3134067 : pari_sp av = avma;
1464 : GEN P, y;
1465 : long l, lP, i;
1466 3134067 : if (lg(x) == 1) return cgetg(1, t_MAT);
1467 3134067 : l = lgcols(x);
1468 3134044 : x = FpM_echelon(x, &P, p);
1469 3134074 : lP = lg(P); /* rank = lP-1 */
1470 3134074 : if (lP == l) { set_avma(av); return matid(l-1); }
1471 3134074 : y = scalarmat_shallow(p, l-1);
1472 8526094 : for (i = 1; i < lP; i++) gel(y,P[i]) = gel(x,i);
1473 3134315 : return gerepilecopy(av, FpM_hnfend(av,y,p));
1474 : }
1475 :
1476 : static GEN
1477 397091 : allhnfmod(GEN x, GEN dm, int flag)
1478 : {
1479 397091 : if (typ(x)!=t_MAT) pari_err_TYPE("allhnfmod",x);
1480 397091 : RgM_check_ZM(x, "allhnfmod");
1481 397084 : if (isintzero(dm)) return ZM_hnf(x);
1482 397084 : return ZM_hnfmodall(x, dm, flag);
1483 : }
1484 : GEN
1485 14 : hnfmod(GEN x, GEN d)
1486 : {
1487 14 : if (typ(d) != t_INT) pari_err_TYPE("mathnfmod",d);
1488 14 : return allhnfmod(x, d, 0);
1489 : }
1490 : GEN
1491 397076 : hnfmodid(GEN x, GEN d)
1492 : {
1493 397076 : switch(typ(d))
1494 : {
1495 379925 : case t_INT: break;
1496 17151 : case t_VEC: case t_COL:
1497 17151 : if (RgV_is_ZV(d)) break;
1498 0 : default: pari_err_TYPE("mathnfmodid",d);
1499 : }
1500 397076 : return allhnfmod(x, d, hnf_MODID);
1501 : }
1502 :
1503 : /* M a ZM in HNF. Normalize with *centered* residues */
1504 : GEN
1505 25084 : ZM_hnfcenter(GEN M)
1506 : {
1507 25084 : long i, j, k, N = lg(M)-1;
1508 25084 : pari_sp av = avma;
1509 :
1510 88364 : for (j=N-1; j>0; j--) /* skip last line */
1511 : {
1512 63277 : GEN Mj = gel(M,j), a = gel(Mj,j);
1513 210926 : for (k = j+1; k <= N; k++)
1514 : {
1515 147646 : GEN Mk = gel(M,k), q = diviiround(gel(Mk,j), a);
1516 147653 : long s = signe(q);
1517 147653 : if (!s) continue;
1518 68401 : if (is_pm1(q))
1519 : {
1520 31896 : if (s < 0)
1521 8274 : for (i = 1; i <= j; i++) gel(Mk,i) = addii(gel(Mk,i), gel(Mj,i));
1522 : else
1523 92953 : for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), gel(Mj,i));
1524 : }
1525 : else
1526 204319 : for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), mulii(q,gel(Mj,i)));
1527 68399 : if (gc_needed(av,1))
1528 : {
1529 0 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfcenter, j = %ld",j);
1530 0 : M = gerepilecopy(av, M);
1531 : }
1532 : }
1533 : }
1534 25087 : return M;
1535 : }
1536 :
1537 : /***********************************************************************/
1538 : /* */
1539 : /* HNFLLL (Havas, Majewski, Mathews) */
1540 : /* */
1541 : /***********************************************************************/
1542 :
1543 : static void
1544 2029174 : Minus(long j, GEN lambda)
1545 : {
1546 2029174 : long k, n = lg(lambda);
1547 :
1548 11723827 : for (k=1 ; k<j; k++) togglesign_safe(&gcoeff(lambda,k,j));
1549 17108303 : for (k=j+1; k<n; k++) togglesign_safe(&gcoeff(lambda,j,k));
1550 2029102 : }
1551 :
1552 : /* index of first nonzero entry */
1553 : static long
1554 106074608 : findi(GEN M)
1555 : {
1556 106074608 : long i, n = lg(M);
1557 2671527844 : for (i=1; i<n; i++)
1558 2640367874 : if (signe(gel(M,i))) return i;
1559 31159970 : return 0;
1560 : }
1561 :
1562 : static long
1563 106062714 : findi_normalize(GEN Aj, GEN B, long j, GEN lambda)
1564 : {
1565 106062714 : long r = findi(Aj);
1566 106125840 : if (r && signe(gel(Aj,r)) < 0)
1567 : {
1568 2029017 : ZV_togglesign(Aj); if (B) ZV_togglesign(gel(B,j));
1569 2029176 : Minus(j,lambda);
1570 : }
1571 106130311 : return r;
1572 : }
1573 :
1574 : static void
1575 52991151 : reduce2(GEN A, GEN B, long k, long j, long *row0, long *row1, GEN lambda, GEN D)
1576 : {
1577 : GEN q;
1578 : long i;
1579 :
1580 52991151 : *row0 = findi_normalize(gel(A,j), B,j,lambda);
1581 53074764 : *row1 = findi_normalize(gel(A,k), B,k,lambda);
1582 53093689 : if (*row0)
1583 31083430 : q = truedivii(gcoeff(A,*row0,k), gcoeff(A,*row0,j));
1584 22010259 : else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
1585 7295343 : q = diviiround(gcoeff(lambda,j,k), gel(D,j));
1586 : else
1587 14703714 : return;
1588 :
1589 38363088 : if (signe(q))
1590 : {
1591 14315780 : GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
1592 14315780 : togglesign_safe(&q);
1593 14331680 : if (*row0) ZC_lincomb1_inplace(gel(A,k),gel(A,j),q);
1594 14327652 : if (B) ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
1595 14275916 : gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
1596 14259566 : if (is_pm1(q))
1597 : {
1598 5381617 : if (signe(q) > 0)
1599 : {
1600 5383203 : for (i=1; i<j; i++)
1601 3330062 : if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), gel(Lj,i));
1602 : }
1603 : else
1604 : {
1605 11285628 : for (i=1; i<j; i++)
1606 7958138 : if (signe(gel(Lj,i))) gel(Lk,i) = subii(gel(Lk,i), gel(Lj,i));
1607 : }
1608 : }
1609 : else
1610 : {
1611 39706738 : for (i=1; i<j; i++)
1612 30785395 : if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
1613 : }
1614 : }
1615 : }
1616 :
1617 : static void
1618 8369468 : hnfswap(GEN A, GEN B, long k, GEN lambda, GEN D)
1619 : {
1620 8369468 : GEN t, p1, p2, Lk = gel(lambda,k);
1621 8369468 : long i,j,n = lg(A);
1622 :
1623 8369468 : swap(gel(A,k), gel(A,k-1));
1624 8369468 : if (B) swap(gel(B,k), gel(B,k-1));
1625 46294830 : for (j=k-2; j; j--) swap(gcoeff(lambda,j,k-1), gel(Lk,j));
1626 104409513 : for (i=k+1; i<n; i++)
1627 : {
1628 96039281 : GEN Li = gel(lambda,i);
1629 96039281 : p1 = mulii(gel(Li,k-1), gel(D,k));
1630 95971198 : p2 = mulii(gel(Li,k), gel(Lk,k-1));
1631 95997449 : t = subii(p1,p2);
1632 :
1633 96009884 : p1 = mulii(gel(Li,k), gel(D,k-2));
1634 95988759 : p2 = mulii(gel(Li,k-1), gel(Lk,k-1));
1635 95993989 : gel(Li,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
1636 95999864 : gel(Li,k) = diviiexact(t, gel(D,k-1));
1637 : }
1638 8370232 : p1 = mulii(gel(D,k-2), gel(D,k));
1639 8348000 : p2 = sqri(gel(Lk,k-1));
1640 8349555 : gel(D,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
1641 8347014 : }
1642 :
1643 : /* reverse row order in matrix A, IN PLACE */
1644 : static GEN
1645 426786 : reverse_rows(GEN A)
1646 : {
1647 426786 : long i, j, h, n = lg(A);
1648 426786 : if (n == 1) return A;
1649 426786 : h = lgcols(A);
1650 3764935 : for (j=1; j<n; j++)
1651 : {
1652 3338148 : GEN c = gel(A,j);
1653 : /* start at (h-1) >>1 : if h = 2i even, no need to swap c[i] and itself */
1654 9352006 : for (i=(h-1)>>1; i; i--) swap(gel(c,i), gel(c,h-i));
1655 : }
1656 426787 : return A;
1657 : }
1658 : /* decide whether to swap */
1659 : static int
1660 4722764 : must_swap(long k, GEN lambda, GEN D)
1661 : {
1662 4722764 : pari_sp av = avma;
1663 4722764 : GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
1664 4717610 : return gc_bool(av, cmpii(z, sqri(gel(D,k-1))) < 0);
1665 : }
1666 :
1667 : GEN
1668 213399 : ZM_hnflll(GEN A, GEN *ptB, int remove)
1669 : {
1670 213399 : pari_sp av = avma;
1671 : long n, k, kmax;
1672 : GEN B, lambda, D;
1673 :
1674 213399 : n = lg(A);
1675 213399 : A = reverse_rows(ZM_copy(A)); /* ZM_copy for in place findi_normalize() */
1676 213400 : B = ptB? matid(n-1): NULL;
1677 213399 : D = const_vec(n, gen_1) + 1;
1678 213399 : lambda = zeromatcopy(n-1,n-1);
1679 186246 : k = kmax = 2;
1680 15531459 : while (k < n)
1681 : {
1682 : long row0, row1;
1683 : int do_swap;
1684 15318063 : reduce2(A,B,k,k-1,&row0,&row1,lambda,D);
1685 15329926 : if (row0) do_swap = (!row1 || row0 <= row1);
1686 5598358 : else if (row1) do_swap = 0;
1687 4454450 : else do_swap = must_swap(k,lambda,D);
1688 15369818 : if (do_swap)
1689 : {
1690 8122699 : hnfswap(A,B,k,lambda,D);
1691 8106491 : if (k > 2) k--;
1692 : }
1693 : else
1694 : {
1695 : long i;
1696 44920924 : for (i=k-2; i; i--)
1697 : {
1698 : long row0, row1;
1699 37682202 : reduce2(A,B,k,i,&row0,&row1,lambda,D);
1700 : }
1701 7238722 : if (++k > kmax) kmax = k;
1702 : }
1703 15345213 : if (gc_needed(av,3))
1704 : {
1705 187 : GEN b = D-1;
1706 187 : if (DEBUGMEM>1) pari_warn(warnmem,"hnflll, kmax = %ld / %ld",kmax,n-1);
1707 187 : gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
1708 187 : if (gc_needed(av,1)) paristack_resize(0); /* avoid desperation GC */
1709 187 : D = b+1;
1710 : }
1711 : }
1712 : /* handle trivial case: return negative diag coefficient otherwise */
1713 213396 : if (n == 2) (void)findi_normalize(gel(A,1), B,1,lambda);
1714 213396 : A = reverse_rows(A);
1715 213399 : if (remove)
1716 : {
1717 : long i;
1718 7371 : for (i = 1; i < n; i++)
1719 7147 : if (!ZV_equal0(gel(A,i))) break;
1720 2107 : remove_0cols(i-1, &A, &B, remove);
1721 : }
1722 213399 : gerepileall(av, B? 2: 1, &A, &B);
1723 213400 : if (B) *ptB = B;
1724 213400 : return A;
1725 : }
1726 :
1727 : GEN
1728 7 : hnflll(GEN x)
1729 : {
1730 7 : GEN z = cgetg(3, t_VEC);
1731 7 : gel(z,1) = ZM_hnflll(x, &gel(z,2), 1);
1732 7 : return z;
1733 : }
1734 :
1735 : /* Variation on HNFLLL: Extended GCD */
1736 :
1737 : static void
1738 948741 : reduce1(GEN A, GEN B, long k, long j, GEN lambda, GEN D)
1739 : {
1740 : GEN q;
1741 : long i;
1742 :
1743 948741 : if (signe(gel(A,j)))
1744 172264 : q = diviiround(gel(A,k),gel(A,j));
1745 776477 : else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
1746 96158 : q = diviiround(gcoeff(lambda,j,k), gel(D,j));
1747 : else
1748 680319 : return;
1749 :
1750 268422 : if (signe(q))
1751 : {
1752 231954 : GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
1753 231954 : togglesign_safe(&q);
1754 231954 : gel(A,k) = addmulii(gel(A,k), q, gel(A,j));
1755 231954 : ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
1756 231954 : gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
1757 421124 : for (i=1; i<j; i++)
1758 189170 : if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
1759 : }
1760 : }
1761 :
1762 : static GEN
1763 108829 : ZV_gcdext_i(GEN A)
1764 : {
1765 108829 : long k, n = lg(A);
1766 : GEN B, lambda, D;
1767 :
1768 108829 : if (n == 1) retmkvec2(gen_1, cgetg(1,t_MAT));
1769 108829 : A = leafcopy(A);
1770 108829 : B = matid(n-1);
1771 108829 : lambda = zeromatcopy(n-1,n-1);
1772 108829 : D = const_vec(n, gen_1) + 1;
1773 108829 : k = 2;
1774 696564 : while (k < n)
1775 : {
1776 : int do_swap;
1777 :
1778 587735 : reduce1(A,B,k,k-1,lambda,D);
1779 587735 : if (signe(gel(A,k-1))) do_swap = 1;
1780 415471 : else if (signe(gel(A,k))) do_swap = 0;
1781 223481 : else do_swap = must_swap(k,lambda,D);
1782 587735 : if (do_swap)
1783 : {
1784 240531 : hnfswap(A,B,k,lambda,D);
1785 240531 : if (k > 2) k--;
1786 : }
1787 : else
1788 : {
1789 : long i;
1790 708210 : for (i=k-2; i; i--) reduce1(A,B,k,i,lambda,D);
1791 347204 : k++;
1792 : }
1793 : }
1794 108829 : if (signe(gel(A,n-1)) < 0)
1795 : {
1796 13293 : gel(A,n-1) = negi(gel(A,n-1));
1797 13293 : ZV_togglesign(gel(B,n-1));
1798 : }
1799 108829 : return mkvec2(gel(A,n-1), B);
1800 : }
1801 : GEN
1802 108815 : ZV_extgcd(GEN A)
1803 : {
1804 108815 : pari_sp av = avma;
1805 108815 : return gerepilecopy(av, ZV_gcdext_i(A));
1806 : }
1807 : /* as ZV_extgcd, transforming the gcd into a t_MAT, for mathnf0 */
1808 : static GEN
1809 21 : ZV_hnfgcdext(GEN A)
1810 : {
1811 21 : pari_sp av = avma;
1812 : GEN z;
1813 21 : if (lg(A) == 1) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
1814 14 : z = ZV_gcdext_i(A);
1815 14 : gel(z,1) = mkmat(mkcol(gel(z,1)));
1816 14 : return gerepilecopy(av, z);
1817 : }
1818 :
1819 : /* HNF with permutation. */
1820 : GEN
1821 385 : ZM_hnfperm(GEN A, GEN *ptU, GEN *ptperm)
1822 : {
1823 : GEN U, c, l, perm, d, p, q, b;
1824 385 : pari_sp av = avma, av1;
1825 : long r, t, i, j, j1, k, m, n;
1826 :
1827 385 : n = lg(A)-1;
1828 385 : if (!n)
1829 : {
1830 7 : if (ptU) *ptU = cgetg(1,t_MAT);
1831 7 : if (ptperm) *ptperm = cgetg(1,t_VEC);
1832 7 : return cgetg(1, t_MAT);
1833 : }
1834 378 : m = nbrows(A);
1835 378 : c = zero_zv(m);
1836 378 : l = zero_zv(n);
1837 378 : perm = cgetg(m+1, t_VECSMALL);
1838 378 : av1 = avma;
1839 378 : A = RgM_shallowcopy(A);
1840 378 : U = ptU? matid(n): NULL;
1841 : /* U base change matrix : A0*U = A all along */
1842 1722 : for (r=0, k=1; k <= n; k++)
1843 : {
1844 3976 : for (j=1; j<k; j++)
1845 : {
1846 2632 : if (!l[j]) continue;
1847 2478 : t=l[j]; b=gcoeff(A,t,k);
1848 2478 : if (!signe(b)) continue;
1849 :
1850 533 : ZC_elem(b,gcoeff(A,t,j), A,U,k,j);
1851 533 : d = gcoeff(A,t,j);
1852 533 : if (signe(d) < 0)
1853 : {
1854 0 : ZV_neg_inplace(gel(A,j));
1855 0 : if (U) ZV_togglesign(gel(U,j));
1856 0 : d = gcoeff(A,t,j);
1857 : }
1858 1375 : for (j1=1; j1<j; j1++)
1859 : {
1860 842 : if (!l[j1]) continue;
1861 821 : q = truedivii(gcoeff(A,t,j1),d);
1862 821 : if (!signe(q)) continue;
1863 :
1864 329 : togglesign(q);
1865 329 : ZC_lincomb1_inplace(gel(A,j1), gel(A,j), q);
1866 329 : if (U) ZC_lincomb1_inplace(gel(U,j1), gel(U,j), q);
1867 : }
1868 : }
1869 4585 : t = m; while (t && (c[t] || !signe(gcoeff(A,t,k)))) t--;
1870 1344 : if (t)
1871 : {
1872 1225 : p = gcoeff(A,t,k);
1873 19971 : for (i=t-1; i; i--)
1874 : {
1875 18746 : q = gcoeff(A,i,k);
1876 18746 : if (signe(q) && abscmpii(p,q) > 0) { p = q; t = i; }
1877 : }
1878 1225 : perm[++r] = l[k] = t; c[t] = k;
1879 1225 : if (signe(p) < 0)
1880 : {
1881 156 : ZV_neg_inplace(gel(A,k));
1882 156 : if (U) ZV_togglesign(gel(U,k));
1883 156 : p = gcoeff(A,t,k);
1884 : }
1885 : /* p > 0 */
1886 3493 : for (j=1; j<k; j++)
1887 : {
1888 2268 : if (!l[j]) continue;
1889 2233 : q = truedivii(gcoeff(A,t,j),p);
1890 2233 : if (!signe(q)) continue;
1891 :
1892 245 : togglesign(q);
1893 245 : ZC_lincomb1_inplace(gel(A,j), gel(A,k), q);
1894 245 : if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,k), q);
1895 : }
1896 : }
1897 1344 : if (gc_needed(av1,1))
1898 : {
1899 0 : if (DEBUGMEM>1) pari_warn(warnmem,"hnfperm, k=%ld",k);
1900 0 : gerepileall(av1, U? 2: 1, &A, &U);
1901 : }
1902 : }
1903 378 : if (r < m)
1904 : {
1905 5131 : for (i=1,k=r; i<=m; i++)
1906 4816 : if (!c[i]) perm[++k] = i;
1907 : }
1908 :
1909 : /* We have A0*U=A, U in Gl(n,Z)
1910 : * basis for Im(A): columns of A s.t l[j]>0 (r cols)
1911 : * basis for Ker(A): columns of U s.t l[j]=0 (n-r cols) */
1912 378 : p = cgetg(r+1,t_MAT);
1913 2814 : for (i=1; i<=m/2; i++) lswap(perm[i], perm[m+1-i]);
1914 378 : if (U)
1915 : {
1916 84 : GEN u = cgetg(n+1,t_MAT);
1917 378 : for (t=1,k=r,j=1; j<=n; j++)
1918 294 : if (l[j])
1919 : {
1920 182 : u[k + n-r] = U[j];
1921 182 : gel(p,k--) = vecpermute(gel(A,j), perm);
1922 : }
1923 : else
1924 112 : u[t++] = U[j];
1925 84 : *ptU = u;
1926 84 : if (ptperm) *ptperm = perm;
1927 84 : gerepileall(av, ptperm? 3: 2, &p, ptU, ptperm);
1928 : }
1929 : else
1930 : {
1931 1344 : for (k=r,j=1; j<=n; j++)
1932 1050 : if (l[j]) gel(p,k--) = vecpermute(gel(A,j), perm);
1933 294 : if (ptperm) *ptperm = perm;
1934 294 : gerepileall(av, ptperm? 2: 1, &p, ptperm);
1935 : }
1936 378 : return p;
1937 : }
1938 :
1939 : GEN
1940 238 : ZM_hnf_knapsack(GEN x)
1941 : {
1942 238 : GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
1943 238 : long i,j, l = lg(H), h = lgcols(H);
1944 3213 : for (i=1; i<h; i++)
1945 : {
1946 3066 : int fl = 0;
1947 16697 : for (j=1; j<l; j++)
1948 : {
1949 13722 : t = gcoeff(H,i,j);
1950 13722 : if (signe(t))
1951 : {
1952 3101 : if (!is_pm1(t) || fl) return NULL;
1953 3010 : fl = 1;
1954 : }
1955 : }
1956 : }
1957 147 : return rowpermute(H, perm_inv(perm));
1958 : }
1959 :
1960 : GEN
1961 14 : hnfperm(GEN A)
1962 : {
1963 14 : GEN y = cgetg(4, t_VEC);
1964 14 : gel(y,1) = ZM_hnfperm(A, &gel(y,2), &gel(y,3));
1965 14 : return y;
1966 : }
1967 :
1968 : /* Hermite Normal Form, with base change matrix if ptB != NULL.
1969 : * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
1970 : * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
1971 : GEN
1972 717412 : ZM_hnfall_i(GEN A, GEN *ptB, long remove)
1973 : {
1974 : pari_sp av;
1975 : long m, n, r, i, j, k, li;
1976 : GEN B, c, h, a;
1977 :
1978 717412 : RgM_dimensions(A, &m,&n);
1979 717411 : if (!n)
1980 : {
1981 7 : if (ptB) *ptB = cgetg(1,t_MAT);
1982 7 : return cgetg(1,t_MAT);
1983 : }
1984 717404 : c = zero_zv(m);
1985 717395 : h = const_vecsmall(n, m);
1986 717391 : av = avma;
1987 717391 : A = RgM_shallowcopy(A);
1988 717409 : B = ptB? matid(n): NULL;
1989 717411 : r = n+1;
1990 2014501 : for (li=m; li; li--)
1991 : {
1992 2908359 : for (j=1; j<r; j++)
1993 : {
1994 3542025 : for (i=h[j]; i>li; i--)
1995 : {
1996 634061 : a = gcoeff(A,i,j);
1997 634061 : k = c[i];
1998 : /* zero a = Aij using Aik */
1999 634061 : if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B,j,k);
2000 633996 : ZM_reduce(A,B, i,k); /* ensure reduced entries even if a = 0 */
2001 : }
2002 2907964 : if (gc_needed(av,1) && (j & 0x7f) == 0)
2003 : {
2004 0 : if (DEBUGMEM>1)
2005 0 : pari_warn(warnmem,"ZM_hnfall[1], li = %ld, j = %ld", li, j);
2006 0 : gerepileall(av, B? 2: 1, &A, &B);
2007 : }
2008 2908024 : if (signe( gcoeff(A,li,j) )) break;
2009 1611239 : h[j] = li-1;
2010 : }
2011 1297142 : if (j == r) continue;
2012 1296855 : r--;
2013 1296855 : if (j < r) /* A[j] != 0 */
2014 : {
2015 839536 : swap(gel(A,j), gel(A,r));
2016 839536 : if (B) swap(gel(B,j), gel(B,r));
2017 839536 : h[j] = h[r]; h[r] = li; c[li] = r;
2018 : }
2019 1296855 : if (signe(gcoeff(A,li,r)) < 0)
2020 : {
2021 298963 : ZV_neg_inplace(gel(A,r));
2022 298972 : if (B) ZV_togglesign(gel(B,r));
2023 : }
2024 1296864 : ZM_reduce(A,B, li,r);
2025 1296807 : if (gc_needed(av,1))
2026 : {
2027 0 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[2], li = %ld", li);
2028 0 : gerepileall(av, B? 2: 1, &A, &B);
2029 : }
2030 : }
2031 :
2032 717381 : if (DEBUGLEVEL>5) err_printf("\nhnfall, final phase: ");
2033 717397 : r--; /* first r cols are in the image the n-r (independent) last ones */
2034 2468454 : for (j=1; j<=r; j++)
2035 : {
2036 3853329 : for (i=h[j]; i; i--)
2037 : {
2038 2102284 : a = gcoeff(A,i,j);
2039 2102284 : k = c[i];
2040 2102284 : if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B, j,k);
2041 2102253 : ZM_reduce(A,B, i,k); /* ensure reduced entries, even if a = 0 */
2042 : }
2043 1751045 : if (gc_needed(av,1) && (j & 0x7f) == 0)
2044 : {
2045 0 : if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[3], j = %ld", j);
2046 0 : gerepileall(av, B? 2: 1, &A, &B);
2047 : }
2048 : }
2049 717380 : if (DEBUGLEVEL>5) err_printf("\n");
2050 717380 : if (remove) remove_0cols(r, &A, &B, remove);
2051 717399 : if (ptB) *ptB = B;
2052 717399 : return A;
2053 : }
2054 : GEN
2055 24380 : ZM_hnfall(GEN A, GEN *ptB, long remove)
2056 : {
2057 24380 : pari_sp av = avma;
2058 24380 : A = ZM_hnfall_i(A, ptB, remove);
2059 24379 : return gc_all(av, ptB? 2: 1, &A, ptB);
2060 : }
2061 :
2062 : GEN
2063 14 : hnfall(GEN x)
2064 : {
2065 14 : GEN z = cgetg(3, t_VEC);
2066 14 : gel(z,1) = ZM_hnfall(x, (GEN*)(z+2), 1);
2067 14 : return z;
2068 : }
2069 : GEN
2070 14 : hnf(GEN x) { return mathnf0(x,0); }
2071 :
2072 : /* C = A^(-1)t where A and C are integral, A is upper triangular, t t_INT */
2073 : GEN
2074 236780 : hnf_invscale(GEN A, GEN t)
2075 : {
2076 236780 : long n = lg(A)-1, i,j,k;
2077 236780 : GEN m, c = cgetg(n+1,t_MAT);
2078 :
2079 236778 : if (!n) return c;
2080 878211 : for (k=1; k<=n; k++)
2081 : { /* cf hnf_divscale with B = id, thus b = e_k */
2082 641448 : GEN u = cgetg(n+1, t_COL);
2083 641445 : pari_sp av = avma;
2084 641445 : gel(c,k) = u;
2085 641445 : gel(u,n) = k == n? gerepileuptoint(av, diviiexact(t, gcoeff(A,n,n))): gen_0;
2086 2169801 : for (i=n-1; i>0; i--)
2087 : {
2088 1528368 : av = avma; m = i == k? t: gen_0;
2089 5782335 : for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
2090 1528146 : gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
2091 : }
2092 : }
2093 236763 : return c;
2094 : }
2095 :
2096 : /* C = A^(-1)(tB) where A, B, C are integral, A is upper triangular, t t_INT */
2097 : GEN
2098 195895 : hnf_divscale(GEN A, GEN B, GEN t)
2099 : {
2100 195895 : long n = lg(A)-1, i,j,k;
2101 195895 : GEN m, c = cgetg(n+1,t_MAT);
2102 :
2103 195895 : if (!n) return c;
2104 870302 : for (k=1; k<=n; k++)
2105 : {
2106 674406 : GEN u = cgetg(n+1, t_COL), b = gel(B,k);
2107 674406 : pari_sp av = avma;
2108 674406 : gel(c,k) = u; m = mulii(gel(b,n),t);
2109 674391 : gel(u,n) = gerepileuptoint(av, diviiexact(m, gcoeff(A,n,n)));
2110 3893944 : for (i=n-1; i>0; i--)
2111 : {
2112 3219537 : av = avma; m = mulii(gel(b,i),t);
2113 30098664 : for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
2114 3219456 : gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
2115 : }
2116 : }
2117 195896 : return c;
2118 : }
2119 :
2120 : /* A, B integral upper HNF. A^(-1) B integral ? */
2121 : int
2122 133 : hnfdivide(GEN A, GEN B)
2123 : {
2124 133 : pari_sp av = avma;
2125 133 : long n = lg(A)-1, i,j,k;
2126 : GEN u, b, m, r;
2127 :
2128 133 : if (!n) return 1;
2129 133 : if (lg(B)-1 != n) pari_err_DIM("hnfdivide");
2130 133 : u = cgetg(n+1, t_COL);
2131 483 : for (k=1; k<=n; k++)
2132 : {
2133 364 : b = gel(B,k);
2134 364 : m = gel(b,k);
2135 364 : gel(u,k) = dvmdii(m, gcoeff(A,k,k), &r);
2136 364 : if (r != gen_0) return gc_long(av, 0);
2137 826 : for (i=k-1; i>0; i--)
2138 : {
2139 476 : m = gel(b,i);
2140 1337 : for (j=i+1; j<=k; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
2141 476 : m = dvmdii(m, gcoeff(A,i,i), &r);
2142 476 : if (r != gen_0) return gc_long(av, 0);
2143 476 : gel(u,i) = m;
2144 : }
2145 : }
2146 119 : return gc_long(av, 1);
2147 : }
2148 :
2149 : /* A upper HNF, b integral vector. Return A^(-1) b if integral,
2150 : * NULL otherwise. Assume #A[,1] = #b. */
2151 : GEN
2152 365602 : hnf_invimage(GEN A, GEN b)
2153 : {
2154 365602 : pari_sp av = avma;
2155 365602 : long n = lg(A)-1, m, i, k;
2156 : GEN u, r;
2157 :
2158 365602 : if (!n) return lg(b)==1? cgetg(1,t_COL):NULL;
2159 365560 : m = nbrows(A); /* m >= n */
2160 365557 : u = cgetg(n+1, t_COL);
2161 829211 : for (i = n, k = m; k > 0; k--)
2162 : {
2163 829212 : pari_sp av2 = avma;
2164 : long j;
2165 829212 : GEN t = gel(b,k), Aki = gcoeff(A,k,i);
2166 829212 : if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
2167 1997578 : for (j=i+1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
2168 829155 : if (!signe(Aki))
2169 : {
2170 0 : if (signe(t)) return gc_NULL(av);
2171 0 : set_avma(av2); gel(u,i) = gen_0; continue;
2172 : }
2173 829155 : t = dvmdii(t, Aki, &r);
2174 829057 : if (r != gen_0) return gc_NULL(av);
2175 606814 : gel(u,i) = gerepileuptoint(av2, t);
2176 606953 : if (--i == 0) break;
2177 : }
2178 : /* If there is a solution, it must be u. Check remaining equations */
2179 286586 : for (; k > 0; k--)
2180 : {
2181 143294 : pari_sp av2 = avma;
2182 : long j;
2183 143294 : GEN t = gel(b,k);
2184 143294 : if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
2185 548347 : for (j=1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
2186 143291 : if (signe(t)) return gc_NULL(av);
2187 143291 : set_avma(av2);
2188 : }
2189 143292 : return u;
2190 : }
2191 :
2192 : /* A upper HNF, B integral matrix or column. Return A^(-1) B if integral,
2193 : * NULL otherwise */
2194 : GEN
2195 314279 : hnf_solve(GEN A, GEN B)
2196 : {
2197 : pari_sp av;
2198 : long i, l;
2199 : GEN C;
2200 :
2201 314279 : if (typ(B) == t_COL) return hnf_invimage(A, B);
2202 251727 : av = avma; C = cgetg_copy(B, &l);
2203 350720 : for (i = 1; i < l; i++)
2204 : {
2205 265964 : GEN c = hnf_invimage(A, gel(B,i));
2206 265946 : if (!c) return gc_NULL(av);
2207 98978 : gel(C,i) = c;
2208 : }
2209 84756 : return C;
2210 : }
2211 :
2212 : /***************************************************************/
2213 : /** **/
2214 : /** SMITH NORMAL FORM REDUCTION **/
2215 : /** **/
2216 : /***************************************************************/
2217 :
2218 : static GEN
2219 0 : trivsmith(long all)
2220 : {
2221 : GEN z;
2222 0 : if (!all) return cgetg(1,t_VEC);
2223 0 : z=cgetg(4,t_VEC);
2224 0 : gel(z,1) = cgetg(1,t_MAT);
2225 0 : gel(z,2) = cgetg(1,t_MAT);
2226 0 : gel(z,3) = cgetg(1,t_MAT); return z;
2227 : }
2228 :
2229 : static void
2230 63 : snf_pile1(pari_sp av, GEN *x, GEN *U)
2231 : {
2232 : GEN *gptr[2];
2233 63 : int c = 1; gptr[0]=x;
2234 63 : if (*U) gptr[c++] = U;
2235 63 : gerepilemany(av,gptr,c);
2236 63 : }
2237 : static void
2238 971748 : snf_pile(pari_sp av, GEN *x, GEN *U, GEN *V)
2239 : {
2240 : GEN *gptr[3];
2241 971748 : int c = 1; gptr[0]=x;
2242 971748 : if (*U) gptr[c++] = U;
2243 971748 : if (*V) gptr[c++] = V;
2244 971748 : gerepilemany(av,gptr,c);
2245 971804 : }
2246 :
2247 : static GEN
2248 695747 : bezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv)
2249 : {
2250 695747 : GEN a = *pa, b = *pb, d;
2251 695747 : if (absequalii(a,b))
2252 : {
2253 414139 : long sa = signe(a), sb = signe(b);
2254 414139 : *pv = gen_0;
2255 414139 : if (sb == sa) {
2256 406729 : *pa = *pb = gen_1;
2257 406729 : if (sa > 0) {*pu=gen_1; return a;} else {*pu=gen_m1; return absi(a);}
2258 : }
2259 7410 : if (sa > 0) { *pa = *pu = gen_1; *pb = gen_m1; return a; }
2260 1149 : *pa = *pu = gen_m1; *pb = gen_1; return b;
2261 : }
2262 281613 : d = bezout(a,b, pu,pv);
2263 281626 : *pa = diviiexact(a, d);
2264 281622 : *pb = diviiexact(b, d); return d;
2265 : }
2266 :
2267 : static int
2268 517393 : negcmpii(void *E, GEN x, GEN y) { (void)E; return -cmpii(x,y); }
2269 :
2270 : /* x square of maximal rank; does b = x[i,i] divide all entries in
2271 : * x[1..i-1, 1..i-1] ? If so, return 0; else the index of a problematic row */
2272 : static long
2273 1063452 : ZM_snf_no_divide(GEN x, long i)
2274 : {
2275 1063452 : GEN b = gcoeff(x,i,i);
2276 : long j, k;
2277 :
2278 1063452 : if (is_pm1(b)) return 0;
2279 907574 : for (k = 1; k < i; k++)
2280 2256951 : for (j = 1; j < i; j++)
2281 1782381 : if (!dvdii(gcoeff(x,k,j),b)) return k;
2282 283202 : return 0;
2283 : }
2284 :
2285 : static void
2286 1378417 : ZM_redpart(GEN x, GEN p, long I)
2287 : {
2288 1378417 : long l = lgefint(p), i, j;
2289 5880166 : for (i = 1; i <= I; i++)
2290 27116175 : for (j = 1; j <= I; j++)
2291 : {
2292 22614426 : GEN c = gcoeff(x,i,j);
2293 22614426 : if (lgefint(c) > l) gcoeff(x,i,j) = remii(c, p);
2294 : }
2295 1378417 : }
2296 : static void
2297 861315 : ZMrow_divexact_inplace(GEN M, long i, GEN c)
2298 : {
2299 861315 : long j, l = lg(M);
2300 3411844 : for (j = 1; j < l; j++) gcoeff(M,i,j) = diviiexact(gcoeff(M,i,j), c);
2301 861280 : }
2302 :
2303 : /* Return the SNF D of matrix X. If ptU/ptV non-NULL set them to U/V
2304 : * to that D = UXV */
2305 : GEN
2306 711523 : ZM_snfall_i(GEN x, GEN *ptU, GEN *ptV, long flag)
2307 : {
2308 711523 : pari_sp av0 = avma, av;
2309 711523 : const long return_vec = flag & 1;
2310 : long i, j, k, m0, m, n0, n;
2311 711523 : GEN u, v, U, V, V0, mdet, A = NULL, perm = NULL;
2312 :
2313 711523 : n0 = n = lg(x)-1;
2314 711523 : if (!n) {
2315 41014 : if (ptU) *ptU = cgetg(1,t_MAT);
2316 41014 : if (ptV) *ptV = cgetg(1,t_MAT);
2317 41014 : return cgetg(1, return_vec? t_VEC: t_MAT);
2318 : }
2319 670509 : m0 = m = nbrows(x);
2320 :
2321 670513 : U = V = V0 = NULL; /* U = TRANSPOSE of row transform matrix [act on columns]*/
2322 670513 : if (m == n && ZM_ishnf(x))
2323 : {
2324 610084 : mdet = ZM_det_triangular(x); /* != 0 */
2325 610038 : if (ptV) *ptV = matid(n);
2326 : }
2327 : else
2328 : {
2329 60433 : mdet = ZM_detmult(x);
2330 60445 : if (!signe(mdet))
2331 77 : x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
2332 : else
2333 : { /* m <= n */
2334 60368 : if (!ptV)
2335 357 : x = ZM_hnfmod(x,mdet);
2336 60011 : else if (m == n)
2337 : {
2338 59983 : GEN H = ZM_hnfmod(x,mdet);
2339 59983 : *ptV = ZM_gauss(x,H);
2340 59983 : x = H;
2341 : }
2342 : else
2343 28 : x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
2344 60368 : mdet = ZM_det_triangular(x); /* > 0 */
2345 : }
2346 60445 : n = lg(x)-1; /* n independent columns */
2347 60445 : if (ptV)
2348 : {
2349 60032 : V = *ptV;
2350 60032 : if (n != n0)
2351 : {
2352 28 : V0 = vecslice(V, 1, n0 - n); /* kernel */
2353 28 : V = vecslice(V, n0-n+1, n0);
2354 : }
2355 : }
2356 60445 : if (!signe(mdet))
2357 : {
2358 77 : if (n)
2359 : {
2360 70 : x = ZM_snfall_i(shallowtrans(x), ptV, ptU, return_vec); /* swap V,U */
2361 70 : if (!return_vec && n != m) x = shallowtrans(x);
2362 70 : if (ptV) V = ZM_mul(V, shallowtrans(*ptV));
2363 70 : if (ptU) U = *ptU; /* TRANSPOSE */
2364 : }
2365 : else /* 0 matrix */
2366 : {
2367 7 : x = cgetg(1,t_MAT);
2368 7 : if (ptV) V = cgetg(1, t_MAT);
2369 7 : if (ptU) U = matid(m);
2370 : }
2371 77 : goto THEEND;
2372 : }
2373 : }
2374 670472 : if (ptV || ptU) U = matid(n); /* we will compute V in terms of U */
2375 670485 : if (DEBUGLEVEL>7) err_printf("starting SNF loop");
2376 :
2377 : /* square, maximal rank n */
2378 670485 : A = x; x = shallowcopy(x); av = avma;
2379 1584125 : for (i = n; i > 1; i--)
2380 : {
2381 913674 : if (DEBUGLEVEL>7) err_printf("\ni = %ld: ",i);
2382 : for(;;)
2383 694140 : {
2384 1607796 : int c = 0;
2385 : GEN a, b;
2386 4625413 : for (j = i-1; j >= 1; j--)
2387 : {
2388 3017785 : b = gcoeff(x,i,j); if (!signe(b)) continue;
2389 262659 : a = gcoeff(x,i,i);
2390 262659 : ZC_elem(b, a, x,NULL, j,i);
2391 262642 : if (gc_needed(av,1))
2392 : {
2393 0 : if (DEBUGMEM>1) pari_warn(warnmem,"[1]: ZM_snfall i = %ld", i);
2394 0 : snf_pile1(av, &x,&U);
2395 : }
2396 : }
2397 1607628 : if (DEBUGLEVEL>7) err_printf("; ");
2398 4625424 : for (j=i-1; j>=1; j--)
2399 : {
2400 : GEN d;
2401 3017753 : b = gcoeff(x,j,i); if (!signe(b)) continue;
2402 695736 : a = gcoeff(x,i,i);
2403 695736 : d = bezout_step(&a, &b, &u, &v);
2404 4172456 : for (k = 1; k < i; k++)
2405 : {
2406 3476808 : GEN t = addii(mulii(u,gcoeff(x,i,k)),mulii(v,gcoeff(x,j,k)));
2407 3476876 : gcoeff(x,j,k) = subii(mulii(a,gcoeff(x,j,k)),
2408 3476879 : mulii(b,gcoeff(x,i,k)));
2409 3476725 : gcoeff(x,i,k) = t;
2410 : }
2411 695648 : gcoeff(x,j,i) = gen_0;
2412 695648 : gcoeff(x,i,i) = d;
2413 695648 : if (U) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
2414 695687 : if (gc_needed(av,1))
2415 : {
2416 63 : if (DEBUGMEM>1) pari_warn(warnmem,"[2]: ZM_snfall, i = %ld", i);
2417 63 : snf_pile1(av, &x,&U);
2418 : }
2419 695699 : c = 1;
2420 : }
2421 1607671 : if (!c)
2422 : {
2423 1063453 : k = ZM_snf_no_divide(x, i);
2424 1063405 : if (!k) break;
2425 :
2426 : /* x[k,j] != 0 mod b */
2427 573558 : for (j = 1; j <= i; j++)
2428 423810 : gcoeff(x,i,j) = addii(gcoeff(x,i,j),gcoeff(x,k,j));
2429 149748 : if (U) gel(U,i) = gadd(gel(U,i),gel(U,k));
2430 : }
2431 693969 : ZM_redpart(x, mdet, i);
2432 694116 : if (U && (flag & 2)) ZM_redpart(U, mdet, n);
2433 694136 : if (gc_needed(av,1))
2434 : {
2435 0 : if (DEBUGMEM>1) pari_warn(warnmem,"[3]: ZM_snfall");
2436 0 : snf_pile1(av, &x,&U);
2437 : }
2438 : }
2439 : }
2440 670451 : if (DEBUGLEVEL>7) err_printf("\n");
2441 2253578 : for (k = n; k; k--)
2442 : {
2443 1583644 : GEN d = gcdii(gcoeff(x,k,k), mdet);
2444 1583233 : gcoeff(x,k,k) = d;
2445 1583233 : if (!is_pm1(d)) mdet = diviiexact(mdet,d);
2446 : }
2447 669934 : THEEND:
2448 670011 : if (U) U = shallowtrans(U);
2449 670208 : if (ptV && A)
2450 : { /* U A V = D => D^(-1) U A = V^(-1) */
2451 665501 : long l = lg(x);
2452 665501 : GEN W = ZM_mul(U, A);
2453 1526616 : for (i = 1; i < l; i++)
2454 : {
2455 1334808 : GEN c = gcoeff(x,i,i);
2456 1334808 : if (is_pm1(c)) break; /* only 1 from now on */
2457 861107 : ZMrow_divexact_inplace(W, i, c);
2458 : }
2459 665553 : if (flag & 2)
2460 : {
2461 641340 : W = FpM_red(W, gcoeff(x,1,1));
2462 641367 : W = matinvmod(W, gcoeff(x,1,1));
2463 : }
2464 : else
2465 24213 : W = ZM_inv(W, NULL);
2466 665540 : V = V? ZM_mul(V, W): W;
2467 : }
2468 670240 : if (return_vec)
2469 : {
2470 646368 : long l = lg(x)-1;
2471 646368 : if (typ(x) == t_MAT) x = RgM_diagonal_shallow(x);
2472 646356 : if (m0 > l) x = shallowconcat(zerovec(m0-l), x);
2473 : }
2474 :
2475 670228 : if (V0)
2476 : { /* add kernel */
2477 28 : if (!return_vec) x = shallowconcat(zeromat(m,n0-n), x);
2478 28 : if (ptV) V = shallowconcat(V0, V);
2479 : }
2480 670228 : if (perm && U) U = vecpermute(U, perm_inv(perm));
2481 670228 : snf_pile(av0, &x,&U,&V);
2482 670579 : if (ptU) *ptU = U;
2483 670579 : if (ptV) *ptV = V;
2484 670579 : return x;
2485 : }
2486 : GEN
2487 63802 : ZM_snfall(GEN x, GEN *U, GEN *V) { return ZM_snfall_i(x, U, V, 0); }
2488 : GEN
2489 1638 : ZM_snf(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
2490 : GEN
2491 385 : smith(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
2492 : GEN
2493 35 : smithall(GEN x)
2494 : {
2495 35 : GEN z = cgetg(4, t_VEC);
2496 35 : gel(z,3) = ZM_snfall_i(x, (GEN*)(z+1),(GEN*)(z+2), 0);
2497 35 : return z;
2498 : }
2499 :
2500 : GEN
2501 214050 : ZV_snfclean(GEN d)
2502 : {
2503 214050 : long c, l = lg(d);
2504 226410 : for (c = 1; c < l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
2505 214051 : return c == l? d: vec_shorten(d, c-1);
2506 : }
2507 : void
2508 943988 : ZM_snfclean(GEN d, GEN u, GEN v)
2509 : {
2510 943988 : long i, c, l = lg(d);
2511 :
2512 943988 : if (typ(d) == t_VEC)
2513 2424052 : for (c=1; c<l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
2514 : else
2515 : {
2516 0 : for (c=1; c<l; c++) { GEN t = gcoeff(d,c,c); if (is_pm1(t)) break; }
2517 0 : if (c < l) for (i = 1; i < c; i++) setlg(gel(d,i), c);
2518 : }
2519 943983 : setlg(d, c);
2520 2918949 : if (u) for (i=1; i<l; i++) setlg(gel(u,i), c);
2521 943940 : if (v) setlg(v, c);
2522 943936 : }
2523 :
2524 : /* Assume z was computed by [g]smithall(). Remove the 1s on the diagonal */
2525 : GEN
2526 693 : smithclean(GEN z)
2527 : {
2528 : long i, j, h, l, c, d;
2529 : GEN U, V, y, D, t;
2530 :
2531 693 : if (typ(z) != t_VEC) pari_err_TYPE("smithclean",z);
2532 693 : l = lg(z); if (l == 1) return cgetg(1,t_VEC);
2533 686 : U = gel(z,1);
2534 686 : if (l != 4 || typ(U) != t_MAT)
2535 : { /* assume z = vector of elementary divisors */
2536 1827 : for (c=1; c<l; c++)
2537 1519 : if (gequal1(gel(z,c))) break;
2538 665 : return gcopy_lg(z, c);
2539 : }
2540 21 : V = gel(z,2);
2541 21 : D = gel(z,3);
2542 21 : l = lg(D);
2543 21 : if (l == 1) return gcopy(z);
2544 21 : h = lgcols(D);
2545 21 : if (h > l)
2546 : { /* D = vconcat(zero matrix, diagonal matrix) */
2547 21 : for (c=1+h-l, d=1; c<h; c++,d++)
2548 21 : if (gequal1(gcoeff(D,c,d))) break;
2549 : }
2550 7 : else if (h < l)
2551 : { /* D = concat(zero matrix, diagonal matrix) */
2552 7 : for (c=1, d=1+l-h; d<l; c++,d++)
2553 7 : if (gequal1(gcoeff(D,c,d))) break;
2554 : }
2555 : else
2556 : { /* D diagonal */
2557 0 : for (c=1; c<l; c++)
2558 0 : if (gequal1(gcoeff(D,c,c))) break;
2559 0 : d = c;
2560 : }
2561 : /* U was (h-1)x(h-1), V was (l-1)x(l-1), D was (h-1)x(l-1) */
2562 21 : y = cgetg(4,t_VEC);
2563 : /* truncate U to (c-1) x (h-1) */
2564 21 : gel(y,1) = t = cgetg(h,t_MAT);
2565 77 : for (j=1; j<h; j++) gel(t,j) = gcopy_lg(gel(U,j), c);
2566 : /* truncate V to (l-1) x (d-1) */
2567 21 : gel(y,2) = gcopy_lg(V, d);
2568 21 : gel(y,3) = t = zeromatcopy(c-1, d-1);
2569 : /* truncate D to a (c-1) x (d-1) matrix */
2570 21 : if (d > 1)
2571 : {
2572 14 : if (h > l)
2573 : {
2574 14 : for (i=1+h-l, j=1; i<c; i++,j++)
2575 7 : gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
2576 : }
2577 7 : else if (h < l)
2578 : {
2579 7 : for (i=1, j=1+l-h; j<d; i++,j++)
2580 0 : gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
2581 : }
2582 : else
2583 : {
2584 0 : for (j=1; j<d; j++)
2585 0 : gcoeff(t,j,j) = gcopy(gcoeff(D,j,j));
2586 : }
2587 : }
2588 21 : return y;
2589 : }
2590 :
2591 : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
2592 : * else return the index of a problematic row */
2593 : static long
2594 196 : gsnf_no_divide(GEN x, long i, long vx)
2595 : {
2596 196 : GEN b = gcoeff(x,i,i);
2597 : long j, k;
2598 :
2599 196 : if (gequal0(b))
2600 : {
2601 14 : for (k = 1; k < i; k++)
2602 14 : for (j = 1; j < i; j++)
2603 14 : if (!gequal0(gcoeff(x,k,j))) return k;
2604 0 : return 0;
2605 : }
2606 :
2607 182 : if (!is_RgX(b,vx) || degpol(b)<=0) return 0;
2608 217 : for (k = 1; k < i; k++)
2609 378 : for (j = 1; j < i; j++)
2610 : {
2611 266 : GEN z = gcoeff(x,k,j), r;
2612 266 : if (!is_RgX(z,vx)) z = scalarpol(z, vx);
2613 266 : r = RgX_rem(z, b);
2614 266 : if (signe(r) && (! isinexactreal(r) ||
2615 0 : gexpo(r) > 16 + gexpo(b) - prec2nbits(gprecision(r)))
2616 35 : ) return k;
2617 : }
2618 70 : return 0;
2619 : }
2620 :
2621 : /* Hermite Normal Form, with base change matrix if ptB != NULL.
2622 : * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
2623 : * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
2624 : GEN
2625 42 : RgM_hnfall(GEN A, GEN *pB, long remove)
2626 : {
2627 : pari_sp av;
2628 : long li, j, k, m, n, def, ldef;
2629 : GEN B;
2630 42 : long vx = gvar(A);
2631 :
2632 42 : n = lg(A)-1;
2633 42 : if (vx==NO_VARIABLE || !n)
2634 : {
2635 0 : RgM_check_ZM(A, "mathnf0");
2636 0 : return ZM_hnfall(A, pB, remove);
2637 : }
2638 42 : m = nbrows(A);
2639 42 : av = avma;
2640 42 : A = RgM_shallowcopy(A);
2641 42 : B = pB? matid(n): NULL;
2642 42 : def = n; ldef = (m>n)? m-n: 0;
2643 126 : for (li=m; li>ldef; li--)
2644 : {
2645 : GEN d, T;
2646 714 : for (j=def-1; j; j--)
2647 : {
2648 630 : GEN a = gcoeff(A,li,j);
2649 630 : if (gequal0(a)) continue;
2650 :
2651 511 : k = (j==1)? def: j-1;
2652 511 : RgC_elem(a,gcoeff(A,li,k), A,B, j,k, li, vx);
2653 : }
2654 84 : T = normalize_as_RgX(gcoeff(A,li,def), vx, &d);
2655 84 : if (gequal0(T))
2656 0 : { if (ldef) ldef--; }
2657 : else
2658 : {
2659 84 : if (!gequal1(d))
2660 : {
2661 49 : gel(A,def) = RgC_Rg_div(gel(A,def), d);
2662 49 : if (B) gel(B, def) = RgC_Rg_div(gel(B, def), d);
2663 : }
2664 84 : RgM_reduce(A, B, li, def, vx);
2665 84 : def--;
2666 : }
2667 84 : if (gc_needed(av,1))
2668 : {
2669 0 : if (DEBUGMEM>1) pari_warn(warnmem,"ghnfall");
2670 0 : gerepileall(av, B? 2: 1, &A, &B);
2671 : }
2672 : }
2673 : /* rank A = n - def */
2674 42 : if (remove) remove_0cols(def, &A, &B, remove);
2675 42 : gerepileall(av, B? 2: 1, &A, &B);
2676 42 : if (B) *pB = B;
2677 42 : return A;
2678 : }
2679 :
2680 : static GEN
2681 49 : RgXM_snf(GEN x,long all)
2682 : {
2683 : pari_sp av;
2684 : long i, j, k, n;
2685 : GEN z, u, v, U, V;
2686 49 : long vx = gvar(x);
2687 49 : n = lg(x)-1; if (!n) return trivsmith(all);
2688 49 : if (vx==NO_VARIABLE) pari_err_TYPE("RgXM_snf",x);
2689 49 : if (lgcols(x) != n+1) pari_err_DIM("gsmithall");
2690 49 : av = avma;
2691 49 : x = RgM_shallowcopy(x);
2692 49 : if (all) { U = matid(n); V = matid(n); }
2693 196 : for (i=n; i>=2; i--)
2694 : {
2695 : for(;;)
2696 168 : {
2697 : GEN a, b, d;
2698 315 : int c = 0;
2699 1092 : for (j=i-1; j>=1; j--)
2700 : {
2701 777 : b = gcoeff(x,i,j); if (gequal0(b)) continue;
2702 196 : a = gcoeff(x,i,i);
2703 196 : d = gbezout_step(&b, &a, &v, &u, vx);
2704 700 : for (k = 1; k < i; k++)
2705 : {
2706 504 : GEN t = gadd(gmul(u,gcoeff(x,k,i)),gmul(v,gcoeff(x,k,j)));
2707 504 : gcoeff(x,k,j) = gsub(gmul(a,gcoeff(x,k,j)),gmul(b,gcoeff(x,k,i)));
2708 504 : gcoeff(x,k,i) = t;
2709 : }
2710 196 : gcoeff(x,i,j) = gen_0;
2711 196 : gcoeff(x,i,i) = d;
2712 196 : if (all) update(u,v,a,b,(GEN*)(V+i),(GEN*)(V+j));
2713 : }
2714 1092 : for (j=i-1; j>=1; j--)
2715 : {
2716 777 : b = gcoeff(x,j,i); if (gequal0(b)) continue;
2717 175 : a = gcoeff(x,i,i);
2718 175 : d = gbezout_step(&b, &a, &v, &u, vx);
2719 651 : for (k = 1; k < i; k++)
2720 : {
2721 476 : GEN t = gadd(gmul(u,gcoeff(x,i,k)),gmul(v,gcoeff(x,j,k)));
2722 476 : gcoeff(x,j,k) = gsub(gmul(a,gcoeff(x,j,k)),gmul(b,gcoeff(x,i,k)));
2723 476 : gcoeff(x,i,k) = t;
2724 : }
2725 175 : gcoeff(x,j,i) = gen_0;
2726 175 : gcoeff(x,i,i) = d;
2727 175 : if (all) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
2728 175 : c = 1;
2729 : }
2730 315 : if (!c)
2731 : {
2732 196 : k = gsnf_no_divide(x, i, vx);
2733 196 : if (!k) break;
2734 :
2735 245 : for (j=1; j<=i; j++)
2736 196 : gcoeff(x,i,j) = gadd(gcoeff(x,i,j),gcoeff(x,k,j));
2737 49 : if (all) gel(U,i) = gadd(gel(U,i),gel(U,k));
2738 : }
2739 168 : if (gc_needed(av,1))
2740 : {
2741 0 : if (DEBUGMEM>1) pari_warn(warnmem,"gsmithall");
2742 0 : gerepileall(av, all? 3: 1, &x, &U, &V);
2743 : }
2744 : }
2745 : }
2746 245 : for (k=1; k<=n; k++)
2747 : {
2748 196 : GEN d, T = normalize_as_RgX(gcoeff(x,k,k), vx, &d);
2749 196 : if (gequal0(T)) continue;
2750 182 : if (all && !gequal1(d)) gel(V,k) = RgC_Rg_div(gel(V,k), d);
2751 182 : gcoeff(x,k,k) = T;
2752 : }
2753 49 : z = all? mkvec3(shallowtrans(U), V, x): RgM_diagonal_shallow(x);
2754 49 : return gerepilecopy(av, z);
2755 : }
2756 :
2757 : GEN
2758 469 : matsnf0(GEN x,long flag)
2759 : {
2760 469 : pari_sp av = avma;
2761 469 : if (flag > 7) pari_err_FLAG("matsnf");
2762 469 : if (typ(x) == t_VEC && flag & 4) return smithclean(x);
2763 469 : if (typ(x)!=t_MAT) pari_err_TYPE("matsnf",x);
2764 469 : if (RgM_is_ZM(x)) x = flag&1 ? smithall(x): smith(x);
2765 49 : else x = RgXM_snf(x, flag&1);
2766 469 : if (flag & 4) x = gerepileupto(av, smithclean(x));
2767 469 : return x;
2768 : }
2769 : GEN
2770 0 : gsmith(GEN x) { return RgXM_snf(x,0); }
2771 : GEN
2772 0 : gsmithall(GEN x) { return RgXM_snf(x,1); }
2773 :
2774 : /* H is a relation matrix, either in HNF or a t_VEC (diagonal HNF) */
2775 : static GEN
2776 943992 : snf_group(GEN H, GEN D, GEN *newU, GEN *newUi)
2777 : {
2778 : long i, j, l;
2779 :
2780 943992 : ZM_snfclean(D, newU? *newU: NULL, newUi? *newUi: NULL);
2781 943933 : l = lg(D);
2782 943933 : if (newU) {
2783 822203 : GEN U = *newU;
2784 2122069 : for (i = 1; i < l; i++)
2785 : {
2786 1300119 : GEN d = gel(D,i), d2 = shifti(d, 1);
2787 5083409 : for (j = 1; j < lg(U); j++)
2788 3783543 : gcoeff(U,i,j) = centermodii(gcoeff(U,i,j), d, d2);
2789 : }
2790 821950 : *newU = U;
2791 : }
2792 943680 : if (newUi && l > 1)
2793 : { /* UHV=D -> U^-1 = (HV)D^-1 -> U^-1 = H(VD^-1 mod 1) mod H */
2794 : /* Ui = ZM_inv(U, NULL); setlg(Ui, l); */
2795 856523 : GEN V = *newUi, Ui;
2796 856523 : int Hvec = (typ(H) == t_VEC);
2797 2335497 : for (i = 1; i < l; i++) gel(V,i) = FpC_red(gel(V,i), gel(D,i));
2798 856382 : if (!Hvec)
2799 : {
2800 555642 : if (ZM_isdiagonal(H)) { H = RgM_diagonal_shallow(H); Hvec = 1; }
2801 : }
2802 856634 : Ui = Hvec? ZM_diag_mul(H, V): ZM_mul(H, V);
2803 2335350 : for (i = 1; i < l; i++) gel(Ui,i) = ZC_Z_divexact(gel(Ui,i), gel(D,i));
2804 856338 : *newUi = Hvec? ZM_ZV_mod(Ui, H): ZM_hnfrem(Ui, H);
2805 : }
2806 943691 : return D;
2807 : }
2808 : /* H relation matrix among row of generators g in HNF. Let URV = D its SNF,
2809 : * newU R newV = newD its clean SNF (no 1 in Dnew). Return the diagonal of
2810 : * newD, newU and newUi such that 1/U = (newUi, ?).
2811 : * Rationale: let (G,0) = g Ui be the new generators then
2812 : * 0 = G U R --> G D = 0, g = G newU and G = g newUi */
2813 : GEN
2814 642705 : ZM_snf_group(GEN H, GEN *newU, GEN *newUi)
2815 : {
2816 642705 : GEN D = ZM_snfall_i(H, newU, newUi, 1 + 2);
2817 642788 : return snf_group(H, D, newU, newUi);
2818 : }
2819 :
2820 : /* D a ZV: SNF for matdiagonal(D). Faster because we only ensure elementary
2821 : * divisors condition: d[n] | ... | d[1] and need not convert D to matrix form*/
2822 : GEN
2823 301223 : ZV_snfall(GEN D, GEN *pU, GEN *pV)
2824 : {
2825 301223 : pari_sp av = avma;
2826 301223 : long j, n = lg(D)-1;
2827 301223 : GEN U = pU? matid(n): NULL;
2828 301224 : GEN V = pV? matid(n): NULL;
2829 : GEN p;
2830 :
2831 301219 : D = leafcopy(D);
2832 961036 : for (j = n; j > 0; j--)
2833 : {
2834 659819 : GEN b = gel(D,j);
2835 659819 : if (signe(b) < 0)
2836 : {
2837 0 : gel(D,j) = negi(b);
2838 0 : if (V) ZV_togglesign(gel(V,j));
2839 : }
2840 : }
2841 : /* entries are nonnegative integers */
2842 301217 : p = gen_indexsort(D, NULL, &negcmpii);
2843 301215 : D = vecpermute(D, p);
2844 301213 : if (U) U = vecpermute(U, p);
2845 301211 : if (V) V = vecpermute(V, p);
2846 : /* entries are sorted by decreasing value */
2847 960979 : for (j = n; j > 0; j--)
2848 : {
2849 659775 : GEN b = gel(D,j);
2850 : long i;
2851 1180877 : for (i = j-1; i > 0; i--)
2852 : { /* invariant: a >= b. If au+bv = d is a Bezout relation, A=a/d and B=b/d
2853 : * we have [B,-A;u,v]*diag(a,b)*[1-u*A,1; -u*A,1]] = diag(Ab, d) */
2854 533294 : GEN a = gel(D,i), u,v, d = bezout(a,b, &u,&v), A, Wi, Wj;
2855 533272 : if (equalii(d,b)) continue;
2856 70297 : A = diviiexact(a,d);
2857 70295 : if (V)
2858 : {
2859 70253 : GEN t = mulii(u,A);
2860 70252 : Wi = ZC_lincomb(subui(1,t), negi(t), gel(V,i), gel(V,j));
2861 70254 : Wj = ZC_add(gel(V,i), gel(V,j));
2862 70253 : gel(V,i) = Wi;
2863 70253 : gel(V,j) = Wj;
2864 : }
2865 70295 : if (U)
2866 : {
2867 70295 : GEN B = diviiexact(b,d);
2868 70294 : Wi = ZC_lincomb(B, negi(A), gel(U,i), gel(U,j));
2869 70295 : Wj = ZC_lincomb(u, v, gel(U,i), gel(U,j));
2870 70297 : gel(U,i) = Wi;
2871 70297 : gel(U,j) = Wj;
2872 : }
2873 70297 : gel(D,i) = mulii(A,b); /* lcm(a,b) */
2874 70294 : gel(D,j) = d; /* gcd(a,b) */
2875 70294 : b = gel(D,j); if (equali1(b)) break;
2876 : }
2877 : }
2878 301204 : snf_pile(av, &D,&U,&V);
2879 301234 : if (U) *pU = shallowtrans(U);
2880 301230 : if (V) *pV = V;
2881 301230 : return D;
2882 : }
2883 : GEN
2884 301222 : ZV_snf_group(GEN d, GEN *newU, GEN *newUi)
2885 : {
2886 301222 : GEN D = ZV_snfall(d, newU, newUi);
2887 301230 : return snf_group(d, D, newU, newUi);
2888 : }
2889 :
2890 : /* D a vector of elementary divisors. Truncate (setlg) to leave out trivial
2891 : * entries (= 1) */
2892 : void
2893 0 : ZV_snf_trunc(GEN D)
2894 : {
2895 0 : long i, l = lg(D);
2896 0 : for (i = 1; i < l; i++)
2897 0 : if (is_pm1(gel(D,i))) { setlg(D,i); break; }
2898 0 : }
2899 :
2900 : long
2901 0 : zv_snf_rank(GEN D, ulong p)
2902 : {
2903 0 : long i, l = lg(D);
2904 0 : if (!p) return l - 1;
2905 0 : for (i = 1; i < l; i++)
2906 0 : if (D[i] % p) break;
2907 0 : return i - 1;
2908 : }
2909 : long
2910 49 : ZV_snf_rank_u(GEN D, ulong p)
2911 : {
2912 49 : long i, l = lg(D);
2913 49 : while (l > 1 && D[l-1] == 1) l--;
2914 49 : if (!p) return l - 1;
2915 49 : if (p == 2)
2916 : {
2917 49 : for (i = 1; i < l; i++)
2918 42 : if (mpodd(gel(D,i))) break;
2919 : }
2920 35 : else if (!(p & (p-1)))
2921 : { /* power of 2 */
2922 14 : long n = vals(p);
2923 28 : for (i = 1; i < l; i++)
2924 28 : if (umodi2n(gel(D,i), n)) break;
2925 : }
2926 : else
2927 : {
2928 49 : for (i = 1; i < l; i++)
2929 42 : if (umodiu(gel(D,i), p)) break;
2930 : }
2931 49 : return i - 1;
2932 : }
2933 : long
2934 91 : ZV_snf_rank(GEN D, GEN p)
2935 : {
2936 91 : long i, l = lg(D);
2937 91 : if (lgefint(p) == 3) return ZV_snf_rank_u(D, p[2]);
2938 77 : while (l > 1 && equali1(gel(D, l-1))) l--;
2939 42 : if (!signe(p)) return l - 1;
2940 77 : for (i = 1; i < l; i++)
2941 70 : if (!dvdii(gel(D,i), p)) break;
2942 21 : return i - 1;
2943 : }
2944 : long
2945 154 : snfrank(GEN D, GEN p)
2946 : {
2947 : long i, l;
2948 154 : if (typ(D) != t_VEC) pari_err_TYPE("snfrank", D);
2949 154 : if (!p) p = gen_0;
2950 154 : l = lg(D);
2951 154 : if (l == 4 && typ(gel(D,3)) == t_MAT)
2952 : { /* from matsnf(,1) */
2953 14 : pari_sp av = avma;
2954 : long z;
2955 : GEN v;
2956 14 : D = gel(D,3); l = lg(D);
2957 14 : if (l == 1) return 0;
2958 14 : z = lgcols(D) - l; /* missing columns of 0s */
2959 14 : if (z < 0) pari_err_TYPE("snfrank", D);
2960 14 : v = cgetg(l, t_VEC);
2961 35 : for (i = 1; i < l; i++) gel(v, i) = gcoeff(D, i + z, i);
2962 14 : return gc_long(av, z + snfrank(v, p));
2963 : }
2964 140 : switch(typ(p))
2965 : {
2966 98 : case t_INT:
2967 98 : if (RgV_is_ZV(D)) return ZV_snf_rank(D, p);
2968 7 : if (!signe(p)) break; /* allow p = 0 */
2969 0 : pari_err_TYPE("snfrank", D);
2970 42 : case t_POL: break;
2971 0 : default: pari_err_TYPE("snfrank", p);
2972 : }
2973 175 : while (l > 1 && gequal1(gel(D, l-1))) l--;
2974 49 : if (gequal0(p)) return l - 1;
2975 112 : for (i = 1; i < l; i++)
2976 91 : if (!gdvd(gel(D,i), p)) break;
2977 42 : return i - 1;
2978 : }
|