Line data Source code
1 : /* Copyright (C) 2012 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 : /********************************************************************/
16 : /** **/
17 : /** LINEAR ALGEBRA **/
18 : /** (third part) **/
19 : /** **/
20 : /********************************************************************/
21 : #include "pari.h"
22 : #include "paripriv.h"
23 :
24 : #define DEBUGLEVEL DEBUGLEVEL_mat
25 :
26 : /*******************************************************************/
27 : /* */
28 : /* SUM */
29 : /* */
30 : /*******************************************************************/
31 :
32 : GEN
33 114041 : vecsum(GEN v)
34 : {
35 114041 : pari_sp av = avma;
36 : long i, l;
37 : GEN p;
38 114041 : if (!is_vec_t(typ(v)))
39 7 : pari_err_TYPE("vecsum", v);
40 114034 : l = lg(v);
41 114034 : if (l == 1) return gen_0;
42 114027 : p = gel(v,1);
43 114027 : if (l == 2) return gcopy(p);
44 201652 : for (i=2; i<l; i++)
45 : {
46 143968 : p = gadd(p, gel(v,i));
47 143968 : if (gc_needed(av, 2))
48 : {
49 0 : if (DEBUGMEM>1) pari_warn(warnmem,"sum");
50 0 : p = gerepileupto(av, p);
51 : }
52 : }
53 57684 : return gerepileupto(av, p);
54 : }
55 :
56 : /*******************************************************************/
57 : /* */
58 : /* TRANSPOSE */
59 : /* */
60 : /*******************************************************************/
61 : /* A[x0,]~ */
62 : static GEN
63 25575190 : row_transpose(GEN A, long x0)
64 : {
65 25575190 : long i, lB = lg(A);
66 25575190 : GEN B = cgetg(lB, t_COL);
67 198837012 : for (i=1; i<lB; i++) gel(B, i) = gcoeff(A, x0, i);
68 25575334 : return B;
69 : }
70 : static GEN
71 18616 : row_transposecopy(GEN A, long x0)
72 : {
73 18616 : long i, lB = lg(A);
74 18616 : GEN B = cgetg(lB, t_COL);
75 161293 : for (i=1; i<lB; i++) gel(B, i) = gcopy(gcoeff(A, x0, i));
76 18616 : return B;
77 : }
78 :
79 : /* No copy*/
80 : GEN
81 6829640 : shallowtrans(GEN x)
82 : {
83 : long i, dx, lx;
84 : GEN y;
85 6829640 : switch(typ(x))
86 : {
87 1162 : case t_VEC: y = leafcopy(x); settyp(y,t_COL); break;
88 896 : case t_COL: y = leafcopy(x); settyp(y,t_VEC); break;
89 6827596 : case t_MAT:
90 6827596 : lx = lg(x); if (lx==1) return cgetg(1,t_MAT);
91 6826301 : dx = lgcols(x); y = cgetg(dx,t_MAT);
92 32401832 : for (i = 1; i < dx; i++) gel(y,i) = row_transpose(x,i);
93 6826968 : break;
94 0 : default: pari_err_TYPE("shallowtrans",x);
95 : return NULL;/*LCOV_EXCL_LINE*/
96 : }
97 6829026 : return y;
98 : }
99 :
100 : GEN
101 43603 : gtrans(GEN x)
102 : {
103 : long i, dx, lx;
104 : GEN y;
105 43603 : switch(typ(x))
106 : {
107 36771 : case t_VEC: y = gcopy(x); settyp(y,t_COL); break;
108 4872 : case t_COL: y = gcopy(x); settyp(y,t_VEC); break;
109 1953 : case t_MAT:
110 1953 : lx = lg(x); if (lx==1) return cgetg(1,t_MAT);
111 1946 : dx = lgcols(x); y = cgetg(dx,t_MAT);
112 20562 : for (i = 1; i < dx; i++) gel(y,i) = row_transposecopy(x,i);
113 1946 : break;
114 7 : default: pari_err_TYPE("gtrans",x);
115 : return NULL;/*LCOV_EXCL_LINE*/
116 : }
117 43589 : return y;
118 : }
119 :
120 : /*******************************************************************/
121 : /* */
122 : /* EXTRACTION */
123 : /* */
124 : /*******************************************************************/
125 :
126 : static long
127 182 : str_to_long(char *s, char **pt)
128 : {
129 182 : long a = atol(s);
130 182 : while (isspace((unsigned char)*s)) s++;
131 182 : if (*s == '-' || *s == '+') s++;
132 385 : while (isdigit((unsigned char)*s) || isspace((unsigned char)*s)) s++;
133 182 : *pt = s; return a;
134 : }
135 :
136 : static int
137 112 : get_range(char *s, long *a, long *b, long *cmpl, long lx)
138 : {
139 112 : long max = lx - 1;
140 :
141 112 : *a = 1; *b = max;
142 112 : if (*s == '^') { *cmpl = 1; s++; } else *cmpl = 0;
143 112 : if (!*s) return 0;
144 112 : if (*s != '.')
145 : {
146 105 : *a = str_to_long(s, &s);
147 105 : if (*a < 0) *a += lx;
148 105 : if (*a<1 || *a>max) return 0;
149 : }
150 112 : if (*s == '.')
151 : {
152 105 : s++; if (*s != '.') return 0;
153 105 : do s++; while (isspace((unsigned char)*s));
154 105 : if (*s)
155 : {
156 77 : *b = str_to_long(s, &s);
157 77 : if (*b < 0) *b += lx;
158 77 : if (*b<1 || *b>max || *s) return 0;
159 : }
160 98 : return 1;
161 : }
162 7 : if (*s) return 0;
163 7 : *b = *a; return 1;
164 : }
165 :
166 : static int
167 35 : extract_selector_ok(long lx, GEN L)
168 : {
169 : long i, l;
170 35 : switch (typ(L))
171 : {
172 7 : case t_INT: {
173 : long maxj;
174 7 : if (!signe(L)) return 1;
175 7 : l = lgefint(L)-1;
176 7 : maxj = BITS_IN_LONG - bfffo(*int_MSW(L));
177 7 : return ((l-2) * BITS_IN_LONG + maxj < lx);
178 : }
179 7 : case t_STR: {
180 : long first, last, cmpl;
181 7 : return get_range(GSTR(L), &first, &last, &cmpl, lx);
182 : }
183 14 : case t_VEC: case t_COL:
184 14 : l = lg(L);
185 28 : for (i=1; i<l; i++)
186 : {
187 21 : long j = itos(gel(L,i));
188 21 : if (j>=lx || j<=0) return 0;
189 : }
190 7 : return 1;
191 7 : case t_VECSMALL:
192 7 : l = lg(L);
193 21 : for (i=1; i<l; i++)
194 : {
195 14 : long j = L[i];
196 14 : if (j>=lx || j<=0) return 0;
197 : }
198 7 : return 1;
199 : }
200 0 : return 0;
201 : }
202 :
203 : GEN
204 10269 : shallowmatextract(GEN x, GEN l1, GEN l2)
205 : {
206 10269 : long i, j, n1 = lg(l1), n2 = lg(l2);
207 10269 : GEN M = cgetg(n2, t_MAT);
208 65205 : for(i=1; i < n2; i++)
209 : {
210 54936 : long ii = l2[i];
211 54936 : GEN C = cgetg(n1, t_COL);
212 957348 : for (j=1; j < n1; j++)
213 : {
214 902412 : long jj = l1[j];
215 902412 : gel(C, j) = gmael(x, ii, jj);
216 : }
217 54936 : gel(M, i) = C;
218 : }
219 10269 : return M;
220 : }
221 :
222 : GEN
223 38891 : shallowextract(GEN x, GEN L)
224 : {
225 38891 : long i,j, tl = typ(L), tx = typ(x), lx = lg(x);
226 : GEN y;
227 :
228 38891 : switch(tx)
229 : {
230 38884 : case t_VEC:
231 : case t_COL:
232 : case t_MAT:
233 38884 : case t_VECSMALL: break;
234 7 : default: pari_err_TYPE("extract",x);
235 :
236 : }
237 38884 : if (tl==t_INT)
238 : { /* extract components of x as per the bits of mask L */
239 : long k, l, ix, iy, maxj;
240 : GEN Ld;
241 3318 : if (!signe(L)) return cgetg(1,tx);
242 3311 : y = new_chunk(lx);
243 3311 : l = lgefint(L)-1; ix = iy = 1;
244 3311 : maxj = BITS_IN_LONG - bfffo(*int_MSW(L));
245 3311 : if ((l-2) * BITS_IN_LONG + maxj >= lx)
246 7 : pari_err_TYPE("vecextract [mask too large]", L);
247 3679 : for (k = 2, Ld = int_LSW(L); k < l; k++, Ld = int_nextW(Ld))
248 : {
249 375 : ulong B = *Ld;
250 20439 : for (j = 0; j < BITS_IN_LONG; j++, B >>= 1, ix++)
251 20064 : if (B & 1) y[iy++] = x[ix];
252 : }
253 : { /* k = l */
254 3304 : ulong B = *Ld;
255 28698 : for (j = 0; j < maxj; j++, B >>= 1, ix++)
256 25394 : if (B & 1) y[iy++] = x[ix];
257 : }
258 3304 : y[0] = evaltyp(tx) | evallg(iy);
259 3304 : return y;
260 : }
261 35566 : if (tl==t_STR)
262 : {
263 105 : char *s = GSTR(L);
264 : long first, last, cmpl, d;
265 105 : if (! get_range(s, &first, &last, &cmpl, lx))
266 7 : pari_err_TYPE("vecextract [incorrect range]", L);
267 98 : if (lx == 1) return cgetg(1,tx);
268 98 : d = last - first;
269 98 : if (cmpl)
270 : {
271 21 : if (d >= 0)
272 : {
273 14 : y = cgetg(lx - (1+d),tx);
274 469 : for (j=1; j<first; j++) gel(y,j) = gel(x,j);
275 266 : for (i=last+1; i<lx; i++,j++) gel(y,j) = gel(x,i);
276 : }
277 : else
278 : {
279 7 : y = cgetg(lx - (1-d),tx);
280 14 : for (j=1,i=lx-1; i>first; i--,j++) gel(y,j) = gel(x,i);
281 14 : for (i=last-1; i>0; i--,j++) gel(y,j) = gel(x,i);
282 : }
283 : }
284 : else
285 : {
286 77 : if (d >= 0)
287 : {
288 35 : y = cgetg(d+2,tx);
289 112 : for (i=first,j=1; i<=last; i++,j++) gel(y,j) = gel(x,i);
290 : }
291 : else
292 : {
293 42 : y = cgetg(2-d,tx);
294 203 : for (i=first,j=1; i>=last; i--,j++) gel(y,j) = gel(x,i);
295 : }
296 : }
297 98 : return y;
298 : }
299 :
300 35461 : if (is_vec_t(tl))
301 : {
302 77 : long ll=lg(L); y=cgetg(ll,tx);
303 196 : for (i=1; i<ll; i++)
304 : {
305 133 : j = itos(gel(L,i));
306 133 : if (j<=0) pari_err_COMPONENT("vecextract","<=",gen_0,stoi(j));
307 126 : if (j>=lx) pari_err_COMPONENT("vecextract",">=",stoi(lx),stoi(j));
308 119 : gel(y,i) = gel(x,j);
309 : }
310 63 : return y;
311 : }
312 35384 : if (tl == t_VECSMALL)
313 : {
314 35377 : long ll=lg(L); y=cgetg(ll,tx);
315 158165 : for (i=1; i<ll; i++)
316 : {
317 122788 : j = L[i];
318 122788 : if (j<=0) pari_err_COMPONENT("vecextract","<=",gen_0,stoi(j));
319 122788 : if (j>=lx) pari_err_COMPONENT("vecextract",">=",stoi(lx),stoi(j));
320 122788 : gel(y,i) = gel(x,j);
321 : }
322 35377 : return y;
323 : }
324 7 : pari_err_TYPE("vecextract [mask]", L);
325 : return NULL; /* LCOV_EXCL_LINE */
326 : }
327 :
328 : /* does the component selector l select 0 component ? */
329 : static int
330 77 : select_0(GEN l)
331 : {
332 77 : switch(typ(l))
333 : {
334 14 : case t_INT:
335 14 : return (!signe(l));
336 42 : case t_VEC: case t_COL: case t_VECSMALL:
337 42 : return (lg(l) == 1);
338 : }
339 21 : return 0;
340 : }
341 :
342 : GEN
343 28901 : extract0(GEN x, GEN l1, GEN l2)
344 : {
345 28901 : pari_sp av = avma, av2;
346 : GEN y;
347 28901 : if (! l2)
348 : {
349 28824 : y = shallowextract(x, l1);
350 28782 : if (lg(y) == 1 || typ(y) == t_VECSMALL) return y;
351 28775 : av2 = avma;
352 28775 : y = gcopy(y);
353 : }
354 : else
355 : {
356 77 : if (typ(x) != t_MAT) pari_err_TYPE("extract",x);
357 77 : y = shallowextract(x,l2);
358 77 : if (select_0(l1)) { set_avma(av); return zeromat(0, lg(y)-1); }
359 63 : if (lg(y) == 1 && lg(x) > 1)
360 : {
361 35 : if (!extract_selector_ok(lgcols(x), l1))
362 7 : pari_err_TYPE("vecextract [incorrect mask]", l1);
363 28 : set_avma(av); return cgetg(1, t_MAT);
364 : }
365 28 : y = shallowextract(shallowtrans(y), l1);
366 28 : av2 = avma;
367 28 : y = gtrans(y);
368 : }
369 28803 : stackdummy(av, av2);
370 28803 : return y;
371 : }
372 :
373 : static long
374 2016 : vecslice_parse_arg(long lA, long *y1, long *y2, long *skip)
375 : {
376 2016 : *skip=0;
377 2016 : if (*y1==LONG_MAX)
378 : {
379 252 : if (*y2!=LONG_MAX)
380 : {
381 140 : if (*y2<0) *y2 += lA;
382 140 : if (*y2<0 || *y2==LONG_MAX || *y2>=lA)
383 0 : pari_err_DIM("_[..]");
384 140 : *skip=*y2;
385 : }
386 252 : *y1 = 1; *y2 = lA-1;
387 : }
388 1764 : else if (*y2==LONG_MAX) *y2 = *y1;
389 2016 : if (*y1<=0) *y1 += lA;
390 2016 : if (*y2<0) *y2 += lA;
391 2016 : if (*y1<=0 || *y1>*y2+1 || *y2>=lA) pari_err_DIM("_[..]");
392 2002 : return *y2 - *y1 + 2 - !!*skip;
393 : }
394 :
395 : static GEN
396 2653 : vecslice_i(GEN A, long t, long lB, long y1, long skip)
397 : {
398 2653 : GEN B = cgetg(lB, t);
399 : long i;
400 27034 : for (i=1; i<lB; i++, y1++)
401 : {
402 24381 : if (y1 == skip) { i--; continue; }
403 24241 : gel(B,i) = gcopy(gel(A,y1));
404 : }
405 2653 : return B;
406 : }
407 :
408 : static GEN
409 14 : rowslice_i(GEN A, long lB, long x1, long y1, long skip)
410 : {
411 14 : GEN B = cgetg(lB, t_VEC);
412 : long i;
413 77 : for (i=1; i<lB; i++, y1++)
414 : {
415 63 : if (y1 == skip) { i--; continue; }
416 56 : gel(B,i) = gcopy(gcoeff(A,x1,y1));
417 : }
418 14 : return B;
419 : }
420 :
421 : static GEN
422 0 : rowsmallslice_i(GEN A, long lB, long x1, long y1, long skip)
423 : {
424 0 : GEN B = cgetg(lB, t_VECSMALL);
425 : long i;
426 0 : for (i=1; i<lB; i++, y1++)
427 : {
428 0 : if (y1 == skip) { i--; continue; }
429 0 : B[i] = coeff(A,x1,y1);
430 : }
431 0 : return B;
432 : }
433 :
434 : static GEN
435 28 : vecsmallslice_i(GEN A, long t, long lB, long y1, long skip)
436 : {
437 28 : GEN B = cgetg(lB, t);
438 : long i;
439 126 : for (i=1; i<lB; i++, y1++)
440 : {
441 98 : if (y1 == skip) { i--; continue; }
442 91 : B[i] = A[y1];
443 : }
444 28 : return B;
445 : }
446 : GEN
447 1617 : vecslice0(GEN A, long y1, long y2)
448 : {
449 1617 : long skip, lB, t = typ(A);
450 1617 : switch(t)
451 : {
452 1519 : case t_VEC: case t_COL:
453 1519 : lB = vecslice_parse_arg(lg(A), &y1, &y2, &skip);
454 1505 : return vecslice_i(A, t,lB,y1,skip);
455 28 : case t_VECSMALL:
456 28 : lB = vecslice_parse_arg(lg(A), &y1, &y2, &skip);
457 28 : return vecsmallslice_i(A, t,lB,y1,skip);
458 63 : case t_LIST:
459 63 : if (list_typ(A) == t_LIST_RAW)
460 : {
461 63 : GEN y, z = list_data(A);
462 63 : long l = z? lg(z): 1;
463 63 : lB = vecslice_parse_arg(l, &y1, &y2, &skip);
464 63 : y = mklist(); if (!z) return y;
465 63 : list_data(y) = vecslice_i(z, t_VEC,lB,y1,skip);
466 63 : return y;
467 : }
468 : default:
469 7 : pari_err_TYPE("_[_.._]",A);
470 : return NULL;/*LCOV_EXCL_LINE*/
471 : }
472 : }
473 :
474 : GEN
475 210 : matslice0(GEN A, long x1, long x2, long y1, long y2)
476 : {
477 : GEN B;
478 210 : long i, lB, lA = lg(A), rA, t, skip, rskip, rlB;
479 210 : long is_col = y1!=LONG_MAX && y2==LONG_MAX;
480 210 : long is_row = x1!=LONG_MAX && x2==LONG_MAX;
481 : GEN (*slice)(GEN A, long t, long lB, long y1, long skip);
482 210 : if (typ(A)!=t_MAT) pari_err_TYPE("_[_.._,_.._]",A);
483 210 : lB = vecslice_parse_arg(lA, &y1, &y2, &skip);
484 210 : if (is_col) return vecslice0(gel(A, y1), x1, x2);
485 196 : rA = lg(A)==1 ? 1: lgcols(A);
486 196 : rlB = vecslice_parse_arg(rA, &x1, &x2, &rskip);
487 196 : t = lg(A)==1 ? t_COL: typ(gel(A,1));
488 196 : if (is_row) return t == t_COL ? rowslice_i(A, lB, x1, y1, skip):
489 0 : rowsmallslice_i(A, lB, x1, y1, skip);
490 182 : slice = t == t_COL? &vecslice_i: &vecsmallslice_i;
491 :
492 182 : B = cgetg(lB, t_MAT);
493 1281 : for (i=1; i<lB; i++, y1++)
494 : {
495 1099 : if (y1 == skip) { i--; continue; }
496 1085 : gel(B,i) = slice(gel(A,y1),t,rlB, x1, rskip);
497 : }
498 182 : return B;
499 : }
500 :
501 : GEN
502 10597 : vecrange(GEN a, GEN b)
503 : {
504 : GEN y;
505 : long i, l;
506 10597 : if (typ(a)!=t_INT) pari_err_TYPE("[_.._]",a);
507 10590 : if (typ(b)!=t_INT) pari_err_TYPE("[_.._]",b);
508 10583 : if (cmpii(a,b)>0) return cgetg(1,t_VEC);
509 10576 : l = itos(subii(b,a))+1;
510 10576 : a = setloop(a);
511 10576 : y = cgetg(l+1, t_VEC);
512 26384087 : for (i=1; i<=l; a = incloop(a), i++) gel(y,i) = icopy(a);
513 10576 : return y;
514 : }
515 :
516 : GEN
517 0 : vecrangess(long a, long b)
518 : {
519 : GEN y;
520 : long i, l;
521 0 : if (a>b) return cgetg(1,t_VEC);
522 0 : l = b-a+1;
523 0 : y = cgetg(l+1, t_VEC);
524 0 : for (i=1; i<=l; a++, i++) gel(y,i) = stoi(a);
525 0 : return y;
526 : }
527 :
528 : GEN
529 96 : genindexselect(void *E, long (*f)(void* E, GEN x), GEN A)
530 : {
531 : long l, i, lv;
532 : GEN v, z;
533 : pari_sp av;
534 96 : clone_lock(A);
535 96 : switch(typ(A))
536 : {
537 7 : case t_LIST:
538 7 : z = list_data(A);
539 7 : l = z? lg(z): 1;
540 7 : break;
541 82 : case t_VEC: case t_COL: case t_MAT:
542 82 : l = lg(A);
543 82 : z = A;
544 82 : break;
545 7 : default:
546 7 : pari_err_TYPE("select",A);
547 : return NULL;/*LCOV_EXCL_LINE*/
548 : }
549 89 : v = cgetg(l, t_VECSMALL);
550 89 : av = avma;
551 12801 : for (i = lv = 1; i < l; i++) {
552 12712 : if (f(E, gel(z,i))) v[lv++] = i;
553 12712 : set_avma(av);
554 : }
555 89 : clone_unlock_deep(A); fixlg(v, lv); return v;
556 : }
557 : static GEN
558 94 : extract_copy(GEN A, GEN v)
559 : {
560 94 : long i, l = lg(v);
561 94 : GEN B = cgetg(l, typ(A));
562 4581 : for (i = 1; i < l; i++) gel(B,i) = gcopy(gel(A,v[i]));
563 94 : return B;
564 : }
565 : /* as genselect, but treat A [ t_VEC,t_COL, or t_MAT] as a t_VEC */
566 : GEN
567 0 : vecselect(void *E, long (*f)(void* E, GEN x), GEN A)
568 : {
569 : GEN v;
570 0 : clone_lock(A);
571 0 : v = genindexselect(E, f, A);
572 0 : A = extract_copy(A, v); settyp(A, t_VEC);
573 0 : clone_unlock_deep(A); return A;
574 : }
575 : GEN
576 83 : genselect(void *E, long (*f)(void* E, GEN x), GEN A)
577 : {
578 : GEN y, z, v;/* v left on stack for efficiency */
579 83 : clone_lock(A);
580 83 : switch(typ(A))
581 : {
582 14 : case t_LIST:
583 14 : z = list_data(A);
584 14 : if (!z) y = mklist();
585 : else
586 : {
587 : GEN B;
588 14 : y = cgetg(3, t_LIST);
589 14 : v = genindexselect(E, f, z);
590 14 : B = extract_copy(z, v);
591 14 : y[1] = lg(B)-1;
592 14 : list_data(y) = B;
593 : }
594 14 : break;
595 62 : case t_VEC: case t_COL: case t_MAT:
596 62 : v = genindexselect(E, f, A);
597 62 : y = extract_copy(A, v);
598 62 : break;
599 7 : default:
600 7 : pari_err_TYPE("select",A);
601 : return NULL;/*LCOV_EXCL_LINE*/
602 : }
603 76 : clone_unlock_deep(A); return y;
604 : }
605 :
606 : static void
607 54689 : check_callgen1(GEN f, const char *s)
608 : {
609 54689 : if (typ(f) != t_CLOSURE || closure_is_variadic(f) || closure_arity(f) < 1)
610 0 : pari_err_TYPE(s, f);
611 54689 : }
612 :
613 : GEN
614 103 : select0(GEN f, GEN x, long flag)
615 : {
616 103 : check_callgen1(f, "select");
617 103 : switch(flag)
618 : {
619 83 : case 0: return genselect((void *) f, gp_callbool, x);
620 20 : case 1: return genindexselect((void *) f, gp_callbool, x);
621 0 : default: pari_err_FLAG("select");
622 : return NULL;/*LCOV_EXCL_LINE*/
623 : }
624 : }
625 :
626 : GEN
627 30 : parselect(GEN C, GEN D, long flag)
628 : {
629 : pari_sp av, av2;
630 30 : long lv, l = lg(D), i, pending = 0, workid;
631 : GEN V, done;
632 : struct pari_mt pt;
633 30 : check_callgen1(C, "parselect");
634 30 : if (!is_vec_t(typ(D))) pari_err_TYPE("parselect",D);
635 30 : V = cgetg(l, t_VECSMALL); av = avma;
636 30 : mt_queue_start_lim(&pt, C, l-1);
637 30 : av2 = avma;
638 30364 : for (i=1; i<l || pending; i++)
639 : {
640 30334 : mt_queue_submit(&pt, i, i<l? mkvec(gel(D,i)): NULL);
641 30334 : done = mt_queue_get(&pt, &workid, &pending);
642 30334 : if (done) V[workid] = !gequal0(done);
643 30334 : set_avma(av2);
644 : }
645 30 : mt_queue_end(&pt);
646 30 : set_avma(av);
647 30054 : for (lv=1, i=1; i<l; i++)
648 30024 : if (V[i]) V[lv++]=i;
649 30 : fixlg(V, lv);
650 30 : return flag? V: extract_copy(D, V);
651 : }
652 :
653 : GEN
654 0 : veccatapply(void *E, GEN (*f)(void* E, GEN x), GEN x)
655 : {
656 0 : pari_sp av = avma;
657 0 : GEN v = vecapply(E, f, x);
658 0 : return lg(v) == 1? v: gerepilecopy(av, shallowconcat1(v));
659 : }
660 :
661 : static GEN
662 14 : vecapply2(void *E, GEN (*f)(void* E, GEN x), GEN x)
663 : {
664 : long i, lx;
665 14 : GEN y = cgetg_copy(x, &lx); y[1] = x[1];
666 56 : for (i=2; i<lx; i++) gel(y,i) = f(E, gel(x,i));
667 14 : return y;
668 : }
669 : static GEN
670 168162 : vecapply1(void *E, GEN (*f)(void* E, GEN x), GEN x)
671 : {
672 : long i, lx;
673 168162 : GEN y = cgetg_copy(x, &lx);
674 863891 : for (i=1; i<lx; i++) gel(y,i) = f(E, gel(x,i));
675 168162 : return y;
676 : }
677 : static GEN
678 7 : mapapply1(void *E, GEN (*f)(void* E, GEN x), GEN x)
679 : {
680 : long i, lx;
681 7 : GEN y = cgetg_copy(x, &lx);
682 28 : for (i=1; i<lx; i++)
683 : {
684 21 : GEN xi = gel(x, i);
685 21 : gel(y,i) = mkvec2(mkvec2(gcopy(gmael(xi, 1, 1)), f(E,gmael(xi, 1, 2))),
686 21 : gcopy(gel(xi, 2)));
687 : }
688 7 : return y;
689 : }
690 : /* as genapply, but treat A [ t_VEC,t_COL, or t_MAT] as a t_VEC */
691 : GEN
692 115336 : vecapply(void *E, GEN (*f)(void* E, GEN x), GEN x)
693 : {
694 : GEN y;
695 115336 : clone_lock(x); y = vecapply1(E,f,x);
696 115336 : clone_unlock_deep(x); settyp(y, t_VEC); return y;
697 : }
698 : GEN
699 52812 : genapply(void *E, GEN (*f)(void* E, GEN x), GEN x)
700 : {
701 52812 : long i, lx, tx = typ(x);
702 : GEN y, z;
703 52812 : if (is_scalar_t(tx)) return f(E, x);
704 52812 : clone_lock(x);
705 52812 : switch(tx) {
706 7 : case t_POL: y = normalizepol(vecapply2(E,f,x)); break;
707 7 : case t_SER:
708 7 : y = ser_isexactzero(x)? gcopy(x): normalizeser(vecapply2(E,f,x));
709 7 : break;
710 28 : case t_LIST:
711 : {
712 28 : long t = list_typ(x);
713 28 : z = list_data(x);
714 28 : if (!z)
715 7 : y = mklist_typ(t);
716 : else
717 : {
718 21 : y = cgetg(3, t_LIST);
719 21 : y[1] = evaltyp(t)|evallg(lg(z)-1);
720 : switch(t)
721 : {
722 14 : case t_LIST_RAW:
723 14 : list_data(y) = vecapply1(E,f,z);
724 14 : break;
725 7 : case t_LIST_MAP:
726 7 : list_data(y) = mapapply1(E,f,z);
727 7 : break;
728 : }
729 28 : }
730 : }
731 28 : break;
732 42 : case t_MAT:
733 42 : y = cgetg_copy(x, &lx);
734 126 : for (i = 1; i < lx; i++) gel(y,i) = vecapply1(E,f,gel(x,i));
735 42 : break;
736 :
737 52728 : case t_VEC: case t_COL: y = vecapply1(E,f,x); break;
738 0 : default:
739 0 : pari_err_TYPE("apply",x);
740 : return NULL;/*LCOV_EXCL_LINE*/
741 : }
742 52812 : clone_unlock_deep(x); return y;
743 : }
744 :
745 : GEN
746 52812 : apply0(GEN f, GEN x)
747 : {
748 52812 : check_callgen1(f, "apply");
749 52812 : return genapply((void *) f, gp_call, x);
750 : }
751 :
752 : GEN
753 448 : vecselapply(void *Epred, long (*pred)(void* E, GEN x), void *Efun,
754 : GEN (*fun)(void* E, GEN x), GEN A)
755 : {
756 : GEN y;
757 448 : long i, l = lg(A), nb=1;
758 448 : clone_lock(A); y = cgetg(l, t_VEC);
759 26165475 : for (i=1; i<l; i++)
760 26165027 : if (pred(Epred, gel(A,i))) gel(y,nb++) = fun(Efun, gel(A,i));
761 448 : fixlg(y,nb); clone_unlock_deep(A); return y;
762 : }
763 :
764 : GEN
765 0 : veccatselapply(void *Epred, long (*pred)(void* E, GEN x), void *Efun,
766 : GEN (*fun)(void* E, GEN x), GEN A)
767 : {
768 0 : pari_sp av = avma;
769 0 : GEN v = vecselapply(Epred, pred, Efun, fun, A);
770 0 : return lg(v) == 1? v: gerepilecopy(av, shallowconcat1(v));
771 : }
772 :
773 : /* suitable for gerepileupto */
774 : GEN
775 75 : parapply_slice_worker(GEN D, GEN worker)
776 : {
777 : long l, i;
778 75 : GEN w = cgetg_copy(D, &l);
779 17555 : for (i = 1; i < l; i++) gel(w,i) = closure_callgen1(worker, gel(D,i));
780 75 : return w;
781 : }
782 :
783 : /* B <- {A[i] : i = r (mod m)}, 1 <= r <= m */
784 : static void
785 77958 : arithprogset(GEN B, GEN A, long r, long m)
786 : {
787 77958 : long i, k, l = lg(A);
788 176082 : for (k = 1, i = r; i < l; i += m, k++) gel(B, k) = gel(A, i);
789 77958 : setlg(B, k);
790 77958 : }
791 : GEN
792 7307 : gen_parapply_slice(GEN worker, GEN D, long mmin)
793 : {
794 7307 : long l, r, n = lg(D)-1, m = minss(mmin, n), pending = 0;
795 7307 : GEN L = cgetg(n / m + 2, t_VEC), va = mkvec(L), V = cgetg_copy(D, &l);
796 : struct pari_mt pt;
797 7307 : mt_queue_start_lim(&pt, worker, m);
798 155916 : for (r = 1; r <= m || pending; r++)
799 : {
800 : long workid;
801 : GEN done;
802 148609 : if (r <= m) arithprogset(L, D, r, m);
803 148609 : mt_queue_submit(&pt, r, r <= m? va: NULL);
804 148609 : done = mt_queue_get(&pt, &workid, &pending);
805 148609 : if (done)
806 : {
807 77958 : long j, k, J = lg(done)-1;
808 176082 : for (j = 1, k = workid; j <= J; j++, k +=m) gel(V, k) = gel(done, j);
809 : }
810 : }
811 7307 : mt_queue_end(&pt); return V;
812 : }
813 :
814 : GEN
815 374089 : gen_parapply_percent(GEN worker, GEN D, long percent)
816 : {
817 374089 : long l = lg(D), i, pending = 0, cnt = 0, lper = -1, lcnt = 0;
818 : GEN W, V;
819 : struct pari_mt pt;
820 :
821 374089 : if (l == 1) return cgetg(1, typ(D));
822 343682 : W = cgetg(2, t_VEC);
823 343684 : V = cgetg(l, typ(D));
824 343680 : mt_queue_start_lim(&pt, worker, l-1);
825 2762237 : for (i = 1; i < l || pending; i++)
826 : {
827 : long workid;
828 : GEN done;
829 2418554 : if (i < l) gel(W,1) = gel(D,i);
830 2418554 : mt_queue_submit(&pt, i, i<l? W: NULL);
831 2418457 : done = mt_queue_get(&pt, &workid, &pending);
832 2418498 : if (done)
833 : {
834 2344278 : gel(V,workid) = done;
835 2344278 : if (percent && (++cnt)-lcnt>=percent)
836 : {
837 0 : long per = (long)(cnt*100./(l-1));
838 0 : lcnt = cnt;
839 0 : if (per > lper) { err_printf("%ld%% ",per); lper = per; }
840 : }
841 : }
842 : }
843 343683 : mt_queue_end(&pt); return V;
844 : }
845 :
846 : GEN
847 371186 : gen_parapply(GEN worker, GEN D)
848 : {
849 371186 : return gen_parapply_percent(worker, D, 0);
850 : }
851 :
852 : GEN
853 1744 : parapply(GEN C, GEN D)
854 : {
855 1744 : pari_sp av = avma;
856 1744 : check_callgen1(C, "parapply");
857 1744 : if (!is_vec_t(typ(D))) pari_err_TYPE("parapply",D);
858 1744 : return gerepileupto(av, gen_parapply(C, D));
859 : }
860 :
861 : GEN
862 28 : genfold(void *E, GEN (*f)(void* E, GEN x, GEN y), GEN x)
863 : {
864 28 : pari_sp av = avma;
865 : GEN z;
866 28 : long i, l = lg(x);
867 28 : if (!is_vec_t(typ(x))|| l==1 ) pari_err_TYPE("fold",x);
868 28 : clone_lock(x);
869 28 : z = gel(x,1);
870 119 : for (i=2; i<l; i++)
871 : {
872 91 : z = f(E,z,gel(x,i));
873 91 : if (gc_needed(av, 2))
874 : {
875 0 : if (DEBUGMEM>1) pari_warn(warnmem,"fold");
876 0 : z = gerepilecopy(av, z);
877 : }
878 : }
879 28 : clone_unlock_deep(x);
880 28 : return gerepilecopy(av, z);
881 : }
882 :
883 : GEN
884 28 : fold0(GEN f, GEN x)
885 : {
886 28 : if (typ(f) != t_CLOSURE || closure_arity(f) < 2) pari_err_TYPE("fold",f);
887 28 : return genfold((void *) f, gp_call2, x);
888 : }
889 : /*******************************************************************/
890 : /* */
891 : /* SCALAR-MATRIX OPERATIONS */
892 : /* */
893 : /*******************************************************************/
894 : GEN
895 359539 : gtomat(GEN x)
896 : {
897 : long lx, i;
898 : GEN y;
899 :
900 359539 : if (!x) return cgetg(1, t_MAT);
901 359518 : switch(typ(x))
902 : {
903 28 : case t_LIST:
904 28 : if (list_typ(x)==t_LIST_MAP)
905 14 : return maptomat(x);
906 14 : x = list_data(x);
907 14 : if (!x) return cgetg(1, t_MAT);
908 : /* fall through */
909 : case t_VEC: {
910 2773 : lx=lg(x); y=cgetg(lx,t_MAT);
911 2779 : if (lx == 1) break;
912 2779 : if (typ(gel(x,1)) == t_COL) {
913 2415 : long h = lgcols(x);
914 5698 : for (i=2; i<lx; i++) {
915 3283 : if (typ(gel(x,i)) != t_COL || lg(gel(x,i)) != h) break;
916 : }
917 2415 : if (i == lx) { /* matrix with h-1 rows */
918 2415 : y = cgetg(lx, t_MAT);
919 8113 : for (i=1 ; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
920 2415 : return y;
921 : }
922 : }
923 1092 : for (i=1; i<lx; i++) gel(y,i) = mkcolcopy(gel(x,i));
924 364 : break;
925 : }
926 112546 : case t_COL:
927 112546 : lx = lg(x);
928 112546 : if (lx == 1) return cgetg(1, t_MAT);
929 112532 : if (typ(gel(x,1)) == t_VEC) {
930 7 : long j, h = lg(gel(x,1));
931 14 : for (i=2; i<lx; i++) {
932 7 : if (typ(gel(x,i)) != t_VEC || lg(gel(x,i)) != h) break;
933 : }
934 7 : if (i == lx) { /* matrix with h cols */
935 7 : y = cgetg(h, t_MAT);
936 28 : for (j=1 ; j<h; j++) {
937 21 : gel(y,j) = cgetg(lx, t_COL);
938 63 : for (i=1; i<lx; i++) gcoeff(y,i,j) = gcopy(gmael(x,i,j));
939 : }
940 7 : return y;
941 : }
942 : }
943 112525 : y = mkmatcopy(x); break;
944 243282 : case t_MAT:
945 243282 : y = gcopy(x); break;
946 7 : case t_QFB: {
947 : GEN b;
948 7 : y = cgetg(3,t_MAT); b = gmul2n(gel(x,2),-1);
949 7 : gel(y,1) = mkcol2(icopy(gel(x,1)), b);
950 7 : gel(y,2) = mkcol2(b, icopy(gel(x,3)));
951 7 : break;
952 : }
953 889 : default:
954 889 : y = cgetg(2,t_MAT); gel(y,1) = mkcolcopy(x);
955 889 : break;
956 : }
957 357070 : return y;
958 : }
959 :
960 : /* create the diagonal matrix, whose diagonal is given by x */
961 : GEN
962 1355112 : diagonal(GEN x)
963 : {
964 1355112 : long j, lx, tx = typ(x);
965 : GEN y;
966 :
967 1355112 : if (! is_matvec_t(tx)) return scalarmat(x,1);
968 1355105 : if (tx==t_MAT)
969 : {
970 14 : if (RgM_isdiagonal(x)) return gcopy(x);
971 7 : pari_err_TYPE("diagonal",x);
972 : }
973 1355091 : lx=lg(x); y=cgetg(lx,t_MAT);
974 4843780 : for (j=1; j<lx; j++)
975 : {
976 3488687 : gel(y,j) = zerocol(lx-1);
977 3488687 : gcoeff(y,j,j) = gcopy(gel(x,j));
978 : }
979 1355093 : return y;
980 : }
981 : /* same, assuming x is a t_VEC/t_COL. Not memory clean. */
982 : GEN
983 350167 : diagonal_shallow(GEN x)
984 : {
985 350167 : long j, lx = lg(x);
986 350167 : GEN y = cgetg(lx,t_MAT);
987 :
988 962198 : for (j=1; j<lx; j++)
989 : {
990 612003 : gel(y,j) = zerocol(lx-1);
991 612016 : gcoeff(y,j,j) = gel(x,j);
992 : }
993 350195 : return y;
994 : }
995 :
996 : GEN
997 448 : zv_diagonal(GEN x)
998 : {
999 448 : long j, l = lg(x), n = l-1;
1000 448 : GEN y = cgetg(l,t_MAT);
1001 :
1002 1428 : for (j = 1; j < l; j++)
1003 : {
1004 980 : gel(y,j) = zero_Flv(n);
1005 980 : ucoeff(y,j,j) = uel(x,j);
1006 : }
1007 448 : return y;
1008 : }
1009 :
1010 : /* compute m*diagonal(d) */
1011 : GEN
1012 70 : matmuldiagonal(GEN m, GEN d)
1013 : {
1014 : long j, lx;
1015 70 : GEN y = cgetg_copy(m, &lx);
1016 :
1017 70 : if (typ(m)!=t_MAT) pari_err_TYPE("matmuldiagonal",m);
1018 70 : if (! is_vec_t(typ(d))) pari_err_TYPE("matmuldiagonal",d);
1019 70 : if (lg(d) != lx) pari_err_OP("operation 'matmuldiagonal'", m,d);
1020 350 : for (j=1; j<lx; j++) gel(y,j) = RgC_Rg_mul(gel(m,j), gel(d,j));
1021 70 : return y;
1022 : }
1023 :
1024 : /* compute A*B assuming the result is a diagonal matrix */
1025 : GEN
1026 7 : matmultodiagonal(GEN A, GEN B)
1027 : {
1028 7 : long i, j, hA, hB, lA = lg(A), lB = lg(B);
1029 7 : GEN y = matid(lB-1);
1030 :
1031 7 : if (typ(A) != t_MAT) pari_err_TYPE("matmultodiagonal",A);
1032 7 : if (typ(B) != t_MAT) pari_err_TYPE("matmultodiagonal",B);
1033 7 : hA = (lA == 1)? lB: lgcols(A);
1034 7 : hB = (lB == 1)? lA: lgcols(B);
1035 7 : if (lA != hB || lB != hA) pari_err_OP("operation 'matmultodiagonal'", A,B);
1036 56 : for (i=1; i<lB; i++)
1037 : {
1038 49 : GEN z = gen_0;
1039 392 : for (j=1; j<lA; j++) z = gadd(z, gmul(gcoeff(A,i,j),gcoeff(B,j,i)));
1040 49 : gcoeff(y,i,i) = z;
1041 : }
1042 7 : return y;
1043 : }
1044 :
1045 : /* [m[1,1], ..., m[l,l]], internal */
1046 : GEN
1047 875789 : RgM_diagonal_shallow(GEN m)
1048 : {
1049 875789 : long i, lx = lg(m);
1050 875789 : GEN y = cgetg(lx,t_VEC);
1051 2879842 : for (i=1; i<lx; i++) gel(y, i) = gcoeff(m,i,i);
1052 875802 : return y;
1053 : }
1054 :
1055 : /* same, public function */
1056 : GEN
1057 0 : RgM_diagonal(GEN m)
1058 : {
1059 0 : long i, lx = lg(m);
1060 0 : GEN y = cgetg(lx,t_VEC);
1061 0 : for (i=1; i<lx; i++) gel(y,i) = gcopy(gcoeff(m,i,i));
1062 0 : return y;
1063 : }
|