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 8073798 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
188 : INLINE GEN
189 1834 : mkintmodu(ulong x, ulong y) {
190 1834 : GEN v = cgetg(3,t_INTMOD);
191 1834 : gel(v,1) = utoipos(y);
192 1834 : 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 3297286 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
198 : INLINE GEN
199 54523996 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
200 : INLINE GEN
201 1209605 : 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 35364 : Qtoss(GEN q, long *n, long *d)
205 : {
206 35364 : if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
207 5824 : else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
208 35364 : }
209 : INLINE GEN
210 1097313 : sstoQ(long n, long d)
211 : {
212 : ulong r;
213 : long g, q;
214 1097313 : if (!n)
215 : {
216 117971 : if (!d) pari_err_INV("sstoQ",gen_0);
217 117971 : return gen_0;
218 : }
219 979342 : if (d < 0) { d = -d; n = -n; }
220 979342 : if (d == 1) return stoi(n);
221 960008 : r = labs(n);
222 960008 : if (r == 1) retmkfrac(n > 0? gen_1: gen_m1, utoipos(d));
223 909839 : q = udivuu_rem(r, d, &r);
224 909839 : if (!r) return n > 0? utoipos(q): utoineg(q);
225 854966 : g = ugcd(d,r); /* gcd(n,d) */
226 854966 : if (g != 1) { n /= g; d /= g; }
227 854966 : retmkfrac(stoi(n), utoipos(d));
228 : }
229 :
230 : INLINE GEN
231 1602256 : uutoQ(ulong n, ulong d)
232 : {
233 : ulong r;
234 : long g, q;
235 1602256 : if (!n)
236 : {
237 595 : if (!d) pari_err_INV("uutoQ",gen_0);
238 595 : return gen_0;
239 : }
240 1601661 : if (d == 1) return utoipos(n);
241 1590559 : if (n == 1) retmkfrac(gen_1, utoipos(d));
242 1501421 : q = udivuu_rem(n,d,&r);
243 1501421 : if (!r) return utoipos(q);
244 1259669 : g = ugcd(d,r); /* gcd(n,d) */
245 1259669 : if (g != 1) { n /= g; d /= g; }
246 1259669 : retmkfrac(utoipos(n), utoipos(d));
247 : }
248 :
249 : INLINE GEN
250 5217043 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
251 : INLINE GEN
252 698726 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
253 698726 : 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 93000202 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
259 : INLINE GEN
260 1269362 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
261 : INLINE GEN
262 976273 : 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 143634804 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
269 : INLINE GEN
270 262033119 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
271 262033119 : v[1]=x; v[2]=y; return v; }
272 : INLINE GEN
273 106870713 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
274 106870713 : v[1]=x; v[2]=y; v[3]=z; return v; }
275 : INLINE GEN
276 20242460 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
277 20242460 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
278 : INLINE GEN
279 1898422 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
280 1898422 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
281 :
282 : INLINE GEN
283 28268765 : mkqfb(GEN x, GEN y, GEN z, GEN d) { retmkqfb(x,y,z,d); }
284 : /* vec */
285 : INLINE GEN
286 70142866 : mkvec(GEN x) { retmkvec(x); }
287 : INLINE GEN
288 221526753 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
289 : INLINE GEN
290 125744807 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
291 : INLINE GEN
292 10533231 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
293 : INLINE GEN
294 15190541 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
295 : INLINE GEN
296 42 : 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 221667 : 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 122528 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
311 : INLINE GEN
312 114572 : mkvec2copy(GEN x, GEN y) {
313 114572 : GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
314 : /* col */
315 : INLINE GEN
316 10667571 : mkcol(GEN x) { retmkcol(x); }
317 : INLINE GEN
318 168879889 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
319 : INLINE GEN
320 88320 : 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 232582 : 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 33559 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
337 : /* mat */
338 : INLINE GEN
339 4783134 : mkmat(GEN x) { retmkmat(x); }
340 : INLINE GEN
341 33669609 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
342 : INLINE GEN
343 17003 : 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 122332 : 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 1333341 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
354 : INLINE GEN
355 108951 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
356 : INLINE long
357 1454514 : 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 54534335 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
362 :
363 : /* pol */
364 : INLINE GEN
365 3887046 : pol_x(long v) {
366 3887046 : GEN p = cgetg(4, t_POL);
367 3887046 : p[1] = evalsigne(1)|evalvarn(v);
368 3887046 : gel(p,2) = gen_0;
369 3887046 : gel(p,3) = gen_1; return p;
370 : }
371 : /* x^n, assume n >= 0 */
372 : INLINE GEN
373 2237774 : pol_xn(long n, long v) {
374 2237774 : long i, a = n+2;
375 2237774 : GEN p = cgetg(a+1, t_POL);
376 2237774 : p[1] = evalsigne(1)|evalvarn(v);
377 5438757 : for (i = 2; i < a; i++) gel(p,i) = gen_0;
378 2237774 : 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 3762740 : pol_1(long v) {
398 3762740 : GEN p = cgetg(3, t_POL);
399 3762740 : p[1] = evalsigne(1)|evalvarn(v);
400 3762740 : gel(p,2) = gen_1; return p;
401 : }
402 : INLINE GEN
403 84220470 : pol_0(long v)
404 : {
405 84220470 : GEN x = cgetg(2,t_POL);
406 84220470 : 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 245697781 : 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 39635995 : const_col(long n, GEN x) { retconst_col(n, x); }
422 : INLINE GEN
423 18940831 : const_vecsmall(long n, long c)
424 : {
425 : long i;
426 18940831 : GEN V = cgetg(n+1,t_VECSMALL);
427 900951698 : for(i=1;i<=n;i++) V[i] = c;
428 18940831 : return V;
429 : }
430 :
431 : /*** ZERO ***/
432 : /* O(p^e) */
433 : INLINE GEN
434 858086 : zeropadic(GEN p, long e) { retmkpadic(gen_0, icopy(p), gen_1, e, 0); }
435 : INLINE GEN
436 17150 : 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 139020 : zeroser(long v, long e)
440 : {
441 139020 : GEN x = cgetg(2, t_SER);
442 139020 : x[1] = evalvalser(e) | evalvarn(v); return x;
443 : }
444 : INLINE int
445 11871524 : ser_isexactzero(GEN x)
446 : {
447 11871524 : if (!signe(x)) switch(lg(x))
448 : {
449 132951 : case 2: return 1;
450 4872 : case 3: return isexactzero(gel(x,2));
451 : }
452 11733701 : return 0;
453 : }
454 : /* 0 * pol_x(v) */
455 : INLINE GEN
456 42429172 : zeropol(long v) { return pol_0(v); }
457 : /* vector(n) */
458 : INLINE GEN
459 125331874 : zerocol(long n)
460 : {
461 125331874 : GEN y = cgetg(n+1,t_COL);
462 867658872 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
463 125331874 : return y;
464 : }
465 : /* vectorv(n) */
466 : INLINE GEN
467 29356842 : zerovec(long n)
468 : {
469 29356842 : GEN y = cgetg(n+1,t_VEC);
470 423759420 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
471 29356842 : return y;
472 : }
473 : /* matrix(m, n) */
474 : INLINE GEN
475 108897 : zeromat(long m, long n)
476 : {
477 108897 : GEN y = cgetg(n+1,t_MAT);
478 108897 : GEN v = zerocol(m);
479 400920 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
480 108897 : return y;
481 : }
482 : /* = zero_zx, sv is a evalvarn()*/
483 : INLINE GEN
484 3256237 : zero_Flx(long sv) { return pol0_Flx(sv); }
485 : INLINE GEN
486 61813718 : zero_Flv(long n)
487 : {
488 61813718 : GEN y = cgetg(n+1,t_VECSMALL);
489 1028865764 : long i; for (i=1; i<=n; i++) y[i] = 0;
490 61813718 : return y;
491 : }
492 : /* matrix(m, n) */
493 : INLINE GEN
494 2854704 : zero_Flm(long m, long n)
495 : {
496 2854704 : GEN y = cgetg(n+1,t_MAT);
497 2854704 : GEN v = zero_Flv(m);
498 18258707 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
499 2854704 : return y;
500 : }
501 : /* matrix(m, n) */
502 : INLINE GEN
503 241025 : zero_Flm_copy(long m, long n)
504 : {
505 241025 : GEN y = cgetg(n+1,t_MAT);
506 2708092 : long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
507 241025 : return y;
508 : }
509 :
510 : INLINE GEN
511 3663639 : zero_F2v(long m)
512 : {
513 3663639 : long l = nbits2nlong(m);
514 3663639 : GEN v = zero_Flv(l+1);
515 3663639 : v[1] = m;
516 3663639 : 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 847272 : zero_F2m_copy(long m, long n)
533 : {
534 : long i;
535 847272 : GEN M = cgetg(n+1, t_MAT);
536 2025017 : for (i = 1; i <= n; i++)
537 1177745 : gel(M,i)= zero_F2v(m);
538 847272 : return M;
539 : }
540 :
541 : /* matrix(m, n) */
542 : INLINE GEN
543 14382904 : zeromatcopy(long m, long n)
544 : {
545 14382904 : GEN y = cgetg(n+1,t_MAT);
546 73081140 : long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
547 14382904 : return y;
548 : }
549 :
550 : INLINE GEN
551 29675 : zerovec_block(long len)
552 : {
553 : long i;
554 29675 : GEN blk = cgetg_block(len + 1, t_VEC);
555 979275 : for (i = 1; i <= len; ++i)
556 949600 : gel(blk, i) = gen_0;
557 29675 : return blk;
558 : }
559 :
560 : /* i-th vector in the standard basis */
561 : INLINE GEN
562 5929174 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
563 : INLINE GEN
564 1716809 : 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 585509 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
569 : INLINE GEN
570 1472525 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
571 :
572 : INLINE GEN
573 27956175 : shallowcopy(GEN x)
574 27956175 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
575 :
576 : /* routines for naive growarrays */
577 : INLINE GEN
578 11795767 : vectrunc_init(long l)
579 : {
580 11795767 : GEN z = new_chunk(l);
581 11795767 : z[0] = evaltyp(t_VEC) | _evallg(1); return z;
582 : }
583 : INLINE GEN
584 740376 : coltrunc_init(long l)
585 : {
586 740376 : GEN z = new_chunk(l);
587 740376 : z[0] = evaltyp(t_COL) | _evallg(1); return z;
588 : }
589 : INLINE void
590 469290444 : lg_increase(GEN x) { x[0]++; }
591 : INLINE void
592 15915273 : 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 253542645 : vecsmalltrunc_init(long l)
603 : {
604 253542645 : GEN z = new_chunk(l);
605 253542641 : z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
606 : }
607 : INLINE void
608 101231027 : 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 2787689 : hash_str(const char *str)
617 : {
618 2787689 : ulong hash = 5381UL, c;
619 29247051 : while ( (c = (ulong)*str++) )
620 26459362 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
621 2787689 : return hash;
622 : }
623 : INLINE ulong
624 31645685 : hash_str_len(const char *str, long len)
625 : {
626 31645685 : ulong hash = 5381UL;
627 : long i;
628 229436080 : for (i = 0; i < len; i++)
629 : {
630 197790395 : ulong c = (ulong)*str++;
631 197790395 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
632 : }
633 31645685 : return hash;
634 : }
635 :
636 : /*******************************************************************/
637 : /* */
638 : /* VEC / COL / VECSMALL */
639 : /* */
640 : /*******************************************************************/
641 : /* shallow*/
642 : INLINE GEN
643 11564 : vec_shorten(GEN v, long n)
644 : {
645 11564 : GEN V = cgetg(n+1, t_VEC);
646 : long i;
647 13027 : for(i = 1; i <= n; i++) gel(V,i) = gel(v,i);
648 11564 : return V;
649 : }
650 : /* shallow*/
651 : INLINE GEN
652 18983 : vec_lengthen(GEN v, long n)
653 : {
654 18983 : GEN V = cgetg(n+1, t_VEC);
655 18983 : long i, l = lg(v);
656 1941005 : for(i = 1; i < l; i++) gel(V,i) = gel(v,i);
657 18983 : return V;
658 : }
659 : /* shallow*/
660 : INLINE GEN
661 3470866 : vec_append(GEN V, GEN s)
662 : {
663 3470866 : long i, l2 = lg(V);
664 3470866 : GEN res = cgetg(l2+1, typ(V));
665 10827207 : for (i = 1; i < l2; i++) gel(res, i) = gel(V,i);
666 3470866 : gel(res,l2) = s; return res;
667 : }
668 : /* shallow*/
669 : INLINE GEN
670 313943 : vec_prepend(GEN v, GEN s)
671 : {
672 313943 : long i, l = lg(v);
673 313943 : GEN w = cgetg(l+1, typ(v));
674 313943 : gel(w,1) = s;
675 796282 : for (i = 2; i <= l; i++) gel(w,i) = gel(v,i-1);
676 313943 : 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 77080 : vecsmall_shorten(GEN v, long n)
688 : {
689 77080 : GEN V = cgetg(n+1,t_VECSMALL);
690 : long i;
691 1050501 : for(i = 1; i <= n; i++) V[i] = v[i];
692 77080 : 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 4149430 : vec_to_vecsmall(GEN x)
705 19366869 : { pari_APPLY_long(itos(gel(x,i))) }
706 : INLINE GEN
707 513109 : vecsmall_to_vec(GEN x)
708 7078698 : { 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 7993138 : vecsmall_to_col(GEN x)
718 60179727 : { pari_APPLY_type(t_COL, stoi(x[i])) }
719 :
720 : INLINE int
721 7211133 : vecsmall_lexcmp(GEN x, GEN y)
722 : {
723 : long lx,ly,l,i;
724 7211133 : lx = lg(x);
725 7211133 : ly = lg(y); l = minss(lx,ly);
726 30806200 : for (i=1; i<l; i++)
727 29323028 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
728 1483172 : 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 187025 : vecsmall_prepend(GEN V, long s)
744 : {
745 187025 : long i, l2 = lg(V);
746 187025 : GEN res = cgetg(l2+1, typ(V));
747 187025 : res[1] = s;
748 625432 : for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
749 187025 : return res;
750 : }
751 :
752 : INLINE GEN
753 4117808 : vecsmall_append(GEN V, long s)
754 : {
755 4117808 : long i, l2 = lg(V);
756 4117808 : GEN res = cgetg(l2+1, t_VECSMALL);
757 6045167 : for (i = 1; i < l2; ++i) res[i] = V[i];
758 4117808 : res[l2] = s; return res;
759 : }
760 :
761 : INLINE GEN
762 1466297 : vecsmall_concat(GEN u, GEN v)
763 : {
764 1466297 : long i, l1 = lg(u)-1, l2 = lg(v)-1;
765 1466297 : GEN res = cgetg(l1+l2+1, t_VECSMALL);
766 11639351 : for (i = 1; i <= l1; ++i) res[i] = u[i];
767 15285994 : for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
768 1466297 : 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 1177922 : vecsmall_max(GEN x)
810 : {
811 1177922 : long i, t = x[1], lx = lg(x);
812 3410885 : for (i=2; i<lx; i++)
813 2232963 : if (x[i] > t) t = x[i];
814 1177922 : 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 5509 : vecsmall_min(GEN x)
828 : {
829 5509 : long i, t = x[1], lx =lg(x);
830 38563 : for (i=2; i<lx; i++)
831 33054 : if (x[i] < t) t = x[i];
832 5509 : return t;
833 : }
834 :
835 : INLINE int
836 23697366 : ZV_isscalar(GEN x)
837 : {
838 23697366 : long l = lg(x);
839 55629655 : while (--l > 1)
840 55194773 : if (signe(gel(x, l))) return 0;
841 434882 : return 1;
842 : }
843 : INLINE int
844 54425466 : QV_isscalar(GEN x)
845 : {
846 54425466 : long lx = lg(x),i;
847 70433701 : for (i=2; i<lx; i++)
848 66474877 : if (!isintzero(gel(x, i))) return 0;
849 3958824 : return 1;
850 : }
851 : INLINE int
852 1211819 : RgV_isscalar(GEN x)
853 : {
854 1211819 : long lx = lg(x),i;
855 1238089 : for (i=2; i<lx; i++)
856 1234372 : if (!gequal0(gel(x, i))) return 0;
857 3717 : 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 27145972 : 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 68842 : RgX_is_rational(GEN x)
876 : {
877 : long i;
878 313844 : for (i = lg(x)-1; i > 1; i--)
879 297377 : if (!is_rational_t(typ(gel(x,i)))) return 0;
880 16467 : return 1;
881 : }
882 : INLINE int
883 22521844 : RgX_is_ZX(GEN x)
884 : {
885 : long i;
886 83301096 : for (i = lg(x)-1; i > 1; i--)
887 62560994 : if (typ(gel(x,i)) != t_INT) return 0;
888 20740102 : return 1;
889 : }
890 : INLINE int
891 400122 : RgX_is_QX(GEN x)
892 : {
893 400122 : long k = lg(x)-1;
894 1497223 : for ( ; k>1; k--)
895 1097395 : if (!is_rational_t(typ(gel(x,k)))) return 0;
896 399828 : return 1;
897 : }
898 : INLINE int
899 1346284 : RgX_is_monomial(GEN x)
900 : {
901 : long i;
902 1346284 : if (!signe(x)) return 0;
903 3394378 : for (i=lg(x)-2; i>1; i--)
904 2696226 : if (!isexactzero(gel(x,i))) return 0;
905 698152 : return 1;
906 : }
907 : INLINE int
908 32132606 : RgV_is_ZV(GEN x)
909 : {
910 : long i;
911 127590189 : for (i = lg(x)-1; i > 0; i--)
912 95490406 : if (typ(gel(x,i)) != t_INT) return 0;
913 32099783 : return 1;
914 : }
915 : INLINE int
916 142058 : RgV_is_QV(GEN x)
917 : {
918 : long i;
919 629496 : for (i = lg(x)-1; i > 0; i--)
920 490392 : if (!is_rational_t(typ(gel(x,i)))) return 0;
921 139104 : return 1;
922 : }
923 : INLINE long
924 111958 : RgV_isin_i(GEN v, GEN x, long n)
925 : {
926 : long i;
927 1035293 : for (i = 1; i <= n; i++)
928 1028531 : if (gequal(gel(v,i), x)) return i;
929 6762 : return 0;
930 : }
931 : INLINE long
932 111958 : 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 988180126 : pari_stack_base(pari_stack *s) { return s->data; }
941 :
942 : INLINE void
943 6446225 : pari_stack_init(pari_stack *s, size_t size, void **data)
944 : {
945 6446225 : s->data = data;
946 6446225 : *data = NULL;
947 6446225 : s->n = 0;
948 6446225 : s->alloc = 0;
949 6446225 : s->size = size;
950 6446225 : }
951 :
952 : INLINE void
953 982110628 : pari_stack_alloc(pari_stack *s, long nb)
954 : {
955 982110628 : void **sdat = pari_stack_base(s);
956 982110628 : long alloc = s->alloc;
957 982110628 : if (s->n+nb <= alloc) return;
958 2377833 : if (!alloc)
959 2278050 : alloc = nb;
960 : else
961 : {
962 202866 : while (s->n+nb > alloc) alloc <<= 1;
963 : }
964 2377833 : pari_realloc_ip(sdat,alloc*s->size);
965 2377833 : s->alloc = alloc;
966 : }
967 :
968 : INLINE long
969 886645232 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
970 :
971 : INLINE void
972 6063652 : pari_stack_delete(pari_stack *s)
973 : {
974 6063652 : void **sdat = pari_stack_base(s);
975 6063652 : if (*sdat) pari_free(*sdat);
976 6063652 : }
977 :
978 : INLINE void
979 5840 : pari_stack_pushp(pari_stack *s, void *u)
980 : {
981 5840 : long n = pari_stack_new(s);
982 5840 : void **sdat =(void**) *pari_stack_base(s);
983 5840 : sdat[n] = u;
984 5840 : }
985 :
986 : /*******************************************************************/
987 : /* */
988 : /* EXTRACT */
989 : /* */
990 : /*******************************************************************/
991 : INLINE GEN
992 670240293 : vecslice(GEN A, long y1, long y2)
993 : {
994 670240293 : long i,lB = y2 - y1 + 2;
995 670240293 : GEN B = cgetg(lB, typ(A));
996 2988724890 : for (i=1; i<lB; i++) B[i] = A[y1-1+i];
997 670240293 : return B;
998 : }
999 : INLINE GEN
1000 2880042 : vecslicepermute(GEN A, GEN p, long y1, long y2)
1001 : {
1002 2880042 : long i,lB = y2 - y1 + 2;
1003 2880042 : GEN B = cgetg(lB, typ(A));
1004 32691104 : for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
1005 2880042 : return B;
1006 : }
1007 : /* rowslice(rowpermute(A,p), x1, x2) */
1008 : INLINE GEN
1009 234018 : rowslicepermute(GEN x, GEN p, long j1, long j2)
1010 996954 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
1011 :
1012 : INLINE GEN
1013 80071659 : rowslice(GEN x, long j1, long j2)
1014 686077903 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
1015 :
1016 : INLINE GEN
1017 16931409 : matslice(GEN A, long x1, long x2, long y1, long y2)
1018 16931409 : { 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 385296 : vecsplice(GEN a, long j)
1028 : {
1029 385296 : long i, k, l = lg(a);
1030 : GEN b;
1031 385296 : if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
1032 385296 : b = cgetg(l-1, typ(a));
1033 1594679 : for (i = k = 1; i < l; i++)
1034 1209383 : if (i != j) gel(b, k++) = gel(a,i);
1035 385296 : 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 767844 : row(GEN x, long j)
1050 6928922 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
1051 : INLINE GEN
1052 8756277 : Flm_row(GEN x, long j)
1053 232787217 : { 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 697305 : vecreverse(GEN A)
1070 : {
1071 : long i, l;
1072 697305 : GEN B = cgetg_copy(A, &l);
1073 2408595 : for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
1074 697305 : 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 2612 : vecreverse_inplace(GEN y)
1088 : {
1089 2612 : long l = lg(y), lim = l>>1, i;
1090 8326 : for (i = 1; i <= lim; i++)
1091 : {
1092 5714 : GEN z = gel(y,i);
1093 5714 : gel(y,i) = gel(y,l-i);
1094 5714 : gel(y,l-i) = z;
1095 : }
1096 2612 : }
1097 :
1098 : INLINE GEN
1099 79342267 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
1100 :
1101 : INLINE GEN
1102 21130029 : vecpermute(GEN A, GEN x)
1103 118191065 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
1104 :
1105 : INLINE GEN
1106 9557830 : veclast(GEN A) { return gel(A, lg(A)-1); }
1107 :
1108 : INLINE GEN
1109 14794342 : rowpermute(GEN x, GEN p)
1110 103488226 : { 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 3284390 : identity_zv(long n)
1120 : {
1121 3284390 : GEN v = cgetg(n+1, t_VECSMALL);
1122 : long i;
1123 33351309 : for (i = 1; i <= n; i++) v[i] = i;
1124 3284390 : return v;
1125 : }
1126 : INLINE GEN
1127 8120 : identity_ZV(long n)
1128 : {
1129 8120 : GEN v = cgetg(n+1, t_VEC);
1130 : long i;
1131 169113 : for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
1132 8120 : return v;
1133 : }
1134 : /* identity permutation */
1135 : INLINE GEN
1136 3260027 : identity_perm(long n) { return identity_zv(n); }
1137 :
1138 : /* assume d <= n */
1139 : INLINE GEN
1140 99932 : cyclic_perm(long n, long d)
1141 : {
1142 99932 : GEN perm = cgetg(n+1, t_VECSMALL);
1143 : long i;
1144 520401 : for (i = 1; i <= n-d; i++) perm[i] = i+d;
1145 226723 : for ( ; i <= n; i++) perm[i] = i-n+d;
1146 99932 : return perm;
1147 : }
1148 :
1149 : /* Multiply (compose) two permutations */
1150 : INLINE GEN
1151 81655621 : perm_mul(GEN s, GEN x)
1152 1044551525 : { 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 3781183 : perm_inv(GEN x)
1161 : {
1162 : long i, lx;
1163 3781183 : GEN y = cgetg_copy(x, &lx);
1164 47184312 : for (i=1; i<lx; i++) y[ x[i] ] = i;
1165 3781183 : return y;
1166 : }
1167 : /* Return s*t*s^-1 */
1168 : INLINE GEN
1169 422429 : perm_conj(GEN s, GEN t)
1170 : {
1171 : long i, l;
1172 422429 : GEN v = cgetg_copy(s, &l);
1173 7109823 : for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
1174 422429 : return v;
1175 : }
1176 :
1177 : INLINE void
1178 494837388 : pari_free(void *pointer)
1179 : {
1180 494837388 : BLOCK_SIGINT_START;
1181 494837388 : free(pointer);
1182 494837388 : BLOCK_SIGINT_END;
1183 494837387 : }
1184 : INLINE void*
1185 730381216 : pari_malloc(size_t size)
1186 : {
1187 730381216 : if (size)
1188 : {
1189 : char *tmp;
1190 730381216 : BLOCK_SIGINT_START;
1191 730381216 : tmp = (char*)malloc(size);
1192 730381216 : BLOCK_SIGINT_END;
1193 730381216 : if (!tmp) pari_err(e_MEM);
1194 730381216 : return tmp;
1195 : }
1196 0 : return NULL;
1197 : }
1198 : INLINE void*
1199 1887 : pari_realloc(void *pointer, size_t size)
1200 : {
1201 : char *tmp;
1202 :
1203 1887 : BLOCK_SIGINT_START;
1204 1887 : if (!pointer) tmp = (char *) malloc(size);
1205 1887 : else tmp = (char *) realloc(pointer,size);
1206 1887 : BLOCK_SIGINT_END;
1207 1887 : if (!tmp) pari_err(e_MEM);
1208 1887 : return tmp;
1209 : }
1210 : INLINE void
1211 2385295 : pari_realloc_ip(void **pointer, size_t size)
1212 : {
1213 : char *tmp;
1214 2385295 : BLOCK_SIGINT_START;
1215 2385295 : if (!*pointer) tmp = (char *) malloc(size);
1216 107245 : else tmp = (char *) realloc(*pointer,size);
1217 2385295 : if (!tmp) pari_err(e_MEM);
1218 2385295 : *pointer = tmp;
1219 2385295 : BLOCK_SIGINT_END;
1220 2385295 : }
1221 :
1222 : INLINE void*
1223 48209 : pari_calloc(size_t size)
1224 : {
1225 48209 : void *t = pari_malloc(size);
1226 48209 : memset(t, 0, size); return t;
1227 : }
1228 : INLINE GEN
1229 10153 : cgetalloc(size_t l, long t)
1230 : { /* evallg may raise e_OVERFLOW, which would leak x */
1231 10153 : ulong x0 = evaltyp(t) | evallg(l);
1232 10153 : GEN x = (GEN)pari_malloc(l * sizeof(long));
1233 10153 : 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 8133002995 : icopy_avma(GEN x, pari_sp av)
1244 : {
1245 8133002995 : long i = lgefint(x), lx = i;
1246 8133002995 : GEN y = ((GEN)av) - i;
1247 59906413375 : while (--i > 0) y[i] = x[i];
1248 8133002995 : y[0] = evaltyp(t_INT)|evallg(lx);
1249 8133002995 : return y;
1250 : }
1251 : /* copy leaf x as if we had set_avma(av) */
1252 : INLINE GEN
1253 586384696 : leafcopy_avma(GEN x, pari_sp av)
1254 : {
1255 586384696 : long i = lg(x);
1256 586384696 : GEN y = ((GEN)av) - i;
1257 3184492356 : while (--i > 0) y[i] = x[i];
1258 586384696 : y[0] = x[0] & (~CLONEBIT);
1259 586384696 : return y;
1260 : }
1261 : INLINE GEN
1262 1208913210 : gc_leaf(pari_sp av, GEN x)
1263 : {
1264 : long lx;
1265 : GEN q;
1266 :
1267 1208913210 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1268 1207909113 : lx = lg(x);
1269 1207909113 : q = ((GEN)av) - lx;
1270 1207909113 : set_avma((pari_sp)q);
1271 11890742046 : while (--lx >= 0) q[lx] = x[lx];
1272 1207909113 : return q;
1273 : }
1274 : INLINE GEN
1275 4194538216 : gc_INT(pari_sp av, GEN x)
1276 : {
1277 4194538216 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1278 3739722857 : set_avma((pari_sp)icopy_avma(x, av));
1279 3739722856 : return (GEN)avma;
1280 : }
1281 : INLINE void
1282 11657609 : gc_INT_affii(pari_sp av, GEN x, GEN *y)
1283 : {
1284 11657609 : long l = lg(*y);
1285 11657609 : if (lgefint(x) <= l && isonstack(*y))
1286 : {
1287 9442002 : affii(x,*y);
1288 9442002 : set_avma(av);
1289 : }
1290 : else
1291 2215607 : *y = gc_INT(av, x);
1292 11657609 : }
1293 : INLINE GEN
1294 2017876410 : gc_upto(pari_sp av, GEN q)
1295 : {
1296 2017876410 : if (!isonstack(q) || (GEN)av<=q) return gc_const(av,q);
1297 1823628672 : switch(typ(q))
1298 : { /* first all non recursive types */
1299 533336558 : case t_INT: return gc_INT(av, q);
1300 391935216 : case t_REAL:
1301 : case t_STR:
1302 391935216 : case t_VECSMALL: return gc_leaf(av,q);
1303 898356898 : 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 238009025 : gc_GEN(pari_sp av, GEN x)
1310 : {
1311 238009025 : if (is_recursive_t(typ(x)))
1312 : {
1313 216903941 : GENbin *p = copy_bin(x);
1314 216903941 : set_avma(av); return bin_copy(p);
1315 : }
1316 : else
1317 : {
1318 21105084 : set_avma(av);
1319 21105084 : if (x < (GEN)av) {
1320 20887235 : if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
1321 20887235 : x = leafcopy_avma(x, av);
1322 20887235 : set_avma((pari_sp)x);
1323 : } else
1324 217849 : x = leafcopy(x);
1325 21105084 : return x;
1326 : }
1327 : }
1328 :
1329 : INLINE void
1330 48998102 : guncloneNULL(GEN x) { if (x) gunclone(x); }
1331 : INLINE void
1332 1555389 : guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
1333 :
1334 : /* assume 1 <= n < 10 */
1335 : INLINE GEN
1336 103408413 : gc_all(pari_sp av, int n, ...)
1337 : {
1338 : int i;
1339 103408413 : va_list a; va_start(a, n);
1340 103408413 : if (n < 10)
1341 : {
1342 : GEN *v[10];
1343 342801718 : for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
1344 103408413 : set_avma(av);
1345 342801718 : for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
1346 103408413 : 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 2217869 : gc_slice(pari_sp av, GEN x, int n)
1360 : {
1361 : int i;
1362 26390446 : for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
1363 2217869 : set_avma(av);
1364 26390446 : for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
1365 2217869 : }
1366 :
1367 : /* p from copy_bin. Copy p->x back to stack, then destroy p */
1368 : INLINE GEN
1369 480521109 : bin_copy(GENbin *p)
1370 : {
1371 : GEN x, y, base;
1372 : long dx, len;
1373 :
1374 480521109 : x = p->x; if (!x) { pari_free(p); return gen_0; }
1375 453486335 : len = p->len;
1376 453486335 : base= p->base; dx = x - base;
1377 453486335 : y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
1378 453486335 : y += dx;
1379 453486335 : p->rebase(y, ((ulong)y-(ulong)x));
1380 453486335 : pari_free(p); return y;
1381 : }
1382 :
1383 : INLINE GEN
1384 934007492 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
1385 :
1386 : INLINE void
1387 106109357 : cgiv(GEN x)
1388 : {
1389 106109357 : pari_sp av = (pari_sp)(x+lg(x));
1390 106109357 : if (isonstack((GEN)av)) set_avma(av);
1391 106109357 : }
1392 :
1393 : INLINE void
1394 1982426 : killblock(GEN x) { gunclone(x); }
1395 :
1396 : INLINE int
1397 334827327 : 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 18767855 : cxcompotor(GEN z, long prec)
1407 : {
1408 18767855 : switch(typ(z))
1409 : {
1410 11781495 : case t_INT: return itor(z, prec);
1411 294462 : case t_FRAC: return fractor(z, prec);
1412 6691898 : 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 9249388 : cxtofp(GEN x, long prec)
1419 9249388 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
1420 :
1421 : INLINE GEN
1422 338020 : cxtoreal(GEN q)
1423 338020 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
1424 :
1425 : INLINE double
1426 66286454 : gtodouble(GEN x)
1427 : {
1428 66286454 : if (typ(x)!=t_REAL) {
1429 9629891 : pari_sp av = avma;
1430 9629891 : x = gtofp(x, DEFAULTPREC);
1431 9629891 : if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
1432 9629891 : set_avma(av);
1433 : }
1434 66286454 : return rtodbl(x);
1435 : }
1436 :
1437 : INLINE int
1438 2991745 : gisdouble(GEN x, double *g)
1439 : {
1440 2991745 : if (typ(x)!=t_REAL) {
1441 42339 : pari_sp av = avma;
1442 42339 : x = gtofp(x, DEFAULTPREC);
1443 42339 : if (typ(x)!=t_REAL) return gc_double(av, 0);
1444 42339 : set_avma(av);
1445 : }
1446 2991745 : if (expo(x) >= 0x3ff) return 0;
1447 2991745 : *g = rtodbl(x); return 1;
1448 : }
1449 :
1450 : INLINE long
1451 85888586 : gtos(GEN x) {
1452 85888586 : if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
1453 85888572 : return itos(x);
1454 : }
1455 :
1456 : INLINE ulong
1457 102019 : gtou(GEN x) {
1458 102019 : if (typ(x) != t_INT || signe(x)<0)
1459 0 : pari_err_TYPE("gtou [integer >=0 expected]",x);
1460 102019 : return itou(x);
1461 : }
1462 :
1463 : INLINE GEN
1464 48084674 : absfrac(GEN x)
1465 : {
1466 48084674 : GEN y = cgetg(3, t_FRAC);
1467 48084674 : gel(y,1) = absi(gel(x,1));
1468 48084674 : gel(y,2) = icopy(gel(x,2)); return y;
1469 : }
1470 : INLINE GEN
1471 13097 : absfrac_shallow(GEN x)
1472 13097 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
1473 : INLINE GEN
1474 8131131 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
1475 : INLINE GEN
1476 127658 : Q_abs_shallow(GEN x)
1477 127658 : { 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 260478218 : gtofp(GEN z, long prec)
1488 : {
1489 260478218 : switch(typ(z))
1490 : {
1491 219748237 : case t_INT: return itor(z, prec);
1492 4106414 : case t_FRAC: return fractor(z, prec);
1493 27458207 : case t_REAL: return rtor(z, prec);
1494 9165360 : case t_COMPLEX: {
1495 9165360 : GEN a = gel(z,1), b = gel(z,2);
1496 9165360 : if (isintzero(b)) return cxcompotor(a, prec);
1497 9165360 : if (isintzero(a)) {
1498 244212 : GEN y = cgetg(3, t_COMPLEX);
1499 244212 : b = cxcompotor(b, prec);
1500 244212 : gel(y,1) = real_0_bit(expo(b) - prec);
1501 244212 : gel(y,2) = b; return y;
1502 : }
1503 8921148 : 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 7173999 : RgX_gtofp(GEN x, long prec)
1528 37435623 : { pari_APPLY_pol_normalized(gtofp(gel(x,i), prec)); }
1529 :
1530 : INLINE GEN
1531 34117806 : RgC_gtofp(GEN x, long prec)
1532 222279815 : { 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 8263654 : RgM_gtofp(GEN x, long prec)
1540 41749462 : { 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 62766 : RgX_fpnorml2(GEN x, long prec)
1552 : {
1553 62766 : pari_sp av = avma;
1554 62766 : return gc_upto(av, gnorml2(RgX_gtofp(x, prec)));
1555 : }
1556 : INLINE GEN
1557 620024 : RgC_fpnorml2(GEN x, long prec)
1558 : {
1559 620024 : pari_sp av = avma;
1560 620024 : return gc_upto(av, gnorml2(RgC_gtofp(x, prec)));
1561 : }
1562 : INLINE GEN
1563 23338 : RgM_fpnorml2(GEN x, long prec)
1564 : {
1565 23338 : pari_sp av = avma;
1566 23338 : return gc_upto(av, gnorml2(RgM_gtofp(x, prec)));
1567 : }
1568 :
1569 : /* y a t_REAL */
1570 : INLINE void
1571 665850 : affgr(GEN x, GEN y)
1572 : {
1573 665850 : switch(typ(x)) {
1574 289569 : case t_INT: affir(x,y); break;
1575 376281 : 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 665850 : }
1580 :
1581 : INLINE GEN
1582 250436 : affc_fixlg(GEN x, GEN res)
1583 : {
1584 250436 : if (typ(x) == t_COMPLEX)
1585 : {
1586 207410 : affrr_fixlg(gel(x,1), gel(res,1));
1587 207410 : affrr_fixlg(gel(x,2), gel(res,2));
1588 : }
1589 : else
1590 : {
1591 43026 : set_avma((pari_sp)(res+3));
1592 43026 : res = cgetr(realprec(gel(res,1)));
1593 43026 : affrr_fixlg(x, res);
1594 : }
1595 250436 : 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 41829 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
1608 : INLINE long
1609 33559 : 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 620898685 : nbits2nlong(long x) {
1617 620898685 : return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1618 : }
1619 :
1620 : INLINE long
1621 918239078 : nbits2extraprec(long x) {
1622 918239078 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1623 : }
1624 :
1625 : INLINE long
1626 189567379 : nbits2prec(long x) {
1627 189567379 : return (((ulong)x+BITS_IN_LONG-1)>>TWOPOTBITS_IN_LONG) << TWOPOTBITS_IN_LONG;
1628 : }
1629 :
1630 : INLINE long
1631 4748877239 : prec2lg(long x) {
1632 4748877239 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1633 : }
1634 : /* ceil(x / sizeof(long)) */
1635 : INLINE long
1636 127830498 : nchar2nlong(long x) {
1637 127830498 : return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
1638 : }
1639 :
1640 : /* convert an lg to a bit accuracy */
1641 : INLINE long
1642 3221857287 : bit_accuracy(long l) { return (l-2) * BITS_IN_LONG; }
1643 : INLINE double
1644 3176882 : bit_accuracy_mul(long l, double y) { return (l-2) * (BITS_IN_LONG*y); }
1645 : INLINE long
1646 9778 : prec2ndec(long x) { return (long)(x * LOG10_2); }
1647 : INLINE long
1648 476743 : precdbl(long x) {return x << 1;}
1649 : INLINE long
1650 8489564986 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
1651 : INLINE long
1652 8395064353 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
1653 :
1654 : /* compatibility, to be deprecated */
1655 : INLINE long
1656 93802085 : prec2nbits(long x) { return x; }
1657 : INLINE double
1658 581910 : prec2nbits_mul(long x, double y) { return x * y; }
1659 : INLINE long
1660 0 : bit_prec(GEN x) { return realprec(x); }
1661 : INLINE long
1662 0 : nbits2ndec(long x) { return prec2ndec(x); }
1663 :
1664 : /*********************************************************************/
1665 : /** **/
1666 : /** OPERATIONS MODULO m **/
1667 : /** **/
1668 : /*********************************************************************/
1669 : /* Assume m > 0, more efficient if 0 <= a, b < m */
1670 :
1671 : INLINE GEN
1672 83009137 : Fp_red(GEN a, GEN m) { return modii(a, m); }
1673 : INLINE GEN
1674 185069636 : Fp_add(GEN a, GEN b, GEN m)
1675 : {
1676 185069636 : pari_sp av=avma;
1677 185069636 : GEN p = addii(a,b);
1678 185069635 : long s = signe(p);
1679 185069635 : if (!s) return p; /* = gen_0 */
1680 171890804 : if (s > 0) /* general case */
1681 : {
1682 171633528 : GEN t = subii(p, m);
1683 171633527 : s = signe(t);
1684 171633527 : if (!s) return gc_const(av, gen_0);
1685 162451161 : if (s < 0) return gc_const((pari_sp)p, p);
1686 78093313 : if (cmpii(t, m) < 0) return gc_INT(av, t); /* general case ! */
1687 3291906 : p = remii(t, m);
1688 : }
1689 : else
1690 257276 : p = modii(p, m);
1691 3549181 : return gc_INT(av, p);
1692 : }
1693 : INLINE GEN
1694 21432309 : Fp_double(GEN x, GEN N)
1695 : {
1696 21432309 : GEN z = shifti(x, 1);
1697 21432309 : return cmpii(z, N) >= 0? subii(z, N): z;
1698 : }
1699 : INLINE GEN
1700 203723235 : Fp_sub(GEN a, GEN b, GEN m)
1701 : {
1702 203723235 : pari_sp av=avma;
1703 203723235 : GEN p = subii(a,b);
1704 203723234 : long s = signe(p);
1705 203723234 : if (!s) return p; /* = gen_0 */
1706 188053431 : if (s > 0)
1707 : {
1708 93446476 : if (cmpii(p, m) < 0) return p; /* general case ! */
1709 1484703 : p = remii(p, m);
1710 : }
1711 : else
1712 : {
1713 94606955 : GEN t = addii(p, m);
1714 94606952 : if (!s) return gc_const(av, gen_0);
1715 94606952 : if (s > 0) return gc_INT(av, t); /* general case ! */
1716 94606952 : p = modii(t, m);
1717 : }
1718 96091653 : return gc_INT(av, p);
1719 : }
1720 : INLINE GEN
1721 37725005 : Fp_neg(GEN b, GEN m)
1722 : {
1723 37725005 : pari_sp av = avma;
1724 37725005 : long s = signe(b);
1725 : GEN p;
1726 37725005 : if (!s) return gen_0;
1727 30165771 : if (s > 0)
1728 : {
1729 28902994 : p = subii(m, b);
1730 28902994 : if (signe(p) >= 0) return p; /* general case ! */
1731 614826 : p = modii(p, m);
1732 : } else
1733 1262777 : p = remii(negi(b), m);
1734 1877603 : return gc_INT(av, p);
1735 : }
1736 :
1737 : INLINE GEN
1738 243517 : Fp_halve(GEN a, GEN p)
1739 : {
1740 243517 : if (mpodd(a)) a = addii(a,p);
1741 243517 : return shifti(a,-1);
1742 : }
1743 :
1744 : /* assume 0 <= u < p and ps2 = p>>1 */
1745 : INLINE GEN
1746 78862955 : Fp_center(GEN u, GEN p, GEN ps2)
1747 78862955 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
1748 : /* same without copy */
1749 : INLINE GEN
1750 18208104 : Fp_center_i(GEN u, GEN p, GEN ps2)
1751 18208104 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
1752 :
1753 : /* x + y*z mod p */
1754 : INLINE GEN
1755 11351472 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
1756 : {
1757 : pari_sp av;
1758 11351472 : if (!signe(y) || !signe(z)) return Fp_red(x, p);
1759 10884190 : if (!signe(x)) return Fp_mul(z,y, p);
1760 9839688 : av = avma;
1761 9839688 : return gc_INT(av, modii(addii(x, mulii(y,z)), p));
1762 : }
1763 :
1764 : INLINE GEN
1765 205034739 : Fp_mul(GEN a, GEN b, GEN m)
1766 : {
1767 205034739 : pari_sp av=avma;
1768 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1769 205034739 : (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
1770 205034739 : p = mulii(a,b);
1771 205034739 : set_avma(av); return modii(p,m);
1772 : }
1773 : INLINE GEN
1774 69021504 : Fp_sqr(GEN a, GEN m)
1775 : {
1776 69021504 : pari_sp av=avma;
1777 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1778 69021504 : (void)new_chunk((lg(a)+lg(m))<<1);
1779 69021504 : p = sqri(a);
1780 69021503 : set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
1781 : }
1782 : INLINE GEN
1783 72696519 : Fp_mulu(GEN a, ulong b, GEN m)
1784 : {
1785 72696519 : long l = lgefint(m);
1786 72696519 : if (l == 3)
1787 : {
1788 52999387 : ulong mm = m[2];
1789 52999387 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1790 : } else {
1791 19697132 : pari_sp av = avma;
1792 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1793 19697132 : (void)new_chunk(lg(a)+1+(l<<1));
1794 19697132 : p = muliu(a,b);
1795 19697132 : set_avma(av); return modii(p,m);
1796 : }
1797 : }
1798 : INLINE GEN
1799 17640 : Fp_muls(GEN a, long b, GEN m)
1800 : {
1801 17640 : long l = lgefint(m);
1802 17640 : if (l == 3)
1803 : {
1804 3438 : ulong mm = m[2];
1805 3438 : if (b < 0)
1806 : {
1807 3438 : ulong t = Fl_mul(umodiu(a, mm), -b, mm);
1808 3438 : return t? utoipos(mm - t): gen_0;
1809 : }
1810 : else
1811 0 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1812 : } else {
1813 14202 : pari_sp av = avma;
1814 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1815 14202 : (void)new_chunk(lg(a)+1+(l<<1));
1816 14202 : p = mulis(a,b);
1817 14202 : set_avma(av); return modii(p,m);
1818 : }
1819 : }
1820 :
1821 : INLINE GEN
1822 23544870 : Fp_inv(GEN a, GEN m)
1823 : {
1824 : GEN res;
1825 23544870 : if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
1826 23544542 : return res;
1827 : }
1828 : INLINE GEN
1829 1098686 : Fp_invsafe(GEN a, GEN m)
1830 : {
1831 : GEN res;
1832 1098686 : if (! invmod(a,m,&res)) return NULL;
1833 1098686 : return res;
1834 : }
1835 : INLINE GEN
1836 14993225 : Fp_div(GEN a, GEN b, GEN m)
1837 : {
1838 14993225 : pari_sp av = avma;
1839 : GEN p;
1840 14993225 : if (lgefint(b) == 3)
1841 : {
1842 13848641 : a = Fp_divu(a, b[2], m);
1843 13848641 : if (signe(b) < 0) a = Fp_neg(a, m);
1844 13848641 : return a;
1845 : }
1846 : /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1847 1144584 : (void)new_chunk(lg(a)+(lg(m)<<1));
1848 1144584 : p = mulii(a, Fp_inv(b,m));
1849 1144584 : set_avma(av); return modii(p,m);
1850 : }
1851 : INLINE GEN
1852 15087853 : Fp_divu(GEN x, ulong a, GEN p)
1853 : {
1854 15087853 : pari_sp av = avma;
1855 : ulong b;
1856 15087853 : if (lgefint(p) == 3)
1857 : {
1858 14560087 : ulong pp = p[2], xp = umodiu(x, pp);
1859 14560087 : return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
1860 : }
1861 527766 : x = Fp_red(x, p);
1862 527766 : b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
1863 527766 : return gc_INT(av, diviuexact(addmuliu(x, p, b), a));
1864 : }
1865 :
1866 : INLINE GEN
1867 1155304 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
1868 :
1869 : INLINE GEN
1870 3311009 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1871 :
1872 : INLINE long
1873 2176625 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1874 :
1875 : INLINE long
1876 1936516 : get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
1877 :
1878 : INLINE GEN
1879 343 : get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1880 :
1881 : INLINE long
1882 376106 : get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1883 :
1884 : INLINE long
1885 201185 : get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1886 :
1887 : INLINE GEN
1888 25663245 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1889 :
1890 : INLINE long
1891 69187849 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1892 :
1893 : INLINE long
1894 84097989 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1895 :
1896 : INLINE GEN
1897 8033 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1898 :
1899 : INLINE long
1900 266299 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1901 :
1902 : INLINE long
1903 320067 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1904 :
1905 : INLINE GEN
1906 3391338 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1907 :
1908 : INLINE long
1909 6879853 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1910 :
1911 : INLINE long
1912 8094069 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1913 :
1914 : INLINE GEN
1915 146583 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1916 :
1917 : INLINE long
1918 51430 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1919 :
1920 : INLINE long
1921 3569 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1922 :
1923 : /*******************************************************************/
1924 : /* */
1925 : /* ADDMULII / SUBMULII */
1926 : /* */
1927 : /*******************************************************************/
1928 : /* x - y*z */
1929 : INLINE GEN
1930 33037982 : submulii(GEN x, GEN y, GEN z)
1931 : {
1932 33037982 : long lx = lgefint(x), ly, lz;
1933 : pari_sp av;
1934 : GEN t;
1935 33037982 : if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
1936 30289505 : ly = lgefint(y);
1937 30289505 : if (ly == 2) return icopy(x);
1938 28976649 : lz = lgefint(z);
1939 28976649 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1940 28976649 : t = mulii(z, y);
1941 28976649 : set_avma(av); return subii(x,t);
1942 : }
1943 : /* y*z - x */
1944 : INLINE GEN
1945 3898069 : mulsubii(GEN y, GEN z, GEN x)
1946 : {
1947 3898069 : long lx = lgefint(x), ly, lz;
1948 : pari_sp av;
1949 : GEN t;
1950 3898069 : if (lx == 2) return mulii(z,y);
1951 2460770 : ly = lgefint(y);
1952 2460770 : if (ly == 2) return negi(x);
1953 2174899 : lz = lgefint(z);
1954 2174899 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1955 2174899 : t = mulii(z, y);
1956 2174899 : set_avma(av); return subii(t,x);
1957 : }
1958 :
1959 : /* x - u*y */
1960 : INLINE GEN
1961 7700 : submuliu(GEN x, GEN y, ulong u)
1962 : {
1963 : pari_sp av;
1964 7700 : long ly = lgefint(y);
1965 7700 : if (ly == 2) return icopy(x);
1966 7700 : av = avma;
1967 7700 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1968 7700 : y = mului(u,y);
1969 7700 : set_avma(av); return subii(x, y);
1970 : }
1971 : /* x + u*y */
1972 : INLINE GEN
1973 549715 : addmuliu(GEN x, GEN y, ulong u)
1974 : {
1975 : pari_sp av;
1976 549715 : long ly = lgefint(y);
1977 549715 : if (ly == 2) return icopy(x);
1978 549715 : av = avma;
1979 549715 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1980 549715 : y = mului(u,y);
1981 549715 : set_avma(av); return addii(x, y);
1982 : }
1983 : /* x - u*y */
1984 : INLINE GEN
1985 66603699 : submuliu_inplace(GEN x, GEN y, ulong u)
1986 : {
1987 : pari_sp av;
1988 66603699 : long ly = lgefint(y);
1989 66603699 : if (ly == 2) return x;
1990 43474254 : av = avma;
1991 43474254 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1992 43474254 : y = mului(u,y);
1993 43474254 : set_avma(av); return subii(x, y);
1994 : }
1995 : /* x + u*y */
1996 : INLINE GEN
1997 66099633 : addmuliu_inplace(GEN x, GEN y, ulong u)
1998 : {
1999 : pari_sp av;
2000 66099633 : long ly = lgefint(y);
2001 66099633 : if (ly == 2) return x;
2002 43415675 : av = avma;
2003 43415675 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
2004 43415675 : y = mului(u,y);
2005 43415675 : set_avma(av); return addii(x, y);
2006 : }
2007 : /* ux + vy */
2008 : INLINE GEN
2009 38347504 : lincombii(GEN u, GEN v, GEN x, GEN y)
2010 : {
2011 38347504 : long lx = lgefint(x), ly;
2012 : GEN p1, p2;
2013 : pari_sp av;
2014 38347504 : if (lx == 2) return mulii(v,y);
2015 24109712 : ly = lgefint(y);
2016 24109712 : if (ly == 2) return mulii(u,x);
2017 21366901 : av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
2018 21366901 : p1 = mulii(u,x);
2019 21366901 : p2 = mulii(v,y);
2020 21366901 : set_avma(av); return addii(p1,p2);
2021 : }
2022 :
2023 : /*******************************************************************/
2024 : /* */
2025 : /* GEN SUBTYPES */
2026 : /* */
2027 : /*******************************************************************/
2028 :
2029 : INLINE int
2030 5001647016 : is_const_t(long t) { return (t < t_POLMOD); }
2031 : INLINE int
2032 6904 : is_extscalar_t(long t) { return (t <= t_POL); }
2033 : INLINE int
2034 9716413 : is_intreal_t(long t) { return (t <= t_REAL); }
2035 : INLINE int
2036 639693543 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
2037 : INLINE int
2038 86218867 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
2039 : INLINE int
2040 0 : is_qfb_t(long t) { return (t == t_QFB); }
2041 : INLINE int
2042 5304028 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
2043 : INLINE int
2044 62080967 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
2045 : INLINE int
2046 9575550081 : is_recursive_t(long t) { return lontyp[t]; }
2047 : INLINE int
2048 376860009 : is_scalar_t(long t) { return (t < t_POL); }
2049 : INLINE int
2050 44253497 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
2051 :
2052 : INLINE int
2053 65334819 : qfb_is_qfi(GEN q) { return signe(gel(q,4)) < 0; }
2054 :
2055 : /*******************************************************************/
2056 : /* */
2057 : /* TRANSCENDENTAL */
2058 : /* */
2059 : /*******************************************************************/
2060 : INLINE GEN
2061 87238019 : sqrtr(GEN x) {
2062 87238019 : long s = signe(x);
2063 87238019 : if (s == 0) return real_0_bit(expo(x) >> 1);
2064 87204836 : if (s >= 0) return sqrtr_abs(x);
2065 443243 : retmkcomplex(gen_0, sqrtr_abs(x));
2066 : }
2067 : INLINE GEN
2068 0 : cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
2069 : INLINE GEN
2070 0 : cbrtr(GEN x) {
2071 0 : long s = signe(x);
2072 : GEN r;
2073 0 : if (s == 0) return real_0_bit(expo(x) / 3);
2074 0 : r = cbrtr_abs(x);
2075 0 : if (s < 0) togglesign(r);
2076 0 : return r;
2077 : }
2078 : INLINE GEN
2079 2811833 : sqrtnr(GEN x, long n) {
2080 2811833 : long s = signe(x);
2081 : GEN r;
2082 2811833 : if (s == 0) return real_0_bit(expo(x) / n);
2083 2811833 : r = sqrtnr_abs(x, n);
2084 2811833 : if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
2085 2811833 : return r;
2086 : }
2087 : INLINE long
2088 757174 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
2089 : INLINE ulong
2090 2101623 : ulogint(ulong B, ulong y)
2091 : {
2092 : ulong r;
2093 : long e;
2094 2101623 : if (y == 2) return expu(B);
2095 2053093 : r = y;
2096 6219787 : for (e=1;; e++)
2097 : { /* here, r = y^e, r2 = y^(e-1) */
2098 6219787 : if (r >= B) return r == B? e: e-1;
2099 4168250 : r = umuluu_or_0(y, r);
2100 4168250 : if (!r) return e;
2101 : }
2102 : }
2103 :
2104 : /*******************************************************************/
2105 : /* */
2106 : /* MISCELLANEOUS */
2107 : /* */
2108 : /*******************************************************************/
2109 9716322 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
2110 2910465747 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
2111 18256765 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
2112 6224605 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
2113 1180130937 : INLINE int equali1(GEN n)
2114 1180130937 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
2115 139584499 : INLINE int equalim1(GEN n)
2116 139584499 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
2117 : /* works only for POSITIVE integers */
2118 2307731149 : INLINE int is_pm1(GEN n)
2119 2307731149 : { return lgefint(n) == 3 && n[2] == 1; }
2120 592014736 : INLINE int is_bigint(GEN n)
2121 592014736 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
2122 :
2123 2139247958 : INLINE int odd(long x) { return x & 1; }
2124 124810985 : INLINE int both_odd(long x, long y) { return x & y & 1; }
2125 :
2126 : INLINE int
2127 8505372751 : isonstack(GEN x)
2128 8505372751 : { return ((pari_sp)x >= pari_mainstack->bot
2129 8505372751 : && (pari_sp)x < pari_mainstack->top); }
2130 :
2131 : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
2132 : INLINE double
2133 94962106 : dbllog2r(GEN x)
2134 94962106 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
2135 :
2136 : INLINE GEN
2137 2672461 : mul_content(GEN cx, GEN cy)
2138 : {
2139 2672461 : if (!cx) return cy;
2140 573800 : if (!cy) return cx;
2141 377775 : return gmul(cx,cy);
2142 : }
2143 : INLINE GEN
2144 0 : inv_content(GEN c) { return c? ginv(c): NULL; }
2145 : INLINE GEN
2146 58502 : div_content(GEN cx, GEN cy)
2147 : {
2148 58502 : if (!cy) return cx;
2149 57368 : if (!cx) return ginv(cy);
2150 26020 : return gdiv(cx,cy);
2151 : }
2152 : INLINE GEN
2153 10788586 : mul_denom(GEN dx, GEN dy)
2154 : {
2155 10788586 : if (!dx) return dy;
2156 1904077 : if (!dy) return dx;
2157 1391486 : return mulii(dx,dy);
2158 : }
2159 :
2160 : /* POLYNOMIALS */
2161 : INLINE GEN
2162 1065052 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
2163 : INLINE GEN
2164 169548675 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
2165 : INLINE ulong
2166 1067069 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
2167 : INLINE ulong
2168 1092086 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
2169 : INLINE long
2170 5656786132 : degpol(GEN x) { return lg(x)-3; }
2171 : INLINE long
2172 2775934838 : lgpol(GEN x) { return lg(x)-2; }
2173 : INLINE long
2174 354520278 : lgcols(GEN x) { return lg(gel(x,1)); }
2175 : INLINE long
2176 159333706 : nbrows(GEN x) { return lg(gel(x,1))-1; }
2177 : INLINE GEN
2178 0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
2179 :
2180 : INLINE GEN
2181 2192274 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
2182 : INLINE GEN
2183 963429 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
2184 :
2185 : INLINE GEN
2186 103803432 : RgX_copy(GEN x)
2187 377979966 : { pari_APPLY_pol_normalized(gcopy(gel(x,i))); }
2188 : /* have to use ulong to avoid silly warnings from gcc "assuming signed
2189 : * overflow does not occur" */
2190 : INLINE GEN
2191 2909086 : RgX_coeff(GEN x, long n)
2192 : {
2193 2909086 : ulong l = lg(x);
2194 2909086 : return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
2195 : }
2196 : INLINE GEN
2197 34135 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
2198 : INLINE GEN
2199 7001097 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
2200 : INLINE GEN
2201 1904 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
2202 : INLINE GEN
2203 127908 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
2204 : INLINE GEN
2205 5609837 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
2206 : INLINE GEN
2207 2003312 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
2208 : INLINE GEN
2209 21817184 : Flx_div_pre(GEN x, GEN y, ulong p, ulong pi)
2210 21817184 : { return Flx_divrem_pre(x,y,p,pi, NULL); }
2211 : INLINE GEN
2212 1580201 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
2213 : INLINE GEN
2214 0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
2215 : INLINE GEN
2216 114780154 : pol0_Flx(long sv) { return mkvecsmall(sv); }
2217 : INLINE GEN
2218 56077880 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
2219 : INLINE GEN
2220 37979987 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
2221 : INLINE GEN
2222 0 : zero_zx(long sv) { return zero_Flx(sv); }
2223 : INLINE GEN
2224 0 : polx_zx(long sv) { return polx_Flx(sv); }
2225 : INLINE GEN
2226 0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
2227 : INLINE GEN
2228 14049 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
2229 : INLINE GEN
2230 1350 : zero_F2x(long sv) { return zero_Flx(sv); }
2231 : INLINE GEN
2232 17425503 : pol0_F2x(long sv) { return pol0_Flx(sv); }
2233 : INLINE GEN
2234 4776318 : pol1_F2x(long sv) { return pol1_Flx(sv); }
2235 : INLINE GEN
2236 1083679 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
2237 : INLINE int
2238 2871807 : F2x_equal1(GEN x) { return Flx_equal1(x); }
2239 : INLINE int
2240 4257850 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
2241 : INLINE GEN
2242 66592263 : F2x_copy(GEN x) { return leafcopy(x); }
2243 : INLINE GEN
2244 4893 : F2v_copy(GEN x) { return leafcopy(x); }
2245 : INLINE GEN
2246 2251387 : Flv_copy(GEN x) { return leafcopy(x); }
2247 : INLINE GEN
2248 196483468 : Flx_copy(GEN x) { return leafcopy(x); }
2249 : INLINE GEN
2250 7353851 : vecsmall_copy(GEN x) { return leafcopy(x); }
2251 : INLINE int
2252 8248275 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
2253 : INLINE int
2254 10514 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
2255 : INLINE int
2256 7723199 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
2257 :
2258 : INLINE GEN
2259 158593007 : ZX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2260 : INLINE GEN
2261 146568442 : FpX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2262 : INLINE GEN
2263 782142 : FpXX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2264 : INLINE GEN
2265 4641900 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2266 : INLINE GEN
2267 130577349 : F2x_renormalize(GEN x, long lx) { return Flx_renormalize(x,lx); }
2268 :
2269 : INLINE GEN
2270 0 : F2xX_shift(GEN a, long n, long vs) { return FlxX_shift(a, n, vs); }
2271 :
2272 : INLINE GEN
2273 38976 : F2v_to_F2x(GEN x, long sv) {
2274 38976 : GEN y = leafcopy(x);
2275 38976 : y[1] = sv; F2x_renormalize(y, lg(y)); return y;
2276 : }
2277 :
2278 : INLINE long
2279 385 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
2280 :
2281 : INLINE long
2282 6391 : gval(GEN x, long v)
2283 6391 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
2284 :
2285 : INLINE void
2286 479615 : RgX_shift_inplace_init(long v)
2287 479615 : { if (v) (void)cgetg(v, t_VECSMALL); }
2288 : /* shift polynomial in place. assume v free cells have been left before x */
2289 : INLINE GEN
2290 479615 : RgX_shift_inplace(GEN x, long v)
2291 : {
2292 : long i, lx;
2293 : GEN z;
2294 479615 : if (!v) return x;
2295 252260 : lx = lg(x);
2296 252260 : if (lx == 2) return x;
2297 252260 : z = x + lx;
2298 : /* stackdummy's from normalizepol */
2299 252260 : while (lg(z) != v) z += lg(z);
2300 252260 : z += v;
2301 4637681 : for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
2302 672402 : for (i = 0; i < v; i++) gel(--z,0) = gen_0;
2303 252260 : z -= 2;
2304 252260 : z[1] = x[1];
2305 252260 : z[0] = evaltyp(t_POL) | evallg(lx+v);
2306 252260 : stackdummy((pari_sp)z, (pari_sp)x); return z;
2307 : }
2308 :
2309 :
2310 : /* LINEAR ALGEBRA */
2311 : INLINE GEN
2312 504205 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
2313 : INLINE GEN
2314 7978592 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
2315 : INLINE GEN
2316 3318880 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
2317 : INLINE GEN
2318 0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
2319 : INLINE GEN
2320 104146 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
2321 : INLINE GEN
2322 0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
2323 : INLINE GEN
2324 0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
2325 : INLINE GEN
2326 39184826 : zero_zv(long x) { return zero_Flv(x); }
2327 : INLINE GEN
2328 266 : zm_transpose(GEN x) { return Flm_transpose(x); }
2329 : INLINE GEN
2330 0 : zm_copy(GEN x) { return Flm_copy(x); }
2331 : INLINE GEN
2332 1879827 : zv_copy(GEN x) { return Flv_copy(x); }
2333 : INLINE GEN
2334 0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
2335 :
2336 : INLINE GEN
2337 10699866 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfdivrem(x,y,NULL); }
2338 : INLINE GEN
2339 401525 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
2340 : INLINE GEN
2341 7142087 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
2342 : INLINE void
2343 9956267 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
2344 : INLINE GEN
2345 22268925 : RgM_shallowcopy(GEN x)
2346 96587255 : { pari_APPLY_same(leafcopy(gel(x,i))); }
2347 : INLINE GEN
2348 201408 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
2349 :
2350 : INLINE GEN
2351 0 : F3m_copy(GEN x) { return RgM_shallowcopy(x); }
2352 :
2353 : INLINE GEN
2354 6442229 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
2355 :
2356 : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
2357 : * x,y are ZV of the same length */
2358 : INLINE int
2359 23485 : ZV_dvd(GEN x, GEN y)
2360 : {
2361 23485 : long i, l = lg(x);
2362 37856 : for (i=1; i < l; i++)
2363 30464 : if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
2364 7392 : return 1;
2365 : }
2366 : INLINE GEN
2367 610960 : ZM_ZV_mod(GEN x, GEN y)
2368 1796063 : { pari_APPLY_same(ZV_ZV_mod(gel(x,i), y)) }
2369 : INLINE GEN
2370 8147966 : ZV_ZV_mod(GEN x, GEN y)
2371 22536652 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
2372 : INLINE GEN
2373 0 : vecmodii(GEN x, GEN y) { return ZV_ZV_mod(x,y); }
2374 : INLINE GEN
2375 174881 : vecmoduu(GEN x, GEN y) { pari_APPLY_ulong(((ulong*)x)[i] % ((ulong*)y)[i]) }
2376 :
2377 : /* Fq */
2378 : INLINE GEN
2379 3109642 : Fq_red(GEN x, GEN T, GEN p)
2380 3109642 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
2381 : INLINE GEN
2382 73652 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
2383 : {
2384 : (void) p;
2385 73652 : return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
2386 : }
2387 : INLINE GEN
2388 756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
2389 :
2390 : INLINE GEN
2391 29946 : gener_Fq_local(GEN T, GEN p, GEN L)
2392 0 : { return T? gener_FpXQ_local(T,p, L)
2393 29946 : : pgener_Fp_local(p, L); }
2394 :
2395 : INLINE GEN
2396 0 : random_Fq(GEN T, GEN p)
2397 0 : { return T ? random_FpX(get_FpX_degree(T), get_FpX_var(T), p): randomi(p); }
2398 :
2399 : /* FpXQX */
2400 : INLINE GEN
2401 4519 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
2402 : INLINE GEN
2403 0 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
2404 : INLINE GEN
2405 165917 : FlxqX_div_pre(GEN x, GEN y, GEN T, ulong p, ulong pi) { return FlxqX_divrem_pre(x, y, T, p, pi, NULL); }
2406 : INLINE GEN
2407 33187 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
2408 :
2409 : INLINE GEN
2410 20405 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
2411 20405 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
2412 :
2413 : /* FqX */
2414 : INLINE GEN
2415 25298 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
2416 : INLINE GEN
2417 124082 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2418 : INLINE GEN
2419 24900 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
2420 : INLINE GEN
2421 68026 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2422 : INLINE GEN
2423 579685 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
2424 579685 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
2425 : INLINE GEN
2426 424510 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
2427 424510 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
2428 : INLINE GEN
2429 369137 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
2430 369137 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
2431 : INLINE GEN
2432 21900 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
2433 21900 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
2434 : INLINE GEN
2435 71232 : FqX_sqr(GEN x, GEN T, GEN p)
2436 71232 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
2437 : INLINE GEN
2438 1288 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
2439 1288 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
2440 : INLINE GEN
2441 19250 : FqX_halve(GEN x, GEN T, GEN p)
2442 19250 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
2443 : INLINE GEN
2444 38569 : FqX_div(GEN x, GEN y, GEN T, GEN p)
2445 38569 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
2446 : INLINE GEN
2447 6069 : FqX_get_red(GEN S, GEN T, GEN p)
2448 6069 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
2449 : INLINE GEN
2450 37659 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
2451 37659 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
2452 : INLINE GEN
2453 0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
2454 0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
2455 : INLINE GEN
2456 69699 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
2457 69699 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
2458 : INLINE GEN
2459 0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
2460 0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
2461 : INLINE GEN
2462 8288 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
2463 8288 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
2464 : INLINE GEN
2465 449759 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
2466 449759 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
2467 : INLINE GEN
2468 4144 : FqX_normalize(GEN z, GEN T, GEN p)
2469 4144 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
2470 : INLINE GEN
2471 39697 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
2472 : INLINE long
2473 683718 : FqX_is_squarefree(GEN P, GEN T, GEN p)
2474 683718 : { return T ? FpXQX_is_squarefree(P, T, p): FpX_is_squarefree(P, p); }
2475 : INLINE GEN
2476 0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
2477 : INLINE GEN
2478 140960 : FqX_factor(GEN f, GEN T, GEN p)
2479 140960 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
2480 : INLINE GEN
2481 7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
2482 7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
2483 : INLINE GEN
2484 7 : FqX_ddf(GEN f, GEN T, GEN p)
2485 7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
2486 : INLINE GEN
2487 47786 : FqX_degfact(GEN f, GEN T, GEN p)
2488 47786 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
2489 : INLINE GEN
2490 7231 : FqX_roots(GEN f, GEN T, GEN p)
2491 7231 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
2492 : INLINE GEN
2493 175 : FqX_to_mod(GEN f, GEN T, GEN p)
2494 175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
2495 :
2496 : /*FqXQ*/
2497 : INLINE GEN
2498 0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2499 0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2500 : INLINE GEN
2501 0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2502 0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2503 : INLINE GEN
2504 0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
2505 0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
2506 : INLINE GEN
2507 0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
2508 0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
2509 : INLINE GEN
2510 0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
2511 0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
2512 : INLINE GEN
2513 22745 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
2514 22745 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
2515 : INLINE GEN
2516 0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
2517 0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
2518 : INLINE GEN
2519 0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
2520 0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
2521 :
2522 : /*FqXn*/
2523 : INLINE GEN
2524 6590 : FqXn_expint(GEN x, long n, GEN T, GEN p)
2525 6590 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
2526 : INLINE GEN
2527 0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
2528 0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
2529 : INLINE GEN
2530 7070 : FqXn_inv(GEN x, long n, GEN T, GEN p)
2531 7070 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
2532 : INLINE GEN
2533 261240 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
2534 261240 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
2535 : INLINE GEN
2536 0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
2537 0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
2538 :
2539 : /*FpXQ*/
2540 : INLINE GEN
2541 0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
2542 0 : { (void)T; return FpX_add(x,y,p); }
2543 : INLINE GEN
2544 0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
2545 0 : { (void)T; return FpX_sub(x,y,p); }
2546 :
2547 : /*Flxq*/
2548 : INLINE GEN
2549 0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
2550 0 : { (void)T; return Flx_add(x,y,p); }
2551 : INLINE GEN
2552 0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
2553 0 : { (void)T; return Flx_sub(x,y,p); }
2554 :
2555 : /* F2x */
2556 :
2557 : INLINE ulong
2558 672394599 : F2x_coeff(GEN x,long v)
2559 : {
2560 672394599 : ulong u=(ulong)x[2+divsBIL(v)];
2561 672394599 : return (u>>remsBIL(v))&1UL;
2562 : }
2563 :
2564 : INLINE void
2565 12463975 : F2x_clear(GEN x,long v)
2566 : {
2567 12463975 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2568 12463975 : *u&=~(1UL<<remsBIL(v));
2569 12463975 : }
2570 :
2571 : INLINE void
2572 130227878 : F2x_set(GEN x,long v)
2573 : {
2574 130227878 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2575 130227878 : *u|=1UL<<remsBIL(v);
2576 130227878 : }
2577 :
2578 : INLINE void
2579 1560519 : F2x_flip(GEN x,long v)
2580 : {
2581 1560519 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2582 1560519 : *u^=1UL<<remsBIL(v);
2583 1560519 : }
2584 :
2585 : /* F2v */
2586 :
2587 : INLINE ulong
2588 665814691 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
2589 :
2590 : INLINE void
2591 12463975 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
2592 :
2593 : INLINE void
2594 104219420 : F2v_set(GEN x,long v) { F2x_set(x,v-1); }
2595 :
2596 : INLINE void
2597 1560519 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
2598 :
2599 : /* F2m */
2600 :
2601 : INLINE ulong
2602 8463008 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
2603 :
2604 : INLINE void
2605 0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
2606 :
2607 : INLINE void
2608 798 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
2609 :
2610 : INLINE void
2611 1560519 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
2612 :
2613 : /* F3m */
2614 :
2615 : INLINE ulong
2616 144858 : F3m_coeff(GEN x, long a, long b) { return F3v_coeff(gel(x,b), a); }
2617 :
2618 : INLINE void
2619 0 : F3m_set(GEN x, long a, long b, ulong c) { F3v_set(gel(x,b), a, c); }
2620 :
2621 : /* ARITHMETIC */
2622 : INLINE GEN
2623 4935 : matpascal(long n) { return matqpascal(n, NULL); }
2624 : INLINE long
2625 509837 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
2626 : INLINE long
2627 98 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
2628 : INLINE GEN
2629 6618551 : sqrti(GEN x) { return sqrtremi(x,NULL); }
2630 : INLINE GEN
2631 123273820 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
2632 : INLINE int
2633 761951 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
2634 : INLINE int
2635 29246 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
2636 : INLINE GEN
2637 0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
2638 : INLINE GEN
2639 0 : gminsg(long s, GEN y) { return gmings(y,s); }
2640 : INLINE GEN
2641 29729724 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
2642 : INLINE GEN
2643 283377954 : gmulgu(GEN y, ulong s) { return gmulug(s,y); }
2644 : INLINE GEN
2645 1731854 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
2646 : INLINE GEN
2647 561958 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
2648 :
2649 : INLINE GEN
2650 13510257 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
2651 : INLINE GEN
2652 902822 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
2653 :
2654 : /* x t_COMPLEX */
2655 : INLINE GEN
2656 95486508 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
2657 : /* q t_QUAD */
2658 : INLINE GEN
2659 70105 : quadnorm(GEN q)
2660 : {
2661 70105 : GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
2662 70105 : GEN z, u = gel(q,3), v = gel(q,2);
2663 70105 : if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
2664 : {
2665 69720 : z = signe(b)? mulii(v, addii(u,v)): sqri(v);
2666 69720 : return addii(z, mulii(c, sqri(u)));
2667 : }
2668 : else
2669 : {
2670 385 : z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
2671 385 : return gadd(z, gmul(c, gsqr(u)));
2672 : }
2673 : }
2674 : /* x a t_QUAD, return the attached discriminant */
2675 : INLINE GEN
2676 1379 : quad_disc(GEN x)
2677 : {
2678 1379 : GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
2679 1379 : if (is_pm1(b)) return subsi(1, c4);
2680 392 : togglesign_safe(&c4); return c4;
2681 : }
2682 : INLINE GEN
2683 5001659 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
2684 : INLINE GEN
2685 18522403 : qfb_disc(GEN x) { return gel(x,4); }
2686 :
2687 : INLINE GEN
2688 4399588 : sqrfrac(GEN x)
2689 : {
2690 4399588 : GEN z = cgetg(3,t_FRAC);
2691 4399588 : gel(z,1) = sqri(gel(x,1));
2692 4399588 : gel(z,2) = sqri(gel(x,2)); return z;
2693 : }
2694 :
2695 : INLINE void
2696 28120726 : normalize_frac(GEN z) {
2697 28120726 : if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
2698 28120726 : }
2699 :
2700 : INLINE GEN
2701 43757736 : powii(GEN x, GEN n)
2702 : {
2703 43757736 : long ln = lgefint(n);
2704 43757736 : if (ln == 3) {
2705 : GEN z;
2706 43716108 : if (signe(n) > 0) return powiu(x, n[2]);
2707 75076 : z = cgetg(3, t_FRAC);
2708 75076 : gel(z,1) = gen_1;
2709 75076 : gel(z,2) = powiu(x, n[2]);
2710 75076 : return z;
2711 : }
2712 41628 : if (ln == 2) return gen_1; /* rare */
2713 : /* should never happen */
2714 0 : return powgi(x, n); /* overflow unless x = 0, 1, -1 */
2715 : }
2716 : INLINE GEN
2717 620095 : powIs(long n) {
2718 620095 : switch(n & 3)
2719 : {
2720 154938 : case 1: return mkcomplex(gen_0,gen_1);
2721 156632 : case 2: return gen_m1;
2722 91308 : case 3: return mkcomplex(gen_0,gen_m1);
2723 : }
2724 217217 : return gen_1;
2725 : }
2726 :
2727 : /*******************************************************************/
2728 : /* */
2729 : /* ELLIPTIC CURVES */
2730 : /* */
2731 : /*******************************************************************/
2732 17419107 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
2733 11293909 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
2734 13229523 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
2735 10507534 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
2736 10552241 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
2737 7552253 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
2738 2550719 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
2739 3863695 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
2740 3297567 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
2741 13707219 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
2742 16166544 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
2743 18144792 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
2744 2126895 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
2745 22946868 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
2746 1791831 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
2747 551881 : INLINE GEN ellff_get_a4a6(GEN x) { return gmael(x, 15, 2); }
2748 1463 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
2749 308 : INLINE long ellQp_get_prec(GEN E) { return valp(ellQp_get_zero(E)); }
2750 1134 : INLINE GEN ellQp_get_p(GEN E) { return padic_p(ellQp_get_zero(E)); }
2751 232939 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
2752 371829 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
2753 2027312 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
2754 119 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
2755 :
2756 10953993 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
2757 59323249 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
2758 1097909 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
2759 :
2760 : /*******************************************************************/
2761 : /* */
2762 : /* ALGEBRAIC NUMBER THEORY */
2763 : /* */
2764 : /*******************************************************************/
2765 31237605 : INLINE GEN modpr_get_pr(GEN x) { return gel(x,3); }
2766 1887627 : INLINE GEN modpr_get_p(GEN x) { return pr_get_p(modpr_get_pr(x)); }
2767 13867549 : INLINE GEN modpr_get_T(GEN x) { return lg(x) == 4? NULL: gel(x,4); }
2768 :
2769 92765275 : INLINE GEN pr_get_p(GEN pr) { return gel(pr,1); }
2770 12077744 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
2771 : /* .[2] instead of itos works: e and f are small positive integers */
2772 16165058 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
2773 26447308 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
2774 49777004 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
2775 : INLINE int
2776 17227552 : pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
2777 : INLINE GEN
2778 1090562 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
2779 : INLINE ulong
2780 347921 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
2781 :
2782 : /* assume nf a genuine nf */
2783 : INLINE long
2784 1504035 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
2785 : INLINE GEN
2786 98101841 : nf_get_pol(GEN nf) { return gel(nf,1); }
2787 : INLINE long
2788 71318949 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
2789 : INLINE long
2790 23476569 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
2791 : INLINE long
2792 4154 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
2793 : INLINE GEN
2794 464439 : nf_get_disc(GEN nf) { return gel(nf,3); }
2795 : INLINE GEN
2796 3716978 : nf_get_index(GEN nf) { return gel(nf,4); }
2797 : INLINE GEN
2798 21751506 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
2799 : INLINE GEN
2800 304593 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
2801 : INLINE GEN
2802 1749523 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
2803 : INLINE GEN
2804 23352 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
2805 : INLINE GEN
2806 4445 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
2807 : INLINE GEN
2808 65098 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
2809 : INLINE GEN
2810 1632825 : nf_get_roots(GEN nf) { return gel(nf,6); }
2811 : INLINE GEN
2812 4382 : nf_get_zk(GEN nf)
2813 : {
2814 4382 : GEN y = gel(nf,7), D = gel(y, 1);
2815 4382 : if (typ(D) == t_POL) D = gel(D, 2);
2816 4382 : if (!equali1(D)) y = gdiv(y, D);
2817 4382 : return y;
2818 : }
2819 : INLINE GEN
2820 5419599 : nf_get_zkprimpart(GEN nf)
2821 : {
2822 5419599 : GEN y = gel(nf,7);
2823 : /* test for old format of nf.zk: non normalized */
2824 5419599 : if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
2825 5419599 : return y;
2826 : }
2827 : INLINE GEN
2828 5465323 : nf_get_zkden(GEN nf)
2829 : {
2830 5465323 : GEN y = gel(nf,7), D = gel(y,1);
2831 5465323 : if (typ(D) == t_POL) D = gel(D,2);
2832 : /* test for old format of nf.zk: non normalized */
2833 5465323 : if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
2834 5465323 : return D;
2835 : }
2836 : INLINE GEN
2837 9782858 : nf_get_invzk(GEN nf) { return gel(nf,8); }
2838 : INLINE void
2839 522482 : nf_get_sign(GEN nf, long *r1, long *r2)
2840 : {
2841 522482 : GEN x = gel(nf,2);
2842 522482 : *r1 = itou(gel(x,1));
2843 522482 : *r2 = itou(gel(x,2));
2844 522482 : }
2845 :
2846 : INLINE GEN
2847 2757632 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
2848 : INLINE GEN
2849 407116 : abgrp_get_no(GEN x) { return gel(x,1); }
2850 : INLINE GEN
2851 12235183 : abgrp_get_cyc(GEN x) { return gel(x,2); }
2852 : INLINE GEN
2853 1766677 : abgrp_get_gen(GEN x) { return gel(x,3); }
2854 : INLINE GEN
2855 9875390 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
2856 : INLINE GEN
2857 5380463 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
2858 : INLINE GEN
2859 8386 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
2860 : INLINE GEN
2861 3689996 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
2862 : INLINE GEN
2863 1681899 : bnf_get_gen(GEN bnf) { return abgrp_get_gen(bnf_get_clgp(bnf)); }
2864 : INLINE GEN
2865 648 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
2866 : INLINE GEN
2867 2877898 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
2868 : INLINE GEN
2869 1369339 : bnf_get_sunits(GEN bnf)
2870 1369339 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
2871 : INLINE GEN
2872 303613 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
2873 : INLINE long
2874 283031 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
2875 : INLINE GEN
2876 289112 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
2877 : INLINE GEN
2878 26300 : nfV_to_scalar_or_alg(GEN nf, GEN x)
2879 63442 : { pari_APPLY_same(nf_to_scalar_or_alg(nf, gel(x,i))) }
2880 : INLINE GEN
2881 25859 : bnf_get_fu(GEN bnf) {
2882 25859 : GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
2883 25859 : if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
2884 25859 : return nfV_to_scalar_or_alg(nf, vecslice(fu, 2, lg(fu)-1));
2885 : }
2886 :
2887 : INLINE GEN
2888 3710982 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
2889 : INLINE GEN
2890 2046122 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
2891 : INLINE GEN
2892 77126 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
2893 : INLINE GEN
2894 1399132 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
2895 : INLINE GEN
2896 2830388 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
2897 : INLINE GEN
2898 277494 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
2899 : INLINE GEN
2900 2522682 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
2901 : INLINE GEN
2902 70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
2903 : INLINE GEN
2904 7847 : bnr_get_gen(GEN bnr) {
2905 7847 : GEN G = bnr_get_clgp(bnr);
2906 7847 : if (lg(G) != 4)
2907 0 : pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
2908 7847 : return gel(G,3);
2909 : }
2910 :
2911 : /* localstar, used in gchar */
2912 : INLINE GEN
2913 54144 : locs_get_cyc(GEN locs) { return gel(locs,1); }
2914 : INLINE GEN
2915 195755 : locs_get_Lsprk(GEN locs) { return gel(locs,2); }
2916 : INLINE GEN
2917 1288 : locs_get_Lgenfil(GEN locs) { return gel(locs,3); }
2918 : INLINE GEN
2919 8225 : locs_get_mod(GEN locs) { return gel(locs,4); }
2920 : /* pr dividing the modulus N of locs, 0 <= i < v_pr(N)
2921 : * return a t_MAT whose columns are the logs
2922 : * of generators of U_i(pr)/U_{i+1}(pr). */
2923 : INLINE GEN
2924 1288 : locs_get_famod(GEN locs) { return gmael(locs,4,1); }
2925 : INLINE GEN
2926 211534 : locs_get_m_infty(GEN locs) { return gmael(locs,4,2); }
2927 :
2928 : /* G a grossenchar group */
2929 : INLINE GEN
2930 34731 : gchar_get_basis(GEN gc) { return gel(gc, 1); }
2931 : INLINE GEN
2932 207685 : gchar_get_bnf(GEN gc) { return gel(gc, 2); }
2933 : INLINE GEN
2934 247942 : gchar_get_nf(GEN gc) { return gel(gc, 3); }
2935 : INLINE GEN
2936 286343 : gchar_get_zm(GEN gc) { return gel(gc, 4); }
2937 : INLINE GEN
2938 8225 : gchar_get_mod(GEN gc) { return locs_get_mod(gchar_get_zm(gc)); }
2939 : INLINE GEN
2940 4095 : gchar_get_modP(GEN gc) { return gmael(gchar_get_mod(gc),1,1); }
2941 : INLINE GEN
2942 38371 : gchar_get_S(GEN gc) { return gel(gc, 5); }
2943 : INLINE GEN
2944 192479 : gchar_get_DLdata(GEN gc) { return gel(gc, 6); }
2945 : INLINE GEN
2946 1996 : gchar_get_sfu(GEN gc) { return gel(gc, 7); }
2947 : INLINE GEN
2948 16535 : gchar_get_cyc(GEN gc) { return gel(gc, 9); }
2949 : INLINE GEN
2950 0 : gchar_get_hnf(GEN gc) { return gmael(gc, 10, 1); }
2951 : INLINE GEN
2952 0 : gchar_get_U(GEN gc) { return gmael(gc, 10, 2); }
2953 : INLINE GEN
2954 11096 : gchar_get_Ui(GEN gc) { return gmael(gc, 10, 3); }
2955 : INLINE GEN
2956 3823 : gchar_get_m0(GEN gc) { return gel(gc, 11); }
2957 : INLINE GEN
2958 5002 : gchar_get_u0(GEN gc) { return gel(gc, 12); }
2959 : INLINE long
2960 17683 : gchar_get_r1(GEN gc) { return nf_get_r1(gchar_get_nf(gc)); }
2961 : INLINE long
2962 623 : gchar_get_r2(GEN gc) { return nf_get_r2(gchar_get_nf(gc)); }
2963 : INLINE GEN
2964 52520 : gchar_get_loccyc(GEN gc) { return locs_get_cyc(gchar_get_zm(gc)); }
2965 : INLINE long
2966 38876 : gchar_get_nc(GEN gc) { return lg(gchar_get_loccyc(gc))-1; }
2967 : INLINE long
2968 38371 : gchar_get_ns(GEN gc) { return lg(gchar_get_S(gc))-1; }
2969 : INLINE long
2970 2108 : gchar_get_nm(GEN gc) { return lg(gchar_get_basis(gc))-1; }
2971 : INLINE long
2972 4011 : gchar_get_evalprec(GEN gc) { return gmael(gc, 8, 1)[1]; }
2973 : INLINE long
2974 19427 : gchar_get_prec(GEN gc) { return gmael(gc, 8, 1)[2]; }
2975 : INLINE long
2976 13854 : gchar_get_nfprec(GEN gc) { return gmael(gc, 8, 1)[3]; }
2977 : INLINE void
2978 2086 : gchar_set_evalprec(GEN gc, long prec) { gmael(gc, 8, 1)[1] = prec; }
2979 : INLINE void
2980 1470 : gchar_set_prec(GEN gc, long prec) { gmael(gc, 8, 1)[2] = prec; }
2981 : INLINE void
2982 1470 : gchar_copy_precs(GEN gc, GEN gc2)
2983 : {
2984 1470 : gel(gc2, 8) = shallowcopy(gel(gc,8));
2985 1470 : gmael(gc2, 8, 1) = shallowcopy(gmael(gc, 8, 1));
2986 1470 : }
2987 : INLINE void
2988 3466 : gchar_set_nfprec(GEN gc, long prec) { gmael(gc, 8, 1)[3] = prec; }
2989 : INLINE long
2990 322 : gchar_get_ntors(GEN gc) { return gmael(gc, 8, 2)[1]; }
2991 : INLINE long
2992 322 : gchar_get_nfree(GEN gc) { return gmael(gc, 8, 2)[2]; }
2993 : INLINE long
2994 2296 : gchar_get_nalg(GEN gc) { return gmael(gc, 8, 2)[3]; }
2995 : INLINE void
2996 1827 : gchar_set_basis(GEN gc, GEN m_inv) { gel(gc, 1) = m_inv; }
2997 : INLINE void
2998 2363 : gchar_set_nf(GEN gc, GEN nf) { gel(gc, 3) = nf; }
2999 : INLINE void
3000 637 : gchar_set_ntors(GEN gc, long n) { gmael(gc, 8, 2)[1] = n; }
3001 : INLINE void
3002 637 : gchar_set_nfree(GEN gc, long n) { gmael(gc, 8, 2)[2] = n; }
3003 : INLINE void
3004 637 : gchar_set_nalg(GEN gc, long n) { gmael(gc, 8, 2)[3] = n; }
3005 : INLINE void
3006 1827 : gchar_set_cyc(GEN gc, GEN cyc) { gel(gc, 9) = cyc; }
3007 : INLINE void
3008 637 : gchar_set_HUUi(GEN gc, GEN hnf, GEN U, GEN Ui) { gel(gc, 10) = mkvec3(hnf, U, Ui); }
3009 : INLINE void
3010 1190 : gchar_set_m0(GEN gc, GEN m0) { gel(gc, 11) = m0; }
3011 : INLINE void
3012 2142 : gchar_set_u0(GEN gc, GEN u0) { gel(gc, 12) = u0; }
3013 :
3014 : INLINE GEN
3015 1521921 : bid_get_mod(GEN bid) { return gel(bid,1); }
3016 : INLINE GEN
3017 72849 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
3018 : INLINE GEN
3019 41188 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
3020 : INLINE GEN
3021 5804874 : bid_get_grp(GEN bid) { return gel(bid,2); }
3022 : INLINE GEN
3023 2499065 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
3024 : INLINE GEN
3025 2201621 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
3026 : INLINE GEN
3027 2157304 : bid_get_MOD(GEN bid) { GEN y = gel(bid,4); return lg(y) == 4? gel(y,3): NULL; }
3028 : INLINE GEN
3029 2158039 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
3030 : INLINE GEN
3031 48594 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
3032 : INLINE GEN
3033 2397285 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
3034 : INLINE GEN
3035 3680282 : bid_get_U(GEN bid) { return gel(bid,5); }
3036 : INLINE GEN
3037 0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
3038 : INLINE GEN
3039 5740019 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
3040 : INLINE GEN
3041 0 : bid_get_gen_nocheck(GEN bid) { return abgrp_get_gen(bid_get_grp(bid)); }
3042 : INLINE GEN
3043 64274 : bid_get_gen(GEN bid) {
3044 64274 : GEN G = bid_get_grp(bid);
3045 64274 : if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
3046 64274 : return abgrp_get_gen(G);
3047 : }
3048 :
3049 : INLINE GEN
3050 38658755 : znstar_get_N(GEN G) { return gmael(G,1,1); }
3051 : INLINE GEN
3052 7334854 : znstar_get_faN(GEN G) { return gel(G,3); }
3053 : INLINE GEN
3054 14 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
3055 : INLINE GEN
3056 262088 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
3057 : INLINE GEN
3058 16150 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
3059 : INLINE GEN
3060 7982136 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
3061 : INLINE GEN
3062 3290533 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
3063 : INLINE GEN
3064 46788 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
3065 : INLINE GEN
3066 176107 : znstar_get_U(GEN G) { return gel(G,5); }
3067 : INLINE GEN
3068 2869070 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
3069 : INLINE GEN
3070 36428 : gal_get_pol(GEN gal) { return gel(gal,1); }
3071 : INLINE GEN
3072 6118 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
3073 : INLINE GEN
3074 91 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
3075 : INLINE GEN
3076 23870 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
3077 : INLINE GEN
3078 32844 : gal_get_roots(GEN gal) { return gel(gal,3); }
3079 : INLINE GEN
3080 27874 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
3081 : INLINE GEN
3082 27867 : gal_get_den(GEN gal) { return gel(gal,5); }
3083 : INLINE GEN
3084 80528 : gal_get_group(GEN gal) { return gel(gal,6); }
3085 : INLINE GEN
3086 15624 : gal_get_gen(GEN gal) { return gel(gal,7); }
3087 : INLINE GEN
3088 6713 : gal_get_orders(GEN gal) { return gel(gal,8); }
3089 :
3090 : /* assume rnf a genuine rnf */
3091 : INLINE long
3092 2506335 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
3093 : INLINE long
3094 21455 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
3095 : INLINE long
3096 1392746 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
3097 : INLINE GEN
3098 1386 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
3099 : INLINE GEN
3100 1225 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
3101 : INLINE GEN
3102 1428 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
3103 : INLINE GEN
3104 786086 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
3105 : INLINE GEN
3106 7245 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
3107 : INLINE GEN
3108 327628 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
3109 : INLINE GEN
3110 2845192 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
3111 : INLINE GEN
3112 357 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
3113 : INLINE GEN
3114 105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
3115 : INLINE GEN
3116 2849 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
3117 : INLINE long
3118 574 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
3119 : INLINE GEN
3120 255354 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
3121 : INLINE long
3122 4886 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
3123 : INLINE GEN
3124 4830 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
3125 : INLINE GEN
3126 135659 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
3127 : INLINE GEN
3128 1225 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
3129 :
3130 : INLINE GEN
3131 139130 : idealred(GEN nf, GEN id) { return idealred0(nf, id, NULL); }
3132 :
3133 : INLINE GEN
3134 5427 : idealchineseinit(GEN nf, GEN x)
3135 5427 : { return idealchinese(nf,x,NULL); }
3136 :
3137 : /*******************************************************************/
3138 : /* */
3139 : /* CLOSURES */
3140 : /* */
3141 : /*******************************************************************/
3142 333258083 : INLINE long closure_arity(GEN C) { return ((ulong)C[1])&ARITYBITS; }
3143 46077092 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
3144 282705339 : INLINE const char *closure_codestr(GEN C) { return GSTR(gel(C,2))-1; }
3145 28 : INLINE GEN closure_get_code(GEN C) { return gel(C,2); }
3146 282705339 : INLINE GEN closure_get_oper(GEN C) { return gel(C,3); }
3147 282691941 : INLINE GEN closure_get_data(GEN C) { return gel(C,4); }
3148 13674 : INLINE GEN closure_get_dbg(GEN C) { return gel(C,5); }
3149 37178 : INLINE GEN closure_get_text(GEN C) { return gel(C,6); }
3150 14757962 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
3151 :
3152 : /*******************************************************************/
3153 : /* */
3154 : /* ERRORS */
3155 : /* */
3156 : /*******************************************************************/
3157 : INLINE long
3158 439305 : err_get_num(GEN e) { return e[1]; }
3159 : INLINE GEN
3160 294 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
3161 :
3162 : INLINE void
3163 14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
3164 : INLINE void
3165 21 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
3166 : INLINE void
3167 91 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
3168 : INLINE void
3169 718 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
3170 : INLINE void
3171 0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
3172 : INLINE void
3173 36 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
3174 : INLINE void
3175 98 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
3176 : INLINE void
3177 539 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
3178 : INLINE void
3179 20032 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
3180 : INLINE void
3181 63 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
3182 : INLINE void
3183 2876 : 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); }
3184 : INLINE void
3185 214 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
3186 : INLINE void
3187 0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
3188 : INLINE void
3189 406 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
3190 : INLINE void
3191 156 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
3192 : INLINE void
3193 238 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
3194 : INLINE void
3195 0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
3196 : INLINE void
3197 98 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
3198 : INLINE void
3199 1316 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
3200 : INLINE void
3201 70 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
3202 : INLINE void
3203 7084 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
3204 : INLINE void
3205 15370 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
3206 : INLINE void
3207 3493 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
3208 : INLINE void
3209 371 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
3210 : INLINE void
3211 245 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
3212 245 : { pari_err(e_PRIORITY, f,x,op,v); }
3213 :
|