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 : /* MALLOC/FREE WRAPPERS */
17 : /*********************************************************************/
18 : #define BLOCK_SIGALRM_START \
19 : { \
20 : int block=PARI_SIGINT_block; \
21 : PARI_SIGINT_block = 2; \
22 : MT_SIGINT_BLOCK(block);
23 :
24 : #define BLOCK_SIGINT_START \
25 : { \
26 : int block=PARI_SIGINT_block; \
27 : PARI_SIGINT_block = 1; \
28 : MT_SIGINT_BLOCK(block);
29 :
30 : #define BLOCK_SIGINT_END \
31 : PARI_SIGINT_block = block; \
32 : MT_SIGINT_UNBLOCK(block); \
33 : if (!block && PARI_SIGINT_pending) \
34 : { \
35 : int sig = PARI_SIGINT_pending; \
36 : PARI_SIGINT_pending = 0; \
37 : raise(sig); \
38 : } \
39 : }
40 :
41 : /*******************************************************************/
42 : /* */
43 : /* CONSTRUCTORS */
44 : /* */
45 : /*******************************************************************/
46 : #define retmkfrac(x,y)\
47 : do { GEN _v = cgetg(3, t_FRAC);\
48 : gel(_v,1) = (x);\
49 : gel(_v,2) = (y); return _v; } while(0)
50 : #define retmkrfrac(x,y)\
51 : do { GEN _v = cgetg(3, t_RFRAC);\
52 : gel(_v,1) = (x);\
53 : gel(_v,2) = (y); return _v; } while(0)
54 : #define retmkintmod(x,y)\
55 : do { GEN _v = cgetg(3, t_INTMOD);\
56 : gel(_v,1) = (y);\
57 : gel(_v,2) = (x); return _v; } while(0)
58 : #define retmkcomplex(x,y)\
59 : do { GEN _v = cgetg(3, t_COMPLEX);\
60 : gel(_v,1) = (x);\
61 : gel(_v,2) = (y); return _v; } while(0)
62 : #define retmkpolmod(x,y)\
63 : do { GEN _v = cgetg(3, t_POLMOD);\
64 : gel(_v,1) = (y);\
65 : gel(_v,2) = (x); return _v; } while(0)
66 : #define retmkvec(x)\
67 : do { GEN _v = cgetg(2, t_VEC);\
68 : gel(_v,1) = (x); return _v; } while(0)
69 : #define retmkvec2(x,y)\
70 : do { GEN _v = cgetg(3, t_VEC);\
71 : gel(_v,1) = (x);\
72 : gel(_v,2) = (y); return _v; } while(0)
73 : #define retmkvec3(x,y,z)\
74 : do { GEN _v = cgetg(4, t_VEC);\
75 : gel(_v,1) = (x);\
76 : gel(_v,2) = (y);\
77 : gel(_v,3) = (z); return _v; } while(0)
78 : #define retmkqfb(x,y,z,d)\
79 : do { GEN _v = cgetg(5, t_QFB);\
80 : gel(_v,1) = (x);\
81 : gel(_v,2) = (y);\
82 : gel(_v,3) = (z);\
83 : gel(_v,4) = (d); return _v; } while(0)
84 : #define retmkquad(x,y,z)\
85 : do { GEN _v = cgetg(4, t_QUAD);\
86 : gel(_v,1) = (x);\
87 : gel(_v,2) = (y);\
88 : gel(_v,3) = (z); return _v; } while(0)
89 : #define retmkvec4(x,y,z,t)\
90 : do { GEN _v = cgetg(5, t_VEC);\
91 : gel(_v,1) = (x);\
92 : gel(_v,2) = (y);\
93 : gel(_v,3) = (z);\
94 : gel(_v,4) = (t); return _v; } while(0)
95 : #define retmkvec5(x,y,z,t,u)\
96 : do { GEN _v = cgetg(6, t_VEC);\
97 : gel(_v,1) = (x);\
98 : gel(_v,2) = (y);\
99 : gel(_v,3) = (z);\
100 : gel(_v,4) = (t);\
101 : gel(_v,5) = (u); return _v; } while(0)
102 : #define retmkcol(x)\
103 : do { GEN _v = cgetg(2, t_COL);\
104 : gel(_v,1) = (x); return _v; } while(0)
105 : #define retmkcol2(x,y)\
106 : do { GEN _v = cgetg(3, t_COL);\
107 : gel(_v,1) = (x);\
108 : gel(_v,2) = (y); return _v; } while(0)
109 : #define retmkcol3(x,y,z)\
110 : do { GEN _v = cgetg(4, t_COL);\
111 : gel(_v,1) = (x);\
112 : gel(_v,2) = (y);\
113 : gel(_v,3) = (z); return _v; } while(0)
114 : #define retmkcol4(x,y,z,t)\
115 : do { GEN _v = cgetg(5, t_COL);\
116 : gel(_v,1) = (x);\
117 : gel(_v,2) = (y);\
118 : gel(_v,3) = (z);\
119 : gel(_v,4) = (t); return _v; } while(0)
120 : #define retmkcol5(x,y,z,t,u)\
121 : do { GEN _v = cgetg(6, t_COL);\
122 : gel(_v,1) = (x);\
123 : gel(_v,2) = (y);\
124 : gel(_v,3) = (z);\
125 : gel(_v,4) = (t);\
126 : gel(_v,5) = (u); return _v; } while(0)
127 : #define retmkcol6(x,y,z,t,u,v)\
128 : do { GEN _v = cgetg(7, t_COL);\
129 : gel(_v,1) = (x);\
130 : gel(_v,2) = (y);\
131 : gel(_v,3) = (z);\
132 : gel(_v,4) = (t);\
133 : gel(_v,5) = (u);\
134 : gel(_v,6) = (v); return _v; } while(0)
135 : #define retmkmat(x)\
136 : do { GEN _v = cgetg(2, t_MAT);\
137 : gel(_v,1) = (x); return _v; } while(0)
138 : #define retmkmat2(x,y)\
139 : do { GEN _v = cgetg(3, t_MAT);\
140 : gel(_v,1) = (x);\
141 : gel(_v,2) = (y); return _v; } while(0)
142 : #define retmkmat3(x,y,z)\
143 : do { GEN _v = cgetg(4, t_MAT);\
144 : gel(_v,1) = (x);\
145 : gel(_v,2) = (y);\
146 : gel(_v,3) = (z); return _v; } while(0)
147 : #define retmkmat4(x,y,z,t)\
148 : do { GEN _v = cgetg(5, t_MAT);\
149 : gel(_v,1) = (x);\
150 : gel(_v,2) = (y);\
151 : gel(_v,3) = (z);\
152 : gel(_v,4) = (t); return _v; } while(0)
153 : #define retmkmat5(x,y,z,t,u)\
154 : do { GEN _v = cgetg(6, t_MAT);\
155 : gel(_v,1) = (x);\
156 : gel(_v,2) = (y);\
157 : gel(_v,3) = (z);\
158 : gel(_v,4) = (t);\
159 : gel(_v,5) = (u); return _v; } while(0)
160 :
161 : INLINE GEN
162 6554370 : mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
163 : INLINE GEN
164 1813 : mkintmodu(ulong x, ulong y) {
165 1813 : GEN v = cgetg(3,t_INTMOD);
166 1813 : gel(v,1) = utoipos(y);
167 1813 : gel(v,2) = utoi(x); return v;
168 : }
169 : INLINE GEN
170 2581123 : mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
171 : INLINE GEN
172 29408177 : mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
173 : INLINE GEN
174 1205620 : mkfracss(long x, long y) { retmkfrac(stoi(x),utoipos(y)); }
175 : /* q = n/d a t_FRAC or t_INT; recover (n,d) */
176 : INLINE void
177 18501 : Qtoss(GEN q, long *n, long *d)
178 : {
179 18501 : if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
180 5691 : else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
181 18501 : }
182 : INLINE GEN
183 769223 : sstoQ(long n, long d)
184 : {
185 : ulong r;
186 : long g, q;
187 769223 : if (!n)
188 : {
189 92267 : if (!d) pari_err_INV("sstoQ",gen_0);
190 92267 : return gen_0;
191 : }
192 676956 : if (d < 0) { d = -d; n = -n; }
193 676956 : if (d == 1) return stoi(n);
194 664034 : r = labs(n);
195 664034 : if (r == 1) retmkfrac(n > 0? gen_1: gen_m1, utoipos(d));
196 629972 : q = udivuu_rem(r, d, &r);
197 629972 : if (!r) return n > 0? utoipos(q): utoineg(q);
198 575379 : g = ugcd(d,r); /* gcd(n,d) */
199 575379 : if (g != 1) { n /= g; d /= g; }
200 575379 : retmkfrac(stoi(n), utoipos(d));
201 : }
202 :
203 : INLINE GEN
204 157848191 : uutoQ(ulong n, ulong d)
205 : {
206 : ulong r;
207 : long g, q;
208 157848191 : if (!n)
209 : {
210 9438 : if (!d) pari_err_INV("uutoQ",gen_0);
211 9438 : return gen_0;
212 : }
213 157838753 : if (d == 1) return utoipos(n);
214 157827658 : if (n == 1) retmkfrac(gen_1, utoipos(d));
215 157740417 : q = udivuu_rem(n,d,&r);
216 157740417 : if (!r) return utoipos(q);
217 5114797 : g = ugcd(d,r); /* gcd(n,d) */
218 5114797 : if (g != 1) { n /= g; d /= g; }
219 5114797 : retmkfrac(utoipos(n), utoipos(d));
220 : }
221 :
222 : INLINE GEN
223 3720507 : mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
224 : INLINE GEN
225 10310 : mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
226 10310 : gel(v,1) = x; gel(v,2) = y; return v; }
227 : INLINE GEN
228 7 : mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
229 7 : gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
230 : INLINE GEN
231 73473575 : mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
232 : INLINE GEN
233 825642 : gen_I(void) { return mkcomplex(gen_0, gen_1); }
234 : INLINE GEN
235 778681 : cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
236 : INLINE GEN
237 69132 : mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
238 69132 : gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
239 : /* vecsmall */
240 : INLINE GEN
241 117430881 : mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
242 : INLINE GEN
243 194457641 : mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
244 194432975 : v[1]=x; v[2]=y; return v; }
245 : INLINE GEN
246 93727214 : mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
247 93713005 : v[1]=x; v[2]=y; v[3]=z; return v; }
248 : INLINE GEN
249 13985055 : mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
250 13973325 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
251 : INLINE GEN
252 575298 : mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
253 575296 : v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
254 :
255 : INLINE GEN
256 16870238 : mkqfb(GEN x, GEN y, GEN z, GEN d) { retmkqfb(x,y,z,d); }
257 : /* vec */
258 : INLINE GEN
259 60123007 : mkvec(GEN x) { retmkvec(x); }
260 : INLINE GEN
261 147555248 : mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
262 : INLINE GEN
263 97334931 : mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
264 : INLINE GEN
265 5912497 : mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
266 : INLINE GEN
267 14569324 : mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
268 : INLINE GEN
269 126 : mkvecs(long x) { retmkvec(stoi(x)); }
270 : INLINE GEN
271 211201 : mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
272 : INLINE GEN
273 488650 : mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
274 : INLINE GEN
275 21 : mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
276 : INLINE GEN
277 35854 : mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
278 : INLINE GEN
279 125021 : mkvec2copy(GEN x, GEN y) {
280 125021 : GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
281 : /* col */
282 : INLINE GEN
283 5431249 : mkcol(GEN x) { retmkcol(x); }
284 : INLINE GEN
285 53779599 : mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
286 : INLINE GEN
287 85543 : mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
288 : INLINE GEN
289 0 : mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
290 : INLINE GEN
291 101 : mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
292 : INLINE GEN
293 11993 : mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
294 : INLINE GEN
295 36589 : mkcols(long x) { retmkcol(stoi(x)); }
296 : INLINE GEN
297 2761682 : mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
298 : INLINE GEN
299 294 : mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
300 : INLINE GEN
301 0 : mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
302 : INLINE GEN
303 29038 : mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
304 : /* mat */
305 : INLINE GEN
306 3764219 : mkmat(GEN x) { retmkmat(x); }
307 : INLINE GEN
308 36562210 : mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
309 : INLINE GEN
310 16485 : mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
311 : INLINE GEN
312 0 : mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
313 : INLINE GEN
314 0 : mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
315 : INLINE GEN
316 118046 : mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
317 : INLINE GEN
318 0 : mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
319 : INLINE GEN
320 199451 : mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
321 : INLINE GEN
322 102741 : mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
323 : INLINE long
324 314336 : inf_get_sign(GEN x) { return signe(gel(x,1)); }
325 : INLINE GEN
326 12495 : mkmat22s(long a, long b, long c, long d) {retmkmat2(mkcol2s(a,c),mkcol2s(b,d));}
327 : INLINE GEN
328 4467262 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
329 :
330 : /* pol */
331 : INLINE GEN
332 2436268 : pol_x(long v) {
333 2436268 : GEN p = cgetg(4, t_POL);
334 2436298 : p[1] = evalsigne(1)|evalvarn(v);
335 2436298 : gel(p,2) = gen_0;
336 2436298 : gel(p,3) = gen_1; return p;
337 : }
338 : /* x^n, assume n >= 0 */
339 : INLINE GEN
340 2155396 : pol_xn(long n, long v) {
341 2155396 : long i, a = n+2;
342 2155396 : GEN p = cgetg(a+1, t_POL);
343 2155397 : p[1] = evalsigne(1)|evalvarn(v);
344 5079455 : for (i = 2; i < a; i++) gel(p,i) = gen_0;
345 2155397 : gel(p,a) = gen_1; return p;
346 : }
347 : /* x^n, no assumption on n */
348 : INLINE GEN
349 287 : pol_xnall(long n, long v)
350 : {
351 287 : if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
352 280 : return pol_xn(n, v);
353 : }
354 : /* x^n, assume n >= 0 */
355 : INLINE GEN
356 210 : polxn_Flx(long n, long sv) {
357 210 : long i, a = n+2;
358 210 : GEN p = cgetg(a+1, t_VECSMALL);
359 210 : p[1] = sv;
360 1022 : for (i = 2; i < a; i++) p[i] = 0;
361 210 : p[a] = 1; return p;
362 : }
363 : INLINE GEN
364 3200601 : pol_1(long v) {
365 3200601 : GEN p = cgetg(3, t_POL);
366 3200593 : p[1] = evalsigne(1)|evalvarn(v);
367 3200593 : gel(p,2) = gen_1; return p;
368 : }
369 : INLINE GEN
370 33500467 : pol_0(long v)
371 : {
372 33500467 : GEN x = cgetg(2,t_POL);
373 33500458 : x[1] = evalvarn(v); return x;
374 : }
375 : #define retconst_vec(n,x)\
376 : do { long _i, _n = (n);\
377 : GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
378 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
379 : return _v; } while(0)
380 : INLINE GEN
381 201138896 : const_vec(long n, GEN x) { retconst_vec(n, x); }
382 : #define retconst_col(n,x)\
383 : do { long _i, _n = (n);\
384 : GEN _v = cgetg(_n+1, t_COL), _x = (x);\
385 : for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
386 : return _v; } while(0)
387 : INLINE GEN
388 37137862 : const_col(long n, GEN x) { retconst_col(n, x); }
389 : INLINE GEN
390 16730756 : const_vecsmall(long n, long c)
391 : {
392 : long i;
393 16730756 : GEN V = cgetg(n+1,t_VECSMALL);
394 593416372 : for(i=1;i<=n;i++) V[i] = c;
395 16730760 : return V;
396 : }
397 :
398 : /*** ZERO ***/
399 : /* O(p^e) */
400 : INLINE GEN
401 850658 : zeropadic(GEN p, long e)
402 : {
403 850658 : GEN y = cgetg(5,t_PADIC);
404 850634 : gel(y,4) = gen_0;
405 850634 : gel(y,3) = gen_1;
406 850634 : gel(y,2) = icopy(p);
407 850577 : y[1] = evalvalp(e) | _evalprecp(0);
408 850569 : return y;
409 : }
410 : INLINE GEN
411 10220 : zeropadic_shallow(GEN p, long e)
412 : {
413 10220 : GEN y = cgetg(5,t_PADIC);
414 10220 : gel(y,4) = gen_0;
415 10220 : gel(y,3) = gen_1;
416 10220 : gel(y,2) = p;
417 10220 : y[1] = evalvalp(e) | _evalprecp(0);
418 10220 : return y;
419 : }
420 : /* O(pol_x(v)^e) */
421 : INLINE GEN
422 135618 : zeroser(long v, long e)
423 : {
424 135618 : GEN x = cgetg(2, t_SER);
425 135618 : x[1] = evalvalp(e) | evalvarn(v); return x;
426 : }
427 : INLINE int
428 9766057 : ser_isexactzero(GEN x)
429 : {
430 9766057 : if (!signe(x)) switch(lg(x))
431 : {
432 130802 : case 2: return 1;
433 3640 : case 3: return isexactzero(gel(x,2));
434 : }
435 9631615 : return 0;
436 : }
437 : /* 0 * pol_x(v) */
438 : INLINE GEN
439 14649135 : zeropol(long v) { return pol_0(v); }
440 : /* vector(n) */
441 : INLINE GEN
442 67398940 : zerocol(long n)
443 : {
444 67398940 : GEN y = cgetg(n+1,t_COL);
445 521382702 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
446 67399217 : return y;
447 : }
448 : /* vectorv(n) */
449 : INLINE GEN
450 25522394 : zerovec(long n)
451 : {
452 25522394 : GEN y = cgetg(n+1,t_VEC);
453 332078546 : long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
454 25522383 : return y;
455 : }
456 : /* matrix(m, n) */
457 : INLINE GEN
458 91579 : zeromat(long m, long n)
459 : {
460 91579 : GEN y = cgetg(n+1,t_MAT);
461 91579 : GEN v = zerocol(m);
462 297272 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
463 91580 : return y;
464 : }
465 : /* = zero_zx, sv is a evalvarn()*/
466 : INLINE GEN
467 1147600 : zero_Flx(long sv) { return pol0_Flx(sv); }
468 : INLINE GEN
469 51913091 : zero_Flv(long n)
470 : {
471 51913091 : GEN y = cgetg(n+1,t_VECSMALL);
472 1029353449 : long i; for (i=1; i<=n; i++) y[i] = 0;
473 51913007 : return y;
474 : }
475 : /* matrix(m, n) */
476 : INLINE GEN
477 2424425 : zero_Flm(long m, long n)
478 : {
479 2424425 : GEN y = cgetg(n+1,t_MAT);
480 2424394 : GEN v = zero_Flv(m);
481 15448619 : long i; for (i=1; i<=n; i++) gel(y,i) = v;
482 2424413 : return y;
483 : }
484 : /* matrix(m, n) */
485 : INLINE GEN
486 221878 : zero_Flm_copy(long m, long n)
487 : {
488 221878 : GEN y = cgetg(n+1,t_MAT);
489 2263005 : long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
490 221873 : return y;
491 : }
492 :
493 : INLINE GEN
494 2933737 : zero_F2v(long m)
495 : {
496 2933737 : long l = nbits2nlong(m);
497 2933725 : GEN v = zero_Flv(l+1);
498 2933718 : v[1] = m;
499 2933718 : return v;
500 : }
501 :
502 : INLINE GEN
503 0 : zero_F2m(long m, long n)
504 : {
505 : long i;
506 0 : GEN M = cgetg(n+1, t_MAT);
507 0 : GEN v = zero_F2v(m);
508 0 : for (i = 1; i <= n; i++)
509 0 : gel(M,i) = v;
510 0 : return M;
511 : }
512 :
513 :
514 : INLINE GEN
515 842979 : zero_F2m_copy(long m, long n)
516 : {
517 : long i;
518 842979 : GEN M = cgetg(n+1, t_MAT);
519 2007652 : for (i = 1; i <= n; i++)
520 1164673 : gel(M,i)= zero_F2v(m);
521 842979 : return M;
522 : }
523 :
524 : /* matrix(m, n) */
525 : INLINE GEN
526 4949019 : zeromatcopy(long m, long n)
527 : {
528 4949019 : GEN y = cgetg(n+1,t_MAT);
529 25117484 : long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
530 4949056 : return y;
531 : }
532 :
533 : INLINE GEN
534 18235 : zerovec_block(long len)
535 : {
536 : long i;
537 18235 : GEN blk = cgetg_block(len + 1, t_VEC);
538 601755 : for (i = 1; i <= len; ++i)
539 583520 : gel(blk, i) = gen_0;
540 18235 : return blk;
541 : }
542 :
543 : /* i-th vector in the standard basis */
544 : INLINE GEN
545 4528582 : col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
546 : INLINE GEN
547 1690643 : vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
548 : INLINE GEN
549 42 : F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
550 : INLINE GEN
551 66446 : vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
552 : INLINE GEN
553 1141531 : Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
554 :
555 : INLINE GEN
556 18156638 : shallowcopy(GEN x)
557 18156638 : { return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
558 :
559 : /* routines for naive growarrays */
560 : INLINE GEN
561 11149386 : vectrunc_init(long l)
562 : {
563 11149386 : GEN z = new_chunk(l);
564 11149250 : z[0] = evaltyp(t_VEC) | _evallg(1); return z;
565 : }
566 : INLINE GEN
567 348894 : coltrunc_init(long l)
568 : {
569 348894 : GEN z = new_chunk(l);
570 348894 : z[0] = evaltyp(t_COL) | _evallg(1); return z;
571 : }
572 : INLINE void
573 268045727 : lg_increase(GEN x) { x[0]++; }
574 : INLINE void
575 13863566 : vectrunc_append(GEN x, GEN t) { gel(x, lg(x)) = t; lg_increase(x); }
576 : INLINE void
577 10864 : vectrunc_append_batch(GEN x, GEN y)
578 : {
579 10864 : long i, l = lg(x), ly = lg(y);
580 10864 : GEN z = x + l-1;
581 36358 : for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
582 10864 : setlg(x, l+ly-1);
583 10864 : }
584 : INLINE GEN
585 106077956 : vecsmalltrunc_init(long l)
586 : {
587 106077956 : GEN z = new_chunk(l);
588 105978107 : z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
589 : }
590 : INLINE void
591 36435461 : vecsmalltrunc_append(GEN x, long t) { x[ lg(x) ] = t; lg_increase(x); }
592 :
593 : /*******************************************************************/
594 : /* */
595 : /* STRING HASH FUNCTIONS */
596 : /* */
597 : /*******************************************************************/
598 : INLINE ulong
599 2522630 : hash_str(const char *str)
600 : {
601 2522630 : ulong hash = 5381UL, c;
602 26032671 : while ( (c = (ulong)*str++) )
603 23510041 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
604 2522630 : return hash;
605 : }
606 : INLINE ulong
607 60459616 : hash_str_len(const char *str, long len)
608 : {
609 60459616 : ulong hash = 5381UL;
610 : long i;
611 256214956 : for (i = 0; i < len; i++)
612 : {
613 195755340 : ulong c = (ulong)*str++;
614 195755340 : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
615 : }
616 60459616 : return hash;
617 : }
618 :
619 : /*******************************************************************/
620 : /* */
621 : /* VEC / COL / VECSMALL */
622 : /* */
623 : /*******************************************************************/
624 : /* shallow*/
625 : INLINE GEN
626 11466 : vec_shorten(GEN v, long n)
627 : {
628 11466 : GEN V = cgetg(n+1, t_VEC);
629 : long i;
630 12845 : for(i = 1; i <= n; i++) gel(V,i) = gel(v,i);
631 11466 : return V;
632 : }
633 : /* shallow*/
634 : INLINE GEN
635 19612 : vec_lengthen(GEN v, long n)
636 : {
637 19612 : GEN V = cgetg(n+1, t_VEC);
638 19612 : long i, l = lg(v);
639 16748521 : for(i = 1; i < l; i++) gel(V,i) = gel(v,i);
640 19612 : return V;
641 : }
642 : /* shallow*/
643 : INLINE GEN
644 3419593 : vec_append(GEN V, GEN s)
645 : {
646 3419593 : long i, l2 = lg(V);
647 3419593 : GEN res = cgetg(l2+1, typ(V));
648 10705369 : for (i = 1; i < l2; i++) gel(res, i) = gel(V,i);
649 3419581 : gel(res,l2) = s; return res;
650 : }
651 : /* shallow*/
652 : INLINE GEN
653 272210 : vec_prepend(GEN v, GEN s)
654 : {
655 272210 : long i, l = lg(v);
656 272210 : GEN w = cgetg(l+1, typ(v));
657 272210 : gel(w,1) = s;
658 576596 : for (i = 2; i <= l; i++) gel(w,i) = gel(v,i-1);
659 272210 : return w;
660 : }
661 : /* shallow*/
662 : INLINE GEN
663 0 : vec_setconst(GEN v, GEN x)
664 : {
665 0 : long i, l = lg(v);
666 0 : for (i = 1; i < l; i++) gel(v,i) = x;
667 0 : return v;
668 : }
669 : INLINE GEN
670 51945 : vecsmall_shorten(GEN v, long n)
671 : {
672 51945 : GEN V = cgetg(n+1,t_VECSMALL);
673 : long i;
674 628556 : for(i = 1; i <= n; i++) V[i] = v[i];
675 51944 : return V;
676 : }
677 : INLINE GEN
678 469 : vecsmall_lengthen(GEN v, long n)
679 : {
680 469 : long i, l = lg(v);
681 469 : GEN V = cgetg(n+1,t_VECSMALL);
682 24976 : for(i = 1; i < l; i++) V[i] = v[i];
683 469 : return V;
684 : }
685 :
686 : INLINE GEN
687 4238876 : vec_to_vecsmall(GEN x)
688 19626542 : { pari_APPLY_long(itos(gel(x,i))) }
689 : INLINE GEN
690 491841 : vecsmall_to_vec(GEN x)
691 6229594 : { pari_APPLY_type(t_VEC, stoi(x[i])) }
692 : INLINE GEN
693 2457 : vecsmall_to_vec_inplace(GEN z)
694 : {
695 2457 : long i, l = lg(z);
696 76867 : for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
697 2457 : settyp(z, t_VEC); return z;
698 : }
699 : INLINE GEN
700 6947916 : vecsmall_to_col(GEN x)
701 57608256 : { pari_APPLY_type(t_COL, stoi(x[i])) }
702 :
703 : INLINE int
704 7071892 : vecsmall_lexcmp(GEN x, GEN y)
705 : {
706 : long lx,ly,l,i;
707 7071892 : lx = lg(x);
708 7071892 : ly = lg(y); l = minss(lx,ly);
709 30507687 : for (i=1; i<l; i++)
710 28880852 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
711 1626835 : if (lx == ly) return 0;
712 30578 : return (lx < ly)? -1 : 1;
713 : }
714 :
715 : INLINE int
716 108088071 : vecsmall_prefixcmp(GEN x, GEN y)
717 : {
718 108088071 : long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
719 542691324 : for (i=1; i<l; i++)
720 519806035 : if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
721 22885289 : return 0;
722 : }
723 :
724 : /*Can be used on t_VEC, but coeffs not gcopy-ed*/
725 : INLINE GEN
726 182889 : vecsmall_prepend(GEN V, long s)
727 : {
728 182889 : long i, l2 = lg(V);
729 182889 : GEN res = cgetg(l2+1, typ(V));
730 182889 : res[1] = s;
731 613249 : for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
732 182889 : return res;
733 : }
734 :
735 : INLINE GEN
736 3617352 : vecsmall_append(GEN V, long s)
737 : {
738 3617352 : long i, l2 = lg(V);
739 3617352 : GEN res = cgetg(l2+1, t_VECSMALL);
740 5455015 : for (i = 1; i < l2; ++i) res[i] = V[i];
741 3617351 : res[l2] = s; return res;
742 : }
743 :
744 : INLINE GEN
745 1813742 : vecsmall_concat(GEN u, GEN v)
746 : {
747 1813742 : long i, l1 = lg(u)-1, l2 = lg(v)-1;
748 1813742 : GEN res = cgetg(l1+l2+1, t_VECSMALL);
749 14788041 : for (i = 1; i <= l1; ++i) res[i] = u[i];
750 17954793 : for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
751 1813741 : return res;
752 : }
753 :
754 : /* return the number of indices where u and v are equal */
755 : INLINE long
756 0 : vecsmall_coincidence(GEN u, GEN v)
757 : {
758 0 : long i, s = 0, l = minss(lg(u),lg(v));
759 0 : for(i=1; i<l; i++)
760 0 : if(u[i] == v[i]) s++;
761 0 : return s;
762 : }
763 :
764 : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
765 : INLINE long
766 42 : vecsmall_isin(GEN v, long x)
767 : {
768 42 : long i, l = lg(v);
769 68 : for (i = 1; i < l; i++)
770 52 : if (v[i] == x) return i;
771 16 : return 0;
772 : }
773 :
774 : INLINE long
775 84 : vecsmall_pack(GEN V, long base, long mod)
776 : {
777 84 : long i, s = 0;
778 273 : for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
779 84 : return s;
780 : }
781 :
782 : INLINE long
783 14 : vecsmall_indexmax(GEN x)
784 : {
785 14 : long i, i0 = 1, t = x[1], lx = lg(x);
786 56 : for (i=2; i<lx; i++)
787 42 : if (x[i] > t) t = x[i0=i];
788 14 : return i0;
789 : }
790 :
791 : INLINE long
792 674958 : vecsmall_max(GEN x)
793 : {
794 674958 : long i, t = x[1], lx = lg(x);
795 2028249 : for (i=2; i<lx; i++)
796 1353291 : if (x[i] > t) t = x[i];
797 674958 : return t;
798 : }
799 :
800 : INLINE long
801 14 : vecsmall_indexmin(GEN x)
802 : {
803 14 : long i, i0 = 1, t = x[1], lx =lg(x);
804 56 : for (i=2; i<lx; i++)
805 42 : if (x[i] < t) t = x[i0=i];
806 14 : return i0;
807 : }
808 :
809 : INLINE long
810 1435 : vecsmall_min(GEN x)
811 : {
812 1435 : long i, t = x[1], lx =lg(x);
813 10045 : for (i=2; i<lx; i++)
814 8610 : if (x[i] < t) t = x[i];
815 1435 : return t;
816 : }
817 :
818 : INLINE int
819 34671394 : ZV_isscalar(GEN x)
820 : {
821 34671394 : long l = lg(x);
822 59683150 : while (--l > 1)
823 59236146 : if (signe(gel(x, l))) return 0;
824 447004 : return 1;
825 : }
826 : INLINE int
827 36350183 : QV_isscalar(GEN x)
828 : {
829 36350183 : long lx = lg(x),i;
830 43323682 : for (i=2; i<lx; i++)
831 41646740 : if (!isintzero(gel(x, i))) return 0;
832 1676942 : return 1;
833 : }
834 : INLINE int
835 9357 : RgV_isscalar(GEN x)
836 : {
837 9357 : long lx = lg(x),i;
838 11196 : for (i=2; i<lx; i++)
839 11154 : if (!gequal0(gel(x, i))) return 0;
840 42 : return 1;
841 : }
842 : INLINE int
843 0 : RgX_isscalar(GEN x)
844 : {
845 : long i;
846 0 : for (i=lg(x)-1; i>2; i--)
847 0 : if (!gequal0(gel(x, i))) return 0;
848 0 : return 1;
849 : }
850 : INLINE long
851 25077893 : RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
852 : INLINE GEN
853 140 : RgX_to_RgV(GEN x, long N) { x = RgX_to_RgC(x, N); settyp(x, t_VEC); return x; }
854 :
855 : INLINE int
856 63677 : RgX_is_rational(GEN x)
857 : {
858 : long i;
859 296441 : for (i = lg(x)-1; i > 1; i--)
860 281349 : if (!is_rational_t(typ(gel(x,i)))) return 0;
861 15092 : return 1;
862 : }
863 : INLINE int
864 11683684 : RgX_is_ZX(GEN x)
865 : {
866 : long i;
867 49960922 : for (i = lg(x)-1; i > 1; i--)
868 38334819 : if (typ(gel(x,i)) != t_INT) return 0;
869 11626103 : return 1;
870 : }
871 : INLINE int
872 333359 : RgX_is_QX(GEN x)
873 : {
874 333359 : long k = lg(x)-1;
875 1232150 : for ( ; k>1; k--)
876 899050 : if (!is_rational_t(typ(gel(x,k)))) return 0;
877 333100 : return 1;
878 : }
879 : INLINE int
880 2466555 : RgX_is_monomial(GEN x)
881 : {
882 : long i;
883 2466555 : if (!signe(x)) return 0;
884 6082646 : for (i=lg(x)-2; i>1; i--)
885 3686440 : if (!isexactzero(gel(x,i))) return 0;
886 2396206 : return 1;
887 : }
888 : INLINE int
889 30108516 : RgV_is_ZV(GEN x)
890 : {
891 : long i;
892 120311763 : for (i = lg(x)-1; i > 0; i--)
893 90232318 : if (typ(gel(x,i)) != t_INT) return 0;
894 30079445 : return 1;
895 : }
896 : INLINE int
897 110264 : RgV_is_QV(GEN x)
898 : {
899 : long i;
900 527926 : for (i = lg(x)-1; i > 0; i--)
901 420617 : if (!is_rational_t(typ(gel(x,i)))) return 0;
902 107309 : return 1;
903 : }
904 : INLINE long
905 108892 : RgV_isin_i(GEN v, GEN x, long n)
906 : {
907 : long i;
908 1031205 : for (i = 1; i <= n; i++)
909 1025416 : if (gequal(gel(v,i), x)) return i;
910 5789 : return 0;
911 : }
912 : INLINE long
913 108892 : RgV_isin(GEN v, GEN x) { return RgV_isin_i(v, x, lg(v)-1); }
914 :
915 : /********************************************************************/
916 : /** **/
917 : /** Dynamic arrays implementation **/
918 : /** **/
919 : /********************************************************************/
920 : INLINE void **
921 1243750711 : pari_stack_base(pari_stack *s) { return s->data; }
922 :
923 : INLINE void
924 3870462 : pari_stack_init(pari_stack *s, size_t size, void **data)
925 : {
926 3870462 : s->data = data;
927 3870462 : *data = NULL;
928 3870462 : s->n = 0;
929 3870462 : s->alloc = 0;
930 3870462 : s->size = size;
931 3870462 : }
932 :
933 : INLINE void
934 1240172917 : pari_stack_alloc(pari_stack *s, long nb)
935 : {
936 1240172917 : void **sdat = pari_stack_base(s);
937 1240092253 : long alloc = s->alloc;
938 1240092253 : if (s->n+nb <= alloc) return;
939 1486151 : if (!alloc)
940 1389429 : alloc = nb;
941 : else
942 : {
943 197403 : while (s->n+nb > alloc) alloc <<= 1;
944 : }
945 1486151 : pari_realloc_ip(sdat,alloc*s->size);
946 1496465 : s->alloc = alloc;
947 : }
948 :
949 : INLINE long
950 1153543571 : pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
951 :
952 : INLINE void
953 3608092 : pari_stack_delete(pari_stack *s)
954 : {
955 3608092 : void **sdat = pari_stack_base(s);
956 3602204 : if (*sdat) pari_free(*sdat);
957 3624255 : }
958 :
959 : INLINE void
960 5557 : pari_stack_pushp(pari_stack *s, void *u)
961 : {
962 5557 : long n = pari_stack_new(s);
963 5557 : void **sdat =(void**) *pari_stack_base(s);
964 5557 : sdat[n] = u;
965 5557 : }
966 :
967 : /*******************************************************************/
968 : /* */
969 : /* EXTRACT */
970 : /* */
971 : /*******************************************************************/
972 : INLINE GEN
973 582420563 : vecslice(GEN A, long y1, long y2)
974 : {
975 582420563 : long i,lB = y2 - y1 + 2;
976 582420563 : GEN B = cgetg(lB, typ(A));
977 2584532428 : for (i=1; i<lB; i++) B[i] = A[y1-1+i];
978 582409824 : return B;
979 : }
980 : INLINE GEN
981 2071779 : vecslicepermute(GEN A, GEN p, long y1, long y2)
982 : {
983 2071779 : long i,lB = y2 - y1 + 2;
984 2071779 : GEN B = cgetg(lB, typ(A));
985 31495362 : for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
986 2071759 : return B;
987 : }
988 : /* rowslice(rowpermute(A,p), x1, x2) */
989 : INLINE GEN
990 167626 : rowslicepermute(GEN x, GEN p, long j1, long j2)
991 725311 : { pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
992 :
993 : INLINE GEN
994 64511275 : rowslice(GEN x, long j1, long j2)
995 593566592 : { pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
996 :
997 : INLINE GEN
998 9385418 : matslice(GEN A, long x1, long x2, long y1, long y2)
999 9385418 : { return rowslice(vecslice(A, y1, y2), x1, x2); }
1000 :
1001 : /* shallow, remove coeff of index j */
1002 : INLINE GEN
1003 210 : rowsplice(GEN x, long j)
1004 1036 : { pari_APPLY_same(vecsplice(gel(x,i), j)) }
1005 :
1006 : /* shallow, remove coeff of index j */
1007 : INLINE GEN
1008 353919 : vecsplice(GEN a, long j)
1009 : {
1010 353919 : long i, k, l = lg(a);
1011 : GEN b;
1012 353919 : if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
1013 353919 : b = cgetg(l-1, typ(a));
1014 1498109 : for (i = k = 1; i < l; i++)
1015 1144191 : if (i != j) gel(b, k++) = gel(a,i);
1016 353918 : return b;
1017 : }
1018 : /* shallow */
1019 : INLINE GEN
1020 1036 : RgM_minor(GEN a, long i, long j)
1021 : {
1022 1036 : GEN b = vecsplice(a, j);
1023 1036 : long k, l = lg(b);
1024 4242 : for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
1025 1036 : return b;
1026 : }
1027 :
1028 : /* A[x0,] */
1029 : INLINE GEN
1030 474034 : row(GEN x, long j)
1031 4402149 : { pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
1032 : INLINE GEN
1033 9877316 : Flm_row(GEN x, long j)
1034 242639836 : { pari_APPLY_ulong((ulong)coeff(x, j, i)) }
1035 : /* A[x0,] */
1036 : INLINE GEN
1037 204988 : rowcopy(GEN x, long j)
1038 2105509 : { pari_APPLY_type(t_VEC, gcopy(gcoeff(x, j, i))) }
1039 : /* A[x0, x1..x2] */
1040 : INLINE GEN
1041 987 : row_i(GEN A, long x0, long x1, long x2)
1042 : {
1043 987 : long i, lB = x2 - x1 + 2;
1044 987 : GEN B = cgetg(lB, t_VEC);
1045 2989 : for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
1046 987 : return B;
1047 : }
1048 :
1049 : INLINE GEN
1050 398643 : vecreverse(GEN A)
1051 : {
1052 : long i, l;
1053 398643 : GEN B = cgetg_copy(A, &l);
1054 1187137 : for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
1055 398643 : return B;
1056 : }
1057 :
1058 : INLINE GEN
1059 2961 : vecsmall_reverse(GEN A)
1060 : {
1061 : long i, l;
1062 2961 : GEN B = cgetg_copy(A, &l);
1063 11669 : for (i=1; i<l; i++) B[i] = A[l-i];
1064 2961 : return B;
1065 : }
1066 :
1067 : INLINE void
1068 2321 : vecreverse_inplace(GEN y)
1069 : {
1070 2321 : long l = lg(y), lim = l>>1, i;
1071 7615 : for (i = 1; i <= lim; i++)
1072 : {
1073 5294 : GEN z = gel(y,i);
1074 5294 : gel(y,i) = gel(y,l-i);
1075 5294 : gel(y,l-i) = z;
1076 : }
1077 2321 : }
1078 :
1079 : INLINE GEN
1080 75676066 : vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
1081 :
1082 : INLINE GEN
1083 11510292 : vecpermute(GEN A, GEN x)
1084 97545915 : { pari_APPLY_type(typ(A), gel(A, x[i])) }
1085 :
1086 : INLINE GEN
1087 10070491 : rowpermute(GEN x, GEN p)
1088 88105161 : { pari_APPLY_same(typ(gel(x,i)) == t_VECSMALL ? vecsmallpermute(gel(x, i), p)
1089 : : vecpermute(gel(x, i), p))
1090 : }
1091 : /*******************************************************************/
1092 : /* */
1093 : /* PERMUTATIONS */
1094 : /* */
1095 : /*******************************************************************/
1096 : INLINE GEN
1097 2102125 : identity_zv(long n)
1098 : {
1099 2102125 : GEN v = cgetg(n+1, t_VECSMALL);
1100 : long i;
1101 25998721 : for (i = 1; i <= n; i++) v[i] = i;
1102 2102085 : return v;
1103 : }
1104 : INLINE GEN
1105 4242 : identity_ZV(long n)
1106 : {
1107 4242 : GEN v = cgetg(n+1, t_VEC);
1108 : long i;
1109 51541 : for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
1110 4242 : return v;
1111 : }
1112 : /* identity permutation */
1113 : INLINE GEN
1114 2077821 : identity_perm(long n) { return identity_zv(n); }
1115 :
1116 : /* assume d <= n */
1117 : INLINE GEN
1118 98483 : cyclic_perm(long n, long d)
1119 : {
1120 98483 : GEN perm = cgetg(n+1, t_VECSMALL);
1121 : long i;
1122 504098 : for (i = 1; i <= n-d; i++) perm[i] = i+d;
1123 225029 : for ( ; i <= n; i++) perm[i] = i-n+d;
1124 98483 : return perm;
1125 : }
1126 :
1127 : /* Multiply (compose) two permutations */
1128 : INLINE GEN
1129 77870569 : perm_mul(GEN s, GEN x)
1130 1000545885 : { pari_APPLY_long(s[x[i]]) }
1131 :
1132 : INLINE GEN
1133 728 : perm_sqr(GEN x)
1134 18284 : { pari_APPLY_long(x[x[i]]) }
1135 :
1136 : /* Compute the inverse (reciprocal) of a permutation. */
1137 : INLINE GEN
1138 2692623 : perm_inv(GEN x)
1139 : {
1140 : long i, lx;
1141 2692623 : GEN y = cgetg_copy(x, &lx);
1142 37979896 : for (i=1; i<lx; i++) y[ x[i] ] = i;
1143 2692618 : return y;
1144 : }
1145 : /* Return s*t*s^-1 */
1146 : INLINE GEN
1147 418257 : perm_conj(GEN s, GEN t)
1148 : {
1149 : long i, l;
1150 418257 : GEN v = cgetg_copy(s, &l);
1151 6857683 : for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
1152 418257 : return v;
1153 : }
1154 :
1155 : INLINE void
1156 367145602 : pari_free(void *pointer)
1157 : {
1158 367145602 : BLOCK_SIGINT_START;
1159 367208248 : free(pointer);
1160 367208248 : BLOCK_SIGINT_END;
1161 367211500 : }
1162 : INLINE void*
1163 589586429 : pari_malloc(size_t size)
1164 : {
1165 589586429 : if (size)
1166 : {
1167 : char *tmp;
1168 589588162 : BLOCK_SIGINT_START;
1169 589666021 : tmp = (char*)malloc(size);
1170 589666021 : BLOCK_SIGINT_END;
1171 589665862 : if (!tmp) pari_err(e_MEM);
1172 589681587 : return tmp;
1173 : }
1174 0 : return NULL;
1175 : }
1176 : INLINE void*
1177 1792 : pari_realloc(void *pointer, size_t size)
1178 : {
1179 : char *tmp;
1180 :
1181 1792 : BLOCK_SIGINT_START;
1182 1792 : if (!pointer) tmp = (char *) malloc(size);
1183 1792 : else tmp = (char *) realloc(pointer,size);
1184 1792 : BLOCK_SIGINT_END;
1185 1792 : if (!tmp) pari_err(e_MEM);
1186 1792 : return tmp;
1187 : }
1188 : INLINE void
1189 1491469 : pari_realloc_ip(void **pointer, size_t size)
1190 : {
1191 : char *tmp;
1192 1491469 : BLOCK_SIGINT_START;
1193 1490348 : if (!*pointer) tmp = (char *) malloc(size);
1194 102752 : else tmp = (char *) realloc(*pointer,size);
1195 1490348 : if (!tmp) pari_err(e_MEM);
1196 1490348 : *pointer = tmp;
1197 1490348 : BLOCK_SIGINT_END;
1198 1486282 : }
1199 :
1200 : INLINE void*
1201 43870 : pari_calloc(size_t size)
1202 : {
1203 43870 : void *t = pari_malloc(size);
1204 43870 : memset(t, 0, size); return t;
1205 : }
1206 : INLINE GEN
1207 8059 : cgetalloc(long t, size_t l)
1208 : { /* evallg may raise e_OVERFLOW, which would leak x */
1209 8059 : ulong x0 = evaltyp(t) | evallg(l);
1210 8059 : GEN x = (GEN)pari_malloc(l * sizeof(long));
1211 8059 : x[0] = x0; return x;
1212 : }
1213 :
1214 : /*******************************************************************/
1215 : /* */
1216 : /* GARBAGE COLLECTION */
1217 : /* */
1218 : /*******************************************************************/
1219 : /* copy integer x as if we had set_avma(av) */
1220 : INLINE GEN
1221 6373869910 : icopy_avma(GEN x, pari_sp av)
1222 : {
1223 6373869910 : long i = lgefint(x), lx = i;
1224 6373869910 : GEN y = ((GEN)av) - i;
1225 42882100338 : while (--i > 0) y[i] = x[i];
1226 6373869910 : y[0] = evaltyp(t_INT)|evallg(lx);
1227 6377834999 : return y;
1228 : }
1229 : /* copy leaf x as if we had set_avma(av) */
1230 : INLINE GEN
1231 434456213 : leafcopy_avma(GEN x, pari_sp av)
1232 : {
1233 434456213 : long i = lg(x);
1234 434456213 : GEN y = ((GEN)av) - i;
1235 2729598874 : while (--i > 0) y[i] = x[i];
1236 434456213 : y[0] = x[0] & (~CLONEBIT);
1237 434456213 : return y;
1238 : }
1239 : INLINE GEN
1240 1027550986 : gerepileuptoleaf(pari_sp av, GEN x)
1241 : {
1242 : long lx;
1243 : GEN q;
1244 :
1245 1027550986 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1246 1026905060 : lx = lg(x);
1247 1026905060 : q = ((GEN)av) - lx;
1248 1026905060 : set_avma((pari_sp)q);
1249 10586326504 : while (--lx >= 0) q[lx] = x[lx];
1250 1026845569 : return q;
1251 : }
1252 : INLINE GEN
1253 2916055140 : gerepileuptoint(pari_sp av, GEN x)
1254 : {
1255 2916055140 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1256 2553025650 : set_avma((pari_sp)icopy_avma(x, av));
1257 2556395775 : return (GEN)avma;
1258 : }
1259 : INLINE GEN
1260 1537706384 : gerepileupto(pari_sp av, GEN x)
1261 : {
1262 1537706384 : if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1263 1367950841 : switch(typ(x))
1264 : { /* non-default = !is_recursive_t(tq) */
1265 412497369 : case t_INT: return gerepileuptoint(av, x);
1266 320618275 : case t_REAL:
1267 : case t_STR:
1268 320618275 : case t_VECSMALL: return gerepileuptoleaf(av,x);
1269 634837627 : default:
1270 : /* NB: x+i --> ((long)x) + i*sizeof(long) */
1271 634837627 : return gerepile(av, (pari_sp) (x+lg(x)), x);
1272 : }
1273 : }
1274 :
1275 : /* gerepileupto(av, gcopy(x)) */
1276 : INLINE GEN
1277 140454517 : gerepilecopy(pari_sp av, GEN x)
1278 : {
1279 140454517 : if (is_recursive_t(typ(x)))
1280 : {
1281 134073014 : GENbin *p = copy_bin(x);
1282 134088855 : set_avma(av); return bin_copy(p);
1283 : }
1284 : else
1285 : {
1286 6382123 : set_avma(av);
1287 6382164 : if (x < (GEN)av) {
1288 4636939 : if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
1289 4636941 : x = leafcopy_avma(x, av);
1290 4636958 : set_avma((pari_sp)x);
1291 : } else
1292 1745228 : x = leafcopy(x);
1293 6382191 : return x;
1294 : }
1295 : }
1296 :
1297 : INLINE void
1298 28111905 : guncloneNULL(GEN x) { if (x) gunclone(x); }
1299 : INLINE void
1300 944343 : guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
1301 :
1302 : /* Takes an array of pointers to GENs, of length n. Copies all
1303 : * objects to contiguous locations and cleans up the stack between
1304 : * av and avma. */
1305 : INLINE void
1306 938357 : gerepilemany(pari_sp av, GEN* gptr[], int n)
1307 : {
1308 : int i;
1309 3745199 : for (i=0; i<n; i++) *gptr[i] = (GEN)copy_bin(*gptr[i]);
1310 938008 : set_avma(av);
1311 3745909 : for (i=0; i<n; i++) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1312 938574 : }
1313 :
1314 : INLINE void
1315 11420931 : gerepileall(pari_sp av, int n, ...)
1316 : {
1317 : int i;
1318 11420931 : va_list a; va_start(a, n);
1319 11420931 : if (n < 10)
1320 : {
1321 : GEN *gptr[10];
1322 34538082 : for (i=0; i<n; i++)
1323 23116885 : { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
1324 11421197 : set_avma(av);
1325 34538434 : for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1326 :
1327 : }
1328 : else
1329 : {
1330 1 : GEN **gptr = (GEN**) pari_malloc(n*sizeof(GEN*));
1331 0 : for (i=0; i<n; i++)
1332 0 : { gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
1333 0 : set_avma(av);
1334 0 : for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1335 0 : pari_free(gptr);
1336 : }
1337 11421648 : va_end(a);
1338 11421648 : }
1339 :
1340 : /* assume 1 <= n < 10 */
1341 : INLINE GEN
1342 70429329 : gc_all(pari_sp av, int n, ...)
1343 : {
1344 : int i;
1345 : GEN *v[10];
1346 70429329 : va_list a; va_start(a, n);
1347 241588205 : for (i=0; i<n; i++) { v[i] = va_arg(a,GEN*); *v[i] = (GEN)copy_bin(*v[i]); }
1348 70429726 : set_avma(av);
1349 241589154 : for (i=0; i<n; i++) *v[i] = bin_copy((GENbin*)*v[i]);
1350 70430089 : return *v[0];
1351 : }
1352 :
1353 : INLINE void
1354 2195687 : gerepilecoeffs(pari_sp av, GEN x, int n)
1355 : {
1356 : int i;
1357 26131622 : for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
1358 2196238 : set_avma(av);
1359 26133115 : for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
1360 2195941 : }
1361 :
1362 : /* p from copy_bin. Copy p->x back to stack, then destroy p */
1363 : INLINE GEN
1364 355148053 : bin_copy(GENbin *p)
1365 : {
1366 : GEN x, y, base;
1367 : long dx, len;
1368 :
1369 355148053 : x = p->x; if (!x) { pari_free(p); return gen_0; }
1370 327013270 : len = p->len;
1371 327013270 : base= p->base; dx = x - base;
1372 327013270 : y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
1373 327007480 : y += dx;
1374 327007480 : p->rebase(y, ((ulong)y-(ulong)x));
1375 327007127 : pari_free(p); return y;
1376 : }
1377 :
1378 : INLINE GEN
1379 682157133 : GENbinbase(GENbin *p) { return (GEN)(p + 1); }
1380 :
1381 : INLINE void
1382 111007172 : cgiv(GEN x)
1383 : {
1384 111007172 : pari_sp av = (pari_sp)(x+lg(x));
1385 111007172 : if (isonstack((GEN)av)) set_avma(av);
1386 111009922 : }
1387 :
1388 : INLINE void
1389 1547506 : killblock(GEN x) { gunclone(x); }
1390 :
1391 : INLINE int
1392 319153516 : is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
1393 :
1394 : /*******************************************************************/
1395 : /* */
1396 : /* CONVERSION / ASSIGNMENT */
1397 : /* */
1398 : /*******************************************************************/
1399 : /* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
1400 : INLINE GEN
1401 17639168 : cxcompotor(GEN z, long prec)
1402 : {
1403 17639168 : switch(typ(z))
1404 : {
1405 11192553 : case t_INT: return itor(z, prec);
1406 289429 : case t_FRAC: return fractor(z, prec);
1407 6157513 : case t_REAL: return rtor(z, prec);
1408 0 : default: pari_err_TYPE("cxcompotor",z);
1409 : return NULL; /* LCOV_EXCL_LINE */
1410 : }
1411 : }
1412 : INLINE GEN
1413 8805020 : cxtofp(GEN x, long prec)
1414 8805020 : { retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
1415 :
1416 : INLINE GEN
1417 275452 : cxtoreal(GEN q)
1418 275452 : { return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
1419 :
1420 : INLINE double
1421 55381935 : gtodouble(GEN x)
1422 : {
1423 55381935 : if (typ(x)!=t_REAL) {
1424 6007946 : pari_sp av = avma;
1425 6007946 : x = gtofp(x, DEFAULTPREC);
1426 6007263 : if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
1427 6007263 : set_avma(av);
1428 : }
1429 55381122 : return rtodbl(x);
1430 : }
1431 :
1432 : INLINE int
1433 8926208 : gisdouble(GEN x, double *g)
1434 : {
1435 8926208 : if (typ(x)!=t_REAL) {
1436 146817 : pari_sp av = avma;
1437 146817 : x = gtofp(x, DEFAULTPREC);
1438 146814 : if (typ(x)!=t_REAL) pari_err_TYPE("gisdouble [t_REAL expected]", x);
1439 146814 : set_avma(av);
1440 : }
1441 8926194 : if (expo(x) >= 0x3ff) return 0;
1442 8926194 : *g = rtodbl(x); return 1;
1443 : }
1444 :
1445 : INLINE long
1446 73835003 : gtos(GEN x) {
1447 73835003 : if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
1448 73834996 : return itos(x);
1449 : }
1450 :
1451 : INLINE ulong
1452 102510 : gtou(GEN x) {
1453 102510 : if (typ(x) != t_INT || signe(x)<0)
1454 7 : pari_err_TYPE("gtou [integer >=0 expected]",x);
1455 102503 : return itou(x);
1456 : }
1457 :
1458 : INLINE GEN
1459 40800646 : absfrac(GEN x)
1460 : {
1461 40800646 : GEN y = cgetg(3, t_FRAC);
1462 40800672 : gel(y,1) = absi(gel(x,1));
1463 40800666 : gel(y,2) = icopy(gel(x,2)); return y;
1464 : }
1465 : INLINE GEN
1466 43890 : absfrac_shallow(GEN x)
1467 43890 : { return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
1468 : INLINE GEN
1469 7091833 : Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
1470 : INLINE GEN
1471 159347 : Q_abs_shallow(GEN x)
1472 159347 : { return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
1473 : INLINE GEN
1474 12754 : R_abs_shallow(GEN x)
1475 12754 : { return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
1476 : INLINE GEN
1477 0 : R_abs(GEN x)
1478 0 : { return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
1479 :
1480 : /* Force z to be of type real/complex with floating point components */
1481 : INLINE GEN
1482 74638253 : gtofp(GEN z, long prec)
1483 : {
1484 74638253 : switch(typ(z))
1485 : {
1486 42171224 : case t_INT: return itor(z, prec);
1487 4091773 : case t_FRAC: return fractor(z, prec);
1488 19838282 : case t_REAL: return rtor(z, prec);
1489 8544798 : case t_COMPLEX: {
1490 8544798 : GEN a = gel(z,1), b = gel(z,2);
1491 8544798 : if (isintzero(b)) return cxcompotor(a, prec);
1492 8544808 : if (isintzero(a)) {
1493 8308 : GEN y = cgetg(3, t_COMPLEX);
1494 8308 : b = cxcompotor(b, prec);
1495 8308 : gel(y,1) = real_0_bit(expo(b) - prec2nbits(prec));
1496 8308 : gel(y,2) = b; return y;
1497 : }
1498 8536460 : return cxtofp(z, prec);
1499 : }
1500 0 : case t_QUAD: return quadtofp(z, prec);
1501 0 : default: pari_err_TYPE("gtofp",z);
1502 : return NULL; /* LCOV_EXCL_LINE */
1503 : }
1504 : }
1505 : /* Force z to be of type real / int */
1506 : INLINE GEN
1507 22428 : gtomp(GEN z, long prec)
1508 : {
1509 22428 : switch(typ(z))
1510 : {
1511 42 : case t_INT: return z;
1512 22386 : case t_FRAC: return fractor(z, prec);
1513 0 : case t_REAL: return rtor(z, prec);
1514 0 : case t_QUAD: z = quadtofp(z, prec);
1515 0 : if (typ(z) == t_REAL) return z;
1516 0 : default: pari_err_TYPE("gtomp",z);
1517 : return NULL; /* LCOV_EXCL_LINE */
1518 : }
1519 : }
1520 :
1521 : INLINE GEN
1522 4686227 : RgX_gtofp(GEN x, long prec)
1523 : {
1524 : long l;
1525 4686227 : GEN y = cgetg_copy(x, &l);
1526 26548274 : while (--l > 1) gel(y,l) = gtofp(gel(x,l), prec);
1527 4684083 : y[1] = x[1]; return y;
1528 : }
1529 : INLINE GEN
1530 3122222 : RgC_gtofp(GEN x, long prec)
1531 25453961 : { pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
1532 :
1533 : INLINE GEN
1534 49 : RgV_gtofp(GEN x, long prec)
1535 4746 : { pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
1536 :
1537 : INLINE GEN
1538 584088 : RgM_gtofp(GEN x, long prec)
1539 3080352 : { pari_APPLY_same(RgC_gtofp(gel(x,i), prec)) }
1540 :
1541 : INLINE GEN
1542 574 : RgC_gtomp(GEN x, long prec)
1543 23002 : { pari_APPLY_type(t_COL, gtomp(gel(x,i), prec)) }
1544 :
1545 : INLINE GEN
1546 21 : RgM_gtomp(GEN x, long prec)
1547 595 : { pari_APPLY_same(RgC_gtomp(gel(x,i), prec)) }
1548 :
1549 : INLINE GEN
1550 57604 : RgX_fpnorml2(GEN x, long prec)
1551 : {
1552 57604 : pari_sp av = avma;
1553 57604 : return gerepileupto(av, gnorml2(RgX_gtofp(x, prec)));
1554 : }
1555 : INLINE GEN
1556 615234 : RgC_fpnorml2(GEN x, long prec)
1557 : {
1558 615234 : pari_sp av = avma;
1559 615234 : return gerepileupto(av, gnorml2(RgC_gtofp(x, prec)));
1560 : }
1561 : INLINE GEN
1562 18616 : RgM_fpnorml2(GEN x, long prec)
1563 : {
1564 18616 : pari_sp av = avma;
1565 18616 : return gerepileupto(av, gnorml2(RgM_gtofp(x, prec)));
1566 : }
1567 :
1568 : /* y a t_REAL */
1569 : INLINE void
1570 216244 : affgr(GEN x, GEN y)
1571 : {
1572 : pari_sp av;
1573 216244 : switch(typ(x)) {
1574 5992 : case t_INT: affir(x,y); break;
1575 210252 : case t_REAL: affrr(x,y); break;
1576 0 : case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
1577 0 : case t_QUAD: av = avma; affgr(quadtofp(x,realprec(y)), y); set_avma(av); break;
1578 0 : default: pari_err_TYPE2("=",x,y);
1579 : }
1580 216244 : }
1581 :
1582 : INLINE GEN
1583 245073 : affc_fixlg(GEN x, GEN res)
1584 : {
1585 245073 : if (typ(x) == t_COMPLEX)
1586 : {
1587 212849 : affrr_fixlg(gel(x,1), gel(res,1));
1588 212849 : affrr_fixlg(gel(x,2), gel(res,2));
1589 : }
1590 : else
1591 : {
1592 32224 : set_avma((pari_sp)(res+3));
1593 32223 : res = cgetr(realprec(gel(res,1)));
1594 32229 : affrr_fixlg(x, res);
1595 : }
1596 245076 : return res;
1597 : }
1598 :
1599 : INLINE GEN
1600 0 : trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
1601 :
1602 : /*******************************************************************/
1603 : /* */
1604 : /* LENGTH CONVERSIONS */
1605 : /* */
1606 : /*******************************************************************/
1607 : INLINE long
1608 38405 : ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
1609 : INLINE long
1610 30052 : ndec2prec(long x) { return 2 + ndec2nlong(x); }
1611 : INLINE long
1612 8353 : ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1613 : /* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
1614 : * to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
1615 : * addl, shrl instead of subl, sarl, addl */
1616 : INLINE long
1617 11350914 : nbits2nlong(long x) {
1618 11350914 : return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1619 : }
1620 :
1621 : INLINE long
1622 513328645 : nbits2extraprec(long x) {
1623 513328645 : return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1624 : }
1625 :
1626 : /* Fast implementation of 2 + nbits2nlong(x) */
1627 : INLINE long
1628 178962864 : nbits2prec(long x) {
1629 178962864 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1630 : }
1631 : INLINE long
1632 314935555 : nbits2lg(long x) {
1633 314935555 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1634 : }
1635 : /* ceil(x / sizeof(long)) */
1636 : INLINE long
1637 100152232 : nchar2nlong(long x) {
1638 100152232 : return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
1639 : }
1640 : INLINE long
1641 2142935364 : prec2nbits(long x) { return (x-2) * BITS_IN_LONG; }
1642 : INLINE double
1643 2866907 : bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
1644 : INLINE double
1645 586430 : prec2nbits_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
1646 : INLINE long
1647 99669334 : bit_prec(GEN x) { return prec2nbits(realprec(x)); }
1648 : INLINE long
1649 1911147730 : bit_accuracy(long x) { return prec2nbits(x); }
1650 : INLINE long
1651 10152 : prec2ndec(long x) { return (long)prec2nbits_mul(x, LOG10_2); }
1652 : INLINE long
1653 228 : nbits2ndec(long x) { return (long)(x * LOG10_2); }
1654 : INLINE long
1655 4774 : precdbl(long x) {return (x - 1) << 1;}
1656 : INLINE long
1657 6051820377 : divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
1658 : INLINE long
1659 5948357062 : remsBIL(long n) { return n & (BITS_IN_LONG-1); }
1660 :
1661 : /*********************************************************************/
1662 : /** **/
1663 : /** OPERATIONS MODULO m **/
1664 : /** **/
1665 : /*********************************************************************/
1666 : /* Assume m > 0, more efficient if 0 <= a, b < m */
1667 :
1668 : INLINE GEN
1669 45843593 : Fp_red(GEN a, GEN m) { return modii(a, m); }
1670 : INLINE GEN
1671 109532415 : Fp_add(GEN a, GEN b, GEN m)
1672 : {
1673 109532415 : pari_sp av=avma;
1674 109532415 : GEN p = addii(a,b);
1675 107964697 : long s = signe(p);
1676 107964697 : if (!s) return p; /* = gen_0 */
1677 97057354 : if (s > 0) /* general case */
1678 : {
1679 97075003 : GEN t = subii(p, m);
1680 96714030 : s = signe(t);
1681 96714030 : if (!s) return gc_const(av, gen_0);
1682 89240020 : if (s < 0) return gc_const((pari_sp)p, p);
1683 44915973 : if (cmpii(t, m) < 0) return gerepileuptoint(av, t); /* general case ! */
1684 3001949 : p = remii(t, m);
1685 : }
1686 : else
1687 226384 : p = modii(p, m);
1688 3228443 : return gerepileuptoint(av, p);
1689 : }
1690 : INLINE GEN
1691 113808811 : Fp_sub(GEN a, GEN b, GEN m)
1692 : {
1693 113808811 : pari_sp av=avma;
1694 113808811 : GEN p = subii(a,b);
1695 111720382 : long s = signe(p);
1696 111720382 : if (!s) return p; /* = gen_0 */
1697 100199284 : if (s > 0)
1698 : {
1699 45009634 : if (cmpii(p, m) < 0) return p; /* general case ! */
1700 1251763 : p = remii(p, m);
1701 : }
1702 : else
1703 : {
1704 55189650 : GEN t = addii(p, m);
1705 56971723 : if (!s) return gc_const(av, gen_0);
1706 57016827 : if (s > 0) return gerepileuptoint(av, t); /* general case ! */
1707 57016827 : p = modii(t, m);
1708 : }
1709 58551332 : return gerepileuptoint(av, p);
1710 : }
1711 : INLINE GEN
1712 16916210 : Fp_neg(GEN b, GEN m)
1713 : {
1714 16916210 : pari_sp av = avma;
1715 16916210 : long s = signe(b);
1716 : GEN p;
1717 16916210 : if (!s) return gen_0;
1718 12940165 : if (s > 0)
1719 : {
1720 11826938 : p = subii(m, b);
1721 11824570 : if (signe(p) >= 0) return p; /* general case ! */
1722 382852 : p = modii(p, m);
1723 : } else
1724 1114179 : p = remii(negi(b), m);
1725 1497234 : return gerepileuptoint(av, p);
1726 : }
1727 :
1728 : INLINE GEN
1729 63925 : Fp_halve(GEN a, GEN p)
1730 : {
1731 63925 : if (mpodd(a)) a = addii(a,p);
1732 63925 : return shifti(a,-1);
1733 : }
1734 :
1735 : /* assume 0 <= u < p and ps2 = p>>1 */
1736 : INLINE GEN
1737 58859903 : Fp_center(GEN u, GEN p, GEN ps2)
1738 58859903 : { return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
1739 : /* same without copy */
1740 : INLINE GEN
1741 12199735 : Fp_center_i(GEN u, GEN p, GEN ps2)
1742 12199735 : { return abscmpii(u,ps2)<=0? u: subii(u,p); }
1743 :
1744 : /* x + y*z mod p */
1745 : INLINE GEN
1746 10746406 : Fp_addmul(GEN x, GEN y, GEN z, GEN p)
1747 : {
1748 : pari_sp av;
1749 10746406 : if (!signe(y) || !signe(z)) return Fp_red(x, p);
1750 10418292 : if (!signe(x)) return Fp_mul(z,y, p);
1751 9418307 : av = avma;
1752 9418307 : return gerepileuptoint(av, modii(addii(x, mulii(y,z)), p));
1753 : }
1754 :
1755 : INLINE GEN
1756 170520099 : Fp_mul(GEN a, GEN b, GEN m)
1757 : {
1758 170520099 : pari_sp av=avma;
1759 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1760 170520099 : (void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
1761 170526262 : p = mulii(a,b);
1762 170628818 : set_avma(av); return modii(p,m);
1763 : }
1764 : INLINE GEN
1765 67571453 : Fp_sqr(GEN a, GEN m)
1766 : {
1767 67571453 : pari_sp av=avma;
1768 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1769 67571453 : (void)new_chunk((lg(a)+lg(m))<<1);
1770 67749676 : p = sqri(a);
1771 65061920 : set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
1772 : }
1773 : INLINE GEN
1774 79143608 : Fp_mulu(GEN a, ulong b, GEN m)
1775 : {
1776 79143608 : long l = lgefint(m);
1777 79143608 : if (l == 3)
1778 : {
1779 44837796 : ulong mm = m[2];
1780 44837796 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1781 : } else {
1782 34305812 : pari_sp av = avma;
1783 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1784 34305812 : (void)new_chunk(lg(a)+1+(l<<1));
1785 34298064 : p = muliu(a,b);
1786 32445100 : set_avma(av); return modii(p,m);
1787 : }
1788 : }
1789 : INLINE GEN
1790 17290 : Fp_muls(GEN a, long b, GEN m)
1791 : {
1792 17290 : long l = lgefint(m);
1793 17290 : if (l == 3)
1794 : {
1795 3058 : ulong mm = m[2];
1796 3058 : if (b < 0)
1797 : {
1798 3058 : ulong t = Fl_mul(umodiu(a, mm), -b, mm);
1799 3058 : return t? utoipos(mm - t): gen_0;
1800 : }
1801 : else
1802 0 : return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1803 : } else {
1804 14232 : pari_sp av = avma;
1805 : GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1806 14232 : (void)new_chunk(lg(a)+1+(l<<1));
1807 14232 : p = mulis(a,b);
1808 14232 : set_avma(av); return modii(p,m);
1809 : }
1810 : }
1811 :
1812 : INLINE GEN
1813 24065753 : Fp_inv(GEN a, GEN m)
1814 : {
1815 : GEN res;
1816 24065753 : if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
1817 24065084 : return res;
1818 : }
1819 : INLINE GEN
1820 778242 : Fp_invsafe(GEN a, GEN m)
1821 : {
1822 : GEN res;
1823 778242 : if (! invmod(a,m,&res)) return NULL;
1824 778161 : return res;
1825 : }
1826 : INLINE GEN
1827 1462407 : Fp_div(GEN a, GEN b, GEN m)
1828 : {
1829 1462407 : pari_sp av = avma;
1830 : GEN p;
1831 1462407 : if (lgefint(b) == 3)
1832 : {
1833 489849 : a = Fp_divu(a, b[2], m);
1834 489849 : if (signe(b) < 0) a = Fp_neg(a, m);
1835 489849 : return a;
1836 : }
1837 : /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1838 972558 : (void)new_chunk(lg(a)+(lg(m)<<1));
1839 972558 : p = mulii(a, Fp_inv(b,m));
1840 972558 : set_avma(av); return modii(p,m);
1841 : }
1842 : INLINE GEN
1843 1654989 : Fp_divu(GEN x, ulong a, GEN p)
1844 : {
1845 1654989 : pari_sp av = avma;
1846 : ulong b;
1847 1654989 : if (lgefint(p) == 3)
1848 : {
1849 1356786 : ulong pp = p[2], xp = umodiu(x, pp);
1850 1356786 : return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
1851 : }
1852 298203 : x = Fp_red(x, p);
1853 298202 : b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
1854 298207 : return gerepileuptoint(av, diviuexact(addmuliu(x, p, b), a));
1855 : }
1856 :
1857 : INLINE GEN
1858 1140743 : Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
1859 :
1860 : INLINE GEN
1861 1904617 : get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1862 :
1863 : INLINE long
1864 2472672 : get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1865 :
1866 : INLINE long
1867 1927959 : get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
1868 :
1869 : INLINE GEN
1870 406 : get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1871 :
1872 : INLINE long
1873 376187 : get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1874 :
1875 : INLINE long
1876 201481 : get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1877 :
1878 : INLINE GEN
1879 23837352 : get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1880 :
1881 : INLINE long
1882 64297038 : get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1883 :
1884 : INLINE long
1885 83069649 : get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1886 :
1887 : INLINE GEN
1888 7956 : get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1889 :
1890 : INLINE long
1891 234932 : get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1892 :
1893 : INLINE long
1894 290934 : get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1895 :
1896 : INLINE GEN
1897 2634571 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1898 :
1899 : INLINE long
1900 6528389 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1901 :
1902 : INLINE long
1903 5236940 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1904 :
1905 : INLINE GEN
1906 146583 : get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1907 :
1908 : INLINE long
1909 34808 : get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1910 :
1911 : INLINE long
1912 2259 : get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1913 :
1914 : /*******************************************************************/
1915 : /* */
1916 : /* ADDMULII / SUBMULII */
1917 : /* */
1918 : /*******************************************************************/
1919 : /* x - y*z */
1920 : INLINE GEN
1921 43298157 : submulii(GEN x, GEN y, GEN z)
1922 : {
1923 43298157 : long lx = lgefint(x), ly, lz;
1924 : pari_sp av;
1925 : GEN t;
1926 43298157 : if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
1927 38377115 : ly = lgefint(y);
1928 38377115 : if (ly == 2) return icopy(x);
1929 36320274 : lz = lgefint(z);
1930 36320274 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1931 36320274 : t = mulii(z, y);
1932 36320274 : set_avma(av); return subii(x,t);
1933 : }
1934 : /* y*z - x */
1935 : INLINE GEN
1936 2694365 : mulsubii(GEN y, GEN z, GEN x)
1937 : {
1938 2694365 : long lx = lgefint(x), ly, lz;
1939 : pari_sp av;
1940 : GEN t;
1941 2694365 : if (lx == 2) return mulii(z,y);
1942 1671125 : ly = lgefint(y);
1943 1671125 : if (ly == 2) return negi(x);
1944 1497259 : lz = lgefint(z);
1945 1497259 : av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1946 1497311 : t = mulii(z, y);
1947 1497251 : set_avma(av); return subii(t,x);
1948 : }
1949 :
1950 : /* x - u*y */
1951 : INLINE GEN
1952 7693 : submuliu(GEN x, GEN y, ulong u)
1953 : {
1954 : pari_sp av;
1955 7693 : long ly = lgefint(y);
1956 7693 : if (ly == 2) return icopy(x);
1957 7693 : av = avma;
1958 7693 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1959 7693 : y = mului(u,y);
1960 7693 : set_avma(av); return subii(x, y);
1961 : }
1962 : /* x + u*y */
1963 : INLINE GEN
1964 306275 : addmuliu(GEN x, GEN y, ulong u)
1965 : {
1966 : pari_sp av;
1967 306275 : long ly = lgefint(y);
1968 306275 : if (ly == 2) return icopy(x);
1969 306275 : av = avma;
1970 306275 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1971 306275 : y = mului(u,y);
1972 306272 : set_avma(av); return addii(x, y);
1973 : }
1974 : /* x - u*y */
1975 : INLINE GEN
1976 147898423 : submuliu_inplace(GEN x, GEN y, ulong u)
1977 : {
1978 : pari_sp av;
1979 147898423 : long ly = lgefint(y);
1980 147898423 : if (ly == 2) return x;
1981 102364770 : av = avma;
1982 102364770 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1983 102372098 : y = mului(u,y);
1984 102369339 : set_avma(av); return subii(x, y);
1985 : }
1986 : /* x + u*y */
1987 : INLINE GEN
1988 144962161 : addmuliu_inplace(GEN x, GEN y, ulong u)
1989 : {
1990 : pari_sp av;
1991 144962161 : long ly = lgefint(y);
1992 144962161 : if (ly == 2) return x;
1993 101355413 : av = avma;
1994 101355413 : (void)new_chunk(3+ly+lgefint(x)); /* HACK */
1995 101362239 : y = mului(u,y);
1996 101359787 : set_avma(av); return addii(x, y);
1997 : }
1998 : /* ux + vy */
1999 : INLINE GEN
2000 32647936 : lincombii(GEN u, GEN v, GEN x, GEN y)
2001 : {
2002 32647936 : long lx = lgefint(x), ly;
2003 : GEN p1, p2;
2004 : pari_sp av;
2005 32647936 : if (lx == 2) return mulii(v,y);
2006 18876448 : ly = lgefint(y);
2007 18876448 : if (ly == 2) return mulii(u,x);
2008 16331845 : av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
2009 16335615 : p1 = mulii(u,x);
2010 16333303 : p2 = mulii(v,y);
2011 16333299 : set_avma(av); return addii(p1,p2);
2012 : }
2013 :
2014 : /*******************************************************************/
2015 : /* */
2016 : /* GEN SUBTYPES */
2017 : /* */
2018 : /*******************************************************************/
2019 :
2020 : INLINE int
2021 3980748349 : is_const_t(long t) { return (t < t_POLMOD); }
2022 : INLINE int
2023 6841 : is_extscalar_t(long t) { return (t <= t_POL); }
2024 : INLINE int
2025 8214474 : is_intreal_t(long t) { return (t <= t_REAL); }
2026 : INLINE int
2027 583818277 : is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
2028 : INLINE int
2029 84755746 : is_noncalc_t(long tx) { return (tx) >= t_LIST; }
2030 : INLINE int
2031 0 : is_qfb_t(long t) { return (t == t_QFB); }
2032 : INLINE int
2033 3648490 : is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
2034 : INLINE int
2035 1180237 : is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
2036 : INLINE int
2037 6851599147 : is_recursive_t(long t) { return lontyp[t]; }
2038 : INLINE int
2039 224514309 : is_scalar_t(long t) { return (t < t_POL); }
2040 : INLINE int
2041 1426950 : is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
2042 :
2043 : INLINE int
2044 56152610 : qfb_is_qfi(GEN q) { return signe(gel(q,4)) < 0; }
2045 :
2046 : /*******************************************************************/
2047 : /* */
2048 : /* TRANSCENDENTAL */
2049 : /* */
2050 : /*******************************************************************/
2051 : INLINE GEN
2052 57501263 : sqrtr(GEN x) {
2053 57501263 : long s = signe(x);
2054 57501263 : if (s == 0) return real_0_bit(expo(x) >> 1);
2055 57468173 : if (s >= 0) return sqrtr_abs(x);
2056 259841 : retmkcomplex(gen_0, sqrtr_abs(x));
2057 : }
2058 : INLINE GEN
2059 0 : cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
2060 : INLINE GEN
2061 0 : cbrtr(GEN x) {
2062 0 : long s = signe(x);
2063 : GEN r;
2064 0 : if (s == 0) return real_0_bit(expo(x) / 3);
2065 0 : r = cbrtr_abs(x);
2066 0 : if (s < 0) togglesign(r);
2067 0 : return r;
2068 : }
2069 : INLINE GEN
2070 1090529 : sqrtnr(GEN x, long n) {
2071 1090529 : long s = signe(x);
2072 : GEN r;
2073 1090529 : if (s == 0) return real_0_bit(expo(x) / n);
2074 1090529 : r = sqrtnr_abs(x, n);
2075 1090556 : if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
2076 1090556 : return r;
2077 : }
2078 : INLINE long
2079 598249 : logint(GEN B, GEN y) { return logintall(B,y,NULL); }
2080 : INLINE ulong
2081 2814161 : ulogint(ulong B, ulong y)
2082 : {
2083 : ulong r;
2084 : long e;
2085 2814161 : if (y == 2) return expu(B);
2086 2758206 : r = y;
2087 2758206 : for (e=1;; e++)
2088 : { /* here, r = y^e, r2 = y^(e-1) */
2089 8387923 : if (r >= B) return r == B? e: e-1;
2090 5631200 : r = umuluu_or_0(y, r);
2091 5631281 : if (!r) return e;
2092 : }
2093 : }
2094 :
2095 : /*******************************************************************/
2096 : /* */
2097 : /* MISCELLANEOUS */
2098 : /* */
2099 : /*******************************************************************/
2100 8214382 : INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
2101 2507154937 : INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
2102 14790366 : INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
2103 4347315 : INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
2104 1011539648 : INLINE int equali1(GEN n)
2105 1011539648 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
2106 136870507 : INLINE int equalim1(GEN n)
2107 136870507 : { return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
2108 : /* works only for POSITIVE integers */
2109 2466051719 : INLINE int is_pm1(GEN n)
2110 2466051719 : { return lgefint(n) == 3 && n[2] == 1; }
2111 480451791 : INLINE int is_bigint(GEN n)
2112 480451791 : { long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
2113 :
2114 1831241035 : INLINE int odd(long x) { return x & 1; }
2115 62349906 : INLINE int both_odd(long x, long y) { return x & y & 1; }
2116 :
2117 : INLINE int
2118 6151326030 : isonstack(GEN x)
2119 6151326030 : { return ((pari_sp)x >= pari_mainstack->bot
2120 6151326030 : && (pari_sp)x < pari_mainstack->top); }
2121 :
2122 : /* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
2123 : INLINE double
2124 60318063 : dbllog2r(GEN x)
2125 60318063 : { return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
2126 :
2127 : INLINE GEN
2128 1318649 : mul_content(GEN cx, GEN cy)
2129 : {
2130 1318649 : if (!cx) return cy;
2131 540549 : if (!cy) return cx;
2132 346729 : return gmul(cx,cy);
2133 : }
2134 : INLINE GEN
2135 0 : inv_content(GEN c) { return c? ginv(c): NULL; }
2136 : INLINE GEN
2137 36358 : div_content(GEN cx, GEN cy)
2138 : {
2139 36358 : if (!cy) return cx;
2140 35308 : if (!cx) return ginv(cy);
2141 7214 : return gdiv(cx,cy);
2142 : }
2143 : INLINE GEN
2144 5306980 : mul_denom(GEN dx, GEN dy)
2145 : {
2146 5306980 : if (!dx) return dy;
2147 1586959 : if (!dy) return dx;
2148 643080 : return mulii(dx,dy);
2149 : }
2150 :
2151 : /* POLYNOMIALS */
2152 : INLINE GEN
2153 1284116 : constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
2154 : INLINE GEN
2155 77389057 : leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
2156 : INLINE ulong
2157 652673 : Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
2158 : INLINE ulong
2159 38629 : Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
2160 : INLINE long
2161 4489339576 : degpol(GEN x) { return lg(x)-3; }
2162 : INLINE long
2163 2286644079 : lgpol(GEN x) { return lg(x)-2; }
2164 : INLINE long
2165 282260630 : lgcols(GEN x) { return lg(gel(x,1)); }
2166 : INLINE long
2167 131586716 : nbrows(GEN x) { return lg(gel(x,1))-1; }
2168 : INLINE GEN
2169 0 : truecoef(GEN x, long n) { return polcoef(x,n,-1); }
2170 :
2171 : INLINE GEN
2172 1429849 : ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
2173 : INLINE GEN
2174 576784 : ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
2175 :
2176 : INLINE GEN
2177 96466850 : RgX_copy(GEN x)
2178 : {
2179 : long lx, i;
2180 96466850 : GEN y = cgetg_copy(x, &lx); y[1] = x[1];
2181 351324338 : for (i = 2; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
2182 96466883 : return y;
2183 : }
2184 : /* have to use ulong to avoid silly warnings from gcc "assuming signed
2185 : * overflow does not occur" */
2186 : INLINE GEN
2187 2243253 : RgX_coeff(GEN x, long n)
2188 : {
2189 2243253 : ulong l = lg(x);
2190 2243253 : return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
2191 : }
2192 : INLINE GEN
2193 345537 : RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
2194 : INLINE GEN
2195 6840303 : RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
2196 : INLINE GEN
2197 1792 : RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
2198 : INLINE GEN
2199 104610 : RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
2200 : INLINE GEN
2201 4514358 : FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
2202 : INLINE GEN
2203 22658290 : Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
2204 : INLINE GEN
2205 1265801 : F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
2206 : INLINE GEN
2207 0 : FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
2208 : INLINE GEN
2209 92976962 : pol0_Flx(long sv) { return mkvecsmall(sv); }
2210 : INLINE GEN
2211 50980584 : pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
2212 : INLINE GEN
2213 34602612 : polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
2214 : INLINE GEN
2215 0 : zero_zx(long sv) { return zero_Flx(sv); }
2216 : INLINE GEN
2217 0 : polx_zx(long sv) { return polx_Flx(sv); }
2218 : INLINE GEN
2219 0 : zx_shift(GEN x, long n) { return Flx_shift(x,n); }
2220 : INLINE GEN
2221 14042 : zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
2222 : INLINE GEN
2223 1357 : zero_F2x(long sv) { return zero_Flx(sv); }
2224 : INLINE GEN
2225 11861497 : pol0_F2x(long sv) { return pol0_Flx(sv); }
2226 : INLINE GEN
2227 3293580 : pol1_F2x(long sv) { return pol1_Flx(sv); }
2228 : INLINE GEN
2229 1009864 : polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
2230 : INLINE int
2231 1031867 : F2x_equal1(GEN x) { return Flx_equal1(x); }
2232 : INLINE int
2233 3382243 : F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
2234 : INLINE GEN
2235 56877877 : F2x_copy(GEN x) { return leafcopy(x); }
2236 : INLINE GEN
2237 4074 : F2v_copy(GEN x) { return leafcopy(x); }
2238 : INLINE GEN
2239 2358304 : Flv_copy(GEN x) { return leafcopy(x); }
2240 : INLINE GEN
2241 163213724 : Flx_copy(GEN x) { return leafcopy(x); }
2242 : INLINE GEN
2243 4101144 : vecsmall_copy(GEN x) { return leafcopy(x); }
2244 : INLINE int
2245 7829468 : Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
2246 : INLINE int
2247 19366 : ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
2248 : INLINE int
2249 5721913 : ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
2250 :
2251 : INLINE GEN
2252 122627606 : ZX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2253 : INLINE GEN
2254 98698608 : FpX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2255 : INLINE GEN
2256 676857 : FpXX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2257 : INLINE GEN
2258 4251191 : FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2259 : INLINE GEN
2260 113905170 : F2x_renormalize(GEN x, long lx) { return Flx_renormalize(x,lx); }
2261 : INLINE GEN
2262 38393 : F2v_to_F2x(GEN x, long sv) {
2263 38393 : GEN y = leafcopy(x);
2264 38393 : y[1] = sv; F2x_renormalize(y, lg(y)); return y;
2265 : }
2266 :
2267 : INLINE long
2268 378 : sturm(GEN x) { return sturmpart(x, NULL, NULL); }
2269 :
2270 : INLINE long
2271 4655 : gval(GEN x, long v)
2272 4655 : { pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
2273 :
2274 : INLINE void
2275 490087 : RgX_shift_inplace_init(long v)
2276 490087 : { if (v) (void)cgetg(v, t_VECSMALL); }
2277 : /* shift polynomial in place. assume v free cells have been left before x */
2278 : INLINE GEN
2279 490087 : RgX_shift_inplace(GEN x, long v)
2280 : {
2281 : long i, lx;
2282 : GEN z;
2283 490087 : if (!v) return x;
2284 253165 : lx = lg(x);
2285 253165 : if (lx == 2) return x;
2286 253165 : z = x + lx;
2287 : /* stackdummy's from normalizepol */
2288 253165 : while (lg(z) != v) z += lg(z);
2289 253165 : z += v;
2290 4654452 : for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
2291 675883 : for (i = 0; i < v; i++) gel(--z,0) = gen_0;
2292 253165 : z -= 2;
2293 253165 : z[1] = x[1];
2294 253165 : z[0] = evaltyp(t_POL) | evallg(lx+v);
2295 253165 : stackdummy((pari_sp)z, (pari_sp)x); return z;
2296 : }
2297 :
2298 :
2299 : /* LINEAR ALGEBRA */
2300 : INLINE GEN
2301 486780 : zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
2302 : INLINE GEN
2303 6934102 : zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
2304 : INLINE GEN
2305 3418316 : ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
2306 : INLINE GEN
2307 0 : zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
2308 : INLINE GEN
2309 104139 : zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
2310 : INLINE GEN
2311 0 : zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
2312 : INLINE GEN
2313 0 : zero_zm(long x, long y) { return zero_Flm(x,y); }
2314 : INLINE GEN
2315 28143648 : zero_zv(long x) { return zero_Flv(x); }
2316 : INLINE GEN
2317 266 : zm_transpose(GEN x) { return Flm_transpose(x); }
2318 : INLINE GEN
2319 0 : zm_copy(GEN x) { return Flm_copy(x); }
2320 : INLINE GEN
2321 2130826 : zv_copy(GEN x) { return Flv_copy(x); }
2322 : INLINE GEN
2323 0 : zm_row(GEN x, long i) { return Flm_row(x,i); }
2324 :
2325 : INLINE GEN
2326 9973365 : ZC_hnfrem(GEN x, GEN y) { return ZC_hnfremdiv(x,y,NULL); }
2327 : INLINE GEN
2328 313773 : ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
2329 : INLINE GEN
2330 2514573 : ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
2331 : INLINE void
2332 9173252 : RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
2333 : INLINE GEN
2334 18045332 : RgM_shallowcopy(GEN x)
2335 : {
2336 : long l;
2337 18045332 : GEN y = cgetg_copy(x, &l);
2338 93707065 : while (--l > 0) gel(y,l) = leafcopy(gel(x,l));
2339 18045088 : return y;
2340 : }
2341 : INLINE GEN
2342 199285 : F2m_copy(GEN x) { return RgM_shallowcopy(x); }
2343 :
2344 : INLINE GEN
2345 0 : F3m_copy(GEN x) { return RgM_shallowcopy(x); }
2346 :
2347 : INLINE GEN
2348 5773084 : Flm_copy(GEN x) { return RgM_shallowcopy(x); }
2349 :
2350 : /* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
2351 : * x,y are ZV of the same length */
2352 : INLINE int
2353 23240 : ZV_dvd(GEN x, GEN y)
2354 : {
2355 23240 : long i, l = lg(x);
2356 36722 : for (i=1; i < l; i++)
2357 29575 : if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
2358 7147 : return 1;
2359 : }
2360 :
2361 : /* Fq */
2362 : INLINE GEN
2363 2563488 : Fq_red(GEN x, GEN T, GEN p)
2364 2563488 : { return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
2365 : INLINE GEN
2366 101219 : Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
2367 : {
2368 : (void) p;
2369 101219 : return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
2370 : }
2371 : INLINE GEN
2372 756 : Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
2373 :
2374 : INLINE GEN
2375 14624 : gener_Fq_local(GEN T, GEN p, GEN L)
2376 0 : { return T? gener_FpXQ_local(T,p, L)
2377 14624 : : pgener_Fp_local(p, L); }
2378 :
2379 : /* FpXQX */
2380 : INLINE GEN
2381 4417 : FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
2382 : INLINE GEN
2383 163871 : FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
2384 : INLINE GEN
2385 33376 : F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
2386 :
2387 : INLINE GEN
2388 20006 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
2389 20006 : { return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
2390 :
2391 : /* FqX */
2392 : INLINE GEN
2393 25144 : FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
2394 : INLINE GEN
2395 105343 : FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2396 : INLINE GEN
2397 24313 : FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
2398 : INLINE GEN
2399 66458 : FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2400 : INLINE GEN
2401 546195 : FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
2402 546195 : { return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
2403 : INLINE GEN
2404 404743 : FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
2405 404743 : { return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
2406 : INLINE GEN
2407 364867 : FqX_mul(GEN x, GEN y, GEN T, GEN p)
2408 364867 : { return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
2409 : INLINE GEN
2410 17510 : FqX_mulu(GEN x, ulong y, GEN T, GEN p)
2411 17510 : { return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
2412 : INLINE GEN
2413 69678 : FqX_sqr(GEN x, GEN T, GEN p)
2414 69678 : { return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
2415 : INLINE GEN
2416 1281 : FqX_powu(GEN x, ulong n, GEN T, GEN p)
2417 1281 : { return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
2418 : INLINE GEN
2419 18774 : FqX_halve(GEN x, GEN T, GEN p)
2420 18774 : { return T? FpXX_halve(x, p): FpX_halve(x, p); }
2421 : INLINE GEN
2422 38884 : FqX_div(GEN x, GEN y, GEN T, GEN p)
2423 38884 : { return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
2424 : INLINE GEN
2425 6081 : FqX_get_red(GEN S, GEN T, GEN p)
2426 6081 : { return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
2427 : INLINE GEN
2428 31086 : FqX_rem(GEN x, GEN y, GEN T, GEN p)
2429 31086 : { return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
2430 : INLINE GEN
2431 0 : FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
2432 0 : { return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
2433 : INLINE GEN
2434 5936 : FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
2435 5936 : { return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
2436 : INLINE GEN
2437 0 : FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
2438 0 : {return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
2439 : INLINE GEN
2440 260911 : FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
2441 260911 : {return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
2442 : INLINE GEN
2443 425995 : FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
2444 425995 : { return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
2445 : INLINE GEN
2446 3969 : FqX_normalize(GEN z, GEN T, GEN p)
2447 3969 : { return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
2448 : INLINE GEN
2449 291893 : FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
2450 : INLINE GEN
2451 0 : FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
2452 : INLINE GEN
2453 86456 : FqX_factor(GEN f, GEN T, GEN p)
2454 86456 : { return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
2455 : INLINE GEN
2456 7 : FqX_factor_squarefree(GEN f, GEN T, GEN p)
2457 7 : { return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
2458 : INLINE GEN
2459 7 : FqX_ddf(GEN f, GEN T, GEN p)
2460 7 : { return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
2461 : INLINE GEN
2462 63112 : FqX_degfact(GEN f, GEN T, GEN p)
2463 63112 : { return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
2464 : INLINE GEN
2465 7147 : FqX_roots(GEN f, GEN T, GEN p)
2466 7147 : { return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
2467 : INLINE GEN
2468 175 : FqX_to_mod(GEN f, GEN T, GEN p)
2469 175 : { return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
2470 :
2471 : /*FqXQ*/
2472 : INLINE GEN
2473 0 : FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2474 0 : { (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2475 : INLINE GEN
2476 0 : FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2477 0 : { (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2478 : INLINE GEN
2479 0 : FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
2480 0 : { return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
2481 : INLINE GEN
2482 0 : FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
2483 0 : { return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
2484 : INLINE GEN
2485 0 : FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
2486 0 : { return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
2487 : INLINE GEN
2488 22320 : FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
2489 22320 : { return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
2490 : INLINE GEN
2491 0 : FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
2492 0 : { return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
2493 : INLINE GEN
2494 0 : FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
2495 0 : { return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
2496 :
2497 : /*FqXn*/
2498 : INLINE GEN
2499 6610 : FqXn_expint(GEN x, long n, GEN T, GEN p)
2500 6610 : { return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
2501 : INLINE GEN
2502 0 : FqXn_exp(GEN x, long n, GEN T, GEN p)
2503 0 : { return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
2504 : INLINE GEN
2505 6916 : FqXn_inv(GEN x, long n, GEN T, GEN p)
2506 6916 : { return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
2507 : INLINE GEN
2508 254926 : FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
2509 254926 : { return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
2510 : INLINE GEN
2511 0 : FqXn_sqr(GEN x, long n, GEN T, GEN p)
2512 0 : { return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
2513 :
2514 : /*FpXQ*/
2515 : INLINE GEN
2516 0 : FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
2517 0 : { (void)T; return FpX_add(x,y,p); }
2518 : INLINE GEN
2519 0 : FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
2520 0 : { (void)T; return FpX_sub(x,y,p); }
2521 :
2522 : /*Flxq*/
2523 : INLINE GEN
2524 0 : Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
2525 0 : { (void)T; return Flx_add(x,y,p); }
2526 : INLINE GEN
2527 0 : Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
2528 0 : { (void)T; return Flx_sub(x,y,p); }
2529 :
2530 : /* F2x */
2531 :
2532 : INLINE ulong
2533 539028979 : F2x_coeff(GEN x,long v)
2534 : {
2535 539028979 : ulong u=(ulong)x[2+divsBIL(v)];
2536 539027789 : return (u>>remsBIL(v))&1UL;
2537 : }
2538 :
2539 : INLINE void
2540 12330224 : F2x_clear(GEN x,long v)
2541 : {
2542 12330224 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2543 12330166 : *u&=~(1UL<<remsBIL(v));
2544 12330175 : }
2545 :
2546 : INLINE void
2547 119797422 : F2x_set(GEN x,long v)
2548 : {
2549 119797422 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2550 119781319 : *u|=1UL<<remsBIL(v);
2551 119774916 : }
2552 :
2553 : INLINE void
2554 1564591 : F2x_flip(GEN x,long v)
2555 : {
2556 1564591 : ulong* u=(ulong*)&x[2+divsBIL(v)];
2557 1564591 : *u^=1UL<<remsBIL(v);
2558 1564591 : }
2559 :
2560 : /* F2v */
2561 :
2562 : INLINE ulong
2563 532263321 : F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
2564 :
2565 : INLINE void
2566 12330236 : F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
2567 :
2568 : INLINE void
2569 94176703 : F2v_set(GEN x,long v) { F2x_set(x,v-1); }
2570 :
2571 : INLINE void
2572 1564591 : F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
2573 :
2574 : /* F2m */
2575 :
2576 : INLINE ulong
2577 6852248 : F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
2578 :
2579 : INLINE void
2580 0 : F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
2581 :
2582 : INLINE void
2583 672 : F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
2584 :
2585 : INLINE void
2586 1564592 : F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
2587 :
2588 : /* F3m */
2589 :
2590 : INLINE ulong
2591 1919366 : F3m_coeff(GEN x, long a, long b) { return F3v_coeff(gel(x,b), a); }
2592 :
2593 : INLINE void
2594 0 : F3m_set(GEN x, long a, long b, ulong c) { F3v_set(gel(x,b), a, c); }
2595 :
2596 : /* ARITHMETIC */
2597 : INLINE GEN
2598 34125 : matpascal(long n) { return matqpascal(n, NULL); }
2599 : INLINE long
2600 523444 : Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
2601 : INLINE long
2602 98 : Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
2603 : INLINE GEN
2604 4946709 : sqrti(GEN x) { return sqrtremi(x,NULL); }
2605 : INLINE GEN
2606 128398173 : gaddgs(GEN y, long s) { return gaddsg(s,y); }
2607 : INLINE int
2608 551628 : gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
2609 : INLINE int
2610 24976 : gequalgs(GEN y, long s) { return gequalsg(s,y); }
2611 : INLINE GEN
2612 0 : gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
2613 : INLINE GEN
2614 0 : gminsg(long s, GEN y) { return gmings(y,s); }
2615 : INLINE GEN
2616 47096183 : gmulgs(GEN y, long s) { return gmulsg(s,y); }
2617 : INLINE GEN
2618 132229460 : gmulgu(GEN y, ulong s) { return gmulug(s,y); }
2619 : INLINE GEN
2620 1389562 : gsubgs(GEN y, long s) { return gaddgs(y, -s); }
2621 : INLINE GEN
2622 709451 : gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
2623 :
2624 : INLINE GEN
2625 13508495 : gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
2626 : INLINE GEN
2627 868432 : gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
2628 :
2629 : /* x t_COMPLEX */
2630 : INLINE GEN
2631 77049284 : cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
2632 : /* q t_QUAD */
2633 : INLINE GEN
2634 69860 : quadnorm(GEN q)
2635 : {
2636 69860 : GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
2637 69860 : GEN z, u = gel(q,3), v = gel(q,2);
2638 69860 : if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
2639 : {
2640 69671 : z = signe(b)? mulii(v, addii(u,v)): sqri(v);
2641 69671 : return addii(z, mulii(c, sqri(u)));
2642 : }
2643 : else
2644 : {
2645 189 : z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
2646 189 : return gadd(z, gmul(c, gsqr(u)));
2647 : }
2648 : }
2649 : /* x a t_QUAD, return the attached discriminant */
2650 : INLINE GEN
2651 1071 : quad_disc(GEN x)
2652 : {
2653 1071 : GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
2654 1071 : if (is_pm1(b)) return subsi(1, c4);
2655 154 : togglesign_safe(&c4); return c4;
2656 : }
2657 : INLINE GEN
2658 85015 : qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
2659 : INLINE GEN
2660 18274662 : qfb_disc(GEN x) { return gel(x,4); }
2661 :
2662 : INLINE GEN
2663 2977346 : sqrfrac(GEN x)
2664 : {
2665 2977346 : GEN z = cgetg(3,t_FRAC);
2666 2977346 : gel(z,1) = sqri(gel(x,1));
2667 2977346 : gel(z,2) = sqri(gel(x,2)); return z;
2668 : }
2669 :
2670 : INLINE void
2671 14344255 : normalize_frac(GEN z) {
2672 14344255 : if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
2673 14344255 : }
2674 :
2675 : INLINE GEN
2676 663633 : powii(GEN x, GEN n)
2677 : {
2678 663633 : long ln = lgefint(n);
2679 663633 : if (ln == 3) {
2680 : GEN z;
2681 641111 : if (signe(n) > 0) return powiu(x, n[2]);
2682 69694 : z = cgetg(3, t_FRAC);
2683 69702 : gel(z,1) = gen_1;
2684 69702 : gel(z,2) = powiu(x, n[2]);
2685 69704 : return z;
2686 : }
2687 22522 : if (ln == 2) return gen_1; /* rare */
2688 : /* should never happen */
2689 0 : return powgi(x, n); /* overflow unless x = 0, 1, -1 */
2690 : }
2691 : INLINE GEN
2692 1575 : powIs(long n) {
2693 1575 : switch(n & 3)
2694 : {
2695 63 : case 1: return mkcomplex(gen_0,gen_1);
2696 385 : case 2: return gen_m1;
2697 721 : case 3: return mkcomplex(gen_0,gen_m1);
2698 : }
2699 406 : return gen_1;
2700 : }
2701 :
2702 : /*******************************************************************/
2703 : /* */
2704 : /* ASSIGNMENTS */
2705 : /* */
2706 : /*******************************************************************/
2707 0 : INLINE void mpexpz(GEN x, GEN z)
2708 0 : { pari_sp av = avma; gaffect(mpexp(x), z); set_avma(av); }
2709 0 : INLINE void mplogz(GEN x, GEN z)
2710 0 : { pari_sp av = avma; gaffect(mplog(x), z); set_avma(av); }
2711 0 : INLINE void mpcosz(GEN x, GEN z)
2712 0 : { pari_sp av = avma; gaffect(mpcos(x), z); set_avma(av); }
2713 0 : INLINE void mpsinz(GEN x, GEN z)
2714 0 : { pari_sp av = avma; gaffect(mpsin(x), z); set_avma(av); }
2715 0 : INLINE void gnegz(GEN x, GEN z)
2716 0 : { pari_sp av = avma; gaffect(gneg(x), z); set_avma(av); }
2717 0 : INLINE void gabsz(GEN x, long prec, GEN z)
2718 0 : { pari_sp av = avma; gaffect(gabs(x,prec), z); set_avma(av); }
2719 0 : INLINE void gaddz(GEN x, GEN y, GEN z)
2720 0 : { pari_sp av = avma; gaffect(gadd(x,y), z); set_avma(av); }
2721 0 : INLINE void gsubz(GEN x, GEN y, GEN z)
2722 0 : { pari_sp av = avma; gaffect(gsub(x,y), z); set_avma(av); }
2723 0 : INLINE void gmulz(GEN x, GEN y, GEN z)
2724 0 : { pari_sp av = avma; gaffect(gmul(x,y), z); set_avma(av); }
2725 0 : INLINE void gdivz(GEN x, GEN y, GEN z)
2726 0 : { pari_sp av = avma; gaffect(gdiv(x,y), z); set_avma(av); }
2727 0 : INLINE void gdiventz(GEN x, GEN y, GEN z)
2728 0 : { pari_sp av = avma; gaffect(gdivent(x,y), z); set_avma(av); }
2729 0 : INLINE void gmodz(GEN x, GEN y, GEN z)
2730 0 : { pari_sp av = avma; gaffect(gmod(x,y), z); set_avma(av); }
2731 0 : INLINE void gmul2nz(GEN x, long s, GEN z)
2732 0 : { pari_sp av = avma; gaffect(gmul2n(x,s), z); set_avma(av); }
2733 0 : INLINE void gshiftz(GEN x, long s, GEN z)
2734 0 : { pari_sp av = avma; gaffect(gshift(x,s), z); set_avma(av); }
2735 :
2736 : /*******************************************************************/
2737 : /* */
2738 : /* ELLIPTIC CURVES */
2739 : /* */
2740 : /*******************************************************************/
2741 4215484 : INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
2742 3208994 : INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
2743 4041737 : INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
2744 3978595 : INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
2745 4764758 : INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
2746 3390756 : INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
2747 1058130 : INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
2748 1664176 : INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
2749 1490531 : INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
2750 6094740 : INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
2751 7292781 : INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
2752 9251766 : INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
2753 1044209 : INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
2754 9926449 : INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
2755 1571397 : INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
2756 683553 : INLINE GEN ellff_get_a4a6(GEN x) { return gmael(x, 15, 2); }
2757 1484 : INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
2758 315 : INLINE long ellQp_get_prec(GEN E) { GEN z = ellQp_get_zero(E); return valp(z); }
2759 1134 : INLINE GEN ellQp_get_p(GEN E) { GEN z = ellQp_get_zero(E); return gel(z,2); }
2760 37562 : INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
2761 17246 : INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
2762 735255 : INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
2763 70 : INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
2764 :
2765 4140215 : INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
2766 38713722 : INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
2767 931122 : INLINE GEN ellinf(void) { return mkvec(gen_0); }
2768 :
2769 : /*******************************************************************/
2770 : /* */
2771 : /* ALGEBRAIC NUMBER THEORY */
2772 : /* */
2773 : /*******************************************************************/
2774 16980424 : INLINE GEN modpr_get_pr(GEN x) { return gel(x,3); }
2775 725135 : INLINE GEN modpr_get_p(GEN x) { return pr_get_p(modpr_get_pr(x)); }
2776 7118905 : INLINE GEN modpr_get_T(GEN x) { return lg(x) == 4? NULL: gel(x,4); }
2777 :
2778 69509107 : INLINE GEN pr_get_p(GEN pr) { return gel(pr,1); }
2779 9044121 : INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
2780 : /* .[2] instead of itos works: e and f are small positive integers */
2781 8675370 : INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
2782 24047809 : INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
2783 45533329 : INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
2784 : INLINE int
2785 5837122 : pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
2786 : INLINE GEN
2787 1213271 : pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
2788 : INLINE ulong
2789 339009 : upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
2790 :
2791 : /* assume nf a genuine nf */
2792 : INLINE long
2793 829511 : nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
2794 : INLINE GEN
2795 67496061 : nf_get_pol(GEN nf) { return gel(nf,1); }
2796 : INLINE long
2797 47302098 : nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
2798 : INLINE long
2799 4085376 : nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
2800 : INLINE long
2801 4007 : nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
2802 : INLINE GEN
2803 450552 : nf_get_disc(GEN nf) { return gel(nf,3); }
2804 : INLINE GEN
2805 2459976 : nf_get_index(GEN nf) { return gel(nf,4); }
2806 : INLINE GEN
2807 3938567 : nf_get_M(GEN nf) { return gmael(nf,5,1); }
2808 : INLINE GEN
2809 547115 : nf_get_G(GEN nf) { return gmael(nf,5,2); }
2810 : INLINE GEN
2811 1316310 : nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
2812 : INLINE GEN
2813 22194 : nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
2814 : INLINE GEN
2815 6034 : nf_get_diff(GEN nf) { return gmael(nf,5,5); }
2816 : INLINE GEN
2817 55265 : nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
2818 : INLINE GEN
2819 1234103 : nf_get_roots(GEN nf) { return gel(nf,6); }
2820 : INLINE GEN
2821 1792 : nf_get_zk(GEN nf)
2822 : {
2823 1792 : GEN y = gel(nf,7), D = gel(y, 1);
2824 1792 : if (typ(D) == t_POL) D = gel(D, 2);
2825 1792 : if (!equali1(D)) y = gdiv(y, D);
2826 1792 : return y;
2827 : }
2828 : INLINE GEN
2829 3133280 : nf_get_zkprimpart(GEN nf)
2830 : {
2831 3133280 : GEN y = gel(nf,7);
2832 : /* test for old format of nf.zk: non normalized */
2833 3133280 : if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
2834 3133279 : return y;
2835 : }
2836 : INLINE GEN
2837 3179965 : nf_get_zkden(GEN nf)
2838 : {
2839 3179965 : GEN y = gel(nf,7), D = gel(y,1);
2840 3179965 : if (typ(D) == t_POL) D = gel(D,2);
2841 : /* test for old format of nf.zk: non normalized */
2842 3179965 : if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
2843 3179952 : return D;
2844 : }
2845 : INLINE GEN
2846 7742680 : nf_get_invzk(GEN nf) { return gel(nf,8); }
2847 : INLINE void
2848 283422 : nf_get_sign(GEN nf, long *r1, long *r2)
2849 : {
2850 283422 : GEN x = gel(nf,2);
2851 283422 : *r1 = itou(gel(x,1));
2852 283422 : *r2 = itou(gel(x,2));
2853 283422 : }
2854 :
2855 : INLINE GEN
2856 1495985 : cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
2857 : INLINE GEN
2858 387530 : abgrp_get_no(GEN x) { return gel(x,1); }
2859 : INLINE GEN
2860 5872162 : abgrp_get_cyc(GEN x) { return gel(x,2); }
2861 : INLINE GEN
2862 1024261 : abgrp_get_gen(GEN x) { return gel(x,3); }
2863 : INLINE GEN
2864 5189592 : bnf_get_nf(GEN bnf) { return gel(bnf,7); }
2865 : INLINE GEN
2866 2868691 : bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
2867 : INLINE GEN
2868 8162 : bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
2869 : INLINE GEN
2870 1893306 : bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
2871 : INLINE GEN
2872 967066 : bnf_get_gen(GEN bnf) { return abgrp_get_gen(bnf_get_clgp(bnf)); }
2873 : INLINE GEN
2874 625 : bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
2875 : INLINE GEN
2876 1479194 : bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
2877 : INLINE GEN
2878 661641 : bnf_get_sunits(GEN bnf)
2879 661641 : { GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
2880 : INLINE GEN
2881 289708 : bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
2882 : INLINE long
2883 264405 : bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
2884 : INLINE GEN
2885 287529 : bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
2886 : INLINE GEN
2887 26212 : nfV_to_scalar_or_alg(GEN nf, GEN x)
2888 63261 : { pari_APPLY_same(nf_to_scalar_or_alg(nf, gel(x,i))) }
2889 : INLINE GEN
2890 25777 : bnf_get_fu(GEN bnf) {
2891 25777 : GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
2892 25778 : if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
2893 25778 : return nfV_to_scalar_or_alg(nf, vecslice(fu, 2, lg(fu)-1));
2894 : }
2895 :
2896 : INLINE GEN
2897 1630618 : bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
2898 : INLINE GEN
2899 935265 : bnr_get_bid(GEN bnr) { return gel(bnr,2); }
2900 : INLINE GEN
2901 137676 : bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
2902 : INLINE GEN
2903 565053 : bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
2904 : INLINE GEN
2905 1717621 : bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
2906 : INLINE GEN
2907 311337 : bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
2908 : INLINE GEN
2909 1388956 : bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
2910 : INLINE GEN
2911 70 : bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
2912 : INLINE GEN
2913 7847 : bnr_get_gen(GEN bnr) {
2914 7847 : GEN G = bnr_get_clgp(bnr);
2915 7847 : if (lg(G) != 4)
2916 0 : pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
2917 7847 : return gel(G,3);
2918 : }
2919 :
2920 : /* localstar, used in gchar */
2921 : INLINE GEN
2922 49179 : locs_get_cyc(GEN locs) { return gel(locs,1); }
2923 : INLINE GEN
2924 190792 : locs_get_Lsprk(GEN locs) { return gel(locs,2); }
2925 : INLINE GEN
2926 1155 : locs_get_Lgenfil(GEN locs) { return gel(locs,3); }
2927 : INLINE GEN
2928 7959 : locs_get_mod(GEN locs) { return gel(locs,4); }
2929 : /* pr dividing the modulus N of locs, 0 <= i < v_pr(N)
2930 : * return a t_MAT whose columns are the logs
2931 : * of generators of U_i(pr)/U_{i+1}(pr). */
2932 : INLINE GEN
2933 1155 : locs_get_famod(GEN locs) { return gmael(locs,4,1); }
2934 : INLINE GEN
2935 205996 : locs_get_m_infty(GEN locs) { return gmael(locs,4,2); }
2936 :
2937 : /* G a grossenchar group */
2938 : INLINE GEN
2939 30553 : gchar_get_basis(GEN gc) { return gel(gc, 1); }
2940 : INLINE GEN
2941 205933 : gchar_get_bnf(GEN gc) { return gel(gc, 2); }
2942 : INLINE GEN
2943 237479 : gchar_get_nf(GEN gc) { return gel(gc, 3); }
2944 : INLINE GEN
2945 274565 : gchar_get_zm(GEN gc) { return gel(gc, 4); }
2946 : INLINE GEN
2947 7959 : gchar_get_mod(GEN gc) { return locs_get_mod(gchar_get_zm(gc)); }
2948 : INLINE GEN
2949 3969 : gchar_get_modP(GEN gc) { return gmael(gchar_get_mod(gc),1,1); }
2950 : INLINE GEN
2951 35069 : gchar_get_S(GEN gc) { return gel(gc, 5); }
2952 : INLINE GEN
2953 187845 : gchar_get_DLdata(GEN gc) { return gel(gc, 6); }
2954 : INLINE GEN
2955 1821 : gchar_get_sfu(GEN gc) { return gel(gc, 7); }
2956 : INLINE GEN
2957 15433 : gchar_get_cyc(GEN gc) { return gel(gc, 9); }
2958 : INLINE GEN
2959 0 : gchar_get_hnf(GEN gc) { return gmael(gc, 10, 1); }
2960 : INLINE GEN
2961 0 : gchar_get_U(GEN gc) { return gmael(gc, 10, 2); }
2962 : INLINE GEN
2963 10458 : gchar_get_Ui(GEN gc) { return gmael(gc, 10, 3); }
2964 : INLINE GEN
2965 3233 : gchar_get_m0(GEN gc) { return gel(gc, 11); }
2966 : INLINE GEN
2967 4423 : gchar_get_u0(GEN gc) { return gel(gc, 12); }
2968 : INLINE long
2969 16884 : gchar_get_r1(GEN gc) { return nf_get_r1(gchar_get_nf(gc)); }
2970 : INLINE long
2971 525 : gchar_get_r2(GEN gc) { return nf_get_r2(gchar_get_nf(gc)); }
2972 : INLINE GEN
2973 47746 : gchar_get_loccyc(GEN gc) { return locs_get_cyc(gchar_get_zm(gc)); }
2974 : INLINE long
2975 35265 : gchar_get_nc(GEN gc) { return lg(gchar_get_loccyc(gc))-1; }
2976 : INLINE long
2977 35069 : gchar_get_ns(GEN gc) { return lg(gchar_get_S(gc))-1; }
2978 : INLINE long
2979 1610 : gchar_get_nm(GEN gc) { return lg(gchar_get_basis(gc))-1; }
2980 : INLINE long
2981 3934 : gchar_get_evalprec(GEN gc) { return gmael(gc, 8, 1)[1]; }
2982 : INLINE long
2983 17927 : gchar_get_prec(GEN gc) { return gmael(gc, 8, 1)[2]; }
2984 : INLINE long
2985 12663 : gchar_get_nfprec(GEN gc) { return gmael(gc, 8, 1)[3]; }
2986 : INLINE void
2987 1624 : gchar_set_evalprec(GEN gc, long prec) { gmael(gc, 8, 1)[1] = prec; }
2988 : INLINE void
2989 1118 : gchar_set_prec(GEN gc, long prec) { gmael(gc, 8, 1)[2] = prec; }
2990 : INLINE void
2991 2939 : gchar_set_nfprec(GEN gc, long prec) { gmael(gc, 8, 1)[3] = prec; }
2992 : INLINE long
2993 273 : gchar_get_ntors(GEN gc) { return gmael(gc, 8, 2)[1]; }
2994 : INLINE long
2995 273 : gchar_get_nfree(GEN gc) { return gmael(gc, 8, 2)[2]; }
2996 : INLINE long
2997 1818 : gchar_get_nalg(GEN gc) { return gmael(gc, 8, 2)[3]; }
2998 : INLINE void
2999 1412 : gchar_set_basis(GEN gc, GEN m_inv) { gel(gc, 1) = m_inv; }
3000 : INLINE void
3001 2336 : gchar_set_nf(GEN gc, GEN nf) { gel(gc, 3) = nf; }
3002 : INLINE void
3003 532 : gchar_set_ntors(GEN gc, long n) { gmael(gc, 8, 2)[1] = n; }
3004 : INLINE void
3005 532 : gchar_set_nfree(GEN gc, long n) { gmael(gc, 8, 2)[2] = n; }
3006 : INLINE void
3007 532 : gchar_set_nalg(GEN gc, long n) { gmael(gc, 8, 2)[3] = n; }
3008 : INLINE void
3009 1412 : gchar_set_cyc(GEN gc, GEN cyc) { gel(gc, 9) = cyc; }
3010 : INLINE void
3011 532 : gchar_set_HUUi(GEN gc, GEN hnf, GEN U, GEN Ui) { gel(gc, 10) = mkvec3(hnf, U, Ui); }
3012 : INLINE void
3013 0 : gchar_set_m0(GEN gc, GEN m0) { gel(gc, 11) = m0; }
3014 : INLINE void
3015 1799 : gchar_set_u0(GEN gc, GEN u0) { gel(gc, 12) = u0; }
3016 :
3017 : INLINE GEN
3018 387493 : bid_get_mod(GEN bid) { return gel(bid,1); }
3019 : INLINE GEN
3020 69552 : bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
3021 : INLINE GEN
3022 33579 : bid_get_arch(GEN bid) { return gmael(bid,1,2); }
3023 : INLINE GEN
3024 2414470 : bid_get_grp(GEN bid) { return gel(bid,2); }
3025 : INLINE GEN
3026 1310401 : bid_get_fact(GEN bid) { return gmael(bid,3,1); }
3027 : INLINE GEN
3028 1022813 : bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
3029 : INLINE GEN
3030 989005 : bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
3031 : INLINE GEN
3032 50736 : bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
3033 : INLINE GEN
3034 1214715 : bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
3035 : INLINE GEN
3036 1376831 : bid_get_U(GEN bid) { return gel(bid,5); }
3037 : INLINE GEN
3038 0 : bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
3039 : INLINE GEN
3040 2361296 : bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
3041 : INLINE GEN
3042 0 : bid_get_gen_nocheck(GEN bid) { return abgrp_get_gen(bid_get_grp(bid)); }
3043 : INLINE GEN
3044 52598 : bid_get_gen(GEN bid) {
3045 52598 : GEN G = bid_get_grp(bid);
3046 52598 : if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
3047 52598 : return abgrp_get_gen(G);
3048 : }
3049 :
3050 : INLINE GEN
3051 29861412 : znstar_get_N(GEN G) { return gmael(G,1,1); }
3052 : INLINE GEN
3053 7087129 : znstar_get_faN(GEN G) { return gel(G,3); }
3054 : INLINE GEN
3055 14 : znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
3056 : INLINE GEN
3057 212779 : znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
3058 : INLINE GEN
3059 343 : znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
3060 : INLINE GEN
3061 7743757 : znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
3062 : INLINE GEN
3063 3194765 : znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
3064 : INLINE GEN
3065 46788 : znstar_get_Ui(GEN G) { return gmael(G,4,3); }
3066 : INLINE GEN
3067 140224 : znstar_get_U(GEN G) { return gel(G,5); }
3068 : INLINE GEN
3069 2797242 : znstar_get_pe(GEN G) { return gmael(G,4,1); }
3070 : INLINE GEN
3071 36042 : gal_get_pol(GEN gal) { return gel(gal,1); }
3072 : INLINE GEN
3073 5978 : gal_get_p(GEN gal) { return gmael(gal,2,1); }
3074 : INLINE GEN
3075 91 : gal_get_e(GEN gal) { return gmael(gal,2,2); }
3076 : INLINE GEN
3077 23589 : gal_get_mod(GEN gal) { return gmael(gal,2,3); }
3078 : INLINE GEN
3079 32178 : gal_get_roots(GEN gal) { return gel(gal,3); }
3080 : INLINE GEN
3081 27334 : gal_get_invvdm(GEN gal) { return gel(gal,4); }
3082 : INLINE GEN
3083 27334 : gal_get_den(GEN gal) { return gel(gal,5); }
3084 : INLINE GEN
3085 79336 : gal_get_group(GEN gal) { return gel(gal,6); }
3086 : INLINE GEN
3087 8869 : gal_get_gen(GEN gal) { return gel(gal,7); }
3088 : INLINE GEN
3089 6867 : gal_get_orders(GEN gal) { return gel(gal,8); }
3090 :
3091 : /* assume rnf a genuine rnf */
3092 : INLINE long
3093 1235910 : rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
3094 : INLINE long
3095 16772 : rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
3096 : INLINE long
3097 776489 : rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
3098 : INLINE GEN
3099 707 : rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
3100 : INLINE GEN
3101 756 : rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
3102 : INLINE GEN
3103 868 : rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
3104 : INLINE GEN
3105 442370 : rnf_get_nf(GEN rnf) { return gel(rnf,10); }
3106 : INLINE GEN
3107 4018 : rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
3108 : INLINE GEN
3109 215008 : rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
3110 : INLINE GEN
3111 1483486 : rnf_get_pol(GEN rnf) { return gel(rnf,1); }
3112 : INLINE GEN
3113 175 : rnf_get_disc(GEN rnf) { return gel(rnf,3); }
3114 : INLINE GEN
3115 105 : rnf_get_index(GEN rnf) { return gel(rnf,4); }
3116 : INLINE GEN
3117 1645 : rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
3118 : INLINE long
3119 231 : rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
3120 : INLINE GEN
3121 189378 : rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
3122 : INLINE long
3123 3332 : rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
3124 : INLINE GEN
3125 3591 : rnf_get_zk(GEN rnf) { return gel(rnf,7); }
3126 : INLINE GEN
3127 103544 : rnf_get_map(GEN rnf) { return gel(rnf,11); }
3128 : INLINE GEN
3129 1197 : rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
3130 :
3131 : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
3132 : * T2 matrix. Reduce I wrt G */
3133 : INLINE GEN
3134 673045 : idealpseudored(GEN I, GEN G)
3135 673045 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
3136 :
3137 : /* Same I, G; m in I with T2(m) small */
3138 : INLINE GEN
3139 140094 : idealpseudomin(GEN I, GEN G)
3140 : {
3141 140094 : GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
3142 140096 : return ZM_ZC_mul(I, gel(u,1));
3143 : }
3144 : /* Same I,G; irrational m in I with T2(m) small */
3145 : INLINE GEN
3146 0 : idealpseudomin_nonscalar(GEN I, GEN G)
3147 : {
3148 0 : GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
3149 0 : GEN m = ZM_ZC_mul(I, gel(u,1));
3150 0 : if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
3151 0 : return m;
3152 : }
3153 : /* Same I,G; t_VEC of irrational m in I with T2(m) small */
3154 : INLINE GEN
3155 594370 : idealpseudominvec(GEN I, GEN G)
3156 : {
3157 594370 : long i, j, k, n = lg(I)-1;
3158 594370 : GEN x, L, b = idealpseudored(I, G);
3159 594369 : L = cgetg(1 + (n*(n+1))/2, t_VEC);
3160 2420387 : for (i = k = 1; i <= n; i++)
3161 : {
3162 1826017 : x = gel(b,i);
3163 1826017 : if (!ZV_isscalar(x)) gel(L,k++) = x;
3164 : }
3165 1826019 : for (i = 2; i <= n; i++)
3166 3440190 : for (j = 1; j < i; j++)
3167 : {
3168 2208541 : x = ZC_add(gel(b,i),gel(b,j));
3169 2208541 : if (!ZV_isscalar(x)) gel(L,k++) = x;
3170 : }
3171 594370 : setlg(L,k); return L;
3172 : }
3173 :
3174 : INLINE GEN
3175 13754 : idealred_elt(GEN nf, GEN I) {
3176 13754 : pari_sp av = avma;
3177 13754 : GEN u = idealpseudomin(I, nf_get_roundG(nf));
3178 13754 : return gerepileupto(av, u);
3179 : }
3180 : INLINE GEN
3181 135579 : idealred(GEN nf, GEN I) { return idealred0(nf, I, NULL); }
3182 :
3183 : INLINE GEN
3184 2310 : idealchineseinit(GEN nf, GEN x)
3185 2310 : { return idealchinese(nf,x,NULL); }
3186 :
3187 : /*******************************************************************/
3188 : /* */
3189 : /* CLOSURES */
3190 : /* */
3191 : /*******************************************************************/
3192 344155010 : INLINE long closure_arity(GEN C) { return ((ulong)C[1])&ARITYBITS; }
3193 36570193 : INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
3194 301124493 : INLINE const char *closure_codestr(GEN C) { return GSTR(gel(C,2))-1; }
3195 0 : INLINE GEN closure_get_code(GEN C) { return gel(C,2); }
3196 301111368 : INLINE GEN closure_get_oper(GEN C) { return gel(C,3); }
3197 301090644 : INLINE GEN closure_get_data(GEN C) { return gel(C,4); }
3198 11591 : INLINE GEN closure_get_dbg(GEN C) { return gel(C,5); }
3199 27125 : INLINE GEN closure_get_text(GEN C) { return gel(C,6); }
3200 8023014 : INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
3201 :
3202 : /*******************************************************************/
3203 : /* */
3204 : /* ERRORS */
3205 : /* */
3206 : /*******************************************************************/
3207 : INLINE long
3208 57132 : err_get_num(GEN e) { return e[1]; }
3209 : INLINE GEN
3210 273 : err_get_compo(GEN e, long i) { return gel(e, i+1); }
3211 :
3212 : INLINE void
3213 14 : pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
3214 : INLINE void
3215 7 : pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
3216 : INLINE void
3217 84 : pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
3218 : INLINE void
3219 550 : pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
3220 : INLINE void
3221 0 : pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
3222 : INLINE void
3223 36 : pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
3224 : INLINE void
3225 98 : pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
3226 : INLINE void
3227 469 : pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
3228 : INLINE void
3229 19963 : pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
3230 : INLINE void
3231 63 : pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
3232 : INLINE void
3233 2575 : 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); }
3234 : INLINE void
3235 200 : pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
3236 : INLINE void
3237 0 : pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
3238 : INLINE void
3239 378 : pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
3240 : INLINE void
3241 142 : pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
3242 : INLINE void
3243 239 : pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
3244 : INLINE void
3245 0 : pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
3246 : INLINE void
3247 77 : pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
3248 : INLINE void
3249 756 : pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
3250 : INLINE void
3251 56 : pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
3252 : INLINE void
3253 84 : pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
3254 : INLINE void
3255 13536 : pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
3256 : INLINE void
3257 3395 : pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
3258 : INLINE void
3259 280 : pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
3260 : INLINE void
3261 231 : pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
3262 231 : { pari_err(e_PRIORITY, f,x,op,v); }
|