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 : /********************************************************************/
16 : /** **/
17 : /** GENERIC OPERATIONS **/
18 : /** (second part) **/
19 : /** **/
20 : /********************************************************************/
21 : #include "pari.h"
22 : #include "paripriv.h"
23 :
24 : /*********************************************************************/
25 : /** **/
26 : /** MAP FUNCTIONS WITH GIVEN PROTOTYPES **/
27 : /** **/
28 : /*********************************************************************/
29 : GEN
30 462 : map_proto_G(GEN (*f)(GEN), GEN x)
31 : {
32 462 : if (is_matvec_t(typ(x))) pari_APPLY_same(map_proto_G(f, gel(x,i)));
33 462 : return f(x);
34 : }
35 :
36 : GEN
37 37294049 : map_proto_lG(long (*f)(GEN), GEN x)
38 : {
39 37294133 : if (is_matvec_t(typ(x))) pari_APPLY_same(map_proto_lG(f, gel(x,i)));
40 37283234 : return stoi(f(x));
41 : }
42 :
43 : GEN
44 126 : map_proto_lGL(long (*f)(GEN,long), GEN x, long y)
45 : {
46 154 : if (is_matvec_t(typ(x))) pari_APPLY_same(map_proto_lGL(f,gel(x,i),y));
47 119 : return stoi(f(x,y));
48 : }
49 :
50 : static GEN
51 2204941 : _domul(void *data, GEN x, GEN y)
52 : {
53 2204941 : GEN (*mul)(GEN,GEN)=(GEN (*)(GEN,GEN)) data;
54 2204941 : return mul(x,y);
55 : }
56 :
57 : GEN
58 2418429 : gassoc_proto(GEN (*f)(GEN,GEN), GEN x, GEN y)
59 : {
60 2418429 : if (!y)
61 : {
62 2418429 : pari_sp av = avma;
63 2418429 : switch(typ(x))
64 : {
65 21 : case t_LIST:
66 21 : x = list_data(x); if (!x) return gen_1;
67 : case t_VEC:
68 2418415 : case t_COL: break;
69 7 : default: pari_err_TYPE("association",x);
70 : }
71 2418415 : return gerepileupto(av, gen_product(x, (void *)f, _domul));
72 :
73 : }
74 0 : return f(x,y);
75 : }
76 : /*******************************************************************/
77 : /* */
78 : /* CREATION OF A P-ADIC GEN */
79 : /* */
80 : /*******************************************************************/
81 : GEN
82 16899467 : cgetp(GEN x)
83 : {
84 16899467 : GEN y = cgetg(5,t_PADIC);
85 16899401 : y[1] = (x[1]&PRECPBITS) | _evalvalp(0);
86 16899401 : gel(y,2) = icopy(gel(x,2));
87 16899339 : gel(y,3) = icopy(gel(x,3));
88 16899431 : gel(y,4) = cgeti(lgefint(gel(x,3))); return y;
89 : }
90 :
91 : /*******************************************************************/
92 : /* */
93 : /* SIZES */
94 : /* */
95 : /*******************************************************************/
96 :
97 : long
98 5141966 : glength(GEN x)
99 : {
100 5141966 : long tx = typ(x);
101 5141966 : switch(tx)
102 : {
103 126 : case t_INT: return lgefint(x)-2;
104 539 : case t_LIST: {
105 539 : GEN L = list_data(x);
106 539 : return L? lg(L)-1: 0;
107 : }
108 14 : case t_REAL: return signe(x)? lg(x)-2: 0;
109 11 : case t_STR: return strlen( GSTR(x) );
110 91 : case t_VECSMALL: return lg(x)-1;
111 : }
112 5141185 : return lg(x) - lontyp[tx];
113 : }
114 :
115 : long
116 0 : gtranslength(GEN x)
117 : {
118 0 : switch(typ(x))
119 : {
120 0 : case t_VEC: case t_COL:
121 0 : return lg(x)-1;
122 0 : case t_MAT:
123 0 : return lg(x)==1 ? 0: nbrows(x);
124 0 : default:
125 0 : pari_err_TYPE("trans",x);
126 : return 0; /* LCOV_EXCL_LINE */
127 : }
128 : }
129 :
130 : GEN
131 203 : matsize(GEN x)
132 : {
133 203 : long L = lg(x) - 1;
134 203 : switch(typ(x))
135 : {
136 7 : case t_VEC: return mkvec2s(1, L);
137 7 : case t_COL: return mkvec2s(L, 1);
138 182 : case t_MAT: return mkvec2s(L? nbrows(x): 0, L);
139 : }
140 7 : pari_err_TYPE("matsize",x);
141 : return NULL; /* LCOV_EXCL_LINE */
142 : }
143 :
144 : /*******************************************************************/
145 : /* */
146 : /* CONVERSION GEN --> long */
147 : /* */
148 : /*******************************************************************/
149 :
150 : long
151 77 : gtolong(GEN x)
152 : {
153 77 : switch(typ(x))
154 : {
155 42 : case t_INT:
156 42 : return itos(x);
157 7 : case t_REAL:
158 7 : return (long)(rtodbl(x) + 0.5);
159 7 : case t_FRAC:
160 7 : { pari_sp av = avma; return gc_long(av, itos(ground(x))); }
161 7 : case t_COMPLEX:
162 7 : if (gequal0(gel(x,2))) return gtolong(gel(x,1)); break;
163 7 : case t_QUAD:
164 7 : if (gequal0(gel(x,3))) return gtolong(gel(x,2)); break;
165 : }
166 7 : pari_err_TYPE("gtolong",x);
167 : return 0; /* LCOV_EXCL_LINE */
168 : }
169 :
170 : /*******************************************************************/
171 : /* */
172 : /* COMPARISONS */
173 : /* */
174 : /*******************************************************************/
175 : static void
176 189 : chk_true_err()
177 : {
178 189 : GEN E = pari_err_last();
179 189 : switch(err_get_num(E))
180 : {
181 0 : case e_STACK: case e_MEM: case e_ALARM:
182 0 : pari_err(0, E); /* rethrow */
183 : }
184 189 : }
185 : /* x - y == 0 or undefined */
186 : static int
187 3196114 : gequal_try(GEN x, GEN y)
188 : {
189 : int i;
190 3196114 : pari_CATCH(CATCH_ALL) { chk_true_err(); return 0; }
191 3196114 : pari_TRY { i = gequal0(gadd(x, gneg_i(y))); } pari_ENDCATCH;
192 3195932 : return i;
193 : }
194 : /* x + y == 0 or undefined */
195 : static int
196 28 : gmequal_try(GEN x, GEN y)
197 : {
198 : int i;
199 28 : pari_CATCH(CATCH_ALL) { chk_true_err(); return 0; }
200 28 : pari_TRY { i = gequal0(gadd(x, y)); } pari_ENDCATCH;
201 21 : return i;
202 : }
203 :
204 : int
205 450603059 : isexactzero(GEN g)
206 : {
207 : long i, lx;
208 450603059 : switch (typ(g))
209 : {
210 392665966 : case t_INT:
211 392665966 : return !signe(g);
212 1242212 : case t_INTMOD:
213 1242212 : return !signe(gel(g,2));
214 14809238 : case t_COMPLEX:
215 14809238 : return isexactzero(gel(g,1)) && isexactzero(gel(g,2));
216 8107278 : case t_FFELT:
217 8107278 : return FF_equal0(g);
218 511 : case t_QUAD:
219 511 : return isexactzero(gel(g,2)) && isexactzero(gel(g,3));
220 290166 : case t_POLMOD:
221 290166 : return isexactzero(gel(g,2));
222 12069562 : case t_POL:
223 12069562 : lx = lg(g); /* cater for Mod(0,2)*x^0 */
224 12069562 : return lx == 2 || (lx == 3 && isexactzero(gel(g,2)));
225 462452 : case t_RFRAC:
226 462452 : return isexactzero(gel(g,1)); /* may occur: Mod(0,2)/x */
227 43435 : case t_VEC: case t_COL: case t_MAT:
228 43820 : for (i=lg(g)-1; i; i--)
229 43673 : if (!isexactzero(gel(g,i))) return 0;
230 147 : return 1;
231 : }
232 20912239 : return 0;
233 : }
234 : GEN
235 63700684 : gisexactzero(GEN g)
236 : {
237 : long i, lx;
238 : GEN a, b;
239 63700684 : switch (typ(g))
240 : {
241 26747722 : case t_INT:
242 26747722 : return !signe(g)? g: NULL;
243 7065821 : case t_INTMOD:
244 7065821 : return !signe(gel(g,2))? g: NULL;
245 2632 : case t_COMPLEX:
246 2632 : a = gisexactzero(gel(g,1)); if (!a) return NULL;
247 616 : b = gisexactzero(gel(g,2)); if (!b) return NULL;
248 0 : return ggcd(a,b);
249 20608 : case t_FFELT:
250 20608 : return FF_equal0(g)? g: NULL;
251 518 : case t_QUAD:
252 518 : a = gisexactzero(gel(g,2)); if (!a) return NULL;
253 77 : b = gisexactzero(gel(g,3)); if (!b) return NULL;
254 7 : return ggcd(a,b);
255 17004 : case t_POLMOD:
256 17004 : return gisexactzero(gel(g,2));
257 28070039 : case t_POL:
258 28070039 : lx = lg(g); /* cater for Mod(0,2)*x^0 */
259 28070039 : if (lx == 2) return gen_0;
260 22748693 : if (lx == 3) return gisexactzero(gel(g,2));
261 19027341 : return NULL;
262 1190027 : case t_RFRAC:
263 1190027 : return gisexactzero(gel(g,1)); /* may occur: Mod(0,2)/x */
264 0 : case t_VEC: case t_COL: case t_MAT:
265 0 : a = gen_0;
266 0 : for (i=lg(g)-1; i; i--)
267 : {
268 0 : b = gisexactzero(gel(g,i));
269 0 : if (!b) return NULL;
270 0 : a = ggcd(a, b);
271 : }
272 0 : return a;
273 : }
274 586313 : return NULL;
275 : }
276 :
277 : int
278 547456679 : isrationalzero(GEN g)
279 : {
280 : long i;
281 547456679 : switch (typ(g))
282 : {
283 341102308 : case t_INT:
284 341102308 : return !signe(g);
285 39268517 : case t_COMPLEX:
286 39268517 : return isintzero(gel(g,1)) && isintzero(gel(g,2));
287 1428 : case t_QUAD:
288 1428 : return isintzero(gel(g,2)) && isintzero(gel(g,3));
289 491704 : case t_POLMOD:
290 491704 : return isrationalzero(gel(g,2));
291 22393653 : case t_POL: return lg(g) == 2;
292 133 : case t_VEC: case t_COL: case t_MAT:
293 448 : for (i=lg(g)-1; i; i--)
294 315 : if (!isrationalzero(gel(g,i))) return 0;
295 133 : return 1;
296 : }
297 144198936 : return 0;
298 : }
299 :
300 : int
301 2250416714 : gequal0(GEN x)
302 : {
303 2250416714 : switch(typ(x))
304 : {
305 2087033521 : case t_INT: case t_REAL: case t_POL: case t_SER:
306 2087033521 : return !signe(x);
307 :
308 7844749 : case t_INTMOD:
309 7844749 : return !signe(gel(x,2));
310 :
311 649353 : case t_FFELT:
312 649353 : return FF_equal0(x);
313 :
314 105016442 : case t_COMPLEX:
315 : /* is 0 iff norm(x) would be 0 (can happen with Re(x) and Im(x) != 0
316 : * only if Re(x) and Im(x) are of type t_REAL). See mp.c:addrr().
317 : */
318 105016442 : if (gequal0(gel(x,1)))
319 : {
320 8087515 : if (gequal0(gel(x,2))) return 1;
321 7706545 : if (typ(gel(x,1))!=t_REAL || typ(gel(x,2))!=t_REAL) return 0;
322 268532 : return (expo(gel(x,1))>=expo(gel(x,2)));
323 : }
324 96929322 : if (gequal0(gel(x,2)))
325 : {
326 1647764 : if (typ(gel(x,1))!=t_REAL || typ(gel(x,2))!=t_REAL) return 0;
327 1563811 : return (expo(gel(x,2))>=expo(gel(x,1)));
328 : }
329 95285737 : return 0;
330 :
331 2046362 : case t_PADIC:
332 2046362 : return !signe(gel(x,4));
333 :
334 1806 : case t_QUAD:
335 1806 : return gequal0(gel(x,2)) && gequal0(gel(x,3));
336 :
337 8474607 : case t_POLMOD:
338 8474607 : return gequal0(gel(x,2));
339 :
340 6103921 : case t_RFRAC:
341 6103921 : return gequal0(gel(x,1));
342 :
343 9906722 : case t_VEC: case t_COL: case t_MAT:
344 : {
345 : long i;
346 22821042 : for (i=lg(x)-1; i; i--)
347 19042263 : if (!gequal0(gel(x,i))) return 0;
348 3778779 : return 1;
349 : }
350 : }
351 23339231 : return 0;
352 : }
353 :
354 : /* x a t_POL or t_SER, return 1 if test(coeff(X,d)) is true and
355 : * coeff(X,i) = 0 for all i != d. Return 0 (false) otherwise */
356 : static int
357 14684174 : is_monomial_test(GEN x, long d, int(*test)(GEN))
358 : {
359 14684174 : long i, l = lg(x);
360 14684174 : if (typ(x) == t_SER)
361 : { /* "0" * x^v * (1+O(x)) ? v <= 0 or null ring */
362 602 : if (l == 3 && isexactzero(gel(x,2))) return d >= 2 || test(gel(x,2));
363 553 : if (d < 2) return 0; /* v > 0 */
364 : }
365 14683943 : if (d >= l)
366 : {
367 60079 : if (typ(x) == t_POL) return 0; /* l = 2 */
368 : /* t_SER, v = 2-d <= 0 */
369 56 : if (!signe(x)) return 1;
370 : }
371 14623864 : else if (!test(gel(x,d))) return 0;
372 7356738 : for (i = 2; i < l; i++) /* 2 <= d < l */
373 4976498 : if (i != d && !gequal0(gel(x,i))) return 0;
374 2380240 : return 1;
375 : }
376 : static int
377 292411 : col_test(GEN x, int(*test)(GEN))
378 : {
379 292411 : long i, l = lg(x);
380 292411 : if (l == 1 || !test(gel(x,1))) return 0;
381 70 : for (i = 2; i < l; i++)
382 42 : if (!gequal0(gel(x,i))) return 0;
383 28 : return 1;
384 : }
385 : static int
386 16268 : mat_test(GEN x, int(*test)(GEN))
387 : {
388 16268 : long i, j, l = lg(x);
389 16268 : if (l == 1) return 1;
390 16254 : if (l != lgcols(x)) return 0;
391 51569 : for (i = 1; i < l; i++)
392 135142 : for (j = 1; j < l; j++)
393 99827 : if (i == j) {
394 35378 : if (!test(gcoeff(x,i,i))) return 0;
395 : } else {
396 64449 : if (!gequal0(gcoeff(x,i,j))) return 0;
397 : }
398 16191 : return 1;
399 : }
400 :
401 : /* returns 1 whenever x = 1, and 0 otherwise */
402 : int
403 312958137 : gequal1(GEN x)
404 : {
405 312958137 : switch(typ(x))
406 : {
407 293956975 : case t_INT:
408 293956975 : return equali1(x);
409 :
410 68558 : case t_REAL:
411 : {
412 68558 : long s = signe(x);
413 68558 : if (!s) return expo(x) >= 0;
414 68460 : return s > 0 ? absrnz_equal1(x): 0;
415 : }
416 632400 : case t_INTMOD:
417 632400 : return is_pm1(gel(x,2)) || is_pm1(gel(x,1));
418 393538 : case t_POLMOD:
419 393538 : return !degpol(gel(x,1)) || gequal1(gel(x,2));
420 :
421 16891 : case t_FFELT:
422 16891 : return FF_equal1(x);
423 :
424 1682391 : case t_FRAC:
425 1682391 : return 0;
426 :
427 21488 : case t_COMPLEX:
428 21488 : return gequal1(gel(x,1)) && gequal0(gel(x,2));
429 :
430 166225 : case t_PADIC:
431 166225 : if (!signe(gel(x,4))) return valp(x) <= 0;
432 166183 : return valp(x) == 0 && gequal1(gel(x,4));
433 :
434 42 : case t_QUAD:
435 42 : return gequal1(gel(x,2)) && gequal0(gel(x,3));
436 :
437 14683502 : case t_POL: return is_monomial_test(x, 2, &gequal1);
438 476 : case t_SER: return is_monomial_test(x, 2 - valser(x), &gequal1);
439 :
440 1028389 : case t_RFRAC: return gequal(gel(x,1), gel(x,2));
441 292362 : case t_COL: return col_test(x, &gequal1);
442 16156 : case t_MAT: return mat_test(x, &gequal1);
443 : }
444 30 : return 0;
445 : }
446 :
447 : /* returns 1 whenever the x = -1, 0 otherwise */
448 : int
449 74232864 : gequalm1(GEN x)
450 : {
451 : pari_sp av;
452 : GEN t;
453 :
454 74232864 : switch(typ(x))
455 : {
456 74224027 : case t_INT:
457 74224027 : return equalim1(x);
458 :
459 1484 : case t_REAL:
460 : {
461 1484 : long s = signe(x);
462 1484 : if (!s) return expo(x) >= 0;
463 1477 : return s < 0 ? absrnz_equal1(x): 0;
464 : }
465 4602 : case t_INTMOD:
466 4602 : av = avma; return gc_bool(av, equalii(addui(1,gel(x,2)), gel(x,1)));
467 :
468 154 : case t_FRAC:
469 154 : return 0;
470 :
471 42 : case t_FFELT:
472 42 : return FF_equalm1(x);
473 :
474 2044 : case t_COMPLEX:
475 2044 : return gequalm1(gel(x,1)) && gequal0(gel(x,2));
476 :
477 7 : case t_QUAD:
478 7 : return gequalm1(gel(x,2)) && gequal0(gel(x,3));
479 :
480 49 : case t_PADIC:
481 49 : t = gel(x,4); if (!signe(t)) return valp(x) <= 0;
482 21 : av = avma; return gc_bool(av, !valp(x) && equalii(addui(1,t), gel(x,3)));
483 :
484 56 : case t_POLMOD:
485 56 : return !degpol(gel(x,1)) || gequalm1(gel(x,2));
486 :
487 70 : case t_POL: return is_monomial_test(x, 2, &gequalm1);
488 126 : case t_SER: return is_monomial_test(x, 2 - valser(x), &gequalm1);
489 :
490 28 : case t_RFRAC:
491 28 : av = avma; return gc_bool(av, gmequal_try(gel(x,1), gel(x,2)));
492 49 : case t_COL: return col_test(x, &gequalm1);
493 112 : case t_MAT: return mat_test(x, &gequalm1);
494 : }
495 14 : return 0;
496 : }
497 :
498 : int
499 1469511 : gequalX(GEN x) { return typ(x) == t_POL && lg(x) == 4
500 2953830 : && isintzero(gel(x,2)) && isint1(gel(x,3)); }
501 :
502 : static int
503 672 : cmp_str(const char *x, const char *y)
504 : {
505 672 : int f = strcmp(x, y);
506 : return f > 0? 1
507 672 : : f? -1: 0;
508 : }
509 :
510 : static int
511 39093937 : cmp_universal_rec(GEN x, GEN y, long i0)
512 : {
513 39093937 : long i, lx = lg(x), ly = lg(y);
514 39093937 : if (lx < ly) return -1;
515 39091162 : if (lx > ly) return 1;
516 68764525 : for (i = i0; i < lx; i++)
517 : {
518 59875264 : int f = cmp_universal(gel(x,i), gel(y,i));
519 59875264 : if (f) return f;
520 : }
521 8889261 : return 0;
522 : }
523 : /* Universal "meaningless" comparison function. Transitive, returns 0 iff
524 : * gidentical(x,y) */
525 : int
526 84343322 : cmp_universal(GEN x, GEN y)
527 : {
528 84343322 : long lx, ly, i, tx = typ(x), ty = typ(y);
529 :
530 84343322 : if (tx < ty) return -1;
531 83983221 : if (ty < tx) return 1;
532 83456860 : switch(tx)
533 : {
534 43427206 : case t_INT: return cmpii(x,y);
535 651 : case t_STR: return cmp_str(GSTR(x),GSTR(y));
536 934976 : case t_REAL:
537 : case t_VECSMALL:
538 934976 : lx = lg(x);
539 934976 : ly = lg(y);
540 934976 : if (lx < ly) return -1;
541 886704 : if (lx > ly) return 1;
542 3586363 : for (i = 1; i < lx; i++)
543 : {
544 3478731 : if (x[i] < y[i]) return -1;
545 3109810 : if (x[i] > y[i]) return 1;
546 : }
547 107632 : return 0;
548 :
549 771626 : case t_POL:
550 : {
551 771626 : long X = x[1] & (VARNBITS|SIGNBITS);
552 771626 : long Y = y[1] & (VARNBITS|SIGNBITS);
553 771626 : if (X < Y) return -1;
554 771605 : if (X > Y) return 1;
555 771549 : return cmp_universal_rec(x, y, 2);
556 : }
557 881076 : case t_SER:
558 : case t_FFELT:
559 : case t_CLOSURE:
560 881076 : if (x[1] < y[1]) return -1;
561 881069 : if (x[1] > y[1]) return 1;
562 881062 : return cmp_universal_rec(x, y, 2);
563 :
564 35 : case t_LIST:
565 : {
566 35 : long tx = list_typ(x), ty = list_typ(y);
567 : GEN vx, vy;
568 : pari_sp av;
569 35 : if (tx < ty) return -1;
570 35 : if (tx > ty) return 1;
571 35 : vx = list_data(x);
572 35 : vy = list_data(y);
573 35 : if (!vx) return vy? -1: 0;
574 35 : if (!vy) return 1;
575 35 : av = avma;
576 35 : if (tx == t_LIST_MAP)
577 : {
578 14 : vx = maptomat_shallow(x);
579 14 : vy = maptomat_shallow(y);
580 : }
581 35 : return gc_int(av, cmp_universal_rec(vx, vy, 1));
582 : }
583 37441290 : default:
584 37441290 : return cmp_universal_rec(x, y, lontyp[tx]);
585 : }
586 : }
587 :
588 : static int
589 4588483 : cmpfrac(GEN x, GEN y)
590 : {
591 4588483 : pari_sp av = avma;
592 4588483 : GEN a = gel(x,1), b = gel(x,2);
593 4588483 : GEN c = gel(y,1), d = gel(y,2);
594 4588483 : return gc_bool(av, cmpii(mulii(a, d), mulii(b, c)));
595 : }
596 : static int
597 455541 : cmpifrac(GEN a, GEN y)
598 : {
599 455541 : pari_sp av = avma;
600 455541 : GEN c = gel(y,1), d = gel(y,2);
601 455541 : return gc_int(av, cmpii(mulii(a, d), c));
602 : }
603 : static int
604 81133 : cmprfrac(GEN a, GEN y)
605 : {
606 81133 : pari_sp av = avma;
607 81133 : GEN c = gel(y,1), d = gel(y,2);
608 81133 : return gc_int(av, cmpri(mulri(a, d), c));
609 : }
610 : static int
611 161 : cmpgen(GEN x, GEN y)
612 : {
613 161 : pari_sp av = avma;
614 161 : return gc_int(av, gsigne(gsub(x,y)));
615 : }
616 :
617 : /* returns the sign of x - y when it makes sense. 0 otherwise */
618 : int
619 289331961 : gcmp(GEN x, GEN y)
620 : {
621 289331961 : long tx = typ(x), ty = typ(y);
622 :
623 289331961 : if (tx == ty) /* generic case */
624 280622712 : switch(tx)
625 : {
626 153432358 : case t_INT: return cmpii(x, y);
627 122520084 : case t_REAL: return cmprr(x, y);
628 4588483 : case t_FRAC: return cmpfrac(x, y);
629 70 : case t_QUAD: return cmpgen(x, y);
630 21 : case t_STR: return cmp_str(GSTR(x), GSTR(y));
631 101774 : case t_INFINITY:
632 : {
633 101774 : long sx = inf_get_sign(x), sy = inf_get_sign(y);
634 101774 : if (sx < sy) return -1;
635 42 : if (sx > sy) return 1;
636 14 : return 0;
637 : }
638 : }
639 8689171 : if (ty == t_INFINITY) return -inf_get_sign(y);
640 8616382 : switch(tx)
641 : {
642 8046416 : case t_INT:
643 : switch(ty)
644 : {
645 7733648 : case t_REAL: return cmpir(x, y);
646 312754 : case t_FRAC: return cmpifrac(x, y);
647 7 : case t_QUAD: return cmpgen(x, y);
648 : }
649 7 : break;
650 379106 : case t_REAL:
651 : switch(ty)
652 : {
653 342252 : case t_INT: return cmpri(x, y);
654 36833 : case t_FRAC: return cmprfrac(x, y);
655 14 : case t_QUAD: return cmpgen(x, y);
656 : }
657 7 : break;
658 187101 : case t_FRAC:
659 : switch(ty)
660 : {
661 142787 : case t_INT: return -cmpifrac(y, x);
662 44300 : case t_REAL: return -cmprfrac(y, x);
663 7 : case t_QUAD: return cmpgen(x, y);
664 : }
665 7 : break;
666 63 : case t_QUAD:
667 63 : return cmpgen(x, y);
668 31653 : case t_INFINITY: return inf_get_sign(x);
669 : }
670 24 : pari_err_TYPE2("comparison",x,y);
671 : return 0;/*LCOV_EXCL_LINE*/
672 : }
673 :
674 : int
675 616386 : gcmpsg(long s, GEN y)
676 : {
677 616386 : switch(typ(y))
678 : {
679 12208 : case t_INT: return cmpsi(s,y);
680 599005 : case t_REAL: return cmpsr(s,y);
681 5173 : case t_FRAC: {
682 5173 : pari_sp av = avma;
683 5173 : return gc_int(av, cmpii(mulsi(s,gel(y,2)), gel(y,1)));
684 : }
685 0 : case t_QUAD: {
686 0 : pari_sp av = avma;
687 0 : return gc_int(av, gsigne(gsubsg(s, y)));
688 : }
689 0 : case t_INFINITY: return -inf_get_sign(y);
690 : }
691 0 : pari_err_TYPE2("comparison",stoi(s),y);
692 : return 0; /* LCOV_EXCL_LINE */
693 : }
694 :
695 : static long
696 3232579 : roughtype(GEN x)
697 : {
698 3232579 : switch(typ(x))
699 : {
700 2114 : case t_MAT: return t_MAT;
701 742362 : case t_VEC: case t_COL: return t_VEC;
702 1613554 : case t_VECSMALL: return t_VECSMALL;
703 874549 : default: return t_INT;
704 : }
705 : }
706 :
707 : static int lexcmpsg(long x, GEN y);
708 42 : static int lexcmpgs(GEN x, long y) { return -lexcmpsg(y,x); }
709 : /* lexcmp(stoi(x),y), y t_VEC/t_COL/t_MAT */
710 : static int
711 21 : lexcmp_s_matvec(long x, GEN y)
712 : {
713 : int fl;
714 21 : if (lg(y)==1) return 1;
715 14 : fl = lexcmpsg(x,gel(y,1));
716 14 : if (fl) return fl;
717 7 : return -1;
718 : }
719 : /* x a scalar, y a t_VEC/t_COL/t_MAT */
720 : static int
721 357 : lexcmp_scal_matvec(GEN x, GEN y)
722 : {
723 : int fl;
724 357 : if (lg(y)==1) return 1;
725 357 : fl = lexcmp(x,gel(y,1));
726 357 : if (fl) return fl;
727 7 : return -1;
728 : }
729 : /* x a scalar, y a t_VECSMALL */
730 : static int
731 42 : lexcmp_scal_vecsmall(GEN x, GEN y)
732 : {
733 : int fl;
734 42 : if (lg(y)==1) return 1;
735 42 : fl = lexcmpgs(x, y[1]);
736 42 : if (fl) return fl;
737 0 : return -1;
738 : }
739 :
740 : /* tx = ty = t_MAT, or x and y are both vect_t */
741 : static int
742 371973 : lexcmp_similar(GEN x, GEN y)
743 : {
744 371973 : long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
745 456827 : for (i=1; i<l; i++)
746 : {
747 425200 : int fl = lexcmp(gel(x,i),gel(y,i));
748 425200 : if (fl) return fl;
749 : }
750 31627 : if (lx == ly) return 0;
751 36 : return (lx < ly)? -1 : 1;
752 : }
753 : /* x a t_VECSMALL, y a t_VEC/t_COL ~ lexcmp_similar */
754 : static int
755 154 : lexcmp_vecsmall_vec(GEN x, GEN y)
756 : {
757 154 : long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
758 343 : for (i=1; i<l; i++)
759 : {
760 287 : int fl = lexcmpsg(x[i], gel(y,i));
761 287 : if (fl) return fl;
762 : }
763 56 : if (lx == ly) return 0;
764 21 : return (lx < ly)? -1 : 1;
765 : }
766 :
767 : /* x t_VEC/t_COL, y t_MAT */
768 : static int
769 98 : lexcmp_vec_mat(GEN x, GEN y)
770 : {
771 : int fl;
772 98 : if (lg(x)==1) return -1;
773 98 : if (lg(y)==1) return 1;
774 98 : fl = lexcmp_similar(x,gel(y,1));
775 98 : if (fl) return fl;
776 7 : return -1;
777 : }
778 : /* x t_VECSMALl, y t_MAT ~ lexcmp_vec_mat */
779 : static int
780 42 : lexcmp_vecsmall_mat(GEN x, GEN y)
781 : {
782 : int fl;
783 42 : if (lg(x)==1) return -1;
784 42 : if (lg(y)==1) return 1;
785 42 : fl = lexcmp_vecsmall_vec(x, gel(y,1));
786 42 : if (fl) return fl;
787 0 : return -1;
788 : }
789 :
790 : /* x a t_VECSMALL, not y */
791 : static int
792 196 : lexcmp_vecsmall_other(GEN x, GEN y, long ty)
793 : {
794 196 : switch(ty)
795 : {
796 42 : case t_MAT: return lexcmp_vecsmall_mat(x, y);
797 112 : case t_VEC: return lexcmp_vecsmall_vec(x, y);
798 42 : default: return -lexcmp_scal_vecsmall(y, x); /*y scalar*/
799 : }
800 : }
801 :
802 : /* lexcmp(stoi(s), y) */
803 : static int
804 343 : lexcmpsg(long x, GEN y)
805 : {
806 343 : switch(roughtype(y))
807 : {
808 21 : case t_MAT:
809 : case t_VEC:
810 21 : return lexcmp_s_matvec(x,y);
811 14 : case t_VECSMALL: /* ~ lexcmp_scal_matvec */
812 14 : if (lg(y)==1) return 1;
813 7 : return (x > y[1])? 1: -1;
814 308 : default: return gcmpsg(x,y);
815 : }
816 : }
817 :
818 : /* as gcmp for vector/matrices, using lexicographic ordering on components */
819 : static int
820 1616119 : lexcmp_i(GEN x, GEN y)
821 : {
822 1616119 : const long tx = roughtype(x), ty = roughtype(y);
823 1616121 : if (tx == ty)
824 1615470 : switch(tx)
825 : {
826 371874 : case t_MAT:
827 371874 : case t_VEC: return lexcmp_similar(x,y);
828 806672 : case t_VECSMALL: return vecsmall_lexcmp(x,y);
829 436924 : default: return gcmp(x,y);
830 : }
831 651 : if (tx == t_VECSMALL) return lexcmp_vecsmall_other(x,y,ty);
832 518 : if (ty == t_VECSMALL) return -lexcmp_vecsmall_other(y,x,tx);
833 :
834 455 : if (tx == t_INT) return lexcmp_scal_matvec(x,y); /*scalar*/
835 203 : if (ty == t_INT) return -lexcmp_scal_matvec(y,x);
836 :
837 98 : if (ty==t_MAT) return lexcmp_vec_mat(x,y);
838 42 : return -lexcmp_vec_mat(y,x); /*tx==t_MAT*/
839 : }
840 : int
841 1616119 : lexcmp(GEN x, GEN y)
842 : {
843 1616119 : pari_sp av = avma;
844 1616119 : if (typ(x) == t_COMPLEX)
845 : {
846 875 : x = mkvec2(gel(x,1), gel(x,2));
847 875 : if (typ(y) == t_COMPLEX) y = mkvec2(gel(y,1), gel(y,2));
848 49 : else y = mkvec2(y, gen_0);
849 : }
850 1615244 : else if (typ(y) == t_COMPLEX)
851 : {
852 63 : x = mkvec2(x, gen_0);
853 63 : y = mkvec2(gel(y,1), gel(y,2));
854 : }
855 1616119 : return gc_int(av, lexcmp_i(x, y));
856 : }
857 :
858 : /*****************************************************************/
859 : /* */
860 : /* EQUALITY */
861 : /* returns 1 if x == y, 0 otherwise */
862 : /* */
863 : /*****************************************************************/
864 : /* x,y t_POL */
865 : static int
866 3432196 : polidentical(GEN x, GEN y)
867 : {
868 : long lx;
869 3432196 : if (x[1] != y[1]) return 0;
870 3432098 : lx = lg(x); if (lg(y) != lg(x)) return 0;
871 14948834 : for (lx--; lx >= 2; lx--) if (!gidentical(gel(x,lx), gel(y,lx))) return 0;
872 3432007 : return 1;
873 : }
874 : /* x,y t_SER */
875 : static int
876 14 : seridentical(GEN x, GEN y) { return polidentical(x,y); }
877 : /* typ(x) = typ(y) = t_VEC/COL/MAT */
878 : static int
879 5249403 : vecidentical(GEN x, GEN y)
880 : {
881 : long i;
882 5249403 : if ((x[0] ^ y[0]) & (TYPBITS|LGBITS)) return 0;
883 16801751 : for (i = lg(x)-1; i; i--)
884 12831475 : if (! gidentical(gel(x,i),gel(y,i)) ) return 0;
885 3970276 : return 1;
886 : }
887 : static int
888 1547 : identicalrr(GEN x, GEN y)
889 : {
890 1547 : long i, lx = lg(x);
891 1547 : if (lg(y) != lx) return 0;
892 1547 : if (x[1] != y[1]) return 0;
893 5465 : i=2; while (i<lx && x[i]==y[i]) i++;
894 1540 : return (i == lx);
895 : }
896 :
897 : static int
898 70 : closure_identical(GEN x, GEN y)
899 : {
900 70 : if (lg(x)!=lg(y) || x[1]!=y[1]) return 0;
901 56 : if (!gidentical(gel(x,2),gel(y,2)) || !gidentical(gel(x,3),gel(y,3))
902 56 : || !gidentical(gel(x,4),gel(y,4))) return 0;
903 42 : if (lg(x)<8) return 1;
904 0 : return gidentical(gel(x,7),gel(y,7));
905 : }
906 :
907 : static int
908 343 : list_cmp(GEN x, GEN y, int cmp(GEN x, GEN y))
909 : {
910 343 : int t = list_typ(x);
911 : GEN vx, vy;
912 : long lvx, lvy;
913 343 : if (list_typ(y)!=t) return 0;
914 343 : vx = list_data(x);
915 343 : vy = list_data(y);
916 343 : lvx = vx ? lg(vx): 1;
917 343 : lvy = vy ? lg(vy): 1;
918 343 : if (lvx==1 && lvy==1) return 1;
919 329 : if (lvx != lvy) return 0;
920 301 : switch (t)
921 : {
922 280 : case t_LIST_MAP:
923 : {
924 280 : pari_sp av = avma;
925 280 : GEN mx = maptomat_shallow(x), my = maptomat_shallow(y);
926 280 : int ret = gidentical(gel(mx, 1), gel(my, 1)) && cmp(gel(mx, 2), gel(my, 2));
927 280 : return gc_bool(av, ret);
928 : }
929 21 : default:
930 21 : return cmp(vx, vy);
931 : }
932 : }
933 :
934 : int
935 61987945 : gidentical(GEN x, GEN y)
936 : {
937 : long tx;
938 :
939 61987945 : if (x == y) return 1;
940 58284062 : tx = typ(x); if (typ(y) != tx) return 0;
941 58051726 : switch(tx)
942 : {
943 19066398 : case t_INT:
944 19066398 : return equalii(x,y);
945 :
946 1547 : case t_REAL:
947 1547 : return identicalrr(x,y);
948 :
949 794008 : case t_FRAC: case t_INTMOD:
950 794008 : return equalii(gel(x,2), gel(y,2)) && equalii(gel(x,1), gel(y,1));
951 :
952 343 : case t_COMPLEX:
953 343 : return gidentical(gel(x,2),gel(y,2)) && gidentical(gel(x,1),gel(y,1));
954 14 : case t_PADIC:
955 14 : return valp(x) == valp(y)
956 14 : && equalii(gel(x,2),gel(y,2))
957 14 : && equalii(gel(x,3),gel(y,3))
958 28 : && equalii(gel(x,4),gel(y,4));
959 3843 : case t_POLMOD:
960 3843 : return gidentical(gel(x,2),gel(y,2)) && polidentical(gel(x,1),gel(y,1));
961 3432161 : case t_POL:
962 3432161 : return polidentical(x,y);
963 14 : case t_SER:
964 14 : return seridentical(x,y);
965 3024 : case t_FFELT:
966 3024 : return FF_equal(x,y);
967 :
968 401624 : case t_QFB:
969 401624 : return equalii(gel(x,1),gel(y,1))
970 401617 : && equalii(gel(x,2),gel(y,2))
971 803241 : && equalii(gel(x,3),gel(y,3));
972 :
973 14 : case t_QUAD:
974 14 : return ZX_equal(gel(x,1),gel(y,1))
975 7 : && gidentical(gel(x,2),gel(y,2))
976 21 : && gidentical(gel(x,3),gel(y,3));
977 :
978 7 : case t_RFRAC:
979 7 : return gidentical(gel(x,1),gel(y,1)) && gidentical(gel(x,2),gel(y,2));
980 :
981 70 : case t_STR:
982 70 : return !strcmp(GSTR(x),GSTR(y));
983 5249403 : case t_VEC: case t_COL: case t_MAT:
984 5249403 : return vecidentical(x,y);
985 29099046 : case t_VECSMALL:
986 29099046 : return zv_equal(x,y);
987 28 : case t_CLOSURE:
988 28 : return closure_identical(x,y);
989 161 : case t_LIST:
990 161 : return list_cmp(x, y, gidentical);
991 21 : case t_INFINITY: return gidentical(gel(x,1),gel(y,1));
992 : }
993 0 : return 0;
994 : }
995 : /* x,y t_POL in the same variable */
996 : static int
997 7446899 : polequal(GEN x, GEN y)
998 : {
999 : long lx, ly;
1000 : /* Can't do that: Mod(0,1)*x^0 == x^0
1001 : if (signe(x) != signe(y)) return 0; */
1002 7446899 : lx = lg(x); ly = lg(y);
1003 7446899 : while (lx > ly) if (!gequal0(gel(x,--lx))) return 0;
1004 7443350 : while (ly > lx) if (!gequal0(gel(y,--ly))) return 0;
1005 29586271 : for (lx--; lx >= 2; lx--) if (!gequal(gel(x,lx), gel(y,lx))) return 0;
1006 7371346 : return 1;
1007 : }
1008 :
1009 : /* x,y t_SER in the same variable */
1010 : static int
1011 420 : serequal(GEN x, GEN y)
1012 : {
1013 : long LX, LY, lx, ly, vx, vy;
1014 420 : if (!signe(x) && !signe(y)) return 1;
1015 56 : lx = lg(x); vx = valser(x); LX = lx + vx;
1016 56 : ly = lg(y); vy = valser(y); LY = ly + vy;
1017 56 : if (LX > LY) lx = LY - vx; else ly = LX - vy;
1018 282877 : while (lx >= 3 && ly >= 3)
1019 282821 : if (!gequal(gel(x,--lx), gel(y,--ly))) return 0;
1020 56 : while(--ly >= 2) if (!gequal0(gel(y,ly))) return 0;
1021 84 : while(--lx >= 2) if (!gequal0(gel(x,lx))) return 0;
1022 49 : return 1;
1023 : }
1024 :
1025 : /* typ(x) = typ(y) = t_VEC/COL/MAT */
1026 : static int
1027 5625492 : vecequal(GEN x, GEN y)
1028 : {
1029 : long i;
1030 5625492 : if ((x[0] ^ y[0]) & (TYPBITS|LGBITS)) return 0;
1031 18529859 : for (i = lg(x)-1; i; i--)
1032 16149277 : if (! gequal(gel(x,i),gel(y,i)) ) return 0;
1033 2380582 : return 1;
1034 : }
1035 :
1036 : int
1037 232599691 : gequal(GEN x, GEN y)
1038 : {
1039 : pari_sp av;
1040 : long tx, ty;
1041 : long i;
1042 :
1043 232599691 : if (x == y) return 1;
1044 203009454 : tx = typ(x);
1045 203009454 : ty = typ(y);
1046 203009454 : if (tx == ty)
1047 195214883 : switch(tx)
1048 : {
1049 169355369 : case t_INT:
1050 169355369 : return equalii(x,y);
1051 :
1052 20327 : case t_REAL:
1053 20327 : return equalrr(x,y);
1054 :
1055 6838731 : case t_FRAC: case t_INTMOD:
1056 6838731 : return equalii(gel(x,2), gel(y,2)) && equalii(gel(x,1), gel(y,1));
1057 :
1058 1288 : case t_COMPLEX:
1059 1288 : return gequal(gel(x,2),gel(y,2)) && gequal(gel(x,1),gel(y,1));
1060 763 : case t_PADIC:
1061 763 : if (!equalii(gel(x,2),gel(y,2))) return 0;
1062 763 : av = avma; i = gequal0(gsub(x,y)); set_avma(av);
1063 763 : return i;
1064 3209303 : case t_POLMOD:
1065 3209303 : if (varn(gel(x,1)) != varn(gel(y,1))) break;
1066 3209296 : return gequal(gel(x,2),gel(y,2)) && RgX_equal_var(gel(x,1),gel(y,1));
1067 7455390 : case t_POL:
1068 7455390 : if (varn(x) != varn(y)) break;
1069 7446899 : return polequal(x,y);
1070 420 : case t_SER:
1071 420 : if (varn(x) != varn(y)) break;
1072 420 : return serequal(x,y);
1073 :
1074 56098 : case t_FFELT:
1075 56098 : return FF_equal(x,y);
1076 :
1077 1097034 : case t_QFB:
1078 1097034 : return equalii(gel(x,1),gel(y,1))
1079 247773 : && equalii(gel(x,2),gel(y,2))
1080 1344807 : && equalii(gel(x,3),gel(y,3));
1081 :
1082 7 : case t_QUAD:
1083 7 : return ZX_equal(gel(x,1),gel(y,1))
1084 0 : && gequal(gel(x,2),gel(y,2))
1085 7 : && gequal(gel(x,3),gel(y,3));
1086 :
1087 73717 : case t_RFRAC:
1088 : {
1089 73717 : GEN a = gel(x,1), b = gel(x,2), c = gel(y,1), d = gel(y,2);
1090 73717 : if (gequal(b,d)) return gequal(a,c); /* simple case */
1091 0 : av = avma;
1092 0 : a = simplify_shallow(gmul(a,d));
1093 0 : b = simplify_shallow(gmul(b,c));
1094 0 : return gc_bool(av, gequal(a,b));
1095 : }
1096 :
1097 64925 : case t_STR:
1098 64925 : return !strcmp(GSTR(x),GSTR(y));
1099 5625492 : case t_VEC: case t_COL: case t_MAT:
1100 5625492 : return vecequal(x,y);
1101 1415775 : case t_VECSMALL:
1102 1415775 : return zv_equal(x,y);
1103 182 : case t_LIST:
1104 182 : return list_cmp(x, y, gequal);
1105 42 : case t_CLOSURE:
1106 42 : return closure_identical(x,y);
1107 28 : case t_INFINITY:
1108 28 : return gequal(gel(x,1),gel(y,1));
1109 : }
1110 7803061 : if (is_noncalc_t(tx) || is_noncalc_t(ty)) return 0;
1111 7803073 : if (tx == t_INT && !signe(x)) return gequal0(y);
1112 7799839 : if (ty == t_INT && !signe(y)) return gequal0(x);
1113 3196114 : (void)&av; av = avma; /* emulate volatile */
1114 3196114 : return gc_bool(av, gequal_try(x, y));
1115 : }
1116 :
1117 : int
1118 43988 : gequalsg(long s, GEN x)
1119 43988 : { pari_sp av = avma; return gc_bool(av, gequal(stoi(s), x)); }
1120 :
1121 : /* a and b are t_INT, t_FRAC, t_REAL or t_COMPLEX of those. Check whether
1122 : * a-b is invertible */
1123 : int
1124 53397 : cx_approx_equal(GEN a, GEN b)
1125 : {
1126 53397 : pari_sp av = avma;
1127 : GEN d;
1128 53397 : if (a == b) return 1;
1129 24486 : d = gsub(a,b);
1130 24486 : return gc_bool(av, gequal0(d) || (typ(d)==t_COMPLEX && gequal0(cxnorm(d))));
1131 : }
1132 : static int
1133 1322130 : r_approx0(GEN x, long e) { return e - expo(x) > bit_prec(x); }
1134 : /* x ~ 0 compared to reference y */
1135 : int
1136 1849534 : cx_approx0(GEN x, GEN y)
1137 : {
1138 : GEN a, b;
1139 : long e;
1140 1849534 : switch(typ(x))
1141 : {
1142 469 : case t_COMPLEX:
1143 469 : a = gel(x,1); b = gel(x,2);
1144 469 : if (typ(a) != t_REAL)
1145 : {
1146 14 : if (!gequal0(a)) return 0;
1147 0 : a = NULL;
1148 : }
1149 455 : else if (!signe(a)) a = NULL;
1150 455 : if (typ(b) != t_REAL)
1151 : {
1152 0 : if (!gequal0(b)) return 0;
1153 0 : if (!a) return 1;
1154 0 : b = NULL;
1155 : }
1156 455 : else if (!signe(b))
1157 : {
1158 7 : if (!a) return 1;
1159 7 : b = NULL;
1160 : }
1161 : /* a or b is != NULL iff it is non-zero t_REAL; one of them is */
1162 455 : e = gexpo(y);
1163 455 : return (!a || r_approx0(a, e)) && (!b || r_approx0(b, e));
1164 1321668 : case t_REAL:
1165 1321668 : return !signe(x) || r_approx0(x, gexpo(y));
1166 527397 : default:
1167 527397 : return gequal0(x);
1168 : }
1169 : }
1170 : /*******************************************************************/
1171 : /* */
1172 : /* VALUATION */
1173 : /* p is either a t_INT or a t_POL. */
1174 : /* returns the largest exponent of p dividing x when this makes */
1175 : /* sense : error for types real, integermod and polymod if p does */
1176 : /* not divide the modulus, q-adic if q!=p. */
1177 : /* */
1178 : /*******************************************************************/
1179 :
1180 : static long
1181 336 : minval(GEN x, GEN p)
1182 : {
1183 336 : long i,k, val = LONG_MAX, lx = lg(x);
1184 6538 : for (i=lontyp[typ(x)]; i<lx; i++)
1185 : {
1186 6202 : k = gvaluation(gel(x,i),p);
1187 6202 : if (k < val) val = k;
1188 : }
1189 336 : return val;
1190 : }
1191 :
1192 : static int
1193 91 : intdvd(GEN x, GEN y, GEN *z) { GEN r; *z = dvmdii(x,y,&r); return (r==gen_0); }
1194 :
1195 : /* x t_FRAC, p t_INT, return v_p(x) */
1196 : static long
1197 292597 : frac_val(GEN x, GEN p) {
1198 292597 : long v = Z_pval(gel(x,2),p);
1199 292597 : if (v) return -v;
1200 292454 : return Z_pval(gel(x,1),p);
1201 : }
1202 : long
1203 9293001 : Q_pval(GEN x, GEN p)
1204 : {
1205 9293001 : if (lgefint(p) == 3) return Q_lval(x, uel(p,2));
1206 568 : return (typ(x)==t_INT)? Z_pval(x, p): frac_val(x, p);
1207 : }
1208 :
1209 : static long
1210 390862 : frac_lval(GEN x, ulong p) {
1211 390862 : long v = Z_lval(gel(x,2),p);
1212 390862 : if (v) return -v;
1213 221193 : return Z_lval(gel(x,1),p);
1214 : }
1215 : long
1216 9297180 : Q_lval(GEN x, ulong p){return (typ(x)==t_INT)? Z_lval(x, p): frac_lval(x, p);}
1217 :
1218 : long
1219 6270479 : Q_pvalrem(GEN x, GEN p, GEN *y)
1220 : {
1221 : GEN a, b;
1222 : long v;
1223 6270479 : if (lgefint(p) == 3) return Q_lvalrem(x, uel(p,2), y);
1224 5810 : if (typ(x) == t_INT) return Z_pvalrem(x, p, y);
1225 0 : a = gel(x,1);
1226 0 : b = gel(x,2);
1227 0 : v = Z_pvalrem(b, p, &b);
1228 0 : if (v) { *y = isint1(b)? a: mkfrac(a, b); return -v; }
1229 0 : v = Z_pvalrem(a, p, &a);
1230 0 : *y = mkfrac(a, b); return v;
1231 : }
1232 : long
1233 6268861 : Q_lvalrem(GEN x, ulong p, GEN *y)
1234 : {
1235 : GEN a, b;
1236 : long v;
1237 6268861 : if (typ(x) == t_INT) return Z_lvalrem(x, p, y);
1238 398333 : a = gel(x,1);
1239 398333 : b = gel(x,2);
1240 398333 : v = Z_lvalrem(b, p, &b);
1241 398336 : if (v) { *y = isint1(b)? a: mkfrac(a, b); return -v; }
1242 228910 : v = Z_lvalrem(a, p, &a);
1243 228910 : *y = mkfrac(a, b); return v;
1244 : }
1245 :
1246 : long
1247 1166387 : gvaluation(GEN x, GEN p)
1248 : {
1249 1166387 : long tx = typ(x), tp;
1250 : pari_sp av;
1251 :
1252 1166387 : if (!p)
1253 28 : switch(tx)
1254 : {
1255 7 : case t_PADIC: return valp(x);
1256 7 : case t_POL: return RgX_val(x);
1257 7 : case t_SER: return valser(x);
1258 7 : default: pari_err_TYPE("gvaluation", x);
1259 : }
1260 1166359 : tp = typ(p);
1261 1166359 : switch(tp)
1262 : {
1263 1159359 : case t_INT:
1264 1159359 : if (signe(p) && !is_pm1(p)) break;
1265 28 : pari_err_DOMAIN("gvaluation", "p", "=", p, p);
1266 6993 : case t_POL:
1267 6993 : if (degpol(p) > 0) break;
1268 : default:
1269 7 : pari_err_DOMAIN("gvaluation", "p", "=", p, p);
1270 : }
1271 :
1272 1166324 : switch(tx)
1273 : {
1274 145061 : case t_INT:
1275 145061 : if (!signe(x)) return LONG_MAX;
1276 144956 : if (tp == t_POL) return 0;
1277 144634 : return Z_pval(x,p);
1278 :
1279 49 : case t_REAL:
1280 49 : if (tp == t_POL) return 0;
1281 21 : break;
1282 :
1283 28 : case t_FFELT:
1284 28 : if (tp == t_POL) return FF_equal0(x)? LONG_MAX: 0;
1285 14 : break;
1286 :
1287 105 : case t_INTMOD: {
1288 105 : GEN a = gel(x,1), b = gel(x,2);
1289 : long val;
1290 133 : if (tp == t_POL) return signe(b)? 0: LONG_MAX;
1291 42 : av = avma;
1292 42 : if (!intdvd(a, p, &a)) break;
1293 28 : if (!intdvd(b, p, &b)) return gc_long(av,0);
1294 14 : val = 1; while (intdvd(a,p,&a) && intdvd(b,p,&b)) val++;
1295 14 : return gc_long(av,val);
1296 : }
1297 :
1298 292509 : case t_FRAC:
1299 292509 : if (tp == t_POL) return 0;
1300 292495 : return frac_val(x, p);
1301 :
1302 721817 : case t_PADIC:
1303 721817 : if (tp == t_POL) return 0;
1304 721796 : if (!equalii(p,gel(x,2))) break;
1305 721789 : return valp(x);
1306 :
1307 35 : case t_POLMOD: {
1308 35 : GEN a = gel(x,1), b = gel(x,2);
1309 : long v, val;
1310 35 : if (tp == t_INT) return gvaluation(b,p);
1311 21 : v = varn(p);
1312 21 : if (varn(a) != v) return 0;
1313 21 : av = avma;
1314 21 : a = RgX_divrem(a, p, ONLY_DIVIDES);
1315 21 : if (!a) break;
1316 28 : if (typ(b) != t_POL || varn(b) != v ||
1317 21 : !(b = RgX_divrem(b, p, ONLY_DIVIDES)) ) return gc_long(av,0);
1318 7 : val = 1;
1319 28 : while ((a = RgX_divrem(a, p, ONLY_DIVIDES)) &&
1320 21 : (b = RgX_divrem(b, p, ONLY_DIVIDES)) ) val++;
1321 7 : return gc_long(av,val);
1322 : }
1323 6055 : case t_POL: {
1324 6055 : if (tp == t_POL) {
1325 5866 : long vp = varn(p), vx = varn(x);
1326 5866 : if (vp == vx)
1327 : {
1328 : long val;
1329 5852 : if (RgX_is_monomial(p))
1330 : {
1331 5817 : val = RgX_val(x); if (val == LONG_MAX) return LONG_MAX;
1332 5740 : return val / degpol(p);
1333 : }
1334 35 : if (!signe(x)) return LONG_MAX;
1335 21 : av = avma;
1336 21 : for (val=0; ; val++)
1337 : {
1338 35 : x = RgX_divrem(x,p,ONLY_DIVIDES);
1339 35 : if (!x) return gc_long(av,val);
1340 14 : if (gc_needed(av,1))
1341 : {
1342 0 : if(DEBUGMEM>1) pari_warn(warnmem,"gvaluation");
1343 0 : x = gerepilecopy(av, x);
1344 : }
1345 : }
1346 : }
1347 14 : if (varncmp(vx, vp) > 0) return 0;
1348 : }
1349 196 : return minval(x,p);
1350 : }
1351 :
1352 490 : case t_SER: {
1353 490 : if (tp == t_POL) {
1354 476 : long vp = varn(p), vx = varn(x);
1355 476 : if (vp == vx)
1356 : {
1357 469 : long val = RgX_val(p);
1358 469 : if (!val) pari_err_DOMAIN("gvaluation", "p", "=", p, p);
1359 462 : return (long)(valser(x) / val);
1360 : }
1361 7 : if (varncmp(vx, vp) > 0) return 0;
1362 : }
1363 14 : return minval(x,p);
1364 : }
1365 :
1366 49 : case t_RFRAC:
1367 49 : return gvaluation(gel(x,1),p) - gvaluation(gel(x,2),p);
1368 :
1369 126 : case t_COMPLEX: case t_QUAD: case t_VEC: case t_COL: case t_MAT:
1370 126 : return minval(x,p);
1371 : }
1372 63 : pari_err_OP("valuation", x,p);
1373 : return 0; /* LCOV_EXCL_LINE */
1374 : }
1375 : GEN
1376 3808 : gpvaluation(GEN x, GEN p)
1377 : {
1378 3808 : long v = gvaluation(x,p);
1379 3703 : return v == LONG_MAX? mkoo(): stoi(v);
1380 : }
1381 :
1382 : /* x is nonzero */
1383 : long
1384 77932025 : u_lvalrem(ulong x, ulong p, ulong *py)
1385 : {
1386 : ulong vx;
1387 77932025 : if (p == 2) { vx = vals(x); *py = x >> vx; return vx; }
1388 67944711 : for(vx = 0;;)
1389 : {
1390 118789229 : if (x % p) { *py = x; return vx; }
1391 50844518 : x /= p; /* gcc is smart enough to make a single div */
1392 50844518 : vx++;
1393 : }
1394 : }
1395 : long
1396 65767742 : u_lval(ulong x, ulong p)
1397 : {
1398 : ulong vx;
1399 65767742 : if (p == 2) return vals(x);
1400 62485001 : for(vx = 0;;)
1401 : {
1402 101349381 : if (x % p) return vx;
1403 38864380 : x /= p; /* gcc is smart enough to make a single div */
1404 38864380 : vx++;
1405 : }
1406 : }
1407 :
1408 : long
1409 1822470 : z_lval(long s, ulong p) { return u_lval(labs(s), p); }
1410 : long
1411 87342 : z_lvalrem(long s, ulong p, long *py)
1412 : {
1413 : long v;
1414 87342 : if (s < 0)
1415 : {
1416 0 : ulong u = (ulong)-s;
1417 0 : v = u_lvalrem(u, p, &u);
1418 0 : *py = -(long)u;
1419 : }
1420 : else
1421 : {
1422 87342 : ulong u = (ulong)s;
1423 87342 : v = u_lvalrem(u, p, &u);
1424 87341 : *py = (long)u;
1425 : }
1426 87341 : return v;
1427 : }
1428 : /* assume |p| > 1 */
1429 : long
1430 1317742 : z_pval(long s, GEN p)
1431 : {
1432 1317742 : if (lgefint(p) > 3) return 0;
1433 1317742 : return z_lval(s, uel(p,2));
1434 : }
1435 : /* assume |p| > 1 */
1436 : long
1437 399 : z_pvalrem(long s, GEN p, long *py)
1438 : {
1439 399 : if (lgefint(p) > 3) { *py = s; return 0; }
1440 399 : return z_lvalrem(s, uel(p,2), py);
1441 : }
1442 :
1443 : /* return v_q(x) and set *py = x / q^v_q(x), using divide & conquer */
1444 : static long
1445 2095602 : Z_pvalrem_DC(GEN x, GEN q, GEN *py)
1446 : {
1447 2095602 : GEN r, z = dvmdii(x, q, &r);
1448 : long v;
1449 2095559 : if (r != gen_0) { *py = x; return 0; }
1450 1446630 : if (2 * lgefint(q) <= lgefint(z)+3) /* avoid squaring if pointless */
1451 1430343 : v = Z_pvalrem_DC(z, sqri(q), py) << 1;
1452 : else
1453 16287 : { v = 0; *py = z; }
1454 1446634 : z = dvmdii(*py, q, &r);
1455 1446708 : if (r != gen_0) return v + 1;
1456 602046 : *py = z; return v + 2;
1457 : }
1458 :
1459 : static const long VAL_DC_THRESHOLD = 16;
1460 :
1461 : long
1462 62675705 : Z_lval(GEN x, ulong p)
1463 : {
1464 : long vx;
1465 : pari_sp av;
1466 62675705 : if (p == 2) return vali(x);
1467 48365977 : if (lgefint(x) == 3) return u_lval(uel(x,2), p);
1468 2155867 : av = avma;
1469 2155867 : for(vx = 0;;)
1470 10398845 : {
1471 : ulong r;
1472 12554712 : GEN q = absdiviu_rem(x, p, &r);
1473 12555388 : if (r) break;
1474 10574892 : vx++; x = q;
1475 10574892 : if (vx == VAL_DC_THRESHOLD) {
1476 176047 : if (p == 1) pari_err_DOMAIN("Z_lval", "p", "=", gen_1, gen_1);
1477 176047 : vx += Z_pvalrem_DC(x, sqru(p), &x) << 1;
1478 176047 : q = absdiviu_rem(x, p, &r); if (!r) vx++;
1479 176047 : break;
1480 : }
1481 : }
1482 2156543 : return gc_long(av,vx);
1483 : }
1484 : long
1485 59486385 : Z_lvalrem(GEN x, ulong p, GEN *py)
1486 : {
1487 : long vx, sx;
1488 : pari_sp av;
1489 59486385 : if (p == 2) { vx = vali(x); *py = shifti(x, -vx); return vx; }
1490 46062223 : if (lgefint(x) == 3) {
1491 : ulong u;
1492 40011563 : vx = u_lvalrem(uel(x,2), p, &u);
1493 40011178 : *py = signe(x) < 0? utoineg(u): utoipos(u);
1494 40010327 : return vx;
1495 : }
1496 6050660 : av = avma; (void)new_chunk(lgefint(x));
1497 6050965 : sx = signe(x);
1498 6050965 : for(vx = 0;;)
1499 16389704 : {
1500 : ulong r;
1501 22440669 : GEN q = absdiviu_rem(x, p, &r);
1502 22440647 : if (r) break;
1503 16878348 : vx++; x = q;
1504 16878348 : if (vx == VAL_DC_THRESHOLD) {
1505 488644 : if (p == 1) pari_err_DOMAIN("Z_lvalrem", "p", "=", gen_1, gen_1);
1506 488644 : vx += Z_pvalrem_DC(x, sqru(p), &x) << 1;
1507 488651 : q = absdiviu_rem(x, p, &r); if (!r) { vx++; x = q; }
1508 488650 : break;
1509 : }
1510 : }
1511 6050949 : set_avma(av); *py = icopy(x); setsigne(*py, sx); return vx;
1512 : }
1513 :
1514 : /* Is |q| <= p ? */
1515 : static int
1516 22167665 : isless_iu(GEN q, ulong p) {
1517 22167665 : long l = lgefint(q);
1518 22167665 : return l==2 || (l == 3 && uel(q,2) <= p);
1519 : }
1520 :
1521 : long
1522 248098354 : u_lvalrem_stop(ulong *n, ulong p, int *stop)
1523 : {
1524 248098354 : ulong N = *n, q = N / p, r = N % p; /* gcc makes a single div */
1525 248098354 : long v = 0;
1526 248098354 : if (!r)
1527 : {
1528 29348171 : do { v++; N = q; q = N / p; r = N % p; } while (!r);
1529 20623342 : *n = N;
1530 : }
1531 248098354 : *stop = q <= p; return v;
1532 : }
1533 : /* Assume n > 0. Return v_p(n), set *n := n/p^v_p(n). Set 'stop' if now
1534 : * n < p^2 [implies n prime if no prime < p divides n] */
1535 : long
1536 156207627 : Z_lvalrem_stop(GEN *n, ulong p, int *stop)
1537 : {
1538 : pari_sp av;
1539 : long v;
1540 : ulong r;
1541 : GEN N, q;
1542 :
1543 156207627 : if (lgefint(*n) == 3)
1544 : {
1545 134040671 : r = (*n)[2];
1546 134040671 : v = u_lvalrem_stop(&r, p, stop);
1547 134044005 : if (v) *n = utoipos(r);
1548 134046761 : return v;
1549 : }
1550 22166956 : av = avma; v = 0; q = absdiviu_rem(*n, p, &r);
1551 22167677 : if (r) set_avma(av);
1552 : else
1553 : {
1554 : do {
1555 263520 : v++; N = q;
1556 263520 : if (v == VAL_DC_THRESHOLD)
1557 : {
1558 607 : v += Z_pvalrem_DC(N,sqru(p),&N) << 1;
1559 607 : q = absdiviu_rem(N, p, &r); if (!r) { v++; N = q; }
1560 607 : break;
1561 : }
1562 262913 : q = absdiviu_rem(N, p, &r);
1563 262913 : } while (!r);
1564 222468 : *n = N;
1565 : }
1566 22167676 : *stop = isless_iu(q,p); return v;
1567 : }
1568 :
1569 : /* x is a nonzero integer, |p| > 1 */
1570 : long
1571 49638633 : Z_pvalrem(GEN x, GEN p, GEN *py)
1572 : {
1573 : long vx;
1574 : pari_sp av;
1575 :
1576 49638633 : if (lgefint(p) == 3) return Z_lvalrem(x, uel(p,2), py);
1577 25626 : if (lgefint(x) == 3) { *py = icopy(x); return 0; }
1578 8338 : av = avma; vx = 0; (void)new_chunk(lgefint(x));
1579 : for(;;)
1580 14222 : {
1581 22626 : GEN r, q = dvmdii(x,p,&r);
1582 22626 : if (r != gen_0) { set_avma(av); *py = icopy(x); return vx; }
1583 14222 : vx++; x = q;
1584 : }
1585 : }
1586 : long
1587 2593734 : u_pvalrem(ulong x, GEN p, ulong *py)
1588 : {
1589 2593734 : if (lgefint(p) == 3) return u_lvalrem(x, uel(p,2), py);
1590 555 : *py = x; return 0;
1591 : }
1592 : long
1593 136948 : u_pval(ulong x, GEN p)
1594 : {
1595 136948 : if (lgefint(p) == 3) return u_lval(x, uel(p,2));
1596 0 : return 0;
1597 : }
1598 : long
1599 47068987 : Z_pval(GEN x, GEN p) {
1600 : long vx;
1601 : pari_sp av;
1602 :
1603 47068987 : if (lgefint(p) == 3) return Z_lval(x, uel(p,2));
1604 33024 : if (lgefint(x) == 3) return 0;
1605 8012 : av = avma; vx = 0;
1606 : for(;;)
1607 25479 : {
1608 33491 : GEN r, q = dvmdii(x,p,&r);
1609 33494 : if (r != gen_0) return gc_long(av,vx);
1610 25479 : vx++; x = q;
1611 : }
1612 : }
1613 :
1614 : /* return v_p(n!) = [n/p] + [n/p^2] + ... */
1615 : long
1616 1982927 : factorial_lval(ulong n, ulong p)
1617 : {
1618 : ulong q, v;
1619 1982927 : if (p == 2) return n - hammingl(n);
1620 1314882 : q = p; v = 0;
1621 1443406 : do { v += n/q; q *= p; } while (n >= q);
1622 1314882 : return (long)v;
1623 : }
1624 :
1625 : /********** Same for "containers" ZX / ZV / ZC **********/
1626 :
1627 : /* If the t_INT q divides the ZX/ZV x, return the quotient. Otherwise NULL.
1628 : * Stack clean; assumes lg(x) > 1 */
1629 : static GEN
1630 6574 : gen_Z_divides(GEN x, GEN q, long imin)
1631 : {
1632 : long i, l;
1633 6574 : GEN y = cgetg_copy(x, &l);
1634 :
1635 6574 : y[1] = x[1]; /* Needed for ZX; no-op if ZV, overwritten in first iteration */
1636 88538 : for (i = imin; i < l; i++)
1637 : {
1638 85954 : GEN r, xi = gel(x,i);
1639 85954 : if (!signe(xi)) { gel(y,i) = xi; continue; }
1640 55384 : gel(y,i) = dvmdii(xi, q, &r);
1641 55384 : if (r != gen_0) { set_avma((pari_sp)(y+l)); return NULL; }
1642 : }
1643 2584 : return y;
1644 : }
1645 : /* If q divides the ZX/ZV x, return the quotient. Otherwise NULL.
1646 : * Stack clean; assumes lg(x) > 1 */
1647 : static GEN
1648 4879 : gen_z_divides(GEN x, ulong q, long imin)
1649 : {
1650 : long i, l;
1651 4879 : GEN y = cgetg_copy(x, &l);
1652 :
1653 4879 : y[1] = x[1]; /* Needed for ZX; no-op if ZV, overwritten in first iteration */
1654 43224 : for (i = imin; i < l; i++)
1655 : {
1656 : ulong r;
1657 41923 : GEN xi = gel(x,i);
1658 41923 : if (!signe(xi)) { gel(y,i) = xi; continue; }
1659 28609 : gel(y,i) = absdiviu_rem(xi, q, &r);
1660 28609 : if (r) { set_avma((pari_sp)(y+l)); return NULL; }
1661 25031 : affectsign_safe(xi, &gel(y,i));
1662 : }
1663 1301 : return y;
1664 : }
1665 :
1666 : /* return v_q(x) and set *py = x / q^v_q(x), using divide & conquer */
1667 : static long
1668 11416 : gen_pvalrem_DC(GEN x, GEN q, GEN *py, long imin)
1669 : {
1670 :
1671 11416 : pari_sp av = avma;
1672 11416 : long v, i, l, lz = LONG_MAX;
1673 11416 : GEN y = cgetg_copy(x, &l);
1674 :
1675 11416 : y[1] = x[1];
1676 134510 : for (i = imin; i < l; i++)
1677 : {
1678 127936 : GEN r, xi = gel(x,i);
1679 127936 : if (!signe(xi)) { gel(y,i) = xi; continue; }
1680 86124 : gel(y,i) = dvmdii(xi, q, &r);
1681 86124 : if (r != gen_0) { *py = x; return gc_long(av,0); }
1682 81282 : lz = minss(lz, lgefint(gel(y,i)));
1683 : }
1684 6574 : if (2 * lgefint(q) <= lz+3) /* avoid squaring if pointless */
1685 6523 : v = gen_pvalrem_DC(y, sqri(q), py, imin) << 1;
1686 : else
1687 51 : { v = 0; *py = y; }
1688 :
1689 6574 : y = gen_Z_divides(*py, q, imin);
1690 6574 : if (!y) return v+1;
1691 2584 : *py = y; return v+2;
1692 : }
1693 :
1694 : static long
1695 772514 : gen_2val(GEN x, long imin)
1696 : {
1697 772514 : long i, lx = lg(x), v = LONG_MAX;
1698 2917528 : for (i = imin; i < lx; i++)
1699 : {
1700 2481624 : GEN c = gel(x,i);
1701 : long w;
1702 2481624 : if (!signe(c)) continue;
1703 2269095 : w = vali(c);
1704 2269095 : if (w < v) { v = w; if (!v) break; }
1705 : }
1706 772514 : return v;
1707 : }
1708 : static long
1709 1278455 : gen_lval(GEN x, ulong p, long imin)
1710 : {
1711 : long i, lx, v;
1712 : pari_sp av;
1713 : GEN y;
1714 1278455 : if (p == 2) return gen_2val(x, imin);
1715 505941 : av = avma;
1716 505941 : lx = lg(x); y = leafcopy(x);
1717 739074 : for(v = 0;; v++)
1718 2294197 : for (i = imin; i < lx; i++)
1719 : {
1720 : ulong r;
1721 2061064 : gel(y,i) = absdiviu_rem(gel(y,i), p, &r);
1722 2061064 : if (r) return gc_long(av,v);
1723 : }
1724 : }
1725 : long
1726 746096 : ZX_lval(GEN x, ulong p) { return gen_lval(x, p, 2); }
1727 : long
1728 0 : ZV_lval(GEN x, ulong p) { return gen_lval(x, p, 1); }
1729 :
1730 : long
1731 28931 : zx_lval(GEN f, long p)
1732 : {
1733 28931 : long i, l = lg(f), x = LONG_MAX;
1734 30219 : for(i=2; i<l; i++)
1735 : {
1736 : long y;
1737 29477 : if (f[i] == 0) continue;
1738 29428 : y = z_lval(f[i], p);
1739 29428 : if (y < x) { x = y; if (x == 0) return x; }
1740 : }
1741 742 : return x;
1742 : }
1743 :
1744 : static long
1745 542709 : gen_pval(GEN x, GEN p, long imin)
1746 : {
1747 : long i, lx, v;
1748 : pari_sp av;
1749 : GEN y;
1750 542709 : if (lgefint(p) == 3) return gen_lval(x, p[2], imin);
1751 10350 : av = avma;
1752 10350 : lx = lg(x); y = leafcopy(x);
1753 10350 : for(v = 0;; v++)
1754 : {
1755 10350 : if (v == VAL_DC_THRESHOLD)
1756 : {
1757 0 : if (is_pm1(p)) pari_err_DOMAIN("gen_pval", "p", "=", p, p);
1758 0 : v += gen_pvalrem_DC(y, p, &y, imin);
1759 0 : return gc_long(av,v);
1760 : }
1761 :
1762 10350 : for (i = imin; i < lx; i++)
1763 : {
1764 10350 : GEN r; gel(y,i) = dvmdii(gel(y,i), p, &r);
1765 10350 : if (r != gen_0) return gc_long(av,v);
1766 : }
1767 : }
1768 : }
1769 : long
1770 510180 : ZX_pval(GEN x, GEN p) { return gen_pval(x, p, 2); }
1771 : long
1772 32529 : ZV_pval(GEN x, GEN p) { return gen_pval(x, p, 1); }
1773 : /* v = 0 (mod p) */
1774 : int
1775 539 : ZV_Z_dvd(GEN v, GEN p)
1776 : {
1777 539 : pari_sp av = avma;
1778 539 : long i, l = lg(v);
1779 1729 : for (i=1; i<l; i++)
1780 1239 : if (!dvdii(gel(v,i), p)) return gc_long(av,0);
1781 490 : return gc_long(av,1);
1782 : }
1783 :
1784 : static long
1785 4759319 : gen_2valrem(GEN x, GEN *px, long imin)
1786 : {
1787 4759319 : long i, lx = lg(x), v = LONG_MAX;
1788 : GEN z;
1789 13715510 : for (i = imin; i < lx; i++)
1790 : {
1791 12406051 : GEN c = gel(x,i);
1792 : long w;
1793 12406051 : if (!signe(c)) continue;
1794 11520368 : w = vali(c);
1795 11520554 : if (w < v) {
1796 6835509 : v = w;
1797 6835509 : if (!v) { *px = x; return 0; } /* early abort */
1798 : }
1799 : }
1800 1309459 : z = cgetg_copy(x, &lx); z[1] = x[1];
1801 8401485 : for (i=imin; i<lx; i++) gel(z,i) = shifti(gel(x,i), -v);
1802 1309195 : *px = z; return v;
1803 : }
1804 : static long
1805 7737843 : gen_lvalrem(GEN x, ulong p, GEN *px, long imin)
1806 : {
1807 : long i, lx, v;
1808 : GEN y;
1809 7737843 : if (p == 2) return gen_2valrem(x, px, imin);
1810 2978602 : y = cgetg_copy(x, &lx);
1811 2978819 : y[1] = x[1];
1812 2978819 : x = leafcopy(x);
1813 2978382 : for(v = 0;; v++)
1814 : {
1815 4285248 : if (v == VAL_DC_THRESHOLD)
1816 : {
1817 4879 : if (p == 1) pari_err_DOMAIN("gen_lvalrem", "p", "=", gen_1, gen_1);
1818 4879 : v += gen_pvalrem_DC(x, sqru(p), px, imin) << 1;
1819 4879 : x = gen_z_divides(*px, p, imin);
1820 4879 : if (x) { *px = x; v++; }
1821 4879 : return v;
1822 : }
1823 :
1824 14211015 : for (i = imin; i < lx; i++)
1825 : {
1826 12904149 : ulong r; gel(y,i) = absdiviu_rem(gel(x,i), p, &r);
1827 12903984 : if (r) { *px = x; return v; }
1828 9930087 : affectsign_safe(gel(x,i), &gel(y,i));
1829 : }
1830 1306866 : swap(x, y);
1831 : }
1832 : }
1833 : long
1834 721 : ZX_lvalrem(GEN x, ulong p, GEN *px) { return gen_lvalrem(x,p,px, 2); }
1835 : long
1836 0 : ZV_lvalrem(GEN x, ulong p, GEN *px) { return gen_lvalrem(x,p,px, 1); }
1837 :
1838 : static long
1839 7749931 : gen_pvalrem(GEN x, GEN p, GEN *px, long imin)
1840 : {
1841 : long i, lx, v;
1842 : GEN y;
1843 7749931 : if (lgefint(p) == 3) return gen_lvalrem(x, p[2], px, imin);
1844 12859 : y = cgetg_copy(x, &lx);
1845 12863 : y[1] = x[1];
1846 12863 : x = leafcopy(x);
1847 12863 : for(v = 0;; v++)
1848 : {
1849 13652 : if (v == VAL_DC_THRESHOLD)
1850 : {
1851 14 : if (is_pm1(p)) pari_err_DOMAIN("gen_pvalrem", "p", "=", p, p);
1852 14 : return v + gen_pvalrem_DC(x, p, px, imin);
1853 : }
1854 :
1855 22302 : for (i = imin; i < lx; i++)
1856 : {
1857 21513 : GEN r; gel(y,i) = dvmdii(gel(x,i), p, &r);
1858 21513 : if (r != gen_0) { *px = x; return v; }
1859 : }
1860 789 : swap(x, y);
1861 : }
1862 : }
1863 : long
1864 3820842 : ZX_pvalrem(GEN x, GEN p, GEN *px) { return gen_pvalrem(x,p,px, 2); }
1865 : long
1866 3929086 : ZV_pvalrem(GEN x, GEN p, GEN *px) { return gen_pvalrem(x,p,px, 1); }
1867 :
1868 : /*******************************************************************/
1869 : /* */
1870 : /* NEGATION: Create -x */
1871 : /* */
1872 : /*******************************************************************/
1873 :
1874 : GEN
1875 454782771 : gneg(GEN x)
1876 : {
1877 : long lx, i;
1878 : GEN y;
1879 :
1880 454782771 : switch(typ(x))
1881 : {
1882 129413123 : case t_INT:
1883 129413123 : return signe(x)? negi(x): gen_0;
1884 240559554 : case t_REAL:
1885 240559554 : return mpneg(x);
1886 :
1887 222151 : case t_INTMOD: y=cgetg(3,t_INTMOD);
1888 222151 : gel(y,1) = icopy(gel(x,1));
1889 222151 : gel(y,2) = signe(gel(x,2))? subii(gel(y,1),gel(x,2)): gen_0;
1890 222151 : break;
1891 :
1892 2610114 : case t_FRAC:
1893 2610114 : y = cgetg(3, t_FRAC);
1894 2610114 : gel(y,1) = negi(gel(x,1));
1895 2610114 : gel(y,2) = icopy(gel(x,2)); break;
1896 :
1897 75601894 : case t_COMPLEX:
1898 75601894 : y=cgetg(3, t_COMPLEX);
1899 75603355 : gel(y,1) = gneg(gel(x,1));
1900 75605220 : gel(y,2) = gneg(gel(x,2));
1901 75605113 : break;
1902 :
1903 246920 : case t_POLMOD:
1904 246920 : retmkpolmod(gneg(gel(x,2)), RgX_copy(gel(x,1)));
1905 :
1906 153363 : case t_RFRAC:
1907 153363 : y = cgetg(3, t_RFRAC);
1908 153363 : gel(y,1) = gneg(gel(x,1));
1909 153363 : gel(y,2) = RgX_copy(gel(x,2)); break;
1910 :
1911 650288 : case t_PADIC:
1912 650288 : if (!signe(gel(x,4))) return gcopy(x);
1913 646669 : y = cgetg(5, t_PADIC);
1914 646669 : y[1] = x[1];
1915 646669 : gel(y,2) = icopy(gel(x,2));
1916 646669 : gel(y,3) = icopy(gel(x,3));
1917 646669 : gel(y,4) = subii(gel(x,3),gel(x,4));
1918 646669 : break;
1919 :
1920 133 : case t_QUAD:
1921 133 : y=cgetg(4,t_QUAD);
1922 133 : gel(y,1) = ZX_copy(gel(x,1));
1923 133 : gel(y,2) = gneg(gel(x,2));
1924 133 : gel(y,3) = gneg(gel(x,3)); break;
1925 :
1926 80424 : case t_FFELT: return FF_neg(x);
1927 4891920 : case t_POL: return RgX_neg(x);
1928 1379 : case t_SER:
1929 1379 : y = cgetg_copy(x, &lx); y[1] = x[1];
1930 16688 : for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
1931 1379 : break;
1932 1533 : case t_VEC: return RgV_neg(x);
1933 462214 : case t_COL: return RgC_neg(x);
1934 315 : case t_MAT: return RgM_neg(x);
1935 777 : case t_INFINITY: return inf_get_sign(x) == 1? mkmoo(): mkoo();
1936 0 : default:
1937 0 : pari_err_TYPE("gneg",x);
1938 : return NULL; /* LCOV_EXCL_LINE */
1939 : }
1940 79238874 : return y;
1941 : }
1942 :
1943 : GEN
1944 129126479 : gneg_i(GEN x)
1945 : {
1946 : long lx, i;
1947 : GEN y;
1948 :
1949 129126479 : switch(typ(x))
1950 : {
1951 66042377 : case t_INT:
1952 66042377 : return signe(x)? negi(x): gen_0;
1953 29616065 : case t_REAL:
1954 29616065 : return mpneg(x);
1955 :
1956 717747 : case t_INTMOD: y=cgetg(3,t_INTMOD);
1957 717747 : gel(y,1) = gel(x,1);
1958 717747 : gel(y,2) = signe(gel(x,2))? subii(gel(y,1),gel(x,2)): gen_0;
1959 717747 : break;
1960 :
1961 5126657 : case t_FRAC:
1962 5126657 : y = cgetg(3, t_FRAC);
1963 5126656 : gel(y,1) = negi(gel(x,1));
1964 5126656 : gel(y,2) = gel(x,2); break;
1965 :
1966 10678880 : case t_COMPLEX:
1967 10678880 : y = cgetg(3, t_COMPLEX);
1968 10679006 : gel(y,1) = gneg_i(gel(x,1));
1969 10679055 : gel(y,2) = gneg_i(gel(x,2)); break;
1970 :
1971 2015113 : case t_PADIC: y = cgetg(5,t_PADIC);
1972 2015113 : y[1] = x[1];
1973 2015113 : gel(y,2) = gel(x,2);
1974 2015113 : gel(y,3) = gel(x,3);
1975 2015113 : gel(y,4) = signe(gel(x,4))? subii(gel(x,3),gel(x,4)): gen_0; break;
1976 :
1977 143196 : case t_POLMOD:
1978 143196 : retmkpolmod(gneg_i(gel(x,2)), RgX_copy(gel(x,1)));
1979 :
1980 84504 : case t_FFELT: return FF_neg_i(x);
1981 :
1982 672 : case t_QUAD: y=cgetg(4,t_QUAD);
1983 672 : gel(y,1) = gel(x,1);
1984 672 : gel(y,2) = gneg_i(gel(x,2));
1985 672 : gel(y,3) = gneg_i(gel(x,3)); break;
1986 :
1987 2478 : case t_VEC: case t_COL: case t_MAT:
1988 2478 : y = cgetg_copy(x, &lx);
1989 13594 : for (i=1; i<lx; i++) gel(y,i) = gneg_i(gel(x,i));
1990 2478 : break;
1991 :
1992 9660893 : case t_POL: case t_SER:
1993 9660893 : y = cgetg_copy(x, &lx); y[1]=x[1];
1994 40349435 : for (i=2; i<lx; i++) gel(y,i) = gneg_i(gel(x,i));
1995 9660744 : break;
1996 :
1997 5044843 : case t_RFRAC:
1998 5044843 : y = cgetg(3, t_RFRAC);
1999 5044843 : gel(y,1) = gneg_i(gel(x,1));
2000 5044902 : gel(y,2) = gel(x,2); break;
2001 :
2002 0 : default:
2003 0 : pari_err_TYPE("gneg_i",x);
2004 : return NULL; /* LCOV_EXCL_LINE */
2005 : }
2006 33247369 : return y;
2007 : }
2008 :
2009 : /******************************************************************/
2010 : /* */
2011 : /* ABSOLUTE VALUE */
2012 : /* Create abs(x) if x is integer, real, fraction or complex. */
2013 : /* Error otherwise. */
2014 : /* */
2015 : /******************************************************************/
2016 : static int
2017 0 : is_negative(GEN x) {
2018 0 : switch(typ(x))
2019 : {
2020 0 : case t_INT: case t_REAL:
2021 0 : return (signe(x) < 0);
2022 0 : case t_FRAC:
2023 0 : return (signe(gel(x,1)) < 0);
2024 : }
2025 0 : return 0;
2026 : }
2027 :
2028 : GEN
2029 50286715 : gabs(GEN x, long prec)
2030 : {
2031 : long lx;
2032 : pari_sp av;
2033 : GEN y, N;
2034 :
2035 50286715 : switch(typ(x))
2036 : {
2037 32334619 : case t_INT: case t_REAL:
2038 32334619 : return mpabs(x);
2039 :
2040 12811 : case t_FRAC:
2041 12811 : return absfrac(x);
2042 :
2043 17842261 : case t_COMPLEX:
2044 17842261 : av=avma; N=cxnorm(x);
2045 17822702 : switch(typ(N))
2046 : {
2047 266 : case t_INT:
2048 266 : if (!Z_issquareall(N, &y)) break;
2049 105 : return gerepileupto(av, y);
2050 21735 : case t_FRAC: {
2051 : GEN a,b;
2052 36036 : if (!Z_issquareall(gel(N,1), &a)) break;
2053 14301 : if (!Z_issquareall(gel(N,2), &b)) break;
2054 0 : return gerepileupto(av, gdiv(a,b));
2055 : }
2056 : }
2057 17822597 : return gerepileupto(av, gsqrt(N,prec));
2058 :
2059 21 : case t_QUAD:
2060 21 : av = avma;
2061 21 : return gerepileuptoleaf(av, gabs(quadtofp(x, prec), prec));
2062 :
2063 0 : case t_POL:
2064 0 : lx = lg(x); if (lx<=2) return RgX_copy(x);
2065 0 : return is_negative(gel(x,lx-1))? RgX_neg(x): RgX_copy(x);
2066 :
2067 7 : case t_SER:
2068 7 : if (!signe(x)) pari_err_DOMAIN("abs", "argument", "=", gen_0, x);
2069 7 : if (valser(x)) pari_err_DOMAIN("abs", "series valuation", "!=", gen_0, x);
2070 0 : return is_negative(gel(x,2))? gneg(x): gcopy(x);
2071 :
2072 102859 : case t_VEC: case t_COL: case t_MAT:
2073 616926 : pari_APPLY_same(gabs(gel(x,i),prec));
2074 :
2075 14 : case t_INFINITY:
2076 14 : return mkoo();
2077 : }
2078 0 : pari_err_TYPE("gabs",x);
2079 : return NULL; /* LCOV_EXCL_LINE */
2080 : }
2081 :
2082 : GEN
2083 79268 : gmax(GEN x, GEN y) { return gcopy(gmax_shallow(x,y)); }
2084 : GEN
2085 0 : gmaxgs(GEN x, long s) { return (gcmpsg(s,x)>=0)? stoi(s): gcopy(x); }
2086 :
2087 : GEN
2088 12180 : gmin(GEN x, GEN y) { return gcopy(gmin_shallow(x,y)); }
2089 : GEN
2090 0 : gmings(GEN x, long s) { return (gcmpsg(s,x)>0)? gcopy(x): stoi(s); }
2091 :
2092 : long
2093 503132 : vecindexmax(GEN x)
2094 : {
2095 503132 : long lx = lg(x), i0, i;
2096 : GEN s;
2097 :
2098 503132 : if (lx==1) pari_err_DOMAIN("vecindexmax", "empty argument", "=", x,x);
2099 503132 : switch(typ(x))
2100 : {
2101 503132 : case t_VEC: case t_COL:
2102 503132 : s = gel(x,i0=1);
2103 1502042 : for (i=2; i<lx; i++)
2104 998910 : if (gcmp(gel(x,i),s) > 0) s = gel(x,i0=i);
2105 503132 : return i0;
2106 0 : case t_VECSMALL:
2107 0 : return vecsmall_indexmax(x);
2108 0 : default: pari_err_TYPE("vecindexmax",x);
2109 : }
2110 : /* LCOV_EXCL_LINE */
2111 0 : return 0;
2112 : }
2113 : long
2114 181317 : vecindexmin(GEN x)
2115 : {
2116 181317 : long lx = lg(x), i0, i;
2117 : GEN s;
2118 :
2119 181317 : if (lx==1) pari_err_DOMAIN("vecindexmin", "empty argument", "=", x,x);
2120 181317 : switch(typ(x))
2121 : {
2122 181317 : case t_VEC: case t_COL:
2123 181317 : s = gel(x,i0=1);
2124 943613 : for (i=2; i<lx; i++)
2125 762296 : if (gcmp(gel(x,i),s) < 0) s = gel(x,i0=i);
2126 181317 : return i0;
2127 0 : case t_VECSMALL:
2128 0 : return vecsmall_indexmin(x);
2129 0 : default: pari_err_TYPE("vecindexmin",x);
2130 : }
2131 : /* LCOV_EXCL_LINE */
2132 0 : return 0;
2133 : }
2134 :
2135 : GEN
2136 226720 : vecmax0(GEN x, GEN *pi)
2137 : {
2138 226720 : long i, lx = lg(x), tx = typ(x);
2139 226720 : if (!is_matvec_t(tx) && tx != t_VECSMALL
2140 49 : && (tx != t_LIST || list_typ(x) != t_LIST_RAW)) return gcopy(x);
2141 226699 : if (tx == t_LIST)
2142 28 : { if (list_data(x)) { x = list_data(x); lx = lg(x); } else lx = 1; }
2143 226699 : if (lx==1) pari_err_DOMAIN("vecmax", "empty argument", "=", x,x);
2144 226657 : switch(typ(x))
2145 : {
2146 226166 : case t_VEC: case t_COL:
2147 226166 : i = vecindexmax(x); if (pi) *pi = utoipos(i);
2148 226166 : return gcopy(gel(x,i));
2149 470 : case t_MAT: {
2150 470 : long j, i0 = 1, j0 = 1, lx2 = lgcols(x);
2151 : GEN s;
2152 470 : if (lx2 == 1) pari_err_DOMAIN("vecmax", "empty argument", "=", x,x);
2153 463 : s = gcoeff(x,i0,j0); i = 2;
2154 1779 : for (j=1; j<lx; j++,i=1)
2155 : {
2156 1316 : GEN c = gel(x,j);
2157 24763 : for (; i<lx2; i++)
2158 23447 : if (gcmp(gel(c,i),s) > 0) { s = gel(c,i); j0=j; i0=i; }
2159 : }
2160 463 : if (pi) *pi = mkvec2(utoipos(i0), utoipos(j0));
2161 463 : return gcopy(s);
2162 : }
2163 21 : case t_VECSMALL:
2164 21 : i = vecsmall_indexmax(x); if (pi) *pi = utoipos(i);
2165 21 : return stoi(x[i]);
2166 : }
2167 : return NULL;/*LCOV_EXCL_LINE*/
2168 : }
2169 : GEN
2170 146667 : vecmin0(GEN x, GEN *pi)
2171 : {
2172 146667 : long i, lx = lg(x), tx = typ(x);
2173 146667 : if (!is_matvec_t(tx) && tx != t_VECSMALL
2174 49 : && (tx != t_LIST || list_typ(x) != t_LIST_RAW)) return gcopy(x);
2175 146646 : if (tx == t_LIST)
2176 28 : { if (list_data(x)) { x = list_data(x); lx = lg(x); } else lx = 1; }
2177 146646 : if (lx==1) pari_err_DOMAIN("vecmin", "empty argument", "=", x,x);
2178 146611 : switch(typ(x))
2179 : {
2180 146569 : case t_VEC: case t_COL:
2181 146569 : i = vecindexmin(x); if (pi) *pi = utoipos(i);
2182 146569 : return gcopy(gel(x,i));
2183 21 : case t_MAT: {
2184 21 : long j, i0 = 1, j0 = 1, lx2 = lgcols(x);
2185 : GEN s;
2186 21 : if (lx2 == 1) pari_err_DOMAIN("vecmin", "empty argument", "=", x,x);
2187 21 : s = gcoeff(x,i0,j0); i = 2;
2188 63 : for (j=1; j<lx; j++,i=1)
2189 : {
2190 42 : GEN c = gel(x,j);
2191 105 : for (; i<lx2; i++)
2192 63 : if (gcmp(gel(c,i),s) < 0) { s = gel(c,i); j0=j; i0=i; }
2193 : }
2194 21 : if (pi) *pi = mkvec2(utoipos(i0), utoipos(j0));
2195 21 : return gcopy(s);
2196 : }
2197 21 : case t_VECSMALL:
2198 21 : i = vecsmall_indexmin(x); if (pi) *pi = utoipos(i);
2199 21 : return stoi(x[i]);
2200 : }
2201 : return NULL;/*LCOV_EXCL_LINE*/
2202 : }
2203 :
2204 : GEN
2205 66121 : vecmax(GEN x) { return vecmax0(x, NULL); }
2206 : GEN
2207 34499 : vecmin(GEN x) { return vecmin0(x, NULL); }
2208 :
2209 : /*******************************************************************/
2210 : /* */
2211 : /* AFFECT long --> GEN */
2212 : /* affect long s to GEN x. Useful for initialization. */
2213 : /* */
2214 : /*******************************************************************/
2215 :
2216 : static void
2217 0 : padicaff0(GEN x)
2218 : {
2219 0 : if (signe(gel(x,4)))
2220 : {
2221 0 : x[1] = evalvalp(valp(x)+precp(x));
2222 0 : affsi(0,gel(x,4));
2223 : }
2224 0 : }
2225 :
2226 : void
2227 91826 : gaffsg(long s, GEN x)
2228 : {
2229 91826 : switch(typ(x))
2230 : {
2231 90916 : case t_INT: affsi(s,x); break;
2232 910 : case t_REAL: affsr(s,x); break;
2233 0 : case t_INTMOD: modsiz(s,gel(x,1),gel(x,2)); break;
2234 0 : case t_FRAC: affsi(s,gel(x,1)); affsi(1,gel(x,2)); break;
2235 0 : case t_COMPLEX: gaffsg(s,gel(x,1)); gaffsg(0,gel(x,2)); break;
2236 0 : case t_PADIC: {
2237 : long vx;
2238 : GEN y;
2239 0 : if (!s) { padicaff0(x); break; }
2240 0 : vx = Z_pvalrem(stoi(s), gel(x,2), &y);
2241 0 : setvalp(x,vx); modiiz(y,gel(x,3),gel(x,4));
2242 0 : break;
2243 : }
2244 0 : case t_QUAD: gaffsg(s,gel(x,2)); gaffsg(0,gel(x,3)); break;
2245 0 : default: pari_err_TYPE2("=",stoi(s),x);
2246 : }
2247 91826 : }
2248 :
2249 : /*******************************************************************/
2250 : /* */
2251 : /* GENERIC AFFECTATION */
2252 : /* Affect the content of x to y, whenever possible */
2253 : /* */
2254 : /*******************************************************************/
2255 : /* x PADIC, Y INT, return lift(x * Mod(1,Y)) */
2256 : GEN
2257 4466 : padic_to_Fp(GEN x, GEN Y) {
2258 4466 : pari_sp av = avma;
2259 4466 : GEN p = gel(x,2), z;
2260 4466 : long vy, vx = valp(x);
2261 4466 : if (!signe(Y)) pari_err_INV("padic_to_Fp",Y);
2262 4466 : vy = Z_pvalrem(Y,p, &z);
2263 4466 : if (vx < 0 || !gequal1(z)) pari_err_OP("",x, mkintmod(gen_1,Y));
2264 4445 : if (vx >= vy) { set_avma(av); return gen_0; }
2265 4088 : z = gel(x,4);
2266 4088 : if (!signe(z) || vy > vx + precp(x)) pari_err_OP("",x, mkintmod(gen_1,Y));
2267 4088 : if (vx) z = mulii(z, powiu(p,vx));
2268 4088 : return gerepileuptoint(av, remii(z, Y));
2269 : }
2270 : ulong
2271 421704 : padic_to_Fl(GEN x, ulong Y) {
2272 421704 : GEN p = gel(x,2);
2273 : ulong u, z;
2274 421704 : long vy, vx = valp(x);
2275 421704 : vy = u_pvalrem(Y,p, &u);
2276 421705 : if (vx < 0 || u != 1) pari_err_OP("",x, mkintmodu(1,Y));
2277 : /* Y = p^vy */
2278 421705 : if (vx >= vy) return 0;
2279 375995 : z = umodiu(gel(x,4), Y);
2280 375996 : if (!z || vy > vx + precp(x)) pari_err_OP("",x, mkintmodu(1,Y));
2281 375995 : if (vx) {
2282 0 : ulong pp = p[2];
2283 0 : z = Fl_mul(z, upowuu(pp,vx), Y); /* p^vx < p^vy = Y */
2284 : }
2285 375995 : return z;
2286 : }
2287 :
2288 : static void
2289 0 : croak(const char *s) {
2290 : char *t;
2291 0 : t = stack_sprintf("gaffect [overwriting universal object: %s]",s);
2292 0 : pari_err_BUG(t);
2293 0 : }
2294 :
2295 : void
2296 664508 : gaffect(GEN x, GEN y)
2297 : {
2298 664508 : long vx, i, lx, ly, tx = typ(x), ty = typ(y);
2299 : pari_sp av;
2300 : GEN p1, num, den;
2301 :
2302 664508 : if (tx == ty) switch(tx) {
2303 215615 : case t_INT:
2304 572682 : if (!is_universal_constant(y)) { affii(x,y); return; }
2305 : /* y = gen_0, gnil, gen_1 or gen_2 */
2306 0 : if (y==gen_0) croak("gen_0");
2307 0 : if (y==gen_1) croak("gen_1");
2308 0 : if (y==gen_m1) croak("gen_m1");
2309 0 : if (y==gen_m2) croak("gen_m2");
2310 0 : if (y==gen_2) croak("gen_2");
2311 0 : croak("gnil)");
2312 188930 : case t_REAL: affrr(x,y); return;
2313 0 : case t_INTMOD:
2314 0 : if (!dvdii(gel(x,1),gel(y,1))) pari_err_OP("",x,y);
2315 0 : modiiz(gel(x,2),gel(y,1),gel(y,2)); return;
2316 0 : case t_FRAC:
2317 0 : affii(gel(x,1),gel(y,1));
2318 0 : affii(gel(x,2),gel(y,2)); return;
2319 95424 : case t_COMPLEX:
2320 95424 : gaffect(gel(x,1),gel(y,1));
2321 95424 : gaffect(gel(x,2),gel(y,2)); return;
2322 0 : case t_PADIC:
2323 0 : if (!equalii(gel(x,2),gel(y,2))) pari_err_OP("",x,y);
2324 0 : modiiz(gel(x,4),gel(y,3),gel(y,4));
2325 0 : setvalp(y,valp(x)); return;
2326 0 : case t_QUAD:
2327 0 : if (! ZX_equal(gel(x,1),gel(y,1))) pari_err_OP("",x,y);
2328 0 : affii(gel(x,2),gel(y,2));
2329 0 : affii(gel(x,3),gel(y,3)); return;
2330 72713 : case t_VEC: case t_COL: case t_MAT:
2331 72713 : lx = lg(x); if (lx != lg(y)) pari_err_DIM("gaffect");
2332 194584 : for (i=1; i<lx; i++) gaffect(gel(x,i),gel(y,i));
2333 72713 : return;
2334 : }
2335 :
2336 : /* Various conversions. Avoid them, use specialized routines ! */
2337 :
2338 91826 : if (!is_const_t(ty)) pari_err_TYPE2("=",x,y);
2339 91826 : switch(tx)
2340 : {
2341 0 : case t_INT:
2342 : switch(ty)
2343 : {
2344 0 : case t_REAL:
2345 0 : affir(x,y); break;
2346 :
2347 0 : case t_INTMOD:
2348 0 : modiiz(x,gel(y,1),gel(y,2)); break;
2349 :
2350 0 : case t_COMPLEX:
2351 0 : gaffect(x,gel(y,1)); gaffsg(0,gel(y,2)); break;
2352 :
2353 0 : case t_PADIC:
2354 0 : if (!signe(x)) { padicaff0(y); break; }
2355 0 : av = avma;
2356 0 : setvalp(y, Z_pvalrem(x,gel(y,2),&p1));
2357 0 : affii(modii(p1,gel(y,3)), gel(y,4));
2358 0 : set_avma(av); break;
2359 :
2360 0 : case t_QUAD: gaffect(x,gel(y,2)); gaffsg(0,gel(y,3)); break;
2361 0 : default: pari_err_TYPE2("=",x,y);
2362 : }
2363 0 : break;
2364 :
2365 91826 : case t_REAL:
2366 : switch(ty)
2367 : {
2368 91826 : case t_COMPLEX: gaffect(x,gel(y,1)); gaffsg(0,gel(y,2)); break;
2369 0 : default: pari_err_TYPE2("=",x,y);
2370 : }
2371 91826 : break;
2372 :
2373 0 : case t_FRAC:
2374 : switch(ty)
2375 : {
2376 0 : case t_REAL: rdiviiz(gel(x,1),gel(x,2), y); break;
2377 0 : case t_INTMOD: av = avma;
2378 0 : p1 = Fp_inv(gel(x,2),gel(y,1));
2379 0 : affii(modii(mulii(gel(x,1),p1),gel(y,1)), gel(y,2));
2380 0 : set_avma(av); break;
2381 0 : case t_COMPLEX: gaffect(x,gel(y,1)); gaffsg(0,gel(y,2)); break;
2382 0 : case t_PADIC:
2383 0 : if (!signe(gel(x,1))) { padicaff0(y); break; }
2384 0 : num = gel(x,1);
2385 0 : den = gel(x,2);
2386 0 : av = avma; vx = Z_pvalrem(num, gel(y,2), &num);
2387 0 : if (!vx) vx = -Z_pvalrem(den,gel(y,2),&den);
2388 0 : setvalp(y,vx);
2389 0 : p1 = mulii(num,Fp_inv(den,gel(y,3)));
2390 0 : affii(modii(p1,gel(y,3)), gel(y,4)); set_avma(av); break;
2391 0 : case t_QUAD: gaffect(x,gel(y,2)); gaffsg(0,gel(y,3)); break;
2392 0 : default: pari_err_TYPE2("=",x,y);
2393 : }
2394 0 : break;
2395 :
2396 0 : case t_COMPLEX:
2397 0 : if (!gequal0(gel(x,2))) pari_err_TYPE2("=",x,y);
2398 0 : gaffect(gel(x,1), y);
2399 0 : break;
2400 :
2401 0 : case t_PADIC:
2402 : switch(ty)
2403 : {
2404 0 : case t_INTMOD:
2405 0 : av = avma; affii(padic_to_Fp(x, gel(y,1)), gel(y,2));
2406 0 : set_avma(av); break;
2407 0 : default: pari_err_TYPE2("=",x,y);
2408 : }
2409 0 : break;
2410 :
2411 0 : case t_QUAD:
2412 : switch(ty)
2413 : {
2414 0 : case t_INT: case t_INTMOD: case t_FRAC: case t_PADIC:
2415 0 : pari_err_TYPE2("=",x,y);
2416 :
2417 0 : case t_REAL:
2418 0 : av = avma; affgr(quadtofp(x,realprec(y)), y); set_avma(av); break;
2419 0 : case t_COMPLEX:
2420 0 : ly = precision(y); if (!ly) pari_err_TYPE2("=",x,y);
2421 0 : av = avma; gaffect(quadtofp(x,ly), y); set_avma(av); break;
2422 0 : default: pari_err_TYPE2("=",x,y);
2423 : }
2424 0 : default: pari_err_TYPE2("=",x,y);
2425 : }
2426 : }
2427 :
2428 : /*******************************************************************/
2429 : /* */
2430 : /* CONVERSION QUAD --> REAL, COMPLEX OR P-ADIC */
2431 : /* */
2432 : /*******************************************************************/
2433 : GEN
2434 252 : quadtofp(GEN x, long prec)
2435 : {
2436 252 : GEN b, D, z, u = gel(x,2), v = gel(x,3);
2437 : pari_sp av;
2438 252 : if (prec < LOWDEFAULTPREC) prec = LOWDEFAULTPREC;
2439 252 : if (isintzero(v)) return cxcompotor(u, prec);
2440 252 : av = avma; D = quad_disc(x); b = gel(gel(x,1),3); /* 0 or -1 */
2441 : /* u + v (-b + sqrt(D)) / 2 */
2442 252 : if (!signe(b)) b = NULL;
2443 252 : if (b) u = gadd(gmul2n(u,1), v);
2444 252 : z = sqrtr_abs(itor(D, prec));
2445 252 : if (!b) shiftr_inplace(z, -1);
2446 252 : z = gmul(v, z);
2447 252 : if (signe(D) < 0)
2448 : {
2449 35 : z = mkcomplex(cxcompotor(u, prec), z);
2450 35 : if (!b) return gerepilecopy(av, z);
2451 0 : z = gmul2n(z, -1);
2452 : }
2453 : else
2454 : { /* if (b) x ~ (u + z) / 2 and quadnorm(x) ~ (u^2 - z^2) / 4
2455 : * else x ~ u + z and quadnorm(x) ~ u^2 - z^2 */
2456 217 : long s = gsigne(u);
2457 217 : if (s == -gsigne(v)) /* conjugate expression avoids cancellation */
2458 : {
2459 14 : z = gdiv(quadnorm(x), gsub(u, z));
2460 14 : if (b) shiftr_inplace(z, 1);
2461 : }
2462 : else
2463 : {
2464 203 : if (s) z = gadd(u, z);
2465 203 : if (b) shiftr_inplace(z, -1);
2466 : }
2467 : }
2468 217 : return gerepileupto(av, z);
2469 : }
2470 :
2471 : static GEN
2472 28 : qtop(GEN x, GEN p, long d)
2473 : {
2474 28 : GEN z, D, P, b, u = gel(x,2), v = gel(x,3);
2475 : pari_sp av;
2476 28 : if (gequal0(v)) return cvtop(u, p, d);
2477 28 : P = gel(x,1);
2478 28 : b = gel(P,3);
2479 28 : av = avma; D = quad_disc(x);
2480 28 : if (absequaliu(p,2)) d += 2;
2481 28 : z = Qp_sqrt(cvtop(D,p,d));
2482 28 : if (!z) pari_err_SQRTN("Qp_sqrt",D);
2483 14 : z = gmul2n(gsub(z, b), -1);
2484 :
2485 14 : z = gadd(u, gmul(v, z));
2486 14 : if (typ(z) != t_PADIC) /* t_INTMOD for t_QUAD of t_INTMODs... */
2487 0 : z = cvtop(z, p, d);
2488 14 : return gerepileupto(av, z);
2489 : }
2490 : static GEN
2491 14 : ctop(GEN x, GEN p, long d)
2492 : {
2493 14 : pari_sp av = avma;
2494 14 : GEN z, u = gel(x,1), v = gel(x,2);
2495 14 : if (isrationalzero(v)) return cvtop(u, p, d);
2496 14 : z = Qp_sqrt(cvtop(gen_m1, p, d - gvaluation(v, p))); /* = I */
2497 14 : if (!z) pari_err_SQRTN("Qp_sqrt",gen_m1);
2498 :
2499 14 : z = gadd(u, gmul(v, z));
2500 14 : if (typ(z) != t_PADIC) /* t_INTMOD for t_COMPLEX of t_INTMODs... */
2501 0 : z = cvtop(z, p, d);
2502 14 : return gerepileupto(av, z);
2503 : }
2504 :
2505 : /* cvtop2(stoi(s), y) */
2506 : GEN
2507 399 : cvstop2(long s, GEN y)
2508 : {
2509 399 : GEN z, p = gel(y,2);
2510 399 : long v, d = signe(gel(y,4))? precp(y): 0;
2511 399 : if (!s) return zeropadic_shallow(p, d);
2512 399 : v = z_pvalrem(s, p, &s);
2513 399 : if (d <= 0) return zeropadic_shallow(p, v);
2514 399 : z = cgetg(5, t_PADIC);
2515 399 : z[1] = evalprecp(d) | evalvalp(v);
2516 399 : gel(z,2) = p;
2517 399 : gel(z,3) = gel(y,3);
2518 399 : gel(z,4) = modsi(s, gel(y,3)); return z;
2519 : }
2520 :
2521 : static GEN
2522 17607544 : itop2_coprime(GEN x, GEN y, long v, long d)
2523 : {
2524 17607544 : GEN z = cgetg(5, t_PADIC);
2525 17606834 : z[1] = evalprecp(d) | evalvalp(v);
2526 17606736 : gel(z,2) = gel(y,2);
2527 17606736 : gel(z,3) = gel(y,3);
2528 17606736 : gel(z,4) = modii(x, gel(y,3)); return z;
2529 : }
2530 : /* cvtop(x, gel(y,2), precp(y)), shallow */
2531 : GEN
2532 17615342 : cvtop2(GEN x, GEN y)
2533 : {
2534 17615342 : GEN p = gel(y,2);
2535 17615342 : long v, d = signe(gel(y,4))? precp(y): 0;
2536 17615342 : switch(typ(x))
2537 : {
2538 14818239 : case t_INT:
2539 14818239 : if (!signe(x)) return zeropadic_shallow(p, d);
2540 14818239 : if (d <= 0) return zeropadic_shallow(p, Z_pval(x,p));
2541 14813850 : v = Z_pvalrem(x, p, &x); return itop2_coprime(x, y, v, d);
2542 :
2543 0 : case t_INTMOD:
2544 0 : v = Z_pval(gel(x,1),p); if (v > d) v = d;
2545 0 : return cvtop(gel(x,2), p, v);
2546 :
2547 2796325 : case t_FRAC:
2548 : {
2549 : GEN num, den;
2550 2796325 : if (d <= 0) return zeropadic_shallow(p, Q_pval(x,p));
2551 2795044 : num = gel(x,1); v = Z_pvalrem(num, p, &num);
2552 2795040 : den = gel(x,2); if (!v) v = -Z_pvalrem(den, p, &den);
2553 2795044 : if (!is_pm1(den)) num = mulii(num, Fp_inv(den, gel(y,3)));
2554 2795042 : return itop2_coprime(num, y, v, d);
2555 : }
2556 7 : case t_COMPLEX: return ctop(x, p, d);
2557 28 : case t_QUAD: return qtop(x, p, d);
2558 931 : case t_PADIC:
2559 931 : if (!signe(gel(x,4))) return zeropadic_shallow(p, d);
2560 931 : if (precp(x) <= d) return x;
2561 35 : return itop2_coprime(gel(x,4), y, valp(x), d); /* reduce accuracy */
2562 : }
2563 0 : pari_err_TYPE("cvtop2",x);
2564 : return NULL; /* LCOV_EXCL_LINE */
2565 : }
2566 :
2567 : /* assume is_const_t(tx) */
2568 : GEN
2569 600438 : cvtop(GEN x, GEN p, long d)
2570 : {
2571 : GEN z;
2572 : long v;
2573 :
2574 600438 : if (typ(p) != t_INT) pari_err_TYPE("cvtop",p);
2575 600439 : switch(typ(x))
2576 : {
2577 268170 : case t_INT:
2578 268170 : if (!signe(x)) return zeropadic(p, d);
2579 266952 : if (d <= 0) return zeropadic(p, Z_pval(x,p));
2580 266896 : v = Z_pvalrem(x, p, &x);
2581 266896 : z = cgetg(5, t_PADIC);
2582 266896 : z[1] = evalprecp(d) | evalvalp(v);
2583 266896 : gel(z,2) = icopy(p);
2584 266896 : gel(z,3) = powiu(p, d);
2585 266895 : gel(z,4) = modii(x, gel(z,3)); return z; /* not memory-clean */
2586 :
2587 28 : case t_INTMOD:
2588 28 : v = Z_pval(gel(x,1),p); if (v > d) v = d;
2589 28 : return cvtop(gel(x,2), p, v);
2590 :
2591 163019 : case t_FRAC:
2592 : {
2593 : GEN num, den;
2594 163019 : if (d <= 0) return zeropadic(p, Q_pval(x,p));
2595 163005 : num = gel(x,1); v = Z_pvalrem(num, p, &num);
2596 163005 : den = gel(x,2); if (!v) v = -Z_pvalrem(den, p, &den);
2597 163005 : z = cgetg(5, t_PADIC);
2598 163005 : z[1] = evalprecp(d) | evalvalp(v);
2599 163005 : gel(z,2) = icopy(p);
2600 163005 : gel(z,3) = powiu(p, d);
2601 163005 : if (!is_pm1(den)) num = mulii(num, Fp_inv(den, gel(z,3)));
2602 163005 : gel(z,4) = modii(num, gel(z,3)); return z; /* not memory-clean */
2603 : }
2604 7 : case t_COMPLEX: return ctop(x, p, d);
2605 169215 : case t_PADIC:
2606 169215 : p = gel(x,2); /* override */
2607 169215 : if (!signe(gel(x,4))) return zeropadic(p, d);
2608 169138 : z = cgetg(5,t_PADIC);
2609 169138 : z[1] = x[1]; setprecp(z,d);
2610 169138 : gel(z,2) = icopy(p);
2611 169138 : gel(z,3) = powiu(p, d);
2612 169138 : gel(z,4) = modii(gel(x,4), gel(z,3)); return z;
2613 :
2614 0 : case t_QUAD: return qtop(x, p, d);
2615 : }
2616 0 : pari_err_TYPE("cvtop",x);
2617 : return NULL; /* LCOV_EXCL_LINE */
2618 : }
2619 :
2620 : GEN
2621 126 : gcvtop(GEN x, GEN p, long r)
2622 : {
2623 : long i, lx;
2624 : GEN y;
2625 :
2626 126 : switch(typ(x))
2627 : {
2628 28 : case t_POL: case t_SER:
2629 28 : y = cgetg_copy(x, &lx); y[1] = x[1];
2630 98 : for (i=2; i<lx; i++) gel(y,i) = gcvtop(gel(x,i),p,r);
2631 28 : return y;
2632 0 : case t_POLMOD: case t_RFRAC: case t_VEC: case t_COL: case t_MAT:
2633 0 : pari_APPLY_same(gcvtop(gel(x,i),p,r));
2634 : }
2635 98 : return cvtop(x,p,r);
2636 : }
2637 :
2638 : long
2639 817651898 : gexpo_safe(GEN x)
2640 : {
2641 817651898 : long tx = typ(x), lx, e, f, i;
2642 :
2643 817651898 : switch(tx)
2644 : {
2645 167259642 : case t_INT:
2646 167259642 : return expi(x);
2647 :
2648 1094614 : case t_FRAC:
2649 1094614 : return expi(gel(x,1)) - expi(gel(x,2));
2650 :
2651 454512017 : case t_REAL:
2652 454512017 : return expo(x);
2653 :
2654 86930588 : case t_COMPLEX:
2655 86930588 : e = gexpo(gel(x,1));
2656 86931359 : f = gexpo(gel(x,2)); return maxss(e, f);
2657 :
2658 91 : case t_QUAD: {
2659 91 : GEN p = gel(x,1); /* mod = X^2 + {0,1}* X - {D/4, (1-D)/4})*/
2660 91 : long d = 1 + expi(gel(p,2))/2; /* ~ expo(sqrt(D)) */
2661 91 : e = gexpo(gel(x,2));
2662 91 : f = gexpo(gel(x,3)) + d; return maxss(e, f);
2663 : }
2664 76527650 : case t_POL: case t_SER:
2665 76527650 : lx = lg(x); f = -(long)HIGHEXPOBIT;
2666 305899941 : for (i=2; i<lx; i++) { e=gexpo(gel(x,i)); if (e>f) f=e; }
2667 76525500 : return f;
2668 31444187 : case t_VEC: case t_COL: case t_MAT:
2669 31444187 : lx = lg(x); f = -(long)HIGHEXPOBIT;
2670 217452443 : for (i=1; i<lx; i++) { e=gexpo(gel(x,i)); if (e>f) f=e; }
2671 31444380 : return f;
2672 : }
2673 48 : return -1-(long)HIGHEXPOBIT;
2674 : }
2675 : long
2676 817412547 : gexpo(GEN x)
2677 : {
2678 817412547 : long e = gexpo_safe(x);
2679 817422775 : if (e < -(long)HIGHEXPOBIT) pari_err_TYPE("gexpo",x);
2680 817424638 : return e;
2681 : }
2682 : GEN
2683 89627 : gpexponent(GEN x)
2684 : {
2685 89627 : long e = gexpo(x);
2686 89627 : return e == -(long)HIGHEXPOBIT? mkmoo(): stoi(e);
2687 : }
2688 :
2689 : long
2690 7 : sizedigit(GEN x)
2691 : {
2692 7 : return gequal0(x)? 0: (long) ((gexpo(x)+1) * LOG10_2) + 1;
2693 : }
2694 :
2695 : /* normalize series. avma is not updated */
2696 : GEN
2697 13402114 : normalizeser(GEN x)
2698 : {
2699 13402114 : long i, lx = lg(x), vx=varn(x), vp=valser(x);
2700 : GEN y, z;
2701 :
2702 13402114 : if (lx == 2) { setsigne(x,0); return x; }
2703 13401750 : if (lx == 3) {
2704 199422 : z = gel(x,2);
2705 199422 : if (!gequal0(z)) { setsigne(x,1); return x; }
2706 23765 : if (isrationalzero(z)) return zeroser(vx,vp+1);
2707 4312 : if (isexactzero(z)) {
2708 : /* dangerous case: already normalized ? */
2709 280 : if (!signe(x)) return x;
2710 35 : setvalser(x,vp+1); /* no: normalize */
2711 : }
2712 4067 : setsigne(x,0); return x;
2713 : }
2714 13496444 : for (i=2; i<lx; i++)
2715 13449712 : if (! isrationalzero(gel(x,i))) break;
2716 13202328 : if (i == lx) return zeroser(vx,lx-2+vp);
2717 13155596 : z = gel(x,i);
2718 13159397 : while (i<lx && isexactzero(gel(x,i))) i++;
2719 13155596 : if (i == lx)
2720 : {
2721 273 : i -= 3; y = x + i;
2722 273 : stackdummy((pari_sp)y, (pari_sp)x);
2723 273 : gel(y,2) = z;
2724 273 : y[1] = evalsigne(0) | evalvalser(lx-2+vp) | evalvarn(vx);
2725 273 : y[0] = evaltyp(t_SER) | _evallg(3);
2726 273 : return y;
2727 : }
2728 :
2729 13155323 : i -= 2; y = x + i; lx -= i;
2730 13155323 : y[1] = evalsigne(1) | evalvalser(vp+i) | evalvarn(vx);
2731 13155323 : y[0] = evaltyp(t_SER) | _evallg(lx);
2732 :
2733 13155323 : stackdummy((pari_sp)y, (pari_sp)x);
2734 13184497 : for (i = 2; i < lx; i++)
2735 13183559 : if (!gequal0(gel(y, i))) return y;
2736 938 : setsigne(y, 0); return y;
2737 : }
2738 :
2739 : GEN
2740 0 : normalizepol_approx(GEN x, long lx)
2741 : {
2742 : long i;
2743 0 : for (i = lx-1; i>1; i--)
2744 0 : if (! gequal0(gel(x,i))) break;
2745 0 : stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
2746 0 : setlg(x, i+1); setsigne(x, i!=1); return x;
2747 : }
2748 :
2749 : GEN
2750 767748202 : normalizepol_lg(GEN x, long lx)
2751 : {
2752 767748202 : long i, LX = 0;
2753 767748202 : GEN KEEP = NULL;
2754 :
2755 1018711268 : for (i = lx-1; i>1; i--)
2756 : {
2757 914741145 : GEN z = gel(x,i);
2758 914741145 : if (! gequal0(z) ) {
2759 663997817 : if (!LX) LX = i+1;
2760 663997817 : stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + LX));
2761 663989954 : x[0] = evaltyp(t_POL) | _evallg(LX);
2762 663989954 : setsigne(x,1); return x;
2763 250723334 : } else if (!isexactzero(z)) {
2764 963662 : if (!LX) LX = i+1; /* to be kept as leading coeff */
2765 250001742 : } else if (!isrationalzero(z))
2766 832720 : KEEP = z; /* to be kept iff all other coeffs are exact 0s */
2767 : }
2768 103970123 : if (!LX) {
2769 103468860 : if (KEEP) { /* e.g. Pol(Mod(0,2)) */
2770 348358 : gel(x,2) = KEEP;
2771 348358 : LX = 3;
2772 : } else
2773 103120502 : LX = 2; /* Pol(0) */
2774 : }
2775 103970123 : stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + LX));
2776 103868501 : x[0] = evaltyp(t_POL) | _evallg(LX);
2777 103868501 : setsigne(x,0); return x;
2778 : }
2779 :
2780 : /* normalize polynomial x in place */
2781 : GEN
2782 98847932 : normalizepol(GEN x)
2783 : {
2784 98847932 : return normalizepol_lg(x, lg(x));
2785 : }
2786 :
2787 : int
2788 74531830 : gsigne(GEN x)
2789 : {
2790 74531830 : switch(typ(x))
2791 : {
2792 74151688 : case t_INT: case t_REAL: return signe(x);
2793 379514 : case t_FRAC: return signe(gel(x,1));
2794 623 : case t_QUAD:
2795 : {
2796 623 : pari_sp av = avma;
2797 623 : GEN T = gel(x,1), a = gel(x,2), b = gel(x,3);
2798 : long sa, sb;
2799 623 : if (signe(gel(T,2)) > 0) break;
2800 609 : a = gmul2n(a,1);
2801 609 : if (signe(gel(T,3))) a = gadd(a,b);
2802 : /* a + b sqrt(D) > 0 ? */
2803 609 : sa = gsigne(a);
2804 609 : sb = gsigne(b); if (sa == sb) return gc_int(av,sa);
2805 224 : if (sa == 0) return gc_int(av,sb);
2806 217 : if (sb == 0) return gc_int(av,sa);
2807 : /* different signs, take conjugate expression */
2808 210 : sb = gsigne(gsub(gsqr(a), gmul(quad_disc(x), gsqr(b))));
2809 210 : return gc_int(av, sb*sa);
2810 : }
2811 14 : case t_INFINITY: return inf_get_sign(x);
2812 : }
2813 12 : pari_err_TYPE("gsigne",x);
2814 : return 0; /* LCOV_EXCL_LINE */
2815 : }
2816 :
2817 : /*******************************************************************/
2818 : /* */
2819 : /* LISTS */
2820 : /* */
2821 : /*******************************************************************/
2822 : /* make sure L can hold l elements, at least doubling the previous max number
2823 : * of components. */
2824 : static void
2825 791252 : ensure_nb(GEN L, long l)
2826 : {
2827 791252 : long nmax = list_nmax(L), i, lw;
2828 : GEN v, w;
2829 791252 : if (l <= nmax) return;
2830 679 : if (nmax)
2831 : {
2832 273 : nmax <<= 1;
2833 273 : if (l > nmax) nmax = l;
2834 273 : w = list_data(L); lw = lg(w);
2835 273 : v = newblock(nmax+1);
2836 273 : v[0] = w[0];
2837 1045653 : for (i=1; i < lw; i++) gel(v,i) = gel(w, i);
2838 273 : killblock(w);
2839 : }
2840 : else /* unallocated */
2841 : {
2842 406 : nmax = 32;
2843 406 : if (list_data(L))
2844 0 : pari_err(e_MISC, "store list in variable before appending elements");
2845 406 : v = newblock(nmax+1);
2846 406 : v[0] = evaltyp(t_VEC) | _evallg(1);
2847 : }
2848 679 : list_data(L) = v;
2849 679 : L[1] = evaltyp(list_typ(L))|evallg(nmax);
2850 : }
2851 :
2852 : void
2853 7 : listkill(GEN L)
2854 : {
2855 :
2856 7 : if (typ(L) != t_LIST) pari_err_TYPE("listkill",L);
2857 7 : if (list_nmax(L)) {
2858 7 : GEN v = list_data(L);
2859 7 : long i, l = lg(v);
2860 49 : for (i=1; i<l; i++) gunclone_deep(gel(v,i));
2861 7 : killblock(v);
2862 7 : L[1] = evaltyp(list_typ(L));
2863 7 : list_data(L) = NULL;
2864 : }
2865 7 : }
2866 :
2867 : GEN
2868 6548 : mklist_typ(long t)
2869 : {
2870 6548 : GEN L = cgetg(3,t_LIST);
2871 6548 : L[1] = evaltyp(t);
2872 6548 : list_data(L) = NULL; return L;
2873 : }
2874 :
2875 : GEN
2876 6492 : mklist(void)
2877 : {
2878 6492 : return mklist_typ(t_LIST_RAW);
2879 : }
2880 :
2881 : GEN
2882 49 : mkmap(void)
2883 : {
2884 49 : return mklist_typ(t_LIST_MAP);
2885 : }
2886 :
2887 : /* return a list with single element x, allocated on stack */
2888 : GEN
2889 63 : mklistcopy(GEN x)
2890 : {
2891 63 : GEN y = mklist();
2892 63 : list_data(y) = mkveccopy(x);
2893 63 : return y;
2894 : }
2895 :
2896 : GEN
2897 7 : listcreate_gp(long n)
2898 : {
2899 7 : (void) n; return mklist();
2900 : }
2901 :
2902 : GEN
2903 756406 : listput(GEN L, GEN x, long index)
2904 : {
2905 : long l;
2906 : GEN z;
2907 :
2908 756406 : if (index < 0) pari_err_COMPONENT("listput", "<", gen_0, stoi(index));
2909 756399 : z = list_data(L);
2910 756399 : l = z? lg(z): 1;
2911 :
2912 756399 : x = gclone(x);
2913 756399 : if (!index || index >= l)
2914 : {
2915 756245 : ensure_nb(L, l);
2916 756245 : z = list_data(L); /* it may change ! */
2917 756245 : index = l;
2918 756245 : l++;
2919 : } else
2920 154 : gunclone_deep( gel(z, index) );
2921 756399 : gel(z,index) = x;
2922 756399 : z[0] = evaltyp(t_VEC) | evallg(l); /*must be after gel(z,index) is set*/
2923 756399 : return gel(z,index);
2924 : }
2925 :
2926 : GEN
2927 705236 : listput0(GEN L, GEN x, long index)
2928 : {
2929 705236 : if (typ(L) != t_LIST || list_typ(L) != t_LIST_RAW)
2930 14 : pari_err_TYPE("listput",L);
2931 705222 : (void) listput(L, x, index);
2932 705215 : return x;
2933 : }
2934 :
2935 : GEN
2936 35014 : listinsert(GEN L, GEN x, long index)
2937 : {
2938 : long l, i;
2939 : GEN z;
2940 :
2941 35014 : z = list_data(L); l = z? lg(z): 1;
2942 35014 : if (index <= 0) pari_err_COMPONENT("listinsert", "<=", gen_0, stoi(index));
2943 35007 : if (index > l) index = l;
2944 35007 : ensure_nb(L, l);
2945 35007 : BLOCK_SIGINT_START
2946 35007 : z = list_data(L);
2947 87552507 : for (i=l; i > index; i--) gel(z,i) = gel(z,i-1);
2948 35007 : z[0] = evaltyp(t_VEC) | evallg(l+1);
2949 35007 : gel(z,index) = gclone(x);
2950 35007 : BLOCK_SIGINT_END
2951 35007 : return gel(z,index);
2952 : }
2953 :
2954 : GEN
2955 35028 : listinsert0(GEN L, GEN x, long index)
2956 : {
2957 35028 : if (typ(L) != t_LIST || list_typ(L) != t_LIST_RAW)
2958 14 : pari_err_TYPE("listinsert",L);
2959 35014 : (void) listinsert(L, x, index);
2960 35007 : return x;
2961 : }
2962 :
2963 : void
2964 21917 : listpop(GEN L, long index)
2965 : {
2966 : long l, i;
2967 : GEN z;
2968 :
2969 21917 : if (typ(L) != t_LIST) pari_err_TYPE("listinsert",L);
2970 21917 : if (index < 0) pari_err_COMPONENT("listpop", "<", gen_0, stoi(index));
2971 21917 : z = list_data(L);
2972 21917 : if (!z || (l = lg(z)-1) == 0) return;
2973 :
2974 21903 : if (!index || index > l) index = l;
2975 21903 : BLOCK_SIGINT_START
2976 21903 : gunclone_deep( gel(z, index) );
2977 21903 : z[0] = evaltyp(t_VEC) | _evallg(l);
2978 21910 : for (i=index; i < l; i++) z[i] = z[i+1];
2979 21903 : BLOCK_SIGINT_END
2980 : }
2981 :
2982 : void
2983 56 : listpop0(GEN L, long index)
2984 : {
2985 56 : if (typ(L) != t_LIST || list_typ(L) != t_LIST_RAW)
2986 14 : pari_err_TYPE("listpop",L);
2987 42 : listpop(L, index);
2988 42 : }
2989 :
2990 : /* return a copy fully allocated on stack. gclone from changevalue is
2991 : * supposed to malloc() it */
2992 : GEN
2993 5580 : gtolist(GEN x)
2994 : {
2995 : GEN y;
2996 :
2997 5580 : if (!x) return mklist();
2998 370 : switch(typ(x))
2999 : {
3000 300 : case t_VEC: case t_COL:
3001 300 : y = mklist();
3002 300 : if (lg(x) == 1) return y;
3003 279 : list_data(y) = gcopy(x);
3004 279 : settyp(list_data(y), t_VEC);
3005 279 : return y;
3006 7 : case t_LIST:
3007 7 : y = mklist();
3008 7 : list_data(y) = list_data(x)? gcopy(list_data(x)): NULL;
3009 7 : return y;
3010 63 : default:
3011 63 : return mklistcopy(x);
3012 : }
3013 : }
3014 :
3015 : void
3016 21 : listsort(GEN L, long flag)
3017 : {
3018 : long i, l;
3019 21 : pari_sp av = avma;
3020 : GEN perm, v, vnew;
3021 :
3022 21 : if (typ(L) != t_LIST) pari_err_TYPE("listsort",L);
3023 21 : v = list_data(L); l = v? lg(v): 1;
3024 21 : if (l < 3) return;
3025 21 : if (flag)
3026 : {
3027 : long lnew;
3028 14 : perm = gen_indexsort_uniq(L, (void*)&cmp_universal, cmp_nodata);
3029 14 : lnew = lg(perm); /* may have changed since 'uniq' */
3030 14 : vnew = cgetg(lnew,t_VEC);
3031 56 : for (i=1; i<lnew; i++) {
3032 42 : long c = perm[i];
3033 42 : gel(vnew,i) = gel(v,c);
3034 42 : gel(v,c) = NULL;
3035 : }
3036 14 : if (l != lnew) { /* was shortened */
3037 105 : for (i=1; i<l; i++)
3038 91 : if (gel(v,i)) gunclone_deep(gel(v,i));
3039 14 : l = lnew;
3040 : }
3041 : }
3042 : else
3043 : {
3044 7 : perm = gen_indexsort(L, (void*)&cmp_universal, cmp_nodata);
3045 7 : vnew = cgetg(l,t_VEC);
3046 63 : for (i=1; i<l; i++) gel(vnew,i) = gel(v,perm[i]);
3047 : }
3048 119 : for (i=1; i<l; i++) gel(v,i) = gel(vnew,i);
3049 21 : v[0] = vnew[0]; set_avma(av);
3050 : }
|