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 : static int
19 1932385 : check_ZV(GEN x, long l)
20 : {
21 : long i;
22 13037589 : for (i=1; i<l; i++)
23 11105260 : if (typ(gel(x,i)) != t_INT) return 0;
24 1932329 : return 1;
25 : }
26 : void
27 1423595 : RgV_check_ZV(GEN A, const char *s)
28 : {
29 1423595 : if (!RgV_is_ZV(A)) pari_err_TYPE(stack_strcat(s," [integer vector]"), A);
30 1423588 : }
31 : void
32 651139 : RgM_check_ZM(GEN A, const char *s)
33 : {
34 651139 : long n = lg(A);
35 651139 : if (n != 1)
36 : {
37 651006 : long j, m = lgcols(A);
38 2583334 : for (j=1; j<n; j++)
39 1932385 : if (!check_ZV(gel(A,j), m))
40 56 : pari_err_TYPE(stack_strcat(s," [integer matrix]"), A);
41 : }
42 651082 : }
43 :
44 : /* assume m > 1 */
45 : static long
46 108054879 : ZV_max_lg_i(GEN x, long m)
47 : {
48 108054879 : long i, l = lgefint(gel(x,1));
49 865425037 : for (i = 2; i < m; i++) l = maxss(l, lgefint(gel(x,i)));
50 108055481 : return l;
51 : }
52 : long
53 10640 : ZV_max_lg(GEN x)
54 : {
55 10640 : long m = lg(x);
56 10640 : return m == 1? 2: ZV_max_lg_i(x, m);
57 : }
58 :
59 : /* assume n > 1 and m > 1 */
60 : static long
61 26751573 : ZM_max_lg_i(GEN x, long n, long m)
62 : {
63 26751573 : long j, l = ZV_max_lg_i(gel(x,1), m);
64 108044933 : for (j = 2; j < n; j++) l = maxss(l, ZV_max_lg_i(gel(x,j), m));
65 26751887 : return l;
66 : }
67 : long
68 22172 : ZM_max_lg(GEN x)
69 : {
70 22172 : long n = lg(x), m;
71 22172 : if (n == 1) return 2;
72 22172 : m = lgcols(x); return m == 1? 2: ZM_max_lg_i(x, n, m);
73 : }
74 :
75 : /* assume m > 1 */
76 : static long
77 0 : ZV_max_expi_i(GEN x, long m)
78 : {
79 0 : long i, prec = expi(gel(x,1));
80 0 : for (i = 2; i < m; i++) prec = maxss(prec, expi(gel(x,i)));
81 0 : return prec;
82 : }
83 : long
84 0 : ZV_max_expi(GEN x)
85 : {
86 0 : long m = lg(x);
87 0 : return m == 1? 2: ZV_max_expi_i(x, m);
88 : }
89 :
90 : /* assume n > 1 and m > 1 */
91 : static long
92 0 : ZM_max_expi_i(GEN x, long n, long m)
93 : {
94 0 : long j, prec = ZV_max_expi_i(gel(x,1), m);
95 0 : for (j = 2; j < n; j++) prec = maxss(prec, ZV_max_expi_i(gel(x,j), m));
96 0 : return prec;
97 : }
98 : long
99 0 : ZM_max_expi(GEN x)
100 : {
101 0 : long n = lg(x), m;
102 0 : if (n == 1) return 2;
103 0 : m = lgcols(x); return m == 1? 2: ZM_max_expi_i(x, n, m);
104 : }
105 :
106 : GEN
107 4162 : ZM_supnorm(GEN x)
108 : {
109 4162 : long i, j, h, lx = lg(x);
110 4162 : GEN s = gen_0;
111 4162 : if (lx == 1) return gen_1;
112 4162 : h = lgcols(x);
113 25549 : for (j=1; j<lx; j++)
114 : {
115 21387 : GEN xj = gel(x,j);
116 290468 : for (i=1; i<h; i++)
117 : {
118 269081 : GEN c = gel(xj,i);
119 269081 : if (abscmpii(c, s) > 0) s = c;
120 : }
121 : }
122 4162 : return absi(s);
123 : }
124 :
125 : /********************************************************************/
126 : /** **/
127 : /** MULTIPLICATION **/
128 : /** **/
129 : /********************************************************************/
130 : /* x nonempty ZM, y a compatible nc (dimension > 0). */
131 : static GEN
132 0 : ZM_nc_mul_i(GEN x, GEN y, long c, long l)
133 : {
134 : long i, j;
135 : pari_sp av;
136 0 : GEN z = cgetg(l,t_COL), s;
137 :
138 0 : for (i=1; i<l; i++)
139 : {
140 0 : av = avma; s = muliu(gcoeff(x,i,1),y[1]);
141 0 : for (j=2; j<c; j++)
142 0 : if (y[j]) s = addii(s, muliu(gcoeff(x,i,j),y[j]));
143 0 : gel(z,i) = gerepileuptoint(av,s);
144 : }
145 0 : return z;
146 : }
147 :
148 : /* x ZV, y a compatible zc. */
149 : GEN
150 2229528 : ZV_zc_mul(GEN x, GEN y)
151 : {
152 2229528 : long j, l = lg(x);
153 2229528 : pari_sp av = avma;
154 2229528 : GEN s = mulis(gel(x,1),y[1]);
155 50292998 : for (j=2; j<l; j++)
156 48063470 : if (y[j]) s = addii(s, mulis(gel(x,j),y[j]));
157 2229528 : return gerepileuptoint(av,s);
158 : }
159 :
160 : /* x nonempty ZM, y a compatible zc (dimension > 0). */
161 : static GEN
162 19276608 : ZM_zc_mul_i(GEN x, GEN y, long c, long l)
163 : {
164 : long i, j;
165 19276608 : GEN z = cgetg(l,t_COL);
166 :
167 121732129 : for (i=1; i<l; i++)
168 : {
169 102456270 : pari_sp av = avma;
170 102456270 : GEN s = mulis(gcoeff(x,i,1),y[1]);
171 1170717257 : for (j=2; j<c; j++)
172 1068253254 : if (y[j]) s = addii(s, mulis(gcoeff(x,i,j),y[j]));
173 102464003 : gel(z,i) = gerepileuptoint(av,s);
174 : }
175 19275859 : return z;
176 : }
177 : GEN
178 17212670 : ZM_zc_mul(GEN x, GEN y) {
179 17212670 : long l = lg(x);
180 17212670 : if (l == 1) return cgetg(1, t_COL);
181 17212670 : return ZM_zc_mul_i(x,y, l, lgcols(x));
182 : }
183 :
184 : /* y nonempty ZM, x a compatible zv (dimension > 0). */
185 : GEN
186 1736 : zv_ZM_mul(GEN x, GEN y) {
187 1736 : long i,j, lx = lg(x), ly = lg(y);
188 : GEN z;
189 1736 : if (lx == 1) return zerovec(ly-1);
190 1736 : z = cgetg(ly,t_VEC);
191 4046 : for (j=1; j<ly; j++)
192 : {
193 2310 : pari_sp av = avma;
194 2310 : GEN s = mulsi(x[1], gcoeff(y,1,j));
195 3990 : for (i=2; i<lx; i++)
196 1680 : if (x[i]) s = addii(s, mulsi(x[i], gcoeff(y,i,j)));
197 2310 : gel(z,j) = gerepileuptoint(av,s);
198 : }
199 1736 : return z;
200 : }
201 :
202 : /* x ZM, y a compatible zm (dimension > 0). */
203 : GEN
204 975358 : ZM_zm_mul(GEN x, GEN y)
205 : {
206 975358 : long j, c, l = lg(x), ly = lg(y);
207 975358 : GEN z = cgetg(ly, t_MAT);
208 975358 : if (l == 1) return z;
209 975351 : c = lgcols(x);
210 3039293 : for (j = 1; j < ly; j++) gel(z,j) = ZM_zc_mul_i(x, gel(y,j), l,c);
211 975349 : return z;
212 : }
213 : /* x ZM, y a compatible zn (dimension > 0). */
214 : GEN
215 0 : ZM_nm_mul(GEN x, GEN y)
216 : {
217 0 : long j, c, l = lg(x), ly = lg(y);
218 0 : GEN z = cgetg(ly, t_MAT);
219 0 : if (l == 1) return z;
220 0 : c = lgcols(x);
221 0 : for (j = 1; j < ly; j++) gel(z,j) = ZM_nc_mul_i(x, gel(y,j), l,c);
222 0 : return z;
223 : }
224 :
225 : /* Strassen-Winograd algorithm */
226 :
227 : /* Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
228 : * as an (m x n)-matrix, padding the input with zeroes as necessary. */
229 : static GEN
230 585960 : add_slices(long m, long n,
231 : GEN A, long ma, long da, long na, long ea,
232 : GEN B, long mb, long db, long nb, long eb)
233 : {
234 585960 : long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
235 585960 : GEN M = cgetg(n + 1, t_MAT), C;
236 :
237 4916962 : for (j = 1; j <= min_e; j++) {
238 4331002 : gel(M, j) = C = cgetg(m + 1, t_COL);
239 84610477 : for (i = 1; i <= min_d; i++)
240 80279475 : gel(C, i) = addii(gcoeff(A, ma + i, na + j),
241 80279475 : gcoeff(B, mb + i, nb + j));
242 4397371 : for (; i <= da; i++) gel(C, i) = gcoeff(A, ma + i, na + j);
243 4331002 : for (; i <= db; i++) gel(C, i) = gcoeff(B, mb + i, nb + j);
244 4331002 : for (; i <= m; i++) gel(C, i) = gen_0;
245 : }
246 645356 : for (; j <= ea; j++) {
247 59396 : gel(M, j) = C = cgetg(m + 1, t_COL);
248 208387 : for (i = 1; i <= da; i++) gel(C, i) = gcoeff(A, ma + i, na + j);
249 59396 : for (; i <= m; i++) gel(C, i) = gen_0;
250 : }
251 585960 : for (; j <= eb; j++) {
252 0 : gel(M, j) = C = cgetg(m + 1, t_COL);
253 0 : for (i = 1; i <= db; i++) gel(C, i) = gcoeff(B, mb + i, nb + j);
254 0 : for (; i <= m; i++) gel(C, i) = gen_0;
255 : }
256 585960 : for (; j <= n; j++) gel(M, j) = zerocol(m);
257 585960 : return M;
258 : }
259 :
260 : /* Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
261 : * as an (m x n)-matrix, padding the input with zeroes as necessary. */
262 : static GEN
263 512715 : subtract_slices(long m, long n,
264 : GEN A, long ma, long da, long na, long ea,
265 : GEN B, long mb, long db, long nb, long eb)
266 : {
267 512715 : long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
268 512715 : GEN M = cgetg(n + 1, t_MAT), C;
269 :
270 4318632 : for (j = 1; j <= min_e; j++) {
271 3805917 : gel(M, j) = C = cgetg(m + 1, t_COL);
272 75742408 : for (i = 1; i <= min_d; i++)
273 71936491 : gel(C, i) = subii(gcoeff(A, ma + i, na + j),
274 71936491 : gcoeff(B, mb + i, nb + j));
275 3863018 : for (; i <= da; i++) gel(C, i) = gcoeff(A, ma + i, na + j);
276 3893970 : for (; i <= db; i++) gel(C, i) = negi(gcoeff(B, mb + i, nb + j));
277 3805917 : for (; i <= m; i++) gel(C, i) = gen_0;
278 : }
279 512715 : for (; j <= ea; j++) {
280 0 : gel(M, j) = C = cgetg(m + 1, t_COL);
281 0 : for (i = 1; i <= da; i++) gel(C, i) = gcoeff(A, ma + i, na + j);
282 0 : for (; i <= m; i++) gel(C, i) = gen_0;
283 : }
284 547704 : for (; j <= eb; j++) {
285 34989 : gel(M, j) = C = cgetg(m + 1, t_COL);
286 127034 : for (i = 1; i <= db; i++) gel(C, i) = negi(gcoeff(B, mb + i, nb + j));
287 34989 : for (; i <= m; i++) gel(C, i) = gen_0;
288 : }
289 547704 : for (; j <= n; j++) gel(M, j) = zerocol(m);
290 512715 : return M;
291 : }
292 :
293 : static GEN ZM_mul_i(GEN x, GEN y, long l, long lx, long ly);
294 :
295 : /* Strassen-Winograd matrix product A (m x n) * B (n x p) */
296 : static GEN
297 73245 : ZM_mul_sw(GEN A, GEN B, long m, long n, long p)
298 : {
299 73245 : pari_sp av = avma;
300 73245 : long m1 = (m + 1)/2, m2 = m/2,
301 73245 : n1 = (n + 1)/2, n2 = n/2,
302 73245 : p1 = (p + 1)/2, p2 = p/2;
303 : GEN A11, A12, A22, B11, B21, B22,
304 : S1, S2, S3, S4, T1, T2, T3, T4,
305 : M1, M2, M3, M4, M5, M6, M7,
306 : V1, V2, V3, C11, C12, C21, C22, C;
307 :
308 73245 : T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2);
309 73245 : S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1);
310 73245 : M2 = ZM_mul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1);
311 73245 : if (gc_needed(av, 1))
312 0 : gerepileall(av, 2, &T2, &M2); /* destroy S1 */
313 73245 : T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1);
314 73245 : if (gc_needed(av, 1))
315 0 : gerepileall(av, 2, &M2, &T3); /* destroy T2 */
316 73245 : S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2);
317 73245 : T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2);
318 73245 : M3 = ZM_mul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1);
319 73245 : if (gc_needed(av, 1))
320 0 : gerepileall(av, 4, &M2, &T3, &S2, &M3); /* destroy T1 */
321 73245 : S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1);
322 73245 : if (gc_needed(av, 1))
323 0 : gerepileall(av, 4, &M2, &T3, &M3, &S3); /* destroy S2 */
324 73245 : A11 = matslice(A, 1, m1, 1, n1);
325 73245 : B11 = matslice(B, 1, n1, 1, p1);
326 73245 : M1 = ZM_mul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1);
327 73245 : if (gc_needed(av, 1))
328 0 : gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1); /* destroy A11, B11 */
329 73245 : A12 = matslice(A, 1, m1, n1 + 1, n);
330 73245 : B21 = matslice(B, n1 + 1, n, 1, p1);
331 73245 : M4 = ZM_mul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1);
332 73245 : if (gc_needed(av, 1))
333 0 : gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4); /* destroy A12, B21 */
334 73245 : C11 = add_slices(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1);
335 73245 : if (gc_needed(av, 1))
336 0 : gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &C11); /* destroy M4 */
337 73245 : M5 = ZM_mul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1);
338 73245 : S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2);
339 73245 : if (gc_needed(av, 1))
340 5 : gerepileall(av, 7, &M2, &T3, &M3, &M1, &C11, &M5, &S4); /* destroy S3 */
341 73245 : T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1);
342 73245 : if (gc_needed(av, 1))
343 0 : gerepileall(av, 7, &M2, &M3, &M1, &C11, &M5, &S4, &T4); /* destroy T3 */
344 73245 : V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1);
345 73245 : if (gc_needed(av, 1))
346 1 : gerepileall(av, 6, &M2, &M3, &S4, &T4, &C11, &V1); /* destroy M1, M5 */
347 73245 : B22 = matslice(B, n1 + 1, n, p1 + 1, p);
348 73245 : M6 = ZM_mul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1);
349 73245 : if (gc_needed(av, 1))
350 6 : gerepileall(av, 6, &M2, &M3, &T4, &C11, &V1, &M6); /* destroy S4, B22 */
351 73245 : A22 = matslice(A, m1 + 1, m, n1 + 1, n);
352 73245 : M7 = ZM_mul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1);
353 73245 : if (gc_needed(av, 1))
354 0 : gerepileall(av, 6, &M2, &M3, &C11, &V1, &M6, &M7); /* destroy A22, T4 */
355 73245 : V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2);
356 73245 : C12 = add_slices(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2);
357 73245 : if (gc_needed(av, 1))
358 6 : gerepileall(av, 6, &M2, &M3, &C11, &V1, &M7, &C12); /* destroy V3, M6 */
359 73245 : V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2);
360 73245 : if (gc_needed(av, 1))
361 0 : gerepileall(av, 5, &M3, &C11, &M7, &C12, &V2); /* destroy V1, M2 */
362 73245 : C21 = add_slices(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1);
363 73245 : if (gc_needed(av, 1))
364 6 : gerepileall(av, 5, &M3, &C11, &C12, &V2, &C21); /* destroy M7 */
365 73245 : C22 = add_slices(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2);
366 73245 : if (gc_needed(av, 1))
367 0 : gerepileall(av, 4, &C11, &C12, &C21, &C22); /* destroy V2, M3 */
368 73245 : C = shallowconcat(vconcat(C11, C21), vconcat(C12, C22));
369 73245 : return gerepilecopy(av, C);
370 : }
371 :
372 : /* x[i,]*y. Assume lg(x) > 1 and 0 < i < lgcols(x) */
373 : static GEN
374 573251599 : ZMrow_ZC_mul_i(GEN x, GEN y, long i, long lx)
375 : {
376 573251599 : pari_sp av = avma;
377 573251599 : GEN c = mulii(gcoeff(x,i,1), gel(y,1)), ZERO = gen_0;
378 : long k;
379 6630951071 : for (k = 2; k < lx; k++)
380 : {
381 6058055822 : GEN t = mulii(gcoeff(x,i,k), gel(y,k));
382 6056634648 : if (t != ZERO) c = addii(c, t);
383 : }
384 572895249 : return gerepileuptoint(av, c);
385 : }
386 : GEN
387 135032952 : ZMrow_ZC_mul(GEN x, GEN y, long i)
388 135032952 : { return ZMrow_ZC_mul_i(x, y, i, lg(x)); }
389 :
390 : /* return x * y, 1 < lx = lg(x), l = lgcols(x) */
391 : static GEN
392 74536524 : ZM_ZC_mul_i(GEN x, GEN y, long lx, long l)
393 : {
394 74536524 : GEN z = cgetg(l,t_COL);
395 : long i;
396 512780530 : for (i=1; i<l; i++) gel(z,i) = ZMrow_ZC_mul_i(x,y,i,lx);
397 74495656 : return z;
398 : }
399 :
400 : static GEN
401 13302603 : ZM_mul_classical(GEN x, GEN y, long l, long lx, long ly)
402 : {
403 : long j;
404 13302603 : GEN z = cgetg(ly, t_MAT);
405 63746587 : for (j = 1; j < ly; j++)
406 50446798 : gel(z, j) = ZM_ZC_mul_i(x, gel(y, j), lx, l);
407 13299789 : return z;
408 : }
409 :
410 : static GEN
411 1104 : ZM_mul_slice(GEN A, GEN B, GEN P, GEN *mod)
412 : {
413 1104 : pari_sp av = avma;
414 1104 : long i, n = lg(P)-1;
415 : GEN H, T;
416 1104 : if (n == 1)
417 : {
418 0 : ulong p = uel(P,1);
419 0 : GEN a = ZM_to_Flm(A, p);
420 0 : GEN b = ZM_to_Flm(B, p);
421 0 : GEN Hp = gerepileupto(av, Flm_to_ZM(Flm_mul(a, b, p)));
422 0 : *mod = utoi(p); return Hp;
423 : }
424 1104 : T = ZV_producttree(P);
425 1105 : A = ZM_nv_mod_tree(A, P, T);
426 1105 : B = ZM_nv_mod_tree(B, P, T);
427 1105 : H = cgetg(n+1, t_VEC);
428 7878 : for(i=1; i <= n; i++)
429 6773 : gel(H,i) = Flm_mul(gel(A,i),gel(B,i),P[i]);
430 1105 : H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
431 1105 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
432 : }
433 :
434 : GEN
435 1104 : ZM_mul_worker(GEN P, GEN A, GEN B)
436 : {
437 1104 : GEN V = cgetg(3, t_VEC);
438 1104 : gel(V,1) = ZM_mul_slice(A, B, P, &gel(V,2));
439 1105 : return V;
440 : }
441 :
442 : static GEN
443 839 : ZM_mul_fast(GEN A, GEN B, long lA, long lB, long sA, long sB)
444 : {
445 839 : pari_sp av = avma;
446 : forprime_t S;
447 : GEN H, worker;
448 : long h;
449 839 : if (sA == 2 || sB == 2) return zeromat(nbrows(A),lB-1);
450 827 : h = 1 + (sA + sB - 4) * BITS_IN_LONG + expu(lA-1);
451 827 : init_modular_big(&S);
452 827 : worker = snm_closure(is_entry("_ZM_mul_worker"), mkvec2(A,B));
453 827 : H = gen_crt("ZM_mul", worker, &S, NULL, h, 0, NULL,
454 : nmV_chinese_center, FpM_center);
455 827 : return gerepileupto(av, H);
456 : }
457 :
458 : /* s = min(log_BIL |x|, log_BIL |y|), use Strassen-Winograd when
459 : * min(dims) > B */
460 : static long
461 13375844 : sw_bound(long s)
462 13375844 : { return s > 60 ? 2: s > 25 ? 4: s > 15 ? 8 : s > 8 ? 16 : 32; }
463 :
464 : /* assume lx > 1 and ly > 1; x is (l-1) x (lx-1), y is (lx-1) x (ly-1).
465 : * Return x * y */
466 : static GEN
467 20680731 : ZM_mul_i(GEN x, GEN y, long l, long lx, long ly)
468 : {
469 : long sx, sy, B;
470 : #ifdef LONG_IS_64BIT /* From Flm_mul_i */
471 17554363 : long Flm_sw_bound = 70;
472 : #else
473 3126368 : long Flm_sw_bound = 120;
474 : #endif
475 20680731 : if (l == 1) return zeromat(0, ly-1);
476 20678715 : if (lx==2 && l==2 && ly==2)
477 359159 : { retmkmat(mkcol(mulii(gcoeff(x,1,1), gcoeff(y,1,1)))); }
478 20319556 : if (lx==3 && l==3 && ly==3) return ZM2_mul(x, y);
479 13352468 : sx = ZM_max_lg_i(x, lx, l);
480 13353206 : sy = ZM_max_lg_i(y, ly, lx);
481 : /* Use modular reconstruction if Flm_mul would use Strassen and the input
482 : * sizes look balanced */
483 13353250 : if (lx > Flm_sw_bound && ly > Flm_sw_bound && l > Flm_sw_bound
484 851 : && sx <= 10 * sy && sy <= 10 * sx) return ZM_mul_fast(x,y, lx,ly, sx,sy);
485 :
486 13352411 : B = sw_bound(minss(sx, sy));
487 13352412 : if (l <= B || lx <= B || ly <= B)
488 13279197 : return ZM_mul_classical(x, y, l, lx, ly);
489 : else
490 73215 : return ZM_mul_sw(x, y, l - 1, lx - 1, ly - 1);
491 : }
492 :
493 : GEN
494 20304645 : ZM_mul(GEN x, GEN y)
495 : {
496 20304645 : long lx = lg(x), ly = lg(y);
497 20304645 : if (ly == 1) return cgetg(1,t_MAT);
498 20169555 : if (lx == 1) return zeromat(0, ly-1);
499 20167973 : return ZM_mul_i(x, y, lgcols(x), lx, ly);
500 : }
501 :
502 : static GEN
503 0 : ZM_sqr_slice(GEN A, GEN P, GEN *mod)
504 : {
505 0 : pari_sp av = avma;
506 0 : long i, n = lg(P)-1;
507 : GEN H, T;
508 0 : if (n == 1)
509 : {
510 0 : ulong p = uel(P,1);
511 0 : GEN a = ZM_to_Flm(A, p);
512 0 : GEN Hp = gerepileupto(av, Flm_to_ZM(Flm_sqr(a, p)));
513 0 : *mod = utoi(p); return Hp;
514 : }
515 0 : T = ZV_producttree(P);
516 0 : A = ZM_nv_mod_tree(A, P, T);
517 0 : H = cgetg(n+1, t_VEC);
518 0 : for(i=1; i <= n; i++)
519 0 : gel(H,i) = Flm_sqr(gel(A,i), P[i]);
520 0 : H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
521 0 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
522 : }
523 :
524 : GEN
525 0 : ZM_sqr_worker(GEN P, GEN A)
526 : {
527 0 : GEN V = cgetg(3, t_VEC);
528 0 : gel(V,1) = ZM_sqr_slice(A, P, &gel(V,2));
529 0 : return V;
530 : }
531 :
532 : static GEN
533 0 : ZM_sqr_fast(GEN A, long l, long s)
534 : {
535 0 : pari_sp av = avma;
536 : forprime_t S;
537 : GEN H, worker;
538 : long h;
539 0 : if (s == 2) return zeromat(l-1,l-1);
540 0 : h = 1 + (2*s - 4) * BITS_IN_LONG + expu(l-1);
541 0 : init_modular_big(&S);
542 0 : worker = snm_closure(is_entry("_ZM_sqr_worker"), mkvec(A));
543 0 : H = gen_crt("ZM_sqr", worker, &S, NULL, h, 0, NULL,
544 : nmV_chinese_center, FpM_center);
545 0 : return gerepileupto(av, H);
546 : }
547 :
548 : GEN
549 553869 : QM_mul(GEN x, GEN y)
550 : {
551 553869 : GEN dx, nx = Q_primitive_part(x, &dx);
552 553869 : GEN dy, ny = Q_primitive_part(y, &dy);
553 553869 : GEN z = ZM_mul(nx, ny);
554 553869 : if (dx || dy)
555 : {
556 469754 : GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
557 469754 : if (!gequal1(d)) z = ZM_Q_mul(z, d);
558 : }
559 553869 : return z;
560 : }
561 :
562 : GEN
563 700 : QM_sqr(GEN x)
564 : {
565 700 : GEN dx, nx = Q_primitive_part(x, &dx);
566 700 : GEN z = ZM_sqr(nx);
567 700 : if (dx)
568 700 : z = ZM_Q_mul(z, gsqr(dx));
569 700 : return z;
570 : }
571 :
572 : GEN
573 143013 : QM_QC_mul(GEN x, GEN y)
574 : {
575 143013 : GEN dx, nx = Q_primitive_part(x, &dx);
576 143013 : GEN dy, ny = Q_primitive_part(y, &dy);
577 143013 : GEN z = ZM_ZC_mul(nx, ny);
578 143013 : if (dx || dy)
579 : {
580 142985 : GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
581 142985 : if (!gequal1(d)) z = ZC_Q_mul(z, d);
582 : }
583 143013 : return z;
584 : }
585 :
586 : /* assume result is symmetric */
587 : GEN
588 0 : ZM_multosym(GEN x, GEN y)
589 : {
590 0 : long j, lx, ly = lg(y);
591 : GEN M;
592 0 : if (ly == 1) return cgetg(1,t_MAT);
593 0 : lx = lg(x); /* = lgcols(y) */
594 0 : if (lx == 1) return cgetg(1,t_MAT);
595 : /* ly = lgcols(x) */
596 0 : M = cgetg(ly, t_MAT);
597 0 : for (j=1; j<ly; j++)
598 : {
599 0 : GEN z = cgetg(ly,t_COL), yj = gel(y,j);
600 : long i;
601 0 : for (i=1; i<j; i++) gel(z,i) = gcoeff(M,j,i);
602 0 : for (i=j; i<ly; i++)gel(z,i) = ZMrow_ZC_mul_i(x,yj,i,lx);
603 0 : gel(M,j) = z;
604 : }
605 0 : return M;
606 : }
607 :
608 : /* compute m*diagonal(d), assume lg(d) = lg(m). Shallow */
609 : GEN
610 21 : ZM_mul_diag(GEN m, GEN d)
611 : {
612 : long j, l;
613 21 : GEN y = cgetg_copy(m, &l);
614 77 : for (j=1; j<l; j++)
615 : {
616 56 : GEN c = gel(d,j);
617 56 : gel(y,j) = equali1(c)? gel(m,j): ZC_Z_mul(gel(m,j), c);
618 : }
619 21 : return y;
620 : }
621 : /* compute diagonal(d)*m, assume lg(d) = lg(m~). Shallow */
622 : GEN
623 533885 : ZM_diag_mul(GEN d, GEN m)
624 : {
625 533885 : long i, j, l = lg(d), lm = lg(m);
626 533885 : GEN y = cgetg(lm, t_MAT);
627 1521248 : for (j=1; j<lm; j++) gel(y,j) = cgetg(l, t_COL);
628 1647760 : for (i=1; i<l; i++)
629 : {
630 1114145 : GEN c = gel(d,i);
631 1114145 : if (equali1(c))
632 236686 : for (j=1; j<lm; j++) gcoeff(y,i,j) = gcoeff(m,i,j);
633 : else
634 3399954 : for (j=1; j<lm; j++) gcoeff(y,i,j) = mulii(gcoeff(m,i,j), c);
635 : }
636 533615 : return y;
637 : }
638 :
639 : /* assume lx > 1 is lg(x) = lg(y) */
640 : static GEN
641 19175631 : ZV_dotproduct_i(GEN x, GEN y, long lx)
642 : {
643 19175631 : pari_sp av = avma;
644 19175631 : GEN c = mulii(gel(x,1), gel(y,1));
645 : long i;
646 144147341 : for (i = 2; i < lx; i++)
647 : {
648 124972970 : GEN t = mulii(gel(x,i), gel(y,i));
649 124972342 : if (t != gen_0) c = addii(c, t);
650 : }
651 19174371 : return gerepileuptoint(av, c);
652 : }
653 :
654 : /* x~ * y, assuming result is symmetric */
655 : GEN
656 532221 : ZM_transmultosym(GEN x, GEN y)
657 : {
658 532221 : long i, j, l, ly = lg(y);
659 : GEN M;
660 532221 : if (ly == 1) return cgetg(1,t_MAT);
661 : /* lg(x) = ly */
662 532221 : l = lgcols(y); /* = lgcols(x) */
663 532221 : M = cgetg(ly, t_MAT);
664 2708730 : for (i=1; i<ly; i++)
665 : {
666 2176509 : GEN xi = gel(x,i), c = cgetg(ly,t_COL);
667 2176509 : gel(M,i) = c;
668 7109000 : for (j=1; j<i; j++)
669 4932491 : gcoeff(M,i,j) = gel(c,j) = ZV_dotproduct_i(xi,gel(y,j),l);
670 2176509 : gel(c,i) = ZV_dotproduct_i(xi,gel(y,i),l);
671 : }
672 532221 : return M;
673 : }
674 : /* x~ * y */
675 : GEN
676 2289 : ZM_transmul(GEN x, GEN y)
677 : {
678 2289 : long i, j, l, lx, ly = lg(y);
679 : GEN M;
680 2289 : if (ly == 1) return cgetg(1,t_MAT);
681 2289 : lx = lg(x);
682 2289 : l = lgcols(y);
683 2289 : if (lgcols(x) != l) pari_err_OP("operation 'ZM_transmul'", x,y);
684 2289 : M = cgetg(ly, t_MAT);
685 6993 : for (i=1; i<ly; i++)
686 : {
687 4704 : GEN yi = gel(y,i), c = cgetg(lx,t_COL);
688 4704 : gel(M,i) = c;
689 12229 : for (j=1; j<lx; j++) gel(c,j) = ZV_dotproduct_i(yi,gel(x,j),l);
690 : }
691 2289 : return M;
692 : }
693 :
694 : /* assume l > 1; x is (l-1) x (l-1), return x^2.
695 : * FIXME: we ultimately rely on Strassen-Winograd which uses 7M + 15A.
696 : * Should use Bodrato's variant of Winograd, using 3M + 4S + 11A */
697 : static GEN
698 24213 : ZM_sqr_i(GEN x, long l)
699 : {
700 : long s;
701 24213 : if (l == 2) { retmkmat(mkcol(sqri(gcoeff(x,1,1)))); }
702 24213 : if (l == 3) return ZM2_sqr(x);
703 23436 : s = ZM_max_lg_i(x, l, l);
704 23436 : if (l > 70) return ZM_sqr_fast(x, l, s);
705 23436 : if (l <= sw_bound(s))
706 23406 : return ZM_mul_classical(x, x, l, l, l);
707 : else
708 30 : return ZM_mul_sw(x, x, l - 1, l - 1, l - 1);
709 : }
710 :
711 : GEN
712 24213 : ZM_sqr(GEN x)
713 : {
714 24213 : long lx=lg(x);
715 24213 : if (lx==1) return cgetg(1,t_MAT);
716 24213 : return ZM_sqr_i(x, lx);
717 : }
718 : GEN
719 24178360 : ZM_ZC_mul(GEN x, GEN y)
720 : {
721 24178360 : long lx = lg(x);
722 24178360 : return lx==1? cgetg(1,t_COL): ZM_ZC_mul_i(x, y, lx, lgcols(x));
723 : }
724 :
725 : GEN
726 4527789 : ZC_Z_div(GEN x, GEN c)
727 20035791 : { pari_APPLY_type(t_COL, Qdivii(gel(x,i), c)) }
728 :
729 : GEN
730 17553 : ZM_Z_div(GEN x, GEN c)
731 202068 : { pari_APPLY_same(ZC_Z_div(gel(x, i), c)) }
732 :
733 : GEN
734 2723354 : ZC_Q_mul(GEN A, GEN z)
735 : {
736 2723354 : pari_sp av = avma;
737 2723354 : long i, l = lg(A);
738 : GEN d, n, Ad, B, u;
739 2723354 : if (typ(z)==t_INT) return ZC_Z_mul(A,z);
740 2717096 : n = gel(z, 1); d = gel(z, 2);
741 2717096 : Ad = FpC_red(A, d);
742 2717050 : u = gcdii(d, FpV_factorback(Ad, NULL, d));
743 2717044 : B = cgetg(l, t_COL);
744 2717044 : if (equali1(u))
745 : {
746 421964 : for(i=1; i<l; i++)
747 355445 : gel(B, i) = mkfrac(mulii(n, gel(A,i)), d);
748 : } else
749 : {
750 18462534 : for(i=1; i<l; i++)
751 : {
752 15812087 : GEN di = gcdii(gel(Ad, i), u), ni = mulii(n, diviiexact(gel(A,i), di));
753 15812012 : if (equalii(d, di))
754 10850113 : gel(B, i) = ni;
755 : else
756 4961880 : gel(B, i) = mkfrac(ni, diviiexact(d, di));
757 : }
758 : }
759 2716966 : return gerepilecopy(av, B);
760 : }
761 :
762 : GEN
763 1101714 : ZM_Q_mul(GEN x, GEN z)
764 : {
765 1101714 : if (typ(z)==t_INT) return ZM_Z_mul(x,z);
766 3245367 : pari_APPLY_same(ZC_Q_mul(gel(x, i), z));
767 : }
768 :
769 : long
770 196399119 : zv_dotproduct(GEN x, GEN y)
771 : {
772 196399119 : long i, lx = lg(x);
773 : ulong c;
774 196399119 : if (lx == 1) return 0;
775 196399119 : c = uel(x,1)*uel(y,1);
776 3047359154 : for (i = 2; i < lx; i++)
777 2850960035 : c += uel(x,i)*uel(y,i);
778 196399119 : return c;
779 : }
780 :
781 : GEN
782 230629 : ZV_ZM_mul(GEN x, GEN y)
783 : {
784 230629 : long i, lx = lg(x), ly = lg(y);
785 : GEN z;
786 230629 : if (lx == 1) return zerovec(ly-1);
787 230510 : z = cgetg(ly, t_VEC);
788 882063 : for (i = 1; i < ly; i++) gel(z,i) = ZV_dotproduct_i(x, gel(y,i), lx);
789 230510 : return z;
790 : }
791 :
792 : GEN
793 0 : ZC_ZV_mul(GEN x, GEN y)
794 : {
795 0 : long i,j, lx=lg(x), ly=lg(y);
796 : GEN z;
797 0 : if (ly==1) return cgetg(1,t_MAT);
798 0 : z = cgetg(ly,t_MAT);
799 0 : for (j=1; j < ly; j++)
800 : {
801 0 : gel(z,j) = cgetg(lx,t_COL);
802 0 : for (i=1; i<lx; i++) gcoeff(z,i,j) = mulii(gel(x,i),gel(y,j));
803 : }
804 0 : return z;
805 : }
806 :
807 : GEN
808 6660066 : ZV_dotsquare(GEN x)
809 : {
810 : long i, lx;
811 : pari_sp av;
812 : GEN z;
813 6660066 : lx = lg(x);
814 6660066 : if (lx == 1) return gen_0;
815 6660066 : av = avma; z = sqri(gel(x,1));
816 26356064 : for (i=2; i<lx; i++) z = addii(z, sqri(gel(x,i)));
817 6658519 : return gerepileuptoint(av,z);
818 : }
819 :
820 : GEN
821 16187154 : ZV_dotproduct(GEN x,GEN y)
822 : {
823 : long lx;
824 16187154 : if (x == y) return ZV_dotsquare(x);
825 11406816 : lx = lg(x);
826 11406816 : if (lx == 1) return gen_0;
827 11406816 : return ZV_dotproduct_i(x, y, lx);
828 : }
829 :
830 : static GEN
831 280 : _ZM_mul(void *data /*ignored*/, GEN x, GEN y)
832 280 : { (void)data; return ZM_mul(x,y); }
833 : static GEN
834 23198 : _ZM_sqr(void *data /*ignored*/, GEN x)
835 23198 : { (void)data; return ZM_sqr(x); }
836 : /* FIXME: Using Bodrato's squaring, precomputations attached to fixed
837 : * multiplicand should be reused. And some postcomputations can be fused */
838 : GEN
839 0 : ZM_pow(GEN x, GEN n)
840 : {
841 0 : pari_sp av = avma;
842 0 : if (!signe(n)) return matid(lg(x)-1);
843 0 : return gerepilecopy(av, gen_pow_i(x, n, NULL, &_ZM_sqr, &_ZM_mul));
844 : }
845 : GEN
846 22673 : ZM_powu(GEN x, ulong n)
847 : {
848 22673 : pari_sp av = avma;
849 22673 : if (!n) return matid(lg(x)-1);
850 22673 : return gerepilecopy(av, gen_powu_i(x, n, NULL, &_ZM_sqr, &_ZM_mul));
851 : }
852 : /********************************************************************/
853 : /** **/
854 : /** ADD, SUB **/
855 : /** **/
856 : /********************************************************************/
857 : static GEN
858 36280010 : ZC_add_i(GEN x, GEN y, long lx)
859 : {
860 36280010 : GEN A = cgetg(lx, t_COL);
861 : long i;
862 491313154 : for (i=1; i<lx; i++) gel(A,i) = addii(gel(x,i), gel(y,i));
863 36272647 : return A;
864 : }
865 : GEN
866 26481744 : ZC_add(GEN x, GEN y) { return ZC_add_i(x, y, lg(x)); }
867 : GEN
868 364900 : ZC_Z_add(GEN x, GEN y)
869 : {
870 364900 : long k, lx = lg(x);
871 364900 : GEN z = cgetg(lx, t_COL);
872 364900 : if (lx == 1) pari_err_TYPE2("+",x,y);
873 364900 : gel(z,1) = addii(y,gel(x,1));
874 2512080 : for (k = 2; k < lx; k++) gel(z,k) = icopy(gel(x,k));
875 364904 : return z;
876 : }
877 :
878 : static GEN
879 9053996 : ZC_sub_i(GEN x, GEN y, long lx)
880 : {
881 : long i;
882 9053996 : GEN A = cgetg(lx, t_COL);
883 64410798 : for (i=1; i<lx; i++) gel(A,i) = subii(gel(x,i), gel(y,i));
884 9053363 : return A;
885 : }
886 : GEN
887 8754577 : ZC_sub(GEN x, GEN y) { return ZC_sub_i(x, y, lg(x)); }
888 : GEN
889 0 : ZC_Z_sub(GEN x, GEN y)
890 : {
891 0 : long k, lx = lg(x);
892 0 : GEN z = cgetg(lx, t_COL);
893 0 : if (lx == 1) pari_err_TYPE2("+",x,y);
894 0 : gel(z,1) = subii(gel(x,1), y);
895 0 : for (k = 2; k < lx; k++) gel(z,k) = icopy(gel(x,k));
896 0 : return z;
897 : }
898 : GEN
899 545842 : Z_ZC_sub(GEN a, GEN x)
900 : {
901 545842 : long k, lx = lg(x);
902 545842 : GEN z = cgetg(lx, t_COL);
903 545843 : if (lx == 1) pari_err_TYPE2("-",a,x);
904 545843 : gel(z,1) = subii(a, gel(x,1));
905 1511824 : for (k = 2; k < lx; k++) gel(z,k) = negi(gel(x,k));
906 545855 : return z;
907 : }
908 :
909 : GEN
910 855295 : ZM_add(GEN x, GEN y)
911 : {
912 855295 : long lx = lg(x), l, j;
913 : GEN z;
914 855295 : if (lx == 1) return cgetg(1, t_MAT);
915 775777 : z = cgetg(lx, t_MAT); l = lgcols(x);
916 10573985 : for (j = 1; j < lx; j++) gel(z,j) = ZC_add_i(gel(x,j), gel(y,j), l);
917 775777 : return z;
918 : }
919 : GEN
920 91470 : ZM_sub(GEN x, GEN y)
921 : {
922 91470 : long lx = lg(x), l, j;
923 : GEN z;
924 91470 : if (lx == 1) return cgetg(1, t_MAT);
925 91470 : z = cgetg(lx, t_MAT); l = lgcols(x);
926 390838 : for (j = 1; j < lx; j++) gel(z,j) = ZC_sub_i(gel(x,j), gel(y,j), l);
927 91469 : return z;
928 : }
929 : /********************************************************************/
930 : /** **/
931 : /** LINEAR COMBINATION **/
932 : /** **/
933 : /********************************************************************/
934 : /* return X/c assuming division is exact */
935 : GEN
936 5134816 : ZC_Z_divexact(GEN x, GEN c)
937 72258003 : { pari_APPLY_type(t_COL, diviiexact(gel(x,i), c)) }
938 : GEN
939 2261 : ZC_divexactu(GEN x, ulong c)
940 11375 : { pari_APPLY_type(t_COL, diviuexact(gel(x,i), c)) }
941 :
942 : GEN
943 461492 : ZM_Z_divexact(GEN x, GEN c)
944 3527614 : { pari_APPLY_same(ZC_Z_divexact(gel(x,i), c)) }
945 :
946 : GEN
947 441 : ZM_divexactu(GEN x, ulong c)
948 2702 : { pari_APPLY_same(ZC_divexactu(gel(x,i), c)) }
949 :
950 : GEN
951 35524720 : ZC_Z_mul(GEN x, GEN c)
952 : {
953 35524720 : if (!signe(c)) return zerocol(lg(x)-1);
954 34157832 : if (is_pm1(c)) return (signe(c) > 0)? ZC_copy(x): ZC_neg(x);
955 255681095 : pari_APPLY_type(t_COL, mulii(gel(x,i), c))
956 : }
957 :
958 : GEN
959 61772 : ZC_z_mul(GEN x, long c)
960 : {
961 61772 : if (!c) return zerocol(lg(x)-1);
962 52613 : if (c == 1) return ZC_copy(x);
963 47759 : if (c ==-1) return ZC_neg(x);
964 477819 : pari_APPLY_type(t_COL, mulsi(c, gel(x,i)))
965 : }
966 :
967 : GEN
968 28464 : zv_z_mul(GEN x, long n)
969 432127 : { pari_APPLY_long(x[i]*n) }
970 :
971 : /* return a ZM */
972 : GEN
973 0 : nm_Z_mul(GEN X, GEN c)
974 : {
975 0 : long i, j, h, l = lg(X), s = signe(c);
976 : GEN A;
977 0 : if (l == 1) return cgetg(1, t_MAT);
978 0 : h = lgcols(X);
979 0 : if (!s) return zeromat(h-1, l-1);
980 0 : if (is_pm1(c)) {
981 0 : if (s > 0) return Flm_to_ZM(X);
982 0 : X = Flm_to_ZM(X); ZM_togglesign(X); return X;
983 : }
984 0 : A = cgetg(l, t_MAT);
985 0 : for (j = 1; j < l; j++)
986 : {
987 0 : GEN a = cgetg(h, t_COL), x = gel(X, j);
988 0 : for (i = 1; i < h; i++) gel(a,i) = muliu(c, x[i]);
989 0 : gel(A,j) = a;
990 : }
991 0 : return A;
992 : }
993 : GEN
994 3211676 : ZM_Z_mul(GEN X, GEN c)
995 : {
996 3211676 : long i, j, h, l = lg(X);
997 : GEN A;
998 3211676 : if (l == 1) return cgetg(1, t_MAT);
999 3211676 : h = lgcols(X);
1000 3211667 : if (!signe(c)) return zeromat(h-1, l-1);
1001 3166318 : if (is_pm1(c)) return (signe(c) > 0)? ZM_copy(X): ZM_neg(X);
1002 2787399 : A = cgetg(l, t_MAT);
1003 15325389 : for (j = 1; j < l; j++)
1004 : {
1005 12538210 : GEN a = cgetg(h, t_COL), x = gel(X, j);
1006 221302032 : for (i = 1; i < h; i++) gel(a,i) = mulii(c, gel(x,i));
1007 12537993 : gel(A,j) = a;
1008 : }
1009 2787179 : return A;
1010 : }
1011 : void
1012 73057180 : ZC_lincomb1_inplace_i(GEN X, GEN Y, GEN v, long n)
1013 : {
1014 : long i;
1015 1617174728 : for (i = n; i; i--) gel(X,i) = addmulii_inplace(gel(X,i), gel(Y,i), v);
1016 73010942 : }
1017 : /* X <- X + v Y (elementary col operation) */
1018 : void
1019 62159330 : ZC_lincomb1_inplace(GEN X, GEN Y, GEN v)
1020 : {
1021 62159330 : if (lgefint(v) != 2) return ZC_lincomb1_inplace_i(X, Y, v, lg(X)-1);
1022 : }
1023 : void
1024 29484109 : Flc_lincomb1_inplace(GEN X, GEN Y, ulong v, ulong q)
1025 : {
1026 : long i;
1027 29484109 : if (!v) return; /* v = 0 */
1028 741918185 : for (i = lg(X)-1; i; i--) X[i] = Fl_add(X[i], Fl_mul(Y[i], v, q), q);
1029 : }
1030 :
1031 : /* X + v Y, wasteful if (v = 0) */
1032 : static GEN
1033 15375578 : ZC_lincomb1(GEN v, GEN x, GEN y)
1034 121857830 : { pari_APPLY_type(t_COL, addmulii(gel(x,i), gel(y,i), v)) }
1035 :
1036 : /* -X + vY */
1037 : static GEN
1038 739107 : ZC_lincomb_1(GEN v, GEN x, GEN y)
1039 4563058 : { pari_APPLY_type(t_COL, mulsubii(gel(y,i), v, gel(x,i))) }
1040 :
1041 : /* X,Y compatible ZV; u,v in Z. Returns A = u*X + v*Y */
1042 : GEN
1043 33262677 : ZC_lincomb(GEN u, GEN v, GEN X, GEN Y)
1044 : {
1045 : long su, sv;
1046 : GEN A;
1047 :
1048 33262677 : su = signe(u); if (!su) return ZC_Z_mul(Y, v);
1049 33261725 : sv = signe(v); if (!sv) return ZC_Z_mul(X, u);
1050 32831935 : if (is_pm1(v))
1051 : {
1052 11080924 : if (is_pm1(u))
1053 : {
1054 9756637 : if (su != sv) A = ZC_sub(X, Y);
1055 2708597 : else A = ZC_add(X, Y);
1056 9756194 : if (su < 0) ZV_togglesign(A); /* in place but was created above */
1057 : }
1058 : else
1059 : {
1060 1324225 : if (sv > 0) A = ZC_lincomb1 (u, Y, X);
1061 605365 : else A = ZC_lincomb_1(u, Y, X);
1062 : }
1063 : }
1064 21751815 : else if (is_pm1(u))
1065 : {
1066 14789819 : if (su > 0) A = ZC_lincomb1 (v, X, Y);
1067 133073 : else A = ZC_lincomb_1(v, X, Y);
1068 : }
1069 : else
1070 : { /* not cgetg_copy: x may be a t_VEC */
1071 6966463 : long i, lx = lg(X);
1072 6966463 : A = cgetg(lx,t_COL);
1073 44652372 : for (i=1; i<lx; i++) gel(A,i) = lincombii(u,v,gel(X,i),gel(Y,i));
1074 : }
1075 32824862 : return A;
1076 : }
1077 :
1078 : /********************************************************************/
1079 : /** **/
1080 : /** CONVERSIONS **/
1081 : /** **/
1082 : /********************************************************************/
1083 : GEN
1084 466206 : ZV_to_nv(GEN x)
1085 883220 : { pari_APPLY_ulong(itou(gel(x,i))) }
1086 :
1087 : GEN
1088 156365 : zm_to_ZM(GEN x)
1089 911962 : { pari_APPLY_type(t_MAT, zc_to_ZC(gel(x,i))) }
1090 :
1091 : GEN
1092 126 : zmV_to_ZMV(GEN x)
1093 791 : { pari_APPLY_type(t_VEC, zm_to_ZM(gel(x,i))) }
1094 :
1095 : /* same as Flm_to_ZM but do not assume positivity */
1096 : GEN
1097 1022 : ZM_to_zm(GEN x)
1098 17472 : { pari_APPLY_same(ZV_to_zv(gel(x,i))) }
1099 :
1100 : GEN
1101 366646 : zv_to_Flv(GEN x, ulong p)
1102 5418812 : { pari_APPLY_ulong(umodsu(x[i], p)) }
1103 :
1104 : GEN
1105 22694 : zm_to_Flm(GEN x, ulong p)
1106 351750 : { pari_APPLY_same(zv_to_Flv(gel(x,i),p)) }
1107 :
1108 : GEN
1109 49 : ZMV_to_zmV(GEN x)
1110 399 : { pari_APPLY_type(t_VEC, ZM_to_zm(gel(x,i))) }
1111 :
1112 : /********************************************************************/
1113 : /** **/
1114 : /** COPY, NEGATION **/
1115 : /** **/
1116 : /********************************************************************/
1117 : GEN
1118 14713967 : ZC_copy(GEN x)
1119 : {
1120 14713967 : long i, lx = lg(x);
1121 14713967 : GEN y = cgetg(lx, t_COL);
1122 122618258 : for (i=1; i<lx; i++)
1123 : {
1124 107903515 : GEN c = gel(x,i);
1125 107903515 : gel(y,i) = lgefint(c) == 2? gen_0: icopy(c);
1126 : }
1127 14714743 : return y;
1128 : }
1129 :
1130 : GEN
1131 633269 : ZM_copy(GEN x)
1132 5808697 : { pari_APPLY_same(ZC_copy(gel(x,i))) }
1133 :
1134 : void
1135 344029 : ZV_neg_inplace(GEN M)
1136 : {
1137 344029 : long l = lg(M);
1138 1296238 : while (--l > 0) gel(M,l) = negi(gel(M,l));
1139 344077 : }
1140 : GEN
1141 6305246 : ZC_neg(GEN x)
1142 36980005 : { pari_APPLY_type(t_COL, negi(gel(x,i))) }
1143 :
1144 : GEN
1145 51610 : zv_neg(GEN x)
1146 662101 : { pari_APPLY_long(-x[i]) }
1147 : GEN
1148 126 : zv_neg_inplace(GEN M)
1149 : {
1150 126 : long l = lg(M);
1151 425 : while (--l > 0) M[l] = -M[l];
1152 126 : return M;
1153 : }
1154 : GEN
1155 77 : zv_abs(GEN x)
1156 5446 : { pari_APPLY_ulong(labs(x[i])) }
1157 : GEN
1158 1658088 : ZM_neg(GEN x)
1159 5056452 : { pari_APPLY_same(ZC_neg(gel(x,i))) }
1160 :
1161 : void
1162 5036168 : ZV_togglesign(GEN M)
1163 : {
1164 5036168 : long l = lg(M);
1165 75512259 : while (--l > 0) togglesign_safe(&gel(M,l));
1166 5036259 : }
1167 : void
1168 0 : ZM_togglesign(GEN M)
1169 : {
1170 0 : long l = lg(M);
1171 0 : while (--l > 0) ZV_togglesign(gel(M,l));
1172 0 : }
1173 :
1174 : /********************************************************************/
1175 : /** **/
1176 : /** "DIVISION" mod HNF **/
1177 : /** **/
1178 : /********************************************************************/
1179 : /* Reduce ZC x modulo ZM y in HNF, may return x itself (not a copy) */
1180 : GEN
1181 10736171 : ZC_hnfdivrem(GEN x, GEN y, GEN *Q)
1182 : {
1183 10736171 : long i, l = lg(x);
1184 : GEN q;
1185 :
1186 10736171 : if (Q) *Q = cgetg(l,t_COL);
1187 10736171 : if (l == 1) return cgetg(1,t_COL);
1188 61711437 : for (i = l-1; i>0; i--)
1189 : {
1190 50978187 : q = diviiround(gel(x,i), gcoeff(y,i,i));
1191 50977104 : if (signe(q)) {
1192 22997591 : togglesign(q);
1193 22997661 : x = ZC_lincomb(gen_1, q, x, gel(y,i));
1194 : }
1195 50975266 : if (Q) gel(*Q, i) = q;
1196 : }
1197 10733250 : return x;
1198 : }
1199 :
1200 : /* x = y Q + R, may return some columns of x (not copies) */
1201 : GEN
1202 454382 : ZM_hnfdivrem(GEN x, GEN y, GEN *Q)
1203 : {
1204 454382 : long lx = lg(x), i;
1205 454382 : GEN R = cgetg(lx, t_MAT);
1206 454401 : if (Q)
1207 : {
1208 127577 : GEN q = cgetg(lx, t_MAT); *Q = q;
1209 188535 : for (i=1; i<lx; i++) gel(R,i) = ZC_hnfdivrem(gel(x,i),y,(GEN*)(q+i));
1210 : }
1211 : else
1212 824083 : for (i=1; i<lx; i++)
1213 : {
1214 497247 : pari_sp av = avma;
1215 497247 : GEN z = ZC_hnfrem(gel(x,i),y);
1216 497214 : gel(R,i) = (avma == av)? ZC_copy(z): gerepileupto(av, z);
1217 : }
1218 454416 : return R;
1219 : }
1220 :
1221 : /********************************************************************/
1222 : /** **/
1223 : /** TESTS **/
1224 : /** **/
1225 : /********************************************************************/
1226 : int
1227 23103557 : zv_equal0(GEN V)
1228 : {
1229 23103557 : long l = lg(V);
1230 37580093 : while (--l > 0)
1231 30805823 : if (V[l]) return 0;
1232 6774270 : return 1;
1233 : }
1234 :
1235 : int
1236 14456066 : ZV_equal0(GEN V)
1237 : {
1238 14456066 : long l = lg(V);
1239 25521566 : while (--l > 0)
1240 25095267 : if (signe(gel(V,l))) return 0;
1241 426299 : return 1;
1242 : }
1243 : int
1244 16231 : ZMrow_equal0(GEN V, long i)
1245 : {
1246 16231 : long l = lg(V);
1247 25183 : while (--l > 0)
1248 21679 : if (signe(gcoeff(V,i,l))) return 0;
1249 3504 : return 1;
1250 : }
1251 :
1252 : static int
1253 6266489 : ZV_equal_lg(GEN V, GEN W, long l)
1254 : {
1255 25962713 : while (--l > 0)
1256 20178059 : if (!equalii(gel(V,l), gel(W,l))) return 0;
1257 5784654 : return 1;
1258 : }
1259 : int
1260 295039 : ZV_equal(GEN V, GEN W)
1261 : {
1262 295039 : long l = lg(V);
1263 295039 : if (lg(W) != l) return 0;
1264 295032 : return ZV_equal_lg(V, W, l);
1265 : }
1266 : int
1267 3355554 : ZM_equal(GEN A, GEN B)
1268 : {
1269 3355554 : long i, m, l = lg(A);
1270 3355554 : if (lg(B) != l) return 0;
1271 3355471 : if (l == 1) return 1;
1272 3355471 : m = lgcols(A);
1273 3355468 : if (lgcols(B) != m) return 0;
1274 9064478 : for (i = 1; i < l; i++)
1275 5971451 : if (!ZV_equal_lg(gel(A,i), gel(B,i), m)) return 0;
1276 3093027 : return 1;
1277 : }
1278 : int
1279 72442 : ZM_equal0(GEN A)
1280 : {
1281 72442 : long i, j, m, l = lg(A);
1282 72442 : if (l == 1) return 1;
1283 72442 : m = lgcols(A);
1284 123568 : for (j = 1; j < l; j++)
1285 2718915 : for (i = 1; i < m; i++)
1286 2667789 : if (signe(gcoeff(A,i,j))) return 0;
1287 15627 : return 1;
1288 : }
1289 : int
1290 30984852 : zv_equal(GEN V, GEN W)
1291 : {
1292 30984852 : long l = lg(V);
1293 30984852 : if (lg(W) != l) return 0;
1294 262834587 : while (--l > 0)
1295 232966841 : if (V[l] != W[l]) return 0;
1296 29867746 : return 1;
1297 : }
1298 :
1299 : int
1300 1638 : zvV_equal(GEN V, GEN W)
1301 : {
1302 1638 : long l = lg(V);
1303 1638 : if (lg(W) != l) return 0;
1304 80388 : while (--l > 0)
1305 79912 : if (!zv_equal(gel(V,l),gel(W,l))) return 0;
1306 476 : return 1;
1307 : }
1308 :
1309 : int
1310 760745 : ZM_ishnf(GEN x)
1311 : {
1312 760745 : long i,j, lx = lg(x);
1313 2286950 : for (i=1; i<lx; i++)
1314 : {
1315 1639026 : GEN xii = gcoeff(x,i,i);
1316 1639026 : if (signe(xii) <= 0) return 0;
1317 3182196 : for (j=1; j<i; j++)
1318 1580640 : if (signe(gcoeff(x,i,j))) return 0;
1319 3240807 : for (j=i+1; j<lx; j++)
1320 : {
1321 1714602 : GEN xij = gcoeff(x,i,j);
1322 1714602 : if (signe(xij)<0 || cmpii(xij,xii)>=0) return 0;
1323 : }
1324 : }
1325 647924 : return 1;
1326 : }
1327 : int
1328 653055 : ZM_isidentity(GEN x)
1329 : {
1330 653055 : long i,j, lx = lg(x);
1331 :
1332 653055 : if (lx == 1) return 1;
1333 653048 : if (lx != lgcols(x)) return 0;
1334 3190447 : for (j=1; j<lx; j++)
1335 : {
1336 2537456 : GEN c = gel(x,j);
1337 7955187 : for (i=1; i<j; )
1338 5417753 : if (signe(gel(c,i++))) return 0;
1339 : /* i = j */
1340 2537434 : if (!equali1(gel(c,i++))) return 0;
1341 7955202 : for ( ; i<lx; )
1342 5417789 : if (signe(gel(c,i++))) return 0;
1343 : }
1344 652991 : return 1;
1345 : }
1346 : int
1347 556572 : ZM_isdiagonal(GEN x)
1348 : {
1349 556572 : long i,j, lx = lg(x);
1350 556572 : if (lx == 1) return 1;
1351 556572 : if (lx != lgcols(x)) return 0;
1352 :
1353 1438792 : for (j=1; j<lx; j++)
1354 : {
1355 1209076 : GEN c = gel(x,j);
1356 1697652 : for (i=1; i<j; i++)
1357 815387 : if (signe(gel(c,i))) return 0;
1358 2021506 : for (i++; i<lx; i++)
1359 1139276 : if (signe(gel(c,i))) return 0;
1360 : }
1361 229716 : return 1;
1362 : }
1363 : int
1364 156456 : ZM_isscalar(GEN x, GEN s)
1365 : {
1366 156456 : long i, j, lx = lg(x);
1367 :
1368 156456 : if (lx == 1) return 1;
1369 156456 : if (!s) s = gcoeff(x,1,1);
1370 156456 : if (equali1(s)) return ZM_isidentity(x);
1371 155000 : if (lx != lgcols(x)) return 0;
1372 679721 : for (j=1; j<lx; j++)
1373 : {
1374 585732 : GEN c = gel(x,j);
1375 2641592 : for (i=1; i<j; )
1376 2114784 : if (signe(gel(c,i++))) return 0;
1377 : /* i = j */
1378 526808 : if (!equalii(gel(c,i++), s)) return 0;
1379 2669268 : for ( ; i<lx; )
1380 2144547 : if (signe(gel(c,i++))) return 0;
1381 : }
1382 93989 : return 1;
1383 : }
1384 :
1385 : long
1386 159983 : ZC_is_ei(GEN x)
1387 : {
1388 159983 : long i, j = 0, l = lg(x);
1389 1682600 : for (i = 1; i < l; i++)
1390 : {
1391 1522618 : GEN c = gel(x,i);
1392 1522618 : long s = signe(c);
1393 1522618 : if (!s) continue;
1394 159977 : if (s < 0 || !is_pm1(c) || j) return 0;
1395 159976 : j = i;
1396 : }
1397 159982 : return j;
1398 : }
1399 :
1400 : /********************************************************************/
1401 : /** **/
1402 : /** MISCELLANEOUS **/
1403 : /** **/
1404 : /********************************************************************/
1405 : /* assume lg(x) = lg(y), x,y in Z^n */
1406 : int
1407 3146186 : ZV_cmp(GEN x, GEN y)
1408 : {
1409 3146186 : long fl,i, lx = lg(x);
1410 6373229 : for (i=1; i<lx; i++)
1411 5078020 : if (( fl = cmpii(gel(x,i), gel(y,i)) )) return fl;
1412 1295209 : return 0;
1413 : }
1414 : /* assume lg(x) = lg(y), x,y in Z^n */
1415 : int
1416 19740 : ZV_abscmp(GEN x, GEN y)
1417 : {
1418 19740 : long fl,i, lx = lg(x);
1419 54151 : for (i=1; i<lx; i++)
1420 54024 : if (( fl = abscmpii(gel(x,i), gel(y,i)) )) return fl;
1421 127 : return 0;
1422 : }
1423 :
1424 : long
1425 18454286 : zv_content(GEN x)
1426 : {
1427 18454286 : long i, s, l = lg(x);
1428 18454286 : if (l == 1) return 0;
1429 18454279 : s = labs(x[1]);
1430 42005644 : for (i = 2; i < l && s != 1; i++) s = ugcd(s, labs(x[i]));
1431 18454313 : return s;
1432 : }
1433 : GEN
1434 297337 : ZV_content(GEN x)
1435 : {
1436 297337 : long i, l = lg(x);
1437 297337 : pari_sp av = avma;
1438 : GEN c;
1439 297337 : if (l == 1) return gen_0;
1440 297337 : if (l == 2) return absi(gel(x,1));
1441 204811 : c = gel(x,1);
1442 557880 : for (i = 2; i < l; i++)
1443 : {
1444 403591 : c = gcdii(c, gel(x,i));
1445 403591 : if (is_pm1(c)) { set_avma(av); return gen_1; }
1446 : }
1447 154289 : return gerepileuptoint(av, c);
1448 : }
1449 :
1450 : GEN
1451 3952408 : ZM_det_triangular(GEN mat)
1452 : {
1453 : pari_sp av;
1454 3952408 : long i,l = lg(mat);
1455 : GEN s;
1456 :
1457 3952408 : if (l<3) return l<2? gen_1: icopy(gcoeff(mat,1,1));
1458 3522091 : av = avma; s = gcoeff(mat,1,1);
1459 9540134 : for (i=2; i<l; i++) s = mulii(s,gcoeff(mat,i,i));
1460 3521704 : return gerepileuptoint(av,s);
1461 : }
1462 :
1463 : /* assumes no overflow */
1464 : long
1465 950567 : zv_prod(GEN v)
1466 : {
1467 950567 : long n, i, l = lg(v);
1468 950567 : if (l == 1) return 1;
1469 960602 : n = v[1]; for (i = 2; i < l; i++) n *= v[i];
1470 772116 : return n;
1471 : }
1472 :
1473 : static GEN
1474 318571043 : _mulii(void *E, GEN a, GEN b)
1475 318571043 : { (void) E; return mulii(a, b); }
1476 :
1477 : /* product of ulongs */
1478 : GEN
1479 1864433 : zv_prod_Z(GEN v)
1480 : {
1481 : pari_sp av;
1482 1864433 : long k, m, n = lg(v)-1;
1483 1864433 : int stop = 0;
1484 : GEN V;
1485 1864433 : switch(n) {
1486 21035 : case 0: return gen_1;
1487 130095 : case 1: return utoi(v[1]);
1488 977007 : case 2: return muluu(v[1], v[2]);
1489 : }
1490 736296 : av = avma; m = n >> 1;
1491 736296 : V = cgetg(m + (odd(n)? 2: 1), t_VEC);
1492 153068512 : for (k = n; k; k--) /* start from the end: v is usually sorted */
1493 152333271 : if (v[k] & HIGHMASK) { stop = 1; break; }
1494 2420215 : while (!stop)
1495 : { /* HACK: handle V as a t_VECSMALL; gain a few iterations */
1496 82327276 : for (k = 1; k <= m; k++)
1497 : {
1498 80034989 : V[k] = uel(v,k<<1) * uel(v,(k<<1)-1);
1499 80034989 : if (V[k] & HIGHMASK) stop = 1; /* last "free" iteration */
1500 : }
1501 2292287 : if (odd(n))
1502 : {
1503 1350956 : if (n == 1) { set_avma(av); return utoi(v[1]); }
1504 742589 : V[++m] = v[n];
1505 : }
1506 1683920 : v = V; n = m; m = n >> 1;
1507 : }
1508 : /* n > 1; m > 0 */
1509 127928 : if (n == 2) { set_avma(av); return muluu(v[1], v[2]); }
1510 46793136 : for (k = 1; k <= m; k++) gel(V,k) = muluu(v[k<<1], v[(k<<1)-1]);
1511 87793 : if (odd(n)) gel(V, ++m) = utoipos(v[n]);
1512 87794 : setlg(V, m+1); /* HACK: now V is a bona fide t_VEC */
1513 87794 : return gerepileuptoint(av, gen_product(V, NULL, &_mulii));
1514 : }
1515 : GEN
1516 14694393 : vecsmall_prod(GEN v)
1517 : {
1518 14694393 : pari_sp av = avma;
1519 14694393 : long k, m, n = lg(v)-1;
1520 : GEN V;
1521 14694393 : switch (n) {
1522 0 : case 0: return gen_1;
1523 0 : case 1: return stoi(v[1]);
1524 21 : case 2: return mulss(v[1], v[2]);
1525 : }
1526 14694372 : m = n >> 1;
1527 14694372 : V = cgetg(m + (odd(n)? 2: 1), t_VEC);
1528 161556906 : for (k = 1; k <= m; k++) gel(V,k) = mulss(v[k<<1], v[(k<<1)-1]);
1529 14694372 : if (odd(n)) gel(V,k) = stoi(v[n]);
1530 14694372 : return gerepileuptoint(av, gen_product(V, NULL, &_mulii));
1531 : }
1532 :
1533 : GEN
1534 8829268 : ZV_prod(GEN v)
1535 : {
1536 8829268 : pari_sp av = avma;
1537 8829268 : long i, l = lg(v);
1538 : GEN n;
1539 8829268 : if (l == 1) return gen_1;
1540 8644353 : if (l > 7) return gerepileuptoint(av, gen_product(v, NULL, _mulii));
1541 1301367 : n = gel(v,1);
1542 1301367 : if (l == 2) return icopy(n);
1543 2104094 : for (i = 2; i < l; i++) n = mulii(n, gel(v,i));
1544 846375 : return gerepileuptoint(av, n);
1545 : }
1546 : /* assumes no overflow */
1547 : long
1548 16483 : zv_sum(GEN v)
1549 : {
1550 16483 : long n, i, l = lg(v);
1551 16483 : if (l == 1) return 0;
1552 95742 : n = v[1]; for (i = 2; i < l; i++) n += v[i];
1553 16462 : return n;
1554 : }
1555 : /* assumes no overflow and 0 <= n <= #v */
1556 : long
1557 0 : zv_sumpart(GEN v, long n)
1558 : {
1559 : long i, p;
1560 0 : if (!n) return 0;
1561 0 : p = v[1]; for (i = 2; i <= n; i++) p += v[i];
1562 0 : return p;
1563 : }
1564 : GEN
1565 77 : ZV_sum(GEN v)
1566 : {
1567 77 : pari_sp av = avma;
1568 77 : long i, l = lg(v);
1569 : GEN n;
1570 77 : if (l == 1) return gen_0;
1571 77 : n = gel(v,1);
1572 77 : if (l == 2) return icopy(n);
1573 581 : for (i = 2; i < l; i++) n = addii(n, gel(v,i));
1574 77 : return gerepileuptoint(av, n);
1575 : }
1576 :
1577 : /********************************************************************/
1578 : /** **/
1579 : /** GRAM SCHMIDT REDUCTION (integer matrices) **/
1580 : /** **/
1581 : /********************************************************************/
1582 :
1583 : /* L[k,] += q * L[l,], l < k. Inefficient if q = 0 */
1584 : static void
1585 361041 : Zupdate_row(long k, long l, GEN q, GEN L, GEN B)
1586 : {
1587 361041 : long i, qq = itos_or_0(q);
1588 361041 : if (!qq)
1589 : {
1590 33369 : for(i=1;i<l;i++) gcoeff(L,k,i) = addii(gcoeff(L,k,i),mulii(q,gcoeff(L,l,i)));
1591 7063 : gcoeff(L,k,l) = addii(gcoeff(L,k,l), mulii(q,B));
1592 7063 : return;
1593 : }
1594 353978 : if (qq == 1) {
1595 148435 : for (i=1;i<l; i++) gcoeff(L,k,i) = addii(gcoeff(L,k,i),gcoeff(L,l,i));
1596 102125 : gcoeff(L,k,l) = addii(gcoeff(L,k,l), B);
1597 251854 : } else if (qq == -1) {
1598 150741 : for (i=1;i<l; i++) gcoeff(L,k,i) = subii(gcoeff(L,k,i),gcoeff(L,l,i));
1599 88670 : gcoeff(L,k,l) = addii(gcoeff(L,k,l), negi(B));
1600 : } else {
1601 288801 : for(i=1;i<l;i++) gcoeff(L,k,i) = addii(gcoeff(L,k,i),mulsi(qq,gcoeff(L,l,i)));
1602 163193 : gcoeff(L,k,l) = addii(gcoeff(L,k,l), mulsi(qq,B));
1603 : }
1604 : }
1605 :
1606 : /* update L[k,] */
1607 : static void
1608 1036034 : ZRED(long k, long l, GEN x, GEN L, GEN B)
1609 : {
1610 1036034 : GEN q = truedivii(addii(B,shifti(gcoeff(L,k,l),1)), shifti(B,1));
1611 1036111 : if (!signe(q)) return;
1612 361004 : q = negi(q);
1613 361033 : Zupdate_row(k,l,q,L,B);
1614 361016 : gel(x,k) = ZC_lincomb(gen_1, q, gel(x,k), gel(x,l));
1615 : }
1616 :
1617 : /* Gram-Schmidt reduction, x a ZM */
1618 : static void
1619 1187949 : ZincrementalGS(GEN x, GEN L, GEN B, long k)
1620 : {
1621 : long i, j;
1622 3792685 : for (j=1; j<=k; j++)
1623 : {
1624 2605502 : pari_sp av = avma;
1625 2605502 : GEN u = ZV_dotproduct(gel(x,k), gel(x,j));
1626 5626805 : for (i=1; i<j; i++)
1627 : {
1628 3022771 : u = subii(mulii(gel(B,i+1), u), mulii(gcoeff(L,k,i), gcoeff(L,j,i)));
1629 3022345 : u = diviiexact(u, gel(B,i));
1630 : }
1631 2604034 : gcoeff(L,k,j) = gerepileuptoint(av, u);
1632 : }
1633 1187183 : gel(B,k+1) = gcoeff(L,k,k); gcoeff(L,k,k) = gen_1;
1634 1187183 : }
1635 :
1636 : /* Variant reducemodinvertible(ZC v, ZM y), when y singular.
1637 : * Very inefficient if y is not LLL-reduced of maximal rank */
1638 : static GEN
1639 111747 : ZC_reducemodmatrix_i(GEN v, GEN y)
1640 : {
1641 111747 : GEN B, L, x = shallowconcat(y, v);
1642 111749 : long k, lx = lg(x), nx = lx-1;
1643 :
1644 111749 : B = scalarcol_shallow(gen_1, lx);
1645 111750 : L = zeromatcopy(nx, nx);
1646 459270 : for (k=1; k <= nx; k++) ZincrementalGS(x, L, B, k);
1647 347518 : for (k = nx-1; k >= 1; k--) ZRED(nx,k, x,L,gel(B,k+1));
1648 111744 : return gel(x,nx);
1649 : }
1650 : GEN
1651 111747 : ZC_reducemodmatrix(GEN v, GEN y) {
1652 111747 : pari_sp av = avma;
1653 111747 : return gerepilecopy(av, ZC_reducemodmatrix_i(v,y));
1654 : }
1655 :
1656 : /* Variant reducemodinvertible(ZM v, ZM y), when y singular.
1657 : * Very inefficient if y is not LLL-reduced of maximal rank */
1658 : static GEN
1659 227024 : ZM_reducemodmatrix_i(GEN v, GEN y)
1660 : {
1661 : GEN B, L, V;
1662 227024 : long j, k, lv = lg(v), nx = lg(y), lx = nx+1;
1663 :
1664 227024 : V = cgetg(lv, t_MAT);
1665 227042 : B = scalarcol_shallow(gen_1, lx);
1666 227040 : L = zeromatcopy(nx, nx);
1667 601501 : for (k=1; k < nx; k++) ZincrementalGS(y, L, B, k);
1668 693001 : for (j = 1; j < lg(v); j++)
1669 : {
1670 465999 : GEN x = shallowconcat(y, gel(v,j));
1671 466014 : ZincrementalGS(x, L, B, nx); /* overwrite last */
1672 1266324 : for (k = nx-1; k >= 1; k--) ZRED(nx,k, x,L,gel(B,k+1));
1673 465977 : gel(V,j) = gel(x,nx);
1674 : }
1675 227002 : return V;
1676 : }
1677 : GEN
1678 227023 : ZM_reducemodmatrix(GEN v, GEN y) {
1679 227023 : pari_sp av = avma;
1680 227023 : return gerepilecopy(av, ZM_reducemodmatrix_i(v,y));
1681 : }
1682 :
1683 : GEN
1684 98601 : ZC_reducemodlll(GEN x,GEN y)
1685 : {
1686 98601 : pari_sp av = avma;
1687 98601 : GEN z = ZC_reducemodmatrix(x, ZM_lll(y, 0.75, LLL_INPLACE));
1688 98608 : return gerepilecopy(av, z);
1689 : }
1690 : GEN
1691 0 : ZM_reducemodlll(GEN x,GEN y)
1692 : {
1693 0 : pari_sp av = avma;
1694 0 : GEN z = ZM_reducemodmatrix(x, ZM_lll(y, 0.75, LLL_INPLACE));
1695 0 : return gerepilecopy(av, z);
1696 : }
|