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 retmkpadic(x,p,pd,e,d)\
80 : do { GEN _v = cgetg(5, t_PADIC);\
81 : _v[1] = evalvalp(e) | evalprecp(d);\
82 : gel(_v,2) = (p);\
83 : gel(_v,3) = (pd);\
84 : gel(_v,4) = (x); return _v; } while(0)
85 : /* x allowed to contain _pd */
86 : #define retmkpadic_i(x,p,pd,e,d)\
87 : do { GEN _v = cgetg(5, t_PADIC), _pd = (pd);\
88 : _v[1] = evalvalp(e) | evalprecp(d);\
89 : gel(_v,2) = (p);\
90 : gel(_v,3) = (_pd);\
91 : gel(_v,4) = (x); return _v; } while(0)
92 : #define retmkqfb(x,y,z,d)\
93 : do { GEN _v = cgetg(5, t_QFB);\
94 : gel(_v,1) = (x);\
95 : gel(_v,2) = (y);\
96 : gel(_v,3) = (z);\
97 : gel(_v,4) = (d); return _v; } while(0)
98 : #define retmkquad(x,y,z)\
99 : do { GEN _v = cgetg(4, t_QUAD);\
100 : gel(_v,1) = (x);\
101 : gel(_v,2) = (y);\
102 : gel(_v,3) = (z); return _v; } while(0)
103 : #define retmkvec4(x,y,z,t)\
104 : do { GEN _v = cgetg(5, t_VEC);\
105 : gel(_v,1) = (x);\
106 : gel(_v,2) = (y);\
107 : gel(_v,3) = (z);\
108 : gel(_v,4) = (t); return _v; } while(0)
109 : #define retmkvec5(x,y,z,t,u)\
110 : do { GEN _v = cgetg(6, t_VEC);\
111 : gel(_v,1) = (x);\
112 : gel(_v,2) = (y);\
113 : gel(_v,3) = (z);\
114 : gel(_v,4) = (t);\
115 : gel(_v,5) = (u); return _v; } while(0)
116 : #define retmkcol(x)\
117 : do { GEN _v = cgetg(2, t_COL);\
118 : gel(_v,1) = (x); return _v; } while(0)
119 : #define retmkcol2(x,y)\
120 : do { GEN _v = cgetg(3, t_COL);\
121 : gel(_v,1) = (x);\
122 : gel(_v,2) = (y); return _v; } while(0)
123 : #define retmkcol3(x,y,z)\
124 : do { GEN _v = cgetg(4, t_COL);\
125 : gel(_v,1) = (x);\
126 : gel(_v,2) = (y);\
127 : gel(_v,3) = (z); return _v; } while(0)
128 : #define retmkcol4(x,y,z,t)\
129 : do { GEN _v = cgetg(5, t_COL);\
130 : gel(_v,1) = (x);\
131 : gel(_v,2) = (y);\
132 : gel(_v,3) = (z);\
133 : gel(_v,4) = (t); return _v; } while(0)
134 : #define retmkcol5(x,y,z,t,u)\
135 : do { GEN _v = cgetg(6, t_COL);\
136 : gel(_v,1) = (x);\
137 : gel(_v,2) = (y);\
138 : gel(_v,3) = (z);\
139 : gel(_v,4) = (t);\
140 : gel(_v,5) = (u); return _v; } while(0)
141 : #define retmkcol6(x,y,z,t,u,v)\
142 : do { GEN _v = cgetg(7, t_COL);\
143 : gel(_v,1) = (x);\
144 : gel(_v,2) = (y);\
145 : gel(_v,3) = (z);\
146 : gel(_v,4) = (t);\
147 : gel(_v,5) = (u);\
148 : gel(_v,6) = (v); return _v; } while(0)
149 : #define retmkmat(x)\
150 : do { GEN _v = cgetg(2, t_MAT);\
151 : gel(_v,1) = (x); return _v; } while(0)
152 : #define retmkmat2(x,y)\
153 : do { GEN _v = cgetg(3, t_MAT);\
154 : gel(_v,1) = (x);\
155 : gel(_v,2) = (y); return _v; } while(0)
156 : #define retmkmat3(x,y,z)\
157 : do { GEN _v = cgetg(4, t_MAT);\
158 : gel(_v,1) = (x);\
159 : gel(_v,2) = (y);\
160 : gel(_v,3) = (z); return _v; } while(0)
161 : #define retmkmat4(x,y,z,t)\
162 : do { GEN _v = cgetg(5, t_MAT);\
163 : gel(_v,1) = (x);\
164 : gel(_v,2) = (y);\
165 : gel(_v,3) = (z);\
166 : gel(_v,4) = (t); return _v; } while(0)
167 : #define retmkmat5(x,y,z,t,u)\
168 : do { GEN _v = cgetg(6, t_MAT);\
169 : gel(_v,1) = (x);\
170 : gel(_v,2) = (y);\
171 : gel(_v,3) = (z);\
172 : gel(_v,4) = (t);\
173 : gel(_v,5) = (u); return _v; } while(0)
174 : #define retmkmat22(a,b,c,d)\
175 : do { GEN _v = cgetg(3, t_MAT);\
176 : gel(_v,1) = mkcol2(a,c);\
177 : gel(_v,2) = mkcol2(b,d); return _v; } while(0)
178 : INLINE GEN
179 7983683 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
180 : INLINE GEN
181 1813 : mkintmodu(ulong x, ulong y) {
182 1813 : GEN v = cgetg(3,t_INTMOD);
183 1813 : gel(v,1) = utoipos(y);
184 1813 : gel(v,2) = utoi(x); return v;
185 : }
186 : INLINE GEN
187 2009 : mkpadic(GEN x, GEN p, GEN pd, long e, long d) { retmkpadic(x,p,pd,e,d); }
188 : INLINE GEN
189 3024400 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
190 : INLINE GEN
191 41024999 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
192 : INLINE GEN
193 1209441 : mkfracss(long x, long y) { retmkfrac(stoi(x),utoipos(y)); }
194 : /* q = n/d a t_FRAC or t_INT; recover (n,d) */
195 : INLINE void
196 19936 : Qtoss(GEN q, long *n, long *d)
197 : {
198 19936 : if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
199 5796 : else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
200 19936 : }
201 : INLINE GEN
202 1074206 : sstoQ(long n, long d)
203 : {
204 : ulong r;
205 : long g, q;
206 1074206 : if (!n)
207 : {
208 95137 : if (!d) pari_err_INV("sstoQ",gen_0);
209 95137 : return gen_0;
210 : }
211 979069 : if (d < 0) { d = -d; n = -n; }
212 979069 : if (d == 1) return stoi(n);
213 959756 : r = labs(n);
214 959756 : if (r == 1) retmkfrac(n > 0? gen_1: gen_m1, utoipos(d));
215 909748 : q = udivuu_rem(r, d, &r);
216 909748 : if (!r) return n > 0? utoipos(q): utoineg(q);
217 854875 : g = ugcd(d,r); /* gcd(n,d) */
218 854875 : if (g != 1) { n /= g; d /= g; }
219 854875 : retmkfrac(stoi(n), utoipos(d));
220 : }
221 :
222 : INLINE GEN
223 158080493 : uutoQ(ulong n, ulong d)
224 : {
225 : ulong r;
226 : long g, q;
227 158080493 : if (!n)
228 : {
229 581 : if (!d) pari_err_INV("uutoQ",gen_0);
230 581 : return gen_0;
231 : }
232 158079912 : if (d == 1) return utoipos(n);
233 158068810 : if (n == 1) retmkfrac(gen_1, utoipos(d));
234 157979840 : q = udivuu_rem(n,d,&r);
235 157979840 : if (!r) return utoipos(q);
236 5181915 : g = ugcd(d,r); /* gcd(n,d) */
237 5181915 : if (g != 1) { n /= g; d /= g; }
238 5181915 : retmkfrac(utoipos(n), utoipos(d));
239 : }
240 :
241 : INLINE GEN
242 4959124 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
243 : INLINE GEN
244 698152 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
245 698152 : gel(v,1) = x; gel(v,2) = y; return v; }
246 : INLINE GEN
247 14 : mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
248 14 : gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
249 : INLINE GEN
250 90053967 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
251 : INLINE GEN
252 1269344 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
253 : INLINE GEN
254 973272 : cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
255 : INLINE GEN
256 69118 : mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
257 69118 : gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
258 : /* vecsmall */
259 : INLINE GEN
260 129207217 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
261 : INLINE GEN
262 223741717 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
263 223746069 : v[1]=x; v[2]=y; return v; }
264 : INLINE GEN
265 100300319 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
266 100298189 : v[1]=x; v[2]=y; v[3]=z; return v; }
267 : INLINE GEN
268 15483631 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
269 15485420 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
270 : INLINE GEN
271 691955 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
272 691954 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
273 :
274 : INLINE GEN
275 27798658 : mkqfb(GEN x, GEN y, GEN z, GEN d) { retmkqfb(x,y,z,d); }
276 : /* vec */
277 : INLINE GEN
278 68072668 : mkvec(GEN x) { retmkvec(x); }
279 : INLINE GEN
280 182680736 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
281 : INLINE GEN
282 115405341 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
283 : INLINE GEN
284 6641903 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
285 : INLINE GEN
286 14487034 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
287 : INLINE GEN
288 168 : mkvecs(long x) { retmkvec(stoi(x)); }
289 : INLINE GEN
290 219957 : mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
291 : INLINE GEN
292 524441 : mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
293 : INLINE GEN
294 21 : mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
295 : INLINE GEN
296 122507 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
297 : INLINE GEN
298 114490 : mkvec2copy(GEN x, GEN y) {
299 114490 : GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
300 : /* col */
301 : INLINE GEN
302 10180489 : mkcol(GEN x) { retmkcol(x); }
303 : INLINE GEN
304 154749675 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
305 : INLINE GEN
306 88314 : mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
307 : INLINE GEN
308 103467 : mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
309 : INLINE GEN
310 94 : mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
311 : INLINE GEN
312 232344 : mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
313 : INLINE GEN
314 40411 : mkcols(long x) { retmkcol(stoi(x)); }
315 : INLINE GEN
316 4415390 : mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
317 : INLINE GEN
318 315 : mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
319 : INLINE GEN
320 0 : mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
321 : INLINE GEN
322 33497 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
323 : /* mat */
324 : INLINE GEN
325 4610399 : mkmat(GEN x) { retmkmat(x); }
326 : INLINE GEN
327 32556009 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
328 : INLINE GEN
329 17001 : mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
330 : INLINE GEN
331 84 : mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
332 : INLINE GEN
333 0 : mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
334 : INLINE GEN
335 121011 : mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
336 : INLINE GEN
337 0 : mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
338 : INLINE GEN
339 1077591 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
340 : INLINE GEN
341 105924 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
342 : INLINE long
343 1195776 : inf_get_sign(GEN x) { return signe(gel(x,1)); }
344 : INLINE GEN
345 207501 : mkmat22s(long a, long b, long c, long d) {retmkmat2(mkcol2s(a,c),mkcol2s(b,d));}
346 : INLINE GEN
347 49791344 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
348 :
349 : /* pol */
350 : INLINE GEN
351 3565209 : pol_x(long v) {
352 3565209 : GEN p = cgetg(4, t_POL);
353 3565204 : p[1] = evalsigne(1)|evalvarn(v);
354 3565204 : gel(p,2) = gen_0;
355 3565204 : gel(p,3) = gen_1; return p;
356 : }
357 : /* x^n, assume n >= 0 */
358 : INLINE GEN
359 2232334 : pol_xn(long n, long v) {
360 2232334 : long i, a = n+2;
361 2232334 : GEN p = cgetg(a+1, t_POL);
362 2232339 : p[1] = evalsigne(1)|evalvarn(v);
363 5383792 : for (i = 2; i < a; i++) gel(p,i) = gen_0;
364 2232339 : gel(p,a) = gen_1; return p;
365 : }
366 : /* x^n, no assumption on n */
367 : INLINE GEN
368 294 : pol_xnall(long n, long v)
369 : {
370 294 : if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
371 287 : return pol_xn(n, v);
372 : }
373 : /* x^n, assume n >= 0 */
374 : INLINE GEN
375 371 : polxn_Flx(long n, long sv) {
376 371 : long i, a = n+2;
377 371 : GEN p = cgetg(a+1, t_VECSMALL);
378 371 : p[1] = sv;
379 1806 : for (i = 2; i < a; i++) p[i] = 0;
380 371 : p[a] = 1; return p;
381 : }
382 : INLINE GEN
383 4464297 : pol_1(long v) {
384 4464297 : GEN p = cgetg(3, t_POL);
385 4464289 : p[1] = evalsigne(1)|evalvarn(v);
386 4464289 : gel(p,2) = gen_1; return p;
387 : }
388 : INLINE GEN
389 83279136 : pol_0(long v)
390 : {
391 83279136 : GEN x = cgetg(2,t_POL);
392 83279134 : x[1] = evalvarn(v); return x;
393 : }
394 : #define retconst_vec(n,x)\
395 : do { long _i, _n = (n);\
396 : GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
397 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
398 : return _v; } while(0)
399 : INLINE GEN
400 234266932 : const_vec(long n, GEN x) { retconst_vec(n, x); }
401 : #define retconst_col(n,x)\
402 : do { long _i, _n = (n);\
403 : GEN _v = cgetg(_n+1, t_COL), _x = (x);\
404 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
405 : return _v; } while(0)
406 : INLINE GEN
407 39271655 : const_col(long n, GEN x) { retconst_col(n, x); }
408 : INLINE GEN
409 19057142 : const_vecsmall(long n, long c)
410 : {
411 : long i;
412 19057142 : GEN V = cgetg(n+1,t_VECSMALL);
413 752591723 : for(i=1;i<=n;i++) V[i] = c;
414 19057596 : return V;
415 : }
416 :
417 : /*** ZERO ***/
418 : /* O(p^e) */
419 : INLINE GEN
420 855657 : zeropadic(GEN p, long e) { retmkpadic(gen_0, icopy(p), gen_1, e, 0); }
421 : INLINE GEN
422 17024 : zeropadic_shallow(GEN p, long e) { retmkpadic(gen_0, icopy(p), gen_1, e, 0); }
423 : /* O(pol_x(v)^e) */
424 : INLINE GEN
425 138726 : zeroser(long v, long e)
426 : {
427 138726 : GEN x = cgetg(2, t_SER);
428 138726 : x[1] = evalvalser(e) | evalvarn(v); return x;
429 : }
430 : INLINE int
431 10079199 : ser_isexactzero(GEN x)
432 : {
433 10079199 : if (!signe(x)) switch(lg(x))
434 : {
435 132748 : case 2: return 1;
436 4648 : case 3: return isexactzero(gel(x,2));
437 : }
438 9941803 : return 0;
439 : }
440 : /* 0 * pol_x(v) */
441 : INLINE GEN
442 42307140 : zeropol(long v) { return pol_0(v); }
443 : /* vector(n) */
444 : INLINE GEN
445 118828126 : zerocol(long n)
446 : {
447 118828126 : GEN y = cgetg(n+1,t_COL);
448 803729396 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
449 118828715 : return y;
450 : }
451 : /* vectorv(n) */
452 : INLINE GEN
453 26644719 : zerovec(long n)
454 : {
455 26644719 : GEN y = cgetg(n+1,t_VEC);
456 391651225 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
457 26644715 : return y;
458 : }
459 : /* matrix(m, n) */
460 : INLINE GEN
461 109062 : zeromat(long m, long n)
462 : {
463 109062 : GEN y = cgetg(n+1,t_MAT);
464 109062 : GEN v = zerocol(m);
465 403279 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
466 109062 : return y;
467 : }
468 : /* = zero_zx, sv is a evalvarn()*/
469 : INLINE GEN
470 1426286 : zero_Flx(long sv) { return pol0_Flx(sv); }
471 : INLINE GEN
472 58764380 : zero_Flv(long n)
473 : {
474 58764380 : GEN y = cgetg(n+1,t_VECSMALL);
475 945308301 : long i; for (i=1; i<=n; i++) y[i] = 0;
476 58764323 : return y;
477 : }
478 : /* matrix(m, n) */
479 : INLINE GEN
480 2784167 : zero_Flm(long m, long n)
481 : {
482 2784167 : GEN y = cgetg(n+1,t_MAT);
483 2784157 : GEN v = zero_Flv(m);
484 17708644 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
485 2784164 : return y;
486 : }
487 : /* matrix(m, n) */
488 : INLINE GEN
489 232081 : zero_Flm_copy(long m, long n)
490 : {
491 232081 : GEN y = cgetg(n+1,t_MAT);
492 2397280 : long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
493 232083 : return y;
494 : }
495 :
496 : INLINE GEN
497 3628186 : zero_F2v(long m)
498 : {
499 3628186 : long l = nbits2nlong(m);
500 3628174 : GEN v = zero_Flv(l+1);
501 3628150 : v[1] = m;
502 3628150 : return v;
503 : }
504 :
505 : INLINE GEN
506 0 : zero_F2m(long m, long n)
507 : {
508 : long i;
509 0 : GEN M = cgetg(n+1, t_MAT);
510 0 : GEN v = zero_F2v(m);
511 0 : for (i = 1; i <= n; i++)
512 0 : gel(M,i) = v;
513 0 : return M;
514 : }
515 :
516 :
517 : INLINE GEN
518 840113 : zero_F2m_copy(long m, long n)
519 : {
520 : long i;
521 840113 : GEN M = cgetg(n+1, t_MAT);
522 2007033 : for (i = 1; i <= n; i++)
523 1166929 : gel(M,i)= zero_F2v(m);
524 840104 : return M;
525 : }
526 :
527 : /* matrix(m, n) */
528 : INLINE GEN
529 14024383 : zeromatcopy(long m, long n)
530 : {
531 14024383 : GEN y = cgetg(n+1,t_MAT);
532 70990772 : long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
533 14024424 : return y;
534 : }
535 :
536 : INLINE GEN
537 28009 : zerovec_block(long len)
538 : {
539 : long i;
540 28009 : GEN blk = cgetg_block(len + 1, t_VEC);
541 924297 : for (i = 1; i <= len; ++i)
542 896288 : gel(blk, i) = gen_0;
543 28009 : return blk;
544 : }
545 :
546 : /* i-th vector in the standard basis */
547 : INLINE GEN
548 5679611 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
549 : INLINE GEN
550 1711369 : vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
551 : INLINE GEN
552 42 : F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
553 : INLINE GEN
554 494377 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
555 : INLINE GEN
556 1293851 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
557 :
558 : INLINE GEN
559 25867646 : shallowcopy(GEN x)
560 25867646 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
561 :
562 : /* routines for naive growarrays */
563 : INLINE GEN
564 11683590 : vectrunc_init(long l)
565 : {
566 11683590 : GEN z = new_chunk(l);
567 11683527 : z[0] = evaltyp(t_VEC) | _evallg(1); return z;
568 : }
569 : INLINE GEN
570 393680 : coltrunc_init(long l)
571 : {
572 393680 : GEN z = new_chunk(l);
573 393680 : z[0] = evaltyp(t_COL) | _evallg(1); return z;
574 : }
575 : INLINE void
576 314579291 : lg_increase(GEN x) { x[0]++; }
577 : INLINE void
578 15218777 : vectrunc_append(GEN x, GEN t) { gel(x, lg(x)) = t; lg_increase(x); }
579 : INLINE void
580 9821 : vectrunc_append_batch(GEN x, GEN y)
581 : {
582 9821 : long i, l = lg(x), ly = lg(y);
583 9821 : GEN z = x + l-1;
584 33229 : for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
585 9821 : setlg(x, l+ly-1);
586 9821 : }
587 : INLINE GEN
588 188424238 : vecsmalltrunc_init(long l)
589 : {
590 188424238 : GEN z = new_chunk(l);
591 188463003 : z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
592 : }
593 : INLINE void
594 99669814 : vecsmalltrunc_append(GEN x, long t) { x[ lg(x) ] = t; lg_increase(x); }
595 :
596 : /*******************************************************************/
597 : /* */
598 : /* STRING HASH FUNCTIONS */
599 : /* */
600 : /*******************************************************************/
601 : INLINE ulong
602 2747301 : hash_str(const char *str)
603 : {
604 2747301 : ulong hash = 5381UL, c;
605 28553615 : while ( (c = (ulong)*str++) )
606 25806314 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
607 2747301 : return hash;
608 : }
609 : INLINE ulong
610 18211635 : hash_str_len(const char *str, long len)
611 : {
612 18211635 : ulong hash = 5381UL;
613 : long i;
614 166420004 : for (i = 0; i < len; i++)
615 : {
616 148208369 : ulong c = (ulong)*str++;
617 148208369 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
618 : }
619 18211635 : return hash;
620 : }
621 :
622 : /*******************************************************************/
623 : /* */
624 : /* VEC / COL / VECSMALL */
625 : /* */
626 : /*******************************************************************/
627 : /* shallow*/
628 : INLINE GEN
629 11549 : vec_shorten(GEN v, long n)
630 : {
631 11549 : GEN V = cgetg(n+1, t_VEC);
632 : long i;
633 13005 : for(i = 1; i <= n; i++) gel(V,i) = gel(v,i);
634 11549 : return V;
635 : }
636 : /* shallow*/
637 : INLINE GEN
638 18941 : vec_lengthen(GEN v, long n)
639 : {
640 18941 : GEN V = cgetg(n+1, t_VEC);
641 18941 : long i, l = lg(v);
642 1935639 : for(i = 1; i < l; i++) gel(V,i) = gel(v,i);
643 18941 : return V;
644 : }
645 : /* shallow*/
646 : INLINE GEN
647 3431950 : vec_append(GEN V, GEN s)
648 : {
649 3431950 : long i, l2 = lg(V);
650 3431950 : GEN res = cgetg(l2+1, typ(V));
651 10737700 : for (i = 1; i < l2; i++) gel(res, i) = gel(V,i);
652 3431950 : gel(res,l2) = s; return res;
653 : }
654 : /* shallow*/
655 : INLINE GEN
656 301080 : vec_prepend(GEN v, GEN s)
657 : {
658 301080 : long i, l = lg(v);
659 301080 : GEN w = cgetg(l+1, typ(v));
660 301081 : gel(w,1) = s;
661 770749 : for (i = 2; i <= l; i++) gel(w,i) = gel(v,i-1);
662 301081 : return w;
663 : }
664 : /* shallow*/
665 : INLINE GEN
666 0 : vec_setconst(GEN v, GEN x)
667 : {
668 0 : long i, l = lg(v);
669 0 : for (i = 1; i < l; i++) gel(v,i) = x;
670 0 : return v;
671 : }
672 : INLINE GEN
673 72687 : vecsmall_shorten(GEN v, long n)
674 : {
675 72687 : GEN V = cgetg(n+1,t_VECSMALL);
676 : long i;
677 892278 : for(i = 1; i <= n; i++) V[i] = v[i];
678 72686 : return V;
679 : }
680 : INLINE GEN
681 526 : vecsmall_lengthen(GEN v, long n)
682 : {
683 526 : long i, l = lg(v);
684 526 : GEN V = cgetg(n+1,t_VECSMALL);
685 46315 : for(i = 1; i < l; i++) V[i] = v[i];
686 526 : return V;
687 : }
688 :
689 : INLINE GEN
690 4016004 : vec_to_vecsmall(GEN x)
691 18860553 : { pari_APPLY_long(itos(gel(x,i))) }
692 : INLINE GEN
693 511088 : vecsmall_to_vec(GEN x)
694 7069105 : { pari_APPLY_type(t_VEC, stoi(x[i])) }
695 : INLINE GEN
696 3304 : vecsmall_to_vec_inplace(GEN z)
697 : {
698 3304 : long i, l = lg(z);
699 79604 : for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
700 3304 : settyp(z, t_VEC); return z;
701 : }
702 : INLINE GEN
703 7761357 : vecsmall_to_col(GEN x)
704 59563847 : { pari_APPLY_type(t_COL, stoi(x[i])) }
705 :
706 : INLINE int
707 7209166 : vecsmall_lexcmp(GEN x, GEN y)
708 : {
709 : long lx,ly,l,i;
710 7209166 : lx = lg(x);
711 7209166 : ly = lg(y); l = minss(lx,ly);
712 30649356 : for (i=1; i<l; i++)
713 29021891 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
714 1627465 : if (lx == ly) return 0;
715 30578 : return (lx < ly)? -1 : 1;
716 : }
717 :
718 : INLINE int
719 108088071 : vecsmall_prefixcmp(GEN x, GEN y)
720 : {
721 108088071 : long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
722 542691324 : for (i=1; i<l; i++)
723 519806035 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
724 22885289 : return 0;
725 : }
726 :
727 : /*Can be used on t_VEC, but coeffs not gcopy-ed*/
728 : INLINE GEN
729 186753 : vecsmall_prepend(GEN V, long s)
730 : {
731 186753 : long i, l2 = lg(V);
732 186753 : GEN res = cgetg(l2+1, typ(V));
733 186753 : res[1] = s;
734 624853 : for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
735 186753 : return res;
736 : }
737 :
738 : INLINE GEN
739 4077480 : vecsmall_append(GEN V, long s)
740 : {
741 4077480 : long i, l2 = lg(V);
742 4077480 : GEN res = cgetg(l2+1, t_VECSMALL);
743 5991067 : for (i = 1; i < l2; ++i) res[i] = V[i];
744 4077479 : res[l2] = s; return res;
745 : }
746 :
747 : INLINE GEN
748 1464282 : vecsmall_concat(GEN u, GEN v)
749 : {
750 1464282 : long i, l1 = lg(u)-1, l2 = lg(v)-1;
751 1464282 : GEN res = cgetg(l1+l2+1, t_VECSMALL);
752 11617788 : for (i = 1; i <= l1; ++i) res[i] = u[i];
753 15218916 : for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
754 1464282 : return res;
755 : }
756 :
757 : /* return the number of indices where u and v are equal */
758 : INLINE long
759 0 : vecsmall_coincidence(GEN u, GEN v)
760 : {
761 0 : long i, s = 0, l = minss(lg(u),lg(v));
762 0 : for(i=1; i<l; i++)
763 0 : if(u[i] == v[i]) s++;
764 0 : return s;
765 : }
766 :
767 : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
768 : INLINE long
769 84 : vecsmall_isin(GEN v, long x)
770 : {
771 84 : long i, l = lg(v);
772 124 : for (i = 1; i < l; i++)
773 80 : if (v[i] == x) return i;
774 44 : return 0;
775 : }
776 :
777 : INLINE long
778 84 : vecsmall_pack(GEN V, long base, long mod)
779 : {
780 84 : long i, s = 0;
781 273 : for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
782 84 : return s;
783 : }
784 :
785 : INLINE long
786 21 : vecsmall_indexmax(GEN x)
787 : {
788 21 : long i, i0 = 1, t = x[1], lx = lg(x);
789 84 : for (i=2; i<lx; i++)
790 63 : if (x[i] > t) t = x[i0=i];
791 21 : return i0;
792 : }
793 :
794 : INLINE long
795 1029791 : vecsmall_max(GEN x)
796 : {
797 1029791 : long i, t = x[1], lx = lg(x);
798 2873738 : for (i=2; i<lx; i++)
799 1843947 : if (x[i] > t) t = x[i];
800 1029791 : return t;
801 : }
802 :
803 : INLINE long
804 21 : vecsmall_indexmin(GEN x)
805 : {
806 21 : long i, i0 = 1, t = x[1], lx =lg(x);
807 84 : for (i=2; i<lx; i++)
808 63 : if (x[i] < t) t = x[i0=i];
809 21 : return i0;
810 : }
811 :
812 : INLINE long
813 2324 : vecsmall_min(GEN x)
814 : {
815 2324 : long i, t = x[1], lx =lg(x);
816 16268 : for (i=2; i<lx; i++)
817 13944 : if (x[i] < t) t = x[i];
818 2324 : return t;
819 : }
820 :
821 : INLINE int
822 22760207 : ZV_isscalar(GEN x)
823 : {
824 22760207 : long l = lg(x);
825 54254292 : while (--l > 1)
826 53860188 : if (signe(gel(x, l))) return 0;
827 394104 : return 1;
828 : }
829 : INLINE int
830 52361344 : QV_isscalar(GEN x)
831 : {
832 52361344 : long lx = lg(x),i;
833 68050425 : for (i=2; i<lx; i++)
834 64332795 : if (!isintzero(gel(x, i))) return 0;
835 3717630 : return 1;
836 : }
837 : INLINE int
838 1140178 : RgV_isscalar(GEN x)
839 : {
840 1140178 : long lx = lg(x),i;
841 1166342 : for (i=2; i<lx; i++)
842 1162636 : if (!gequal0(gel(x, i))) return 0;
843 3706 : return 1;
844 : }
845 : INLINE int
846 0 : RgX_isscalar(GEN x)
847 : {
848 : long i;
849 0 : for (i=lg(x)-1; i>2; i--)
850 0 : if (!gequal0(gel(x, i))) return 0;
851 0 : return 1;
852 : }
853 : INLINE long
854 26927649 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
855 : INLINE GEN
856 140 : RgX_to_RgV(GEN x, long N) { x = RgX_to_RgC(x, N); settyp(x, t_VEC); return x; }
857 :
858 : #define RgX_type_code(t1,t2) ((t1 << 6) | t2)
859 :
860 : INLINE int
861 67721 : RgX_is_rational(GEN x)
862 : {
863 : long i;
864 308283 : for (i = lg(x)-1; i > 1; i--)
865 292489 : if (!is_rational_t(typ(gel(x,i)))) return 0;
866 15794 : return 1;
867 : }
868 : INLINE int
869 20850035 : RgX_is_ZX(GEN x)
870 : {
871 : long i;
872 77184791 : for (i = lg(x)-1; i > 1; i--)
873 58113563 : if (typ(gel(x,i)) != t_INT) return 0;
874 19071228 : return 1;
875 : }
876 : INLINE int
877 395810 : RgX_is_QX(GEN x)
878 : {
879 395810 : long k = lg(x)-1;
880 1482950 : for ( ; k>1; k--)
881 1087420 : if (!is_rational_t(typ(gel(x,k)))) return 0;
882 395530 : return 1;
883 : }
884 : INLINE int
885 1395803 : RgX_is_monomial(GEN x)
886 : {
887 : long i;
888 1395803 : if (!signe(x)) return 0;
889 3406974 : for (i=lg(x)-2; i>1; i--)
890 2717414 : if (!isexactzero(gel(x,i))) return 0;
891 689560 : return 1;
892 : }
893 : INLINE int
894 31359051 : RgV_is_ZV(GEN x)
895 : {
896 : long i;
897 124667405 : for (i = lg(x)-1; i > 0; i--)
898 93340798 : if (typ(gel(x,i)) != t_INT) return 0;
899 31326607 : return 1;
900 : }
901 : INLINE int
902 140075 : RgV_is_QV(GEN x)
903 : {
904 : long i;
905 619658 : for (i = lg(x)-1; i > 0; i--)
906 482537 : if (!is_rational_t(typ(gel(x,i)))) return 0;
907 137121 : return 1;
908 : }
909 : INLINE long
910 110130 : RgV_isin_i(GEN v, GEN x, long n)
911 : {
912 : long i;
913 1033437 : for (i = 1; i <= n; i++)
914 1026703 : if (gequal(gel(v,i), x)) return i;
915 6734 : return 0;
916 : }
917 : INLINE long
918 110130 : RgV_isin(GEN v, GEN x) { return RgV_isin_i(v, x, lg(v)-1); }
919 :
920 : /********************************************************************/
921 : /** **/
922 : /** Dynamic arrays implementation **/
923 : /** **/
924 : /********************************************************************/
925 : INLINE void **
926 703909188 : pari_stack_base(pari_stack *s) { return s->data; }
927 :
928 : INLINE void
929 5744473 : pari_stack_init(pari_stack *s, size_t size, void **data)
930 : {
931 5744473 : s->data = data;
932 5744473 : *data = NULL;
933 5744473 : s->n = 0;
934 5744473 : s->alloc = 0;
935 5744473 : s->size = size;
936 5744473 : }
937 :
938 : INLINE void
939 698695024 : pari_stack_alloc(pari_stack *s, long nb)
940 : {
941 698695024 : void **sdat = pari_stack_base(s);
942 698518411 : long alloc = s->alloc;
943 698518411 : if (s->n+nb <= alloc) return;
944 2138611 : if (!alloc)
945 2043142 : alloc = nb;
946 : else
947 : {
948 196400 : while (s->n+nb > alloc) alloc <<= 1;
949 : }
950 2138611 : pari_realloc_ip(sdat,alloc*s->size);
951 2141528 : s->alloc = alloc;
952 : }
953 :
954 : INLINE long
955 605633674 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
956 :
957 : INLINE void
958 5284533 : pari_stack_delete(pari_stack *s)
959 : {
960 5284533 : void **sdat = pari_stack_base(s);
961 5270943 : if (*sdat) pari_free(*sdat);
962 5304091 : }
963 :
964 : INLINE void
965 5851 : pari_stack_pushp(pari_stack *s, void *u)
966 : {
967 5851 : long n = pari_stack_new(s);
968 5851 : void **sdat =(void**) *pari_stack_base(s);
969 5851 : sdat[n] = u;
970 5851 : }
971 :
972 : /*******************************************************************/
973 : /* */
974 : /* EXTRACT */
975 : /* */
976 : /*******************************************************************/
977 : INLINE GEN
978 641493800 : vecslice(GEN A, long y1, long y2)
979 : {
980 641493800 : long i,lB = y2 - y1 + 2;
981 641493800 : GEN B = cgetg(lB, typ(A));
982 2855576567 : for (i=1; i<lB; i++) B[i] = A[y1-1+i];
983 641481778 : return B;
984 : }
985 : INLINE GEN
986 2645652 : vecslicepermute(GEN A, GEN p, long y1, long y2)
987 : {
988 2645652 : long i,lB = y2 - y1 + 2;
989 2645652 : GEN B = cgetg(lB, typ(A));
990 29941264 : for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
991 2645647 : return B;
992 : }
993 : /* rowslice(rowpermute(A,p), x1, x2) */
994 : INLINE GEN
995 182573 : rowslicepermute(GEN x, GEN p, long j1, long j2)
996 770792 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
997 :
998 : INLINE GEN
999 78356399 : rowslice(GEN x, long j1, long j2)
1000 657439228 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
1001 :
1002 : INLINE GEN
1003 16600061 : matslice(GEN A, long x1, long x2, long y1, long y2)
1004 16600061 : { return rowslice(vecslice(A, y1, y2), x1, x2); }
1005 :
1006 : /* shallow, remove coeff of index j */
1007 : INLINE GEN
1008 273 : rowsplice(GEN x, long j)
1009 1225 : { pari_APPLY_same(vecsplice(gel(x,i), j)) }
1010 :
1011 : /* shallow, remove coeff of index j */
1012 : INLINE GEN
1013 354707 : vecsplice(GEN a, long j)
1014 : {
1015 354707 : long i, k, l = lg(a);
1016 : GEN b;
1017 354707 : if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
1018 354707 : b = cgetg(l-1, typ(a));
1019 1503226 : for (i = k = 1; i < l; i++)
1020 1148516 : if (i != j) gel(b, k++) = gel(a,i);
1021 354710 : return b;
1022 : }
1023 : /* shallow */
1024 : INLINE GEN
1025 1036 : RgM_minor(GEN a, long i, long j)
1026 : {
1027 1036 : GEN b = vecsplice(a, j);
1028 1036 : long k, l = lg(b);
1029 4242 : for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
1030 1036 : return b;
1031 : }
1032 :
1033 : /* A[x0,] */
1034 : INLINE GEN
1035 762812 : row(GEN x, long j)
1036 6687291 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
1037 : INLINE GEN
1038 8741646 : Flm_row(GEN x, long j)
1039 231880135 : { pari_APPLY_ulong((ulong)coeff(x, j, i)) }
1040 : /* A[x0,] */
1041 : INLINE GEN
1042 204988 : rowcopy(GEN x, long j)
1043 2105509 : { pari_APPLY_type(t_VEC, gcopy(gcoeff(x, j, i))) }
1044 : /* A[x0, x1..x2] */
1045 : INLINE GEN
1046 987 : row_i(GEN A, long x0, long x1, long x2)
1047 : {
1048 987 : long i, lB = x2 - x1 + 2;
1049 987 : GEN B = cgetg(lB, t_VEC);
1050 2989 : for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
1051 987 : return B;
1052 : }
1053 :
1054 : INLINE GEN
1055 697046 : vecreverse(GEN A)
1056 : {
1057 : long i, l;
1058 697046 : GEN B = cgetg_copy(A, &l);
1059 2407818 : for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
1060 697046 : return B;
1061 : }
1062 :
1063 : INLINE GEN
1064 3052 : vecsmall_reverse(GEN A)
1065 : {
1066 : long i, l;
1067 3052 : GEN B = cgetg_copy(A, &l);
1068 12698 : for (i=1; i<l; i++) B[i] = A[l-i];
1069 3052 : return B;
1070 : }
1071 :
1072 : INLINE void
1073 2570 : vecreverse_inplace(GEN y)
1074 : {
1075 2570 : long l = lg(y), lim = l>>1, i;
1076 8242 : for (i = 1; i <= lim; i++)
1077 : {
1078 5672 : GEN z = gel(y,i);
1079 5672 : gel(y,i) = gel(y,l-i);
1080 5672 : gel(y,l-i) = z;
1081 : }
1082 2570 : }
1083 :
1084 : INLINE GEN
1085 77969477 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
1086 :
1087 : INLINE GEN
1088 19832350 : vecpermute(GEN A, GEN x)
1089 112464268 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
1090 :
1091 : INLINE GEN
1092 6933220 : veclast(GEN A) { return gel(A, lg(A)-1); }
1093 :
1094 : INLINE GEN
1095 14415869 : rowpermute(GEN x, GEN p)
1096 100782289 : { pari_APPLY_same(typ(gel(x,i)) == t_VECSMALL ? vecsmallpermute(gel(x, i), p)
1097 : : vecpermute(gel(x, i), p))
1098 : }
1099 : /*******************************************************************/
1100 : /* */
1101 : /* PERMUTATIONS */
1102 : /* */
1103 : /*******************************************************************/
1104 : INLINE GEN
1105 3150173 : identity_zv(long n)
1106 : {
1107 3150173 : GEN v = cgetg(n+1, t_VECSMALL);
1108 : long i;
1109 32221731 : for (i = 1; i <= n; i++) v[i] = i;
1110 3150162 : return v;
1111 : }
1112 : INLINE GEN
1113 4333 : identity_ZV(long n)
1114 : {
1115 4333 : GEN v = cgetg(n+1, t_VEC);
1116 : long i;
1117 51898 : for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
1118 4333 : return v;
1119 : }
1120 : /* identity permutation */
1121 : INLINE GEN
1122 3125829 : identity_perm(long n) { return identity_zv(n); }
1123 :
1124 : /* assume d <= n */
1125 : INLINE GEN
1126 99911 : cyclic_perm(long n, long d)
1127 : {
1128 99911 : GEN perm = cgetg(n+1, t_VECSMALL);
1129 : long i;
1130 520240 : for (i = 1; i <= n-d; i++) perm[i] = i+d;
1131 226702 : for ( ; i <= n; i++) perm[i] = i-n+d;
1132 99911 : return perm;
1133 : }
1134 :
1135 : /* Multiply (compose) two permutations */
1136 : INLINE GEN
1137 80163898 : perm_mul(GEN s, GEN x)
1138 1014337891 : { pari_APPLY_long(s[x[i]]) }
1139 :
1140 : INLINE GEN
1141 728 : perm_sqr(GEN x)
1142 18284 : { pari_APPLY_long(x[x[i]]) }
1143 :
1144 : /* Compute the inverse (reciprocal) of a permutation. */
1145 : INLINE GEN
1146 2778622 : perm_inv(GEN x)
1147 : {
1148 : long i, lx;
1149 2778622 : GEN y = cgetg_copy(x, &lx);
1150 39059477 : for (i=1; i<lx; i++) y[ x[i] ] = i;
1151 2778618 : return y;
1152 : }
1153 : /* Return s*t*s^-1 */
1154 : INLINE GEN
1155 418299 : perm_conj(GEN s, GEN t)
1156 : {
1157 : long i, l;
1158 418299 : GEN v = cgetg_copy(s, &l);
1159 6857893 : for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
1160 418299 : return v;
1161 : }
1162 :
1163 : INLINE void
1164 495044913 : pari_free(void *pointer)
1165 : {
1166 495044913 : BLOCK_SIGINT_START;
1167 495108825 : free(pointer);
1168 495108825 : BLOCK_SIGINT_END;
1169 495101834 : }
1170 : INLINE void*
1171 725397573 : pari_malloc(size_t size)
1172 : {
1173 725397573 : if (size)
1174 : {
1175 : char *tmp;
1176 725401166 : BLOCK_SIGINT_START;
1177 725463925 : tmp = (char*)malloc(size);
1178 725463925 : BLOCK_SIGINT_END;
1179 725468912 : if (!tmp) pari_err(e_MEM);
1180 725477089 : return tmp;
1181 : }
1182 0 : return NULL;
1183 : }
1184 : INLINE void*
1185 1890 : pari_realloc(void *pointer, size_t size)
1186 : {
1187 : char *tmp;
1188 :
1189 1890 : BLOCK_SIGINT_START;
1190 1890 : if (!pointer) tmp = (char *) malloc(size);
1191 1890 : else tmp = (char *) realloc(pointer,size);
1192 1890 : BLOCK_SIGINT_END;
1193 1890 : if (!tmp) pari_err(e_MEM);
1194 1890 : return tmp;
1195 : }
1196 : INLINE void
1197 2144205 : pari_realloc_ip(void **pointer, size_t size)
1198 : {
1199 : char *tmp;
1200 2144205 : BLOCK_SIGINT_START;
1201 2144199 : if (!*pointer) tmp = (char *) malloc(size);
1202 101253 : else tmp = (char *) realloc(*pointer,size);
1203 2144199 : if (!tmp) pari_err(e_MEM);
1204 2144199 : *pointer = tmp;
1205 2144199 : BLOCK_SIGINT_END;
1206 2143905 : }
1207 :
1208 : INLINE void*
1209 47545 : pari_calloc(size_t size)
1210 : {
1211 47545 : void *t = pari_malloc(size);
1212 47545 : memset(t, 0, size); return t;
1213 : }
1214 : INLINE GEN
1215 10166 : cgetalloc(size_t l, long t)
1216 : { /* evallg may raise e_OVERFLOW, which would leak x */
1217 10166 : ulong x0 = evaltyp(t) | evallg(l);
1218 10166 : GEN x = (GEN)pari_malloc(l * sizeof(long));
1219 10166 : x[0] = x0; return x;
1220 : }
1221 :
1222 : /*******************************************************************/
1223 : /* */
1224 : /* GARBAGE COLLECTION */
1225 : /* */
1226 : /*******************************************************************/
1227 : /* copy integer x as if we had set_avma(av) */
1228 : INLINE GEN
1229 7908096657 : icopy_avma(GEN x, pari_sp av)
1230 : {
1231 7908096657 : long i = lgefint(x), lx = i;
1232 7908096657 : GEN y = ((GEN)av) - i;
1233 57898918070 : while (--i > 0) y[i] = x[i];
1234 7908096657 : y[0] = evaltyp(t_INT)|evallg(lx);
1235 7911171260 : return y;
1236 : }
1237 : /* copy leaf x as if we had set_avma(av) */
1238 : INLINE GEN
1239 566121666 : leafcopy_avma(GEN x, pari_sp av)
1240 : {
1241 566121666 : long i = lg(x);
1242 566121666 : GEN y = ((GEN)av) - i;
1243 3029775696 : while (--i > 0) y[i] = x[i];
1244 566121666 : y[0] = x[0] & (~CLONEBIT);
1245 566121666 : return y;
1246 : }
1247 : INLINE GEN
1248 1115705349 : gc_uptoleaf(pari_sp av, GEN x)
1249 : {
1250 : long lx;
1251 : GEN q;
1252 :
1253 1115705349 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1254 1114497552 : lx = lg(x);
1255 1114497552 : q = ((GEN)av) - lx;
1256 1114497552 : set_avma((pari_sp)q);
1257 11150498380 : while (--lx >= 0) q[lx] = x[lx];
1258 1114257288 : return q;
1259 : }
1260 : INLINE GEN
1261 4043055579 : gc_INT(pari_sp av, GEN x)
1262 : {
1263 4043055579 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1264 3607099271 : set_avma((pari_sp)icopy_avma(x, av));
1265 3610241058 : return (GEN)avma;
1266 : }
1267 : INLINE void
1268 8454530 : gc_INT_affii(pari_sp av, GEN x, GEN *y)
1269 : {
1270 8454530 : long l = lg(*y);
1271 8454530 : if (lgefint(x) <= l && isonstack(*y))
1272 : {
1273 6810728 : affii(x,*y);
1274 6812863 : set_avma(av);
1275 : }
1276 : else
1277 1643704 : *y = gc_INT(av, x);
1278 8465019 : }
1279 : INLINE GEN
1280 1906498807 : gc_upto(pari_sp av, GEN q)
1281 : {
1282 1906498807 : if (!isonstack(q) || (GEN)av<=q) return gc_const(av,q);
1283 1722759316 : switch(typ(q))
1284 : { /* first all non recursive types */
1285 489342986 : case t_INT: return gc_INT(av, q);
1286 342745150 : case t_REAL:
1287 : case t_STR:
1288 342745150 : case t_VECSMALL: return gc_uptoleaf(av,q);
1289 890672834 : default: return (GEN)((pari_sp)q + gc_stack_update(av, (pari_sp)(q+lg(q))));
1290 : }
1291 : }
1292 :
1293 : /* gc_upto(av, gcopy(x)) */
1294 : INLINE GEN
1295 249450665 : gc_GEN(pari_sp av, GEN x)
1296 : {
1297 249450665 : if (is_recursive_t(typ(x)))
1298 : {
1299 213124505 : GENbin *p = copy_bin(x);
1300 213141430 : set_avma(av); return bin_copy(p);
1301 : }
1302 : else
1303 : {
1304 36324921 : set_avma(av);
1305 36325725 : if (x < (GEN)av) {
1306 36118036 : if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
1307 36117020 : x = leafcopy_avma(x, av);
1308 36117036 : set_avma((pari_sp)x);
1309 : } else
1310 207780 : x = leafcopy(x);
1311 36324308 : return x;
1312 : }
1313 : }
1314 :
1315 : INLINE void
1316 47766891 : guncloneNULL(GEN x) { if (x) gunclone(x); }
1317 : INLINE void
1318 1358147 : guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
1319 :
1320 : /* assume 1 <= n < 10 */
1321 : INLINE GEN
1322 106003498 : gc_all(pari_sp av, int n, ...)
1323 : {
1324 : int i;
1325 106003498 : va_list a; va_start(a, n);
1326 106003498 : if (n < 10)
1327 : {
1328 : GEN *v[10];
1329 350570861 : for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
1330 106004430 : set_avma(av);
1331 350573455 : for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
1332 106005467 : va_end(a); return *v[0];
1333 : }
1334 : else
1335 : {
1336 0 : GEN z, **v = (GEN**)pari_malloc(n*sizeof(GEN*));
1337 0 : for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
1338 0 : set_avma(av);
1339 0 : for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
1340 0 : z = *v[0]; pari_free(v); va_end(a); return z;
1341 : }
1342 : }
1343 :
1344 : INLINE void
1345 2201918 : gc_slice(pari_sp av, GEN x, int n)
1346 : {
1347 : int i;
1348 26222076 : for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
1349 2202267 : set_avma(av);
1350 26223011 : for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
1351 2202058 : }
1352 :
1353 : /* p from copy_bin. Copy p->x back to stack, then destroy p */
1354 : INLINE GEN
1355 481775895 : bin_copy(GENbin *p)
1356 : {
1357 : GEN x, y, base;
1358 : long dx, len;
1359 :
1360 481775895 : x = p->x; if (!x) { pari_free(p); return gen_0; }
1361 452777122 : len = p->len;
1362 452777122 : base= p->base; dx = x - base;
1363 452777122 : y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
1364 452771215 : y += dx;
1365 452771215 : p->rebase(y, ((ulong)y-(ulong)x));
1366 452774041 : pari_free(p); return y;
1367 : }
1368 :
1369 : INLINE GEN
1370 934552394 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
1371 :
1372 : INLINE void
1373 100609331 : cgiv(GEN x)
1374 : {
1375 100609331 : pari_sp av = (pari_sp)(x+lg(x));
1376 100609331 : if (isonstack((GEN)av)) set_avma(av);
1377 100620187 : }
1378 :
1379 : INLINE void
1380 1879168 : killblock(GEN x) { gunclone(x); }
1381 :
1382 : INLINE int
1383 290979724 : is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
1384 :
1385 : /*******************************************************************/
1386 : /* */
1387 : /* CONVERSION / ASSIGNMENT */
1388 : /* */
1389 : /*******************************************************************/
1390 : /* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
1391 : INLINE GEN
1392 18004776 : cxcompotor(GEN z, long prec)
1393 : {
1394 18004776 : switch(typ(z))
1395 : {
1396 11784927 : case t_INT: return itor(z, prec);
1397 293370 : case t_FRAC: return fractor(z, prec);
1398 5926751 : case t_REAL: return rtor(z, prec);
1399 0 : default: pari_err_TYPE("cxcompotor",z);
1400 : return NULL; /* LCOV_EXCL_LINE */
1401 : }
1402 : }
1403 : INLINE GEN
1404 8977766 : cxtofp(GEN x, long prec)
1405 8977766 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
1406 :
1407 : INLINE GEN
1408 394215 : cxtoreal(GEN q)
1409 394215 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
1410 :
1411 : INLINE double
1412 60607504 : gtodouble(GEN x)
1413 : {
1414 60607504 : if (typ(x)!=t_REAL) {
1415 8160616 : pari_sp av = avma;
1416 8160616 : x = gtofp(x, DEFAULTPREC);
1417 8160254 : if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
1418 8160254 : set_avma(av);
1419 : }
1420 60607073 : return rtodbl(x);
1421 : }
1422 :
1423 : INLINE int
1424 2967017 : gisdouble(GEN x, double *g)
1425 : {
1426 2967017 : if (typ(x)!=t_REAL) {
1427 41903 : pari_sp av = avma;
1428 41903 : x = gtofp(x, DEFAULTPREC);
1429 41903 : if (typ(x)!=t_REAL) pari_err_TYPE("gisdouble [t_REAL expected]", x);
1430 41903 : set_avma(av);
1431 : }
1432 2967017 : if (expo(x) >= 0x3ff) return 0;
1433 2967017 : *g = rtodbl(x); return 1;
1434 : }
1435 :
1436 : INLINE long
1437 85802455 : gtos(GEN x) {
1438 85802455 : if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
1439 85802441 : return itos(x);
1440 : }
1441 :
1442 : INLINE ulong
1443 102545 : gtou(GEN x) {
1444 102545 : if (typ(x) != t_INT || signe(x)<0)
1445 7 : pari_err_TYPE("gtou [integer >=0 expected]",x);
1446 102539 : return itou(x);
1447 : }
1448 :
1449 : INLINE GEN
1450 45566856 : absfrac(GEN x)
1451 : {
1452 45566856 : GEN y = cgetg(3, t_FRAC);
1453 45566880 : gel(y,1) = absi(gel(x,1));
1454 45566890 : gel(y,2) = icopy(gel(x,2)); return y;
1455 : }
1456 : INLINE GEN
1457 27397 : absfrac_shallow(GEN x)
1458 27397 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
1459 : INLINE GEN
1460 7911056 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
1461 : INLINE GEN
1462 118725 : Q_abs_shallow(GEN x)
1463 118725 : { return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
1464 : INLINE GEN
1465 13083 : R_abs_shallow(GEN x)
1466 13083 : { return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
1467 : INLINE GEN
1468 0 : R_abs(GEN x)
1469 0 : { return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
1470 :
1471 : /* Force z to be of type real/complex with floating point components */
1472 : INLINE GEN
1473 252490554 : gtofp(GEN z, long prec)
1474 : {
1475 252490554 : switch(typ(z))
1476 : {
1477 213708438 : case t_INT: return itor(z, prec);
1478 4097460 : case t_FRAC: return fractor(z, prec);
1479 25951919 : case t_REAL: return rtor(z, prec);
1480 8740590 : case t_COMPLEX: {
1481 8740590 : GEN a = gel(z,1), b = gel(z,2);
1482 8740590 : if (isintzero(b)) return cxcompotor(a, prec);
1483 8740594 : if (isintzero(a)) {
1484 25051 : GEN y = cgetg(3, t_COMPLEX);
1485 25051 : b = cxcompotor(b, prec);
1486 25051 : gel(y,1) = real_0_bit(expo(b) - prec);
1487 25051 : gel(y,2) = b; return y;
1488 : }
1489 8715523 : return cxtofp(z, prec);
1490 : }
1491 0 : case t_QUAD: return quadtofp(z, prec);
1492 0 : default: pari_err_TYPE("gtofp",z);
1493 : return NULL; /* LCOV_EXCL_LINE */
1494 : }
1495 : }
1496 : /* Force z to be of type real / int */
1497 : INLINE GEN
1498 22428 : gtomp(GEN z, long prec)
1499 : {
1500 22428 : switch(typ(z))
1501 : {
1502 42 : case t_INT: return z;
1503 22386 : case t_FRAC: return fractor(z, prec);
1504 0 : case t_REAL: return rtor(z, prec);
1505 0 : case t_QUAD: z = quadtofp(z, prec);
1506 0 : if (typ(z) == t_REAL) return z;
1507 0 : default: pari_err_TYPE("gtomp",z);
1508 : return NULL; /* LCOV_EXCL_LINE */
1509 : }
1510 : }
1511 :
1512 : INLINE GEN
1513 7131510 : RgX_gtofp(GEN x, long prec)
1514 37205443 : { pari_APPLY_pol_normalized(gtofp(gel(x,i), prec)); }
1515 :
1516 : INLINE GEN
1517 33708562 : RgC_gtofp(GEN x, long prec)
1518 217367353 : { pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
1519 :
1520 : INLINE GEN
1521 56 : RgV_gtofp(GEN x, long prec)
1522 4781 : { pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
1523 :
1524 : INLINE GEN
1525 8178202 : RgM_gtofp(GEN x, long prec)
1526 41257603 : { pari_APPLY_same(RgC_gtofp(gel(x,i), prec)) }
1527 :
1528 : INLINE GEN
1529 574 : RgC_gtomp(GEN x, long prec)
1530 23002 : { pari_APPLY_type(t_COL, gtomp(gel(x,i), prec)) }
1531 :
1532 : INLINE GEN
1533 21 : RgM_gtomp(GEN x, long prec)
1534 595 : { pari_APPLY_same(RgC_gtomp(gel(x,i), prec)) }
1535 :
1536 : INLINE GEN
1537 60523 : RgX_fpnorml2(GEN x, long prec)
1538 : {
1539 60523 : pari_sp av = avma;
1540 60523 : return gc_upto(av, gnorml2(RgX_gtofp(x, prec)));
1541 : }
1542 : INLINE GEN
1543 617762 : RgC_fpnorml2(GEN x, long prec)
1544 : {
1545 617762 : pari_sp av = avma;
1546 617762 : return gc_upto(av, gnorml2(RgC_gtofp(x, prec)));
1547 : }
1548 : INLINE GEN
1549 22410 : RgM_fpnorml2(GEN x, long prec)
1550 : {
1551 22410 : pari_sp av = avma;
1552 22410 : return gc_upto(av, gnorml2(RgM_gtofp(x, prec)));
1553 : }
1554 :
1555 : /* y a t_REAL */
1556 : INLINE void
1557 898761 : affgr(GEN x, GEN y)
1558 : {
1559 898761 : switch(typ(x)) {
1560 288967 : case t_INT: affir(x,y); break;
1561 609794 : case t_REAL: affrr(x,y); break;
1562 0 : case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
1563 0 : default: pari_err_TYPE2("=",x,y);
1564 : }
1565 898761 : }
1566 :
1567 : INLINE GEN
1568 249483 : affc_fixlg(GEN x, GEN res)
1569 : {
1570 249483 : if (typ(x) == t_COMPLEX)
1571 : {
1572 206878 : affrr_fixlg(gel(x,1), gel(res,1));
1573 206878 : affrr_fixlg(gel(x,2), gel(res,2));
1574 : }
1575 : else
1576 : {
1577 42605 : set_avma((pari_sp)(res+3));
1578 42605 : res = cgetr(realprec(gel(res,1)));
1579 42605 : affrr_fixlg(x, res);
1580 : }
1581 249483 : return res;
1582 : }
1583 :
1584 : INLINE GEN
1585 0 : trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
1586 :
1587 : /*******************************************************************/
1588 : /* */
1589 : /* LENGTH CONVERSIONS */
1590 : /* */
1591 : /*******************************************************************/
1592 : INLINE long
1593 41465 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
1594 : INLINE long
1595 33195 : ndec2prec(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1596 : INLINE long
1597 8270 : ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1598 : /* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
1599 : * to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
1600 : * addl, shrl instead of subl, sarl, addl */
1601 : INLINE long
1602 12581013 : nbits2nlong(long x) {
1603 12581013 : return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1604 : }
1605 :
1606 : INLINE long
1607 1487200471 : nbits2extraprec(long x) {
1608 1487200471 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1609 : }
1610 :
1611 : INLINE long
1612 185981419 : nbits2prec(long x) {
1613 185981419 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1614 : }
1615 :
1616 : INLINE long
1617 4475684735 : prec2lg(long x) {
1618 4475684735 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1619 : }
1620 : /* ceil(x / sizeof(long)) */
1621 : INLINE long
1622 120814439 : nchar2nlong(long x) {
1623 120814439 : return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
1624 : }
1625 : INLINE long
1626 104859964 : prec2nbits(long x) { return x; }
1627 : INLINE double
1628 3175240 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
1629 : INLINE double
1630 580807 : prec2nbits_mul(long x, double y) { return x * y; }
1631 : INLINE long
1632 141509640 : bit_prec(GEN x) { return realprec(x); }
1633 : INLINE long
1634 3131603435 : bit_accuracy(long x) { return (x-2) * BITS_IN_LONG; }
1635 : INLINE long
1636 9778 : prec2ndec(long x) { return (long)(x * LOG10_2); }
1637 : INLINE long
1638 228 : nbits2ndec(long x) { return prec2ndec(x); }
1639 : INLINE long
1640 475302 : precdbl(long x) {return x << 1;}
1641 : INLINE long
1642 8096062173 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
1643 : INLINE long
1644 8006337155 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
1645 :
1646 : /*********************************************************************/
1647 : /** **/
1648 : /** OPERATIONS MODULO m **/
1649 : /** **/
1650 : /*********************************************************************/
1651 : /* Assume m > 0, more efficient if 0 <= a, b < m */
1652 :
1653 : INLINE GEN
1654 80587148 : Fp_red(GEN a, GEN m) { return modii(a, m); }
1655 : INLINE GEN
1656 173384407 : Fp_add(GEN a, GEN b, GEN m)
1657 : {
1658 173384407 : pari_sp av=avma;
1659 173384407 : GEN p = addii(a,b);
1660 172875825 : long s = signe(p);
1661 172875825 : if (!s) return p; /* = gen_0 */
1662 160054932 : if (s > 0) /* general case */
1663 : {
1664 159800999 : GEN t = subii(p, m);
1665 159653756 : s = signe(t);
1666 159653756 : if (!s) return gc_const(av, gen_0);
1667 151279212 : if (s < 0) return gc_const((pari_sp)p, p);
1668 72265272 : if (cmpii(t, m) < 0) return gc_INT(av, t); /* general case ! */
1669 3289073 : p = remii(t, m);
1670 : }
1671 : else
1672 260297 : p = modii(p, m);
1673 3549452 : return gc_INT(av, p);
1674 : }
1675 : INLINE GEN
1676 21221695 : Fp_double(GEN x, GEN N)
1677 : {
1678 21221695 : GEN z = shifti(x, 1);
1679 20800267 : return cmpii(z, N) >= 0? subii(z, N): z;
1680 : }
1681 : INLINE GEN
1682 203516876 : Fp_sub(GEN a, GEN b, GEN m)
1683 : {
1684 203516876 : pari_sp av=avma;
1685 203516876 : GEN p = subii(a,b);
1686 201842934 : long s = signe(p);
1687 201842934 : if (!s) return p; /* = gen_0 */
1688 186169120 : if (s > 0)
1689 : {
1690 93253108 : if (cmpii(p, m) < 0) return p; /* general case ! */
1691 1451234 : p = remii(p, m);
1692 : }
1693 : else
1694 : {
1695 92916012 : GEN t = addii(p, m);
1696 94197153 : if (!s) return gc_const(av, gen_0);
1697 94189871 : if (s > 0) return gc_INT(av, t); /* general case ! */
1698 94189871 : p = modii(t, m);
1699 : }
1700 95870721 : return gc_INT(av, p);
1701 : }
1702 : INLINE GEN
1703 28824613 : Fp_neg(GEN b, GEN m)
1704 : {
1705 28824613 : pari_sp av = avma;
1706 28824613 : long s = signe(b);
1707 : GEN p;
1708 28824613 : if (!s) return gen_0;
1709 24390289 : if (s > 0)
1710 : {
1711 23264184 : p = subii(m, b);
1712 23264040 : if (signe(p) >= 0) return p; /* general case ! */
1713 387035 : p = modii(p, m);
1714 : } else
1715 1126571 : p = remii(negi(b), m);
1716 1513868 : return gc_INT(av, p);
1717 : }
1718 :
1719 : INLINE GEN
1720 246991 : Fp_halve(GEN a, GEN p)
1721 : {
1722 246991 : if (mpodd(a)) a = addii(a,p);
1723 246991 : return shifti(a,-1);
1724 : }
1725 :
1726 : /* assume 0 <= u < p and ps2 = p>>1 */
1727 : INLINE GEN
1728 75685864 : Fp_center(GEN u, GEN p, GEN ps2)
1729 75685864 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
1730 : /* same without copy */
1731 : INLINE GEN
1732 14135062 : Fp_center_i(GEN u, GEN p, GEN ps2)
1733 14135062 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
1734 :
1735 : /* x + y*z mod p */
1736 : INLINE GEN
1737 11243432 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
1738 : {
1739 : pari_sp av;
1740 11243432 : if (!signe(y) || !signe(z)) return Fp_red(x, p);
1741 10753744 : if (!signe(x)) return Fp_mul(z,y, p);
1742 9755676 : av = avma;
1743 9755676 : return gc_INT(av, modii(addii(x, mulii(y,z)), p));
1744 : }
1745 :
1746 : INLINE GEN
1747 199640193 : Fp_mul(GEN a, GEN b, GEN m)
1748 : {
1749 199640193 : pari_sp av=avma;
1750 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1751 199640193 : (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
1752 199687963 : p = mulii(a,b);
1753 199523158 : set_avma(av); return modii(p,m);
1754 : }
1755 : INLINE GEN
1756 64252039 : Fp_sqr(GEN a, GEN m)
1757 : {
1758 64252039 : pari_sp av=avma;
1759 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1760 64252039 : (void)new_chunk((lg(a)+lg(m))<<1);
1761 64589804 : p = sqri(a);
1762 62830941 : set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
1763 : }
1764 : INLINE GEN
1765 67465162 : Fp_mulu(GEN a, ulong b, GEN m)
1766 : {
1767 67465162 : long l = lgefint(m);
1768 67465162 : if (l == 3)
1769 : {
1770 45876207 : ulong mm = m[2];
1771 45876207 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1772 : } else {
1773 21588955 : pari_sp av = avma;
1774 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1775 21588955 : (void)new_chunk(lg(a)+1+(l<<1));
1776 21562501 : p = muliu(a,b);
1777 21355263 : set_avma(av); return modii(p,m);
1778 : }
1779 : }
1780 : INLINE GEN
1781 17780 : Fp_muls(GEN a, long b, GEN m)
1782 : {
1783 17780 : long l = lgefint(m);
1784 17780 : if (l == 3)
1785 : {
1786 3578 : ulong mm = m[2];
1787 3578 : if (b < 0)
1788 : {
1789 3578 : ulong t = Fl_mul(umodiu(a, mm), -b, mm);
1790 3578 : return t? utoipos(mm - t): gen_0;
1791 : }
1792 : else
1793 0 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1794 : } else {
1795 14202 : pari_sp av = avma;
1796 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1797 14202 : (void)new_chunk(lg(a)+1+(l<<1));
1798 14202 : p = mulis(a,b);
1799 14202 : set_avma(av); return modii(p,m);
1800 : }
1801 : }
1802 :
1803 : INLINE GEN
1804 23224237 : Fp_inv(GEN a, GEN m)
1805 : {
1806 : GEN res;
1807 23224237 : if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
1808 23224083 : return res;
1809 : }
1810 : INLINE GEN
1811 1099920 : Fp_invsafe(GEN a, GEN m)
1812 : {
1813 : GEN res;
1814 1099920 : if (! invmod(a,m,&res)) return NULL;
1815 1099891 : return res;
1816 : }
1817 : INLINE GEN
1818 15035517 : Fp_div(GEN a, GEN b, GEN m)
1819 : {
1820 15035517 : pari_sp av = avma;
1821 : GEN p;
1822 15035517 : if (lgefint(b) == 3)
1823 : {
1824 13890983 : a = Fp_divu(a, b[2], m);
1825 13892162 : if (signe(b) < 0) a = Fp_neg(a, m);
1826 13892166 : return a;
1827 : }
1828 : /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1829 1144534 : (void)new_chunk(lg(a)+(lg(m)<<1));
1830 1144538 : p = mulii(a, Fp_inv(b,m));
1831 1144538 : set_avma(av); return modii(p,m);
1832 : }
1833 : INLINE GEN
1834 15135406 : Fp_divu(GEN x, ulong a, GEN p)
1835 : {
1836 15135406 : pari_sp av = avma;
1837 : ulong b;
1838 15135406 : if (lgefint(p) == 3)
1839 : {
1840 14602388 : ulong pp = p[2], xp = umodiu(x, pp);
1841 14603058 : return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
1842 : }
1843 533018 : x = Fp_red(x, p);
1844 533020 : b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
1845 533020 : return gc_INT(av, diviuexact(addmuliu(x, p, b), a));
1846 : }
1847 :
1848 : INLINE GEN
1849 1094823 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
1850 :
1851 : INLINE GEN
1852 2499134 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1853 :
1854 : INLINE long
1855 2532790 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1856 :
1857 : INLINE long
1858 1929077 : get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
1859 :
1860 : INLINE GEN
1861 343 : get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1862 :
1863 : INLINE long
1864 375375 : get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1865 :
1866 : INLINE long
1867 200851 : get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1868 :
1869 : INLINE GEN
1870 25407882 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1871 :
1872 : INLINE long
1873 67623774 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1874 :
1875 : INLINE long
1876 81885998 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1877 :
1878 : INLINE GEN
1879 8033 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1880 :
1881 : INLINE long
1882 264420 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1883 :
1884 : INLINE long
1885 317565 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1886 :
1887 : INLINE GEN
1888 3393089 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1889 :
1890 : INLINE long
1891 7237244 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1892 :
1893 : INLINE long
1894 6005768 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1895 :
1896 : INLINE GEN
1897 146583 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1898 :
1899 : INLINE long
1900 51630 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1901 :
1902 : INLINE long
1903 3589 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1904 :
1905 : /*******************************************************************/
1906 : /* */
1907 : /* ADDMULII / SUBMULII */
1908 : /* */
1909 : /*******************************************************************/
1910 : /* x - y*z */
1911 : INLINE GEN
1912 32800208 : submulii(GEN x, GEN y, GEN z)
1913 : {
1914 32800208 : long lx = lgefint(x), ly, lz;
1915 : pari_sp av;
1916 : GEN t;
1917 32800208 : if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
1918 30268097 : ly = lgefint(y);
1919 30268097 : if (ly == 2) return icopy(x);
1920 28999841 : lz = lgefint(z);
1921 28999841 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1922 28999841 : t = mulii(z, y);
1923 28999841 : set_avma(av); return subii(x,t);
1924 : }
1925 : /* y*z - x */
1926 : INLINE GEN
1927 3847500 : mulsubii(GEN y, GEN z, GEN x)
1928 : {
1929 3847500 : long lx = lgefint(x), ly, lz;
1930 : pari_sp av;
1931 : GEN t;
1932 3847500 : if (lx == 2) return mulii(z,y);
1933 2428826 : ly = lgefint(y);
1934 2428826 : if (ly == 2) return negi(x);
1935 2151276 : lz = lgefint(z);
1936 2151276 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1937 2151309 : t = mulii(z, y);
1938 2151277 : set_avma(av); return subii(t,x);
1939 : }
1940 :
1941 : /* x - u*y */
1942 : INLINE GEN
1943 7728 : submuliu(GEN x, GEN y, ulong u)
1944 : {
1945 : pari_sp av;
1946 7728 : long ly = lgefint(y);
1947 7728 : if (ly == 2) return icopy(x);
1948 7728 : av = avma;
1949 7728 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1950 7728 : y = mului(u,y);
1951 7728 : set_avma(av); return subii(x, y);
1952 : }
1953 : /* x + u*y */
1954 : INLINE GEN
1955 541123 : addmuliu(GEN x, GEN y, ulong u)
1956 : {
1957 : pari_sp av;
1958 541123 : long ly = lgefint(y);
1959 541123 : if (ly == 2) return icopy(x);
1960 541123 : av = avma;
1961 541123 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1962 541123 : y = mului(u,y);
1963 541123 : set_avma(av); return addii(x, y);
1964 : }
1965 : /* x - u*y */
1966 : INLINE GEN
1967 59902581 : submuliu_inplace(GEN x, GEN y, ulong u)
1968 : {
1969 : pari_sp av;
1970 59902581 : long ly = lgefint(y);
1971 59902581 : if (ly == 2) return x;
1972 38009930 : av = avma;
1973 38009930 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1974 38013606 : y = mului(u,y);
1975 38013510 : set_avma(av); return subii(x, y);
1976 : }
1977 : /* x + u*y */
1978 : INLINE GEN
1979 59404646 : addmuliu_inplace(GEN x, GEN y, ulong u)
1980 : {
1981 : pari_sp av;
1982 59404646 : long ly = lgefint(y);
1983 59404646 : if (ly == 2) return x;
1984 37917293 : av = avma;
1985 37917293 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1986 37920938 : y = mului(u,y);
1987 37920885 : set_avma(av); return addii(x, y);
1988 : }
1989 : /* ux + vy */
1990 : INLINE GEN
1991 37839664 : lincombii(GEN u, GEN v, GEN x, GEN y)
1992 : {
1993 37839664 : long lx = lgefint(x), ly;
1994 : GEN p1, p2;
1995 : pari_sp av;
1996 37839664 : if (lx == 2) return mulii(v,y);
1997 23791703 : ly = lgefint(y);
1998 23791703 : if (ly == 2) return mulii(u,x);
1999 21109003 : av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
2000 21111852 : p1 = mulii(u,x);
2001 21110814 : p2 = mulii(v,y);
2002 21110668 : set_avma(av); return addii(p1,p2);
2003 : }
2004 :
2005 : /*******************************************************************/
2006 : /* */
2007 : /* GEN SUBTYPES */
2008 : /* */
2009 : /*******************************************************************/
2010 :
2011 : INLINE int
2012 4890735732 : is_const_t(long t) { return (t < t_POLMOD); }
2013 : INLINE int
2014 6876 : is_extscalar_t(long t) { return (t <= t_POL); }
2015 : INLINE int
2016 9574716 : is_intreal_t(long t) { return (t <= t_REAL); }
2017 : INLINE int
2018 659795614 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
2019 : INLINE int
2020 85605254 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
2021 : INLINE int
2022 0 : is_qfb_t(long t) { return (t == t_QFB); }
2023 : INLINE int
2024 5016135 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
2025 : INLINE int
2026 62655169 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
2027 : INLINE int
2028 9153510908 : is_recursive_t(long t) { return lontyp[t]; }
2029 : INLINE int
2030 377610878 : is_scalar_t(long t) { return (t < t_POL); }
2031 : INLINE int
2032 43395466 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
2033 :
2034 : INLINE int
2035 67771247 : qfb_is_qfi(GEN q) { return signe(gel(q,4)) < 0; }
2036 :
2037 : /*******************************************************************/
2038 : /* */
2039 : /* TRANSCENDENTAL */
2040 : /* */
2041 : /*******************************************************************/
2042 : INLINE GEN
2043 79922354 : sqrtr(GEN x) {
2044 79922354 : long s = signe(x);
2045 79922354 : if (s == 0) return real_0_bit(expo(x) >> 1);
2046 79889239 : if (s >= 0) return sqrtr_abs(x);
2047 432929 : retmkcomplex(gen_0, sqrtr_abs(x));
2048 : }
2049 : INLINE GEN
2050 0 : cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
2051 : INLINE GEN
2052 0 : cbrtr(GEN x) {
2053 0 : long s = signe(x);
2054 : GEN r;
2055 0 : if (s == 0) return real_0_bit(expo(x) / 3);
2056 0 : r = cbrtr_abs(x);
2057 0 : if (s < 0) togglesign(r);
2058 0 : return r;
2059 : }
2060 : INLINE GEN
2061 2794955 : sqrtnr(GEN x, long n) {
2062 2794955 : long s = signe(x);
2063 : GEN r;
2064 2794955 : if (s == 0) return real_0_bit(expo(x) / n);
2065 2794955 : r = sqrtnr_abs(x, n);
2066 2794974 : if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
2067 2794975 : return r;
2068 : }
2069 : INLINE long
2070 752098 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
2071 : INLINE ulong
2072 1717079 : ulogint(ulong B, ulong y)
2073 : {
2074 : ulong r;
2075 : long e;
2076 1717079 : if (y == 2) return expu(B);
2077 1669051 : r = y;
2078 5042289 : for (e=1;; e++)
2079 : { /* here, r = y^e, r2 = y^(e-1) */
2080 5042289 : if (r >= B) return r == B? e: e-1;
2081 3374775 : r = umuluu_or_0(y, r);
2082 3374792 : if (!r) return e;
2083 : }
2084 : }
2085 :
2086 : /*******************************************************************/
2087 : /* */
2088 : /* MISCELLANEOUS */
2089 : /* */
2090 : /*******************************************************************/
2091 9574613 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
2092 2804655999 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
2093 17088572 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
2094 5702429 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
2095 1289033503 : INLINE int equali1(GEN n)
2096 1289033503 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
2097 139009988 : INLINE int equalim1(GEN n)
2098 139009988 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
2099 : /* works only for POSITIVE integers */
2100 2175918733 : INLINE int is_pm1(GEN n)
2101 2175918733 : { return lgefint(n) == 3 && n[2] == 1; }
2102 558620052 : INLINE int is_bigint(GEN n)
2103 558620052 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
2104 :
2105 2054639402 : INLINE int odd(long x) { return x & 1; }
2106 117218836 : INLINE int both_odd(long x, long y) { return x & y & 1; }
2107 :
2108 : INLINE int
2109 8127510694 : isonstack(GEN x)
2110 8127510694 : { return ((pari_sp)x >= pari_mainstack->bot
2111 8127510694 : && (pari_sp)x < pari_mainstack->top); }
2112 :
2113 : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
2114 : INLINE double
2115 93100434 : dbllog2r(GEN x)
2116 93100434 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
2117 :
2118 : INLINE GEN
2119 2512524 : mul_content(GEN cx, GEN cy)
2120 : {
2121 2512524 : if (!cx) return cy;
2122 520328 : if (!cy) return cx;
2123 333031 : return gmul(cx,cy);
2124 : }
2125 : INLINE GEN
2126 0 : inv_content(GEN c) { return c? ginv(c): NULL; }
2127 : INLINE GEN
2128 57979 : div_content(GEN cx, GEN cy)
2129 : {
2130 57979 : if (!cy) return cx;
2131 56845 : if (!cx) return ginv(cy);
2132 25774 : return gdiv(cx,cy);
2133 : }
2134 : INLINE GEN
2135 10294342 : mul_denom(GEN dx, GEN dy)
2136 : {
2137 10294342 : if (!dx) return dy;
2138 1835974 : if (!dy) return dx;
2139 1368189 : return mulii(dx,dy);
2140 : }
2141 :
2142 : /* POLYNOMIALS */
2143 : INLINE GEN
2144 1439452 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
2145 : INLINE GEN
2146 169166264 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
2147 : INLINE ulong
2148 980167 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
2149 : INLINE ulong
2150 330291 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
2151 : INLINE long
2152 5350677076 : degpol(GEN x) { return lg(x)-3; }
2153 : INLINE long
2154 2635133249 : lgpol(GEN x) { return lg(x)-2; }
2155 : INLINE long
2156 341395958 : lgcols(GEN x) { return lg(gel(x,1)); }
2157 : INLINE long
2158 153674260 : nbrows(GEN x) { return lg(gel(x,1))-1; }
2159 : INLINE GEN
2160 0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
2161 :
2162 : INLINE GEN
2163 2128329 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
2164 : INLINE GEN
2165 950810 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
2166 :
2167 : INLINE GEN
2168 103509890 : RgX_copy(GEN x)
2169 377107777 : { pari_APPLY_pol_normalized(gcopy(gel(x,i))); }
2170 : /* have to use ulong to avoid silly warnings from gcc "assuming signed
2171 : * overflow does not occur" */
2172 : INLINE GEN
2173 2551519 : RgX_coeff(GEN x, long n)
2174 : {
2175 2551519 : ulong l = lg(x);
2176 2551519 : return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
2177 : }
2178 : INLINE GEN
2179 349807 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
2180 : INLINE GEN
2181 7053693 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
2182 : INLINE GEN
2183 1904 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
2184 : INLINE GEN
2185 126529 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
2186 : INLINE GEN
2187 5442101 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
2188 : INLINE GEN
2189 1995647 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
2190 : INLINE GEN
2191 21394271 : Flx_div_pre(GEN x, GEN y, ulong p, ulong pi)
2192 21394271 : { return Flx_divrem_pre(x,y,p,pi, NULL); }
2193 : INLINE GEN
2194 1567217 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
2195 : INLINE GEN
2196 0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
2197 : INLINE GEN
2198 101748526 : pol0_Flx(long sv) { return mkvecsmall(sv); }
2199 : INLINE GEN
2200 54577304 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
2201 : INLINE GEN
2202 37587965 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
2203 : INLINE GEN
2204 0 : zero_zx(long sv) { return zero_Flx(sv); }
2205 : INLINE GEN
2206 0 : polx_zx(long sv) { return polx_Flx(sv); }
2207 : INLINE GEN
2208 0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
2209 : INLINE GEN
2210 14049 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
2211 : INLINE GEN
2212 1350 : zero_F2x(long sv) { return zero_Flx(sv); }
2213 : INLINE GEN
2214 13247651 : pol0_F2x(long sv) { return pol0_Flx(sv); }
2215 : INLINE GEN
2216 3955489 : pol1_F2x(long sv) { return pol1_Flx(sv); }
2217 : INLINE GEN
2218 1076046 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
2219 : INLINE int
2220 2873727 : F2x_equal1(GEN x) { return Flx_equal1(x); }
2221 : INLINE int
2222 4664407 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
2223 : INLINE GEN
2224 60466988 : F2x_copy(GEN x) { return leafcopy(x); }
2225 : INLINE GEN
2226 4487 : F2v_copy(GEN x) { return leafcopy(x); }
2227 : INLINE GEN
2228 2545379 : Flv_copy(GEN x) { return leafcopy(x); }
2229 : INLINE GEN
2230 172089752 : Flx_copy(GEN x) { return leafcopy(x); }
2231 : INLINE GEN
2232 4366125 : vecsmall_copy(GEN x) { return leafcopy(x); }
2233 : INLINE int
2234 8318559 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
2235 : INLINE int
2236 12549 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
2237 : INLINE int
2238 7328029 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
2239 :
2240 : INLINE GEN
2241 157046654 : ZX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2242 : INLINE GEN
2243 145008524 : FpX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2244 : INLINE GEN
2245 849896 : FpXX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2246 : INLINE GEN
2247 4693528 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2248 : INLINE GEN
2249 115237645 : F2x_renormalize(GEN x, long lx) { return Flx_renormalize(x,lx); }
2250 :
2251 : INLINE GEN
2252 0 : F2xX_shift(GEN a, long n, long vs) { return FlxX_shift(a, n, vs); }
2253 :
2254 : INLINE GEN
2255 37958 : F2v_to_F2x(GEN x, long sv) {
2256 37958 : GEN y = leafcopy(x);
2257 37958 : y[1] = sv; F2x_renormalize(y, lg(y)); return y;
2258 : }
2259 :
2260 : INLINE long
2261 385 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
2262 :
2263 : INLINE long
2264 5796 : gval(GEN x, long v)
2265 5796 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
2266 :
2267 : INLINE void
2268 479266 : RgX_shift_inplace_init(long v)
2269 479266 : { if (v) (void)cgetg(v, t_VECSMALL); }
2270 : /* shift polynomial in place. assume v free cells have been left before x */
2271 : INLINE GEN
2272 479266 : RgX_shift_inplace(GEN x, long v)
2273 : {
2274 : long i, lx;
2275 : GEN z;
2276 479266 : if (!v) return x;
2277 252260 : lx = lg(x);
2278 252260 : if (lx == 2) return x;
2279 252260 : z = x + lx;
2280 : /* stackdummy's from normalizepol */
2281 252260 : while (lg(z) != v) z += lg(z);
2282 252260 : z += v;
2283 4637681 : for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
2284 672402 : for (i = 0; i < v; i++) gel(--z,0) = gen_0;
2285 252260 : z -= 2;
2286 252260 : z[1] = x[1];
2287 252260 : z[0] = evaltyp(t_POL) | evallg(lx+v);
2288 252260 : stackdummy((pari_sp)z, (pari_sp)x); return z;
2289 : }
2290 :
2291 :
2292 : /* LINEAR ALGEBRA */
2293 : INLINE GEN
2294 502527 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
2295 : INLINE GEN
2296 7747174 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
2297 : INLINE GEN
2298 3191091 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
2299 : INLINE GEN
2300 0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
2301 : INLINE GEN
2302 104146 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
2303 : INLINE GEN
2304 0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
2305 : INLINE GEN
2306 0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
2307 : INLINE GEN
2308 36171070 : zero_zv(long x) { return zero_Flv(x); }
2309 : INLINE GEN
2310 266 : zm_transpose(GEN x) { return Flm_transpose(x); }
2311 : INLINE GEN
2312 0 : zm_copy(GEN x) { return Flm_copy(x); }
2313 : INLINE GEN
2314 2183679 : zv_copy(GEN x) { return Flv_copy(x); }
2315 : INLINE GEN
2316 0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
2317 :
2318 : INLINE GEN
2319 10191827 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfdivrem(x,y,NULL); }
2320 : INLINE GEN
2321 326773 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
2322 : INLINE GEN
2323 6933525 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
2324 : INLINE void
2325 9404876 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
2326 : INLINE GEN
2327 21441396 : RgM_shallowcopy(GEN x)
2328 93327404 : { pari_APPLY_same(leafcopy(gel(x,i))); }
2329 : INLINE GEN
2330 198018 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
2331 :
2332 : INLINE GEN
2333 0 : F3m_copy(GEN x) { return RgM_shallowcopy(x); }
2334 :
2335 : INLINE GEN
2336 6275334 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
2337 :
2338 : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
2339 : * x,y are ZV of the same length */
2340 : INLINE int
2341 23485 : ZV_dvd(GEN x, GEN y)
2342 : {
2343 23485 : long i, l = lg(x);
2344 37856 : for (i=1; i < l; i++)
2345 30464 : if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
2346 7392 : return 1;
2347 : }
2348 : INLINE GEN
2349 539146 : ZM_ZV_mod(GEN x, GEN y)
2350 1537993 : { pari_APPLY_same(ZV_ZV_mod(gel(x,i), y)) }
2351 : INLINE GEN
2352 7322936 : ZV_ZV_mod(GEN x, GEN y)
2353 18920842 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
2354 : INLINE GEN
2355 0 : vecmodii(GEN x, GEN y) { return ZV_ZV_mod(x,y); }
2356 : INLINE GEN
2357 174881 : vecmoduu(GEN x, GEN y) { pari_APPLY_ulong(((ulong*)x)[i] % ((ulong*)y)[i]) }
2358 :
2359 : /* Fq */
2360 : INLINE GEN
2361 3106455 : Fq_red(GEN x, GEN T, GEN p)
2362 3106455 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
2363 : INLINE GEN
2364 73792 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
2365 : {
2366 : (void) p;
2367 73792 : return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
2368 : }
2369 : INLINE GEN
2370 756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
2371 :
2372 : INLINE GEN
2373 14595 : gener_Fq_local(GEN T, GEN p, GEN L)
2374 0 : { return T? gener_FpXQ_local(T,p, L)
2375 14595 : : pgener_Fp_local(p, L); }
2376 :
2377 : INLINE GEN
2378 0 : random_Fq(GEN T, GEN p)
2379 0 : { return T ? random_FpX(get_FpX_degree(T), get_FpX_var(T), p): randomi(p); }
2380 :
2381 : /* FpXQX */
2382 : INLINE GEN
2383 4526 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
2384 : INLINE GEN
2385 0 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
2386 : INLINE GEN
2387 165415 : FlxqX_div_pre(GEN x, GEN y, GEN T, ulong p, ulong pi) { return FlxqX_divrem_pre(x, y, T, p, pi, NULL); }
2388 : INLINE GEN
2389 33173 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
2390 :
2391 : INLINE GEN
2392 20587 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
2393 20587 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
2394 :
2395 : /* FqX */
2396 : INLINE GEN
2397 25354 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
2398 : INLINE GEN
2399 127694 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2400 : INLINE GEN
2401 24956 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
2402 : INLINE GEN
2403 68607 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2404 : INLINE GEN
2405 568018 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
2406 568018 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
2407 : INLINE GEN
2408 427288 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
2409 427288 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
2410 : INLINE GEN
2411 367097 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
2412 367097 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
2413 : INLINE GEN
2414 21942 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
2415 21942 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
2416 : INLINE GEN
2417 71855 : FqX_sqr(GEN x, GEN T, GEN p)
2418 71855 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
2419 : INLINE GEN
2420 1295 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
2421 1295 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
2422 : INLINE GEN
2423 19411 : FqX_halve(GEN x, GEN T, GEN p)
2424 19411 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
2425 : INLINE GEN
2426 38516 : FqX_div(GEN x, GEN y, GEN T, GEN p)
2427 38516 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
2428 : INLINE GEN
2429 6090 : FqX_get_red(GEN S, GEN T, GEN p)
2430 6090 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
2431 : INLINE GEN
2432 37743 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
2433 37743 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
2434 : INLINE GEN
2435 0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
2436 0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
2437 : INLINE GEN
2438 69629 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
2439 69629 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
2440 : INLINE GEN
2441 0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
2442 0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
2443 : INLINE GEN
2444 8190 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
2445 8190 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
2446 : INLINE GEN
2447 442857 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
2448 442857 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
2449 : INLINE GEN
2450 4095 : FqX_normalize(GEN z, GEN T, GEN p)
2451 4095 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
2452 : INLINE GEN
2453 40061 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
2454 : INLINE long
2455 683802 : FqX_is_squarefree(GEN P, GEN T, GEN p)
2456 683802 : { return T ? FpXQX_is_squarefree(P, T, p): FpX_is_squarefree(P, p); }
2457 : INLINE GEN
2458 0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
2459 : INLINE GEN
2460 140897 : FqX_factor(GEN f, GEN T, GEN p)
2461 140897 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
2462 : INLINE GEN
2463 7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
2464 7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
2465 : INLINE GEN
2466 7 : FqX_ddf(GEN f, GEN T, GEN p)
2467 7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
2468 : INLINE GEN
2469 47786 : FqX_degfact(GEN f, GEN T, GEN p)
2470 47786 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
2471 : INLINE GEN
2472 7273 : FqX_roots(GEN f, GEN T, GEN p)
2473 7273 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
2474 : INLINE GEN
2475 175 : FqX_to_mod(GEN f, GEN T, GEN p)
2476 175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
2477 :
2478 : /*FqXQ*/
2479 : INLINE GEN
2480 0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2481 0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2482 : INLINE GEN
2483 0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2484 0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2485 : INLINE GEN
2486 0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
2487 0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
2488 : INLINE GEN
2489 0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
2490 0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
2491 : INLINE GEN
2492 0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
2493 0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
2494 : INLINE GEN
2495 22766 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
2496 22766 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
2497 : INLINE GEN
2498 0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
2499 0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
2500 : INLINE GEN
2501 0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
2502 0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
2503 :
2504 : /*FqXn*/
2505 : INLINE GEN
2506 6590 : FqXn_expint(GEN x, long n, GEN T, GEN p)
2507 6590 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
2508 : INLINE GEN
2509 0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
2510 0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
2511 : INLINE GEN
2512 7126 : FqXn_inv(GEN x, long n, GEN T, GEN p)
2513 7126 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
2514 : INLINE GEN
2515 263403 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
2516 263403 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
2517 : INLINE GEN
2518 0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
2519 0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
2520 :
2521 : /*FpXQ*/
2522 : INLINE GEN
2523 0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
2524 0 : { (void)T; return FpX_add(x,y,p); }
2525 : INLINE GEN
2526 0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
2527 0 : { (void)T; return FpX_sub(x,y,p); }
2528 :
2529 : /*Flxq*/
2530 : INLINE GEN
2531 0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
2532 0 : { (void)T; return Flx_add(x,y,p); }
2533 : INLINE GEN
2534 0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
2535 0 : { (void)T; return Flx_sub(x,y,p); }
2536 :
2537 : /* F2x */
2538 :
2539 : INLINE ulong
2540 667612120 : F2x_coeff(GEN x,long v)
2541 : {
2542 667612120 : ulong u=(ulong)x[2+divsBIL(v)];
2543 667612052 : return (u>>remsBIL(v))&1UL;
2544 : }
2545 :
2546 : INLINE void
2547 12264926 : F2x_clear(GEN x,long v)
2548 : {
2549 12264926 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2550 12264893 : *u&=~(1UL<<remsBIL(v));
2551 12264878 : }
2552 :
2553 : INLINE void
2554 124705795 : F2x_set(GEN x,long v)
2555 : {
2556 124705795 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2557 124703716 : *u|=1UL<<remsBIL(v);
2558 124704571 : }
2559 :
2560 : INLINE void
2561 1554151 : F2x_flip(GEN x,long v)
2562 : {
2563 1554151 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2564 1554151 : *u^=1UL<<remsBIL(v);
2565 1554151 : }
2566 :
2567 : /* F2v */
2568 :
2569 : INLINE ulong
2570 660985094 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
2571 :
2572 : INLINE void
2573 12264922 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
2574 :
2575 : INLINE void
2576 101339625 : F2v_set(GEN x,long v) { F2x_set(x,v-1); }
2577 :
2578 : INLINE void
2579 1554145 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
2580 :
2581 : /* F2m */
2582 :
2583 : INLINE ulong
2584 8022032 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
2585 :
2586 : INLINE void
2587 0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
2588 :
2589 : INLINE void
2590 798 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
2591 :
2592 : INLINE void
2593 1554145 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
2594 :
2595 : /* F3m */
2596 :
2597 : INLINE ulong
2598 3247912 : F3m_coeff(GEN x, long a, long b) { return F3v_coeff(gel(x,b), a); }
2599 :
2600 : INLINE void
2601 0 : F3m_set(GEN x, long a, long b, ulong c) { F3v_set(gel(x,b), a, c); }
2602 :
2603 : /* ARITHMETIC */
2604 : INLINE GEN
2605 3885 : matpascal(long n) { return matqpascal(n, NULL); }
2606 : INLINE long
2607 458554 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
2608 : INLINE long
2609 98 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
2610 : INLINE GEN
2611 6591068 : sqrti(GEN x) { return sqrtremi(x,NULL); }
2612 : INLINE GEN
2613 123174043 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
2614 : INLINE int
2615 790656 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
2616 : INLINE int
2617 29176 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
2618 : INLINE GEN
2619 0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
2620 : INLINE GEN
2621 0 : gminsg(long s, GEN y) { return gmings(y,s); }
2622 : INLINE GEN
2623 22044774 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
2624 : INLINE GEN
2625 124179972 : gmulgu(GEN y, ulong s) { return gmulug(s,y); }
2626 : INLINE GEN
2627 1718382 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
2628 : INLINE GEN
2629 529208 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
2630 :
2631 : INLINE GEN
2632 13509997 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
2633 : INLINE GEN
2634 874870 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
2635 :
2636 : /* x t_COMPLEX */
2637 : INLINE GEN
2638 90645617 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
2639 : /* q t_QUAD */
2640 : INLINE GEN
2641 69881 : quadnorm(GEN q)
2642 : {
2643 69881 : GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
2644 69881 : GEN z, u = gel(q,3), v = gel(q,2);
2645 69881 : if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
2646 : {
2647 69671 : z = signe(b)? mulii(v, addii(u,v)): sqri(v);
2648 69671 : return addii(z, mulii(c, sqri(u)));
2649 : }
2650 : else
2651 : {
2652 210 : z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
2653 210 : return gadd(z, gmul(c, gsqr(u)));
2654 : }
2655 : }
2656 : /* x a t_QUAD, return the attached discriminant */
2657 : INLINE GEN
2658 1085 : quad_disc(GEN x)
2659 : {
2660 1085 : GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
2661 1085 : if (is_pm1(b)) return subsi(1, c4);
2662 154 : togglesign_safe(&c4); return c4;
2663 : }
2664 : INLINE GEN
2665 4848255 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
2666 : INLINE GEN
2667 18470596 : qfb_disc(GEN x) { return gel(x,4); }
2668 :
2669 : INLINE GEN
2670 4136417 : sqrfrac(GEN x)
2671 : {
2672 4136417 : GEN z = cgetg(3,t_FRAC);
2673 4136413 : gel(z,1) = sqri(gel(x,1));
2674 4136397 : gel(z,2) = sqri(gel(x,2)); return z;
2675 : }
2676 :
2677 : INLINE void
2678 17001943 : normalize_frac(GEN z) {
2679 17001943 : if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
2680 17001943 : }
2681 :
2682 : INLINE GEN
2683 42891926 : powii(GEN x, GEN n)
2684 : {
2685 42891926 : long ln = lgefint(n);
2686 42891926 : if (ln == 3) {
2687 : GEN z;
2688 42868957 : if (signe(n) > 0) return powiu(x, n[2]);
2689 74826 : z = cgetg(3, t_FRAC);
2690 74835 : gel(z,1) = gen_1;
2691 74835 : gel(z,2) = powiu(x, n[2]);
2692 74836 : return z;
2693 : }
2694 22969 : if (ln == 2) return gen_1; /* rare */
2695 : /* should never happen */
2696 0 : return powgi(x, n); /* overflow unless x = 0, 1, -1 */
2697 : }
2698 : INLINE GEN
2699 1561 : powIs(long n) {
2700 1561 : switch(n & 3)
2701 : {
2702 63 : case 1: return mkcomplex(gen_0,gen_1);
2703 385 : case 2: return gen_m1;
2704 707 : case 3: return mkcomplex(gen_0,gen_m1);
2705 : }
2706 406 : return gen_1;
2707 : }
2708 :
2709 : /*******************************************************************/
2710 : /* */
2711 : /* ELLIPTIC CURVES */
2712 : /* */
2713 : /*******************************************************************/
2714 7116193 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
2715 5709709 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
2716 6912840 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
2717 6522710 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
2718 7961483 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
2719 5432770 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
2720 2135641 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
2721 3055105 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
2722 2608978 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
2723 8021747 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
2724 8850387 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
2725 11279895 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
2726 1295726 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
2727 11588481 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
2728 2036874 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
2729 800031 : INLINE GEN ellff_get_a4a6(GEN x) { return gmael(x, 15, 2); }
2730 1463 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
2731 308 : INLINE long ellQp_get_prec(GEN E) { return valp(ellQp_get_zero(E)); }
2732 1134 : INLINE GEN ellQp_get_p(GEN E) { return padic_p(ellQp_get_zero(E)); }
2733 232582 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
2734 371325 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
2735 2027616 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
2736 126 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
2737 :
2738 4677535 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
2739 51126915 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
2740 1868471 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
2741 :
2742 : /*******************************************************************/
2743 : /* */
2744 : /* ALGEBRAIC NUMBER THEORY */
2745 : /* */
2746 : /*******************************************************************/
2747 30053091 : INLINE GEN modpr_get_pr(GEN x) { return gel(x,3); }
2748 1888446 : INLINE GEN modpr_get_p(GEN x) { return pr_get_p(modpr_get_pr(x)); }
2749 13187773 : INLINE GEN modpr_get_T(GEN x) { return lg(x) == 4? NULL: gel(x,4); }
2750 :
2751 89368002 : INLINE GEN pr_get_p(GEN pr) { return gel(pr,1); }
2752 11508513 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
2753 : /* .[2] instead of itos works: e and f are small positive integers */
2754 15718178 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
2755 25340376 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
2756 47841210 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
2757 : INLINE int
2758 16398994 : pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
2759 : INLINE GEN
2760 1066803 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
2761 : INLINE ulong
2762 343931 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
2763 :
2764 : /* assume nf a genuine nf */
2765 : INLINE long
2766 1167401 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
2767 : INLINE GEN
2768 94435221 : nf_get_pol(GEN nf) { return gel(nf,1); }
2769 : INLINE long
2770 68812104 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
2771 : INLINE long
2772 22160284 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
2773 : INLINE long
2774 4140 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
2775 : INLINE GEN
2776 456915 : nf_get_disc(GEN nf) { return gel(nf,3); }
2777 : INLINE GEN
2778 3580353 : nf_get_index(GEN nf) { return gel(nf,4); }
2779 : INLINE GEN
2780 20766050 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
2781 : INLINE GEN
2782 301248 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
2783 : INLINE GEN
2784 1685950 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
2785 : INLINE GEN
2786 22080 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
2787 : INLINE GEN
2788 4438 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
2789 : INLINE GEN
2790 59395 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
2791 : INLINE GEN
2792 1531163 : nf_get_roots(GEN nf) { return gel(nf,6); }
2793 : INLINE GEN
2794 4382 : nf_get_zk(GEN nf)
2795 : {
2796 4382 : GEN y = gel(nf,7), D = gel(y, 1);
2797 4382 : if (typ(D) == t_POL) D = gel(D, 2);
2798 4382 : if (!equali1(D)) y = gdiv(y, D);
2799 4382 : return y;
2800 : }
2801 : INLINE GEN
2802 5021414 : nf_get_zkprimpart(GEN nf)
2803 : {
2804 5021414 : GEN y = gel(nf,7);
2805 : /* test for old format of nf.zk: non normalized */
2806 5021414 : if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
2807 5021399 : return y;
2808 : }
2809 : INLINE GEN
2810 5067299 : nf_get_zkden(GEN nf)
2811 : {
2812 5067299 : GEN y = gel(nf,7), D = gel(y,1);
2813 5067299 : if (typ(D) == t_POL) D = gel(D,2);
2814 : /* test for old format of nf.zk: non normalized */
2815 5067299 : if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
2816 5067279 : return D;
2817 : }
2818 : INLINE GEN
2819 9487871 : nf_get_invzk(GEN nf) { return gel(nf,8); }
2820 : INLINE void
2821 516827 : nf_get_sign(GEN nf, long *r1, long *r2)
2822 : {
2823 516827 : GEN x = gel(nf,2);
2824 516827 : *r1 = itou(gel(x,1));
2825 516827 : *r2 = itou(gel(x,2));
2826 516827 : }
2827 :
2828 : INLINE GEN
2829 2521119 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
2830 : INLINE GEN
2831 391883 : abgrp_get_no(GEN x) { return gel(x,1); }
2832 : INLINE GEN
2833 11549250 : abgrp_get_cyc(GEN x) { return gel(x,2); }
2834 : INLINE GEN
2835 1653654 : abgrp_get_gen(GEN x) { return gel(x,3); }
2836 : INLINE GEN
2837 9425757 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
2838 : INLINE GEN
2839 5097431 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
2840 : INLINE GEN
2841 8358 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
2842 : INLINE GEN
2843 3509046 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
2844 : INLINE GEN
2845 1579859 : bnf_get_gen(GEN bnf) { return abgrp_get_gen(bnf_get_clgp(bnf)); }
2846 : INLINE GEN
2847 641 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
2848 : INLINE GEN
2849 2702019 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
2850 : INLINE GEN
2851 1278292 : bnf_get_sunits(GEN bnf)
2852 1278292 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
2853 : INLINE GEN
2854 290540 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
2855 : INLINE long
2856 265199 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
2857 : INLINE GEN
2858 288183 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
2859 : INLINE GEN
2860 26292 : nfV_to_scalar_or_alg(GEN nf, GEN x)
2861 63433 : { pari_APPLY_same(nf_to_scalar_or_alg(nf, gel(x,i))) }
2862 : INLINE GEN
2863 25852 : bnf_get_fu(GEN bnf) {
2864 25852 : GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
2865 25851 : if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
2866 25851 : return nfV_to_scalar_or_alg(nf, vecslice(fu, 2, lg(fu)-1));
2867 : }
2868 :
2869 : INLINE GEN
2870 3632291 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
2871 : INLINE GEN
2872 1936924 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
2873 : INLINE GEN
2874 141001 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
2875 : INLINE GEN
2876 1260070 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
2877 : INLINE GEN
2878 2728661 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
2879 : INLINE GEN
2880 314570 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
2881 : INLINE GEN
2882 2396484 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
2883 : INLINE GEN
2884 70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
2885 : INLINE GEN
2886 7847 : bnr_get_gen(GEN bnr) {
2887 7847 : GEN G = bnr_get_clgp(bnr);
2888 7847 : if (lg(G) != 4)
2889 0 : pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
2890 7847 : return gel(G,3);
2891 : }
2892 :
2893 : /* localstar, used in gchar */
2894 : INLINE GEN
2895 51955 : locs_get_cyc(GEN locs) { return gel(locs,1); }
2896 : INLINE GEN
2897 191695 : locs_get_Lsprk(GEN locs) { return gel(locs,2); }
2898 : INLINE GEN
2899 1260 : locs_get_Lgenfil(GEN locs) { return gel(locs,3); }
2900 : INLINE GEN
2901 8225 : locs_get_mod(GEN locs) { return gel(locs,4); }
2902 : /* pr dividing the modulus N of locs, 0 <= i < v_pr(N)
2903 : * return a t_MAT whose columns are the logs
2904 : * of generators of U_i(pr)/U_{i+1}(pr). */
2905 : INLINE GEN
2906 1260 : locs_get_famod(GEN locs) { return gmael(locs,4,1); }
2907 : INLINE GEN
2908 207235 : locs_get_m_infty(GEN locs) { return gmael(locs,4,2); }
2909 :
2910 : /* G a grossenchar group */
2911 : INLINE GEN
2912 32639 : gchar_get_basis(GEN gc) { return gel(gc, 1); }
2913 : INLINE GEN
2914 203105 : gchar_get_bnf(GEN gc) { return gel(gc, 2); }
2915 : INLINE GEN
2916 241501 : gchar_get_nf(GEN gc) { return gel(gc, 3); }
2917 : INLINE GEN
2918 279434 : gchar_get_zm(GEN gc) { return gel(gc, 4); }
2919 : INLINE GEN
2920 8225 : gchar_get_mod(GEN gc) { return locs_get_mod(gchar_get_zm(gc)); }
2921 : INLINE GEN
2922 4095 : gchar_get_modP(GEN gc) { return gmael(gchar_get_mod(gc),1,1); }
2923 : INLINE GEN
2924 36865 : gchar_get_S(GEN gc) { return gel(gc, 5); }
2925 : INLINE GEN
2926 188468 : gchar_get_DLdata(GEN gc) { return gel(gc, 6); }
2927 : INLINE GEN
2928 1748 : gchar_get_sfu(GEN gc) { return gel(gc, 7); }
2929 : INLINE GEN
2930 16168 : gchar_get_cyc(GEN gc) { return gel(gc, 9); }
2931 : INLINE GEN
2932 0 : gchar_get_hnf(GEN gc) { return gmael(gc, 10, 1); }
2933 : INLINE GEN
2934 0 : gchar_get_U(GEN gc) { return gmael(gc, 10, 2); }
2935 : INLINE GEN
2936 10969 : gchar_get_Ui(GEN gc) { return gmael(gc, 10, 3); }
2937 : INLINE GEN
2938 3321 : gchar_get_m0(GEN gc) { return gel(gc, 11); }
2939 : INLINE GEN
2940 4267 : gchar_get_u0(GEN gc) { return gel(gc, 12); }
2941 : INLINE long
2942 17416 : gchar_get_r1(GEN gc) { return nf_get_r1(gchar_get_nf(gc)); }
2943 : INLINE long
2944 623 : gchar_get_r2(GEN gc) { return nf_get_r2(gchar_get_nf(gc)); }
2945 : INLINE GEN
2946 50347 : gchar_get_loccyc(GEN gc) { return locs_get_cyc(gchar_get_zm(gc)); }
2947 : INLINE long
2948 37131 : gchar_get_nc(GEN gc) { return lg(gchar_get_loccyc(gc))-1; }
2949 : INLINE long
2950 36865 : gchar_get_ns(GEN gc) { return lg(gchar_get_S(gc))-1; }
2951 : INLINE long
2952 1841 : gchar_get_nm(GEN gc) { return lg(gchar_get_basis(gc))-1; }
2953 : INLINE long
2954 4011 : gchar_get_evalprec(GEN gc) { return gmael(gc, 8, 1)[1]; }
2955 : INLINE long
2956 18732 : gchar_get_prec(GEN gc) { return gmael(gc, 8, 1)[2]; }
2957 : INLINE long
2958 13419 : gchar_get_nfprec(GEN gc) { return gmael(gc, 8, 1)[3]; }
2959 : INLINE void
2960 1785 : gchar_set_evalprec(GEN gc, long prec) { gmael(gc, 8, 1)[1] = prec; }
2961 : INLINE void
2962 1202 : gchar_set_prec(GEN gc, long prec) { gmael(gc, 8, 1)[2] = prec; }
2963 : INLINE void
2964 1202 : gchar_copy_precs(GEN gc, GEN gc2)
2965 : {
2966 1202 : gel(gc2, 8) = shallowcopy(gel(gc,8));
2967 1202 : gmael(gc2, 8, 1) = shallowcopy(gmael(gc, 8, 1));
2968 1202 : }
2969 : INLINE void
2970 2950 : gchar_set_nfprec(GEN gc, long prec) { gmael(gc, 8, 1)[3] = prec; }
2971 : INLINE long
2972 322 : gchar_get_ntors(GEN gc) { return gmael(gc, 8, 2)[1]; }
2973 : INLINE long
2974 322 : gchar_get_nfree(GEN gc) { return gmael(gc, 8, 2)[2]; }
2975 : INLINE long
2976 2042 : gchar_get_nalg(GEN gc) { return gmael(gc, 8, 2)[3]; }
2977 : INLINE void
2978 1573 : gchar_set_basis(GEN gc, GEN m_inv) { gel(gc, 1) = m_inv; }
2979 : INLINE void
2980 2077 : gchar_set_nf(GEN gc, GEN nf) { gel(gc, 3) = nf; }
2981 : INLINE void
2982 623 : gchar_set_ntors(GEN gc, long n) { gmael(gc, 8, 2)[1] = n; }
2983 : INLINE void
2984 623 : gchar_set_nfree(GEN gc, long n) { gmael(gc, 8, 2)[2] = n; }
2985 : INLINE void
2986 623 : gchar_set_nalg(GEN gc, long n) { gmael(gc, 8, 2)[3] = n; }
2987 : INLINE void
2988 1573 : gchar_set_cyc(GEN gc, GEN cyc) { gel(gc, 9) = cyc; }
2989 : INLINE void
2990 623 : gchar_set_HUUi(GEN gc, GEN hnf, GEN U, GEN Ui) { gel(gc, 10) = mkvec3(hnf, U, Ui); }
2991 : INLINE void
2992 950 : gchar_set_m0(GEN gc, GEN m0) { gel(gc, 11) = m0; }
2993 : INLINE void
2994 2093 : gchar_set_u0(GEN gc, GEN u0) { gel(gc, 12) = u0; }
2995 :
2996 : INLINE GEN
2997 1386555 : bid_get_mod(GEN bid) { return gel(bid,1); }
2998 : INLINE GEN
2999 78295 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
3000 : INLINE GEN
3001 35098 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
3002 : INLINE GEN
3003 5415532 : bid_get_grp(GEN bid) { return gel(bid,2); }
3004 : INLINE GEN
3005 2313930 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
3006 : INLINE GEN
3007 2025286 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
3008 : INLINE GEN
3009 1990752 : bid_get_MOD(GEN bid) { GEN y = gel(bid,4); return lg(y) == 4? gel(y,3): NULL; }
3010 : INLINE GEN
3011 1991482 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
3012 : INLINE GEN
3013 51226 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
3014 : INLINE GEN
3015 2217818 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
3016 : INLINE GEN
3017 3378363 : bid_get_U(GEN bid) { return gel(bid,5); }
3018 : INLINE GEN
3019 0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
3020 : INLINE GEN
3021 5361616 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
3022 : INLINE GEN
3023 0 : bid_get_gen_nocheck(GEN bid) { return abgrp_get_gen(bid_get_grp(bid)); }
3024 : INLINE GEN
3025 53333 : bid_get_gen(GEN bid) {
3026 53333 : GEN G = bid_get_grp(bid);
3027 53333 : if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
3028 53333 : return abgrp_get_gen(G);
3029 : }
3030 :
3031 : INLINE GEN
3032 34545576 : znstar_get_N(GEN G) { return gmael(G,1,1); }
3033 : INLINE GEN
3034 7211773 : znstar_get_faN(GEN G) { return gel(G,3); }
3035 : INLINE GEN
3036 14 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
3037 : INLINE GEN
3038 261976 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
3039 : INLINE GEN
3040 16150 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
3041 : INLINE GEN
3042 7824692 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
3043 : INLINE GEN
3044 3221961 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
3045 : INLINE GEN
3046 46788 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
3047 : INLINE GEN
3048 176051 : znstar_get_U(GEN G) { return gel(G,5); }
3049 : INLINE GEN
3050 2820791 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
3051 : INLINE GEN
3052 36112 : gal_get_pol(GEN gal) { return gel(gal,1); }
3053 : INLINE GEN
3054 6020 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
3055 : INLINE GEN
3056 91 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
3057 : INLINE GEN
3058 23772 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
3059 : INLINE GEN
3060 32528 : gal_get_roots(GEN gal) { return gel(gal,3); }
3061 : INLINE GEN
3062 27656 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
3063 : INLINE GEN
3064 27650 : gal_get_den(GEN gal) { return gel(gal,5); }
3065 : INLINE GEN
3066 80169 : gal_get_group(GEN gal) { return gel(gal,6); }
3067 : INLINE GEN
3068 15435 : gal_get_gen(GEN gal) { return gel(gal,7); }
3069 : INLINE GEN
3070 6664 : gal_get_orders(GEN gal) { return gel(gal,8); }
3071 :
3072 : /* assume rnf a genuine rnf */
3073 : INLINE long
3074 2506083 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
3075 : INLINE long
3076 21231 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
3077 : INLINE long
3078 1392746 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
3079 : INLINE GEN
3080 1386 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
3081 : INLINE GEN
3082 1225 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
3083 : INLINE GEN
3084 1204 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
3085 : INLINE GEN
3086 733726 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
3087 : INLINE GEN
3088 6573 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
3089 : INLINE GEN
3090 314496 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
3091 : INLINE GEN
3092 2844933 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
3093 : INLINE GEN
3094 350 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
3095 : INLINE GEN
3096 105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
3097 : INLINE GEN
3098 2625 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
3099 : INLINE long
3100 574 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
3101 : INLINE GEN
3102 255354 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
3103 : INLINE long
3104 4886 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
3105 : INLINE GEN
3106 4606 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
3107 : INLINE GEN
3108 135211 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
3109 : INLINE GEN
3110 1225 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
3111 :
3112 : INLINE GEN
3113 138512 : idealred(GEN nf, GEN id) { return idealred0(nf, id, NULL); }
3114 :
3115 : INLINE GEN
3116 5427 : idealchineseinit(GEN nf, GEN x)
3117 5427 : { return idealchinese(nf,x,NULL); }
3118 :
3119 : /*******************************************************************/
3120 : /* */
3121 : /* CLOSURES */
3122 : /* */
3123 : /*******************************************************************/
3124 367953170 : INLINE long closure_arity(GEN C) { return ((ulong)C[1])&ARITYBITS; }
3125 40098381 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
3126 319227886 : INLINE const char *closure_codestr(GEN C) { return GSTR(gel(C,2))-1; }
3127 0 : INLINE GEN closure_get_code(GEN C) { return gel(C,2); }
3128 319203469 : INLINE GEN closure_get_oper(GEN C) { return gel(C,3); }
3129 319174101 : INLINE GEN closure_get_data(GEN C) { return gel(C,4); }
3130 13523 : INLINE GEN closure_get_dbg(GEN C) { return gel(C,5); }
3131 36855 : INLINE GEN closure_get_text(GEN C) { return gel(C,6); }
3132 13061564 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
3133 :
3134 : /*******************************************************************/
3135 : /* */
3136 : /* ERRORS */
3137 : /* */
3138 : /*******************************************************************/
3139 : INLINE long
3140 62333 : err_get_num(GEN e) { return e[1]; }
3141 : INLINE GEN
3142 294 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
3143 :
3144 : INLINE void
3145 14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
3146 : INLINE void
3147 21 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
3148 : INLINE void
3149 84 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
3150 : INLINE void
3151 718 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
3152 : INLINE void
3153 0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
3154 : INLINE void
3155 36 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
3156 : INLINE void
3157 98 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
3158 : INLINE void
3159 539 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
3160 : INLINE void
3161 20018 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
3162 : INLINE void
3163 63 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
3164 : INLINE void
3165 2771 : 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); }
3166 : INLINE void
3167 214 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
3168 : INLINE void
3169 0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
3170 : INLINE void
3171 406 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
3172 : INLINE void
3173 156 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
3174 : INLINE void
3175 238 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
3176 : INLINE void
3177 0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
3178 : INLINE void
3179 98 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
3180 : INLINE void
3181 1316 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
3182 : INLINE void
3183 63 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
3184 : INLINE void
3185 7084 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
3186 : INLINE void
3187 15384 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
3188 : INLINE void
3189 3493 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
3190 : INLINE void
3191 371 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
3192 : INLINE void
3193 238 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
3194 238 : { pari_err(e_PRIORITY, f,x,op,v); }
3195 :
|