Line data Source code
1 : /* Copyright (C) 2000-2010 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 : /* MALLOC/FREE WRAPPERS */
18 : /*********************************************************************/
19 : #define BLOCK_SIGALRM_START \
20 : { \
21 : int block=PARI_SIGINT_block; \
22 : PARI_SIGINT_block = 2; \
23 : MT_SIGINT_BLOCK(block);
24 :
25 : #define BLOCK_SIGINT_START \
26 : { \
27 : int block=PARI_SIGINT_block; \
28 : PARI_SIGINT_block = 1; \
29 : MT_SIGINT_BLOCK(block);
30 :
31 : #define BLOCK_SIGINT_END \
32 : PARI_SIGINT_block = block; \
33 : MT_SIGINT_UNBLOCK(block); \
34 : if (!block && PARI_SIGINT_pending) \
35 : { \
36 : int sig = PARI_SIGINT_pending; \
37 : PARI_SIGINT_pending = 0; \
38 : raise(sig); \
39 : } \
40 : }
41 :
42 : /*******************************************************************/
43 : /* */
44 : /* CONSTRUCTORS */
45 : /* */
46 : /*******************************************************************/
47 : #define retmkfrac(x,y)\
48 : do { GEN _v = cgetg(3, t_FRAC);\
49 : gel(_v,1) = (x);\
50 : gel(_v,2) = (y); return _v; } while(0)
51 : #define retmkrfrac(x,y)\
52 : do { GEN _v = cgetg(3, t_RFRAC);\
53 : gel(_v,1) = (x);\
54 : gel(_v,2) = (y); return _v; } while(0)
55 : #define retmkintmod(x,y)\
56 : do { GEN _v = cgetg(3, t_INTMOD);\
57 : gel(_v,1) = (y);\
58 : gel(_v,2) = (x); return _v; } while(0)
59 : #define retmkcomplex(x,y)\
60 : do { GEN _v = cgetg(3, t_COMPLEX);\
61 : gel(_v,1) = (x);\
62 : gel(_v,2) = (y); return _v; } while(0)
63 : #define retmkpolmod(x,y)\
64 : do { GEN _v = cgetg(3, t_POLMOD);\
65 : gel(_v,1) = (y);\
66 : gel(_v,2) = (x); return _v; } while(0)
67 : #define retmkvec(x)\
68 : do { GEN _v = cgetg(2, t_VEC);\
69 : gel(_v,1) = (x); return _v; } while(0)
70 : #define retmkvec2(x,y)\
71 : do { GEN _v = cgetg(3, t_VEC);\
72 : gel(_v,1) = (x);\
73 : gel(_v,2) = (y); return _v; } while(0)
74 : #define retmkvec3(x,y,z)\
75 : do { GEN _v = cgetg(4, t_VEC);\
76 : gel(_v,1) = (x);\
77 : gel(_v,2) = (y);\
78 : gel(_v,3) = (z); return _v; } while(0)
79 : #define retmkqfb(x,y,z,d)\
80 : do { GEN _v = cgetg(5, t_QFB);\
81 : gel(_v,1) = (x);\
82 : gel(_v,2) = (y);\
83 : gel(_v,3) = (z);\
84 : gel(_v,4) = (d); return _v; } while(0)
85 : #define retmkquad(x,y,z)\
86 : do { GEN _v = cgetg(4, t_QUAD);\
87 : gel(_v,1) = (x);\
88 : gel(_v,2) = (y);\
89 : gel(_v,3) = (z); return _v; } while(0)
90 : #define retmkvec4(x,y,z,t)\
91 : do { GEN _v = cgetg(5, t_VEC);\
92 : gel(_v,1) = (x);\
93 : gel(_v,2) = (y);\
94 : gel(_v,3) = (z);\
95 : gel(_v,4) = (t); return _v; } while(0)
96 : #define retmkvec5(x,y,z,t,u)\
97 : do { GEN _v = cgetg(6, t_VEC);\
98 : gel(_v,1) = (x);\
99 : gel(_v,2) = (y);\
100 : gel(_v,3) = (z);\
101 : gel(_v,4) = (t);\
102 : gel(_v,5) = (u); return _v; } while(0)
103 : #define retmkcol(x)\
104 : do { GEN _v = cgetg(2, t_COL);\
105 : gel(_v,1) = (x); return _v; } while(0)
106 : #define retmkcol2(x,y)\
107 : do { GEN _v = cgetg(3, t_COL);\
108 : gel(_v,1) = (x);\
109 : gel(_v,2) = (y); return _v; } while(0)
110 : #define retmkcol3(x,y,z)\
111 : do { GEN _v = cgetg(4, t_COL);\
112 : gel(_v,1) = (x);\
113 : gel(_v,2) = (y);\
114 : gel(_v,3) = (z); return _v; } while(0)
115 : #define retmkcol4(x,y,z,t)\
116 : do { GEN _v = cgetg(5, t_COL);\
117 : gel(_v,1) = (x);\
118 : gel(_v,2) = (y);\
119 : gel(_v,3) = (z);\
120 : gel(_v,4) = (t); return _v; } while(0)
121 : #define retmkcol5(x,y,z,t,u)\
122 : do { GEN _v = cgetg(6, t_COL);\
123 : gel(_v,1) = (x);\
124 : gel(_v,2) = (y);\
125 : gel(_v,3) = (z);\
126 : gel(_v,4) = (t);\
127 : gel(_v,5) = (u); return _v; } while(0)
128 : #define retmkcol6(x,y,z,t,u,v)\
129 : do { GEN _v = cgetg(7, t_COL);\
130 : gel(_v,1) = (x);\
131 : gel(_v,2) = (y);\
132 : gel(_v,3) = (z);\
133 : gel(_v,4) = (t);\
134 : gel(_v,5) = (u);\
135 : gel(_v,6) = (v); return _v; } while(0)
136 : #define retmkmat(x)\
137 : do { GEN _v = cgetg(2, t_MAT);\
138 : gel(_v,1) = (x); return _v; } while(0)
139 : #define retmkmat2(x,y)\
140 : do { GEN _v = cgetg(3, t_MAT);\
141 : gel(_v,1) = (x);\
142 : gel(_v,2) = (y); return _v; } while(0)
143 : #define retmkmat3(x,y,z)\
144 : do { GEN _v = cgetg(4, t_MAT);\
145 : gel(_v,1) = (x);\
146 : gel(_v,2) = (y);\
147 : gel(_v,3) = (z); return _v; } while(0)
148 : #define retmkmat4(x,y,z,t)\
149 : do { GEN _v = cgetg(5, t_MAT);\
150 : gel(_v,1) = (x);\
151 : gel(_v,2) = (y);\
152 : gel(_v,3) = (z);\
153 : gel(_v,4) = (t); return _v; } while(0)
154 : #define retmkmat5(x,y,z,t,u)\
155 : do { GEN _v = cgetg(6, t_MAT);\
156 : gel(_v,1) = (x);\
157 : gel(_v,2) = (y);\
158 : gel(_v,3) = (z);\
159 : gel(_v,4) = (t);\
160 : gel(_v,5) = (u); return _v; } while(0)
161 : #define retmkmat22(a,b,c,d)\
162 : do { GEN _v = cgetg(3, t_MAT);\
163 : gel(_v,1) = mkcol2(a,c);\
164 : gel(_v,2) = mkcol2(b,d); return _v; } while(0)
165 : INLINE GEN
166 6536366 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
167 : INLINE GEN
168 1806 : mkintmodu(ulong x, ulong y) {
169 1806 : GEN v = cgetg(3,t_INTMOD);
170 1806 : gel(v,1) = utoipos(y);
171 1806 : gel(v,2) = utoi(x); return v;
172 : }
173 : INLINE GEN
174 2611490 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
175 : INLINE GEN
176 31408045 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
177 : INLINE GEN
178 1205620 : mkfracss(long x, long y) { retmkfrac(stoi(x),utoipos(y)); }
179 : /* q = n/d a t_FRAC or t_INT; recover (n,d) */
180 : INLINE void
181 19229 : Qtoss(GEN q, long *n, long *d)
182 : {
183 19229 : if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
184 5691 : else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
185 19229 : }
186 : INLINE GEN
187 770343 : sstoQ(long n, long d)
188 : {
189 : ulong r;
190 : long g, q;
191 770343 : if (!n)
192 : {
193 92554 : if (!d) pari_err_INV("sstoQ",gen_0);
194 92554 : return gen_0;
195 : }
196 677789 : if (d < 0) { d = -d; n = -n; }
197 677789 : if (d == 1) return stoi(n);
198 664790 : r = labs(n);
199 664790 : if (r == 1) retmkfrac(n > 0? gen_1: gen_m1, utoipos(d));
200 630413 : q = udivuu_rem(r, d, &r);
201 630413 : if (!r) return n > 0? utoipos(q): utoineg(q);
202 575708 : g = ugcd(d,r); /* gcd(n,d) */
203 575708 : if (g != 1) { n /= g; d /= g; }
204 575708 : retmkfrac(stoi(n), utoipos(d));
205 : }
206 :
207 : INLINE GEN
208 157969934 : uutoQ(ulong n, ulong d)
209 : {
210 : ulong r;
211 : long g, q;
212 157969934 : if (!n)
213 : {
214 9438 : if (!d) pari_err_INV("uutoQ",gen_0);
215 9438 : return gen_0;
216 : }
217 157960496 : if (d == 1) return utoipos(n);
218 157949394 : if (n == 1) retmkfrac(gen_1, utoipos(d));
219 157860550 : q = udivuu_rem(n,d,&r);
220 157860550 : if (!r) return utoipos(q);
221 5141991 : g = ugcd(d,r); /* gcd(n,d) */
222 5141991 : if (g != 1) { n /= g; d /= g; }
223 5141991 : retmkfrac(utoipos(n), utoipos(d));
224 : }
225 :
226 : INLINE GEN
227 4704459 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
228 : INLINE GEN
229 697633 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
230 697633 : gel(v,1) = x; gel(v,2) = y; return v; }
231 : INLINE GEN
232 7 : mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
233 7 : gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
234 : INLINE GEN
235 75747411 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
236 : INLINE GEN
237 830417 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
238 : INLINE GEN
239 534026 : cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
240 : INLINE GEN
241 69132 : mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
242 69132 : gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
243 : /* vecsmall */
244 : INLINE GEN
245 124542492 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
246 : INLINE GEN
247 216332198 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
248 216311403 : v[1]=x; v[2]=y; return v; }
249 : INLINE GEN
250 93713694 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
251 93702891 : v[1]=x; v[2]=y; v[3]=z; return v; }
252 : INLINE GEN
253 15170883 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
254 15161338 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
255 : INLINE GEN
256 664427 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
257 664421 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
258 :
259 : INLINE GEN
260 27909298 : mkqfb(GEN x, GEN y, GEN z, GEN d) { retmkqfb(x,y,z,d); }
261 : /* vec */
262 : INLINE GEN
263 66858725 : mkvec(GEN x) { retmkvec(x); }
264 : INLINE GEN
265 179165651 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
266 : INLINE GEN
267 102010575 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
268 : INLINE GEN
269 6280985 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
270 : INLINE GEN
271 15651937 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
272 : INLINE GEN
273 161 : mkvecs(long x) { retmkvec(stoi(x)); }
274 : INLINE GEN
275 212909 : mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
276 : INLINE GEN
277 488650 : mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
278 : INLINE GEN
279 21 : mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
280 : INLINE GEN
281 35889 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
282 : INLINE GEN
283 118460 : mkvec2copy(GEN x, GEN y) {
284 118460 : GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
285 : /* col */
286 : INLINE GEN
287 9340275 : mkcol(GEN x) { retmkcol(x); }
288 : INLINE GEN
289 126409264 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
290 : INLINE GEN
291 86466 : mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
292 : INLINE GEN
293 102935 : mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
294 : INLINE GEN
295 94 : mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
296 : INLINE GEN
297 12805 : mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
298 : INLINE GEN
299 40411 : mkcols(long x) { retmkcol(stoi(x)); }
300 : INLINE GEN
301 2998352 : mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
302 : INLINE GEN
303 308 : mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
304 : INLINE GEN
305 0 : mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
306 : INLINE GEN
307 28804 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
308 : /* mat */
309 : INLINE GEN
310 4417769 : mkmat(GEN x) { retmkmat(x); }
311 : INLINE GEN
312 30900802 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
313 : INLINE GEN
314 16777 : mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
315 : INLINE GEN
316 0 : mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
317 : INLINE GEN
318 0 : mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
319 : INLINE GEN
320 112614 : mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
321 : INLINE GEN
322 0 : mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
323 : INLINE GEN
324 200743 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
325 : INLINE GEN
326 103345 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
327 : INLINE long
328 316293 : inf_get_sign(GEN x) { return signe(gel(x,1)); }
329 : INLINE GEN
330 130830 : mkmat22s(long a, long b, long c, long d) {retmkmat2(mkcol2s(a,c),mkcol2s(b,d));}
331 : INLINE GEN
332 37282365 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
333 :
334 : /* pol */
335 : INLINE GEN
336 2571720 : pol_x(long v) {
337 2571720 : GEN p = cgetg(4, t_POL);
338 2571747 : p[1] = evalsigne(1)|evalvarn(v);
339 2571747 : gel(p,2) = gen_0;
340 2571747 : gel(p,3) = gen_1; return p;
341 : }
342 : /* x^n, assume n >= 0 */
343 : INLINE GEN
344 2168632 : pol_xn(long n, long v) {
345 2168632 : long i, a = n+2;
346 2168632 : GEN p = cgetg(a+1, t_POL);
347 2168631 : p[1] = evalsigne(1)|evalvarn(v);
348 5101128 : for (i = 2; i < a; i++) gel(p,i) = gen_0;
349 2168631 : gel(p,a) = gen_1; return p;
350 : }
351 : /* x^n, no assumption on n */
352 : INLINE GEN
353 294 : pol_xnall(long n, long v)
354 : {
355 294 : if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
356 287 : return pol_xn(n, v);
357 : }
358 : /* x^n, assume n >= 0 */
359 : INLINE GEN
360 371 : polxn_Flx(long n, long sv) {
361 371 : long i, a = n+2;
362 371 : GEN p = cgetg(a+1, t_VECSMALL);
363 371 : p[1] = sv;
364 1806 : for (i = 2; i < a; i++) p[i] = 0;
365 371 : p[a] = 1; return p;
366 : }
367 : INLINE GEN
368 4448801 : pol_1(long v) {
369 4448801 : GEN p = cgetg(3, t_POL);
370 4448783 : p[1] = evalsigne(1)|evalvarn(v);
371 4448783 : gel(p,2) = gen_1; return p;
372 : }
373 : INLINE GEN
374 34798323 : pol_0(long v)
375 : {
376 34798323 : GEN x = cgetg(2,t_POL);
377 34798309 : x[1] = evalvarn(v); return x;
378 : }
379 : #define retconst_vec(n,x)\
380 : do { long _i, _n = (n);\
381 : GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
382 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
383 : return _v; } while(0)
384 : INLINE GEN
385 224748744 : const_vec(long n, GEN x) { retconst_vec(n, x); }
386 : #define retconst_col(n,x)\
387 : do { long _i, _n = (n);\
388 : GEN _v = cgetg(_n+1, t_COL), _x = (x);\
389 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
390 : return _v; } while(0)
391 : INLINE GEN
392 37150055 : const_col(long n, GEN x) { retconst_col(n, x); }
393 : INLINE GEN
394 17562146 : const_vecsmall(long n, long c)
395 : {
396 : long i;
397 17562146 : GEN V = cgetg(n+1,t_VECSMALL);
398 639048739 : for(i=1;i<=n;i++) V[i] = c;
399 17562219 : return V;
400 : }
401 :
402 : /*** ZERO ***/
403 : /* O(p^e) */
404 : INLINE GEN
405 846663 : zeropadic(GEN p, long e)
406 : {
407 846663 : GEN y = cgetg(5,t_PADIC);
408 846631 : gel(y,4) = gen_0;
409 846631 : gel(y,3) = gen_1;
410 846631 : gel(y,2) = icopy(p);
411 846611 : y[1] = evalvalp(e) | _evalprecp(0);
412 846602 : return y;
413 : }
414 : INLINE GEN
415 10101 : zeropadic_shallow(GEN p, long e)
416 : {
417 10101 : GEN y = cgetg(5,t_PADIC);
418 10101 : gel(y,4) = gen_0;
419 10101 : gel(y,3) = gen_1;
420 10101 : gel(y,2) = p;
421 10101 : y[1] = evalvalp(e) | _evalprecp(0);
422 10100 : return y;
423 : }
424 : /* O(pol_x(v)^e) */
425 : INLINE GEN
426 136360 : zeroser(long v, long e)
427 : {
428 136360 : GEN x = cgetg(2, t_SER);
429 136360 : x[1] = evalvalser(e) | evalvarn(v); return x;
430 : }
431 : INLINE int
432 10042708 : ser_isexactzero(GEN x)
433 : {
434 10042708 : if (!signe(x)) switch(lg(x))
435 : {
436 131194 : case 2: return 1;
437 3899 : case 3: return isexactzero(gel(x,2));
438 : }
439 9907615 : return 0;
440 : }
441 : /* 0 * pol_x(v) */
442 : INLINE GEN
443 14734769 : zeropol(long v) { return pol_0(v); }
444 : /* vector(n) */
445 : INLINE GEN
446 113198546 : zerocol(long n)
447 : {
448 113198546 : GEN y = cgetg(n+1,t_COL);
449 773857318 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
450 113198666 : return y;
451 : }
452 : /* vectorv(n) */
453 : INLINE GEN
454 24871090 : zerovec(long n)
455 : {
456 24871090 : GEN y = cgetg(n+1,t_VEC);
457 337821067 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
458 24871082 : return y;
459 : }
460 : /* matrix(m, n) */
461 : INLINE GEN
462 90592 : zeromat(long m, long n)
463 : {
464 90592 : GEN y = cgetg(n+1,t_MAT);
465 90594 : GEN v = zerocol(m);
466 312444 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
467 90595 : return y;
468 : }
469 : /* = zero_zx, sv is a evalvarn()*/
470 : INLINE GEN
471 1331064 : zero_Flx(long sv) { return pol0_Flx(sv); }
472 : INLINE GEN
473 60360316 : zero_Flv(long n)
474 : {
475 60360316 : GEN y = cgetg(n+1,t_VECSMALL);
476 957314266 : long i; for (i=1; i<=n; i++) y[i] = 0;
477 60360494 : return y;
478 : }
479 : /* matrix(m, n) */
480 : INLINE GEN
481 2711442 : zero_Flm(long m, long n)
482 : {
483 2711442 : GEN y = cgetg(n+1,t_MAT);
484 2711421 : GEN v = zero_Flv(m);
485 17171991 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
486 2711432 : return y;
487 : }
488 : /* matrix(m, n) */
489 : INLINE GEN
490 229388 : zero_Flm_copy(long m, long n)
491 : {
492 229388 : GEN y = cgetg(n+1,t_MAT);
493 2371590 : long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
494 229404 : return y;
495 : }
496 :
497 : INLINE GEN
498 3413789 : zero_F2v(long m)
499 : {
500 3413789 : long l = nbits2nlong(m);
501 3413771 : GEN v = zero_Flv(l+1);
502 3413778 : v[1] = m;
503 3413778 : return v;
504 : }
505 :
506 : INLINE GEN
507 0 : zero_F2m(long m, long n)
508 : {
509 : long i;
510 0 : GEN M = cgetg(n+1, t_MAT);
511 0 : GEN v = zero_F2v(m);
512 0 : for (i = 1; i <= n; i++)
513 0 : gel(M,i) = v;
514 0 : return M;
515 : }
516 :
517 :
518 : INLINE GEN
519 835395 : zero_F2m_copy(long m, long n)
520 : {
521 : long i;
522 835395 : GEN M = cgetg(n+1, t_MAT);
523 1975136 : for (i = 1; i <= n; i++)
524 1139742 : gel(M,i)= zero_F2v(m);
525 835394 : return M;
526 : }
527 :
528 : /* matrix(m, n) */
529 : INLINE GEN
530 13005483 : zeromatcopy(long m, long n)
531 : {
532 13005483 : GEN y = cgetg(n+1,t_MAT);
533 66303356 : long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
534 13005539 : return y;
535 : }
536 :
537 : INLINE GEN
538 24072 : zerovec_block(long len)
539 : {
540 : long i;
541 24072 : GEN blk = cgetg_block(len + 1, t_VEC);
542 794376 : for (i = 1; i <= len; ++i)
543 770304 : gel(blk, i) = gen_0;
544 24072 : return blk;
545 : }
546 :
547 : /* i-th vector in the standard basis */
548 : INLINE GEN
549 5078481 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
550 : INLINE GEN
551 1697437 : vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
552 : INLINE GEN
553 42 : F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
554 : INLINE GEN
555 50224 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
556 : INLINE GEN
557 1213410 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
558 :
559 : INLINE GEN
560 19169542 : shallowcopy(GEN x)
561 19169542 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
562 :
563 : /* routines for naive growarrays */
564 : INLINE GEN
565 11244512 : vectrunc_init(long l)
566 : {
567 11244512 : GEN z = new_chunk(l);
568 11244431 : z[0] = evaltyp(t_VEC) | _evallg(1); return z;
569 : }
570 : INLINE GEN
571 348929 : coltrunc_init(long l)
572 : {
573 348929 : GEN z = new_chunk(l);
574 348929 : z[0] = evaltyp(t_COL) | _evallg(1); return z;
575 : }
576 : INLINE void
577 301729547 : lg_increase(GEN x) { x[0]++; }
578 : INLINE void
579 14077962 : vectrunc_append(GEN x, GEN t) { gel(x, lg(x)) = t; lg_increase(x); }
580 : INLINE void
581 10864 : vectrunc_append_batch(GEN x, GEN y)
582 : {
583 10864 : long i, l = lg(x), ly = lg(y);
584 10864 : GEN z = x + l-1;
585 36358 : for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
586 10864 : setlg(x, l+ly-1);
587 10864 : }
588 : INLINE GEN
589 140262444 : vecsmalltrunc_init(long l)
590 : {
591 140262444 : GEN z = new_chunk(l);
592 140150733 : z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
593 : }
594 : INLINE void
595 62523511 : vecsmalltrunc_append(GEN x, long t) { x[ lg(x) ] = t; lg_increase(x); }
596 :
597 : /*******************************************************************/
598 : /* */
599 : /* STRING HASH FUNCTIONS */
600 : /* */
601 : /*******************************************************************/
602 : INLINE ulong
603 2570882 : hash_str(const char *str)
604 : {
605 2570882 : ulong hash = 5381UL, c;
606 26550018 : while ( (c = (ulong)*str++) )
607 23979136 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
608 2570882 : return hash;
609 : }
610 : INLINE ulong
611 17583038 : hash_str_len(const char *str, long len)
612 : {
613 17583038 : ulong hash = 5381UL;
614 : long i;
615 162013008 : for (i = 0; i < len; i++)
616 : {
617 144429970 : ulong c = (ulong)*str++;
618 144429970 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
619 : }
620 17583038 : return hash;
621 : }
622 :
623 : /*******************************************************************/
624 : /* */
625 : /* VEC / COL / VECSMALL */
626 : /* */
627 : /*******************************************************************/
628 : /* shallow*/
629 : INLINE GEN
630 11543 : vec_shorten(GEN v, long n)
631 : {
632 11543 : GEN V = cgetg(n+1, t_VEC);
633 : long i;
634 12999 : for(i = 1; i <= n; i++) gel(V,i) = gel(v,i);
635 11543 : return V;
636 : }
637 : /* shallow*/
638 : INLINE GEN
639 18927 : vec_lengthen(GEN v, long n)
640 : {
641 18927 : GEN V = cgetg(n+1, t_VEC);
642 18927 : long i, l = lg(v);
643 1935611 : for(i = 1; i < l; i++) gel(V,i) = gel(v,i);
644 18927 : return V;
645 : }
646 : /* shallow*/
647 : INLINE GEN
648 3423911 : vec_append(GEN V, GEN s)
649 : {
650 3423911 : long i, l2 = lg(V);
651 3423911 : GEN res = cgetg(l2+1, typ(V));
652 10719977 : for (i = 1; i < l2; i++) gel(res, i) = gel(V,i);
653 3423915 : gel(res,l2) = s; return res;
654 : }
655 : /* shallow*/
656 : INLINE GEN
657 300491 : vec_prepend(GEN v, GEN s)
658 : {
659 300491 : long i, l = lg(v);
660 300491 : GEN w = cgetg(l+1, typ(v));
661 300492 : gel(w,1) = s;
662 769133 : for (i = 2; i <= l; i++) gel(w,i) = gel(v,i-1);
663 300492 : return w;
664 : }
665 : /* shallow*/
666 : INLINE GEN
667 0 : vec_setconst(GEN v, GEN x)
668 : {
669 0 : long i, l = lg(v);
670 0 : for (i = 1; i < l; i++) gel(v,i) = x;
671 0 : return v;
672 : }
673 : INLINE GEN
674 71415 : vecsmall_shorten(GEN v, long n)
675 : {
676 71415 : GEN V = cgetg(n+1,t_VECSMALL);
677 : long i;
678 878414 : for(i = 1; i <= n; i++) V[i] = v[i];
679 71416 : return V;
680 : }
681 : INLINE GEN
682 490 : vecsmall_lengthen(GEN v, long n)
683 : {
684 490 : long i, l = lg(v);
685 490 : GEN V = cgetg(n+1,t_VECSMALL);
686 25053 : for(i = 1; i < l; i++) V[i] = v[i];
687 490 : return V;
688 : }
689 :
690 : INLINE GEN
691 4010700 : vec_to_vecsmall(GEN x)
692 18847691 : { pari_APPLY_long(itos(gel(x,i))) }
693 : INLINE GEN
694 499217 : vecsmall_to_vec(GEN x)
695 5558901 : { pari_APPLY_type(t_VEC, stoi(x[i])) }
696 : INLINE GEN
697 2464 : vecsmall_to_vec_inplace(GEN z)
698 : {
699 2464 : long i, l = lg(z);
700 77014 : for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
701 2464 : settyp(z, t_VEC); return z;
702 : }
703 : INLINE GEN
704 7621318 : vecsmall_to_col(GEN x)
705 57194163 : { pari_APPLY_type(t_COL, stoi(x[i])) }
706 :
707 : INLINE int
708 7073314 : vecsmall_lexcmp(GEN x, GEN y)
709 : {
710 : long lx,ly,l,i;
711 7073314 : lx = lg(x);
712 7073314 : ly = lg(y); l = minss(lx,ly);
713 30509239 : for (i=1; i<l; i++)
714 28882379 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
715 1626860 : if (lx == ly) return 0;
716 30578 : return (lx < ly)? -1 : 1;
717 : }
718 :
719 : INLINE int
720 108088071 : vecsmall_prefixcmp(GEN x, GEN y)
721 : {
722 108088071 : long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
723 542691324 : for (i=1; i<l; i++)
724 519806035 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
725 22885289 : return 0;
726 : }
727 :
728 : /*Can be used on t_VEC, but coeffs not gcopy-ed*/
729 : INLINE GEN
730 184924 : vecsmall_prepend(GEN V, long s)
731 : {
732 184924 : long i, l2 = lg(V);
733 184924 : GEN res = cgetg(l2+1, typ(V));
734 184924 : res[1] = s;
735 619431 : for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
736 184924 : return res;
737 : }
738 :
739 : INLINE GEN
740 4054738 : vecsmall_append(GEN V, long s)
741 : {
742 4054738 : long i, l2 = lg(V);
743 4054738 : GEN res = cgetg(l2+1, t_VECSMALL);
744 5973313 : for (i = 1; i < l2; ++i) res[i] = V[i];
745 4054738 : res[l2] = s; return res;
746 : }
747 :
748 : INLINE GEN
749 1472936 : vecsmall_concat(GEN u, GEN v)
750 : {
751 1472936 : long i, l1 = lg(u)-1, l2 = lg(v)-1;
752 1472936 : GEN res = cgetg(l1+l2+1, t_VECSMALL);
753 11687824 : for (i = 1; i <= l1; ++i) res[i] = u[i];
754 14974638 : for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
755 1472936 : return res;
756 : }
757 :
758 : /* return the number of indices where u and v are equal */
759 : INLINE long
760 0 : vecsmall_coincidence(GEN u, GEN v)
761 : {
762 0 : long i, s = 0, l = minss(lg(u),lg(v));
763 0 : for(i=1; i<l; i++)
764 0 : if(u[i] == v[i]) s++;
765 0 : return s;
766 : }
767 :
768 : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
769 : INLINE long
770 84 : vecsmall_isin(GEN v, long x)
771 : {
772 84 : long i, l = lg(v);
773 125 : for (i = 1; i < l; i++)
774 82 : if (v[i] == x) return i;
775 43 : return 0;
776 : }
777 :
778 : INLINE long
779 84 : vecsmall_pack(GEN V, long base, long mod)
780 : {
781 84 : long i, s = 0;
782 273 : for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
783 84 : return s;
784 : }
785 :
786 : INLINE long
787 14 : vecsmall_indexmax(GEN x)
788 : {
789 14 : long i, i0 = 1, t = x[1], lx = lg(x);
790 56 : for (i=2; i<lx; i++)
791 42 : if (x[i] > t) t = x[i0=i];
792 14 : return i0;
793 : }
794 :
795 : INLINE long
796 920794 : vecsmall_max(GEN x)
797 : {
798 920794 : long i, t = x[1], lx = lg(x);
799 2533837 : for (i=2; i<lx; i++)
800 1613043 : if (x[i] > t) t = x[i];
801 920794 : return t;
802 : }
803 :
804 : INLINE long
805 14 : vecsmall_indexmin(GEN x)
806 : {
807 14 : long i, i0 = 1, t = x[1], lx =lg(x);
808 56 : for (i=2; i<lx; i++)
809 42 : if (x[i] < t) t = x[i0=i];
810 14 : return i0;
811 : }
812 :
813 : INLINE long
814 1442 : vecsmall_min(GEN x)
815 : {
816 1442 : long i, t = x[1], lx =lg(x);
817 10094 : for (i=2; i<lx; i++)
818 8652 : if (x[i] < t) t = x[i];
819 1442 : return t;
820 : }
821 :
822 : INLINE int
823 24305214 : ZV_isscalar(GEN x)
824 : {
825 24305214 : long l = lg(x);
826 55714578 : while (--l > 1)
827 55362609 : if (signe(gel(x, l))) return 0;
828 351969 : return 1;
829 : }
830 : INLINE int
831 41626671 : QV_isscalar(GEN x)
832 : {
833 41626671 : long lx = lg(x),i;
834 48593785 : for (i=2; i<lx; i++)
835 46304051 : if (!isintzero(gel(x, i))) return 0;
836 2289734 : return 1;
837 : }
838 : INLINE int
839 8196 : RgV_isscalar(GEN x)
840 : {
841 8196 : long lx = lg(x),i;
842 10052 : for (i=2; i<lx; i++)
843 10010 : if (!gequal0(gel(x, i))) return 0;
844 42 : return 1;
845 : }
846 : INLINE int
847 0 : RgX_isscalar(GEN x)
848 : {
849 : long i;
850 0 : for (i=lg(x)-1; i>2; i--)
851 0 : if (!gequal0(gel(x, i))) return 0;
852 0 : return 1;
853 : }
854 : INLINE long
855 25524497 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
856 : INLINE GEN
857 140 : RgX_to_RgV(GEN x, long N) { x = RgX_to_RgC(x, N); settyp(x, t_VEC); return x; }
858 :
859 : INLINE int
860 66772 : RgX_is_rational(GEN x)
861 : {
862 : long i;
863 305104 : for (i = lg(x)-1; i > 1; i--)
864 289781 : if (!is_rational_t(typ(gel(x,i)))) return 0;
865 15323 : return 1;
866 : }
867 : INLINE int
868 13178460 : RgX_is_ZX(GEN x)
869 : {
870 : long i;
871 54964407 : for (i = lg(x)-1; i > 1; i--)
872 41848182 : if (typ(gel(x,i)) != t_INT) return 0;
873 13116225 : return 1;
874 : }
875 : INLINE int
876 361459 : RgX_is_QX(GEN x)
877 : {
878 361459 : long k = lg(x)-1;
879 1372166 : for ( ; k>1; k--)
880 1011050 : if (!is_rational_t(typ(gel(x,k)))) return 0;
881 361116 : return 1;
882 : }
883 : INLINE int
884 2924718 : RgX_is_monomial(GEN x)
885 : {
886 : long i;
887 2924718 : if (!signe(x)) return 0;
888 6890162 : for (i=lg(x)-2; i>1; i--)
889 4531461 : if (!isexactzero(gel(x,i))) return 0;
890 2358701 : return 1;
891 : }
892 : INLINE int
893 31170898 : RgV_is_ZV(GEN x)
894 : {
895 : long i;
896 124193070 : for (i = lg(x)-1; i > 0; i--)
897 93051670 : if (typ(gel(x,i)) != t_INT) return 0;
898 31141400 : return 1;
899 : }
900 : INLINE int
901 110609 : RgV_is_QV(GEN x)
902 : {
903 : long i;
904 529202 : for (i = lg(x)-1; i > 0; i--)
905 421546 : if (!is_rational_t(typ(gel(x,i)))) return 0;
906 107656 : return 1;
907 : }
908 : INLINE long
909 109073 : RgV_isin_i(GEN v, GEN x, long n)
910 : {
911 : long i;
912 1031386 : for (i = 1; i <= n; i++)
913 1025597 : if (gequal(gel(v,i), x)) return i;
914 5789 : return 0;
915 : }
916 : INLINE long
917 109073 : RgV_isin(GEN v, GEN x) { return RgV_isin_i(v, x, lg(v)-1); }
918 :
919 : /********************************************************************/
920 : /** **/
921 : /** Dynamic arrays implementation **/
922 : /** **/
923 : /********************************************************************/
924 : INLINE void **
925 683395545 : pari_stack_base(pari_stack *s) { return s->data; }
926 :
927 : INLINE void
928 5949186 : pari_stack_init(pari_stack *s, size_t size, void **data)
929 : {
930 5949186 : s->data = data;
931 5949186 : *data = NULL;
932 5949186 : s->n = 0;
933 5949186 : s->alloc = 0;
934 5949186 : s->size = size;
935 5949186 : }
936 :
937 : INLINE void
938 677928614 : pari_stack_alloc(pari_stack *s, long nb)
939 : {
940 677928614 : void **sdat = pari_stack_base(s);
941 677867125 : long alloc = s->alloc;
942 677867125 : if (s->n+nb <= alloc) return;
943 2218159 : if (!alloc)
944 2118474 : alloc = nb;
945 : else
946 : {
947 202457 : while (s->n+nb > alloc) alloc <<= 1;
948 : }
949 2218159 : pari_realloc_ip(sdat,alloc*s->size);
950 2221140 : s->alloc = alloc;
951 : }
952 :
953 : INLINE long
954 585283566 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
955 :
956 : INLINE void
957 5486959 : pari_stack_delete(pari_stack *s)
958 : {
959 5486959 : void **sdat = pari_stack_base(s);
960 5473997 : if (*sdat) pari_free(*sdat);
961 5508378 : }
962 :
963 : INLINE void
964 5599 : pari_stack_pushp(pari_stack *s, void *u)
965 : {
966 5599 : long n = pari_stack_new(s);
967 5599 : void **sdat =(void**) *pari_stack_base(s);
968 5599 : sdat[n] = u;
969 5599 : }
970 :
971 : /*******************************************************************/
972 : /* */
973 : /* EXTRACT */
974 : /* */
975 : /*******************************************************************/
976 : INLINE GEN
977 593954662 : vecslice(GEN A, long y1, long y2)
978 : {
979 593954662 : long i,lB = y2 - y1 + 2;
980 593954662 : GEN B = cgetg(lB, typ(A));
981 2654870840 : for (i=1; i<lB; i++) B[i] = A[y1-1+i];
982 593930973 : return B;
983 : }
984 : INLINE GEN
985 2502582 : vecslicepermute(GEN A, GEN p, long y1, long y2)
986 : {
987 2502582 : long i,lB = y2 - y1 + 2;
988 2502582 : GEN B = cgetg(lB, typ(A));
989 28707745 : for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
990 2502578 : return B;
991 : }
992 : /* rowslice(rowpermute(A,p), x1, x2) */
993 : INLINE GEN
994 152709 : rowslicepermute(GEN x, GEN p, long j1, long j2)
995 653900 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
996 :
997 : INLINE GEN
998 76174470 : rowslice(GEN x, long j1, long j2)
999 610319802 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
1000 :
1001 : INLINE GEN
1002 16180087 : matslice(GEN A, long x1, long x2, long y1, long y2)
1003 16180087 : { return rowslice(vecslice(A, y1, y2), x1, x2); }
1004 :
1005 : /* shallow, remove coeff of index j */
1006 : INLINE GEN
1007 210 : rowsplice(GEN x, long j)
1008 1036 : { pari_APPLY_same(vecsplice(gel(x,i), j)) }
1009 :
1010 : /* shallow, remove coeff of index j */
1011 : INLINE GEN
1012 354289 : vecsplice(GEN a, long j)
1013 : {
1014 354289 : long i, k, l = lg(a);
1015 : GEN b;
1016 354289 : if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
1017 354289 : b = cgetg(l-1, typ(a));
1018 1499681 : for (i = k = 1; i < l; i++)
1019 1145383 : if (i != j) gel(b, k++) = gel(a,i);
1020 354298 : return b;
1021 : }
1022 : /* shallow */
1023 : INLINE GEN
1024 1036 : RgM_minor(GEN a, long i, long j)
1025 : {
1026 1036 : GEN b = vecsplice(a, j);
1027 1036 : long k, l = lg(b);
1028 4242 : for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
1029 1036 : return b;
1030 : }
1031 :
1032 : /* A[x0,] */
1033 : INLINE GEN
1034 795812 : row(GEN x, long j)
1035 7719004 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
1036 : INLINE GEN
1037 7789642 : Flm_row(GEN x, long j)
1038 202842994 : { pari_APPLY_ulong((ulong)coeff(x, j, i)) }
1039 : /* A[x0,] */
1040 : INLINE GEN
1041 204988 : rowcopy(GEN x, long j)
1042 2105509 : { pari_APPLY_type(t_VEC, gcopy(gcoeff(x, j, i))) }
1043 : /* A[x0, x1..x2] */
1044 : INLINE GEN
1045 987 : row_i(GEN A, long x0, long x1, long x2)
1046 : {
1047 987 : long i, lB = x2 - x1 + 2;
1048 987 : GEN B = cgetg(lB, t_VEC);
1049 2989 : for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
1050 987 : return B;
1051 : }
1052 :
1053 : INLINE GEN
1054 481642 : vecreverse(GEN A)
1055 : {
1056 : long i, l;
1057 481642 : GEN B = cgetg_copy(A, &l);
1058 1761613 : for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
1059 481642 : return B;
1060 : }
1061 :
1062 : INLINE GEN
1063 3003 : vecsmall_reverse(GEN A)
1064 : {
1065 : long i, l;
1066 3003 : GEN B = cgetg_copy(A, &l);
1067 12523 : for (i=1; i<l; i++) B[i] = A[l-i];
1068 3003 : return B;
1069 : }
1070 :
1071 : INLINE void
1072 2549 : vecreverse_inplace(GEN y)
1073 : {
1074 2549 : long l = lg(y), lim = l>>1, i;
1075 8179 : for (i = 1; i <= lim; i++)
1076 : {
1077 5630 : GEN z = gel(y,i);
1078 5630 : gel(y,i) = gel(y,l-i);
1079 5630 : gel(y,l-i) = z;
1080 : }
1081 2549 : }
1082 :
1083 : INLINE GEN
1084 74859079 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
1085 :
1086 : INLINE GEN
1087 18092752 : vecpermute(GEN A, GEN x)
1088 104452903 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
1089 :
1090 : INLINE GEN
1091 4388141 : veclast(GEN A) { return gel(A, lg(A)-1); }
1092 :
1093 : INLINE GEN
1094 13616531 : rowpermute(GEN x, GEN p)
1095 95717221 : { pari_APPLY_same(typ(gel(x,i)) == t_VECSMALL ? vecsmallpermute(gel(x, i), p)
1096 : : vecpermute(gel(x, i), p))
1097 : }
1098 : /*******************************************************************/
1099 : /* */
1100 : /* PERMUTATIONS */
1101 : /* */
1102 : /*******************************************************************/
1103 : INLINE GEN
1104 2787816 : identity_zv(long n)
1105 : {
1106 2787816 : GEN v = cgetg(n+1, t_VECSMALL);
1107 : long i;
1108 30762070 : for (i = 1; i <= n; i++) v[i] = i;
1109 2787777 : return v;
1110 : }
1111 : INLINE GEN
1112 4249 : identity_ZV(long n)
1113 : {
1114 4249 : GEN v = cgetg(n+1, t_VEC);
1115 : long i;
1116 51569 : for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
1117 4249 : return v;
1118 : }
1119 : /* identity permutation */
1120 : INLINE GEN
1121 2763478 : identity_perm(long n) { return identity_zv(n); }
1122 :
1123 : /* assume d <= n */
1124 : INLINE GEN
1125 98483 : cyclic_perm(long n, long d)
1126 : {
1127 98483 : GEN perm = cgetg(n+1, t_VECSMALL);
1128 : long i;
1129 504098 : for (i = 1; i <= n-d; i++) perm[i] = i+d;
1130 225029 : for ( ; i <= n; i++) perm[i] = i-n+d;
1131 98483 : return perm;
1132 : }
1133 :
1134 : /* Multiply (compose) two permutations */
1135 : INLINE GEN
1136 76846839 : perm_mul(GEN s, GEN x)
1137 949247600 : { pari_APPLY_long(s[x[i]]) }
1138 :
1139 : INLINE GEN
1140 728 : perm_sqr(GEN x)
1141 18284 : { pari_APPLY_long(x[x[i]]) }
1142 :
1143 : /* Compute the inverse (reciprocal) of a permutation. */
1144 : INLINE GEN
1145 2686052 : perm_inv(GEN x)
1146 : {
1147 : long i, lx;
1148 2686052 : GEN y = cgetg_copy(x, &lx);
1149 34467546 : for (i=1; i<lx; i++) y[ x[i] ] = i;
1150 2686039 : return y;
1151 : }
1152 : /* Return s*t*s^-1 */
1153 : INLINE GEN
1154 418299 : perm_conj(GEN s, GEN t)
1155 : {
1156 : long i, l;
1157 418299 : GEN v = cgetg_copy(s, &l);
1158 6857893 : for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
1159 418299 : return v;
1160 : }
1161 :
1162 : INLINE void
1163 438150629 : pari_free(void *pointer)
1164 : {
1165 438150629 : BLOCK_SIGINT_START;
1166 438220500 : free(pointer);
1167 438220500 : BLOCK_SIGINT_END;
1168 438213535 : }
1169 : INLINE void*
1170 666650263 : pari_malloc(size_t size)
1171 : {
1172 666650263 : if (size)
1173 : {
1174 : char *tmp;
1175 666653973 : BLOCK_SIGINT_START;
1176 666719700 : tmp = (char*)malloc(size);
1177 666719700 : BLOCK_SIGINT_END;
1178 666727684 : if (!tmp) pari_err(e_MEM);
1179 666733048 : return tmp;
1180 : }
1181 0 : return NULL;
1182 : }
1183 : INLINE void*
1184 1806 : pari_realloc(void *pointer, size_t size)
1185 : {
1186 : char *tmp;
1187 :
1188 1806 : BLOCK_SIGINT_START;
1189 1806 : if (!pointer) tmp = (char *) malloc(size);
1190 1806 : else tmp = (char *) realloc(pointer,size);
1191 1806 : BLOCK_SIGINT_END;
1192 1806 : if (!tmp) pari_err(e_MEM);
1193 1806 : return tmp;
1194 : }
1195 : INLINE void
1196 2222619 : pari_realloc_ip(void **pointer, size_t size)
1197 : {
1198 : char *tmp;
1199 2222619 : BLOCK_SIGINT_START;
1200 2222377 : if (!*pointer) tmp = (char *) malloc(size);
1201 104802 : else tmp = (char *) realloc(*pointer,size);
1202 2222377 : if (!tmp) pari_err(e_MEM);
1203 2222377 : *pointer = tmp;
1204 2222377 : BLOCK_SIGINT_END;
1205 2221809 : }
1206 :
1207 : INLINE void*
1208 44766 : pari_calloc(size_t size)
1209 : {
1210 44766 : void *t = pari_malloc(size);
1211 44766 : memset(t, 0, size); return t;
1212 : }
1213 : INLINE GEN
1214 8045 : cgetalloc(size_t l, long t)
1215 : { /* evallg may raise e_OVERFLOW, which would leak x */
1216 8045 : ulong x0 = evaltyp(t) | evallg(l);
1217 8045 : GEN x = (GEN)pari_malloc(l * sizeof(long));
1218 8045 : x[0] = x0; return x;
1219 : }
1220 :
1221 : /*******************************************************************/
1222 : /* */
1223 : /* GARBAGE COLLECTION */
1224 : /* */
1225 : /*******************************************************************/
1226 : /* copy integer x as if we had set_avma(av) */
1227 : INLINE GEN
1228 6974140204 : icopy_avma(GEN x, pari_sp av)
1229 : {
1230 6974140204 : long i = lgefint(x), lx = i;
1231 6974140204 : GEN y = ((GEN)av) - i;
1232 50330880285 : while (--i > 0) y[i] = x[i];
1233 6974140204 : y[0] = evaltyp(t_INT)|evallg(lx);
1234 6978296336 : return y;
1235 : }
1236 : /* copy leaf x as if we had set_avma(av) */
1237 : INLINE GEN
1238 539642623 : leafcopy_avma(GEN x, pari_sp av)
1239 : {
1240 539642623 : long i = lg(x);
1241 539642623 : GEN y = ((GEN)av) - i;
1242 3077289424 : while (--i > 0) y[i] = x[i];
1243 539642623 : y[0] = x[0] & (~CLONEBIT);
1244 539642623 : return y;
1245 : }
1246 : INLINE GEN
1247 1026865222 : gerepileuptoleaf(pari_sp av, GEN x)
1248 : {
1249 : long lx;
1250 : GEN q;
1251 :
1252 1026865222 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1253 1025931074 : lx = lg(x);
1254 1025931074 : q = ((GEN)av) - lx;
1255 1025931074 : set_avma((pari_sp)q);
1256 10261593440 : while (--lx >= 0) q[lx] = x[lx];
1257 1025912102 : return q;
1258 : }
1259 : INLINE GEN
1260 3244401432 : gerepileuptoint(pari_sp av, GEN x)
1261 : {
1262 3244401432 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1263 2855904942 : set_avma((pari_sp)icopy_avma(x, av));
1264 2859238286 : return (GEN)avma;
1265 : }
1266 : INLINE GEN
1267 1522565061 : gerepileupto(pari_sp av, GEN x)
1268 : {
1269 1522565061 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1270 1343689720 : switch(typ(x))
1271 : { /* non-default = !is_recursive_t(tq) */
1272 374124232 : case t_INT: return gerepileuptoint(av, x);
1273 307178421 : case t_REAL:
1274 : case t_STR:
1275 307178421 : case t_VECSMALL: return gerepileuptoleaf(av,x);
1276 662389509 : default:
1277 : /* NB: x+i --> ((long)x) + i*sizeof(long) */
1278 662389509 : return gerepile(av, (pari_sp) (x+lg(x)), x);
1279 : }
1280 : }
1281 :
1282 : /* gerepileupto(av, gcopy(x)) */
1283 : INLINE GEN
1284 185584771 : gerepilecopy(pari_sp av, GEN x)
1285 : {
1286 185584771 : if (is_recursive_t(typ(x)))
1287 : {
1288 177245561 : GENbin *p = copy_bin(x);
1289 177264819 : set_avma(av); return bin_copy(p);
1290 : }
1291 : else
1292 : {
1293 8338588 : set_avma(av);
1294 8338623 : if (x < (GEN)av) {
1295 6475417 : if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
1296 6475420 : x = leafcopy_avma(x, av);
1297 6475430 : set_avma((pari_sp)x);
1298 : } else
1299 1863206 : x = leafcopy(x);
1300 8338641 : return x;
1301 : }
1302 : }
1303 :
1304 : INLINE void
1305 49243873 : guncloneNULL(GEN x) { if (x) gunclone(x); }
1306 : INLINE void
1307 1419095 : guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
1308 :
1309 : /* Takes an array of pointers to GENs, of length n. Copies all
1310 : * objects to contiguous locations and cleans up the stack between
1311 : * av and avma. */
1312 : INLINE void
1313 971808 : gerepilemany(pari_sp av, GEN* gptr[], int n)
1314 : {
1315 : int i;
1316 3878368 : for (i=0; i<n; i++) *gptr[i] = (GEN)copy_bin(*gptr[i]);
1317 971303 : set_avma(av);
1318 3879285 : for (i=0; i<n; i++) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1319 972065 : }
1320 :
1321 : INLINE void
1322 18278452 : gerepileall(pari_sp av, int n, ...)
1323 : {
1324 : int i;
1325 18278452 : va_list a; va_start(a, n);
1326 18278452 : if (n < 10)
1327 : {
1328 : GEN *gptr[10];
1329 55055145 : for (i=0; i<n; i++)
1330 36776254 : { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
1331 18278891 : set_avma(av);
1332 55055429 : for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1333 :
1334 : }
1335 : else
1336 : {
1337 0 : GEN **gptr = (GEN**) pari_malloc(n*sizeof(GEN*));
1338 0 : for (i=0; i<n; i++)
1339 0 : { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
1340 0 : set_avma(av);
1341 0 : for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1342 0 : pari_free(gptr);
1343 : }
1344 18279607 : va_end(a);
1345 18279607 : }
1346 :
1347 : /* assume 1 <= n < 10 */
1348 : INLINE GEN
1349 77890166 : gc_all(pari_sp av, int n, ...)
1350 : {
1351 : int i;
1352 : GEN *v[10];
1353 77890166 : va_list a; va_start(a, n);
1354 261893080 : for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
1355 77890480 : set_avma(av);
1356 261893749 : for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
1357 77890612 : return *v[0];
1358 : }
1359 :
1360 : INLINE void
1361 2159346 : gerepilecoeffs(pari_sp av, GEN x, int n)
1362 : {
1363 : int i;
1364 26098181 : for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
1365 2159775 : set_avma(av);
1366 26099544 : for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
1367 2159531 : }
1368 :
1369 : /* p from copy_bin. Copy p->x back to stack, then destroy p */
1370 : INLINE GEN
1371 424925151 : bin_copy(GENbin *p)
1372 : {
1373 : GEN x, y, base;
1374 : long dx, len;
1375 :
1376 424925151 : x = p->x; if (!x) { pari_free(p); return gen_0; }
1377 399792781 : len = p->len;
1378 399792781 : base= p->base; dx = x - base;
1379 399792781 : y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
1380 399783718 : y += dx;
1381 399783718 : p->rebase(y, ((ulong)y-(ulong)x));
1382 399792582 : pari_free(p); return y;
1383 : }
1384 :
1385 : INLINE GEN
1386 824713373 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
1387 :
1388 : INLINE void
1389 118792648 : cgiv(GEN x)
1390 : {
1391 118792648 : pari_sp av = (pari_sp)(x+lg(x));
1392 118792648 : if (isonstack((GEN)av)) set_avma(av);
1393 118795263 : }
1394 :
1395 : INLINE void
1396 1794291 : killblock(GEN x) { gunclone(x); }
1397 :
1398 : INLINE int
1399 288466439 : is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
1400 :
1401 : /*******************************************************************/
1402 : /* */
1403 : /* CONVERSION / ASSIGNMENT */
1404 : /* */
1405 : /*******************************************************************/
1406 : /* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
1407 : INLINE GEN
1408 16904806 : cxcompotor(GEN z, long prec)
1409 : {
1410 16904806 : switch(typ(z))
1411 : {
1412 11126948 : case t_INT: return itor(z, prec);
1413 289877 : case t_FRAC: return fractor(z, prec);
1414 5488200 : case t_REAL: return rtor(z, prec);
1415 0 : default: pari_err_TYPE("cxcompotor",z);
1416 : return NULL; /* LCOV_EXCL_LINE */
1417 : }
1418 : }
1419 : INLINE GEN
1420 8436772 : cxtofp(GEN x, long prec)
1421 8436772 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
1422 :
1423 : INLINE GEN
1424 275775 : cxtoreal(GEN q)
1425 275775 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
1426 :
1427 : INLINE double
1428 56508431 : gtodouble(GEN x)
1429 : {
1430 56508431 : if (typ(x)!=t_REAL) {
1431 6382481 : pari_sp av = avma;
1432 6382481 : x = gtofp(x, DEFAULTPREC);
1433 6382000 : if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
1434 6382000 : set_avma(av);
1435 : }
1436 56507883 : return rtodbl(x);
1437 : }
1438 :
1439 : INLINE int
1440 2622642 : gisdouble(GEN x, double *g)
1441 : {
1442 2622642 : if (typ(x)!=t_REAL) {
1443 56662 : pari_sp av = avma;
1444 56662 : x = gtofp(x, DEFAULTPREC);
1445 56662 : if (typ(x)!=t_REAL) pari_err_TYPE("gisdouble [t_REAL expected]", x);
1446 56662 : set_avma(av);
1447 : }
1448 2622640 : if (expo(x) >= 0x3ff) return 0;
1449 2622640 : *g = rtodbl(x); return 1;
1450 : }
1451 :
1452 : INLINE long
1453 85688490 : gtos(GEN x) {
1454 85688490 : if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
1455 85688476 : return itos(x);
1456 : }
1457 :
1458 : INLINE ulong
1459 106321 : gtou(GEN x) {
1460 106321 : if (typ(x) != t_INT || signe(x)<0)
1461 7 : pari_err_TYPE("gtou [integer >=0 expected]",x);
1462 106315 : return itou(x);
1463 : }
1464 :
1465 : INLINE GEN
1466 41531865 : absfrac(GEN x)
1467 : {
1468 41531865 : GEN y = cgetg(3, t_FRAC);
1469 41531897 : gel(y,1) = absi(gel(x,1));
1470 41531912 : gel(y,2) = icopy(gel(x,2)); return y;
1471 : }
1472 : INLINE GEN
1473 46150 : absfrac_shallow(GEN x)
1474 46150 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
1475 : INLINE GEN
1476 7158287 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
1477 : INLINE GEN
1478 166748 : Q_abs_shallow(GEN x)
1479 166748 : { return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
1480 : INLINE GEN
1481 12971 : R_abs_shallow(GEN x)
1482 12971 : { return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
1483 : INLINE GEN
1484 0 : R_abs(GEN x)
1485 0 : { return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
1486 :
1487 : /* Force z to be of type real/complex with floating point components */
1488 : INLINE GEN
1489 222858355 : gtofp(GEN z, long prec)
1490 : {
1491 222858355 : switch(typ(z))
1492 : {
1493 190045934 : case t_INT: return itor(z, prec);
1494 4093372 : case t_FRAC: return fractor(z, prec);
1495 20546187 : case t_REAL: return rtor(z, prec);
1496 8178986 : case t_COMPLEX: {
1497 8178986 : GEN a = gel(z,1), b = gel(z,2);
1498 8178986 : if (isintzero(b)) return cxcompotor(a, prec);
1499 8179005 : if (isintzero(a)) {
1500 8905 : GEN y = cgetg(3, t_COMPLEX);
1501 8905 : b = cxcompotor(b, prec);
1502 8905 : gel(y,1) = real_0_bit(expo(b) - prec2nbits(prec));
1503 8905 : gel(y,2) = b; return y;
1504 : }
1505 8170065 : return cxtofp(z, prec);
1506 : }
1507 0 : case t_QUAD: return quadtofp(z, prec);
1508 1 : default: pari_err_TYPE("gtofp",z);
1509 : return NULL; /* LCOV_EXCL_LINE */
1510 : }
1511 : }
1512 : /* Force z to be of type real / int */
1513 : INLINE GEN
1514 22428 : gtomp(GEN z, long prec)
1515 : {
1516 22428 : switch(typ(z))
1517 : {
1518 42 : case t_INT: return z;
1519 22386 : case t_FRAC: return fractor(z, prec);
1520 0 : case t_REAL: return rtor(z, prec);
1521 0 : case t_QUAD: z = quadtofp(z, prec);
1522 0 : if (typ(z) == t_REAL) return z;
1523 0 : default: pari_err_TYPE("gtomp",z);
1524 : return NULL; /* LCOV_EXCL_LINE */
1525 : }
1526 : }
1527 :
1528 : INLINE GEN
1529 4619318 : RgX_gtofp(GEN x, long prec)
1530 : {
1531 : long l;
1532 4619318 : GEN y = cgetg_copy(x, &l);
1533 26131397 : while (--l > 1) gel(y,l) = gtofp(gel(x,l), prec);
1534 4618127 : y[1] = x[1]; return y;
1535 : }
1536 : INLINE GEN
1537 30323794 : RgC_gtofp(GEN x, long prec)
1538 199387308 : { pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
1539 :
1540 : INLINE GEN
1541 56 : RgV_gtofp(GEN x, long prec)
1542 4781 : { pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
1543 :
1544 : INLINE GEN
1545 7236479 : RgM_gtofp(GEN x, long prec)
1546 36932423 : { pari_APPLY_same(RgC_gtofp(gel(x,i), prec)) }
1547 :
1548 : INLINE GEN
1549 574 : RgC_gtomp(GEN x, long prec)
1550 23002 : { pari_APPLY_type(t_COL, gtomp(gel(x,i), prec)) }
1551 :
1552 : INLINE GEN
1553 21 : RgM_gtomp(GEN x, long prec)
1554 595 : { pari_APPLY_same(RgC_gtomp(gel(x,i), prec)) }
1555 :
1556 : INLINE GEN
1557 59829 : RgX_fpnorml2(GEN x, long prec)
1558 : {
1559 59829 : pari_sp av = avma;
1560 59829 : return gerepileupto(av, gnorml2(RgX_gtofp(x, prec)));
1561 : }
1562 : INLINE GEN
1563 616624 : RgC_fpnorml2(GEN x, long prec)
1564 : {
1565 616624 : pari_sp av = avma;
1566 616624 : return gerepileupto(av, gnorml2(RgC_gtofp(x, prec)));
1567 : }
1568 : INLINE GEN
1569 21569 : RgM_fpnorml2(GEN x, long prec)
1570 : {
1571 21569 : pari_sp av = avma;
1572 21569 : return gerepileupto(av, gnorml2(RgM_gtofp(x, prec)));
1573 : }
1574 :
1575 : /* y a t_REAL */
1576 : INLINE void
1577 220528 : affgr(GEN x, GEN y)
1578 : {
1579 : pari_sp av;
1580 220528 : switch(typ(x)) {
1581 5992 : case t_INT: affir(x,y); break;
1582 214536 : case t_REAL: affrr(x,y); break;
1583 0 : case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
1584 0 : case t_QUAD: av = avma; affgr(quadtofp(x,realprec(y)), y); set_avma(av); break;
1585 0 : default: pari_err_TYPE2("=",x,y);
1586 : }
1587 220528 : }
1588 :
1589 : INLINE GEN
1590 245321 : affc_fixlg(GEN x, GEN res)
1591 : {
1592 245321 : if (typ(x) == t_COMPLEX)
1593 : {
1594 212863 : affrr_fixlg(gel(x,1), gel(res,1));
1595 212863 : affrr_fixlg(gel(x,2), gel(res,2));
1596 : }
1597 : else
1598 : {
1599 32458 : set_avma((pari_sp)(res+3));
1600 32457 : res = cgetr(realprec(gel(res,1)));
1601 32462 : affrr_fixlg(x, res);
1602 : }
1603 245321 : return res;
1604 : }
1605 :
1606 : INLINE GEN
1607 0 : trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
1608 :
1609 : /*******************************************************************/
1610 : /* */
1611 : /* LENGTH CONVERSIONS */
1612 : /* */
1613 : /*******************************************************************/
1614 : INLINE long
1615 39743 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
1616 : INLINE long
1617 31501 : ndec2prec(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1618 : INLINE long
1619 8242 : ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1620 : /* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
1621 : * to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
1622 : * addl, shrl instead of subl, sarl, addl */
1623 : INLINE long
1624 12077083 : nbits2nlong(long x) {
1625 12077083 : return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1626 : }
1627 :
1628 : INLINE long
1629 1134672666 : nbits2extraprec(long x) {
1630 1134672666 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1631 : }
1632 :
1633 : INLINE long
1634 138668420 : nbits2prec(long x) {
1635 138668420 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1636 : }
1637 : INLINE long
1638 2114445965 : nbits2lg(long x) {
1639 2114445965 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1640 : }
1641 : INLINE long
1642 3838082849 : prec2lg(long x) {
1643 3838082849 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1644 : }
1645 : INLINE long
1646 712695950 : lg2prec(long x){ return (x-2) * BITS_IN_LONG; }
1647 :
1648 : /* ceil(x / sizeof(long)) */
1649 : INLINE long
1650 84455737 : nchar2nlong(long x) {
1651 84455737 : return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
1652 : }
1653 : INLINE long
1654 226849151 : prec2nbits(long x) { return x; }
1655 : INLINE double
1656 2871314 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
1657 : INLINE double
1658 590471 : prec2nbits_mul(long x, double y) { return x * y; }
1659 : INLINE long
1660 124757539 : bit_prec(GEN x) { return prec2nbits(realprec(x)); }
1661 : INLINE long
1662 2609213738 : bit_accuracy(long x) { return (x-2) * BITS_IN_LONG; }
1663 : INLINE long
1664 9515 : prec2ndec(long x) { return (long)prec2nbits_mul(x, LOG10_2); }
1665 : INLINE long
1666 214 : nbits2ndec(long x) { return (long)(x * LOG10_2); }
1667 : INLINE long
1668 4636 : precdbl(long x) {return x << 1;}
1669 : INLINE long
1670 7058878400 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
1671 : INLINE long
1672 6972311219 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
1673 :
1674 : /*********************************************************************/
1675 : /** **/
1676 : /** OPERATIONS MODULO m **/
1677 : /** **/
1678 : /*********************************************************************/
1679 : /* Assume m > 0, more efficient if 0 <= a, b < m */
1680 :
1681 : INLINE GEN
1682 46541810 : Fp_red(GEN a, GEN m) { return modii(a, m); }
1683 : INLINE GEN
1684 168819899 : Fp_add(GEN a, GEN b, GEN m)
1685 : {
1686 168819899 : pari_sp av=avma;
1687 168819899 : GEN p = addii(a,b);
1688 167299859 : long s = signe(p);
1689 167299859 : if (!s) return p; /* = gen_0 */
1690 156158414 : if (s > 0) /* general case */
1691 : {
1692 156173823 : GEN t = subii(p, m);
1693 155819172 : s = signe(t);
1694 155819172 : if (!s) return gc_const(av, gen_0);
1695 148329176 : if (s < 0) return gc_const((pari_sp)p, p);
1696 72031808 : if (cmpii(t, m) < 0) return gerepileuptoint(av, t); /* general case ! */
1697 3311370 : p = remii(t, m);
1698 : }
1699 : else
1700 265343 : p = modii(p, m);
1701 3576829 : return gerepileuptoint(av, p);
1702 : }
1703 : INLINE GEN
1704 20584832 : Fp_double(GEN x, GEN N)
1705 : {
1706 20584832 : GEN z = shifti(x, 1);
1707 19809241 : return cmpii(z, N) >= 0? subii(z, N): z;
1708 : }
1709 : INLINE GEN
1710 135088464 : Fp_sub(GEN a, GEN b, GEN m)
1711 : {
1712 135088464 : pari_sp av=avma;
1713 135088464 : GEN p = subii(a,b);
1714 133140518 : long s = signe(p);
1715 133140518 : if (!s) return p; /* = gen_0 */
1716 123758739 : if (s > 0)
1717 : {
1718 62341439 : if (cmpii(p, m) < 0) return p; /* general case ! */
1719 1330923 : p = remii(p, m);
1720 : }
1721 : else
1722 : {
1723 61417300 : GEN t = addii(p, m);
1724 63176733 : if (!s) return gc_const(av, gen_0);
1725 63240725 : if (s > 0) return gerepileuptoint(av, t); /* general case ! */
1726 63240725 : p = modii(t, m);
1727 : }
1728 64906984 : return gerepileuptoint(av, p);
1729 : }
1730 : INLINE GEN
1731 26322665 : Fp_neg(GEN b, GEN m)
1732 : {
1733 26322665 : pari_sp av = avma;
1734 26322665 : long s = signe(b);
1735 : GEN p;
1736 26322665 : if (!s) return gen_0;
1737 22572923 : if (s > 0)
1738 : {
1739 21452317 : p = subii(m, b);
1740 21450281 : if (signe(p) >= 0) return p; /* general case ! */
1741 384332 : p = modii(p, m);
1742 : } else
1743 1121387 : p = remii(negi(b), m);
1744 1505905 : return gerepileuptoint(av, p);
1745 : }
1746 :
1747 : INLINE GEN
1748 252539 : Fp_halve(GEN a, GEN p)
1749 : {
1750 252539 : if (mpodd(a)) a = addii(a,p);
1751 252539 : return shifti(a,-1);
1752 : }
1753 :
1754 : /* assume 0 <= u < p and ps2 = p>>1 */
1755 : INLINE GEN
1756 74188452 : Fp_center(GEN u, GEN p, GEN ps2)
1757 74188452 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
1758 : /* same without copy */
1759 : INLINE GEN
1760 13221274 : Fp_center_i(GEN u, GEN p, GEN ps2)
1761 13221274 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
1762 :
1763 : /* x + y*z mod p */
1764 : INLINE GEN
1765 11136378 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
1766 : {
1767 : pari_sp av;
1768 11136378 : if (!signe(y) || !signe(z)) return Fp_red(x, p);
1769 10807396 : if (!signe(x)) return Fp_mul(z,y, p);
1770 9815237 : av = avma;
1771 9815237 : return gerepileuptoint(av, modii(addii(x, mulii(y,z)), p));
1772 : }
1773 :
1774 : INLINE GEN
1775 173982474 : Fp_mul(GEN a, GEN b, GEN m)
1776 : {
1777 173982474 : pari_sp av=avma;
1778 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1779 173982474 : (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
1780 173976889 : p = mulii(a,b);
1781 173990485 : set_avma(av); return modii(p,m);
1782 : }
1783 : INLINE GEN
1784 62240794 : Fp_sqr(GEN a, GEN m)
1785 : {
1786 62240794 : pari_sp av=avma;
1787 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1788 62240794 : (void)new_chunk((lg(a)+lg(m))<<1);
1789 62459037 : p = sqri(a);
1790 60065436 : set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
1791 : }
1792 : INLINE GEN
1793 65141143 : Fp_mulu(GEN a, ulong b, GEN m)
1794 : {
1795 65141143 : long l = lgefint(m);
1796 65141143 : if (l == 3)
1797 : {
1798 42647095 : ulong mm = m[2];
1799 42647095 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1800 : } else {
1801 22494048 : pari_sp av = avma;
1802 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1803 22494048 : (void)new_chunk(lg(a)+1+(l<<1));
1804 22493996 : p = muliu(a,b);
1805 22045324 : set_avma(av); return modii(p,m);
1806 : }
1807 : }
1808 : INLINE GEN
1809 17780 : Fp_muls(GEN a, long b, GEN m)
1810 : {
1811 17780 : long l = lgefint(m);
1812 17780 : if (l == 3)
1813 : {
1814 3578 : ulong mm = m[2];
1815 3578 : if (b < 0)
1816 : {
1817 3578 : ulong t = Fl_mul(umodiu(a, mm), -b, mm);
1818 3578 : return t? utoipos(mm - t): gen_0;
1819 : }
1820 : else
1821 0 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1822 : } else {
1823 14202 : pari_sp av = avma;
1824 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1825 14202 : (void)new_chunk(lg(a)+1+(l<<1));
1826 14202 : p = mulis(a,b);
1827 14202 : set_avma(av); return modii(p,m);
1828 : }
1829 : }
1830 :
1831 : INLINE GEN
1832 25005705 : Fp_inv(GEN a, GEN m)
1833 : {
1834 : GEN res;
1835 25005705 : if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
1836 25004947 : return res;
1837 : }
1838 : INLINE GEN
1839 1182476 : Fp_invsafe(GEN a, GEN m)
1840 : {
1841 : GEN res;
1842 1182476 : if (! invmod(a,m,&res)) return NULL;
1843 1182404 : return res;
1844 : }
1845 : INLINE GEN
1846 1462261 : Fp_div(GEN a, GEN b, GEN m)
1847 : {
1848 1462261 : pari_sp av = avma;
1849 : GEN p;
1850 1462261 : if (lgefint(b) == 3)
1851 : {
1852 507840 : a = Fp_divu(a, b[2], m);
1853 507840 : if (signe(b) < 0) a = Fp_neg(a, m);
1854 507840 : return a;
1855 : }
1856 : /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1857 954421 : (void)new_chunk(lg(a)+(lg(m)<<1));
1858 954421 : p = mulii(a, Fp_inv(b,m));
1859 954421 : set_avma(av); return modii(p,m);
1860 : }
1861 : INLINE GEN
1862 1754874 : Fp_divu(GEN x, ulong a, GEN p)
1863 : {
1864 1754874 : pari_sp av = avma;
1865 : ulong b;
1866 1754874 : if (lgefint(p) == 3)
1867 : {
1868 1356848 : ulong pp = p[2], xp = umodiu(x, pp);
1869 1356848 : return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
1870 : }
1871 398026 : x = Fp_red(x, p);
1872 398027 : b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
1873 398037 : return gerepileuptoint(av, diviuexact(addmuliu(x, p, b), a));
1874 : }
1875 :
1876 : INLINE GEN
1877 1085666 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
1878 :
1879 : INLINE GEN
1880 2517025 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1881 :
1882 : INLINE long
1883 2472272 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1884 :
1885 : INLINE long
1886 1934244 : get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
1887 :
1888 : INLINE GEN
1889 399 : get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1890 :
1891 : INLINE long
1892 376978 : get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1893 :
1894 : INLINE long
1895 201663 : get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1896 :
1897 : INLINE GEN
1898 25427283 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1899 :
1900 : INLINE long
1901 66740912 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1902 :
1903 : INLINE long
1904 84746347 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1905 :
1906 : INLINE GEN
1907 8061 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1908 :
1909 : INLINE long
1910 262625 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1911 :
1912 : INLINE long
1913 315862 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1914 :
1915 : INLINE GEN
1916 3471541 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1917 :
1918 : INLINE long
1919 6977084 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1920 :
1921 : INLINE long
1922 5803136 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1923 :
1924 : INLINE GEN
1925 146583 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1926 :
1927 : INLINE long
1928 51837 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1929 :
1930 : INLINE long
1931 3686 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1932 :
1933 : /*******************************************************************/
1934 : /* */
1935 : /* ADDMULII / SUBMULII */
1936 : /* */
1937 : /*******************************************************************/
1938 : /* x - y*z */
1939 : INLINE GEN
1940 32957049 : submulii(GEN x, GEN y, GEN z)
1941 : {
1942 32957049 : long lx = lgefint(x), ly, lz;
1943 : pari_sp av;
1944 : GEN t;
1945 32957049 : if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
1946 30319577 : ly = lgefint(y);
1947 30319577 : if (ly == 2) return icopy(x);
1948 29042113 : lz = lgefint(z);
1949 29042113 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1950 29042113 : t = mulii(z, y);
1951 29042113 : set_avma(av); return subii(x,t);
1952 : }
1953 : /* y*z - x */
1954 : INLINE GEN
1955 3541533 : mulsubii(GEN y, GEN z, GEN x)
1956 : {
1957 3541533 : long lx = lgefint(x), ly, lz;
1958 : pari_sp av;
1959 : GEN t;
1960 3541533 : if (lx == 2) return mulii(z,y);
1961 2267532 : ly = lgefint(y);
1962 2267532 : if (ly == 2) return negi(x);
1963 2058452 : lz = lgefint(z);
1964 2058452 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1965 2058492 : t = mulii(z, y);
1966 2058438 : set_avma(av); return subii(t,x);
1967 : }
1968 :
1969 : /* x - u*y */
1970 : INLINE GEN
1971 7693 : submuliu(GEN x, GEN y, ulong u)
1972 : {
1973 : pari_sp av;
1974 7693 : long ly = lgefint(y);
1975 7693 : if (ly == 2) return icopy(x);
1976 7693 : av = avma;
1977 7693 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1978 7693 : y = mului(u,y);
1979 7693 : set_avma(av); return subii(x, y);
1980 : }
1981 : /* x + u*y */
1982 : INLINE GEN
1983 406105 : addmuliu(GEN x, GEN y, ulong u)
1984 : {
1985 : pari_sp av;
1986 406105 : long ly = lgefint(y);
1987 406105 : if (ly == 2) return icopy(x);
1988 406105 : av = avma;
1989 406105 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1990 406105 : y = mului(u,y);
1991 406095 : set_avma(av); return addii(x, y);
1992 : }
1993 : /* x - u*y */
1994 : INLINE GEN
1995 60536539 : submuliu_inplace(GEN x, GEN y, ulong u)
1996 : {
1997 : pari_sp av;
1998 60536539 : long ly = lgefint(y);
1999 60536539 : if (ly == 2) return x;
2000 38273832 : av = avma;
2001 38273832 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
2002 38277330 : y = mului(u,y);
2003 38275640 : set_avma(av); return subii(x, y);
2004 : }
2005 : /* x + u*y */
2006 : INLINE GEN
2007 60044740 : addmuliu_inplace(GEN x, GEN y, ulong u)
2008 : {
2009 : pari_sp av;
2010 60044740 : long ly = lgefint(y);
2011 60044740 : if (ly == 2) return x;
2012 38271401 : av = avma;
2013 38271401 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
2014 38275235 : y = mului(u,y);
2015 38274027 : set_avma(av); return addii(x, y);
2016 : }
2017 : /* ux + vy */
2018 : INLINE GEN
2019 36631114 : lincombii(GEN u, GEN v, GEN x, GEN y)
2020 : {
2021 36631114 : long lx = lgefint(x), ly;
2022 : GEN p1, p2;
2023 : pari_sp av;
2024 36631114 : if (lx == 2) return mulii(v,y);
2025 23083211 : ly = lgefint(y);
2026 23083211 : if (ly == 2) return mulii(u,x);
2027 20629756 : av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
2028 20633395 : p1 = mulii(u,x);
2029 20631322 : p2 = mulii(v,y);
2030 20631288 : set_avma(av); return addii(p1,p2);
2031 : }
2032 :
2033 : /*******************************************************************/
2034 : /* */
2035 : /* GEN SUBTYPES */
2036 : /* */
2037 : /*******************************************************************/
2038 :
2039 : INLINE int
2040 4577343972 : is_const_t(long t) { return (t < t_POLMOD); }
2041 : INLINE int
2042 6932 : is_extscalar_t(long t) { return (t <= t_POL); }
2043 : INLINE int
2044 7900839 : is_intreal_t(long t) { return (t <= t_REAL); }
2045 : INLINE int
2046 585609528 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
2047 : INLINE int
2048 92936580 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
2049 : INLINE int
2050 0 : is_qfb_t(long t) { return (t == t_QFB); }
2051 : INLINE int
2052 4035404 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
2053 : INLINE int
2054 62038356 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
2055 : INLINE int
2056 7535128959 : is_recursive_t(long t) { return lontyp[t]; }
2057 : INLINE int
2058 297723118 : is_scalar_t(long t) { return (t < t_POL); }
2059 : INLINE int
2060 1709017 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
2061 :
2062 : INLINE int
2063 58914653 : qfb_is_qfi(GEN q) { return signe(gel(q,4)) < 0; }
2064 :
2065 : /*******************************************************************/
2066 : /* */
2067 : /* TRANSCENDENTAL */
2068 : /* */
2069 : /*******************************************************************/
2070 : INLINE GEN
2071 65241103 : sqrtr(GEN x) {
2072 65241103 : long s = signe(x);
2073 65241103 : if (s == 0) return real_0_bit(expo(x) >> 1);
2074 65207959 : if (s >= 0) return sqrtr_abs(x);
2075 258722 : retmkcomplex(gen_0, sqrtr_abs(x));
2076 : }
2077 : INLINE GEN
2078 0 : cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
2079 : INLINE GEN
2080 0 : cbrtr(GEN x) {
2081 0 : long s = signe(x);
2082 : GEN r;
2083 0 : if (s == 0) return real_0_bit(expo(x) / 3);
2084 0 : r = cbrtr_abs(x);
2085 0 : if (s < 0) togglesign(r);
2086 0 : return r;
2087 : }
2088 : INLINE GEN
2089 2478839 : sqrtnr(GEN x, long n) {
2090 2478839 : long s = signe(x);
2091 : GEN r;
2092 2478839 : if (s == 0) return real_0_bit(expo(x) / n);
2093 2478839 : r = sqrtnr_abs(x, n);
2094 2478869 : if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
2095 2478869 : return r;
2096 : }
2097 : INLINE long
2098 742546 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
2099 : INLINE ulong
2100 1786995 : ulogint(ulong B, ulong y)
2101 : {
2102 : ulong r;
2103 : long e;
2104 1786995 : if (y == 2) return expu(B);
2105 1738071 : r = y;
2106 5296776 : for (e=1;; e++)
2107 : { /* here, r = y^e, r2 = y^(e-1) */
2108 5296776 : if (r >= B) return r == B? e: e-1;
2109 3560213 : r = umuluu_or_0(y, r);
2110 3560258 : if (!r) return e;
2111 : }
2112 : }
2113 :
2114 : /*******************************************************************/
2115 : /* */
2116 : /* MISCELLANEOUS */
2117 : /* */
2118 : /*******************************************************************/
2119 7900761 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
2120 2645732710 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
2121 14846620 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
2122 4564901 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
2123 1078084656 : INLINE int equali1(GEN n)
2124 1078084656 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
2125 137922733 : INLINE int equalim1(GEN n)
2126 137922733 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
2127 : /* works only for POSITIVE integers */
2128 2183220406 : INLINE int is_pm1(GEN n)
2129 2183220406 : { return lgefint(n) == 3 && n[2] == 1; }
2130 503139543 : INLINE int is_bigint(GEN n)
2131 503139543 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
2132 :
2133 1996420736 : INLINE int odd(long x) { return x & 1; }
2134 66397250 : INLINE int both_odd(long x, long y) { return x & y & 1; }
2135 :
2136 : INLINE int
2137 6531733755 : isonstack(GEN x)
2138 6531733755 : { return ((pari_sp)x >= pari_mainstack->bot
2139 6531733755 : && (pari_sp)x < pari_mainstack->top); }
2140 :
2141 : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
2142 : INLINE double
2143 66626338 : dbllog2r(GEN x)
2144 66626338 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
2145 :
2146 : INLINE GEN
2147 1343551 : mul_content(GEN cx, GEN cy)
2148 : {
2149 1343551 : if (!cx) return cy;
2150 529226 : if (!cy) return cx;
2151 338102 : return gmul(cx,cy);
2152 : }
2153 : INLINE GEN
2154 0 : inv_content(GEN c) { return c? ginv(c): NULL; }
2155 : INLINE GEN
2156 37781 : div_content(GEN cx, GEN cy)
2157 : {
2158 37781 : if (!cy) return cx;
2159 36689 : if (!cx) return ginv(cy);
2160 7419 : return gdiv(cx,cy);
2161 : }
2162 : INLINE GEN
2163 5917918 : mul_denom(GEN dx, GEN dy)
2164 : {
2165 5917918 : if (!dx) return dy;
2166 1703826 : if (!dy) return dx;
2167 694719 : return mulii(dx,dy);
2168 : }
2169 :
2170 : /* POLYNOMIALS */
2171 : INLINE GEN
2172 1371620 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
2173 : INLINE GEN
2174 160497173 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
2175 : INLINE ulong
2176 761374 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
2177 : INLINE ulong
2178 137782 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
2179 : INLINE long
2180 4763592754 : degpol(GEN x) { return lg(x)-3; }
2181 : INLINE long
2182 2431209711 : lgpol(GEN x) { return lg(x)-2; }
2183 : INLINE long
2184 328812024 : lgcols(GEN x) { return lg(gel(x,1)); }
2185 : INLINE long
2186 143557701 : nbrows(GEN x) { return lg(gel(x,1))-1; }
2187 : INLINE GEN
2188 0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
2189 :
2190 : INLINE GEN
2191 1677546 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
2192 : INLINE GEN
2193 626427 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
2194 :
2195 : INLINE GEN
2196 97877234 : RgX_copy(GEN x)
2197 : {
2198 : long lx, i;
2199 97877234 : GEN y = cgetg_copy(x, &lx); y[1] = x[1];
2200 356175517 : for (i = 2; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
2201 97877288 : return y;
2202 : }
2203 : /* have to use ulong to avoid silly warnings from gcc "assuming signed
2204 : * overflow does not occur" */
2205 : INLINE GEN
2206 2517576 : RgX_coeff(GEN x, long n)
2207 : {
2208 2517576 : ulong l = lg(x);
2209 2517576 : return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
2210 : }
2211 : INLINE GEN
2212 347497 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
2213 : INLINE GEN
2214 6916324 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
2215 : INLINE GEN
2216 1904 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
2217 : INLINE GEN
2218 107442 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
2219 : INLINE GEN
2220 5122647 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
2221 : INLINE GEN
2222 1986999 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
2223 : INLINE GEN
2224 21097825 : Flx_div_pre(GEN x, GEN y, ulong p, ulong pi)
2225 21097825 : { return Flx_divrem_pre(x,y,p,pi, NULL); }
2226 : INLINE GEN
2227 1258255 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
2228 : INLINE GEN
2229 0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
2230 : INLINE GEN
2231 97878159 : pol0_Flx(long sv) { return mkvecsmall(sv); }
2232 : INLINE GEN
2233 54807214 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
2234 : INLINE GEN
2235 35594662 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
2236 : INLINE GEN
2237 0 : zero_zx(long sv) { return zero_Flx(sv); }
2238 : INLINE GEN
2239 0 : polx_zx(long sv) { return polx_Flx(sv); }
2240 : INLINE GEN
2241 0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
2242 : INLINE GEN
2243 14042 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
2244 : INLINE GEN
2245 1350 : zero_F2x(long sv) { return zero_Flx(sv); }
2246 : INLINE GEN
2247 13106787 : pol0_F2x(long sv) { return pol0_Flx(sv); }
2248 : INLINE GEN
2249 3910576 : pol1_F2x(long sv) { return pol1_Flx(sv); }
2250 : INLINE GEN
2251 1005665 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
2252 : INLINE int
2253 2867395 : F2x_equal1(GEN x) { return Flx_equal1(x); }
2254 : INLINE int
2255 4614280 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
2256 : INLINE GEN
2257 59918843 : F2x_copy(GEN x) { return leafcopy(x); }
2258 : INLINE GEN
2259 4074 : F2v_copy(GEN x) { return leafcopy(x); }
2260 : INLINE GEN
2261 2376538 : Flv_copy(GEN x) { return leafcopy(x); }
2262 : INLINE GEN
2263 169739892 : Flx_copy(GEN x) { return leafcopy(x); }
2264 : INLINE GEN
2265 3956359 : vecsmall_copy(GEN x) { return leafcopy(x); }
2266 : INLINE int
2267 9961429 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
2268 : INLINE int
2269 14136 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
2270 : INLINE int
2271 6041092 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
2272 :
2273 : INLINE GEN
2274 143440783 : ZX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2275 : INLINE GEN
2276 142225175 : FpX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2277 : INLINE GEN
2278 853094 : FpXX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2279 : INLINE GEN
2280 4701945 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2281 : INLINE GEN
2282 119340240 : F2x_renormalize(GEN x, long lx) { return Flx_renormalize(x,lx); }
2283 :
2284 : INLINE GEN
2285 0 : F2xX_shift(GEN a, long n, long vs) { return FlxX_shift(a, n, vs); }
2286 :
2287 : INLINE GEN
2288 38282 : F2v_to_F2x(GEN x, long sv) {
2289 38282 : GEN y = leafcopy(x);
2290 38281 : y[1] = sv; F2x_renormalize(y, lg(y)); return y;
2291 : }
2292 :
2293 : INLINE long
2294 385 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
2295 :
2296 : INLINE long
2297 4977 : gval(GEN x, long v)
2298 4977 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
2299 :
2300 : INLINE void
2301 479910 : RgX_shift_inplace_init(long v)
2302 479910 : { if (v) (void)cgetg(v, t_VECSMALL); }
2303 : /* shift polynomial in place. assume v free cells have been left before x */
2304 : INLINE GEN
2305 479910 : RgX_shift_inplace(GEN x, long v)
2306 : {
2307 : long i, lx;
2308 : GEN z;
2309 479910 : if (!v) return x;
2310 253169 : lx = lg(x);
2311 253169 : if (lx == 2) return x;
2312 253169 : z = x + lx;
2313 : /* stackdummy's from normalizepol */
2314 253169 : while (lg(z) != v) z += lg(z);
2315 253169 : z += v;
2316 4654514 : for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
2317 675895 : for (i = 0; i < v; i++) gel(--z,0) = gen_0;
2318 253169 : z -= 2;
2319 253169 : z[1] = x[1];
2320 253169 : z[0] = evaltyp(t_POL) | evallg(lx+v);
2321 253169 : stackdummy((pari_sp)z, (pari_sp)x); return z;
2322 : }
2323 :
2324 :
2325 : /* LINEAR ALGEBRA */
2326 : INLINE GEN
2327 494135 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
2328 : INLINE GEN
2329 7607492 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
2330 : INLINE GEN
2331 3189456 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
2332 : INLINE GEN
2333 0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
2334 : INLINE GEN
2335 104139 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
2336 : INLINE GEN
2337 0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
2338 : INLINE GEN
2339 0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
2340 : INLINE GEN
2341 36751376 : zero_zv(long x) { return zero_Flv(x); }
2342 : INLINE GEN
2343 266 : zm_transpose(GEN x) { return Flm_transpose(x); }
2344 : INLINE GEN
2345 0 : zm_copy(GEN x) { return Flm_copy(x); }
2346 : INLINE GEN
2347 2149293 : zv_copy(GEN x) { return Flv_copy(x); }
2348 : INLINE GEN
2349 0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
2350 :
2351 : INLINE GEN
2352 11371111 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfremdiv(x,y,NULL); }
2353 : INLINE GEN
2354 326440 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
2355 : INLINE GEN
2356 6926986 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
2357 : INLINE void
2358 9228543 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
2359 : INLINE GEN
2360 19750825 : RgM_shallowcopy(GEN x)
2361 : {
2362 : long l;
2363 19750825 : GEN y = cgetg_copy(x, &l);
2364 88937424 : while (--l > 0) gel(y,l) = leafcopy(gel(x,l));
2365 19750317 : return y;
2366 : }
2367 : INLINE GEN
2368 195214 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
2369 :
2370 : INLINE GEN
2371 0 : F3m_copy(GEN x) { return RgM_shallowcopy(x); }
2372 :
2373 : INLINE GEN
2374 5971725 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
2375 :
2376 : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
2377 : * x,y are ZV of the same length */
2378 : INLINE int
2379 23240 : ZV_dvd(GEN x, GEN y)
2380 : {
2381 23240 : long i, l = lg(x);
2382 36715 : for (i=1; i < l; i++)
2383 29568 : if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
2384 7147 : return 1;
2385 : }
2386 : INLINE GEN
2387 535816 : ZM_ZV_mod(GEN x, GEN y)
2388 1530713 : { pari_APPLY_same(ZV_ZV_mod(gel(x,i), y)) }
2389 : INLINE GEN
2390 7306569 : ZV_ZV_mod(GEN x, GEN y)
2391 18877043 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
2392 : INLINE GEN
2393 0 : vecmodii(GEN x, GEN y) { return ZV_ZV_mod(x,y); }
2394 : INLINE GEN
2395 174881 : vecmoduu(GEN x, GEN y) { pari_APPLY_ulong(((ulong*)x)[i] % ((ulong*)y)[i]) }
2396 :
2397 : /* Fq */
2398 : INLINE GEN
2399 3335858 : Fq_red(GEN x, GEN T, GEN p)
2400 3335858 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
2401 : INLINE GEN
2402 110255 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
2403 : {
2404 : (void) p;
2405 110255 : return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
2406 : }
2407 : INLINE GEN
2408 756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
2409 :
2410 : INLINE GEN
2411 14579 : gener_Fq_local(GEN T, GEN p, GEN L)
2412 0 : { return T? gener_FpXQ_local(T,p, L)
2413 14579 : : pgener_Fp_local(p, L); }
2414 :
2415 : INLINE GEN
2416 0 : random_Fq(GEN T, GEN p)
2417 0 : { return T ? random_FpX(get_FpX_degree(T), get_FpX_var(T), p): randomi(p); }
2418 :
2419 : /* FpXQX */
2420 : INLINE GEN
2421 4546 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
2422 : INLINE GEN
2423 0 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
2424 : INLINE GEN
2425 165271 : FlxqX_div_pre(GEN x, GEN y, GEN T, ulong p, ulong pi) { return FlxqX_divrem_pre(x, y, T, p, pi, NULL); }
2426 : INLINE GEN
2427 33243 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
2428 :
2429 : INLINE GEN
2430 20587 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
2431 20587 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
2432 :
2433 : /* FqX */
2434 : INLINE GEN
2435 25354 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
2436 : INLINE GEN
2437 127694 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2438 : INLINE GEN
2439 24956 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
2440 : INLINE GEN
2441 68607 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2442 : INLINE GEN
2443 558666 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
2444 558666 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
2445 : INLINE GEN
2446 426566 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
2447 426566 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
2448 : INLINE GEN
2449 367439 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
2450 367439 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
2451 : INLINE GEN
2452 21942 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
2453 21942 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
2454 : INLINE GEN
2455 71855 : FqX_sqr(GEN x, GEN T, GEN p)
2456 71855 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
2457 : INLINE GEN
2458 1295 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
2459 1295 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
2460 : INLINE GEN
2461 19411 : FqX_halve(GEN x, GEN T, GEN p)
2462 19411 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
2463 : INLINE GEN
2464 39547 : FqX_div(GEN x, GEN y, GEN T, GEN p)
2465 39547 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
2466 : INLINE GEN
2467 6090 : FqX_get_red(GEN S, GEN T, GEN p)
2468 6090 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
2469 : INLINE GEN
2470 37743 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
2471 37743 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
2472 : INLINE GEN
2473 0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
2474 0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
2475 : INLINE GEN
2476 71939 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
2477 71939 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
2478 : INLINE GEN
2479 0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
2480 0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
2481 : INLINE GEN
2482 262983 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
2483 262983 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
2484 : INLINE GEN
2485 435376 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
2486 435376 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
2487 : INLINE GEN
2488 4375 : FqX_normalize(GEN z, GEN T, GEN p)
2489 4375 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
2490 : INLINE GEN
2491 294294 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
2492 : INLINE GEN
2493 0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
2494 : INLINE GEN
2495 87172 : FqX_factor(GEN f, GEN T, GEN p)
2496 87172 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
2497 : INLINE GEN
2498 7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
2499 7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
2500 : INLINE GEN
2501 7 : FqX_ddf(GEN f, GEN T, GEN p)
2502 7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
2503 : INLINE GEN
2504 47341 : FqX_degfact(GEN f, GEN T, GEN p)
2505 47341 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
2506 : INLINE GEN
2507 7273 : FqX_roots(GEN f, GEN T, GEN p)
2508 7273 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
2509 : INLINE GEN
2510 175 : FqX_to_mod(GEN f, GEN T, GEN p)
2511 175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
2512 :
2513 : /*FqXQ*/
2514 : INLINE GEN
2515 0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2516 0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2517 : INLINE GEN
2518 0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2519 0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2520 : INLINE GEN
2521 0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
2522 0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
2523 : INLINE GEN
2524 0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
2525 0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
2526 : INLINE GEN
2527 0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
2528 0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
2529 : INLINE GEN
2530 22766 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
2531 22766 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
2532 : INLINE GEN
2533 0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
2534 0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
2535 : INLINE GEN
2536 0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
2537 0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
2538 :
2539 : /*FqXn*/
2540 : INLINE GEN
2541 6590 : FqXn_expint(GEN x, long n, GEN T, GEN p)
2542 6590 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
2543 : INLINE GEN
2544 0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
2545 0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
2546 : INLINE GEN
2547 7126 : FqXn_inv(GEN x, long n, GEN T, GEN p)
2548 7126 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
2549 : INLINE GEN
2550 263403 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
2551 263403 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
2552 : INLINE GEN
2553 0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
2554 0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
2555 :
2556 : /*FpXQ*/
2557 : INLINE GEN
2558 0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
2559 0 : { (void)T; return FpX_add(x,y,p); }
2560 : INLINE GEN
2561 0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
2562 0 : { (void)T; return FpX_sub(x,y,p); }
2563 :
2564 : /*Flxq*/
2565 : INLINE GEN
2566 0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
2567 0 : { (void)T; return Flx_add(x,y,p); }
2568 : INLINE GEN
2569 0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
2570 0 : { (void)T; return Flx_sub(x,y,p); }
2571 :
2572 : /* F2x */
2573 :
2574 : INLINE ulong
2575 647896351 : F2x_coeff(GEN x,long v)
2576 : {
2577 647896351 : ulong u=(ulong)x[2+divsBIL(v)];
2578 647895025 : return (u>>remsBIL(v))&1UL;
2579 : }
2580 :
2581 : INLINE void
2582 11581322 : F2x_clear(GEN x,long v)
2583 : {
2584 11581322 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2585 11581236 : *u&=~(1UL<<remsBIL(v));
2586 11581176 : }
2587 :
2588 : INLINE void
2589 126215377 : F2x_set(GEN x,long v)
2590 : {
2591 126215377 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2592 126198687 : *u|=1UL<<remsBIL(v);
2593 126186962 : }
2594 :
2595 : INLINE void
2596 1562247 : F2x_flip(GEN x,long v)
2597 : {
2598 1562247 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2599 1562247 : *u^=1UL<<remsBIL(v);
2600 1562253 : }
2601 :
2602 : /* F2v */
2603 :
2604 : INLINE ulong
2605 641183410 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
2606 :
2607 : INLINE void
2608 11581320 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
2609 :
2610 : INLINE void
2611 99937409 : F2v_set(GEN x,long v) { F2x_set(x,v-1); }
2612 :
2613 : INLINE void
2614 1562233 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
2615 :
2616 : /* F2m */
2617 :
2618 : INLINE ulong
2619 6519329 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
2620 :
2621 : INLINE void
2622 0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
2623 :
2624 : INLINE void
2625 686 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
2626 :
2627 : INLINE void
2628 1562232 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
2629 :
2630 : /* F3m */
2631 :
2632 : INLINE ulong
2633 1811173 : F3m_coeff(GEN x, long a, long b) { return F3v_coeff(gel(x,b), a); }
2634 :
2635 : INLINE void
2636 0 : F3m_set(GEN x, long a, long b, ulong c) { F3v_set(gel(x,b), a, c); }
2637 :
2638 : /* ARITHMETIC */
2639 : INLINE GEN
2640 35763 : matpascal(long n) { return matqpascal(n, NULL); }
2641 : INLINE long
2642 458489 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
2643 : INLINE long
2644 98 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
2645 : INLINE GEN
2646 6299691 : sqrti(GEN x) { return sqrtremi(x,NULL); }
2647 : INLINE GEN
2648 120413706 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
2649 : INLINE int
2650 555136 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
2651 : INLINE int
2652 26852 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
2653 : INLINE GEN
2654 0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
2655 : INLINE GEN
2656 0 : gminsg(long s, GEN y) { return gmings(y,s); }
2657 : INLINE GEN
2658 36843868 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
2659 : INLINE GEN
2660 124118323 : gmulgu(GEN y, ulong s) { return gmulug(s,y); }
2661 : INLINE GEN
2662 1405990 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
2663 : INLINE GEN
2664 469908 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
2665 :
2666 : INLINE GEN
2667 13508787 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
2668 : INLINE GEN
2669 866592 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
2670 :
2671 : /* x t_COMPLEX */
2672 : INLINE GEN
2673 84098490 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
2674 : /* q t_QUAD */
2675 : INLINE GEN
2676 69860 : quadnorm(GEN q)
2677 : {
2678 69860 : GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
2679 69860 : GEN z, u = gel(q,3), v = gel(q,2);
2680 69860 : if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
2681 : {
2682 69671 : z = signe(b)? mulii(v, addii(u,v)): sqri(v);
2683 69671 : return addii(z, mulii(c, sqri(u)));
2684 : }
2685 : else
2686 : {
2687 189 : z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
2688 189 : return gadd(z, gmul(c, gsqr(u)));
2689 : }
2690 : }
2691 : /* x a t_QUAD, return the attached discriminant */
2692 : INLINE GEN
2693 1078 : quad_disc(GEN x)
2694 : {
2695 1078 : GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
2696 1078 : if (is_pm1(b)) return subsi(1, c4);
2697 154 : togglesign_safe(&c4); return c4;
2698 : }
2699 : INLINE GEN
2700 4836068 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
2701 : INLINE GEN
2702 18470150 : qfb_disc(GEN x) { return gel(x,4); }
2703 :
2704 : INLINE GEN
2705 3207383 : sqrfrac(GEN x)
2706 : {
2707 3207383 : GEN z = cgetg(3,t_FRAC);
2708 3207383 : gel(z,1) = sqri(gel(x,1));
2709 3207383 : gel(z,2) = sqri(gel(x,2)); return z;
2710 : }
2711 :
2712 : INLINE void
2713 16107856 : normalize_frac(GEN z) {
2714 16107856 : if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
2715 16107856 : }
2716 :
2717 : INLINE GEN
2718 37957843 : powii(GEN x, GEN n)
2719 : {
2720 37957843 : long ln = lgefint(n);
2721 37957843 : if (ln == 3) {
2722 : GEN z;
2723 37935114 : if (signe(n) > 0) return powiu(x, n[2]);
2724 73598 : z = cgetg(3, t_FRAC);
2725 73609 : gel(z,1) = gen_1;
2726 73609 : gel(z,2) = powiu(x, n[2]);
2727 73601 : return z;
2728 : }
2729 22729 : if (ln == 2) return gen_1; /* rare */
2730 : /* should never happen */
2731 0 : return powgi(x, n); /* overflow unless x = 0, 1, -1 */
2732 : }
2733 : INLINE GEN
2734 1547 : powIs(long n) {
2735 1547 : switch(n & 3)
2736 : {
2737 63 : case 1: return mkcomplex(gen_0,gen_1);
2738 385 : case 2: return gen_m1;
2739 693 : case 3: return mkcomplex(gen_0,gen_m1);
2740 : }
2741 406 : return gen_1;
2742 : }
2743 :
2744 : /*******************************************************************/
2745 : /* */
2746 : /* ASSIGNMENTS */
2747 : /* */
2748 : /*******************************************************************/
2749 0 : INLINE void mpexpz(GEN x, GEN z)
2750 0 : { pari_sp av = avma; gaffect(mpexp(x), z); set_avma(av); }
2751 0 : INLINE void mplogz(GEN x, GEN z)
2752 0 : { pari_sp av = avma; gaffect(mplog(x), z); set_avma(av); }
2753 0 : INLINE void mpcosz(GEN x, GEN z)
2754 0 : { pari_sp av = avma; gaffect(mpcos(x), z); set_avma(av); }
2755 0 : INLINE void mpsinz(GEN x, GEN z)
2756 0 : { pari_sp av = avma; gaffect(mpsin(x), z); set_avma(av); }
2757 0 : INLINE void gnegz(GEN x, GEN z)
2758 0 : { pari_sp av = avma; gaffect(gneg(x), z); set_avma(av); }
2759 0 : INLINE void gabsz(GEN x, long prec, GEN z)
2760 0 : { pari_sp av = avma; gaffect(gabs(x,prec), z); set_avma(av); }
2761 0 : INLINE void gaddz(GEN x, GEN y, GEN z)
2762 0 : { pari_sp av = avma; gaffect(gadd(x,y), z); set_avma(av); }
2763 0 : INLINE void gsubz(GEN x, GEN y, GEN z)
2764 0 : { pari_sp av = avma; gaffect(gsub(x,y), z); set_avma(av); }
2765 0 : INLINE void gmulz(GEN x, GEN y, GEN z)
2766 0 : { pari_sp av = avma; gaffect(gmul(x,y), z); set_avma(av); }
2767 0 : INLINE void gdivz(GEN x, GEN y, GEN z)
2768 0 : { pari_sp av = avma; gaffect(gdiv(x,y), z); set_avma(av); }
2769 0 : INLINE void gdiventz(GEN x, GEN y, GEN z)
2770 0 : { pari_sp av = avma; gaffect(gdivent(x,y), z); set_avma(av); }
2771 0 : INLINE void gmodz(GEN x, GEN y, GEN z)
2772 0 : { pari_sp av = avma; gaffect(gmod(x,y), z); set_avma(av); }
2773 0 : INLINE void gmul2nz(GEN x, long s, GEN z)
2774 0 : { pari_sp av = avma; gaffect(gmul2n(x,s), z); set_avma(av); }
2775 0 : INLINE void gshiftz(GEN x, long s, GEN z)
2776 0 : { pari_sp av = avma; gaffect(gshift(x,s), z); set_avma(av); }
2777 :
2778 : /*******************************************************************/
2779 : /* */
2780 : /* ELLIPTIC CURVES */
2781 : /* */
2782 : /*******************************************************************/
2783 4227988 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
2784 3217043 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
2785 4052432 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
2786 3986131 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
2787 4772854 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
2788 3401760 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
2789 1063836 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
2790 1670603 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
2791 1492513 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
2792 6115702 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
2793 7308810 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
2794 9269027 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
2795 1047506 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
2796 8503029 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
2797 1608804 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
2798 688190 : INLINE GEN ellff_get_a4a6(GEN x) { return gmael(x, 15, 2); }
2799 1484 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
2800 315 : INLINE long ellQp_get_prec(GEN E) { GEN z = ellQp_get_zero(E); return valp(z); }
2801 1134 : INLINE GEN ellQp_get_p(GEN E) { GEN z = ellQp_get_zero(E); return gel(z,2); }
2802 75201 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
2803 18450 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
2804 841420 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
2805 126 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
2806 :
2807 3634708 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
2808 43784584 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
2809 966146 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
2810 :
2811 : /*******************************************************************/
2812 : /* */
2813 : /* ALGEBRAIC NUMBER THEORY */
2814 : /* */
2815 : /*******************************************************************/
2816 20704994 : INLINE GEN modpr_get_pr(GEN x) { return gel(x,3); }
2817 730821 : INLINE GEN modpr_get_p(GEN x) { return pr_get_p(modpr_get_pr(x)); }
2818 8654124 : INLINE GEN modpr_get_T(GEN x) { return lg(x) == 4? NULL: gel(x,4); }
2819 :
2820 64075605 : INLINE GEN pr_get_p(GEN pr) { return gel(pr,1); }
2821 10499526 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
2822 : /* .[2] instead of itos works: e and f are small positive integers */
2823 8622916 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
2824 23079977 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
2825 37231426 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
2826 : INLINE int
2827 8309838 : pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
2828 : INLINE GEN
2829 945872 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
2830 : INLINE ulong
2831 343931 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
2832 :
2833 : /* assume nf a genuine nf */
2834 : INLINE long
2835 840778 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
2836 : INLINE GEN
2837 77081080 : nf_get_pol(GEN nf) { return gel(nf,1); }
2838 : INLINE long
2839 53718627 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
2840 : INLINE long
2841 5690579 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
2842 : INLINE long
2843 4112 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
2844 : INLINE GEN
2845 453048 : nf_get_disc(GEN nf) { return gel(nf,3); }
2846 : INLINE GEN
2847 3185012 : nf_get_index(GEN nf) { return gel(nf,4); }
2848 : INLINE GEN
2849 5805142 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
2850 : INLINE GEN
2851 248166 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
2852 : INLINE GEN
2853 1626913 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
2854 : INLINE GEN
2855 22067 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
2856 : INLINE GEN
2857 4430 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
2858 : INLINE GEN
2859 58008 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
2860 : INLINE GEN
2861 1231373 : nf_get_roots(GEN nf) { return gel(nf,6); }
2862 : INLINE GEN
2863 1841 : nf_get_zk(GEN nf)
2864 : {
2865 1841 : GEN y = gel(nf,7), D = gel(y, 1);
2866 1841 : if (typ(D) == t_POL) D = gel(D, 2);
2867 1841 : if (!equali1(D)) y = gdiv(y, D);
2868 1841 : return y;
2869 : }
2870 : INLINE GEN
2871 3617005 : nf_get_zkprimpart(GEN nf)
2872 : {
2873 3617005 : GEN y = gel(nf,7);
2874 : /* test for old format of nf.zk: non normalized */
2875 3617005 : if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
2876 3617010 : return y;
2877 : }
2878 : INLINE GEN
2879 3663614 : nf_get_zkden(GEN nf)
2880 : {
2881 3663614 : GEN y = gel(nf,7), D = gel(y,1);
2882 3663614 : if (typ(D) == t_POL) D = gel(D,2);
2883 : /* test for old format of nf.zk: non normalized */
2884 3663614 : if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
2885 3663603 : return D;
2886 : }
2887 : INLINE GEN
2888 9031288 : nf_get_invzk(GEN nf) { return gel(nf,8); }
2889 : INLINE void
2890 286045 : nf_get_sign(GEN nf, long *r1, long *r2)
2891 : {
2892 286045 : GEN x = gel(nf,2);
2893 286045 : *r1 = itou(gel(x,1));
2894 286046 : *r2 = itou(gel(x,2));
2895 286047 : }
2896 :
2897 : INLINE GEN
2898 2514709 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
2899 : INLINE GEN
2900 391262 : abgrp_get_no(GEN x) { return gel(x,1); }
2901 : INLINE GEN
2902 11488467 : abgrp_get_cyc(GEN x) { return gel(x,2); }
2903 : INLINE GEN
2904 1651920 : abgrp_get_gen(GEN x) { return gel(x,3); }
2905 : INLINE GEN
2906 9411213 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
2907 : INLINE GEN
2908 5090757 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
2909 : INLINE GEN
2910 8379 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
2911 : INLINE GEN
2912 3503827 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
2913 : INLINE GEN
2914 1578390 : bnf_get_gen(GEN bnf) { return abgrp_get_gen(bnf_get_clgp(bnf)); }
2915 : INLINE GEN
2916 641 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
2917 : INLINE GEN
2918 2699749 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
2919 : INLINE GEN
2920 1277246 : bnf_get_sunits(GEN bnf)
2921 1277246 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
2922 : INLINE GEN
2923 290171 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
2924 : INLINE long
2925 264835 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
2926 : INLINE GEN
2927 287817 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
2928 : INLINE GEN
2929 26278 : nfV_to_scalar_or_alg(GEN nf, GEN x)
2930 63405 : { pari_APPLY_same(nf_to_scalar_or_alg(nf, gel(x,i))) }
2931 : INLINE GEN
2932 25838 : bnf_get_fu(GEN bnf) {
2933 25838 : GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
2934 25838 : if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
2935 25838 : return nfV_to_scalar_or_alg(nf, vecslice(fu, 2, lg(fu)-1));
2936 : }
2937 :
2938 : INLINE GEN
2939 3623733 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
2940 : INLINE GEN
2941 1932680 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
2942 : INLINE GEN
2943 139825 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
2944 : INLINE GEN
2945 1257579 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
2946 : INLINE GEN
2947 2722103 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
2948 : INLINE GEN
2949 313942 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
2950 : INLINE GEN
2951 2390609 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
2952 : INLINE GEN
2953 70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
2954 : INLINE GEN
2955 7847 : bnr_get_gen(GEN bnr) {
2956 7847 : GEN G = bnr_get_clgp(bnr);
2957 7847 : if (lg(G) != 4)
2958 0 : pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
2959 7847 : return gel(G,3);
2960 : }
2961 :
2962 : /* localstar, used in gchar */
2963 : INLINE GEN
2964 52084 : locs_get_cyc(GEN locs) { return gel(locs,1); }
2965 : INLINE GEN
2966 191695 : locs_get_Lsprk(GEN locs) { return gel(locs,2); }
2967 : INLINE GEN
2968 1260 : locs_get_Lgenfil(GEN locs) { return gel(locs,3); }
2969 : INLINE GEN
2970 8225 : locs_get_mod(GEN locs) { return gel(locs,4); }
2971 : /* pr dividing the modulus N of locs, 0 <= i < v_pr(N)
2972 : * return a t_MAT whose columns are the logs
2973 : * of generators of U_i(pr)/U_{i+1}(pr). */
2974 : INLINE GEN
2975 1260 : locs_get_famod(GEN locs) { return gmael(locs,4,1); }
2976 : INLINE GEN
2977 207235 : locs_get_m_infty(GEN locs) { return gmael(locs,4,2); }
2978 :
2979 : /* G a grossenchar group */
2980 : INLINE GEN
2981 32639 : gchar_get_basis(GEN gc) { return gel(gc, 1); }
2982 : INLINE GEN
2983 203105 : gchar_get_bnf(GEN gc) { return gel(gc, 2); }
2984 : INLINE GEN
2985 241744 : gchar_get_nf(GEN gc) { return gel(gc, 3); }
2986 : INLINE GEN
2987 279563 : gchar_get_zm(GEN gc) { return gel(gc, 4); }
2988 : INLINE GEN
2989 8225 : gchar_get_mod(GEN gc) { return locs_get_mod(gchar_get_zm(gc)); }
2990 : INLINE GEN
2991 4095 : gchar_get_modP(GEN gc) { return gmael(gchar_get_mod(gc),1,1); }
2992 : INLINE GEN
2993 36994 : gchar_get_S(GEN gc) { return gel(gc, 5); }
2994 : INLINE GEN
2995 188468 : gchar_get_DLdata(GEN gc) { return gel(gc, 6); }
2996 : INLINE GEN
2997 1877 : gchar_get_sfu(GEN gc) { return gel(gc, 7); }
2998 : INLINE GEN
2999 16168 : gchar_get_cyc(GEN gc) { return gel(gc, 9); }
3000 : INLINE GEN
3001 0 : gchar_get_hnf(GEN gc) { return gmael(gc, 10, 1); }
3002 : INLINE GEN
3003 0 : gchar_get_U(GEN gc) { return gmael(gc, 10, 2); }
3004 : INLINE GEN
3005 10969 : gchar_get_Ui(GEN gc) { return gmael(gc, 10, 3); }
3006 : INLINE GEN
3007 3450 : gchar_get_m0(GEN gc) { return gel(gc, 11); }
3008 : INLINE GEN
3009 4539 : gchar_get_u0(GEN gc) { return gel(gc, 12); }
3010 : INLINE long
3011 17416 : gchar_get_r1(GEN gc) { return nf_get_r1(gchar_get_nf(gc)); }
3012 : INLINE long
3013 623 : gchar_get_r2(GEN gc) { return nf_get_r2(gchar_get_nf(gc)); }
3014 : INLINE GEN
3015 50476 : gchar_get_loccyc(GEN gc) { return locs_get_cyc(gchar_get_zm(gc)); }
3016 : INLINE long
3017 37260 : gchar_get_nc(GEN gc) { return lg(gchar_get_loccyc(gc))-1; }
3018 : INLINE long
3019 36994 : gchar_get_ns(GEN gc) { return lg(gchar_get_S(gc))-1; }
3020 : INLINE long
3021 1841 : gchar_get_nm(GEN gc) { return lg(gchar_get_basis(gc))-1; }
3022 : INLINE long
3023 4011 : gchar_get_evalprec(GEN gc) { return gmael(gc, 8, 1)[1]; }
3024 : INLINE long
3025 18732 : gchar_get_prec(GEN gc) { return gmael(gc, 8, 1)[2]; }
3026 : INLINE long
3027 13419 : gchar_get_nfprec(GEN gc) { return gmael(gc, 8, 1)[3]; }
3028 : INLINE void
3029 1785 : gchar_set_evalprec(GEN gc, long prec) { gmael(gc, 8, 1)[1] = prec; }
3030 : INLINE void
3031 1202 : gchar_set_prec(GEN gc, long prec) { gmael(gc, 8, 1)[2] = prec; }
3032 : INLINE void
3033 1202 : gchar_copy_precs(GEN gc, GEN gc2)
3034 : {
3035 1202 : gel(gc2, 8) = shallowcopy(gel(gc,8));
3036 1202 : gmael(gc2, 8, 1) = shallowcopy(gmael(gc, 8, 1));
3037 1202 : }
3038 : INLINE void
3039 3079 : gchar_set_nfprec(GEN gc, long prec) { gmael(gc, 8, 1)[3] = prec; }
3040 : INLINE long
3041 322 : gchar_get_ntors(GEN gc) { return gmael(gc, 8, 2)[1]; }
3042 : INLINE long
3043 322 : gchar_get_nfree(GEN gc) { return gmael(gc, 8, 2)[2]; }
3044 : INLINE long
3045 2042 : gchar_get_nalg(GEN gc) { return gmael(gc, 8, 2)[3]; }
3046 : INLINE void
3047 1573 : gchar_set_basis(GEN gc, GEN m_inv) { gel(gc, 1) = m_inv; }
3048 : INLINE void
3049 2296 : gchar_set_nf(GEN gc, GEN nf) { gel(gc, 3) = nf; }
3050 : INLINE void
3051 623 : gchar_set_ntors(GEN gc, long n) { gmael(gc, 8, 2)[1] = n; }
3052 : INLINE void
3053 623 : gchar_set_nfree(GEN gc, long n) { gmael(gc, 8, 2)[2] = n; }
3054 : INLINE void
3055 623 : gchar_set_nalg(GEN gc, long n) { gmael(gc, 8, 2)[3] = n; }
3056 : INLINE void
3057 1573 : gchar_set_cyc(GEN gc, GEN cyc) { gel(gc, 9) = cyc; }
3058 : INLINE void
3059 623 : gchar_set_HUUi(GEN gc, GEN hnf, GEN U, GEN Ui) { gel(gc, 10) = mkvec3(hnf, U, Ui); }
3060 : INLINE void
3061 950 : gchar_set_m0(GEN gc, GEN m0) { gel(gc, 11) = m0; }
3062 : INLINE void
3063 2093 : gchar_set_u0(GEN gc, GEN u0) { gel(gc, 12) = u0; }
3064 :
3065 : INLINE GEN
3066 1382940 : bid_get_mod(GEN bid) { return gel(bid,1); }
3067 : INLINE GEN
3068 71246 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
3069 : INLINE GEN
3070 34944 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
3071 : INLINE GEN
3072 5403312 : bid_get_grp(GEN bid) { return gel(bid,2); }
3073 : INLINE GEN
3074 2309011 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
3075 : INLINE GEN
3076 2020786 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
3077 : INLINE GEN
3078 1986979 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
3079 : INLINE GEN
3080 51058 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
3081 : INLINE GEN
3082 2212916 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
3083 : INLINE GEN
3084 3370258 : bid_get_U(GEN bid) { return gel(bid,5); }
3085 : INLINE GEN
3086 0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
3087 : INLINE GEN
3088 5349620 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
3089 : INLINE GEN
3090 0 : bid_get_gen_nocheck(GEN bid) { return abgrp_get_gen(bid_get_grp(bid)); }
3091 : INLINE GEN
3092 53123 : bid_get_gen(GEN bid) {
3093 53123 : GEN G = bid_get_grp(bid);
3094 53123 : if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
3095 53123 : return abgrp_get_gen(G);
3096 : }
3097 :
3098 : INLINE GEN
3099 33687460 : znstar_get_N(GEN G) { return gmael(G,1,1); }
3100 : INLINE GEN
3101 7126898 : znstar_get_faN(GEN G) { return gel(G,3); }
3102 : INLINE GEN
3103 14 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
3104 : INLINE GEN
3105 228586 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
3106 : INLINE GEN
3107 16150 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
3108 : INLINE GEN
3109 7762882 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
3110 : INLINE GEN
3111 3210579 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
3112 : INLINE GEN
3113 46788 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
3114 : INLINE GEN
3115 156031 : znstar_get_U(GEN G) { return gel(G,5); }
3116 : INLINE GEN
3117 2813049 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
3118 : INLINE GEN
3119 35867 : gal_get_pol(GEN gal) { return gel(gal,1); }
3120 : INLINE GEN
3121 6006 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
3122 : INLINE GEN
3123 91 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
3124 : INLINE GEN
3125 23660 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
3126 : INLINE GEN
3127 32311 : gal_get_roots(GEN gal) { return gel(gal,3); }
3128 : INLINE GEN
3129 27453 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
3130 : INLINE GEN
3131 27447 : gal_get_den(GEN gal) { return gel(gal,5); }
3132 : INLINE GEN
3133 79454 : gal_get_group(GEN gal) { return gel(gal,6); }
3134 : INLINE GEN
3135 8946 : gal_get_gen(GEN gal) { return gel(gal,7); }
3136 : INLINE GEN
3137 6944 : gal_get_orders(GEN gal) { return gel(gal,8); }
3138 :
3139 : /* assume rnf a genuine rnf */
3140 : INLINE long
3141 1224649 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
3142 : INLINE long
3143 17458 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
3144 : INLINE long
3145 768035 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
3146 : INLINE GEN
3147 819 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
3148 : INLINE GEN
3149 805 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
3150 : INLINE GEN
3151 903 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
3152 : INLINE GEN
3153 459613 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
3154 : INLINE GEN
3155 4725 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
3156 : INLINE GEN
3157 227273 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
3158 : INLINE GEN
3159 1486611 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
3160 : INLINE GEN
3161 245 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
3162 : INLINE GEN
3163 105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
3164 : INLINE GEN
3165 1869 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
3166 : INLINE long
3167 455 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
3168 : INLINE GEN
3169 199284 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
3170 : INLINE long
3171 3388 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
3172 : INLINE GEN
3173 3850 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
3174 : INLINE GEN
3175 107826 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
3176 : INLINE GEN
3177 1225 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
3178 :
3179 : INLINE GEN
3180 135764 : idealred(GEN nf, GEN I) { return idealred0(nf, I, NULL); }
3181 :
3182 : INLINE GEN
3183 3415 : idealchineseinit(GEN nf, GEN x)
3184 3415 : { return idealchinese(nf,x,NULL); }
3185 :
3186 : /*******************************************************************/
3187 : /* */
3188 : /* CLOSURES */
3189 : /* */
3190 : /*******************************************************************/
3191 361851976 : INLINE long closure_arity(GEN C) { return ((ulong)C[1])&ARITYBITS; }
3192 40016067 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
3193 313397740 : INLINE const char *closure_codestr(GEN C) { return GSTR(gel(C,2))-1; }
3194 0 : INLINE GEN closure_get_code(GEN C) { return gel(C,2); }
3195 313387060 : INLINE GEN closure_get_oper(GEN C) { return gel(C,3); }
3196 313368453 : INLINE GEN closure_get_data(GEN C) { return gel(C,4); }
3197 12690 : INLINE GEN closure_get_dbg(GEN C) { return gel(C,5); }
3198 35203 : INLINE GEN closure_get_text(GEN C) { return gel(C,6); }
3199 12755320 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
3200 :
3201 : /*******************************************************************/
3202 : /* */
3203 : /* ERRORS */
3204 : /* */
3205 : /*******************************************************************/
3206 : INLINE long
3207 60639 : err_get_num(GEN e) { return e[1]; }
3208 : INLINE GEN
3209 266 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
3210 :
3211 : INLINE void
3212 14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
3213 : INLINE void
3214 21 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
3215 : INLINE void
3216 84 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
3217 : INLINE void
3218 676 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
3219 : INLINE void
3220 0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
3221 : INLINE void
3222 36 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
3223 : INLINE void
3224 98 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
3225 : INLINE void
3226 483 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
3227 : INLINE void
3228 19983 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
3229 : INLINE void
3230 63 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
3231 : INLINE void
3232 2666 : pari_err_DOMAIN(const char *f, const char *v, const char *op, GEN l, GEN x) { pari_err(e_DOMAIN, f,v,op,l,x); }
3233 : INLINE void
3234 200 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
3235 : INLINE void
3236 0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
3237 : INLINE void
3238 406 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
3239 : INLINE void
3240 156 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
3241 : INLINE void
3242 238 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
3243 : INLINE void
3244 0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
3245 : INLINE void
3246 84 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
3247 : INLINE void
3248 1316 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
3249 : INLINE void
3250 56 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
3251 : INLINE void
3252 84 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
3253 : INLINE void
3254 14768 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
3255 : INLINE void
3256 3500 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
3257 : INLINE void
3258 364 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
3259 : INLINE void
3260 245 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
3261 245 : { pari_err(e_PRIORITY, f,x,op,v); }
3262 :
|