Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28485 views
License: GPL3
ubuntu2004
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
mkintmod(GEN x, GEN y) { retmkintmod(x,y); }
163
INLINE GEN
164
mkintmodu(ulong x, ulong y) {
165
GEN v = cgetg(3,t_INTMOD);
166
gel(v,1) = utoipos(y);
167
gel(v,2) = utoi(x); return v;
168
}
169
INLINE GEN
170
mkpolmod(GEN x, GEN y) { retmkpolmod(x,y); }
171
INLINE GEN
172
mkfrac(GEN x, GEN y) { retmkfrac(x,y); }
173
INLINE GEN
174
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
Qtoss(GEN q, long *n, long *d)
178
{
179
if (typ(q) == t_INT) { *n = itos(q); *d = 1; }
180
else { *n = itos(gel(q,1)); *d = itou(gel(q,2)); }
181
}
182
INLINE GEN
183
sstoQ(long n, long d)
184
{
185
ulong r;
186
long g, q;
187
if (!n)
188
{
189
if (!d) pari_err_INV("sstoQ",gen_0);
190
return gen_0;
191
}
192
if (d < 0) { d = -d; n = -n; }
193
if (d == 1) return stoi(n);
194
q = udivuu_rem(labs(n),d,&r);
195
if (!r) return n > 0? utoipos(q): utoineg(q);
196
g = ugcd(d,r); /* gcd(n,d) */
197
if (g != 1) { n /= g; d /= g; }
198
retmkfrac(stoi(n), utoi(d));
199
}
200
201
INLINE GEN
202
mkfraccopy(GEN x, GEN y) { retmkfrac(icopy(x), icopy(y)); }
203
INLINE GEN
204
mkrfrac(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
205
gel(v,1) = x; gel(v,2) = y; return v; }
206
INLINE GEN
207
mkrfraccopy(GEN x, GEN y) { GEN v = cgetg(3, t_RFRAC);
208
gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
209
INLINE GEN
210
mkcomplex(GEN x, GEN y) { retmkcomplex(x,y); }
211
INLINE GEN
212
gen_I(void) { return mkcomplex(gen_0, gen_1); }
213
INLINE GEN
214
cgetc(long l) { retmkcomplex(cgetr(l), cgetr(l)); }
215
INLINE GEN
216
mkquad(GEN n, GEN x, GEN y) { GEN v = cgetg(4, t_QUAD);
217
gel(v,1) = n; gel(v,2) = x; gel(v,3) = y; return v; }
218
/* vecsmall */
219
INLINE GEN
220
mkvecsmall(long x) { GEN v = cgetg(2, t_VECSMALL); v[1] = x; return v; }
221
INLINE GEN
222
mkvecsmall2(long x,long y) { GEN v = cgetg(3, t_VECSMALL);
223
v[1]=x; v[2]=y; return v; }
224
INLINE GEN
225
mkvecsmall3(long x,long y,long z) { GEN v = cgetg(4, t_VECSMALL);
226
v[1]=x; v[2]=y; v[3]=z; return v; }
227
INLINE GEN
228
mkvecsmall4(long x,long y,long z,long t) { GEN v = cgetg(5, t_VECSMALL);
229
v[1]=x; v[2]=y; v[3]=z; v[4]=t; return v; }
230
INLINE GEN
231
mkvecsmall5(long x,long y,long z,long t,long u) { GEN v = cgetg(6, t_VECSMALL);
232
v[1]=x; v[2]=y; v[3]=z; v[4]=t; v[5]=u; return v; }
233
234
INLINE GEN
235
mkqfb(GEN x, GEN y, GEN z, GEN d) { retmkqfb(x,y,z,d); }
236
/* vec */
237
INLINE GEN
238
mkvec(GEN x) { retmkvec(x); }
239
INLINE GEN
240
mkvec2(GEN x, GEN y) { retmkvec2(x,y); }
241
INLINE GEN
242
mkvec3(GEN x, GEN y, GEN z) { retmkvec3(x,y,z); }
243
INLINE GEN
244
mkvec4(GEN x, GEN y, GEN z, GEN t) { retmkvec4(x,y,z,t); }
245
INLINE GEN
246
mkvec5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkvec5(x,y,z,t,u); }
247
INLINE GEN
248
mkvecs(long x) { retmkvec(stoi(x)); }
249
INLINE GEN
250
mkvec2s(long x, long y) { retmkvec2(stoi(x),stoi(y)); }
251
INLINE GEN
252
mkvec3s(long x, long y, long z) { retmkvec3(stoi(x),stoi(y),stoi(z)); }
253
INLINE GEN
254
mkvec4s(long x, long y, long z, long t) { retmkvec4(stoi(x),stoi(y),stoi(z),stoi(t)); }
255
INLINE GEN
256
mkveccopy(GEN x) { GEN v = cgetg(2, t_VEC); gel(v,1) = gcopy(x); return v; }
257
INLINE GEN
258
mkvec2copy(GEN x, GEN y) {
259
GEN v = cgetg(3,t_VEC); gel(v,1) = gcopy(x); gel(v,2) = gcopy(y); return v; }
260
/* col */
261
INLINE GEN
262
mkcol(GEN x) { retmkcol(x); }
263
INLINE GEN
264
mkcol2(GEN x, GEN y) { retmkcol2(x,y); }
265
INLINE GEN
266
mkcol3(GEN x, GEN y, GEN z) { retmkcol3(x,y,z); }
267
INLINE GEN
268
mkcol4(GEN x, GEN y, GEN z, GEN t) { retmkcol4(x,y,z,t); }
269
INLINE GEN
270
mkcol5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkcol5(x,y,z,t,u); }
271
INLINE GEN
272
mkcol6(GEN x, GEN y, GEN z, GEN t, GEN u, GEN v) { retmkcol6(x,y,z,t,u,v); }
273
INLINE GEN
274
mkcols(long x) { retmkcol(stoi(x)); }
275
INLINE GEN
276
mkcol2s(long x, long y) { retmkcol2(stoi(x),stoi(y)); }
277
INLINE GEN
278
mkcol3s(long x, long y, long z) { retmkcol3(stoi(x),stoi(y),stoi(z)); }
279
INLINE GEN
280
mkcol4s(long x, long y, long z, long t) { retmkcol4(stoi(x),stoi(y),stoi(z),stoi(t)); }
281
INLINE GEN
282
mkcolcopy(GEN x) { GEN v = cgetg(2, t_COL); gel(v,1) = gcopy(x); return v; }
283
/* mat */
284
INLINE GEN
285
mkmat(GEN x) { retmkmat(x); }
286
INLINE GEN
287
mkmat2(GEN x, GEN y) { retmkmat2(x,y); }
288
INLINE GEN
289
mkmat3(GEN x, GEN y, GEN z) { retmkmat3(x,y,z); }
290
INLINE GEN
291
mkmat4(GEN x, GEN y, GEN z, GEN t) { retmkmat4(x,y,z,t); }
292
INLINE GEN
293
mkmat5(GEN x, GEN y, GEN z, GEN t, GEN u) { retmkmat5(x,y,z,t,u); }
294
INLINE GEN
295
mkmatcopy(GEN x) { GEN v = cgetg(2, t_MAT); gel(v,1) = gcopy(x); return v; }
296
INLINE GEN
297
mkerr(long x) { GEN v = cgetg(2, t_ERROR); v[1] = x; return v; }
298
INLINE GEN
299
mkoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_1; return v; }
300
INLINE GEN
301
mkmoo(void) { GEN v = cgetg(2, t_INFINITY); gel(v,1) = gen_m1; return v; }
302
INLINE long
303
inf_get_sign(GEN x) { return signe(gel(x,1)); }
304
INLINE GEN
305
mkmat22s(long a, long b, long c, long d) {retmkmat2(mkcol2s(a,c),mkcol2s(b,d));}
306
INLINE GEN
307
mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
308
309
/* pol */
310
INLINE GEN
311
pol_x(long v) {
312
GEN p = cgetg(4, t_POL);
313
p[1] = evalsigne(1)|evalvarn(v);
314
gel(p,2) = gen_0;
315
gel(p,3) = gen_1; return p;
316
}
317
/* x^n, assume n >= 0 */
318
INLINE GEN
319
pol_xn(long n, long v) {
320
long i, a = n+2;
321
GEN p = cgetg(a+1, t_POL);
322
p[1] = evalsigne(1)|evalvarn(v);
323
for (i = 2; i < a; i++) gel(p,i) = gen_0;
324
gel(p,a) = gen_1; return p;
325
}
326
/* x^n, no assumption on n */
327
INLINE GEN
328
pol_xnall(long n, long v)
329
{
330
if (n < 0) retmkrfrac(gen_1, pol_xn(-n,v));
331
return pol_xn(n, v);
332
}
333
/* x^n, assume n >= 0 */
334
INLINE GEN
335
polxn_Flx(long n, long sv) {
336
long i, a = n+2;
337
GEN p = cgetg(a+1, t_VECSMALL);
338
p[1] = sv;
339
for (i = 2; i < a; i++) p[i] = 0;
340
p[a] = 1; return p;
341
}
342
INLINE GEN
343
pol_1(long v) {
344
GEN p = cgetg(3, t_POL);
345
p[1] = evalsigne(1)|evalvarn(v);
346
gel(p,2) = gen_1; return p;
347
}
348
INLINE GEN
349
pol_0(long v)
350
{
351
GEN x = cgetg(2,t_POL);
352
x[1] = evalvarn(v); return x;
353
}
354
#define retconst_vec(n,x)\
355
do { long _i, _n = (n);\
356
GEN _v = cgetg(_n+1, t_VEC), _x = (x);\
357
for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
358
return _v; } while(0)
359
INLINE GEN
360
const_vec(long n, GEN x) { retconst_vec(n, x); }
361
#define retconst_col(n,x)\
362
do { long _i, _n = (n);\
363
GEN _v = cgetg(_n+1, t_COL), _x = (x);\
364
for (_i = 1; _i <= _n; _i++) gel(_v,_i) = _x;\
365
return _v; } while(0)
366
INLINE GEN
367
const_col(long n, GEN x) { retconst_col(n, x); }
368
INLINE GEN
369
const_vecsmall(long n, long c)
370
{
371
long i;
372
GEN V = cgetg(n+1,t_VECSMALL);
373
for(i=1;i<=n;i++) V[i] = c;
374
return V;
375
}
376
377
/*** ZERO ***/
378
/* O(p^e) */
379
INLINE GEN
380
zeropadic(GEN p, long e)
381
{
382
GEN y = cgetg(5,t_PADIC);
383
gel(y,4) = gen_0;
384
gel(y,3) = gen_1;
385
gel(y,2) = icopy(p);
386
y[1] = evalvalp(e) | _evalprecp(0);
387
return y;
388
}
389
INLINE GEN
390
zeropadic_shallow(GEN p, long e)
391
{
392
GEN y = cgetg(5,t_PADIC);
393
gel(y,4) = gen_0;
394
gel(y,3) = gen_1;
395
gel(y,2) = p;
396
y[1] = evalvalp(e) | _evalprecp(0);
397
return y;
398
}
399
/* O(pol_x(v)^e) */
400
INLINE GEN
401
zeroser(long v, long e)
402
{
403
GEN x = cgetg(2, t_SER);
404
x[1] = evalvalp(e) | evalvarn(v); return x;
405
}
406
INLINE int
407
ser_isexactzero(GEN x)
408
{
409
if (!signe(x)) switch(lg(x))
410
{
411
case 2: return 1;
412
case 3: return isexactzero(gel(x,2));
413
}
414
return 0;
415
}
416
/* 0 * pol_x(v) */
417
INLINE GEN
418
zeropol(long v) { return pol_0(v); }
419
/* vector(n) */
420
INLINE GEN
421
zerocol(long n)
422
{
423
GEN y = cgetg(n+1,t_COL);
424
long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
425
return y;
426
}
427
/* vectorv(n) */
428
INLINE GEN
429
zerovec(long n)
430
{
431
GEN y = cgetg(n+1,t_VEC);
432
long i; for (i=1; i<=n; i++) gel(y,i) = gen_0;
433
return y;
434
}
435
/* matrix(m, n) */
436
INLINE GEN
437
zeromat(long m, long n)
438
{
439
GEN y = cgetg(n+1,t_MAT);
440
GEN v = zerocol(m);
441
long i; for (i=1; i<=n; i++) gel(y,i) = v;
442
return y;
443
}
444
/* = zero_zx, sv is a evalvarn()*/
445
INLINE GEN
446
zero_Flx(long sv) { return pol0_Flx(sv); }
447
INLINE GEN
448
zero_Flv(long n)
449
{
450
GEN y = cgetg(n+1,t_VECSMALL);
451
long i; for (i=1; i<=n; i++) y[i] = 0;
452
return y;
453
}
454
/* matrix(m, n) */
455
INLINE GEN
456
zero_Flm(long m, long n)
457
{
458
GEN y = cgetg(n+1,t_MAT);
459
GEN v = zero_Flv(m);
460
long i; for (i=1; i<=n; i++) gel(y,i) = v;
461
return y;
462
}
463
/* matrix(m, n) */
464
INLINE GEN
465
zero_Flm_copy(long m, long n)
466
{
467
GEN y = cgetg(n+1,t_MAT);
468
long i; for (i=1; i<=n; i++) gel(y,i) = zero_Flv(m);
469
return y;
470
}
471
472
INLINE GEN
473
zero_F2v(long m)
474
{
475
long l = nbits2nlong(m);
476
GEN v = zero_Flv(l+1);
477
v[1] = m;
478
return v;
479
}
480
481
INLINE GEN
482
zero_F2m(long m, long n)
483
{
484
long i;
485
GEN M = cgetg(n+1, t_MAT);
486
GEN v = zero_F2v(m);
487
for (i = 1; i <= n; i++)
488
gel(M,i) = v;
489
return M;
490
}
491
492
493
INLINE GEN
494
zero_F2m_copy(long m, long n)
495
{
496
long i;
497
GEN M = cgetg(n+1, t_MAT);
498
for (i = 1; i <= n; i++)
499
gel(M,i)= zero_F2v(m);
500
return M;
501
}
502
503
/* matrix(m, n) */
504
INLINE GEN
505
zeromatcopy(long m, long n)
506
{
507
GEN y = cgetg(n+1,t_MAT);
508
long i; for (i=1; i<=n; i++) gel(y,i) = zerocol(m);
509
return y;
510
}
511
512
INLINE GEN
513
zerovec_block(long len)
514
{
515
long i;
516
GEN blk = cgetg_block(len + 1, t_VEC);
517
for (i = 1; i <= len; ++i)
518
gel(blk, i) = gen_0;
519
return blk;
520
}
521
522
/* i-th vector in the standard basis */
523
INLINE GEN
524
col_ei(long n, long i) { GEN e = zerocol(n); gel(e,i) = gen_1; return e; }
525
INLINE GEN
526
vec_ei(long n, long i) { GEN e = zerovec(n); gel(e,i) = gen_1; return e; }
527
INLINE GEN
528
F2v_ei(long n, long i) { GEN e = zero_F2v(n); F2v_set(e,i); return e; }
529
INLINE GEN
530
vecsmall_ei(long n, long i) { GEN e = zero_zv(n); e[i] = 1; return e; }
531
INLINE GEN
532
Rg_col_ei(GEN x, long n, long i) { GEN e = zerocol(n); gel(e,i) = x; return e; }
533
534
INLINE GEN
535
shallowcopy(GEN x)
536
{ return typ(x) == t_MAT ? RgM_shallowcopy(x): leafcopy(x); }
537
538
/* routines for naive growarrays */
539
INLINE GEN
540
vectrunc_init(long l)
541
{
542
GEN z = new_chunk(l);
543
z[0] = evaltyp(t_VEC) | _evallg(1); return z;
544
}
545
INLINE GEN
546
coltrunc_init(long l)
547
{
548
GEN z = new_chunk(l);
549
z[0] = evaltyp(t_COL) | _evallg(1); return z;
550
}
551
INLINE void
552
lg_increase(GEN x) { x[0]++; }
553
INLINE void
554
vectrunc_append(GEN x, GEN t) { gel(x, lg(x)) = t; lg_increase(x); }
555
INLINE void
556
vectrunc_append_batch(GEN x, GEN y)
557
{
558
long i, l = lg(x), ly = lg(y);
559
GEN z = x + l-1;
560
for (i = 1; i < ly; i++) gel(z,i) = gel(y,i);
561
setlg(x, l+ly-1);
562
}
563
INLINE GEN
564
vecsmalltrunc_init(long l)
565
{
566
GEN z = new_chunk(l);
567
z[0] = evaltyp(t_VECSMALL) | _evallg(1); return z;
568
}
569
INLINE void
570
vecsmalltrunc_append(GEN x, long t) { x[ lg(x) ] = t; lg_increase(x); }
571
572
/*******************************************************************/
573
/* */
574
/* STRING HASH FUNCTIONS */
575
/* */
576
/*******************************************************************/
577
INLINE ulong
578
hash_str(const char *str)
579
{
580
ulong hash = 5381UL, c;
581
while ( (c = (ulong)*str++) )
582
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
583
return hash;
584
}
585
INLINE ulong
586
hash_str_len(const char *str, long len)
587
{
588
ulong hash = 5381UL;
589
long i;
590
for (i = 0; i < len; i++)
591
{
592
ulong c = (ulong)*str++;
593
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
594
}
595
return hash;
596
}
597
598
/*******************************************************************/
599
/* */
600
/* VEC / COL / VECSMALL */
601
/* */
602
/*******************************************************************/
603
/* shallow*/
604
INLINE GEN
605
vec_shorten(GEN v, long n)
606
{
607
long i;
608
GEN V = cgetg(n+1,t_VEC);
609
for(i=1;i<=n;i++) gel(V,i) = gel(v,i);
610
return V;
611
}
612
/* shallow*/
613
INLINE GEN
614
vec_lengthen(GEN v, long n)
615
{
616
long i;
617
long l=lg(v);
618
GEN V = cgetg(n+1,t_VEC);
619
for(i=1;i<l;i++) gel(V,i) = gel(v,i);
620
return V;
621
}
622
/* shallow*/
623
INLINE GEN
624
vec_append(GEN V, GEN s)
625
{
626
long i, l2 = lg(V);
627
GEN res = cgetg(l2+1, typ(V));
628
for (i = 1; i < l2; ++i) gel(res, i) = gel(V,i);
629
gel(res,l2) = s; return res;
630
}
631
/* shallow*/
632
INLINE GEN
633
vec_prepend(GEN v, GEN s)
634
{
635
long i, l = lg(v);
636
GEN w = cgetg(l+1, typ(v));
637
gel(w,1) = s;
638
for (i = 2; i <= l; ++i) gel(w,i) = gel(v,i-1);
639
return w;
640
}
641
/* shallow*/
642
INLINE GEN
643
vec_setconst(GEN v, GEN x)
644
{
645
long i, l = lg(v);
646
for (i = 1; i < l; i++) gel(v,i) = x;
647
return v;
648
}
649
INLINE GEN
650
vecsmall_shorten(GEN v, long n)
651
{
652
long i;
653
GEN V = cgetg(n+1,t_VECSMALL);
654
for(i=1;i<=n;i++) V[i] = v[i];
655
return V;
656
}
657
INLINE GEN
658
vecsmall_lengthen(GEN v, long n)
659
{
660
long i, l = lg(v);
661
GEN V = cgetg(n+1,t_VECSMALL);
662
for(i=1;i<l;i++) V[i] = v[i];
663
return V;
664
}
665
666
INLINE GEN
667
vec_to_vecsmall(GEN x)
668
{ pari_APPLY_long(itos(gel(x,i))) }
669
INLINE GEN
670
vecsmall_to_vec(GEN x)
671
{ pari_APPLY_type(t_VEC, stoi(x[i])) }
672
INLINE GEN
673
vecsmall_to_vec_inplace(GEN z)
674
{
675
long i, l = lg(z);
676
for (i=1; i<l; i++) gel(z,i) = stoi(z[i]);
677
settyp(z, t_VEC); return z;
678
}
679
INLINE GEN
680
vecsmall_to_col(GEN x)
681
{ pari_APPLY_type(t_COL, stoi(x[i])) }
682
683
INLINE int
684
vecsmall_lexcmp(GEN x, GEN y)
685
{
686
long lx,ly,l,i;
687
lx = lg(x);
688
ly = lg(y); l = minss(lx,ly);
689
for (i=1; i<l; i++)
690
if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
691
if (lx == ly) return 0;
692
return (lx < ly)? -1 : 1;
693
}
694
695
INLINE int
696
vecsmall_prefixcmp(GEN x, GEN y)
697
{
698
long i, lx = lg(x), ly = lg(y), l = minss(lx,ly);
699
for (i=1; i<l; i++)
700
if (x[i] != y[i]) return x[i]<y[i]? -1: 1;
701
return 0;
702
}
703
704
/*Can be used on t_VEC, but coeffs not gcopy-ed*/
705
INLINE GEN
706
vecsmall_prepend(GEN V, long s)
707
{
708
long i, l2 = lg(V);
709
GEN res = cgetg(l2+1, typ(V));
710
res[1] = s;
711
for (i = 2; i <= l2; ++i) res[i] = V[i - 1];
712
return res;
713
}
714
715
INLINE GEN
716
vecsmall_append(GEN V, long s)
717
{
718
long i, l2 = lg(V);
719
GEN res = cgetg(l2+1, t_VECSMALL);
720
for (i = 1; i < l2; ++i) res[i] = V[i];
721
res[l2] = s; return res;
722
}
723
724
INLINE GEN
725
vecsmall_concat(GEN u, GEN v)
726
{
727
long i, l1 = lg(u)-1, l2 = lg(v)-1;
728
GEN res = cgetg(l1+l2+1, t_VECSMALL);
729
for (i = 1; i <= l1; ++i) res[i] = u[i];
730
for (i = 1; i <= l2; ++i) res[i+l1] = v[i];
731
return res;
732
}
733
734
/* return the number of indices where u and v are equal */
735
INLINE long
736
vecsmall_coincidence(GEN u, GEN v)
737
{
738
long i, s = 0, l = minss(lg(u),lg(v));
739
for(i=1; i<l; i++)
740
if(u[i] == v[i]) s++;
741
return s;
742
}
743
744
/* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
745
INLINE long
746
vecsmall_isin(GEN v, long x)
747
{
748
long i, l = lg(v);
749
for (i = 1; i < l; i++)
750
if (v[i] == x) return i;
751
return 0;
752
}
753
754
INLINE long
755
vecsmall_pack(GEN V, long base, long mod)
756
{
757
long i, s = 0;
758
for(i=1; i<lg(V); i++) s = (base*s + V[i]) % mod;
759
return s;
760
}
761
762
INLINE long
763
vecsmall_indexmax(GEN x)
764
{
765
long i, i0 = 1, t = x[1], lx = lg(x);
766
for (i=2; i<lx; i++)
767
if (x[i] > t) t = x[i0=i];
768
return i0;
769
}
770
771
INLINE long
772
vecsmall_max(GEN x)
773
{
774
long i, t = x[1], lx = lg(x);
775
for (i=2; i<lx; i++)
776
if (x[i] > t) t = x[i];
777
return t;
778
}
779
780
INLINE long
781
vecsmall_indexmin(GEN x)
782
{
783
long i, i0 = 1, t = x[1], lx =lg(x);
784
for (i=2; i<lx; i++)
785
if (x[i] < t) t = x[i0=i];
786
return i0;
787
}
788
789
INLINE long
790
vecsmall_min(GEN x)
791
{
792
long i, t = x[1], lx =lg(x);
793
for (i=2; i<lx; i++)
794
if (x[i] < t) t = x[i];
795
return t;
796
}
797
798
INLINE int
799
ZV_isscalar(GEN x)
800
{
801
long l = lg(x);
802
while (--l > 1)
803
if (signe(gel(x, l))) return 0;
804
return 1;
805
}
806
INLINE int
807
QV_isscalar(GEN x)
808
{
809
long lx = lg(x),i;
810
for (i=2; i<lx; i++)
811
if (!isintzero(gel(x, i))) return 0;
812
return 1;
813
}
814
INLINE int
815
RgV_isscalar(GEN x)
816
{
817
long lx = lg(x),i;
818
for (i=2; i<lx; i++)
819
if (!gequal0(gel(x, i))) return 0;
820
return 1;
821
}
822
INLINE int
823
RgX_isscalar(GEN x)
824
{
825
long i;
826
for (i=lg(x)-1; i>2; i--)
827
if (!gequal0(gel(x, i))) return 0;
828
return 1;
829
}
830
INLINE long
831
RgX_equal_var(GEN x, GEN y) { return varn(x) == varn(y) && RgX_equal(x,y); }
832
INLINE GEN
833
RgX_to_RgV(GEN x, long N) { x = RgX_to_RgC(x, N); settyp(x, t_VEC); return x; }
834
835
INLINE int
836
RgX_is_rational(GEN x)
837
{
838
long i;
839
for (i = lg(x)-1; i > 1; i--)
840
if (!is_rational_t(typ(gel(x,i)))) return 0;
841
return 1;
842
}
843
INLINE int
844
RgX_is_ZX(GEN x)
845
{
846
long i;
847
for (i = lg(x)-1; i > 1; i--)
848
if (typ(gel(x,i)) != t_INT) return 0;
849
return 1;
850
}
851
INLINE int
852
RgX_is_QX(GEN x)
853
{
854
long k = lg(x)-1;
855
for ( ; k>1; k--)
856
if (!is_rational_t(typ(gel(x,k)))) return 0;
857
return 1;
858
}
859
INLINE int
860
RgX_is_monomial(GEN x)
861
{
862
long i;
863
if (!signe(x)) return 0;
864
for (i=lg(x)-2; i>1; i--)
865
if (!isexactzero(gel(x,i))) return 0;
866
return 1;
867
}
868
INLINE int
869
RgV_is_ZV(GEN x)
870
{
871
long i;
872
for (i = lg(x)-1; i > 0; i--)
873
if (typ(gel(x,i)) != t_INT) return 0;
874
return 1;
875
}
876
INLINE int
877
RgV_is_QV(GEN x)
878
{
879
long i;
880
for (i = lg(x)-1; i > 0; i--)
881
if (!is_rational_t(typ(gel(x,i)))) return 0;
882
return 1;
883
}
884
INLINE long
885
RgV_isin_i(GEN v, GEN x, long n)
886
{
887
long i;
888
for (i = 1; i <= n; i++)
889
if (gequal(gel(v,i), x)) return i;
890
return 0;
891
}
892
INLINE long
893
RgV_isin(GEN v, GEN x) { return RgV_isin_i(v, x, lg(v)-1); }
894
895
/********************************************************************/
896
/** **/
897
/** Dynamic arrays implementation **/
898
/** **/
899
/********************************************************************/
900
INLINE void **
901
pari_stack_base(pari_stack *s) { return s->data; }
902
903
INLINE void
904
pari_stack_init(pari_stack *s, size_t size, void **data)
905
{
906
s->data = data;
907
*data = NULL;
908
s->n = 0;
909
s->alloc = 0;
910
s->size = size;
911
}
912
913
INLINE void
914
pari_stack_alloc(pari_stack *s, long nb)
915
{
916
void **sdat = pari_stack_base(s);
917
long alloc = s->alloc;
918
if (s->n+nb <= alloc) return;
919
if (!alloc)
920
alloc = nb;
921
else
922
{
923
while (s->n+nb > alloc) alloc <<= 1;
924
}
925
pari_realloc_ip(sdat,alloc*s->size);
926
s->alloc = alloc;
927
}
928
929
INLINE long
930
pari_stack_new(pari_stack *s) { pari_stack_alloc(s, 1); return s->n++; }
931
932
INLINE void
933
pari_stack_delete(pari_stack *s)
934
{
935
void **sdat = pari_stack_base(s);
936
if (*sdat) pari_free(*sdat);
937
}
938
939
INLINE void
940
pari_stack_pushp(pari_stack *s, void *u)
941
{
942
long n = pari_stack_new(s);
943
void **sdat =(void**) *pari_stack_base(s);
944
sdat[n] = u;
945
}
946
947
/*******************************************************************/
948
/* */
949
/* EXTRACT */
950
/* */
951
/*******************************************************************/
952
INLINE GEN
953
vecslice(GEN A, long y1, long y2)
954
{
955
long i,lB = y2 - y1 + 2;
956
GEN B = cgetg(lB, typ(A));
957
for (i=1; i<lB; i++) B[i] = A[y1-1+i];
958
return B;
959
}
960
INLINE GEN
961
vecslicepermute(GEN A, GEN p, long y1, long y2)
962
{
963
long i,lB = y2 - y1 + 2;
964
GEN B = cgetg(lB, typ(A));
965
for (i=1; i<lB; i++) B[i] = A[p[y1-1+i]];
966
return B;
967
}
968
/* rowslice(rowpermute(A,p), x1, x2) */
969
INLINE GEN
970
rowslicepermute(GEN x, GEN p, long j1, long j2)
971
{ pari_APPLY_same(vecslicepermute(gel(x,i),p,j1,j2)) }
972
973
INLINE GEN
974
rowslice(GEN x, long j1, long j2)
975
{ pari_APPLY_same(vecslice(gel(x,i), j1, j2)) }
976
977
INLINE GEN
978
matslice(GEN A, long x1, long x2, long y1, long y2)
979
{ return rowslice(vecslice(A, y1, y2), x1, x2); }
980
981
/* shallow, remove coeff of index j */
982
INLINE GEN
983
rowsplice(GEN x, long j)
984
{ pari_APPLY_same(vecsplice(gel(x,i), j)) }
985
986
/* shallow, remove coeff of index j */
987
INLINE GEN
988
vecsplice(GEN a, long j)
989
{
990
long i, k, l = lg(a);
991
GEN b;
992
if (l == 1) pari_err(e_MISC, "incorrect component in vecsplice");
993
b = cgetg(l-1, typ(a));
994
for (i = k = 1; i < l; i++)
995
if (i != j) gel(b, k++) = gel(a,i);
996
return b;
997
}
998
/* shallow */
999
INLINE GEN
1000
RgM_minor(GEN a, long i, long j)
1001
{
1002
GEN b = vecsplice(a, j);
1003
long k, l = lg(b);
1004
for (k = 1; k < l; k++) gel(b,k) = vecsplice(gel(b,k), i);
1005
return b;
1006
}
1007
1008
/* A[x0,] */
1009
INLINE GEN
1010
row(GEN x, long j)
1011
{ pari_APPLY_type(t_VEC, gcoeff(x, j, i)) }
1012
INLINE GEN
1013
Flm_row(GEN x, long j)
1014
{ pari_APPLY_ulong((ulong)coeff(x, j, i)) }
1015
/* A[x0,] */
1016
INLINE GEN
1017
rowcopy(GEN x, long j)
1018
{ pari_APPLY_type(t_VEC, gcopy(gcoeff(x, j, i))) }
1019
/* A[x0, x1..x2] */
1020
INLINE GEN
1021
row_i(GEN A, long x0, long x1, long x2)
1022
{
1023
long i, lB = x2 - x1 + 2;
1024
GEN B = cgetg(lB, t_VEC);
1025
for (i=x1; i<=x2; i++) gel(B, i) = gcoeff(A, x0, i);
1026
return B;
1027
}
1028
1029
INLINE GEN
1030
vecreverse(GEN A)
1031
{
1032
long i, l;
1033
GEN B = cgetg_copy(A, &l);
1034
for (i=1; i<l; i++) gel(B, i) = gel(A, l-i);
1035
return B;
1036
}
1037
1038
INLINE GEN
1039
vecsmall_reverse(GEN A)
1040
{
1041
long i, l;
1042
GEN B = cgetg_copy(A, &l);
1043
for (i=1; i<l; i++) B[i] = A[l-i];
1044
return B;
1045
}
1046
1047
INLINE void
1048
vecreverse_inplace(GEN y)
1049
{
1050
long l = lg(y), lim = l>>1, i;
1051
for (i = 1; i <= lim; i++)
1052
{
1053
GEN z = gel(y,i);
1054
gel(y,i) = gel(y,l-i);
1055
gel(y,l-i) = z;
1056
}
1057
}
1058
1059
INLINE GEN
1060
vecsmallpermute(GEN A, GEN p) { return perm_mul(A, p); }
1061
1062
INLINE GEN
1063
vecpermute(GEN A, GEN x)
1064
{ pari_APPLY_type(typ(A), gel(A, x[i])) }
1065
1066
INLINE GEN
1067
rowpermute(GEN x, GEN p)
1068
{ pari_APPLY_same(typ(gel(x,i)) == t_VECSMALL ? vecsmallpermute(gel(x, i), p)
1069
: vecpermute(gel(x, i), p))
1070
}
1071
/*******************************************************************/
1072
/* */
1073
/* PERMUTATIONS */
1074
/* */
1075
/*******************************************************************/
1076
INLINE GEN
1077
identity_zv(long n)
1078
{
1079
GEN v = cgetg(n+1, t_VECSMALL);
1080
long i;
1081
for (i = 1; i <= n; i++) v[i] = i;
1082
return v;
1083
}
1084
INLINE GEN
1085
identity_ZV(long n)
1086
{
1087
GEN v = cgetg(n+1, t_VEC);
1088
long i;
1089
for (i = 1; i <= n; i++) gel(v,i) = utoipos(i);
1090
return v;
1091
}
1092
/* identity permutation */
1093
INLINE GEN
1094
identity_perm(long n) { return identity_zv(n); }
1095
1096
/* assume d <= n */
1097
INLINE GEN
1098
cyclic_perm(long n, long d)
1099
{
1100
GEN perm = cgetg(n+1, t_VECSMALL);
1101
long i;
1102
for (i = 1; i <= n-d; i++) perm[i] = i+d;
1103
for ( ; i <= n; i++) perm[i] = i-n+d;
1104
return perm;
1105
}
1106
1107
/* Multiply (compose) two permutations */
1108
INLINE GEN
1109
perm_mul(GEN s, GEN x)
1110
{ pari_APPLY_long(s[x[i]]) }
1111
1112
INLINE GEN
1113
perm_sqr(GEN x)
1114
{ pari_APPLY_long(x[x[i]]) }
1115
1116
/* Compute the inverse (reciprocal) of a permutation. */
1117
INLINE GEN
1118
perm_inv(GEN x)
1119
{
1120
long i, lx;
1121
GEN y = cgetg_copy(x, &lx);
1122
for (i=1; i<lx; i++) y[ x[i] ] = i;
1123
return y;
1124
}
1125
/* Return s*t*s^-1 */
1126
INLINE GEN
1127
perm_conj(GEN s, GEN t)
1128
{
1129
long i, l;
1130
GEN v = cgetg_copy(s, &l);
1131
for (i = 1; i < l; i++) v[ s[i] ] = s[ t[i] ];
1132
return v;
1133
}
1134
1135
INLINE void
1136
pari_free(void *pointer)
1137
{
1138
BLOCK_SIGINT_START;
1139
free(pointer);
1140
BLOCK_SIGINT_END;
1141
}
1142
INLINE void*
1143
pari_malloc(size_t size)
1144
{
1145
if (size)
1146
{
1147
char *tmp;
1148
BLOCK_SIGINT_START;
1149
tmp = (char*)malloc(size);
1150
BLOCK_SIGINT_END;
1151
if (!tmp) pari_err(e_MEM);
1152
return tmp;
1153
}
1154
return NULL;
1155
}
1156
INLINE void*
1157
pari_realloc(void *pointer, size_t size)
1158
{
1159
char *tmp;
1160
1161
BLOCK_SIGINT_START;
1162
if (!pointer) tmp = (char *) malloc(size);
1163
else tmp = (char *) realloc(pointer,size);
1164
BLOCK_SIGINT_END;
1165
if (!tmp) pari_err(e_MEM);
1166
return tmp;
1167
}
1168
INLINE void
1169
pari_realloc_ip(void **pointer, size_t size)
1170
{
1171
char *tmp;
1172
BLOCK_SIGINT_START;
1173
if (!*pointer) tmp = (char *) malloc(size);
1174
else tmp = (char *) realloc(*pointer,size);
1175
if (!tmp) pari_err(e_MEM);
1176
*pointer = tmp;
1177
BLOCK_SIGINT_END;
1178
}
1179
1180
INLINE void*
1181
pari_calloc(size_t size)
1182
{
1183
void *t = pari_malloc(size);
1184
memset(t, 0, size); return t;
1185
}
1186
INLINE GEN
1187
cgetalloc(long t, size_t l)
1188
{ /* evallg may raise e_OVERFLOW, which would leak x */
1189
ulong x0 = evaltyp(t) | evallg(l);
1190
GEN x = (GEN)pari_malloc(l * sizeof(long));
1191
x[0] = x0; return x;
1192
}
1193
1194
/*******************************************************************/
1195
/* */
1196
/* GARBAGE COLLECTION */
1197
/* */
1198
/*******************************************************************/
1199
/* copy integer x as if we had set_avma(av) */
1200
INLINE GEN
1201
icopy_avma(GEN x, pari_sp av)
1202
{
1203
long i = lgefint(x), lx = i;
1204
GEN y = ((GEN)av) - i;
1205
while (--i > 0) y[i] = x[i];
1206
y[0] = evaltyp(t_INT)|evallg(lx);
1207
return y;
1208
}
1209
/* copy leaf x as if we had set_avma(av) */
1210
INLINE GEN
1211
leafcopy_avma(GEN x, pari_sp av)
1212
{
1213
long i = lg(x);
1214
GEN y = ((GEN)av) - i;
1215
while (--i > 0) y[i] = x[i];
1216
y[0] = x[0] & (~CLONEBIT);
1217
return y;
1218
}
1219
INLINE GEN
1220
gerepileuptoleaf(pari_sp av, GEN x)
1221
{
1222
long lx;
1223
GEN q;
1224
1225
if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1226
lx = lg(x);
1227
q = ((GEN)av) - lx;
1228
set_avma((pari_sp)q);
1229
while (--lx >= 0) q[lx] = x[lx];
1230
return q;
1231
}
1232
INLINE GEN
1233
gerepileuptoint(pari_sp av, GEN x)
1234
{
1235
if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1236
set_avma((pari_sp)icopy_avma(x, av));
1237
return (GEN)avma;
1238
}
1239
INLINE GEN
1240
gerepileupto(pari_sp av, GEN x)
1241
{
1242
if (!isonstack(x) || (GEN)av<=x) return gc_const(av,x);
1243
switch(typ(x))
1244
{ /* non-default = !is_recursive_t(tq) */
1245
case t_INT: return gerepileuptoint(av, x);
1246
case t_REAL:
1247
case t_STR:
1248
case t_VECSMALL: return gerepileuptoleaf(av,x);
1249
default:
1250
/* NB: x+i --> ((long)x) + i*sizeof(long) */
1251
return gerepile(av, (pari_sp) (x+lg(x)), x);
1252
}
1253
}
1254
1255
/* gerepileupto(av, gcopy(x)) */
1256
INLINE GEN
1257
gerepilecopy(pari_sp av, GEN x)
1258
{
1259
if (is_recursive_t(typ(x)))
1260
{
1261
GENbin *p = copy_bin(x);
1262
set_avma(av); return bin_copy(p);
1263
}
1264
else
1265
{
1266
set_avma(av);
1267
if (x < (GEN)av) {
1268
if (x < (GEN)pari_mainstack->bot) new_chunk(lg(x));
1269
x = leafcopy_avma(x, av);
1270
set_avma((pari_sp)x);
1271
} else
1272
x = leafcopy(x);
1273
return x;
1274
}
1275
}
1276
1277
INLINE void
1278
guncloneNULL(GEN x) { if (x) gunclone(x); }
1279
INLINE void
1280
guncloneNULL_deep(GEN x) { if (x) gunclone_deep(x); }
1281
1282
/* Takes an array of pointers to GENs, of length n. Copies all
1283
* objects to contiguous locations and cleans up the stack between
1284
* av and avma. */
1285
INLINE void
1286
gerepilemany(pari_sp av, GEN* gptr[], int n)
1287
{
1288
int i;
1289
for (i=0; i<n; i++) *gptr[i] = (GEN)copy_bin(*gptr[i]);
1290
set_avma(av);
1291
for (i=0; i<n; i++) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1292
}
1293
1294
INLINE void
1295
gerepileall(pari_sp av, int n, ...)
1296
{
1297
int i;
1298
va_list a; va_start(a, n);
1299
if (n < 10)
1300
{
1301
GEN *gptr[10];
1302
for (i=0; i<n; i++)
1303
{ gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
1304
set_avma(av);
1305
for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1306
1307
}
1308
else
1309
{
1310
GEN **gptr = (GEN**) pari_malloc(n*sizeof(GEN*));
1311
for (i=0; i<n; i++)
1312
{ gptr[i] = va_arg(a,GEN*); *gptr[i] = (GEN)copy_bin(*gptr[i]); }
1313
set_avma(av);
1314
for (--i; i>=0; i--) *gptr[i] = bin_copy((GENbin*)*gptr[i]);
1315
pari_free(gptr);
1316
}
1317
va_end(a);
1318
}
1319
1320
INLINE void
1321
gerepilecoeffs(pari_sp av, GEN x, int n)
1322
{
1323
int i;
1324
for (i=0; i<n; i++) gel(x,i) = (GEN)copy_bin(gel(x,i));
1325
set_avma(av);
1326
for (i=0; i<n; i++) gel(x,i) = bin_copy((GENbin*)x[i]);
1327
}
1328
1329
/* p from copy_bin. Copy p->x back to stack, then destroy p */
1330
INLINE GEN
1331
bin_copy(GENbin *p)
1332
{
1333
GEN x, y, base;
1334
long dx, len;
1335
1336
x = p->x; if (!x) { pari_free(p); return gen_0; }
1337
len = p->len;
1338
base= p->base; dx = x - base;
1339
y = (GEN)memcpy((void*)new_chunk(len), (void*)GENbinbase(p), len*sizeof(long));
1340
y += dx;
1341
p->rebase(y, ((ulong)y-(ulong)x));
1342
pari_free(p); return y;
1343
}
1344
1345
INLINE GEN
1346
GENbinbase(GENbin *p) { return (GEN)(p + 1); }
1347
1348
INLINE void
1349
cgiv(GEN x)
1350
{
1351
pari_sp av = (pari_sp)(x+lg(x));
1352
if (isonstack((GEN)av)) set_avma(av);
1353
}
1354
1355
INLINE void
1356
killblock(GEN x) { gunclone(x); }
1357
1358
INLINE int
1359
is_universal_constant(GEN x) { return (x >= gen_0 && x <= ghalf); }
1360
1361
/*******************************************************************/
1362
/* */
1363
/* CONVERSION / ASSIGNMENT */
1364
/* */
1365
/*******************************************************************/
1366
/* z is a type which may be a t_COMPLEX component (not a t_QUAD) */
1367
INLINE GEN
1368
cxcompotor(GEN z, long prec)
1369
{
1370
switch(typ(z))
1371
{
1372
case t_INT: return itor(z, prec);
1373
case t_FRAC: return fractor(z, prec);
1374
case t_REAL: return rtor(z, prec);
1375
default: pari_err_TYPE("cxcompotor",z);
1376
return NULL; /* LCOV_EXCL_LINE */
1377
}
1378
}
1379
INLINE GEN
1380
cxtofp(GEN x, long prec)
1381
{ retmkcomplex(cxcompotor(gel(x,1),prec), cxcompotor(gel(x,2),prec)); }
1382
1383
INLINE GEN
1384
cxtoreal(GEN q)
1385
{ return (typ(q) == t_COMPLEX && gequal0(gel(q,2)))? gel(q,1): q; }
1386
1387
INLINE double
1388
gtodouble(GEN x)
1389
{
1390
if (typ(x)!=t_REAL) {
1391
pari_sp av = avma;
1392
x = gtofp(x, DEFAULTPREC);
1393
if (typ(x)!=t_REAL) pari_err_TYPE("gtodouble [t_REAL expected]", x);
1394
set_avma(av);
1395
}
1396
return rtodbl(x);
1397
}
1398
1399
INLINE int
1400
gisdouble(GEN x, double *g)
1401
{
1402
if (typ(x)!=t_REAL) {
1403
pari_sp av = avma;
1404
x = gtofp(x, DEFAULTPREC);
1405
if (typ(x)!=t_REAL) pari_err_TYPE("gisdouble [t_REAL expected]", x);
1406
set_avma(av);
1407
}
1408
if (expo(x) >= 0x3ff) return 0;
1409
*g = rtodbl(x); return 1;
1410
}
1411
1412
INLINE long
1413
gtos(GEN x) {
1414
if (typ(x) != t_INT) pari_err_TYPE("gtos [integer expected]",x);
1415
return itos(x);
1416
}
1417
1418
INLINE ulong
1419
gtou(GEN x) {
1420
if (typ(x) != t_INT || signe(x)<0)
1421
pari_err_TYPE("gtou [integer >=0 expected]",x);
1422
return itou(x);
1423
}
1424
1425
INLINE GEN
1426
absfrac(GEN x)
1427
{
1428
GEN y = cgetg(3, t_FRAC);
1429
gel(y,1) = absi(gel(x,1));
1430
gel(y,2) = icopy(gel(x,2)); return y;
1431
}
1432
INLINE GEN
1433
absfrac_shallow(GEN x)
1434
{ return signe(gel(x,1))>0? x: mkfrac(negi(gel(x,1)), gel(x,2)); }
1435
INLINE GEN
1436
Q_abs(GEN x) { return (typ(x) == t_INT)? absi(x): absfrac(x); }
1437
INLINE GEN
1438
Q_abs_shallow(GEN x)
1439
{ return (typ(x) == t_INT)? absi_shallow(x): absfrac_shallow(x); }
1440
INLINE GEN
1441
R_abs_shallow(GEN x)
1442
{ return (typ(x) == t_FRAC)? absfrac_shallow(x): mpabs_shallow(x); }
1443
INLINE GEN
1444
R_abs(GEN x)
1445
{ return (typ(x) == t_FRAC)? absfrac(x): mpabs(x); }
1446
1447
/* Force z to be of type real/complex with floating point components */
1448
INLINE GEN
1449
gtofp(GEN z, long prec)
1450
{
1451
switch(typ(z))
1452
{
1453
case t_INT: return itor(z, prec);
1454
case t_FRAC: return fractor(z, prec);
1455
case t_REAL: return rtor(z, prec);
1456
case t_COMPLEX: {
1457
GEN a = gel(z,1), b = gel(z,2);
1458
if (isintzero(b)) return cxcompotor(a, prec);
1459
if (isintzero(a)) {
1460
GEN y = cgetg(3, t_COMPLEX);
1461
b = cxcompotor(b, prec);
1462
gel(y,1) = real_0_bit(expo(b) - prec2nbits(prec));
1463
gel(y,2) = b; return y;
1464
}
1465
return cxtofp(z, prec);
1466
}
1467
case t_QUAD: return quadtofp(z, prec);
1468
default: pari_err_TYPE("gtofp",z);
1469
return NULL; /* LCOV_EXCL_LINE */
1470
}
1471
}
1472
/* Force z to be of type real / int */
1473
INLINE GEN
1474
gtomp(GEN z, long prec)
1475
{
1476
switch(typ(z))
1477
{
1478
case t_INT: return z;
1479
case t_FRAC: return fractor(z, prec);
1480
case t_REAL: return rtor(z, prec);
1481
case t_QUAD: z = quadtofp(z, prec);
1482
if (typ(z) == t_REAL) return z;
1483
default: pari_err_TYPE("gtomp",z);
1484
return NULL; /* LCOV_EXCL_LINE */
1485
}
1486
}
1487
1488
INLINE GEN
1489
RgX_gtofp(GEN x, long prec)
1490
{
1491
long l;
1492
GEN y = cgetg_copy(x, &l);
1493
while (--l > 1) gel(y,l) = gtofp(gel(x,l), prec);
1494
y[1] = x[1]; return y;
1495
}
1496
INLINE GEN
1497
RgC_gtofp(GEN x, long prec)
1498
{ pari_APPLY_type(t_COL, gtofp(gel(x,i), prec)) }
1499
1500
INLINE GEN
1501
RgV_gtofp(GEN x, long prec)
1502
{ pari_APPLY_type(t_VEC, gtofp(gel(x,i), prec)) }
1503
1504
INLINE GEN
1505
RgM_gtofp(GEN x, long prec)
1506
{ pari_APPLY_same(RgC_gtofp(gel(x,i), prec)) }
1507
1508
INLINE GEN
1509
RgC_gtomp(GEN x, long prec)
1510
{ pari_APPLY_type(t_COL, gtomp(gel(x,i), prec)) }
1511
1512
INLINE GEN
1513
RgM_gtomp(GEN x, long prec)
1514
{ pari_APPLY_same(RgC_gtomp(gel(x,i), prec)) }
1515
1516
INLINE GEN
1517
RgX_fpnorml2(GEN x, long prec)
1518
{
1519
pari_sp av = avma;
1520
return gerepileupto(av, gnorml2(RgX_gtofp(x, prec)));
1521
}
1522
INLINE GEN
1523
RgC_fpnorml2(GEN x, long prec)
1524
{
1525
pari_sp av = avma;
1526
return gerepileupto(av, gnorml2(RgC_gtofp(x, prec)));
1527
}
1528
INLINE GEN
1529
RgM_fpnorml2(GEN x, long prec)
1530
{
1531
pari_sp av = avma;
1532
return gerepileupto(av, gnorml2(RgM_gtofp(x, prec)));
1533
}
1534
1535
/* y a t_REAL */
1536
INLINE void
1537
affgr(GEN x, GEN y)
1538
{
1539
pari_sp av;
1540
switch(typ(x)) {
1541
case t_INT: affir(x,y); break;
1542
case t_REAL: affrr(x,y); break;
1543
case t_FRAC: rdiviiz(gel(x,1),gel(x,2), y); break;
1544
case t_QUAD: av = avma; affgr(quadtofp(x,realprec(y)), y); set_avma(av); break;
1545
default: pari_err_TYPE2("=",x,y);
1546
}
1547
}
1548
1549
INLINE GEN
1550
affc_fixlg(GEN x, GEN res)
1551
{
1552
if (typ(x) == t_COMPLEX)
1553
{
1554
affrr_fixlg(gel(x,1), gel(res,1));
1555
affrr_fixlg(gel(x,2), gel(res,2));
1556
}
1557
else
1558
{
1559
set_avma((pari_sp)(res+3));
1560
res = cgetr(realprec(gel(res,1)));
1561
affrr_fixlg(x, res);
1562
}
1563
return res;
1564
}
1565
1566
INLINE GEN
1567
trunc_safe(GEN x) { long e; return gcvtoi(x,&e); }
1568
1569
/*******************************************************************/
1570
/* */
1571
/* LENGTH CONVERSIONS */
1572
/* */
1573
/*******************************************************************/
1574
INLINE long
1575
ndec2nlong(long x) { return 1 + (long)((x)*(LOG2_10/BITS_IN_LONG)); }
1576
INLINE long
1577
ndec2prec(long x) { return 2 + ndec2nlong(x); }
1578
INLINE long
1579
ndec2nbits(long x) { return ndec2nlong(x) << TWOPOTBITS_IN_LONG; }
1580
/* Fast implementation of ceil(x / (8*sizeof(long))); typecast to (ulong)
1581
* to avoid overflow. Faster than 1 + ((x-1)>>TWOPOTBITS_IN_LONG)) :
1582
* addl, shrl instead of subl, sarl, addl */
1583
INLINE long
1584
nbits2nlong(long x) {
1585
return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1586
}
1587
1588
INLINE long
1589
nbits2extraprec(long x) {
1590
return (long)(((ulong)x+BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1591
}
1592
1593
/* Fast implementation of 2 + nbits2nlong(x) */
1594
INLINE long
1595
nbits2prec(long x) {
1596
return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1597
}
1598
INLINE long
1599
nbits2lg(long x) {
1600
return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
1601
}
1602
/* ceil(x / sizeof(long)) */
1603
INLINE long
1604
nchar2nlong(long x) {
1605
return (long)(((ulong)x+sizeof(long)-1) >> (TWOPOTBITS_IN_LONG-3L));
1606
}
1607
INLINE long
1608
prec2nbits(long x) { return (x-2) * BITS_IN_LONG; }
1609
INLINE double
1610
bit_accuracy_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
1611
INLINE double
1612
prec2nbits_mul(long x, double y) { return (x-2) * (BITS_IN_LONG*y); }
1613
INLINE long
1614
bit_prec(GEN x) { return prec2nbits(realprec(x)); }
1615
INLINE long
1616
bit_accuracy(long x) { return prec2nbits(x); }
1617
INLINE long
1618
prec2ndec(long x) { return (long)prec2nbits_mul(x, LOG10_2); }
1619
INLINE long
1620
nbits2ndec(long x) { return (long)(x * LOG10_2); }
1621
INLINE long
1622
precdbl(long x) {return (x - 1) << 1;}
1623
INLINE long
1624
divsBIL(long n) { return n >> TWOPOTBITS_IN_LONG; }
1625
INLINE long
1626
remsBIL(long n) { return n & (BITS_IN_LONG-1); }
1627
1628
/*********************************************************************/
1629
/** **/
1630
/** OPERATIONS MODULO m **/
1631
/** **/
1632
/*********************************************************************/
1633
/* Assume m > 0, more efficient if 0 <= a, b < m */
1634
1635
INLINE GEN
1636
Fp_red(GEN a, GEN m) { return modii(a, m); }
1637
INLINE GEN
1638
Fp_add(GEN a, GEN b, GEN m)
1639
{
1640
pari_sp av=avma;
1641
GEN p = addii(a,b);
1642
long s = signe(p);
1643
if (!s) return p; /* = gen_0 */
1644
if (s > 0) /* general case */
1645
{
1646
GEN t = subii(p, m);
1647
s = signe(t);
1648
if (!s) return gc_const(av, gen_0);
1649
if (s < 0) return gc_const((pari_sp)p, p);
1650
if (cmpii(t, m) < 0) return gerepileuptoint(av, t); /* general case ! */
1651
p = remii(t, m);
1652
}
1653
else
1654
p = modii(p, m);
1655
return gerepileuptoint(av, p);
1656
}
1657
INLINE GEN
1658
Fp_sub(GEN a, GEN b, GEN m)
1659
{
1660
pari_sp av=avma;
1661
GEN p = subii(a,b);
1662
long s = signe(p);
1663
if (!s) return p; /* = gen_0 */
1664
if (s > 0)
1665
{
1666
if (cmpii(p, m) < 0) return p; /* general case ! */
1667
p = remii(p, m);
1668
}
1669
else
1670
{
1671
GEN t = addii(p, m);
1672
if (!s) return gc_const(av, gen_0);
1673
if (s > 0) return gerepileuptoint(av, t); /* general case ! */
1674
p = modii(t, m);
1675
}
1676
return gerepileuptoint(av, p);
1677
}
1678
INLINE GEN
1679
Fp_neg(GEN b, GEN m)
1680
{
1681
pari_sp av = avma;
1682
long s = signe(b);
1683
GEN p;
1684
if (!s) return gen_0;
1685
if (s > 0)
1686
{
1687
p = subii(m, b);
1688
if (signe(p) >= 0) return p; /* general case ! */
1689
p = modii(p, m);
1690
} else
1691
p = remii(negi(b), m);
1692
return gerepileuptoint(av, p);
1693
}
1694
1695
INLINE GEN
1696
Fp_halve(GEN a, GEN p)
1697
{
1698
if (mpodd(a)) a = addii(a,p);
1699
return shifti(a,-1);
1700
}
1701
1702
/* assume 0 <= u < p and ps2 = p>>1 */
1703
INLINE GEN
1704
Fp_center(GEN u, GEN p, GEN ps2)
1705
{ return abscmpii(u,ps2)<=0? icopy(u): subii(u,p); }
1706
/* same without copy */
1707
INLINE GEN
1708
Fp_center_i(GEN u, GEN p, GEN ps2)
1709
{ return abscmpii(u,ps2)<=0? u: subii(u,p); }
1710
1711
/* x + y*z mod p */
1712
INLINE GEN
1713
Fp_addmul(GEN x, GEN y, GEN z, GEN p)
1714
{
1715
pari_sp av;
1716
if (!signe(y) || !signe(z)) return Fp_red(x, p);
1717
if (!signe(x)) return Fp_mul(z,y, p);
1718
av = avma;
1719
return gerepileuptoint(av, modii(addii(x, mulii(y,z)), p));
1720
}
1721
1722
INLINE GEN
1723
Fp_mul(GEN a, GEN b, GEN m)
1724
{
1725
pari_sp av=avma;
1726
GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1727
(void)new_chunk(lg(a)+lg(b)+(lg(m)<<1));
1728
p = mulii(a,b);
1729
set_avma(av); return modii(p,m);
1730
}
1731
INLINE GEN
1732
Fp_sqr(GEN a, GEN m)
1733
{
1734
pari_sp av=avma;
1735
GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1736
(void)new_chunk((lg(a)+lg(m))<<1);
1737
p = sqri(a);
1738
set_avma(av); return remii(p,m); /*Use remii: p >= 0 */
1739
}
1740
INLINE GEN
1741
Fp_mulu(GEN a, ulong b, GEN m)
1742
{
1743
long l = lgefint(m);
1744
if (l == 3)
1745
{
1746
ulong mm = m[2];
1747
return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1748
} else {
1749
pari_sp av = avma;
1750
GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1751
(void)new_chunk(lg(a)+1+(l<<1));
1752
p = muliu(a,b);
1753
set_avma(av); return modii(p,m);
1754
}
1755
}
1756
INLINE GEN
1757
Fp_muls(GEN a, long b, GEN m)
1758
{
1759
long l = lgefint(m);
1760
if (l == 3)
1761
{
1762
ulong mm = m[2];
1763
if (b < 0)
1764
{
1765
ulong t = Fl_mul(umodiu(a, mm), -b, mm);
1766
return t? utoipos(mm - t): gen_0;
1767
}
1768
else
1769
return utoi( Fl_mul(umodiu(a, mm), b, mm) );
1770
} else {
1771
pari_sp av = avma;
1772
GEN p; /*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1773
(void)new_chunk(lg(a)+1+(l<<1));
1774
p = mulis(a,b);
1775
set_avma(av); return modii(p,m);
1776
}
1777
}
1778
1779
INLINE GEN
1780
Fp_inv(GEN a, GEN m)
1781
{
1782
GEN res;
1783
if (! invmod(a,m,&res)) pari_err_INV("Fp_inv", mkintmod(res,m));
1784
return res;
1785
}
1786
INLINE GEN
1787
Fp_invsafe(GEN a, GEN m)
1788
{
1789
GEN res;
1790
if (! invmod(a,m,&res)) return NULL;
1791
return res;
1792
}
1793
INLINE GEN
1794
Fp_div(GEN a, GEN b, GEN m)
1795
{
1796
pari_sp av = avma;
1797
GEN p;
1798
if (lgefint(b) == 3)
1799
{
1800
a = Fp_divu(a, b[2], m);
1801
if (signe(b) < 0) a = Fp_neg(a, m);
1802
return a;
1803
}
1804
/*HACK: assume modii use <=lg(p)+(lg(m)<<1) space*/
1805
(void)new_chunk(lg(a)+(lg(m)<<1));
1806
p = mulii(a, Fp_inv(b,m));
1807
set_avma(av); return modii(p,m);
1808
}
1809
INLINE GEN
1810
Fp_divu(GEN x, ulong a, GEN p)
1811
{
1812
pari_sp av = avma;
1813
ulong b;
1814
if (lgefint(p) == 3)
1815
{
1816
ulong pp = p[2], xp = umodiu(x, pp);
1817
return xp? utoipos(Fl_div(xp, a % pp, pp)): gen_0;
1818
}
1819
x = Fp_red(x, p);
1820
b = Fl_neg(Fl_div(umodiu(x,a), umodiu(p,a), a), a); /* x + pb = 0 (mod a) */
1821
return gerepileuptoint(av, diviuexact(addmuliu(x, p, b), a));
1822
}
1823
1824
INLINE GEN
1825
Flx_mulu(GEN x, ulong a, ulong p) { return Flx_Fl_mul(x,a%p,p); }
1826
1827
INLINE GEN
1828
get_F2x_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1829
1830
INLINE long
1831
get_F2x_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1832
1833
INLINE long
1834
get_F2x_degree(GEN T) { return typ(T)==t_VEC? F2x_degree(gel(T,2)): F2x_degree(T); }
1835
1836
INLINE GEN
1837
get_F2xqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1838
1839
INLINE long
1840
get_F2xqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1841
1842
INLINE long
1843
get_F2xqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1844
1845
INLINE GEN
1846
get_Flx_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1847
1848
INLINE long
1849
get_Flx_var(GEN T) { return typ(T)==t_VEC? mael(T,2,1): T[1]; }
1850
1851
INLINE long
1852
get_Flx_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1853
1854
INLINE GEN
1855
get_FlxqX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1856
1857
INLINE long
1858
get_FlxqX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1859
1860
INLINE long
1861
get_FlxqX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1862
1863
INLINE GEN
1864
get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1865
1866
INLINE long
1867
get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1868
1869
INLINE long
1870
get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1871
1872
INLINE GEN
1873
get_FpXQX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
1874
1875
INLINE long
1876
get_FpXQX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
1877
1878
INLINE long
1879
get_FpXQX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
1880
1881
/*******************************************************************/
1882
/* */
1883
/* ADDMULII / SUBMULII */
1884
/* */
1885
/*******************************************************************/
1886
/* x - y*z */
1887
INLINE GEN
1888
submulii(GEN x, GEN y, GEN z)
1889
{
1890
long lx = lgefint(x), ly, lz;
1891
pari_sp av;
1892
GEN t;
1893
if (lx == 2) { t = mulii(z,y); togglesign(t); return t; }
1894
ly = lgefint(y);
1895
if (ly == 2) return icopy(x);
1896
lz = lgefint(z);
1897
av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1898
t = mulii(z, y);
1899
set_avma(av); return subii(x,t);
1900
}
1901
/* y*z - x */
1902
INLINE GEN
1903
mulsubii(GEN y, GEN z, GEN x)
1904
{
1905
long lx = lgefint(x), ly, lz;
1906
pari_sp av;
1907
GEN t;
1908
if (lx == 2) return mulii(z,y);
1909
ly = lgefint(y);
1910
if (ly == 2) return negi(x);
1911
lz = lgefint(z);
1912
av = avma; (void)new_chunk(lx+ly+lz); /* HACK */
1913
t = mulii(z, y);
1914
set_avma(av); return subii(t,x);
1915
}
1916
1917
/* x - u*y */
1918
INLINE GEN
1919
submuliu(GEN x, GEN y, ulong u)
1920
{
1921
pari_sp av;
1922
long ly = lgefint(y);
1923
if (ly == 2) return icopy(x);
1924
av = avma;
1925
(void)new_chunk(3+ly+lgefint(x)); /* HACK */
1926
y = mului(u,y);
1927
set_avma(av); return subii(x, y);
1928
}
1929
/* x + u*y */
1930
INLINE GEN
1931
addmuliu(GEN x, GEN y, ulong u)
1932
{
1933
pari_sp av;
1934
long ly = lgefint(y);
1935
if (ly == 2) return icopy(x);
1936
av = avma;
1937
(void)new_chunk(3+ly+lgefint(x)); /* HACK */
1938
y = mului(u,y);
1939
set_avma(av); return addii(x, y);
1940
}
1941
/* x - u*y */
1942
INLINE GEN
1943
submuliu_inplace(GEN x, GEN y, ulong u)
1944
{
1945
pari_sp av;
1946
long ly = lgefint(y);
1947
if (ly == 2) return x;
1948
av = avma;
1949
(void)new_chunk(3+ly+lgefint(x)); /* HACK */
1950
y = mului(u,y);
1951
set_avma(av); return subii(x, y);
1952
}
1953
/* x + u*y */
1954
INLINE GEN
1955
addmuliu_inplace(GEN x, GEN y, ulong u)
1956
{
1957
pari_sp av;
1958
long ly = lgefint(y);
1959
if (ly == 2) return x;
1960
av = avma;
1961
(void)new_chunk(3+ly+lgefint(x)); /* HACK */
1962
y = mului(u,y);
1963
set_avma(av); return addii(x, y);
1964
}
1965
/* ux + vy */
1966
INLINE GEN
1967
lincombii(GEN u, GEN v, GEN x, GEN y)
1968
{
1969
long lx = lgefint(x), ly;
1970
GEN p1, p2;
1971
pari_sp av;
1972
if (lx == 2) return mulii(v,y);
1973
ly = lgefint(y);
1974
if (ly == 2) return mulii(u,x);
1975
av = avma; (void)new_chunk(lx+ly+lgefint(u)+lgefint(v)); /* HACK */
1976
p1 = mulii(u,x);
1977
p2 = mulii(v,y);
1978
set_avma(av); return addii(p1,p2);
1979
}
1980
1981
/*******************************************************************/
1982
/* */
1983
/* GEN SUBTYPES */
1984
/* */
1985
/*******************************************************************/
1986
1987
INLINE int
1988
is_const_t(long t) { return (t < t_POLMOD); }
1989
INLINE int
1990
is_extscalar_t(long t) { return (t <= t_POL); }
1991
INLINE int
1992
is_intreal_t(long t) { return (t <= t_REAL); }
1993
INLINE int
1994
is_matvec_t(long t) { return (t >= t_VEC && t <= t_MAT); }
1995
INLINE int
1996
is_noncalc_t(long tx) { return (tx) >= t_LIST; }
1997
INLINE int
1998
is_qfb_t(long t) { return (t == t_QFB); }
1999
INLINE int
2000
is_rational_t(long t) { return (t == t_INT || t == t_FRAC); }
2001
INLINE int
2002
is_real_t(long t) { return (t == t_INT || t == t_REAL || t == t_FRAC); }
2003
INLINE int
2004
is_recursive_t(long t) { return lontyp[t]; }
2005
INLINE int
2006
is_scalar_t(long t) { return (t < t_POL); }
2007
INLINE int
2008
is_vec_t(long t) { return (t == t_VEC || t == t_COL); }
2009
2010
INLINE int
2011
qfb_is_qfi(GEN q) { return signe(gel(q,4)) < 0; }
2012
2013
/*******************************************************************/
2014
/* */
2015
/* TRANSCENDENTAL */
2016
/* */
2017
/*******************************************************************/
2018
INLINE GEN
2019
sqrtr(GEN x) {
2020
long s = signe(x);
2021
if (s == 0) return real_0_bit(expo(x) >> 1);
2022
if (s >= 0) return sqrtr_abs(x);
2023
retmkcomplex(gen_0, sqrtr_abs(x));
2024
}
2025
INLINE GEN
2026
cbrtr_abs(GEN x) { return sqrtnr_abs(x, 3); }
2027
INLINE GEN
2028
cbrtr(GEN x) {
2029
long s = signe(x);
2030
GEN r;
2031
if (s == 0) return real_0_bit(expo(x) / 3);
2032
r = cbrtr_abs(x);
2033
if (s < 0) togglesign(r);
2034
return r;
2035
}
2036
INLINE GEN
2037
sqrtnr(GEN x, long n) {
2038
long s = signe(x);
2039
GEN r;
2040
if (s == 0) return real_0_bit(expo(x) / n);
2041
r = sqrtnr_abs(x, n);
2042
if (s < 0) pari_err_IMPL("sqrtnr for x < 0");
2043
return r;
2044
}
2045
INLINE long
2046
logint(GEN B, GEN y) { return logintall(B,y,NULL); }
2047
INLINE ulong
2048
ulogint(ulong B, ulong y)
2049
{
2050
ulong r;
2051
long e;
2052
if (y == 2) return expu(B);
2053
r = y;
2054
for (e=1;; e++)
2055
{ /* here, r = y^e, r2 = y^(e-1) */
2056
if (r >= B) return r == B? e: e-1;
2057
r = umuluu_or_0(y, r);
2058
if (!r) return e;
2059
}
2060
}
2061
2062
/*******************************************************************/
2063
/* */
2064
/* MISCELLANEOUS */
2065
/* */
2066
/*******************************************************************/
2067
INLINE int ismpzero(GEN x) { return is_intreal_t(typ(x)) && !signe(x); }
2068
INLINE int isintzero(GEN x) { return typ(x) == t_INT && !signe(x); }
2069
INLINE int isint1(GEN x) { return typ(x)==t_INT && equali1(x); }
2070
INLINE int isintm1(GEN x){ return typ(x)==t_INT && equalim1(x);}
2071
INLINE int equali1(GEN n)
2072
{ return (ulong) n[1] == (evallgefint(3UL) | evalsigne(1)) && n[2] == 1; }
2073
INLINE int equalim1(GEN n)
2074
{ return (ulong) n[1] == (evallgefint(3UL) | evalsigne(-1)) && n[2] == 1; }
2075
/* works only for POSITIVE integers */
2076
INLINE int is_pm1(GEN n)
2077
{ return lgefint(n) == 3 && n[2] == 1; }
2078
INLINE int is_bigint(GEN n)
2079
{ long l = lgefint(n); return l > 3 || (l == 3 && (n[2] & HIGHBIT)); }
2080
2081
INLINE int odd(long x) { return x & 1; }
2082
INLINE int both_odd(long x, long y) { return x & y & 1; }
2083
2084
INLINE int
2085
isonstack(GEN x)
2086
{ return ((pari_sp)x >= pari_mainstack->bot
2087
&& (pari_sp)x < pari_mainstack->top); }
2088
2089
/* assume x != 0 and x t_REAL, return an approximation to log2(|x|) */
2090
INLINE double
2091
dbllog2r(GEN x)
2092
{ return log2((double)(ulong)x[2]) + (double)(expo(x) - (BITS_IN_LONG-1)); }
2093
2094
INLINE GEN
2095
mul_content(GEN cx, GEN cy)
2096
{
2097
if (!cx) return cy;
2098
if (!cy) return cx;
2099
return gmul(cx,cy);
2100
}
2101
INLINE GEN
2102
inv_content(GEN c) { return c? ginv(c): NULL; }
2103
INLINE GEN
2104
div_content(GEN cx, GEN cy)
2105
{
2106
if (!cy) return cx;
2107
if (!cx) return ginv(cy);
2108
return gdiv(cx,cy);
2109
}
2110
INLINE GEN
2111
mul_denom(GEN dx, GEN dy)
2112
{
2113
if (!dx) return dy;
2114
if (!dy) return dx;
2115
return mulii(dx,dy);
2116
}
2117
2118
/* POLYNOMIALS */
2119
INLINE GEN
2120
constant_coeff(GEN x) { return signe(x)? gel(x,2): gen_0; }
2121
INLINE GEN
2122
leading_coeff(GEN x) { return lg(x) == 2? gen_0: gel(x,lg(x)-1); }
2123
INLINE ulong
2124
Flx_lead(GEN x) { return lg(x) == 2? 0: x[lg(x)-1]; }
2125
INLINE ulong
2126
Flx_constant(GEN x) { return lg(x) == 2? 0: x[2]; }
2127
INLINE long
2128
degpol(GEN x) { return lg(x)-3; }
2129
INLINE long
2130
lgpol(GEN x) { return lg(x)-2; }
2131
INLINE long
2132
lgcols(GEN x) { return lg(gel(x,1)); }
2133
INLINE long
2134
nbrows(GEN x) { return lg(gel(x,1))-1; }
2135
INLINE GEN
2136
truecoef(GEN x, long n) { return polcoef(x,n,-1); }
2137
2138
INLINE GEN
2139
ZXQ_mul(GEN y, GEN x, GEN T) { return ZX_rem(ZX_mul(y, x), T); }
2140
INLINE GEN
2141
ZXQ_sqr(GEN x, GEN T) { return ZX_rem(ZX_sqr(x), T); }
2142
2143
INLINE GEN
2144
RgX_copy(GEN x)
2145
{
2146
long lx, i;
2147
GEN y = cgetg_copy(x, &lx); y[1] = x[1];
2148
for (i = 2; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
2149
return y;
2150
}
2151
/* have to use ulong to avoid silly warnings from gcc "assuming signed
2152
* overflow does not occur" */
2153
INLINE GEN
2154
RgX_coeff(GEN x, long n)
2155
{
2156
ulong l = lg(x);
2157
return (n < 0 || ((ulong)n+3) > l)? gen_0: gel(x,n+2);
2158
}
2159
INLINE GEN
2160
RgX_renormalize(GEN x) { return RgX_renormalize_lg(x, lg(x)); }
2161
INLINE GEN
2162
RgX_div(GEN x, GEN y) { return RgX_divrem(x,y,NULL); }
2163
INLINE GEN
2164
RgXQX_div(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,NULL); }
2165
INLINE GEN
2166
RgXQX_rem(GEN x, GEN y, GEN T) { return RgXQX_divrem(x,y,T,ONLY_REM); }
2167
INLINE GEN
2168
FpX_div(GEN x, GEN y, GEN p) { return FpX_divrem(x,y,p, NULL); }
2169
INLINE GEN
2170
Flx_div(GEN x, GEN y, ulong p) { return Flx_divrem(x,y,p, NULL); }
2171
INLINE GEN
2172
F2x_div(GEN x, GEN y) { return F2x_divrem(x,y, NULL); }
2173
INLINE GEN
2174
FpV_FpC_mul(GEN x, GEN y, GEN p) { return FpV_dotproduct(x,y,p); }
2175
INLINE GEN
2176
pol0_Flx(long sv) { return mkvecsmall(sv); }
2177
INLINE GEN
2178
pol1_Flx(long sv) { return mkvecsmall2(sv, 1); }
2179
INLINE GEN
2180
polx_Flx(long sv) { return mkvecsmall3(sv, 0, 1); }
2181
INLINE GEN
2182
zero_zx(long sv) { return zero_Flx(sv); }
2183
INLINE GEN
2184
polx_zx(long sv) { return polx_Flx(sv); }
2185
INLINE GEN
2186
zx_shift(GEN x, long n) { return Flx_shift(x,n); }
2187
INLINE GEN
2188
zx_renormalize(GEN x, long l) { return Flx_renormalize(x,l); }
2189
INLINE GEN
2190
zero_F2x(long sv) { return zero_Flx(sv); }
2191
INLINE GEN
2192
pol0_F2x(long sv) { return pol0_Flx(sv); }
2193
INLINE GEN
2194
pol1_F2x(long sv) { return pol1_Flx(sv); }
2195
INLINE GEN
2196
polx_F2x(long sv) { return mkvecsmall2(sv, 2); }
2197
INLINE int
2198
F2x_equal1(GEN x) { return Flx_equal1(x); }
2199
INLINE int
2200
F2x_equal(GEN V, GEN W) { return Flx_equal(V,W); }
2201
INLINE GEN
2202
F2x_copy(GEN x) { return leafcopy(x); }
2203
INLINE GEN
2204
F2v_copy(GEN x) { return leafcopy(x); }
2205
INLINE GEN
2206
Flv_copy(GEN x) { return leafcopy(x); }
2207
INLINE GEN
2208
Flx_copy(GEN x) { return leafcopy(x); }
2209
INLINE GEN
2210
vecsmall_copy(GEN x) { return leafcopy(x); }
2211
INLINE int
2212
Flx_equal1(GEN x) { return degpol(x)==0 && x[2] == 1; }
2213
INLINE int
2214
ZX_equal1(GEN x) { return degpol(x)==0 && equali1(gel(x,2)); }
2215
INLINE int
2216
ZX_is_monic(GEN x) { return equali1(leading_coeff(x)); }
2217
2218
INLINE GEN
2219
ZX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2220
INLINE GEN
2221
FpX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2222
INLINE GEN
2223
FpXX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2224
INLINE GEN
2225
FpXQX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
2226
INLINE GEN
2227
F2x_renormalize(GEN x, long lx) { return Flx_renormalize(x,lx); }
2228
INLINE GEN
2229
F2v_to_F2x(GEN x, long sv) {
2230
GEN y = leafcopy(x);
2231
y[1] = sv; F2x_renormalize(y, lg(y)); return y;
2232
}
2233
2234
INLINE long
2235
sturm(GEN x) { return sturmpart(x, NULL, NULL); }
2236
2237
INLINE long
2238
gval(GEN x, long v)
2239
{ pari_sp av = avma; return gc_long(av, gvaluation(x, pol_x(v))); }
2240
2241
INLINE void
2242
RgX_shift_inplace_init(long v)
2243
{ if (v) (void)cgetg(v, t_VECSMALL); }
2244
/* shift polynomial in place. assume v free cells have been left before x */
2245
INLINE GEN
2246
RgX_shift_inplace(GEN x, long v)
2247
{
2248
long i, lx;
2249
GEN z;
2250
if (!v) return x;
2251
lx = lg(x);
2252
if (lx == 2) return x;
2253
z = x + lx;
2254
/* stackdummy's from normalizepol */
2255
while (lg(z) != v) z += lg(z);
2256
z += v;
2257
for (i = lx-1; i >= 2; i--) gel(--z,0) = gel(x,i);
2258
for (i = 0; i < v; i++) gel(--z,0) = gen_0;
2259
z -= 2;
2260
z[1] = x[1];
2261
z[0] = evaltyp(t_POL) | evallg(lx+v);
2262
stackdummy((pari_sp)z, (pari_sp)x); return z;
2263
}
2264
2265
2266
/* LINEAR ALGEBRA */
2267
INLINE GEN
2268
zv_to_ZV(GEN x) { return vecsmall_to_vec(x); }
2269
INLINE GEN
2270
zc_to_ZC(GEN x) { return vecsmall_to_col(x); }
2271
INLINE GEN
2272
ZV_to_zv(GEN x) { return vec_to_vecsmall(x); }
2273
INLINE GEN
2274
zx_to_zv(GEN x, long N) { return Flx_to_Flv(x,N); }
2275
INLINE GEN
2276
zv_to_zx(GEN x, long sv) { return Flv_to_Flx(x,sv); }
2277
INLINE GEN
2278
zm_to_zxV(GEN x, long sv) { return Flm_to_FlxV(x,sv); }
2279
INLINE GEN
2280
zero_zm(long x, long y) { return zero_Flm(x,y); }
2281
INLINE GEN
2282
zero_zv(long x) { return zero_Flv(x); }
2283
INLINE GEN
2284
zm_transpose(GEN x) { return Flm_transpose(x); }
2285
INLINE GEN
2286
zm_copy(GEN x) { return Flm_copy(x); }
2287
INLINE GEN
2288
zv_copy(GEN x) { return Flv_copy(x); }
2289
INLINE GEN
2290
zm_row(GEN x, long i) { return Flm_row(x,i); }
2291
2292
INLINE GEN
2293
ZC_hnfrem(GEN x, GEN y) { return ZC_hnfremdiv(x,y,NULL); }
2294
INLINE GEN
2295
ZM_hnfrem(GEN x, GEN y) { return ZM_hnfdivrem(x,y,NULL); }
2296
INLINE GEN
2297
ZM_lll(GEN x, double D, long f) { return ZM_lll_norms(x,D,f,NULL); }
2298
INLINE void
2299
RgM_dimensions(GEN x, long *m, long *n) { *n = lg(x)-1; *m = *n? nbrows(x): 0; }
2300
INLINE GEN
2301
RgM_shallowcopy(GEN x)
2302
{
2303
long l;
2304
GEN y = cgetg_copy(x, &l);
2305
while (--l > 0) gel(y,l) = leafcopy(gel(x,l));
2306
return y;
2307
}
2308
INLINE GEN
2309
F2m_copy(GEN x) { return RgM_shallowcopy(x); }
2310
2311
INLINE GEN
2312
F3m_copy(GEN x) { return RgM_shallowcopy(x); }
2313
2314
INLINE GEN
2315
Flm_copy(GEN x) { return RgM_shallowcopy(x); }
2316
2317
/* divisibility: return 1 if y[i] | x[i] for all i, 0 otherwise. Assume
2318
* x,y are ZV of the same length */
2319
INLINE int
2320
ZV_dvd(GEN x, GEN y)
2321
{
2322
long i, l = lg(x);
2323
for (i=1; i < l; i++)
2324
if ( ! dvdii( gel(x,i), gel(y,i) ) ) return 0;
2325
return 1;
2326
}
2327
2328
/* Fq */
2329
INLINE GEN
2330
Fq_red(GEN x, GEN T, GEN p)
2331
{ return typ(x)==t_INT? Fp_red(x,p): FpXQ_red(x,T,p); }
2332
INLINE GEN
2333
Fq_to_FpXQ(GEN x, GEN T, GEN p /*unused*/)
2334
{
2335
(void) p;
2336
return typ(x)==t_INT ? scalarpol(x, get_FpX_var(T)): x;
2337
}
2338
INLINE GEN
2339
Rg_to_Fq(GEN x, GEN T, GEN p) { return T? Rg_to_FpXQ(x,T,p): Rg_to_Fp(x,p); }
2340
2341
INLINE GEN
2342
gener_Fq_local(GEN T, GEN p, GEN L)
2343
{ return T? gener_FpXQ_local(T,p, L)
2344
: pgener_Fp_local(p, L); }
2345
2346
/* FpXQX */
2347
INLINE GEN
2348
FpXQX_div(GEN x, GEN y, GEN T, GEN p) { return FpXQX_divrem(x, y, T, p, NULL); }
2349
INLINE GEN
2350
FlxqX_div(GEN x, GEN y, GEN T, ulong p) { return FlxqX_divrem(x, y, T, p, NULL); }
2351
INLINE GEN
2352
F2xqX_div(GEN x, GEN y, GEN T) { return F2xqX_divrem(x, y, T, NULL); }
2353
2354
INLINE GEN
2355
FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
2356
{ return T ? FpXY_FpXQ_evaly(Q, y, T, p, vx): FpXY_evaly(Q, y, p, vx); }
2357
2358
/* FqX */
2359
INLINE GEN
2360
FqX_red(GEN z, GEN T, GEN p) { return T? FpXQX_red(z, T, p): FpX_red(z, p); }
2361
INLINE GEN
2362
FqX_add(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2363
INLINE GEN
2364
FqX_neg(GEN x,GEN T,GEN p) { return T? FpXX_neg(x,p): FpX_neg(x,p); }
2365
INLINE GEN
2366
FqX_sub(GEN x,GEN y,GEN T,GEN p) { return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2367
INLINE GEN
2368
FqX_Fp_mul(GEN P, GEN u, GEN T, GEN p)
2369
{ return T? FpXX_Fp_mul(P, u, p): FpX_Fp_mul(P, u, p); }
2370
INLINE GEN
2371
FqX_Fq_mul(GEN P, GEN U, GEN T, GEN p)
2372
{ return typ(U)==t_INT ? FqX_Fp_mul(P, U, T, p): FpXQX_FpXQ_mul(P, U, T, p); }
2373
INLINE GEN
2374
FqX_mul(GEN x, GEN y, GEN T, GEN p)
2375
{ return T? FpXQX_mul(x, y, T, p): FpX_mul(x, y, p); }
2376
INLINE GEN
2377
FqX_mulu(GEN x, ulong y, GEN T, GEN p)
2378
{ return T? FpXX_mulu(x, y, p): FpX_mulu(x, y, p); }
2379
INLINE GEN
2380
FqX_sqr(GEN x, GEN T, GEN p)
2381
{ return T? FpXQX_sqr(x, T, p): FpX_sqr(x, p); }
2382
INLINE GEN
2383
FqX_powu(GEN x, ulong n, GEN T, GEN p)
2384
{ return T? FpXQX_powu(x, n, T, p): FpX_powu(x, n, p); }
2385
INLINE GEN
2386
FqX_halve(GEN x, GEN T, GEN p)
2387
{ return T? FpXX_halve(x, p): FpX_halve(x, p); }
2388
INLINE GEN
2389
FqX_div(GEN x, GEN y, GEN T, GEN p)
2390
{ return T? FpXQX_divrem(x,y,T,p,NULL): FpX_divrem(x,y,p,NULL); }
2391
INLINE GEN
2392
FqX_get_red(GEN S, GEN T, GEN p)
2393
{ return T? FpXQX_get_red(S,T,p): FpX_get_red(S,p); }
2394
INLINE GEN
2395
FqX_rem(GEN x, GEN y, GEN T, GEN p)
2396
{ return T? FpXQX_rem(x,y,T,p): FpX_rem(x,y,p); }
2397
INLINE GEN
2398
FqX_divrem(GEN x, GEN y, GEN T, GEN p, GEN *z)
2399
{ return T? FpXQX_divrem(x,y,T,p,z): FpX_divrem(x,y,p,z); }
2400
INLINE GEN
2401
FqX_div_by_X_x(GEN x, GEN y, GEN T, GEN p, GEN *z)
2402
{ return T? FpXQX_div_by_X_x(x,y,T,p,z): FpX_div_by_X_x(x,y,p,z); }
2403
INLINE GEN
2404
FqX_halfgcd(GEN P,GEN Q,GEN T,GEN p)
2405
{return T? FpXQX_halfgcd(P,Q,T,p): FpX_halfgcd(P,Q,p);}
2406
INLINE GEN
2407
FqX_gcd(GEN P,GEN Q,GEN T,GEN p)
2408
{return T? FpXQX_gcd(P,Q,T,p): FpX_gcd(P,Q,p);}
2409
INLINE GEN
2410
FqX_extgcd(GEN P,GEN Q,GEN T,GEN p, GEN *U, GEN *V)
2411
{ return T? FpXQX_extgcd(P,Q,T,p,U,V): FpX_extgcd(P,Q,p,U,V); }
2412
INLINE GEN
2413
FqX_normalize(GEN z, GEN T, GEN p)
2414
{ return T? FpXQX_normalize(z, T, p): FpX_normalize(z, p); }
2415
INLINE GEN
2416
FqX_deriv(GEN f, GEN T, GEN p) { return T? FpXX_deriv(f, p): FpX_deriv(f, p); }
2417
INLINE GEN
2418
FqX_integ(GEN f, GEN T, GEN p) { return T? FpXX_integ(f, p): FpX_integ(f, p); }
2419
INLINE GEN
2420
FqX_factor(GEN f, GEN T, GEN p)
2421
{ return T?FpXQX_factor(f, T, p): FpX_factor(f, p); }
2422
INLINE GEN
2423
FqX_factor_squarefree(GEN f, GEN T, GEN p)
2424
{ return T ? FpXQX_factor_squarefree(f, T, p): FpX_factor_squarefree(f, p); }
2425
INLINE GEN
2426
FqX_ddf(GEN f, GEN T, GEN p)
2427
{ return T ? FpXQX_ddf(f, T, p): FpX_ddf(f, p); }
2428
INLINE GEN
2429
FqX_degfact(GEN f, GEN T, GEN p)
2430
{ return T?FpXQX_degfact(f, T, p): FpX_degfact(f, p); }
2431
INLINE GEN
2432
FqX_roots(GEN f, GEN T, GEN p)
2433
{ return T?FpXQX_roots(f, T, p): FpX_roots(f, p); }
2434
INLINE GEN
2435
FqX_to_mod(GEN f, GEN T, GEN p)
2436
{ return T?FpXQX_to_mod(f, T, p): FpX_to_mod(f, p); }
2437
2438
/*FqXQ*/
2439
INLINE GEN
2440
FqXQ_add(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2441
{ (void)S; return T? FpXX_add(x,y,p): FpX_add(x,y,p); }
2442
INLINE GEN
2443
FqXQ_sub(GEN x, GEN y, GEN S/*unused*/, GEN T, GEN p)
2444
{ (void)S; return T? FpXX_sub(x,y,p): FpX_sub(x,y,p); }
2445
INLINE GEN
2446
FqXQ_div(GEN x, GEN y, GEN S, GEN T, GEN p)
2447
{ return T? FpXQXQ_div(x,y,S,T,p): FpXQ_div(x,y,S,p); }
2448
INLINE GEN
2449
FqXQ_inv(GEN x, GEN S, GEN T, GEN p)
2450
{ return T? FpXQXQ_inv(x,S,T,p): FpXQ_inv(x,S,p); }
2451
INLINE GEN
2452
FqXQ_invsafe(GEN x, GEN S, GEN T, GEN p)
2453
{ return T? FpXQXQ_invsafe(x,S,T,p): FpXQ_inv(x,S,p); }
2454
INLINE GEN
2455
FqXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN p)
2456
{ return T? FpXQXQ_mul(x,y,S,T,p): FpXQ_mul(x,y,S,p); }
2457
INLINE GEN
2458
FqXQ_sqr(GEN x, GEN S, GEN T, GEN p)
2459
{ return T? FpXQXQ_sqr(x,S,T,p): FpXQ_sqr(x,S,p); }
2460
INLINE GEN
2461
FqXQ_pow(GEN x, GEN n, GEN S, GEN T, GEN p)
2462
{ return T? FpXQXQ_pow(x,n,S,T,p): FpXQ_pow(x,n,S,p); }
2463
2464
/*FqXn*/
2465
INLINE GEN
2466
FqXn_expint(GEN x, long n, GEN T, GEN p)
2467
{ return T? FpXQXn_expint(x,n,T,p): FpXn_expint(x,n,p); }
2468
INLINE GEN
2469
FqXn_exp(GEN x, long n, GEN T, GEN p)
2470
{ return T? FpXQXn_exp(x,n,T,p): FpXn_exp(x,n,p); }
2471
INLINE GEN
2472
FqXn_inv(GEN x, long n, GEN T, GEN p)
2473
{ return T? FpXQXn_inv(x,n,T,p): FpXn_inv(x,n,p); }
2474
INLINE GEN
2475
FqXn_mul(GEN x, GEN y, long n, GEN T, GEN p)
2476
{ return T? FpXQXn_mul(x, y, n, T, p): FpXn_mul(x, y, n, p); }
2477
INLINE GEN
2478
FqXn_sqr(GEN x, long n, GEN T, GEN p)
2479
{ return T? FpXQXn_sqr(x,n,T,p): FpXn_sqr(x,n,p); }
2480
2481
/*FpXQ*/
2482
INLINE GEN
2483
FpXQ_add(GEN x,GEN y,GEN T/*unused*/,GEN p)
2484
{ (void)T; return FpX_add(x,y,p); }
2485
INLINE GEN
2486
FpXQ_sub(GEN x,GEN y,GEN T/*unused*/,GEN p)
2487
{ (void)T; return FpX_sub(x,y,p); }
2488
2489
/*Flxq*/
2490
INLINE GEN
2491
Flxq_add(GEN x,GEN y,GEN T/*unused*/,ulong p)
2492
{ (void)T; return Flx_add(x,y,p); }
2493
INLINE GEN
2494
Flxq_sub(GEN x,GEN y,GEN T/*unused*/,ulong p)
2495
{ (void)T; return Flx_sub(x,y,p); }
2496
2497
/* F2x */
2498
2499
INLINE ulong
2500
F2x_coeff(GEN x,long v)
2501
{
2502
ulong u=(ulong)x[2+divsBIL(v)];
2503
return (u>>remsBIL(v))&1UL;
2504
}
2505
2506
INLINE void
2507
F2x_clear(GEN x,long v)
2508
{
2509
ulong* u=(ulong*)&x[2+divsBIL(v)];
2510
*u&=~(1UL<<remsBIL(v));
2511
}
2512
2513
INLINE void
2514
F2x_set(GEN x,long v)
2515
{
2516
ulong* u=(ulong*)&x[2+divsBIL(v)];
2517
*u|=1UL<<remsBIL(v);
2518
}
2519
2520
INLINE void
2521
F2x_flip(GEN x,long v)
2522
{
2523
ulong* u=(ulong*)&x[2+divsBIL(v)];
2524
*u^=1UL<<remsBIL(v);
2525
}
2526
2527
/* F2v */
2528
2529
INLINE ulong
2530
F2v_coeff(GEN x,long v) { return F2x_coeff(x,v-1); }
2531
2532
INLINE void
2533
F2v_clear(GEN x,long v) { F2x_clear(x,v-1); }
2534
2535
INLINE void
2536
F2v_set(GEN x,long v) { F2x_set(x,v-1); }
2537
2538
INLINE void
2539
F2v_flip(GEN x,long v) { F2x_flip(x,v-1); }
2540
2541
/* F2m */
2542
2543
INLINE ulong
2544
F2m_coeff(GEN x, long a, long b) { return F2v_coeff(gel(x,b), a); }
2545
2546
INLINE void
2547
F2m_clear(GEN x, long a, long b) { F2v_clear(gel(x,b), a); }
2548
2549
INLINE void
2550
F2m_set(GEN x, long a, long b) { F2v_set(gel(x,b), a); }
2551
2552
INLINE void
2553
F2m_flip(GEN x, long a, long b) { F2v_flip(gel(x,b), a); }
2554
2555
/* F3m */
2556
2557
INLINE ulong
2558
F3m_coeff(GEN x, long a, long b) { return F3v_coeff(gel(x,b), a); }
2559
2560
INLINE void
2561
F3m_set(GEN x, long a, long b, ulong c) { F3v_set(gel(x,b), a, c); }
2562
2563
/* ARITHMETIC */
2564
INLINE GEN
2565
matpascal(long n) { return matqpascal(n, NULL); }
2566
INLINE long
2567
Z_issquare(GEN x) { return Z_issquareall(x, NULL); }
2568
INLINE long
2569
Z_ispower(GEN x, ulong k) { return Z_ispowerall(x, k, NULL); }
2570
INLINE GEN
2571
sqrti(GEN x) { return sqrtremi(x,NULL); }
2572
INLINE GEN
2573
gaddgs(GEN y, long s) { return gaddsg(s,y); }
2574
INLINE int
2575
gcmpgs(GEN y, long s) { return -gcmpsg(s,y); }
2576
INLINE int
2577
gequalgs(GEN y, long s) { return gequalsg(s,y); }
2578
INLINE GEN
2579
gmaxsg(long s, GEN y) { return gmaxgs(y,s); }
2580
INLINE GEN
2581
gminsg(long s, GEN y) { return gmings(y,s); }
2582
INLINE GEN
2583
gmulgs(GEN y, long s) { return gmulsg(s,y); }
2584
INLINE GEN
2585
gsubgs(GEN y, long s) { return gaddgs(y, -s); }
2586
INLINE GEN
2587
gdivsg(long s, GEN y) { return gdiv(stoi(s), y); }
2588
2589
INLINE GEN
2590
gmax_shallow(GEN x, GEN y) { return gcmp(x,y)<0? y: x; }
2591
INLINE GEN
2592
gmin_shallow(GEN x, GEN y) { return gcmp(x,y)<0? x: y; }
2593
2594
/* x t_COMPLEX */
2595
INLINE GEN
2596
cxnorm(GEN x) { return gadd(gsqr(gel(x,1)), gsqr(gel(x,2))); }
2597
/* q t_QUAD */
2598
INLINE GEN
2599
quadnorm(GEN q)
2600
{
2601
GEN X = gel(q,1), b = gel(X,3), c = gel(X,2);
2602
GEN z, u = gel(q,3), v = gel(q,2);
2603
if (typ(u) == t_INT && typ(v) == t_INT) /* generic case */
2604
{
2605
z = signe(b)? mulii(v, addii(u,v)): sqri(v);
2606
return addii(z, mulii(c, sqri(u)));
2607
}
2608
else
2609
{
2610
z = signe(b)? gmul(v, gadd(u,v)): gsqr(v);
2611
return gadd(z, gmul(c, gsqr(u)));
2612
}
2613
}
2614
/* x a t_QUAD, return the attached discriminant */
2615
INLINE GEN
2616
quad_disc(GEN x)
2617
{
2618
GEN Q = gel(x,1), b = gel(Q,3), c = gel(Q,2), c4 = shifti(c,2);
2619
if (is_pm1(b)) return subsi(1, c4);
2620
togglesign_safe(&c4); return c4;
2621
}
2622
INLINE GEN
2623
qfb_disc3(GEN x, GEN y, GEN z) { return subii(sqri(y), shifti(mulii(x,z),2)); }
2624
INLINE GEN
2625
qfb_disc(GEN x) { return gel(x,4); }
2626
2627
INLINE GEN
2628
sqrfrac(GEN x)
2629
{
2630
GEN z = cgetg(3,t_FRAC);
2631
gel(z,1) = sqri(gel(x,1));
2632
gel(z,2) = sqri(gel(x,2)); return z;
2633
}
2634
2635
INLINE void
2636
normalize_frac(GEN z) {
2637
if (signe(gel(z,2)) < 0) { togglesign(gel(z,1)); setabssign(gel(z,2)); }
2638
}
2639
2640
INLINE GEN
2641
powii(GEN x, GEN n)
2642
{
2643
long ln = lgefint(n);
2644
if (ln == 3) {
2645
GEN z;
2646
if (signe(n) > 0) return powiu(x, n[2]);
2647
z = cgetg(3, t_FRAC);
2648
gel(z,1) = gen_1;
2649
gel(z,2) = powiu(x, n[2]);
2650
return z;
2651
}
2652
if (ln == 2) return gen_1; /* rare */
2653
/* should never happen */
2654
return powgi(x, n); /* overflow unless x = 0, 1, -1 */
2655
}
2656
INLINE GEN
2657
powIs(long n) {
2658
switch(n & 3)
2659
{
2660
case 1: return mkcomplex(gen_0,gen_1);
2661
case 2: return gen_m1;
2662
case 3: return mkcomplex(gen_0,gen_m1);
2663
}
2664
return gen_1;
2665
}
2666
2667
/*******************************************************************/
2668
/* */
2669
/* ASSIGNMENTS */
2670
/* */
2671
/*******************************************************************/
2672
INLINE void mpexpz(GEN x, GEN z)
2673
{ pari_sp av = avma; gaffect(mpexp(x), z); set_avma(av); }
2674
INLINE void mplogz(GEN x, GEN z)
2675
{ pari_sp av = avma; gaffect(mplog(x), z); set_avma(av); }
2676
INLINE void mpcosz(GEN x, GEN z)
2677
{ pari_sp av = avma; gaffect(mpcos(x), z); set_avma(av); }
2678
INLINE void mpsinz(GEN x, GEN z)
2679
{ pari_sp av = avma; gaffect(mpsin(x), z); set_avma(av); }
2680
INLINE void gnegz(GEN x, GEN z)
2681
{ pari_sp av = avma; gaffect(gneg(x), z); set_avma(av); }
2682
INLINE void gabsz(GEN x, long prec, GEN z)
2683
{ pari_sp av = avma; gaffect(gabs(x,prec), z); set_avma(av); }
2684
INLINE void gaddz(GEN x, GEN y, GEN z)
2685
{ pari_sp av = avma; gaffect(gadd(x,y), z); set_avma(av); }
2686
INLINE void gsubz(GEN x, GEN y, GEN z)
2687
{ pari_sp av = avma; gaffect(gsub(x,y), z); set_avma(av); }
2688
INLINE void gmulz(GEN x, GEN y, GEN z)
2689
{ pari_sp av = avma; gaffect(gmul(x,y), z); set_avma(av); }
2690
INLINE void gdivz(GEN x, GEN y, GEN z)
2691
{ pari_sp av = avma; gaffect(gdiv(x,y), z); set_avma(av); }
2692
INLINE void gdiventz(GEN x, GEN y, GEN z)
2693
{ pari_sp av = avma; gaffect(gdivent(x,y), z); set_avma(av); }
2694
INLINE void gmodz(GEN x, GEN y, GEN z)
2695
{ pari_sp av = avma; gaffect(gmod(x,y), z); set_avma(av); }
2696
INLINE void gmul2nz(GEN x, long s, GEN z)
2697
{ pari_sp av = avma; gaffect(gmul2n(x,s), z); set_avma(av); }
2698
INLINE void gshiftz(GEN x, long s, GEN z)
2699
{ pari_sp av = avma; gaffect(gshift(x,s), z); set_avma(av); }
2700
2701
/*******************************************************************/
2702
/* */
2703
/* ELLIPTIC CURVES */
2704
/* */
2705
/*******************************************************************/
2706
INLINE GEN ell_get_a1(GEN e) { return gel(e,1); }
2707
INLINE GEN ell_get_a2(GEN e) { return gel(e,2); }
2708
INLINE GEN ell_get_a3(GEN e) { return gel(e,3); }
2709
INLINE GEN ell_get_a4(GEN e) { return gel(e,4); }
2710
INLINE GEN ell_get_a6(GEN e) { return gel(e,5); }
2711
INLINE GEN ell_get_b2(GEN e) { return gel(e,6); }
2712
INLINE GEN ell_get_b4(GEN e) { return gel(e,7); }
2713
INLINE GEN ell_get_b6(GEN e) { return gel(e,8); }
2714
INLINE GEN ell_get_b8(GEN e) { return gel(e,9); }
2715
INLINE GEN ell_get_c4(GEN e) { return gel(e,10); }
2716
INLINE GEN ell_get_c6(GEN e) { return gel(e,11); }
2717
INLINE GEN ell_get_disc(GEN e) { return gel(e,12); }
2718
INLINE GEN ell_get_j(GEN e) { return gel(e,13); }
2719
INLINE long ell_get_type(GEN e) { return mael(e,14,1); }
2720
INLINE GEN ellff_get_field(GEN x) { return gmael(x, 15, 1); }
2721
INLINE GEN ellff_get_a4a6(GEN x) { return gmael(x, 15, 2); }
2722
INLINE GEN ellQp_get_zero(GEN x) { return gmael(x, 15, 1); }
2723
INLINE long ellQp_get_prec(GEN E) { GEN z = ellQp_get_zero(E); return valp(z); }
2724
INLINE GEN ellQp_get_p(GEN E) { GEN z = ellQp_get_zero(E); return gel(z,2); }
2725
INLINE long ellR_get_prec(GEN x) { return nbits2prec(mael3(x, 15, 1, 1)); }
2726
INLINE long ellR_get_sign(GEN x) { return mael3(x, 15, 1, 2); }
2727
INLINE GEN ellnf_get_nf(GEN x) { return checknf_i(gmael(x,15,1)); }
2728
INLINE GEN ellnf_get_bnf(GEN x) { return checkbnf_i(gmael(x,15,1)); }
2729
2730
INLINE int checkell_i(GEN e) { return typ(e) == t_VEC && lg(e) == 17; }
2731
INLINE int ell_is_inf(GEN z) { return lg(z) == 2; }
2732
INLINE GEN ellinf(void) { return mkvec(gen_0); }
2733
2734
/*******************************************************************/
2735
/* */
2736
/* ALGEBRAIC NUMBER THEORY */
2737
/* */
2738
/*******************************************************************/
2739
INLINE GEN modpr_get_pr(GEN x) { return gel(x,3); }
2740
INLINE GEN modpr_get_p(GEN x) { return pr_get_p(modpr_get_pr(x)); }
2741
INLINE GEN modpr_get_T(GEN x) { return lg(x) == 4? NULL: gel(x,4); }
2742
2743
INLINE GEN pr_get_p(GEN pr) { return gel(pr,1); }
2744
INLINE GEN pr_get_gen(GEN pr){ return gel(pr,2); }
2745
/* .[2] instead of itos works: e and f are small positive integers */
2746
INLINE long pr_get_e(GEN pr) { return gel(pr,3)[2]; }
2747
INLINE long pr_get_f(GEN pr) { return gel(pr,4)[2]; }
2748
INLINE GEN pr_get_tau(GEN pr){ return gel(pr,5); }
2749
INLINE int
2750
pr_is_inert(GEN P) { return typ(pr_get_tau(P)) == t_INT; }
2751
INLINE GEN
2752
pr_norm(GEN pr) { return powiu(pr_get_p(pr), pr_get_f(pr)); }
2753
INLINE ulong
2754
upr_norm(GEN pr) { return upowuu(pr_get_p(pr)[2], pr_get_f(pr)); }
2755
2756
/* assume nf a genuine nf */
2757
INLINE long
2758
nf_get_varn(GEN nf) { return varn(gel(nf,1)); }
2759
INLINE GEN
2760
nf_get_pol(GEN nf) { return gel(nf,1); }
2761
INLINE long
2762
nf_get_degree(GEN nf) { return degpol( nf_get_pol(nf) ); }
2763
INLINE long
2764
nf_get_r1(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,1)); }
2765
INLINE long
2766
nf_get_r2(GEN nf) { GEN x = gel(nf,2); return itou(gel(x,2)); }
2767
INLINE GEN
2768
nf_get_disc(GEN nf) { return gel(nf,3); }
2769
INLINE GEN
2770
nf_get_index(GEN nf) { return gel(nf,4); }
2771
INLINE GEN
2772
nf_get_M(GEN nf) { return gmael(nf,5,1); }
2773
INLINE GEN
2774
nf_get_G(GEN nf) { return gmael(nf,5,2); }
2775
INLINE GEN
2776
nf_get_roundG(GEN nf) { return gmael(nf,5,3); }
2777
INLINE GEN
2778
nf_get_Tr(GEN nf) { return gmael(nf,5,4); }
2779
INLINE GEN
2780
nf_get_diff(GEN nf) { return gmael(nf,5,5); }
2781
INLINE GEN
2782
nf_get_ramified_primes(GEN nf) { return gmael(nf,5,8); }
2783
INLINE GEN
2784
nf_get_roots(GEN nf) { return gel(nf,6); }
2785
INLINE GEN
2786
nf_get_zk(GEN nf)
2787
{
2788
GEN y = gel(nf,7), D = gel(y, 1);
2789
if (typ(D) == t_POL) D = gel(D, 2);
2790
if (!equali1(D)) y = gdiv(y, D);
2791
return y;
2792
}
2793
INLINE GEN
2794
nf_get_zkprimpart(GEN nf)
2795
{
2796
GEN y = gel(nf,7);
2797
/* test for old format of nf.zk: non normalized */
2798
if (!equali1(gel(nf,4)) && gequal1(gel(y,1))) y = Q_remove_denom(y,NULL);
2799
return y;
2800
}
2801
INLINE GEN
2802
nf_get_zkden(GEN nf)
2803
{
2804
GEN y = gel(nf,7), D = gel(y,1);
2805
if (typ(D) == t_POL) D = gel(D,2);
2806
/* test for old format of nf.zk: non normalized */
2807
if (!equali1(gel(nf,4)) && equali1(D)) D = Q_denom(y);
2808
return D;
2809
}
2810
INLINE GEN
2811
nf_get_invzk(GEN nf) { return gel(nf,8); }
2812
INLINE void
2813
nf_get_sign(GEN nf, long *r1, long *r2)
2814
{
2815
GEN x = gel(nf,2);
2816
*r1 = itou(gel(x,1));
2817
*r2 = itou(gel(x,2));
2818
}
2819
2820
INLINE GEN
2821
cyc_get_expo(GEN c) { return lg(c) == 1? gen_1: gel(c,1); }
2822
INLINE GEN
2823
abgrp_get_no(GEN x) { return gel(x,1); }
2824
INLINE GEN
2825
abgrp_get_cyc(GEN x) { return gel(x,2); }
2826
INLINE GEN
2827
abgrp_get_gen(GEN x) { return gel(x,3); }
2828
INLINE GEN
2829
bnf_get_nf(GEN bnf) { return gel(bnf,7); }
2830
INLINE GEN
2831
bnf_get_clgp(GEN bnf) { return gmael(bnf,8,1); }
2832
INLINE GEN
2833
bnf_get_no(GEN bnf) { return abgrp_get_no(bnf_get_clgp(bnf)); }
2834
INLINE GEN
2835
bnf_get_cyc(GEN bnf) { return abgrp_get_cyc(bnf_get_clgp(bnf)); }
2836
INLINE GEN
2837
bnf_get_gen(GEN bnf) { return abgrp_get_gen(bnf_get_clgp(bnf)); }
2838
INLINE GEN
2839
bnf_get_reg(GEN bnf) { return gmael(bnf,8,2); }
2840
INLINE GEN
2841
bnf_get_logfu(GEN bnf) { return gel(bnf,3); }
2842
INLINE GEN
2843
bnf_get_sunits(GEN bnf)
2844
{ GEN s = gmael(bnf,8,3); return typ(s) == t_INT? NULL: s; }
2845
INLINE GEN
2846
bnf_get_tuU(GEN bnf) { return gmael3(bnf,8,4,2); }
2847
INLINE long
2848
bnf_get_tuN(GEN bnf) { return gmael3(bnf,8,4,1)[2]; }
2849
INLINE GEN
2850
bnf_get_fu_nocheck(GEN bnf) { return gmael(bnf,8,5); }
2851
INLINE GEN
2852
bnf_get_fu(GEN bnf) {
2853
GEN fu = bnf_build_units(bnf), nf = bnf_get_nf(bnf);
2854
long i, l;
2855
if (typ(fu) == t_MAT) pari_err(e_MISC,"missing units in bnf");
2856
l = lg(fu)-1; fu = vecslice(fu, 2, l);
2857
for (i = 1; i < l; i++) gel(fu,i) = nf_to_scalar_or_alg(nf, gel(fu,i));
2858
return fu;
2859
}
2860
2861
INLINE GEN
2862
bnr_get_bnf(GEN bnr) { return gel(bnr,1); }
2863
INLINE GEN
2864
bnr_get_bid(GEN bnr) { return gel(bnr,2); }
2865
INLINE GEN
2866
bnr_get_mod(GEN bnr) { return gmael(bnr,2,1); }
2867
INLINE GEN
2868
bnr_get_nf(GEN bnr) { return gmael(bnr,1,7); }
2869
INLINE GEN
2870
bnr_get_clgp(GEN bnr) { return gel(bnr,5); }
2871
INLINE GEN
2872
bnr_get_no(GEN bnr) { return abgrp_get_no(bnr_get_clgp(bnr)); }
2873
INLINE GEN
2874
bnr_get_cyc(GEN bnr) { return abgrp_get_cyc(bnr_get_clgp(bnr)); }
2875
INLINE GEN
2876
bnr_get_gen_nocheck(GEN bnr) { return abgrp_get_gen(bnr_get_clgp(bnr)); }
2877
INLINE GEN
2878
bnr_get_gen(GEN bnr) {
2879
GEN G = bnr_get_clgp(bnr);
2880
if (lg(G) != 4)
2881
pari_err(e_MISC,"missing bnr generators: please use bnrinit(,,1)");
2882
return gel(G,3);
2883
}
2884
2885
INLINE GEN
2886
bid_get_mod(GEN bid) { return gel(bid,1); }
2887
INLINE GEN
2888
bid_get_ideal(GEN bid) { return gmael(bid,1,1); }
2889
INLINE GEN
2890
bid_get_arch(GEN bid) { return gmael(bid,1,2); }
2891
INLINE GEN
2892
bid_get_grp(GEN bid) { return gel(bid,2); }
2893
INLINE GEN
2894
bid_get_fact(GEN bid) { return gmael(bid,3,1); }
2895
INLINE GEN
2896
bid_get_fact2(GEN bid) { return gmael(bid,3,2); }
2897
INLINE GEN
2898
bid_get_sprk(GEN bid) { return gmael(bid,4,1); }
2899
INLINE GEN
2900
bid_get_sarch(GEN bid) { return gmael(bid,4,2); }
2901
INLINE GEN
2902
bid_get_archp(GEN bid) { return gmael3(bid,4,2,2); }
2903
INLINE GEN
2904
bid_get_U(GEN bid) { return gel(bid,5); }
2905
INLINE GEN
2906
bid_get_no(GEN bid) { return abgrp_get_no(bid_get_grp(bid)); }
2907
INLINE GEN
2908
bid_get_cyc(GEN bid) { return abgrp_get_cyc(bid_get_grp(bid)); }
2909
INLINE GEN
2910
bid_get_gen_nocheck(GEN bid) { return abgrp_get_gen(bid_get_grp(bid)); }
2911
INLINE GEN
2912
bid_get_gen(GEN bid) {
2913
GEN G = bid_get_grp(bid);
2914
if (lg(G) != 4) pari_err(e_MISC,"missing bid generators. Use idealstar(,,2)");
2915
return abgrp_get_gen(G);
2916
}
2917
2918
INLINE GEN
2919
znstar_get_N(GEN G) { return gmael(G,1,1); }
2920
INLINE GEN
2921
znstar_get_faN(GEN G) { return gel(G,3); }
2922
INLINE GEN
2923
znstar_get_no(GEN G) { return abgrp_get_no(gel(G,2)); }
2924
INLINE GEN
2925
znstar_get_cyc(GEN G) { return abgrp_get_cyc(gel(G,2)); }
2926
INLINE GEN
2927
znstar_get_gen(GEN G) { return abgrp_get_gen(gel(G,2)); }
2928
INLINE GEN
2929
znstar_get_conreycyc(GEN G) { return gmael(G,4,5); }
2930
INLINE GEN
2931
znstar_get_conreygen(GEN G) { return gmael(G,4,4); }
2932
INLINE GEN
2933
znstar_get_Ui(GEN G) { return gmael(G,4,3); }
2934
INLINE GEN
2935
znstar_get_U(GEN G) { return gel(G,5); }
2936
INLINE GEN
2937
znstar_get_pe(GEN G) { return gmael(G,4,1); }
2938
INLINE GEN
2939
gal_get_pol(GEN gal) { return gel(gal,1); }
2940
INLINE GEN
2941
gal_get_p(GEN gal) { return gmael(gal,2,1); }
2942
INLINE GEN
2943
gal_get_e(GEN gal) { return gmael(gal,2,2); }
2944
INLINE GEN
2945
gal_get_mod(GEN gal) { return gmael(gal,2,3); }
2946
INLINE GEN
2947
gal_get_roots(GEN gal) { return gel(gal,3); }
2948
INLINE GEN
2949
gal_get_invvdm(GEN gal) { return gel(gal,4); }
2950
INLINE GEN
2951
gal_get_den(GEN gal) { return gel(gal,5); }
2952
INLINE GEN
2953
gal_get_group(GEN gal) { return gel(gal,6); }
2954
INLINE GEN
2955
gal_get_gen(GEN gal) { return gel(gal,7); }
2956
INLINE GEN
2957
gal_get_orders(GEN gal) { return gel(gal,8); }
2958
2959
/* assume rnf a genuine rnf */
2960
INLINE long
2961
rnf_get_degree(GEN rnf) { return degpol(rnf_get_pol(rnf)); }
2962
INLINE long
2963
rnf_get_nfdegree(GEN rnf) { return degpol(nf_get_pol(rnf_get_nf(rnf))); }
2964
INLINE long
2965
rnf_get_absdegree(GEN rnf) { return degpol(gmael(rnf,11,1)); }
2966
INLINE GEN
2967
rnf_get_idealdisc(GEN rnf) { return gmael(rnf,3,1); }
2968
INLINE GEN
2969
rnf_get_k(GEN rnf) { return gmael(rnf,11,3); }
2970
INLINE GEN
2971
rnf_get_alpha(GEN rnf) { return gmael(rnf, 11, 2); }
2972
INLINE GEN
2973
rnf_get_nf(GEN rnf) { return gel(rnf,10); }
2974
INLINE GEN
2975
rnf_get_nfzk(GEN rnf) { return gel(rnf,2); }
2976
INLINE GEN
2977
rnf_get_polabs(GEN rnf) { return gmael(rnf,11,1); }
2978
INLINE GEN
2979
rnf_get_pol(GEN rnf) { return gel(rnf,1); }
2980
INLINE GEN
2981
rnf_get_disc(GEN rnf) { return gel(rnf,3); }
2982
INLINE GEN
2983
rnf_get_index(GEN rnf) { return gel(rnf,4); }
2984
INLINE GEN
2985
rnf_get_ramified_primes(GEN rnf) { return gel(rnf,5); }
2986
INLINE long
2987
rnf_get_varn(GEN rnf) { return varn(gel(rnf,1)); }
2988
INLINE GEN
2989
rnf_get_nfpol(GEN rnf) { return gmael(rnf,10,1); }
2990
INLINE long
2991
rnf_get_nfvarn(GEN rnf) { return varn(gmael(rnf,10,1)); }
2992
INLINE GEN
2993
rnf_get_zk(GEN rnf) { return gel(rnf,7); }
2994
INLINE GEN
2995
rnf_get_map(GEN rnf) { return gel(rnf,11); }
2996
INLINE GEN
2997
rnf_get_invzk(GEN rnf) { return gel(rnf,8); }
2998
2999
/* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
3000
* T2 matrix. Reduce I wrt G */
3001
INLINE GEN
3002
idealpseudored(GEN I, GEN G)
3003
{ return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
3004
3005
/* Same I, G; m in I with T2(m) small */
3006
INLINE GEN
3007
idealpseudomin(GEN I, GEN G)
3008
{
3009
GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
3010
return ZM_ZC_mul(I, gel(u,1));
3011
}
3012
/* Same I,G; irrational m in I with T2(m) small */
3013
INLINE GEN
3014
idealpseudomin_nonscalar(GEN I, GEN G)
3015
{
3016
GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
3017
GEN m = ZM_ZC_mul(I, gel(u,1));
3018
if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
3019
return m;
3020
}
3021
/* Same I,G; t_VEC of irrational m in I with T2(m) small */
3022
INLINE GEN
3023
idealpseudominvec(GEN I, GEN G)
3024
{
3025
long i, j, k, n = lg(I)-1;
3026
GEN x, L, b = idealpseudored(I, G);
3027
L = cgetg(1 + (n*(n+1))/2, t_VEC);
3028
for (i = k = 1; i <= n; i++)
3029
{
3030
x = gel(b,i);
3031
if (!ZV_isscalar(x)) gel(L,k++) = x;
3032
}
3033
for (i = 2; i <= n; i++)
3034
for (j = 1; j < i; j++)
3035
{
3036
x = ZC_add(gel(b,i),gel(b,j));
3037
if (!ZV_isscalar(x)) gel(L,k++) = x;
3038
}
3039
setlg(L,k); return L;
3040
}
3041
3042
INLINE GEN
3043
idealred_elt(GEN nf, GEN I) {
3044
pari_sp av = avma;
3045
GEN u = idealpseudomin(I, nf_get_roundG(nf));
3046
return gerepileupto(av, u);
3047
}
3048
INLINE GEN
3049
idealred(GEN nf, GEN I) { return idealred0(nf, I, NULL); }
3050
3051
INLINE GEN
3052
idealchineseinit(GEN nf, GEN x)
3053
{ return idealchinese(nf,x,NULL); }
3054
3055
/*******************************************************************/
3056
/* */
3057
/* CLOSURES */
3058
/* */
3059
/*******************************************************************/
3060
INLINE long closure_arity(GEN C) { return ((ulong)C[1])&ARITYBITS; }
3061
INLINE long closure_is_variadic(GEN C) { return !!(((ulong)C[1])&VARARGBITS); }
3062
INLINE const char *closure_codestr(GEN C) { return GSTR(gel(C,2))-1; }
3063
INLINE GEN closure_get_code(GEN C) { return gel(C,2); }
3064
INLINE GEN closure_get_oper(GEN C) { return gel(C,3); }
3065
INLINE GEN closure_get_data(GEN C) { return gel(C,4); }
3066
INLINE GEN closure_get_dbg(GEN C) { return gel(C,5); }
3067
INLINE GEN closure_get_text(GEN C) { return gel(C,6); }
3068
INLINE GEN closure_get_frame(GEN C) { return gel(C,7); }
3069
3070
/*******************************************************************/
3071
/* */
3072
/* ERRORS */
3073
/* */
3074
/*******************************************************************/
3075
INLINE long
3076
err_get_num(GEN e) { return e[1]; }
3077
INLINE GEN
3078
err_get_compo(GEN e, long i) { return gel(e, i+1); }
3079
3080
INLINE void
3081
pari_err_BUG(const char *f) { pari_err(e_BUG,f); }
3082
INLINE void
3083
pari_err_CONSTPOL(const char *f) { pari_err(e_CONSTPOL, f); }
3084
INLINE void
3085
pari_err_COPRIME(const char *f, GEN x, GEN y) { pari_err(e_COPRIME, f,x,y); }
3086
INLINE void
3087
pari_err_DIM(const char *f) { pari_err(e_DIM, f); }
3088
INLINE void
3089
pari_err_FILE(const char *f, const char *g) { pari_err(e_FILE, f,g); }
3090
INLINE void
3091
pari_err_FILEDESC(const char *f, long n) { pari_err(e_FILEDESC, f,n); }
3092
INLINE void
3093
pari_err_FLAG(const char *f) { pari_err(e_FLAG,f); }
3094
INLINE void
3095
pari_err_IMPL(const char *f) { pari_err(e_IMPL,f); }
3096
INLINE void
3097
pari_err_INV(const char *f, GEN x) { pari_err(e_INV,f,x); }
3098
INLINE void
3099
pari_err_IRREDPOL(const char *f, GEN x) { pari_err(e_IRREDPOL, f,x); }
3100
INLINE void
3101
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); }
3102
INLINE void
3103
pari_err_COMPONENT(const char *f, const char *op, GEN l, GEN x) { pari_err(e_COMPONENT, f,op,l,x); }
3104
INLINE void
3105
pari_err_MAXPRIME(ulong c) { pari_err(e_MAXPRIME, c); }
3106
INLINE void
3107
pari_err_OP(const char *f, GEN x, GEN y) { pari_err(e_OP, f,x,y); }
3108
INLINE void
3109
pari_err_OVERFLOW(const char *f) { pari_err(e_OVERFLOW, f); }
3110
INLINE void
3111
pari_err_PREC(const char *f) { pari_err(e_PREC,f); }
3112
INLINE void
3113
pari_err_PACKAGE(const char *f) { pari_err(e_PACKAGE,f); }
3114
INLINE void
3115
pari_err_PRIME(const char *f, GEN x) { pari_err(e_PRIME, f,x); }
3116
INLINE void
3117
pari_err_MODULUS(const char *f, GEN x, GEN y) { pari_err(e_MODULUS, f,x,y); }
3118
INLINE void
3119
pari_err_ROOTS0(const char *f) { pari_err(e_ROOTS0, f); }
3120
INLINE void
3121
pari_err_SQRTN(const char *f, GEN x) { pari_err(e_SQRTN, f,x); }
3122
INLINE void
3123
pari_err_TYPE(const char *f, GEN x) { pari_err(e_TYPE, f,x); }
3124
INLINE void
3125
pari_err_TYPE2(const char *f, GEN x, GEN y) { pari_err(e_TYPE2, f,x,y); }
3126
INLINE void
3127
pari_err_VAR(const char *f, GEN x, GEN y) { pari_err(e_VAR, f,x,y); }
3128
INLINE void
3129
pari_err_PRIORITY(const char *f, GEN x, const char *op, long v)
3130
{ pari_err(e_PRIORITY, f,x,op,v); }
3131
3132