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 retmkvec6(x,y,z,t,u,v)\
117 : do { GEN _v = cgetg(7, t_VEC);\
118 : gel(_v,1) = (x);\
119 : gel(_v,2) = (y);\
120 : gel(_v,3) = (z);\
121 : gel(_v,4) = (t);\
122 : gel(_v,5) = (u);\
123 : gel(_v,6) = (v); return _v; } while(0)
124 : #define retmkcol(x)\
125 : do { GEN _v = cgetg(2, t_COL);\
126 : gel(_v,1) = (x); return _v; } while(0)
127 : #define retmkcol2(x,y)\
128 : do { GEN _v = cgetg(3, t_COL);\
129 : gel(_v,1) = (x);\
130 : gel(_v,2) = (y); return _v; } while(0)
131 : #define retmkcol3(x,y,z)\
132 : do { GEN _v = cgetg(4, t_COL);\
133 : gel(_v,1) = (x);\
134 : gel(_v,2) = (y);\
135 : gel(_v,3) = (z); return _v; } while(0)
136 : #define retmkcol4(x,y,z,t)\
137 : do { GEN _v = cgetg(5, t_COL);\
138 : gel(_v,1) = (x);\
139 : gel(_v,2) = (y);\
140 : gel(_v,3) = (z);\
141 : gel(_v,4) = (t); return _v; } while(0)
142 : #define retmkcol5(x,y,z,t,u)\
143 : do { GEN _v = cgetg(6, t_COL);\
144 : gel(_v,1) = (x);\
145 : gel(_v,2) = (y);\
146 : gel(_v,3) = (z);\
147 : gel(_v,4) = (t);\
148 : gel(_v,5) = (u); return _v; } while(0)
149 : #define retmkcol6(x,y,z,t,u,v)\
150 : do { GEN _v = cgetg(7, t_COL);\
151 : gel(_v,1) = (x);\
152 : gel(_v,2) = (y);\
153 : gel(_v,3) = (z);\
154 : gel(_v,4) = (t);\
155 : gel(_v,5) = (u);\
156 : gel(_v,6) = (v); return _v; } while(0)
157 : #define retmkmat(x)\
158 : do { GEN _v = cgetg(2, t_MAT);\
159 : gel(_v,1) = (x); return _v; } while(0)
160 : #define retmkmat2(x,y)\
161 : do { GEN _v = cgetg(3, t_MAT);\
162 : gel(_v,1) = (x);\
163 : gel(_v,2) = (y); return _v; } while(0)
164 : #define retmkmat3(x,y,z)\
165 : do { GEN _v = cgetg(4, t_MAT);\
166 : gel(_v,1) = (x);\
167 : gel(_v,2) = (y);\
168 : gel(_v,3) = (z); return _v; } while(0)
169 : #define retmkmat4(x,y,z,t)\
170 : do { GEN _v = cgetg(5, t_MAT);\
171 : gel(_v,1) = (x);\
172 : gel(_v,2) = (y);\
173 : gel(_v,3) = (z);\
174 : gel(_v,4) = (t); return _v; } while(0)
175 : #define retmkmat5(x,y,z,t,u)\
176 : do { GEN _v = cgetg(6, t_MAT);\
177 : gel(_v,1) = (x);\
178 : gel(_v,2) = (y);\
179 : gel(_v,3) = (z);\
180 : gel(_v,4) = (t);\
181 : gel(_v,5) = (u); return _v; } while(0)
182 : #define retmkmat22(a,b,c,d)\
183 : do { GEN _v = cgetg(3, t_MAT);\
184 : gel(_v,1) = mkcol2(a,c);\
185 : gel(_v,2) = mkcol2(b,d); return _v; } while(0)
186 : INLINE GEN
187 8092624 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
188 : INLINE GEN
189 1813 : mkintmodu(ulong x, ulong y) {
190 1813 : GEN v = cgetg(3,t_INTMOD);
191 1813 : gel(v,1) = utoipos(y);
192 1813 : gel(v,2) = utoi(x); return v;
193 : }
194 : INLINE GEN
195 2009 : mkpadic(GEN x, GEN p, GEN pd, long e, long d) { retmkpadic(x,p,pd,e,d); }
196 : INLINE GEN
197 3104033 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
198 : INLINE GEN
199 41640860 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
200 : INLINE GEN
201 1209563 : mkfracss(long x, long y) { retmkfrac(stoi(x),utoipos(y)); }
202 : /* q = n/d a t_FRAC or t_INT; recover (n,d) */
203 : INLINE void
204 35322 : Qtoss(GEN q, long *n, long *d)
205 : {
206 35322 : if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
207 5796 : else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
208 35322 : }
209 : INLINE GEN
210 1097215 : sstoQ(long n, long d)
211 : {
212 : ulong r;
213 : long g, q;
214 1097215 : if (!n)
215 : {
216 117936 : if (!d) pari_err_INV("sstoQ",gen_0);
217 117936 : return gen_0;
218 : }
219 979279 : if (d < 0) { d = -d; n = -n; }
220 979279 : if (d == 1) return stoi(n);
221 959945 : r = labs(n);
222 959945 : if (r == 1) retmkfrac(n > 0? gen_1: gen_m1, utoipos(d));
223 909825 : q = udivuu_rem(r, d, &r);
224 909825 : if (!r) return n > 0? utoipos(q): utoineg(q);
225 854952 : g = ugcd(d,r); /* gcd(n,d) */
226 854952 : if (g != 1) { n /= g; d /= g; }
227 854952 : retmkfrac(stoi(n), utoipos(d));
228 : }
229 :
230 : INLINE GEN
231 1600198 : uutoQ(ulong n, ulong d)
232 : {
233 : ulong r;
234 : long g, q;
235 1600198 : if (!n)
236 : {
237 595 : if (!d) pari_err_INV("uutoQ",gen_0);
238 595 : return gen_0;
239 : }
240 1599603 : if (d == 1) return utoipos(n);
241 1588501 : if (n == 1) retmkfrac(gen_1, utoipos(d));
242 1499461 : q = udivuu_rem(n,d,&r);
243 1499461 : if (!r) return utoipos(q);
244 1259375 : g = ugcd(d,r); /* gcd(n,d) */
245 1259375 : if (g != 1) { n /= g; d /= g; }
246 1259375 : retmkfrac(utoipos(n), utoipos(d));
247 : }
248 :
249 : INLINE GEN
250 4999377 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
251 : INLINE GEN
252 698439 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
253 698439 : gel(v,1) = x; gel(v,2) = y; return v; }
254 : INLINE GEN
255 14 : mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
256 14 : gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
257 : INLINE GEN
258 91606415 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
259 : INLINE GEN
260 1269717 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
261 : INLINE GEN
262 975455 : cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
263 : INLINE GEN
264 69118 : mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
265 69118 : gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
266 : /* vecsmall */
267 : INLINE GEN
268 131614600 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
269 : INLINE GEN
270 234809731 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
271 234833525 : v[1]=x; v[2]=y; return v; }
272 : INLINE GEN
273 104176411 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
274 104169406 : v[1]=x; v[2]=y; v[3]=z; return v; }
275 : INLINE GEN
276 18663456 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
277 18664253 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
278 : INLINE GEN
279 1717746 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
280 1717741 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
281 :
282 : INLINE GEN
283 27927734 : mkqfb(GEN x, GEN y, GEN z, GEN d) { retmkqfb(x,y,z,d); }
284 : /* vec */
285 : INLINE GEN
286 71104424 : mkvec(GEN x) { retmkvec(x); }
287 : INLINE GEN
288 186084420 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
289 : INLINE GEN
290 117703219 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
291 : INLINE GEN
292 10092899 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
293 : INLINE GEN
294 15185875 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
295 : INLINE GEN
296 0 : mkvec6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkvec6(x,y,z,t,u,v); }
297 : INLINE GEN
298 168 : mkvecs(long x) { retmkvec(stoi(x)); }
299 : INLINE GEN
300 220243 : mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
301 : INLINE GEN
302 524441 : mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
303 : INLINE GEN
304 21 : mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
305 : INLINE GEN
306 0 : mkvec5s(long x, long y, long z, long t, long u) { retmkvec5(stoi(x),stoi(y),stoi(z),stoi(t),stoi(u)); }
307 : INLINE GEN
308 301 : mkvec6s(long x, long y, long z, long t, long u, long v) { retmkvec6(stoi(x),stoi(y),stoi(z),stoi(t),stoi(u),stoi(v)); }
309 : INLINE GEN
310 122521 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
311 : INLINE GEN
312 114506 : mkvec2copy(GEN x, GEN y) {
313 114506 : GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
314 : /* col */
315 : INLINE GEN
316 10639973 : mkcol(GEN x) { retmkcol(x); }
317 : INLINE GEN
318 156799343 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
319 : INLINE GEN
320 88314 : mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
321 : INLINE GEN
322 103467 : mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
323 : INLINE GEN
324 94 : mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
325 : INLINE GEN
326 232470 : mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
327 : INLINE GEN
328 40411 : mkcols(long x) { retmkcol(stoi(x)); }
329 : INLINE GEN
330 4416244 : mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
331 : INLINE GEN
332 315 : mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
333 : INLINE GEN
334 0 : mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
335 : INLINE GEN
336 33532 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
337 : /* mat */
338 : INLINE GEN
339 4751097 : mkmat(GEN x) { retmkmat(x); }
340 : INLINE GEN
341 33651746 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
342 : INLINE GEN
343 17001 : mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
344 : INLINE GEN
345 84 : mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
346 : INLINE GEN
347 0 : mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
348 : INLINE GEN
349 122256 : mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
350 : INLINE GEN
351 0 : mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
352 : INLINE GEN
353 1331863 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
354 : INLINE GEN
355 108788 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
356 : INLINE long
357 1452911 : inf_get_sign(GEN x) { return signe(gel(x,1)); }
358 : INLINE GEN
359 207578 : mkmat22s(long a, long b, long c, long d) {retmkmat2(mkcol2s(a,c),mkcol2s(b,d));}
360 : INLINE GEN
361 50252636 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
362 :
363 : /* pol */
364 : INLINE GEN
365 3857518 : pol_x(long v) {
366 3857518 : GEN p = cgetg(4, t_POL);
367 3857512 : p[1] = evalsigne(1)|evalvarn(v);
368 3857512 : gel(p,2) = gen_0;
369 3857512 : gel(p,3) = gen_1; return p;
370 : }
371 : /* x^n, assume n >= 0 */
372 : INLINE GEN
373 2249696 : pol_xn(long n, long v) {
374 2249696 : long i, a = n+2;
375 2249696 : GEN p = cgetg(a+1, t_POL);
376 2249699 : p[1] = evalsigne(1)|evalvarn(v);
377 5545411 : for (i = 2; i < a; i++) gel(p,i) = gen_0;
378 2249699 : gel(p,a) = gen_1; return p;
379 : }
380 : /* x^n, no assumption on n */
381 : INLINE GEN
382 294 : pol_xnall(long n, long v)
383 : {
384 294 : if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
385 287 : return pol_xn(n, v);
386 : }
387 : /* x^n, assume n >= 0 */
388 : INLINE GEN
389 371 : polxn_Flx(long n, long sv) {
390 371 : long i, a = n+2;
391 371 : GEN p = cgetg(a+1, t_VECSMALL);
392 371 : p[1] = sv;
393 1806 : for (i = 2; i < a; i++) p[i] = 0;
394 371 : p[a] = 1; return p;
395 : }
396 : INLINE GEN
397 3755743 : pol_1(long v) {
398 3755743 : GEN p = cgetg(3, t_POL);
399 3755734 : p[1] = evalsigne(1)|evalvarn(v);
400 3755734 : gel(p,2) = gen_1; return p;
401 : }
402 : INLINE GEN
403 83000968 : pol_0(long v)
404 : {
405 83000968 : GEN x = cgetg(2,t_POL);
406 83000966 : x[1] = evalvarn(v); return x;
407 : }
408 : #define retconst_vec(n,x)\
409 : do { long _i, _n = (n);\
410 : GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
411 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
412 : return _v; } while(0)
413 : INLINE GEN
414 241081680 : const_vec(long n, GEN x) { retconst_vec(n, x); }
415 : #define retconst_col(n,x)\
416 : do { long _i, _n = (n);\
417 : GEN _v = cgetg(_n+1, t_COL), _x = (x);\
418 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
419 : return _v; } while(0)
420 : INLINE GEN
421 39630581 : const_col(long n, GEN x) { retconst_col(n, x); }
422 : INLINE GEN
423 19700839 : const_vecsmall(long n, long c)
424 : {
425 : long i;
426 19700839 : GEN V = cgetg(n+1,t_VECSMALL);
427 772508766 : for(i=1;i<=n;i++) V[i] = c;
428 19701304 : return V;
429 : }
430 :
431 : /*** ZERO ***/
432 : /* O(p^e) */
433 : INLINE GEN
434 855447 : zeropadic(GEN p, long e) { retmkpadic(gen_0, icopy(p), gen_1, e, 0); }
435 : INLINE GEN
436 17023 : zeropadic_shallow(GEN p, long e) { retmkpadic(gen_0, icopy(p), gen_1, e, 0); }
437 : /* O(pol_x(v)^e) */
438 : INLINE GEN
439 138908 : zeroser(long v, long e)
440 : {
441 138908 : GEN x = cgetg(2, t_SER);
442 138908 : x[1] = evalvalser(e) | evalvarn(v); return x;
443 : }
444 : INLINE int
445 10120284 : ser_isexactzero(GEN x)
446 : {
447 10120284 : if (!signe(x)) switch(lg(x))
448 : {
449 132860 : case 2: return 1;
450 4746 : case 3: return isexactzero(gel(x,2));
451 : }
452 9982678 : return 0;
453 : }
454 : /* 0 * pol_x(v) */
455 : INLINE GEN
456 42358336 : zeropol(long v) { return pol_0(v); }
457 : /* vector(n) */
458 : INLINE GEN
459 122323456 : zerocol(long n)
460 : {
461 122323456 : GEN y = cgetg(n+1,t_COL);
462 821076923 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
463 122324014 : return y;
464 : }
465 : /* vectorv(n) */
466 : INLINE GEN
467 28453418 : zerovec(long n)
468 : {
469 28453418 : GEN y = cgetg(n+1,t_VEC);
470 418503685 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
471 28453414 : return y;
472 : }
473 : /* matrix(m, n) */
474 : INLINE GEN
475 109080 : zeromat(long m, long n)
476 : {
477 109080 : GEN y = cgetg(n+1,t_MAT);
478 109081 : GEN v = zerocol(m);
479 403169 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
480 109083 : return y;
481 : }
482 : /* = zero_zx, sv is a evalvarn()*/
483 : INLINE GEN
484 1426678 : zero_Flx(long sv) { return pol0_Flx(sv); }
485 : INLINE GEN
486 59651551 : zero_Flv(long n)
487 : {
488 59651551 : GEN y = cgetg(n+1,t_VECSMALL);
489 966528001 : long i; for (i=1; i<=n; i++) y[i] = 0;
490 59651585 : return y;
491 : }
492 : /* matrix(m, n) */
493 : INLINE GEN
494 2829341 : zero_Flm(long m, long n)
495 : {
496 2829341 : GEN y = cgetg(n+1,t_MAT);
497 2829322 : GEN v = zero_Flv(m);
498 18027639 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
499 2829315 : return y;
500 : }
501 : /* matrix(m, n) */
502 : INLINE GEN
503 238024 : zero_Flm_copy(long m, long n)
504 : {
505 238024 : GEN y = cgetg(n+1,t_MAT);
506 2583246 : long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
507 238019 : return y;
508 : }
509 :
510 : INLINE GEN
511 3647670 : zero_F2v(long m)
512 : {
513 3647670 : long l = nbits2nlong(m);
514 3647666 : GEN v = zero_Flv(l+1);
515 3647651 : v[1] = m;
516 3647651 : return v;
517 : }
518 :
519 : INLINE GEN
520 0 : zero_F2m(long m, long n)
521 : {
522 : long i;
523 0 : GEN M = cgetg(n+1, t_MAT);
524 0 : GEN v = zero_F2v(m);
525 0 : for (i = 1; i <= n; i++)
526 0 : gel(M,i) = v;
527 0 : return M;
528 : }
529 :
530 :
531 : INLINE GEN
532 845040 : zero_F2m_copy(long m, long n)
533 : {
534 : long i;
535 845040 : GEN M = cgetg(n+1, t_MAT);
536 2016917 : for (i = 1; i <= n; i++)
537 1171881 : gel(M,i)= zero_F2v(m);
538 845036 : return M;
539 : }
540 :
541 : /* matrix(m, n) */
542 : INLINE GEN
543 14248309 : zeromatcopy(long m, long n)
544 : {
545 14248309 : GEN y = cgetg(n+1,t_MAT);
546 72047911 : long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
547 14248321 : return y;
548 : }
549 :
550 : INLINE GEN
551 29619 : zerovec_block(long len)
552 : {
553 : long i;
554 29619 : GEN blk = cgetg_block(len + 1, t_VEC);
555 977427 : for (i = 1; i <= len; ++i)
556 947808 : gel(blk, i) = gen_0;
557 29619 : return blk;
558 : }
559 :
560 : /* i-th vector in the standard basis */
561 : INLINE GEN
562 5925183 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
563 : INLINE GEN
564 1713856 : vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
565 : INLINE GEN
566 42 : F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
567 : INLINE GEN
568 584699 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
569 : INLINE GEN
570 1451067 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
571 :
572 : INLINE GEN
573 27625258 : shallowcopy(GEN x)
574 27625258 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
575 :
576 : /* routines for naive growarrays */
577 : INLINE GEN
578 11767381 : vectrunc_init(long l)
579 : {
580 11767381 : GEN z = new_chunk(l);
581 11767532 : z[0] = evaltyp(t_VEC) | _evallg(1); return z;
582 : }
583 : INLINE GEN
584 740250 : coltrunc_init(long l)
585 : {
586 740250 : GEN z = new_chunk(l);
587 740250 : z[0] = evaltyp(t_COL) | _evallg(1); return z;
588 : }
589 : INLINE void
590 320636824 : lg_increase(GEN x) { x[0]++; }
591 : INLINE void
592 15849382 : vectrunc_append(GEN x, GEN t) { gel(x, lg(x)) = t; lg_increase(x); }
593 : INLINE void
594 9667 : vectrunc_append_batch(GEN x, GEN y)
595 : {
596 9667 : long i, l = lg(x), ly = lg(y);
597 9667 : GEN z = x + l-1;
598 32753 : for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
599 9667 : setlg(x, l+ly-1);
600 9667 : }
601 : INLINE GEN
602 191435220 : vecsmalltrunc_init(long l)
603 : {
604 191435220 : GEN z = new_chunk(l);
605 191427085 : z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
606 : }
607 : INLINE void
608 100788386 : vecsmalltrunc_append(GEN x, long t) { x[ lg(x) ] = t; lg_increase(x); }
609 :
610 : /*******************************************************************/
611 : /* */
612 : /* STRING HASH FUNCTIONS */
613 : /* */
614 : /*******************************************************************/
615 : INLINE ulong
616 2756815 : hash_str(const char *str)
617 : {
618 2756815 : ulong hash = 5381UL, c;
619 28682857 : while ( (c = (ulong)*str++) )
620 25926042 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
621 2756815 : return hash;
622 : }
623 : INLINE ulong
624 31545554 : hash_str_len(const char *str, long len)
625 : {
626 31545554 : ulong hash = 5381UL;
627 : long i;
628 228045468 : for (i = 0; i < len; i++)
629 : {
630 196499914 : ulong c = (ulong)*str++;
631 196499914 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
632 : }
633 31545554 : return hash;
634 : }
635 :
636 : /*******************************************************************/
637 : /* */
638 : /* VEC / COL / VECSMALL */
639 : /* */
640 : /*******************************************************************/
641 : /* shallow*/
642 : INLINE GEN
643 11557 : vec_shorten(GEN v, long n)
644 : {
645 11557 : GEN V = cgetg(n+1, t_VEC);
646 : long i;
647 13013 : for(i = 1; i <= n; i++) gel(V,i) = gel(v,i);
648 11557 : return V;
649 : }
650 : /* shallow*/
651 : INLINE GEN
652 18960 : vec_lengthen(GEN v, long n)
653 : {
654 18960 : GEN V = cgetg(n+1, t_VEC);
655 18960 : long i, l = lg(v);
656 1938870 : for(i = 1; i < l; i++) gel(V,i) = gel(v,i);
657 18960 : return V;
658 : }
659 : /* shallow*/
660 : INLINE GEN
661 3465010 : vec_append(GEN V, GEN s)
662 : {
663 3465010 : long i, l2 = lg(V);
664 3465010 : GEN res = cgetg(l2+1, typ(V));
665 10817184 : for (i = 1; i < l2; i++) gel(res, i) = gel(V,i);
666 3464998 : gel(res,l2) = s; return res;
667 : }
668 : /* shallow*/
669 : INLINE GEN
670 313988 : vec_prepend(GEN v, GEN s)
671 : {
672 313988 : long i, l = lg(v);
673 313988 : GEN w = cgetg(l+1, typ(v));
674 313991 : gel(w,1) = s;
675 796218 : for (i = 2; i <= l; i++) gel(w,i) = gel(v,i-1);
676 313991 : return w;
677 : }
678 : /* shallow*/
679 : INLINE GEN
680 0 : vec_setconst(GEN v, GEN x)
681 : {
682 0 : long i, l = lg(v);
683 0 : for (i = 1; i < l; i++) gel(v,i) = x;
684 0 : return v;
685 : }
686 : INLINE GEN
687 75544 : vecsmall_shorten(GEN v, long n)
688 : {
689 75544 : GEN V = cgetg(n+1,t_VECSMALL);
690 : long i;
691 988342 : for(i = 1; i <= n; i++) V[i] = v[i];
692 75544 : return V;
693 : }
694 : INLINE GEN
695 547 : vecsmall_lengthen(GEN v, long n)
696 : {
697 547 : long i, l = lg(v);
698 547 : GEN V = cgetg(n+1,t_VECSMALL);
699 53385 : for(i = 1; i < l; i++) V[i] = v[i];
700 547 : return V;
701 : }
702 :
703 : INLINE GEN
704 4036595 : vec_to_vecsmall(GEN x)
705 18928747 : { pari_APPLY_long(itos(gel(x,i))) }
706 : INLINE GEN
707 511781 : vecsmall_to_vec(GEN x)
708 7076007 : { pari_APPLY_type(t_VEC, stoi(x[i])) }
709 : INLINE GEN
710 3304 : vecsmall_to_vec_inplace(GEN z)
711 : {
712 3304 : long i, l = lg(z);
713 79604 : for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
714 3304 : settyp(z, t_VEC); return z;
715 : }
716 : INLINE GEN
717 7838299 : vecsmall_to_col(GEN x)
718 59639704 : { pari_APPLY_type(t_COL, stoi(x[i])) }
719 :
720 : INLINE int
721 7043251 : vecsmall_lexcmp(GEN x, GEN y)
722 : {
723 : long lx,ly,l,i;
724 7043251 : lx = lg(x);
725 7043251 : ly = lg(y); l = minss(lx,ly);
726 30264485 : for (i=1; i<l; i++)
727 28787551 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
728 1476934 : if (lx == ly) return 0;
729 4154 : return (lx < ly)? -1 : 1;
730 : }
731 :
732 : INLINE int
733 108088071 : vecsmall_prefixcmp(GEN x, GEN y)
734 : {
735 108088071 : long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
736 542691324 : for (i=1; i<l; i++)
737 519806035 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
738 22885289 : return 0;
739 : }
740 :
741 : /*Can be used on t_VEC, but coeffs not gcopy-ed*/
742 : INLINE GEN
743 186970 : vecsmall_prepend(GEN V, long s)
744 : {
745 186970 : long i, l2 = lg(V);
746 186970 : GEN res = cgetg(l2+1, typ(V));
747 186970 : res[1] = s;
748 625301 : for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
749 186970 : return res;
750 : }
751 :
752 : INLINE GEN
753 4098928 : vecsmall_append(GEN V, long s)
754 : {
755 4098928 : long i, l2 = lg(V);
756 4098928 : GEN res = cgetg(l2+1, t_VECSMALL);
757 6014073 : for (i = 1; i < l2; ++i) res[i] = V[i];
758 4098928 : res[l2] = s; return res;
759 : }
760 :
761 : INLINE GEN
762 1464237 : vecsmall_concat(GEN u, GEN v)
763 : {
764 1464237 : long i, l1 = lg(u)-1, l2 = lg(v)-1;
765 1464237 : GEN res = cgetg(l1+l2+1, t_VECSMALL);
766 11617376 : for (i = 1; i <= l1; ++i) res[i] = u[i];
767 15218473 : for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
768 1464236 : return res;
769 : }
770 :
771 : /* return the number of indices where u and v are equal */
772 : INLINE long
773 0 : vecsmall_coincidence(GEN u, GEN v)
774 : {
775 0 : long i, s = 0, l = minss(lg(u),lg(v));
776 0 : for(i=1; i<l; i++)
777 0 : if(u[i] == v[i]) s++;
778 0 : return s;
779 : }
780 :
781 : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
782 : INLINE long
783 84 : vecsmall_isin(GEN v, long x)
784 : {
785 84 : long i, l = lg(v);
786 124 : for (i = 1; i < l; i++)
787 80 : if (v[i] == x) return i;
788 44 : return 0;
789 : }
790 :
791 : INLINE long
792 84 : vecsmall_pack(GEN V, long base, long mod)
793 : {
794 84 : long i, s = 0;
795 273 : for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
796 84 : return s;
797 : }
798 :
799 : INLINE long
800 21 : vecsmall_indexmax(GEN x)
801 : {
802 21 : long i, i0 = 1, t = x[1], lx = lg(x);
803 84 : for (i=2; i<lx; i++)
804 63 : if (x[i] > t) t = x[i0=i];
805 21 : return i0;
806 : }
807 :
808 : INLINE long
809 1164424 : vecsmall_max(GEN x)
810 : {
811 1164424 : long i, t = x[1], lx = lg(x);
812 3383854 : for (i=2; i<lx; i++)
813 2219430 : if (x[i] > t) t = x[i];
814 1164424 : return t;
815 : }
816 :
817 : INLINE long
818 21 : vecsmall_indexmin(GEN x)
819 : {
820 21 : long i, i0 = 1, t = x[1], lx =lg(x);
821 84 : for (i=2; i<lx; i++)
822 63 : if (x[i] < t) t = x[i0=i];
823 21 : return i0;
824 : }
825 :
826 : INLINE long
827 2345 : vecsmall_min(GEN x)
828 : {
829 2345 : long i, t = x[1], lx =lg(x);
830 16415 : for (i=2; i<lx; i++)
831 14070 : if (x[i] < t) t = x[i];
832 2345 : return t;
833 : }
834 :
835 : INLINE int
836 23233640 : ZV_isscalar(GEN x)
837 : {
838 23233640 : long l = lg(x);
839 55101130 : while (--l > 1)
840 54667246 : if (signe(gel(x, l))) return 0;
841 433884 : return 1;
842 : }
843 : INLINE int
844 54415362 : QV_isscalar(GEN x)
845 : {
846 54415362 : long lx = lg(x),i;
847 70421341 : for (i=2; i<lx; i++)
848 66460665 : if (!isintzero(gel(x, i))) return 0;
849 3960676 : return 1;
850 : }
851 : INLINE int
852 1210277 : RgV_isscalar(GEN x)
853 : {
854 1210277 : long lx = lg(x),i;
855 1236546 : for (i=2; i<lx; i++)
856 1232840 : if (!gequal0(gel(x, i))) return 0;
857 3706 : return 1;
858 : }
859 : INLINE int
860 0 : RgX_isscalar(GEN x)
861 : {
862 : long i;
863 0 : for (i=lg(x)-1; i>2; i--)
864 0 : if (!gequal0(gel(x, i))) return 0;
865 0 : return 1;
866 : }
867 : INLINE long
868 26883176 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
869 : INLINE GEN
870 140 : RgX_to_RgV(GEN x, long N) { x = RgX_to_RgC(x, N); settyp(x, t_VEC); return x; }
871 :
872 : #define RgX_type_code(t1,t2) ((t1 << 6) | t2)
873 :
874 : INLINE int
875 68165 : RgX_is_rational(GEN x)
876 : {
877 : long i;
878 310101 : for (i = lg(x)-1; i > 1; i--)
879 294096 : if (!is_rational_t(typ(gel(x,i)))) return 0;
880 16005 : return 1;
881 : }
882 : INLINE int
883 22423576 : RgX_is_ZX(GEN x)
884 : {
885 : long i;
886 82876793 : for (i = lg(x)-1; i > 1; i--)
887 62235180 : if (typ(gel(x,i)) != t_INT) return 0;
888 20641613 : return 1;
889 : }
890 : INLINE int
891 399934 : RgX_is_QX(GEN x)
892 : {
893 399934 : long k = lg(x)-1;
894 1496610 : for ( ; k>1; k--)
895 1096970 : if (!is_rational_t(typ(gel(x,k)))) return 0;
896 399640 : return 1;
897 : }
898 : INLINE int
899 1331780 : RgX_is_monomial(GEN x)
900 : {
901 : long i;
902 1331780 : if (!signe(x)) return 0;
903 3341132 : for (i=lg(x)-2; i>1; i--)
904 2653625 : if (!isexactzero(gel(x,i))) return 0;
905 687507 : return 1;
906 : }
907 : INLINE int
908 32156428 : RgV_is_ZV(GEN x)
909 : {
910 : long i;
911 127581060 : for (i = lg(x)-1; i > 0; i--)
912 95457455 : if (typ(gel(x,i)) != t_INT) return 0;
913 32123605 : return 1;
914 : }
915 : INLINE int
916 140730 : RgV_is_QV(GEN x)
917 : {
918 : long i;
919 622923 : for (i = lg(x)-1; i > 0; i--)
920 485147 : if (!is_rational_t(typ(gel(x,i)))) return 0;
921 137776 : return 1;
922 : }
923 : INLINE long
924 110144 : RgV_isin_i(GEN v, GEN x, long n)
925 : {
926 : long i;
927 1033465 : for (i = 1; i <= n; i++)
928 1026717 : if (gequal(gel(v,i), x)) return i;
929 6748 : return 0;
930 : }
931 : INLINE long
932 110144 : RgV_isin(GEN v, GEN x) { return RgV_isin_i(v, x, lg(v)-1); }
933 :
934 : /********************************************************************/
935 : /** **/
936 : /** Dynamic arrays implementation **/
937 : /** **/
938 : /********************************************************************/
939 : INLINE void **
940 1025096071 : pari_stack_base(pari_stack *s) { return s->data; }
941 :
942 : INLINE void
943 6050919 : pari_stack_init(pari_stack *s, size_t size, void **data)
944 : {
945 6050919 : s->data = data;
946 6050919 : *data = NULL;
947 6050919 : s->n = 0;
948 6050919 : s->alloc = 0;
949 6050919 : s->size = size;
950 6050919 : }
951 :
952 : INLINE void
953 1019547819 : pari_stack_alloc(pari_stack *s, long nb)
954 : {
955 1019547819 : void **sdat = pari_stack_base(s);
956 1019436831 : long alloc = s->alloc;
957 1019436831 : if (s->n+nb <= alloc) return;
958 2249080 : if (!alloc)
959 2151078 : alloc = nb;
960 : else
961 : {
962 199078 : while (s->n+nb > alloc) alloc <<= 1;
963 : }
964 2249080 : pari_realloc_ip(sdat,alloc*s->size);
965 2249656 : s->alloc = alloc;
966 : }
967 :
968 : INLINE long
969 924861165 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
970 :
971 : INLINE void
972 5581494 : pari_stack_delete(pari_stack *s)
973 : {
974 5581494 : void **sdat = pari_stack_base(s);
975 5568428 : if (*sdat) pari_free(*sdat);
976 5598991 : }
977 :
978 : INLINE void
979 5851 : pari_stack_pushp(pari_stack *s, void *u)
980 : {
981 5851 : long n = pari_stack_new(s);
982 5851 : void **sdat =(void**) *pari_stack_base(s);
983 5851 : sdat[n] = u;
984 5851 : }
985 :
986 : /*******************************************************************/
987 : /* */
988 : /* EXTRACT */
989 : /* */
990 : /*******************************************************************/
991 : INLINE GEN
992 656171455 : vecslice(GEN A, long y1, long y2)
993 : {
994 656171455 : long i,lB = y2 - y1 + 2;
995 656171455 : GEN B = cgetg(lB, typ(A));
996 2919220969 : for (i=1; i<lB; i++) B[i] = A[y1-1+i];
997 656156407 : return B;
998 : }
999 : INLINE GEN
1000 2689539 : vecslicepermute(GEN A, GEN p, long y1, long y2)
1001 : {
1002 2689539 : long i,lB = y2 - y1 + 2;
1003 2689539 : GEN B = cgetg(lB, typ(A));
1004 30511535 : for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
1005 2689528 : return B;
1006 : }
1007 : /* rowslice(rowpermute(A,p), x1, x2) */
1008 : INLINE GEN
1009 184699 : rowslicepermute(GEN x, GEN p, long j1, long j2)
1010 781331 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
1011 :
1012 : INLINE GEN
1013 79319208 : rowslice(GEN x, long j1, long j2)
1014 672080930 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
1015 :
1016 : INLINE GEN
1017 16818127 : matslice(GEN A, long x1, long x2, long y1, long y2)
1018 16818127 : { return rowslice(vecslice(A, y1, y2), x1, x2); }
1019 :
1020 : /* shallow, remove coeff of index j */
1021 : INLINE GEN
1022 273 : rowsplice(GEN x, long j)
1023 1225 : { pari_APPLY_same(vecsplice(gel(x,i), j)) }
1024 :
1025 : /* shallow, remove coeff of index j */
1026 : INLINE GEN
1027 384921 : vecsplice(GEN a, long j)
1028 : {
1029 384921 : long i, k, l = lg(a);
1030 : GEN b;
1031 384921 : if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
1032 384921 : b = cgetg(l-1, typ(a));
1033 1593598 : for (i = k = 1; i < l; i++)
1034 1208679 : if (i != j) gel(b, k++) = gel(a,i);
1035 384919 : return b;
1036 : }
1037 : /* shallow */
1038 : INLINE GEN
1039 1036 : RgM_minor(GEN a, long i, long j)
1040 : {
1041 1036 : GEN b = vecsplice(a, j);
1042 1036 : long k, l = lg(b);
1043 4242 : for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
1044 1036 : return b;
1045 : }
1046 :
1047 : /* A[x0,] */
1048 : INLINE GEN
1049 763882 : row(GEN x, long j)
1050 6692631 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
1051 : INLINE GEN
1052 8741196 : Flm_row(GEN x, long j)
1053 231875963 : { pari_APPLY_ulong((ulong)coeff(x, j, i)) }
1054 : /* A[x0,] */
1055 : INLINE GEN
1056 204988 : rowcopy(GEN x, long j)
1057 2105509 : { pari_APPLY_type(t_VEC, gcopy(gcoeff(x, j, i))) }
1058 : /* A[x0, x1..x2] */
1059 : INLINE GEN
1060 987 : row_i(GEN A, long x0, long x1, long x2)
1061 : {
1062 987 : long i, lB = x2 - x1 + 2;
1063 987 : GEN B = cgetg(lB, t_VEC);
1064 2989 : for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
1065 987 : return B;
1066 : }
1067 :
1068 : INLINE GEN
1069 697235 : vecreverse(GEN A)
1070 : {
1071 : long i, l;
1072 697235 : GEN B = cgetg_copy(A, &l);
1073 2408385 : for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
1074 697235 : return B;
1075 : }
1076 :
1077 : INLINE GEN
1078 3052 : vecsmall_reverse(GEN A)
1079 : {
1080 : long i, l;
1081 3052 : GEN B = cgetg_copy(A, &l);
1082 12698 : for (i=1; i<l; i++) B[i] = A[l-i];
1083 3052 : return B;
1084 : }
1085 :
1086 : INLINE void
1087 2605 : vecreverse_inplace(GEN y)
1088 : {
1089 2605 : long l = lg(y), lim = l>>1, i;
1090 8312 : for (i = 1; i <= lim; i++)
1091 : {
1092 5707 : GEN z = gel(y,i);
1093 5707 : gel(y,i) = gel(y,l-i);
1094 5707 : gel(y,l-i) = z;
1095 : }
1096 2605 : }
1097 :
1098 : INLINE GEN
1099 78698845 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
1100 :
1101 : INLINE GEN
1102 20542511 : vecpermute(GEN A, GEN x)
1103 115178887 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
1104 :
1105 : INLINE GEN
1106 9372706 : veclast(GEN A) { return gel(A, lg(A)-1); }
1107 :
1108 : INLINE GEN
1109 14705446 : rowpermute(GEN x, GEN p)
1110 102227837 : { pari_APPLY_same(typ(gel(x,i)) == t_VECSMALL ? vecsmallpermute(gel(x, i), p)
1111 : : vecpermute(gel(x, i), p))
1112 : }
1113 : /*******************************************************************/
1114 : /* */
1115 : /* PERMUTATIONS */
1116 : /* */
1117 : /*******************************************************************/
1118 : INLINE GEN
1119 3265592 : identity_zv(long n)
1120 : {
1121 3265592 : GEN v = cgetg(n+1, t_VECSMALL);
1122 : long i;
1123 32945861 : for (i = 1; i <= n; i++) v[i] = i;
1124 3265576 : return v;
1125 : }
1126 : INLINE GEN
1127 8113 : identity_ZV(long n)
1128 : {
1129 8113 : GEN v = cgetg(n+1, t_VEC);
1130 : long i;
1131 169078 : for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
1132 8113 : return v;
1133 : }
1134 : /* identity permutation */
1135 : INLINE GEN
1136 3241243 : identity_perm(long n) { return identity_zv(n); }
1137 :
1138 : /* assume d <= n */
1139 : INLINE GEN
1140 99911 : cyclic_perm(long n, long d)
1141 : {
1142 99911 : GEN perm = cgetg(n+1, t_VECSMALL);
1143 : long i;
1144 520240 : for (i = 1; i <= n-d; i++) perm[i] = i+d;
1145 226702 : for ( ; i <= n; i++) perm[i] = i-n+d;
1146 99911 : return perm;
1147 : }
1148 :
1149 : /* Multiply (compose) two permutations */
1150 : INLINE GEN
1151 80894258 : perm_mul(GEN s, GEN x)
1152 1025312405 : { pari_APPLY_long(s[x[i]]) }
1153 :
1154 : INLINE GEN
1155 728 : perm_sqr(GEN x)
1156 18284 : { pari_APPLY_long(x[x[i]]) }
1157 :
1158 : /* Compute the inverse (reciprocal) of a permutation. */
1159 : INLINE GEN
1160 2781878 : perm_inv(GEN x)
1161 : {
1162 : long i, lx;
1163 2781878 : GEN y = cgetg_copy(x, &lx);
1164 39151554 : for (i=1; i<lx; i++) y[ x[i] ] = i;
1165 2781874 : return y;
1166 : }
1167 : /* Return s*t*s^-1 */
1168 : INLINE GEN
1169 418299 : perm_conj(GEN s, GEN t)
1170 : {
1171 : long i, l;
1172 418299 : GEN v = cgetg_copy(s, &l);
1173 6857893 : for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
1174 418299 : return v;
1175 : }
1176 :
1177 : INLINE void
1178 509193012 : pari_free(void *pointer)
1179 : {
1180 509193012 : BLOCK_SIGINT_START;
1181 509264495 : free(pointer);
1182 509264495 : BLOCK_SIGINT_END;
1183 509257341 : }
1184 : INLINE void*
1185 745082750 : pari_malloc(size_t size)
1186 : {
1187 745082750 : if (size)
1188 : {
1189 : char *tmp;
1190 745087628 : BLOCK_SIGINT_START;
1191 745161582 : tmp = (char*)malloc(size);
1192 745161582 : BLOCK_SIGINT_END;
1193 745153684 : if (!tmp) pari_err(e_MEM);
1194 745160878 : return tmp;
1195 : }
1196 0 : return NULL;
1197 : }
1198 : INLINE void*
1199 1890 : pari_realloc(void *pointer, size_t size)
1200 : {
1201 : char *tmp;
1202 :
1203 1890 : BLOCK_SIGINT_START;
1204 1890 : if (!pointer) tmp = (char *) malloc(size);
1205 1890 : else tmp = (char *) realloc(pointer,size);
1206 1890 : BLOCK_SIGINT_END;
1207 1890 : if (!tmp) pari_err(e_MEM);
1208 1890 : return tmp;
1209 : }
1210 : INLINE void
1211 2256118 : pari_realloc_ip(void **pointer, size_t size)
1212 : {
1213 : char *tmp;
1214 2256118 : BLOCK_SIGINT_START;
1215 2256088 : if (!*pointer) tmp = (char *) malloc(size);
1216 105450 : else tmp = (char *) realloc(*pointer,size);
1217 2256088 : if (!tmp) pari_err(e_MEM);
1218 2256088 : *pointer = tmp;
1219 2256088 : BLOCK_SIGINT_END;
1220 2255925 : }
1221 :
1222 : INLINE void*
1223 47713 : pari_calloc(size_t size)
1224 : {
1225 47713 : void *t = pari_malloc(size);
1226 47713 : memset(t, 0, size); return t;
1227 : }
1228 : INLINE GEN
1229 10166 : cgetalloc(size_t l, long t)
1230 : { /* evallg may raise e_OVERFLOW, which would leak x */
1231 10166 : ulong x0 = evaltyp(t) | evallg(l);
1232 10166 : GEN x = (GEN)pari_malloc(l * sizeof(long));
1233 10166 : x[0] = x0; return x;
1234 : }
1235 :
1236 : /*******************************************************************/
1237 : /* */
1238 : /* GARBAGE COLLECTION */
1239 : /* */
1240 : /*******************************************************************/
1241 : /* copy integer x as if we had set_avma(av) */
1242 : INLINE GEN
1243 8035082774 : icopy_avma(GEN x, pari_sp av)
1244 : {
1245 8035082774 : long i = lgefint(x), lx = i;
1246 8035082774 : GEN y = ((GEN)av) - i;
1247 58356063567 : while (--i > 0) y[i] = x[i];
1248 8035082774 : y[0] = evaltyp(t_INT)|evallg(lx);
1249 8038212228 : return y;
1250 : }
1251 : /* copy leaf x as if we had set_avma(av) */
1252 : INLINE GEN
1253 577275042 : leafcopy_avma(GEN x, pari_sp av)
1254 : {
1255 577275042 : long i = lg(x);
1256 577275042 : GEN y = ((GEN)av) - i;
1257 3072506333 : while (--i > 0) y[i] = x[i];
1258 577275042 : y[0] = x[0] & (~CLONEBIT);
1259 577275042 : return y;
1260 : }
1261 : INLINE GEN
1262 1135301704 : gc_leaf(pari_sp av, GEN x)
1263 : {
1264 : long lx;
1265 : GEN q;
1266 :
1267 1135301704 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1268 1134039456 : lx = lg(x);
1269 1134039456 : q = ((GEN)av) - lx;
1270 1134039456 : set_avma((pari_sp)q);
1271 11288606428 : while (--lx >= 0) q[lx] = x[lx];
1272 1133764553 : return q;
1273 : }
1274 : INLINE GEN
1275 4106957649 : gc_INT(pari_sp av, GEN x)
1276 : {
1277 4106957649 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1278 3658988373 : set_avma((pari_sp)icopy_avma(x, av));
1279 3662507593 : return (GEN)avma;
1280 : }
1281 : INLINE void
1282 8490619 : gc_INT_affii(pari_sp av, GEN x, GEN *y)
1283 : {
1284 8490619 : long l = lg(*y);
1285 8490619 : if (lgefint(x) <= l && isonstack(*y))
1286 : {
1287 6836864 : affii(x,*y);
1288 6839352 : set_avma(av);
1289 : }
1290 : else
1291 1653554 : *y = gc_INT(av, x);
1292 8502674 : }
1293 : INLINE GEN
1294 1951907082 : gc_upto(pari_sp av, GEN q)
1295 : {
1296 1951907082 : if (!isonstack(q) || (GEN)av<=q) return gc_const(av,q);
1297 1759919989 : switch(typ(q))
1298 : { /* first all non recursive types */
1299 528865306 : case t_INT: return gc_INT(av, q);
1300 348768850 : case t_REAL:
1301 : case t_STR:
1302 348768850 : case t_VECSMALL: return gc_leaf(av,q);
1303 882290083 : default: return (GEN)((pari_sp)q + gc_stack_update(av, (pari_sp)(q+lg(q))));
1304 : }
1305 : }
1306 :
1307 : /* gc_upto(av, gcopy(x)) */
1308 : INLINE GEN
1309 255373925 : gc_GEN(pari_sp av, GEN x)
1310 : {
1311 255373925 : if (is_recursive_t(typ(x)))
1312 : {
1313 218635473 : GENbin *p = copy_bin(x);
1314 218663953 : set_avma(av); return bin_copy(p);
1315 : }
1316 : else
1317 : {
1318 36737928 : set_avma(av);
1319 36740224 : if (x < (GEN)av) {
1320 36522891 : if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
1321 36520010 : x = leafcopy_avma(x, av);
1322 36518929 : set_avma((pari_sp)x);
1323 : } else
1324 217846 : x = leafcopy(x);
1325 36734321 : return x;
1326 : }
1327 : }
1328 :
1329 : INLINE void
1330 47931521 : guncloneNULL(GEN x) { if (x) gunclone(x); }
1331 : INLINE void
1332 1435271 : guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
1333 :
1334 : /* assume 1 <= n < 10 */
1335 : INLINE GEN
1336 109576582 : gc_all(pari_sp av, int n, ...)
1337 : {
1338 : int i;
1339 109576582 : va_list a; va_start(a, n);
1340 109576582 : if (n < 10)
1341 : {
1342 : GEN *v[10];
1343 362652279 : for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
1344 109578345 : set_avma(av);
1345 362656996 : for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
1346 109579879 : va_end(a); return *v[0];
1347 : }
1348 : else
1349 : {
1350 0 : GEN z, **v = (GEN**)pari_malloc(n*sizeof(GEN*));
1351 0 : for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
1352 0 : set_avma(av);
1353 0 : for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
1354 0 : z = *v[0]; pari_free(v); va_end(a); return z;
1355 : }
1356 : }
1357 :
1358 : INLINE void
1359 2204041 : gc_slice(pari_sp av, GEN x, int n)
1360 : {
1361 : int i;
1362 26241545 : for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
1363 2204387 : set_avma(av);
1364 26242524 : for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
1365 2204187 : }
1366 :
1367 : /* p from copy_bin. Copy p->x back to stack, then destroy p */
1368 : INLINE GEN
1369 495823575 : bin_copy(GENbin *p)
1370 : {
1371 : GEN x, y, base;
1372 : long dx, len;
1373 :
1374 495823575 : x = p->x; if (!x) { pari_free(p); return gen_0; }
1375 465633121 : len = p->len;
1376 465633121 : base= p->base; dx = x - base;
1377 465633121 : y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
1378 465622958 : y += dx;
1379 465622958 : p->rebase(y, ((ulong)y-(ulong)x));
1380 465625251 : pari_free(p); return y;
1381 : }
1382 :
1383 : INLINE GEN
1384 961453398 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
1385 :
1386 : INLINE void
1387 102261301 : cgiv(GEN x)
1388 : {
1389 102261301 : pari_sp av = (pari_sp)(x+lg(x));
1390 102261301 : if (isonstack((GEN)av)) set_avma(av);
1391 102275779 : }
1392 :
1393 : INLINE void
1394 1920356 : killblock(GEN x) { gunclone(x); }
1395 :
1396 : INLINE int
1397 325769908 : is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
1398 :
1399 : /*******************************************************************/
1400 : /* */
1401 : /* CONVERSION / ASSIGNMENT */
1402 : /* */
1403 : /*******************************************************************/
1404 : /* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
1405 : INLINE GEN
1406 18210290 : cxcompotor(GEN z, long prec)
1407 : {
1408 18210290 : switch(typ(z))
1409 : {
1410 11780454 : case t_INT: return itor(z, prec);
1411 293678 : case t_FRAC: return fractor(z, prec);
1412 6136379 : case t_REAL: return rtor(z, prec);
1413 0 : default: pari_err_TYPE("cxcompotor",z);
1414 : return NULL; /* LCOV_EXCL_LINE */
1415 : }
1416 : }
1417 : INLINE GEN
1418 9079302 : cxtofp(GEN x, long prec)
1419 9079302 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
1420 :
1421 : INLINE GEN
1422 337201 : cxtoreal(GEN q)
1423 337201 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
1424 :
1425 : INLINE double
1426 60779366 : gtodouble(GEN x)
1427 : {
1428 60779366 : if (typ(x)!=t_REAL) {
1429 8279558 : pari_sp av = avma;
1430 8279558 : x = gtofp(x, DEFAULTPREC);
1431 8278847 : if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
1432 8278847 : set_avma(av);
1433 : }
1434 60778548 : return rtodbl(x);
1435 : }
1436 :
1437 : INLINE int
1438 2991980 : gisdouble(GEN x, double *g)
1439 : {
1440 2991980 : if (typ(x)!=t_REAL) {
1441 42376 : pari_sp av = avma;
1442 42376 : x = gtofp(x, DEFAULTPREC);
1443 42376 : if (typ(x)!=t_REAL) return gc_double(av, 0);
1444 42376 : set_avma(av);
1445 : }
1446 2991985 : if (expo(x) >= 0x3ff) return 0;
1447 2991985 : *g = rtodbl(x); return 1;
1448 : }
1449 :
1450 : INLINE long
1451 85855590 : gtos(GEN x) {
1452 85855590 : if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
1453 85855576 : return itos(x);
1454 : }
1455 :
1456 : INLINE ulong
1457 99448 : gtou(GEN x) {
1458 99448 : if (typ(x) != t_INT || signe(x)<0)
1459 0 : pari_err_TYPE("gtou [integer >=0 expected]",x);
1460 99448 : return itou(x);
1461 : }
1462 :
1463 : INLINE GEN
1464 46638429 : absfrac(GEN x)
1465 : {
1466 46638429 : GEN y = cgetg(3, t_FRAC);
1467 46638465 : gel(y,1) = absi(gel(x,1));
1468 46638468 : gel(y,2) = icopy(gel(x,2)); return y;
1469 : }
1470 : INLINE GEN
1471 28237 : absfrac_shallow(GEN x)
1472 28237 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
1473 : INLINE GEN
1474 8062625 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
1475 : INLINE GEN
1476 120231 : Q_abs_shallow(GEN x)
1477 120231 : { return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
1478 : INLINE GEN
1479 13097 : R_abs_shallow(GEN x)
1480 13097 : { return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
1481 : INLINE GEN
1482 0 : R_abs(GEN x)
1483 0 : { return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
1484 :
1485 : /* Force z to be of type real/complex with floating point components */
1486 : INLINE GEN
1487 254301829 : gtofp(GEN z, long prec)
1488 : {
1489 254301829 : switch(typ(z))
1490 : {
1491 215402807 : case t_INT: return itor(z, prec);
1492 4097293 : case t_FRAC: return fractor(z, prec);
1493 26029789 : case t_REAL: return rtor(z, prec);
1494 8779226 : case t_COMPLEX: {
1495 8779226 : GEN a = gel(z,1), b = gel(z,2);
1496 8779226 : if (isintzero(b)) return cxcompotor(a, prec);
1497 8779239 : if (isintzero(a)) {
1498 27468 : GEN y = cgetg(3, t_COMPLEX);
1499 27468 : b = cxcompotor(b, prec);
1500 27468 : gel(y,1) = real_0_bit(expo(b) - prec);
1501 27468 : gel(y,2) = b; return y;
1502 : }
1503 8751760 : return cxtofp(z, prec);
1504 : }
1505 0 : case t_QUAD: return quadtofp(z, prec);
1506 0 : default: pari_err_TYPE("gtofp",z);
1507 : return NULL; /* LCOV_EXCL_LINE */
1508 : }
1509 : }
1510 : /* Force z to be of type real / int */
1511 : INLINE GEN
1512 22428 : gtomp(GEN z, long prec)
1513 : {
1514 22428 : switch(typ(z))
1515 : {
1516 42 : case t_INT: return z;
1517 22386 : case t_FRAC: return fractor(z, prec);
1518 0 : case t_REAL: return rtor(z, prec);
1519 0 : case t_QUAD: z = quadtofp(z, prec);
1520 0 : if (typ(z) == t_REAL) return z;
1521 0 : default: pari_err_TYPE("gtomp",z);
1522 : return NULL; /* LCOV_EXCL_LINE */
1523 : }
1524 : }
1525 :
1526 : INLINE GEN
1527 7144827 : RgX_gtofp(GEN x, long prec)
1528 37263612 : { pari_APPLY_pol_normalized(gtofp(gel(x,i), prec)); }
1529 :
1530 : INLINE GEN
1531 34030267 : RgC_gtofp(GEN x, long prec)
1532 219221510 : { pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
1533 :
1534 : INLINE GEN
1535 56 : RgV_gtofp(GEN x, long prec)
1536 4781 : { pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
1537 :
1538 : INLINE GEN
1539 8257757 : RgM_gtofp(GEN x, long prec)
1540 41656561 : { pari_APPLY_same(RgC_gtofp(gel(x,i), prec)) }
1541 :
1542 : INLINE GEN
1543 574 : RgC_gtomp(GEN x, long prec)
1544 23002 : { pari_APPLY_type(t_COL, gtomp(gel(x,i), prec)) }
1545 :
1546 : INLINE GEN
1547 21 : RgM_gtomp(GEN x, long prec)
1548 595 : { pari_APPLY_same(RgC_gtomp(gel(x,i), prec)) }
1549 :
1550 : INLINE GEN
1551 61495 : RgX_fpnorml2(GEN x, long prec)
1552 : {
1553 61495 : pari_sp av = avma;
1554 61495 : return gc_upto(av, gnorml2(RgX_gtofp(x, prec)));
1555 : }
1556 : INLINE GEN
1557 619833 : RgC_fpnorml2(GEN x, long prec)
1558 : {
1559 619833 : pari_sp av = avma;
1560 619833 : return gc_upto(av, gnorml2(RgC_gtofp(x, prec)));
1561 : }
1562 : INLINE GEN
1563 22661 : RgM_fpnorml2(GEN x, long prec)
1564 : {
1565 22661 : pari_sp av = avma;
1566 22661 : return gc_upto(av, gnorml2(RgM_gtofp(x, prec)));
1567 : }
1568 :
1569 : /* y a t_REAL */
1570 : INLINE void
1571 898866 : affgr(GEN x, GEN y)
1572 : {
1573 898866 : switch(typ(x)) {
1574 289072 : case t_INT: affir(x,y); break;
1575 609794 : case t_REAL: affrr(x,y); break;
1576 0 : case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
1577 0 : default: pari_err_TYPE2("=",x,y);
1578 : }
1579 898866 : }
1580 :
1581 : INLINE GEN
1582 249622 : affc_fixlg(GEN x, GEN res)
1583 : {
1584 249622 : if (typ(x) == t_COMPLEX)
1585 : {
1586 206892 : affrr_fixlg(gel(x,1), gel(res,1));
1587 206892 : affrr_fixlg(gel(x,2), gel(res,2));
1588 : }
1589 : else
1590 : {
1591 42730 : set_avma((pari_sp)(res+3));
1592 42730 : res = cgetr(realprec(gel(res,1)));
1593 42731 : affrr_fixlg(x, res);
1594 : }
1595 249623 : return res;
1596 : }
1597 :
1598 : INLINE GEN
1599 0 : trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
1600 :
1601 : /*******************************************************************/
1602 : /* */
1603 : /* LENGTH CONVERSIONS */
1604 : /* */
1605 : /*******************************************************************/
1606 : INLINE long
1607 41549 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
1608 : INLINE long
1609 33279 : ndec2prec(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1610 : INLINE long
1611 8270 : ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1612 : /* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
1613 : * to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
1614 : * addl, shrl instead of subl, sarl, addl */
1615 : INLINE long
1616 12750296 : nbits2nlong(long x) {
1617 12750296 : return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1618 : }
1619 :
1620 : INLINE long
1621 1456926912 : nbits2extraprec(long x) {
1622 1456926912 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1623 : }
1624 :
1625 : INLINE long
1626 186127931 : nbits2prec(long x) {
1627 186127931 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1628 : }
1629 :
1630 : INLINE long
1631 4468176244 : prec2lg(long x) {
1632 4468176244 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1633 : }
1634 : /* ceil(x / sizeof(long)) */
1635 : INLINE long
1636 127385043 : nchar2nlong(long x) {
1637 127385043 : return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
1638 : }
1639 : INLINE long
1640 103961707 : prec2nbits(long x) { return x; }
1641 : INLINE double
1642 3175472 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
1643 : INLINE double
1644 581167 : prec2nbits_mul(long x, double y) { return x * y; }
1645 : INLINE long
1646 142806232 : bit_prec(GEN x) { return realprec(x); }
1647 : INLINE long
1648 3155645265 : bit_accuracy(long x) { return (x-2) * BITS_IN_LONG; }
1649 : INLINE long
1650 9778 : prec2ndec(long x) { return (long)(x * LOG10_2); }
1651 : INLINE long
1652 228 : nbits2ndec(long x) { return prec2ndec(x); }
1653 : INLINE long
1654 475373 : precdbl(long x) {return x << 1;}
1655 : INLINE long
1656 8087125235 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
1657 : INLINE long
1658 7996190651 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
1659 :
1660 : /*********************************************************************/
1661 : /** **/
1662 : /** OPERATIONS MODULO m **/
1663 : /** **/
1664 : /*********************************************************************/
1665 : /* Assume m > 0, more efficient if 0 <= a, b < m */
1666 :
1667 : INLINE GEN
1668 82191040 : Fp_red(GEN a, GEN m) { return modii(a, m); }
1669 : INLINE GEN
1670 181060779 : Fp_add(GEN a, GEN b, GEN m)
1671 : {
1672 181060779 : pari_sp av=avma;
1673 181060779 : GEN p = addii(a,b);
1674 180541570 : long s = signe(p);
1675 180541570 : if (!s) return p; /* = gen_0 */
1676 167364863 : if (s > 0) /* general case */
1677 : {
1678 167113845 : GEN t = subii(p, m);
1679 166962538 : s = signe(t);
1680 166962538 : if (!s) return gc_const(av, gen_0);
1681 158691426 : if (s < 0) return gc_const((pari_sp)p, p);
1682 76180530 : if (cmpii(t, m) < 0) return gc_INT(av, t); /* general case ! */
1683 3289232 : p = remii(t, m);
1684 : }
1685 : else
1686 260255 : p = modii(p, m);
1687 3549591 : return gc_INT(av, p);
1688 : }
1689 : INLINE GEN
1690 20888125 : Fp_double(GEN x, GEN N)
1691 : {
1692 20888125 : GEN z = shifti(x, 1);
1693 20487729 : return cmpii(z, N) >= 0? subii(z, N): z;
1694 : }
1695 : INLINE GEN
1696 203177418 : Fp_sub(GEN a, GEN b, GEN m)
1697 : {
1698 203177418 : pari_sp av=avma;
1699 203177418 : GEN p = subii(a,b);
1700 201504278 : long s = signe(p);
1701 201504278 : if (!s) return p; /* = gen_0 */
1702 185816310 : if (s > 0)
1703 : {
1704 93055533 : if (cmpii(p, m) < 0) return p; /* general case ! */
1705 1446632 : p = remii(p, m);
1706 : }
1707 : else
1708 : {
1709 92760777 : GEN t = addii(p, m);
1710 94062136 : if (!s) return gc_const(av, gen_0);
1711 94056820 : if (s > 0) return gc_INT(av, t); /* general case ! */
1712 94056820 : p = modii(t, m);
1713 : }
1714 95790233 : return gc_INT(av, p);
1715 : }
1716 : INLINE GEN
1717 31478964 : Fp_neg(GEN b, GEN m)
1718 : {
1719 31478964 : pari_sp av = avma;
1720 31478964 : long s = signe(b);
1721 : GEN p;
1722 31478964 : if (!s) return gen_0;
1723 26404195 : if (s > 0)
1724 : {
1725 25143132 : p = subii(m, b);
1726 25142749 : if (signe(p) >= 0) return p; /* general case ! */
1727 611091 : p = modii(p, m);
1728 : } else
1729 1261533 : p = remii(negi(b), m);
1730 1872727 : return gc_INT(av, p);
1731 : }
1732 :
1733 : INLINE GEN
1734 247138 : Fp_halve(GEN a, GEN p)
1735 : {
1736 247138 : if (mpodd(a)) a = addii(a,p);
1737 247138 : return shifti(a,-1);
1738 : }
1739 :
1740 : /* assume 0 <= u < p and ps2 = p>>1 */
1741 : INLINE GEN
1742 77194347 : Fp_center(GEN u, GEN p, GEN ps2)
1743 77194347 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
1744 : /* same without copy */
1745 : INLINE GEN
1746 18016034 : Fp_center_i(GEN u, GEN p, GEN ps2)
1747 18016034 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
1748 :
1749 : /* x + y*z mod p */
1750 : INLINE GEN
1751 11246108 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
1752 : {
1753 : pari_sp av;
1754 11246108 : if (!signe(y) || !signe(z)) return Fp_red(x, p);
1755 10780541 : if (!signe(x)) return Fp_mul(z,y, p);
1756 9749435 : av = avma;
1757 9749435 : return gc_INT(av, modii(addii(x, mulii(y,z)), p));
1758 : }
1759 :
1760 : INLINE GEN
1761 200735852 : Fp_mul(GEN a, GEN b, GEN m)
1762 : {
1763 200735852 : pari_sp av=avma;
1764 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1765 200735852 : (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
1766 200793192 : p = mulii(a,b);
1767 200622745 : set_avma(av); return modii(p,m);
1768 : }
1769 : INLINE GEN
1770 63781873 : Fp_sqr(GEN a, GEN m)
1771 : {
1772 63781873 : pari_sp av=avma;
1773 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1774 63781873 : (void)new_chunk((lg(a)+lg(m))<<1);
1775 64139521 : p = sqri(a);
1776 62401952 : set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
1777 : }
1778 : INLINE GEN
1779 67481647 : Fp_mulu(GEN a, ulong b, GEN m)
1780 : {
1781 67481647 : long l = lgefint(m);
1782 67481647 : if (l == 3)
1783 : {
1784 46008028 : ulong mm = m[2];
1785 46008028 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1786 : } else {
1787 21473619 : pari_sp av = avma;
1788 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1789 21473619 : (void)new_chunk(lg(a)+1+(l<<1));
1790 21454861 : p = muliu(a,b);
1791 21233314 : set_avma(av); return modii(p,m);
1792 : }
1793 : }
1794 : INLINE GEN
1795 17780 : Fp_muls(GEN a, long b, GEN m)
1796 : {
1797 17780 : long l = lgefint(m);
1798 17780 : if (l == 3)
1799 : {
1800 3578 : ulong mm = m[2];
1801 3578 : if (b < 0)
1802 : {
1803 3578 : ulong t = Fl_mul(umodiu(a, mm), -b, mm);
1804 3578 : return t? utoipos(mm - t): gen_0;
1805 : }
1806 : else
1807 0 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1808 : } else {
1809 14202 : pari_sp av = avma;
1810 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1811 14202 : (void)new_chunk(lg(a)+1+(l<<1));
1812 14202 : p = mulis(a,b);
1813 14202 : set_avma(av); return modii(p,m);
1814 : }
1815 : }
1816 :
1817 : INLINE GEN
1818 23439138 : Fp_inv(GEN a, GEN m)
1819 : {
1820 : GEN res;
1821 23439138 : if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
1822 23438920 : return res;
1823 : }
1824 : INLINE GEN
1825 1100357 : Fp_invsafe(GEN a, GEN m)
1826 : {
1827 : GEN res;
1828 1100357 : if (! invmod(a,m,&res)) return NULL;
1829 1100307 : return res;
1830 : }
1831 : INLINE GEN
1832 15035267 : Fp_div(GEN a, GEN b, GEN m)
1833 : {
1834 15035267 : pari_sp av = avma;
1835 : GEN p;
1836 15035267 : if (lgefint(b) == 3)
1837 : {
1838 13890686 : a = Fp_divu(a, b[2], m);
1839 13892425 : if (signe(b) < 0) a = Fp_neg(a, m);
1840 13892432 : return a;
1841 : }
1842 : /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1843 1144581 : (void)new_chunk(lg(a)+(lg(m)<<1));
1844 1144587 : p = mulii(a, Fp_inv(b,m));
1845 1144587 : set_avma(av); return modii(p,m);
1846 : }
1847 : INLINE GEN
1848 15129720 : Fp_divu(GEN x, ulong a, GEN p)
1849 : {
1850 15129720 : pari_sp av = avma;
1851 : ulong b;
1852 15129720 : if (lgefint(p) == 3)
1853 : {
1854 14602056 : ulong pp = p[2], xp = umodiu(x, pp);
1855 14602872 : return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
1856 : }
1857 527664 : x = Fp_red(x, p);
1858 527670 : b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
1859 527670 : return gc_INT(av, diviuexact(addmuliu(x, p, b), a));
1860 : }
1861 :
1862 : INLINE GEN
1863 1105412 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
1864 :
1865 : INLINE GEN
1866 2499219 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1867 :
1868 : INLINE long
1869 2533888 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1870 :
1871 : INLINE long
1872 1928975 : get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
1873 :
1874 : INLINE GEN
1875 343 : get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1876 :
1877 : INLINE long
1878 375375 : get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1879 :
1880 : INLINE long
1881 200851 : get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1882 :
1883 : INLINE GEN
1884 25593701 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1885 :
1886 : INLINE long
1887 68348023 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1888 :
1889 : INLINE long
1890 82230995 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1891 :
1892 : INLINE GEN
1893 8033 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1894 :
1895 : INLINE long
1896 264420 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1897 :
1898 : INLINE long
1899 317565 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1900 :
1901 : INLINE GEN
1902 3394358 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1903 :
1904 : INLINE long
1905 6957847 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1906 :
1907 : INLINE long
1908 6424515 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1909 :
1910 : INLINE GEN
1911 146583 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1912 :
1913 : INLINE long
1914 51630 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1915 :
1916 : INLINE long
1917 3589 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1918 :
1919 : /*******************************************************************/
1920 : /* */
1921 : /* ADDMULII / SUBMULII */
1922 : /* */
1923 : /*******************************************************************/
1924 : /* x - y*z */
1925 : INLINE GEN
1926 32838964 : submulii(GEN x, GEN y, GEN z)
1927 : {
1928 32838964 : long lx = lgefint(x), ly, lz;
1929 : pari_sp av;
1930 : GEN t;
1931 32838964 : if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
1932 30292741 : ly = lgefint(y);
1933 30292741 : if (ly == 2) return icopy(x);
1934 29019564 : lz = lgefint(z);
1935 29019564 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1936 29019564 : t = mulii(z, y);
1937 29019564 : set_avma(av); return subii(x,t);
1938 : }
1939 : /* y*z - x */
1940 : INLINE GEN
1941 3880946 : mulsubii(GEN y, GEN z, GEN x)
1942 : {
1943 3880946 : long lx = lgefint(x), ly, lz;
1944 : pari_sp av;
1945 : GEN t;
1946 3880946 : if (lx == 2) return mulii(z,y);
1947 2450819 : ly = lgefint(y);
1948 2450819 : if (ly == 2) return negi(x);
1949 2166236 : lz = lgefint(z);
1950 2166236 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1951 2166268 : t = mulii(z, y);
1952 2166243 : set_avma(av); return subii(t,x);
1953 : }
1954 :
1955 : /* x - u*y */
1956 : INLINE GEN
1957 7700 : submuliu(GEN x, GEN y, ulong u)
1958 : {
1959 : pari_sp av;
1960 7700 : long ly = lgefint(y);
1961 7700 : if (ly == 2) return icopy(x);
1962 7700 : av = avma;
1963 7700 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1964 7700 : y = mului(u,y);
1965 7700 : set_avma(av); return subii(x, y);
1966 : }
1967 : /* x + u*y */
1968 : INLINE GEN
1969 535745 : addmuliu(GEN x, GEN y, ulong u)
1970 : {
1971 : pari_sp av;
1972 535745 : long ly = lgefint(y);
1973 535745 : if (ly == 2) return icopy(x);
1974 535745 : av = avma;
1975 535745 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1976 535745 : y = mului(u,y);
1977 535745 : set_avma(av); return addii(x, y);
1978 : }
1979 : /* x - u*y */
1980 : INLINE GEN
1981 60142613 : submuliu_inplace(GEN x, GEN y, ulong u)
1982 : {
1983 : pari_sp av;
1984 60142613 : long ly = lgefint(y);
1985 60142613 : if (ly == 2) return x;
1986 38157382 : av = avma;
1987 38157382 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1988 38161215 : y = mului(u,y);
1989 38161057 : set_avma(av); return subii(x, y);
1990 : }
1991 : /* x + u*y */
1992 : INLINE GEN
1993 59647744 : addmuliu_inplace(GEN x, GEN y, ulong u)
1994 : {
1995 : pari_sp av;
1996 59647744 : long ly = lgefint(y);
1997 59647744 : if (ly == 2) return x;
1998 38060569 : av = avma;
1999 38060569 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
2000 38064289 : y = mului(u,y);
2001 38064177 : set_avma(av); return addii(x, y);
2002 : }
2003 : /* ux + vy */
2004 : INLINE GEN
2005 38201617 : lincombii(GEN u, GEN v, GEN x, GEN y)
2006 : {
2007 38201617 : long lx = lgefint(x), ly;
2008 : GEN p1, p2;
2009 : pari_sp av;
2010 38201617 : if (lx == 2) return mulii(v,y);
2011 24015320 : ly = lgefint(y);
2012 24015320 : if (ly == 2) return mulii(u,x);
2013 21278933 : av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
2014 21281961 : p1 = mulii(u,x);
2015 21280741 : p2 = mulii(v,y);
2016 21280742 : set_avma(av); return addii(p1,p2);
2017 : }
2018 :
2019 : /*******************************************************************/
2020 : /* */
2021 : /* GEN SUBTYPES */
2022 : /* */
2023 : /*******************************************************************/
2024 :
2025 : INLINE int
2026 4893063574 : is_const_t(long t) { return (t < t_POLMOD); }
2027 : INLINE int
2028 6876 : is_extscalar_t(long t) { return (t <= t_POL); }
2029 : INLINE int
2030 9585755 : is_intreal_t(long t) { return (t <= t_REAL); }
2031 : INLINE int
2032 659416699 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
2033 : INLINE int
2034 85874965 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
2035 : INLINE int
2036 0 : is_qfb_t(long t) { return (t == t_QFB); }
2037 : INLINE int
2038 5032358 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
2039 : INLINE int
2040 62062467 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
2041 : INLINE int
2042 9233900140 : is_recursive_t(long t) { return lontyp[t]; }
2043 : INLINE int
2044 375660753 : is_scalar_t(long t) { return (t < t_POL); }
2045 : INLINE int
2046 43937007 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
2047 :
2048 : INLINE int
2049 62262579 : qfb_is_qfi(GEN q) { return signe(gel(q,4)) < 0; }
2050 :
2051 : /*******************************************************************/
2052 : /* */
2053 : /* TRANSCENDENTAL */
2054 : /* */
2055 : /*******************************************************************/
2056 : INLINE GEN
2057 80369343 : sqrtr(GEN x) {
2058 80369343 : long s = signe(x);
2059 80369343 : if (s == 0) return real_0_bit(expo(x) >> 1);
2060 80336165 : if (s >= 0) return sqrtr_abs(x);
2061 439624 : retmkcomplex(gen_0, sqrtr_abs(x));
2062 : }
2063 : INLINE GEN
2064 0 : cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
2065 : INLINE GEN
2066 0 : cbrtr(GEN x) {
2067 0 : long s = signe(x);
2068 : GEN r;
2069 0 : if (s == 0) return real_0_bit(expo(x) / 3);
2070 0 : r = cbrtr_abs(x);
2071 0 : if (s < 0) togglesign(r);
2072 0 : return r;
2073 : }
2074 : INLINE GEN
2075 2809108 : sqrtnr(GEN x, long n) {
2076 2809108 : long s = signe(x);
2077 : GEN r;
2078 2809108 : if (s == 0) return real_0_bit(expo(x) / n);
2079 2809108 : r = sqrtnr_abs(x, n);
2080 2809135 : if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
2081 2809135 : return r;
2082 : }
2083 : INLINE long
2084 754827 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
2085 : INLINE ulong
2086 1727620 : ulogint(ulong B, ulong y)
2087 : {
2088 : ulong r;
2089 : long e;
2090 1727620 : if (y == 2) return expu(B);
2091 1679585 : r = y;
2092 5078171 : for (e=1;; e++)
2093 : { /* here, r = y^e, r2 = y^(e-1) */
2094 5078171 : if (r >= B) return r == B? e: e-1;
2095 3400095 : r = umuluu_or_0(y, r);
2096 3400128 : if (!r) return e;
2097 : }
2098 : }
2099 :
2100 : /*******************************************************************/
2101 : /* */
2102 : /* MISCELLANEOUS */
2103 : /* */
2104 : /*******************************************************************/
2105 9585650 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
2106 2842719568 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
2107 17585124 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
2108 5710164 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
2109 1174083021 : INLINE int equali1(GEN n)
2110 1174083021 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
2111 139505230 : INLINE int equalim1(GEN n)
2112 139505230 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
2113 : /* works only for POSITIVE integers */
2114 2202928174 : INLINE int is_pm1(GEN n)
2115 2202928174 : { return lgefint(n) == 3 && n[2] == 1; }
2116 562928975 : INLINE int is_bigint(GEN n)
2117 562928975 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
2118 :
2119 2089587382 : INLINE int odd(long x) { return x & 1; }
2120 123977372 : INLINE int both_odd(long x, long y) { return x & y & 1; }
2121 :
2122 : INLINE int
2123 8265126400 : isonstack(GEN x)
2124 8265126400 : { return ((pari_sp)x >= pari_mainstack->bot
2125 8265126400 : && (pari_sp)x < pari_mainstack->top); }
2126 :
2127 : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
2128 : INLINE double
2129 92816910 : dbllog2r(GEN x)
2130 92816910 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
2131 :
2132 : INLINE GEN
2133 2654273 : mul_content(GEN cx, GEN cy)
2134 : {
2135 2654273 : if (!cx) return cy;
2136 568072 : if (!cy) return cx;
2137 374422 : return gmul(cx,cy);
2138 : }
2139 : INLINE GEN
2140 0 : inv_content(GEN c) { return c? ginv(c): NULL; }
2141 : INLINE GEN
2142 58518 : div_content(GEN cx, GEN cy)
2143 : {
2144 58518 : if (!cy) return cx;
2145 57384 : if (!cx) return ginv(cy);
2146 26032 : return gdiv(cx,cy);
2147 : }
2148 : INLINE GEN
2149 10711106 : mul_denom(GEN dx, GEN dy)
2150 : {
2151 10711106 : if (!dx) return dy;
2152 1839283 : if (!dy) return dx;
2153 1370783 : return mulii(dx,dy);
2154 : }
2155 :
2156 : /* POLYNOMIALS */
2157 : INLINE GEN
2158 1441269 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
2159 : INLINE GEN
2160 170262124 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
2161 : INLINE ulong
2162 980303 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
2163 : INLINE ulong
2164 333922 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
2165 : INLINE long
2166 5488631661 : degpol(GEN x) { return lg(x)-3; }
2167 : INLINE long
2168 2684312092 : lgpol(GEN x) { return lg(x)-2; }
2169 : INLINE long
2170 350603632 : lgcols(GEN x) { return lg(gel(x,1)); }
2171 : INLINE long
2172 157440258 : nbrows(GEN x) { return lg(gel(x,1))-1; }
2173 : INLINE GEN
2174 0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
2175 :
2176 : INLINE GEN
2177 2161322 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
2178 : INLINE GEN
2179 960491 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
2180 :
2181 : INLINE GEN
2182 103406925 : RgX_copy(GEN x)
2183 376343960 : { pari_APPLY_pol_normalized(gcopy(gel(x,i))); }
2184 : /* have to use ulong to avoid silly warnings from gcc "assuming signed
2185 : * overflow does not occur" */
2186 : INLINE GEN
2187 2669861 : RgX_coeff(GEN x, long n)
2188 : {
2189 2669861 : ulong l = lg(x);
2190 2669861 : return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
2191 : }
2192 : INLINE GEN
2193 349807 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
2194 : INLINE GEN
2195 7000740 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
2196 : INLINE GEN
2197 1904 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
2198 : INLINE GEN
2199 127635 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
2200 : INLINE GEN
2201 5581277 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
2202 : INLINE GEN
2203 1997941 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
2204 : INLINE GEN
2205 21685239 : Flx_div_pre(GEN x, GEN y, ulong p, ulong pi)
2206 21685239 : { return Flx_divrem_pre(x,y,p,pi, NULL); }
2207 : INLINE GEN
2208 1569827 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
2209 : INLINE GEN
2210 0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
2211 : INLINE GEN
2212 103461645 : pol0_Flx(long sv) { return mkvecsmall(sv); }
2213 : INLINE GEN
2214 55051564 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
2215 : INLINE GEN
2216 37482415 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
2217 : INLINE GEN
2218 0 : zero_zx(long sv) { return zero_Flx(sv); }
2219 : INLINE GEN
2220 0 : polx_zx(long sv) { return polx_Flx(sv); }
2221 : INLINE GEN
2222 0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
2223 : INLINE GEN
2224 14049 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
2225 : INLINE GEN
2226 1350 : zero_F2x(long sv) { return zero_Flx(sv); }
2227 : INLINE GEN
2228 13240129 : pol0_F2x(long sv) { return pol0_Flx(sv); }
2229 : INLINE GEN
2230 3957026 : pol1_F2x(long sv) { return pol1_Flx(sv); }
2231 : INLINE GEN
2232 1078161 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
2233 : INLINE int
2234 2874126 : F2x_equal1(GEN x) { return Flx_equal1(x); }
2235 : INLINE int
2236 4659341 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
2237 : INLINE GEN
2238 60441593 : F2x_copy(GEN x) { return leafcopy(x); }
2239 : INLINE GEN
2240 4487 : F2v_copy(GEN x) { return leafcopy(x); }
2241 : INLINE GEN
2242 2554921 : Flv_copy(GEN x) { return leafcopy(x); }
2243 : INLINE GEN
2244 173150859 : Flx_copy(GEN x) { return leafcopy(x); }
2245 : INLINE GEN
2246 4403857 : vecsmall_copy(GEN x) { return leafcopy(x); }
2247 : INLINE int
2248 8330158 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
2249 : INLINE int
2250 12850 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
2251 : INLINE int
2252 7374366 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
2253 :
2254 : INLINE GEN
2255 154968477 : ZX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2256 : INLINE GEN
2257 144207885 : FpX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2258 : INLINE GEN
2259 849896 : FpXX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2260 : INLINE GEN
2261 4694252 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2262 : INLINE GEN
2263 115219065 : F2x_renormalize(GEN x, long lx) { return Flx_renormalize(x,lx); }
2264 :
2265 : INLINE GEN
2266 0 : F2xX_shift(GEN a, long n, long vs) { return FlxX_shift(a, n, vs); }
2267 :
2268 : INLINE GEN
2269 37755 : F2v_to_F2x(GEN x, long sv) {
2270 37755 : GEN y = leafcopy(x);
2271 37755 : y[1] = sv; F2x_renormalize(y, lg(y)); return y;
2272 : }
2273 :
2274 : INLINE long
2275 385 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
2276 :
2277 : INLINE long
2278 6230 : gval(GEN x, long v)
2279 6230 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
2280 :
2281 : INLINE void
2282 479366 : RgX_shift_inplace_init(long v)
2283 479366 : { if (v) (void)cgetg(v, t_VECSMALL); }
2284 : /* shift polynomial in place. assume v free cells have been left before x */
2285 : INLINE GEN
2286 479366 : RgX_shift_inplace(GEN x, long v)
2287 : {
2288 : long i, lx;
2289 : GEN z;
2290 479366 : if (!v) return x;
2291 252260 : lx = lg(x);
2292 252260 : if (lx == 2) return x;
2293 252260 : z = x + lx;
2294 : /* stackdummy's from normalizepol */
2295 252260 : while (lg(z) != v) z += lg(z);
2296 252260 : z += v;
2297 4637681 : for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
2298 672402 : for (i = 0; i < v; i++) gel(--z,0) = gen_0;
2299 252260 : z -= 2;
2300 252260 : z[1] = x[1];
2301 252260 : z[0] = evaltyp(t_POL) | evallg(lx+v);
2302 252260 : stackdummy((pari_sp)z, (pari_sp)x); return z;
2303 : }
2304 :
2305 :
2306 : /* LINEAR ALGEBRA */
2307 : INLINE GEN
2308 502982 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
2309 : INLINE GEN
2310 7824120 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
2311 : INLINE GEN
2312 3211407 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
2313 : INLINE GEN
2314 0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
2315 : INLINE GEN
2316 104146 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
2317 : INLINE GEN
2318 0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
2319 : INLINE GEN
2320 0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
2321 : INLINE GEN
2322 36730675 : zero_zv(long x) { return zero_Flv(x); }
2323 : INLINE GEN
2324 266 : zm_transpose(GEN x) { return Flm_transpose(x); }
2325 : INLINE GEN
2326 0 : zm_copy(GEN x) { return Flm_copy(x); }
2327 : INLINE GEN
2328 2183684 : zv_copy(GEN x) { return Flv_copy(x); }
2329 : INLINE GEN
2330 0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
2331 :
2332 : INLINE GEN
2333 10695940 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfdivrem(x,y,NULL); }
2334 : INLINE GEN
2335 363768 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
2336 : INLINE GEN
2337 7116493 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
2338 : INLINE void
2339 9741538 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
2340 : INLINE GEN
2341 22174220 : RgM_shallowcopy(GEN x)
2342 96145478 : { pari_APPLY_same(leafcopy(gel(x,i))); }
2343 : INLINE GEN
2344 200475 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
2345 :
2346 : INLINE GEN
2347 0 : F3m_copy(GEN x) { return RgM_shallowcopy(x); }
2348 :
2349 : INLINE GEN
2350 6433617 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
2351 :
2352 : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
2353 : * x,y are ZV of the same length */
2354 : INLINE int
2355 23485 : ZV_dvd(GEN x, GEN y)
2356 : {
2357 23485 : long i, l = lg(x);
2358 37856 : for (i=1; i < l; i++)
2359 30464 : if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
2360 7392 : return 1;
2361 : }
2362 : INLINE GEN
2363 596448 : ZM_ZV_mod(GEN x, GEN y)
2364 1765401 : { pari_APPLY_same(ZV_ZV_mod(gel(x,i), y)) }
2365 : INLINE GEN
2366 8131420 : ZV_ZV_mod(GEN x, GEN y)
2367 22498170 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
2368 : INLINE GEN
2369 0 : vecmodii(GEN x, GEN y) { return ZV_ZV_mod(x,y); }
2370 : INLINE GEN
2371 174881 : vecmoduu(GEN x, GEN y) { pari_APPLY_ulong(((ulong*)x)[i] % ((ulong*)y)[i]) }
2372 :
2373 : /* Fq */
2374 : INLINE GEN
2375 3106165 : Fq_red(GEN x, GEN T, GEN p)
2376 3106165 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
2377 : INLINE GEN
2378 73792 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
2379 : {
2380 : (void) p;
2381 73792 : return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
2382 : }
2383 : INLINE GEN
2384 756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
2385 :
2386 : INLINE GEN
2387 29946 : gener_Fq_local(GEN T, GEN p, GEN L)
2388 0 : { return T? gener_FpXQ_local(T,p, L)
2389 29946 : : pgener_Fp_local(p, L); }
2390 :
2391 : INLINE GEN
2392 0 : random_Fq(GEN T, GEN p)
2393 0 : { return T ? random_FpX(get_FpX_degree(T), get_FpX_var(T), p): randomi(p); }
2394 :
2395 : /* FpXQX */
2396 : INLINE GEN
2397 4526 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
2398 : INLINE GEN
2399 0 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
2400 : INLINE GEN
2401 165410 : FlxqX_div_pre(GEN x, GEN y, GEN T, ulong p, ulong pi) { return FlxqX_divrem_pre(x, y, T, p, pi, NULL); }
2402 : INLINE GEN
2403 33173 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
2404 :
2405 : INLINE GEN
2406 20587 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
2407 20587 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
2408 :
2409 : /* FqX */
2410 : INLINE GEN
2411 25354 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
2412 : INLINE GEN
2413 127694 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2414 : INLINE GEN
2415 24956 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
2416 : INLINE GEN
2417 68607 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2418 : INLINE GEN
2419 571440 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
2420 571440 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
2421 : INLINE GEN
2422 427306 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
2423 427306 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
2424 : INLINE GEN
2425 367700 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
2426 367700 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
2427 : INLINE GEN
2428 21942 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
2429 21942 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
2430 : INLINE GEN
2431 71855 : FqX_sqr(GEN x, GEN T, GEN p)
2432 71855 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
2433 : INLINE GEN
2434 1295 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
2435 1295 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
2436 : INLINE GEN
2437 19411 : FqX_halve(GEN x, GEN T, GEN p)
2438 19411 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
2439 : INLINE GEN
2440 38565 : FqX_div(GEN x, GEN y, GEN T, GEN p)
2441 38565 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
2442 : INLINE GEN
2443 6090 : FqX_get_red(GEN S, GEN T, GEN p)
2444 6090 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
2445 : INLINE GEN
2446 37743 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
2447 37743 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
2448 : INLINE GEN
2449 0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
2450 0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
2451 : INLINE GEN
2452 69629 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
2453 69629 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
2454 : INLINE GEN
2455 0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
2456 0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
2457 : INLINE GEN
2458 8288 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
2459 8288 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
2460 : INLINE GEN
2461 444666 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
2462 444666 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
2463 : INLINE GEN
2464 4144 : FqX_normalize(GEN z, GEN T, GEN p)
2465 4144 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
2466 : INLINE GEN
2467 40061 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
2468 : INLINE long
2469 683802 : FqX_is_squarefree(GEN P, GEN T, GEN p)
2470 683802 : { return T ? FpXQX_is_squarefree(P, T, p): FpX_is_squarefree(P, p); }
2471 : INLINE GEN
2472 0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
2473 : INLINE GEN
2474 140946 : FqX_factor(GEN f, GEN T, GEN p)
2475 140946 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
2476 : INLINE GEN
2477 7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
2478 7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
2479 : INLINE GEN
2480 7 : FqX_ddf(GEN f, GEN T, GEN p)
2481 7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
2482 : INLINE GEN
2483 47786 : FqX_degfact(GEN f, GEN T, GEN p)
2484 47786 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
2485 : INLINE GEN
2486 7273 : FqX_roots(GEN f, GEN T, GEN p)
2487 7273 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
2488 : INLINE GEN
2489 175 : FqX_to_mod(GEN f, GEN T, GEN p)
2490 175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
2491 :
2492 : /*FqXQ*/
2493 : INLINE GEN
2494 0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2495 0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2496 : INLINE GEN
2497 0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2498 0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2499 : INLINE GEN
2500 0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
2501 0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
2502 : INLINE GEN
2503 0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
2504 0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
2505 : INLINE GEN
2506 0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
2507 0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
2508 : INLINE GEN
2509 22766 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
2510 22766 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
2511 : INLINE GEN
2512 0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
2513 0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
2514 : INLINE GEN
2515 0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
2516 0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
2517 :
2518 : /*FqXn*/
2519 : INLINE GEN
2520 6590 : FqXn_expint(GEN x, long n, GEN T, GEN p)
2521 6590 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
2522 : INLINE GEN
2523 0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
2524 0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
2525 : INLINE GEN
2526 7126 : FqXn_inv(GEN x, long n, GEN T, GEN p)
2527 7126 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
2528 : INLINE GEN
2529 263403 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
2530 263403 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
2531 : INLINE GEN
2532 0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
2533 0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
2534 :
2535 : /*FpXQ*/
2536 : INLINE GEN
2537 0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
2538 0 : { (void)T; return FpX_add(x,y,p); }
2539 : INLINE GEN
2540 0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
2541 0 : { (void)T; return FpX_sub(x,y,p); }
2542 :
2543 : /*Flxq*/
2544 : INLINE GEN
2545 0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
2546 0 : { (void)T; return Flx_add(x,y,p); }
2547 : INLINE GEN
2548 0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
2549 0 : { (void)T; return Flx_sub(x,y,p); }
2550 :
2551 : /* F2x */
2552 :
2553 : INLINE ulong
2554 667873403 : F2x_coeff(GEN x,long v)
2555 : {
2556 667873403 : ulong u=(ulong)x[2+divsBIL(v)];
2557 667873201 : return (u>>remsBIL(v))&1UL;
2558 : }
2559 :
2560 : INLINE void
2561 12353689 : F2x_clear(GEN x,long v)
2562 : {
2563 12353689 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2564 12353637 : *u&=~(1UL<<remsBIL(v));
2565 12353625 : }
2566 :
2567 : INLINE void
2568 124730608 : F2x_set(GEN x,long v)
2569 : {
2570 124730608 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2571 124725889 : *u|=1UL<<remsBIL(v);
2572 124727889 : }
2573 :
2574 : INLINE void
2575 1554050 : F2x_flip(GEN x,long v)
2576 : {
2577 1554050 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2578 1554050 : *u^=1UL<<remsBIL(v);
2579 1554050 : }
2580 :
2581 : /* F2v */
2582 :
2583 : INLINE ulong
2584 661319406 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
2585 :
2586 : INLINE void
2587 12353690 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
2588 :
2589 : INLINE void
2590 101403174 : F2v_set(GEN x,long v) { F2x_set(x,v-1); }
2591 :
2592 : INLINE void
2593 1554050 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
2594 :
2595 : /* F2m */
2596 :
2597 : INLINE ulong
2598 8038299 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
2599 :
2600 : INLINE void
2601 0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
2602 :
2603 : INLINE void
2604 798 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
2605 :
2606 : INLINE void
2607 1554050 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
2608 :
2609 : /* F3m */
2610 :
2611 : INLINE ulong
2612 3255921 : F3m_coeff(GEN x, long a, long b) { return F3v_coeff(gel(x,b), a); }
2613 :
2614 : INLINE void
2615 0 : F3m_set(GEN x, long a, long b, ulong c) { F3v_set(gel(x,b), a, c); }
2616 :
2617 : /* ARITHMETIC */
2618 : INLINE GEN
2619 3983 : matpascal(long n) { return matqpascal(n, NULL); }
2620 : INLINE long
2621 458664 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
2622 : INLINE long
2623 98 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
2624 : INLINE GEN
2625 6664730 : sqrti(GEN x) { return sqrtremi(x,NULL); }
2626 : INLINE GEN
2627 123248599 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
2628 : INLINE int
2629 761926 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
2630 : INLINE int
2631 29176 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
2632 : INLINE GEN
2633 0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
2634 : INLINE GEN
2635 0 : gminsg(long s, GEN y) { return gmings(y,s); }
2636 : INLINE GEN
2637 22579045 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
2638 : INLINE GEN
2639 282059385 : gmulgu(GEN y, ulong s) { return gmulug(s,y); }
2640 : INLINE GEN
2641 1724820 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
2642 : INLINE GEN
2643 531591 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
2644 :
2645 : INLINE GEN
2646 13510199 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
2647 : INLINE GEN
2648 877691 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
2649 :
2650 : /* x t_COMPLEX */
2651 : INLINE GEN
2652 91199150 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
2653 : /* q t_QUAD */
2654 : INLINE GEN
2655 69881 : quadnorm(GEN q)
2656 : {
2657 69881 : GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
2658 69881 : GEN z, u = gel(q,3), v = gel(q,2);
2659 69881 : if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
2660 : {
2661 69671 : z = signe(b)? mulii(v, addii(u,v)): sqri(v);
2662 69671 : return addii(z, mulii(c, sqri(u)));
2663 : }
2664 : else
2665 : {
2666 210 : z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
2667 210 : return gadd(z, gmul(c, gsqr(u)));
2668 : }
2669 : }
2670 : /* x a t_QUAD, return the attached discriminant */
2671 : INLINE GEN
2672 1085 : quad_disc(GEN x)
2673 : {
2674 1085 : GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
2675 1085 : if (is_pm1(b)) return subsi(1, c4);
2676 154 : togglesign_safe(&c4); return c4;
2677 : }
2678 : INLINE GEN
2679 4994803 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
2680 : INLINE GEN
2681 18501208 : qfb_disc(GEN x) { return gel(x,4); }
2682 :
2683 : INLINE GEN
2684 4148186 : sqrfrac(GEN x)
2685 : {
2686 4148186 : GEN z = cgetg(3,t_FRAC);
2687 4148185 : gel(z,1) = sqri(gel(x,1));
2688 4148167 : gel(z,2) = sqri(gel(x,2)); return z;
2689 : }
2690 :
2691 : INLINE void
2692 17204728 : normalize_frac(GEN z) {
2693 17204728 : if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
2694 17204728 : }
2695 :
2696 : INLINE GEN
2697 43119928 : powii(GEN x, GEN n)
2698 : {
2699 43119928 : long ln = lgefint(n);
2700 43119928 : if (ln == 3) {
2701 : GEN z;
2702 43078310 : if (signe(n) > 0) return powiu(x, n[2]);
2703 75050 : z = cgetg(3, t_FRAC);
2704 75064 : gel(z,1) = gen_1;
2705 75064 : gel(z,2) = powiu(x, n[2]);
2706 75063 : return z;
2707 : }
2708 41620 : if (ln == 2) return gen_1; /* rare */
2709 : /* should never happen */
2710 0 : return powgi(x, n); /* overflow unless x = 0, 1, -1 */
2711 : }
2712 : INLINE GEN
2713 1561 : powIs(long n) {
2714 1561 : switch(n & 3)
2715 : {
2716 63 : case 1: return mkcomplex(gen_0,gen_1);
2717 385 : case 2: return gen_m1;
2718 707 : case 3: return mkcomplex(gen_0,gen_m1);
2719 : }
2720 406 : return gen_1;
2721 : }
2722 :
2723 : /*******************************************************************/
2724 : /* */
2725 : /* ELLIPTIC CURVES */
2726 : /* */
2727 : /*******************************************************************/
2728 8997074 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
2729 7106180 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
2730 8814042 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
2731 8536720 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
2732 10370959 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
2733 7371024 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
2734 2550216 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
2735 3863115 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
2736 3297414 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
2737 11018140 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
2738 13474543 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
2739 16815348 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
2740 2126227 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
2741 12499483 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
2742 2036850 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
2743 800023 : INLINE GEN ellff_get_a4a6(GEN x) { return gmael(x, 15, 2); }
2744 1463 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
2745 308 : INLINE long ellQp_get_prec(GEN E) { return valp(ellQp_get_zero(E)); }
2746 1134 : INLINE GEN ellQp_get_p(GEN E) { return padic_p(ellQp_get_zero(E)); }
2747 232750 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
2748 371535 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
2749 2027621 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
2750 126 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
2751 :
2752 5593426 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
2753 52358314 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
2754 1869311 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
2755 :
2756 : /*******************************************************************/
2757 : /* */
2758 : /* ALGEBRAIC NUMBER THEORY */
2759 : /* */
2760 : /*******************************************************************/
2761 31238300 : INLINE GEN modpr_get_pr(GEN x) { return gel(x,3); }
2762 1888404 : INLINE GEN modpr_get_p(GEN x) { return pr_get_p(modpr_get_pr(x)); }
2763 13867668 : INLINE GEN modpr_get_T(GEN x) { return lg(x) == 4? NULL: gel(x,4); }
2764 :
2765 92476593 : INLINE GEN pr_get_p(GEN pr) { return gel(pr,1); }
2766 12067644 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
2767 : /* .[2] instead of itos works: e and f are small positive integers */
2768 16101907 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
2769 26342593 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
2770 49564917 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
2771 : INLINE int
2772 17146428 : pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
2773 : INLINE GEN
2774 1089811 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
2775 : INLINE ulong
2776 346135 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
2777 :
2778 : /* assume nf a genuine nf */
2779 : INLINE long
2780 1503727 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
2781 : INLINE GEN
2782 98075948 : nf_get_pol(GEN nf) { return gel(nf,1); }
2783 : INLINE long
2784 71365505 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
2785 : INLINE long
2786 23027121 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
2787 : INLINE long
2788 4154 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
2789 : INLINE GEN
2790 463495 : nf_get_disc(GEN nf) { return gel(nf,3); }
2791 : INLINE GEN
2792 3711579 : nf_get_index(GEN nf) { return gel(nf,4); }
2793 : INLINE GEN
2794 21306653 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
2795 : INLINE GEN
2796 303812 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
2797 : INLINE GEN
2798 1747349 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
2799 : INLINE GEN
2800 22082 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
2801 : INLINE GEN
2802 4445 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
2803 : INLINE GEN
2804 64643 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
2805 : INLINE GEN
2806 1627879 : nf_get_roots(GEN nf) { return gel(nf,6); }
2807 : INLINE GEN
2808 4382 : nf_get_zk(GEN nf)
2809 : {
2810 4382 : GEN y = gel(nf,7), D = gel(y, 1);
2811 4382 : if (typ(D) == t_POL) D = gel(D, 2);
2812 4382 : if (!equali1(D)) y = gdiv(y, D);
2813 4382 : return y;
2814 : }
2815 : INLINE GEN
2816 5417808 : nf_get_zkprimpart(GEN nf)
2817 : {
2818 5417808 : GEN y = gel(nf,7);
2819 : /* test for old format of nf.zk: non normalized */
2820 5417808 : if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
2821 5417807 : return y;
2822 : }
2823 : INLINE GEN
2824 5463527 : nf_get_zkden(GEN nf)
2825 : {
2826 5463527 : GEN y = gel(nf,7), D = gel(y,1);
2827 5463527 : if (typ(D) == t_POL) D = gel(D,2);
2828 : /* test for old format of nf.zk: non normalized */
2829 5463527 : if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
2830 5463512 : return D;
2831 : }
2832 : INLINE GEN
2833 9767700 : nf_get_invzk(GEN nf) { return gel(nf,8); }
2834 : INLINE void
2835 519552 : nf_get_sign(GEN nf, long *r1, long *r2)
2836 : {
2837 519552 : GEN x = gel(nf,2);
2838 519552 : *r1 = itou(gel(x,1));
2839 519552 : *r2 = itou(gel(x,2));
2840 519552 : }
2841 :
2842 : INLINE GEN
2843 2757741 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
2844 : INLINE GEN
2845 354929 : abgrp_get_no(GEN x) { return gel(x,1); }
2846 : INLINE GEN
2847 12231185 : abgrp_get_cyc(GEN x) { return gel(x,2); }
2848 : INLINE GEN
2849 1765021 : abgrp_get_gen(GEN x) { return gel(x,3); }
2850 : INLINE GEN
2851 9864723 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
2852 : INLINE GEN
2853 5375411 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
2854 : INLINE GEN
2855 8365 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
2856 : INLINE GEN
2857 3686563 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
2858 : INLINE GEN
2859 1680312 : bnf_get_gen(GEN bnf) { return abgrp_get_gen(bnf_get_clgp(bnf)); }
2860 : INLINE GEN
2861 648 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
2862 : INLINE GEN
2863 2874759 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
2864 : INLINE GEN
2865 1367775 : bnf_get_sunits(GEN bnf)
2866 1367775 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
2867 : INLINE GEN
2868 303572 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
2869 : INLINE long
2870 282996 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
2871 : INLINE GEN
2872 289086 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
2873 : INLINE GEN
2874 26300 : nfV_to_scalar_or_alg(GEN nf, GEN x)
2875 63441 : { pari_APPLY_same(nf_to_scalar_or_alg(nf, gel(x,i))) }
2876 : INLINE GEN
2877 25859 : bnf_get_fu(GEN bnf) {
2878 25859 : GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
2879 25859 : if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
2880 25859 : return nfV_to_scalar_or_alg(nf, vecslice(fu, 2, lg(fu)-1));
2881 : }
2882 :
2883 : INLINE GEN
2884 3751369 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
2885 : INLINE GEN
2886 2046087 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
2887 : INLINE GEN
2888 77119 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
2889 : INLINE GEN
2890 1368097 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
2891 : INLINE GEN
2892 2830224 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
2893 : INLINE GEN
2894 277488 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
2895 : INLINE GEN
2896 2522566 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
2897 : INLINE GEN
2898 70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
2899 : INLINE GEN
2900 7847 : bnr_get_gen(GEN bnr) {
2901 7847 : GEN G = bnr_get_clgp(bnr);
2902 7847 : if (lg(G) != 4)
2903 0 : pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
2904 7847 : return gel(G,3);
2905 : }
2906 :
2907 : /* localstar, used in gchar */
2908 : INLINE GEN
2909 52136 : locs_get_cyc(GEN locs) { return gel(locs,1); }
2910 : INLINE GEN
2911 193935 : locs_get_Lsprk(GEN locs) { return gel(locs,2); }
2912 : INLINE GEN
2913 1288 : locs_get_Lgenfil(GEN locs) { return gel(locs,3); }
2914 : INLINE GEN
2915 8225 : locs_get_mod(GEN locs) { return gel(locs,4); }
2916 : /* pr dividing the modulus N of locs, 0 <= i < v_pr(N)
2917 : * return a t_MAT whose columns are the logs
2918 : * of generators of U_i(pr)/U_{i+1}(pr). */
2919 : INLINE GEN
2920 1288 : locs_get_famod(GEN locs) { return gmael(locs,4,1); }
2921 : INLINE GEN
2922 209475 : locs_get_m_infty(GEN locs) { return gmael(locs,4,2); }
2923 :
2924 : /* G a grossenchar group */
2925 : INLINE GEN
2926 32816 : gchar_get_basis(GEN gc) { return gel(gc, 1); }
2927 : INLINE GEN
2928 205373 : gchar_get_bnf(GEN gc) { return gel(gc, 2); }
2929 : INLINE GEN
2930 243946 : gchar_get_nf(GEN gc) { return gel(gc, 3); }
2931 : INLINE GEN
2932 281911 : gchar_get_zm(GEN gc) { return gel(gc, 4); }
2933 : INLINE GEN
2934 8225 : gchar_get_mod(GEN gc) { return locs_get_mod(gchar_get_zm(gc)); }
2935 : INLINE GEN
2936 4095 : gchar_get_modP(GEN gc) { return gmael(gchar_get_mod(gc),1,1); }
2937 : INLINE GEN
2938 36995 : gchar_get_S(GEN gc) { return gel(gc, 5); }
2939 : INLINE GEN
2940 190673 : gchar_get_DLdata(GEN gc) { return gel(gc, 6); }
2941 : INLINE GEN
2942 1743 : gchar_get_sfu(GEN gc) { return gel(gc, 7); }
2943 : INLINE GEN
2944 16177 : gchar_get_cyc(GEN gc) { return gel(gc, 9); }
2945 : INLINE GEN
2946 0 : gchar_get_hnf(GEN gc) { return gmael(gc, 10, 1); }
2947 : INLINE GEN
2948 0 : gchar_get_U(GEN gc) { return gmael(gc, 10, 2); }
2949 : INLINE GEN
2950 10983 : gchar_get_Ui(GEN gc) { return gmael(gc, 10, 3); }
2951 : INLINE GEN
2952 3318 : gchar_get_m0(GEN gc) { return gel(gc, 11); }
2953 : INLINE GEN
2954 4252 : gchar_get_u0(GEN gc) { return gel(gc, 12); }
2955 : INLINE long
2956 17444 : gchar_get_r1(GEN gc) { return nf_get_r1(gchar_get_nf(gc)); }
2957 : INLINE long
2958 623 : gchar_get_r2(GEN gc) { return nf_get_r2(gchar_get_nf(gc)); }
2959 : INLINE GEN
2960 50519 : gchar_get_loccyc(GEN gc) { return locs_get_cyc(gchar_get_zm(gc)); }
2961 : INLINE long
2962 37261 : gchar_get_nc(GEN gc) { return lg(gchar_get_loccyc(gc))-1; }
2963 : INLINE long
2964 36995 : gchar_get_ns(GEN gc) { return lg(gchar_get_S(gc))-1; }
2965 : INLINE long
2966 1869 : gchar_get_nm(GEN gc) { return lg(gchar_get_basis(gc))-1; }
2967 : INLINE long
2968 4011 : gchar_get_evalprec(GEN gc) { return gmael(gc, 8, 1)[1]; }
2969 : INLINE long
2970 18795 : gchar_get_prec(GEN gc) { return gmael(gc, 8, 1)[2]; }
2971 : INLINE long
2972 13468 : gchar_get_nfprec(GEN gc) { return gmael(gc, 8, 1)[3]; }
2973 : INLINE void
2974 1813 : gchar_set_evalprec(GEN gc, long prec) { gmael(gc, 8, 1)[1] = prec; }
2975 : INLINE void
2976 1225 : gchar_set_prec(GEN gc, long prec) { gmael(gc, 8, 1)[2] = prec; }
2977 : INLINE void
2978 1225 : gchar_copy_precs(GEN gc, GEN gc2)
2979 : {
2980 1225 : gel(gc2, 8) = shallowcopy(gel(gc,8));
2981 1225 : gmael(gc2, 8, 1) = shallowcopy(gmael(gc, 8, 1));
2982 1225 : }
2983 : INLINE void
2984 2968 : gchar_set_nfprec(GEN gc, long prec) { gmael(gc, 8, 1)[3] = prec; }
2985 : INLINE long
2986 322 : gchar_get_ntors(GEN gc) { return gmael(gc, 8, 2)[1]; }
2987 : INLINE long
2988 322 : gchar_get_nfree(GEN gc) { return gmael(gc, 8, 2)[2]; }
2989 : INLINE long
2990 2044 : gchar_get_nalg(GEN gc) { return gmael(gc, 8, 2)[3]; }
2991 : INLINE void
2992 1575 : gchar_set_basis(GEN gc, GEN m_inv) { gel(gc, 1) = m_inv; }
2993 : INLINE void
2994 2110 : gchar_set_nf(GEN gc, GEN nf) { gel(gc, 3) = nf; }
2995 : INLINE void
2996 630 : gchar_set_ntors(GEN gc, long n) { gmael(gc, 8, 2)[1] = n; }
2997 : INLINE void
2998 630 : gchar_set_nfree(GEN gc, long n) { gmael(gc, 8, 2)[2] = n; }
2999 : INLINE void
3000 630 : gchar_set_nalg(GEN gc, long n) { gmael(gc, 8, 2)[3] = n; }
3001 : INLINE void
3002 1575 : gchar_set_cyc(GEN gc, GEN cyc) { gel(gc, 9) = cyc; }
3003 : INLINE void
3004 630 : gchar_set_HUUi(GEN gc, GEN hnf, GEN U, GEN Ui) { gel(gc, 10) = mkvec3(hnf, U, Ui); }
3005 : INLINE void
3006 945 : gchar_set_m0(GEN gc, GEN m0) { gel(gc, 11) = m0; }
3007 : INLINE void
3008 2121 : gchar_set_u0(GEN gc, GEN u0) { gel(gc, 12) = u0; }
3009 :
3010 : INLINE GEN
3011 1521922 : bid_get_mod(GEN bid) { return gel(bid,1); }
3012 : INLINE GEN
3013 72842 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
3014 : INLINE GEN
3015 41187 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
3016 : INLINE GEN
3017 5804652 : bid_get_grp(GEN bid) { return gel(bid,2); }
3018 : INLINE GEN
3019 2498932 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
3020 : INLINE GEN
3021 2201571 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
3022 : INLINE GEN
3023 2157249 : bid_get_MOD(GEN bid) { GEN y = gel(bid,4); return lg(y) == 4? gel(y,3): NULL; }
3024 : INLINE GEN
3025 2157973 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
3026 : INLINE GEN
3027 48594 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
3028 : INLINE GEN
3029 2397108 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
3030 : INLINE GEN
3031 3680227 : bid_get_U(GEN bid) { return gel(bid,5); }
3032 : INLINE GEN
3033 0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
3034 : INLINE GEN
3035 5739801 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
3036 : INLINE GEN
3037 0 : bid_get_gen_nocheck(GEN bid) { return abgrp_get_gen(bid_get_grp(bid)); }
3038 : INLINE GEN
3039 64274 : bid_get_gen(GEN bid) {
3040 64274 : GEN G = bid_get_grp(bid);
3041 64274 : if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
3042 64274 : return abgrp_get_gen(G);
3043 : }
3044 :
3045 : INLINE GEN
3046 38655073 : znstar_get_N(GEN G) { return gmael(G,1,1); }
3047 : INLINE GEN
3048 7331711 : znstar_get_faN(GEN G) { return gel(G,3); }
3049 : INLINE GEN
3050 14 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
3051 : INLINE GEN
3052 262088 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
3053 : INLINE GEN
3054 16150 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
3055 : INLINE GEN
3056 7979693 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
3057 : INLINE GEN
3058 3290176 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
3059 : INLINE GEN
3060 46788 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
3061 : INLINE GEN
3062 176107 : znstar_get_U(GEN G) { return gel(G,5); }
3063 : INLINE GEN
3064 2869063 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
3065 : INLINE GEN
3066 36316 : gal_get_pol(GEN gal) { return gel(gal,1); }
3067 : INLINE GEN
3068 6006 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
3069 : INLINE GEN
3070 91 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
3071 : INLINE GEN
3072 23758 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
3073 : INLINE GEN
3074 32732 : gal_get_roots(GEN gal) { return gel(gal,3); }
3075 : INLINE GEN
3076 27874 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
3077 : INLINE GEN
3078 27867 : gal_get_den(GEN gal) { return gel(gal,5); }
3079 : INLINE GEN
3080 80387 : gal_get_group(GEN gal) { return gel(gal,6); }
3081 : INLINE GEN
3082 15435 : gal_get_gen(GEN gal) { return gel(gal,7); }
3083 : INLINE GEN
3084 6678 : gal_get_orders(GEN gal) { return gel(gal,8); }
3085 :
3086 : /* assume rnf a genuine rnf */
3087 : INLINE long
3088 2506335 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
3089 : INLINE long
3090 21455 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
3091 : INLINE long
3092 1392746 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
3093 : INLINE GEN
3094 1386 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
3095 : INLINE GEN
3096 1225 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
3097 : INLINE GEN
3098 1428 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
3099 : INLINE GEN
3100 786086 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
3101 : INLINE GEN
3102 7245 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
3103 : INLINE GEN
3104 327628 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
3105 : INLINE GEN
3106 2845192 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
3107 : INLINE GEN
3108 357 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
3109 : INLINE GEN
3110 105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
3111 : INLINE GEN
3112 2849 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
3113 : INLINE long
3114 574 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
3115 : INLINE GEN
3116 255354 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
3117 : INLINE long
3118 4886 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
3119 : INLINE GEN
3120 4830 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
3121 : INLINE GEN
3122 135659 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
3123 : INLINE GEN
3124 1225 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
3125 :
3126 : INLINE GEN
3127 138665 : idealred(GEN nf, GEN id) { return idealred0(nf, id, NULL); }
3128 :
3129 : INLINE GEN
3130 5427 : idealchineseinit(GEN nf, GEN x)
3131 5427 : { return idealchinese(nf,x,NULL); }
3132 :
3133 : /*******************************************************************/
3134 : /* */
3135 : /* CLOSURES */
3136 : /* */
3137 : /*******************************************************************/
3138 371278481 : INLINE long closure_arity(GEN C) { return ((ulong)C[1])&ARITYBITS; }
3139 44789634 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
3140 321405372 : INLINE const char *closure_codestr(GEN C) { return GSTR(gel(C,2))-1; }
3141 0 : INLINE GEN closure_get_code(GEN C) { return gel(C,2); }
3142 321392533 : INLINE GEN closure_get_oper(GEN C) { return gel(C,3); }
3143 321375457 : INLINE GEN closure_get_data(GEN C) { return gel(C,4); }
3144 13621 : INLINE GEN closure_get_dbg(GEN C) { return gel(C,5); }
3145 37051 : INLINE GEN closure_get_text(GEN C) { return gel(C,6); }
3146 14158625 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
3147 :
3148 : /*******************************************************************/
3149 : /* */
3150 : /* ERRORS */
3151 : /* */
3152 : /*******************************************************************/
3153 : INLINE long
3154 62523 : err_get_num(GEN e) { return e[1]; }
3155 : INLINE GEN
3156 294 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
3157 :
3158 : INLINE void
3159 14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
3160 : INLINE void
3161 21 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
3162 : INLINE void
3163 84 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
3164 : INLINE void
3165 718 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
3166 : INLINE void
3167 0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
3168 : INLINE void
3169 36 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
3170 : INLINE void
3171 98 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
3172 : INLINE void
3173 539 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
3174 : INLINE void
3175 20018 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
3176 : INLINE void
3177 63 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
3178 : INLINE void
3179 2862 : 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); }
3180 : INLINE void
3181 214 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
3182 : INLINE void
3183 0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
3184 : INLINE void
3185 406 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
3186 : INLINE void
3187 156 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
3188 : INLINE void
3189 238 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
3190 : INLINE void
3191 0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
3192 : INLINE void
3193 98 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
3194 : INLINE void
3195 1316 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
3196 : INLINE void
3197 63 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
3198 : INLINE void
3199 7084 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
3200 : INLINE void
3201 15370 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
3202 : INLINE void
3203 3493 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
3204 : INLINE void
3205 371 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
3206 : INLINE void
3207 238 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
3208 238 : { pari_err(e_PRIORITY, f,x,op,v); }
3209 :
|