Line data Source code
1 : /* Copyright (C) 2000 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 : #include "pari.h"
16 : #include "paripriv.h"
17 :
18 : #define DEBUGLEVEL DEBUGLEVEL_pol
19 :
20 : /*******************************************************************/
21 : /* */
22 : /* GENERIC */
23 : /* */
24 : /*******************************************************************/
25 :
26 : /* Return optimal parameter l for the evaluation of n/m polynomials of degree d
27 : Fractional values can be used if the evaluations are done with different
28 : accuracies, and thus have different weights.
29 : */
30 : long
31 19438766 : brent_kung_optpow(long d, long n, long m)
32 : {
33 : long p, r;
34 19438766 : long pold=1, rold=n*(d-1);
35 91145664 : for(p=2; p<=d; p++)
36 : {
37 71706898 : r = m*(p-1) + n*((d-1)/p);
38 71706898 : if (r<rold) { pold=p; rold=r; }
39 : }
40 19438766 : return pold;
41 : }
42 :
43 : static GEN
44 14561848 : gen_RgXQ_eval_powers(GEN P, GEN V, long a, long n, void *E, const struct bb_algebra *ff,
45 : GEN cmul(void *E, GEN P, long a, GEN x))
46 : {
47 14561848 : pari_sp av = avma;
48 : long i;
49 14561848 : GEN z = cmul(E,P,a,ff->one(E));
50 14537762 : if (!z) z = gen_0;
51 76392631 : for (i=1; i<=n; i++)
52 : {
53 61853920 : GEN t = cmul(E,P,a+i,gel(V,i+1));
54 61871558 : if (t) {
55 46937404 : z = ff->add(E, z, t);
56 46913320 : if (gc_needed(av,2)) z = gc_upto(av, z);
57 : }
58 : }
59 14538711 : return ff->red(E,z);
60 : }
61 :
62 : /* Brent & Kung
63 : * (Fast algorithms for manipulating formal power series, JACM 25:581-595, 1978)
64 : *
65 : * V as output by FpXQ_powers(x,l,T,p). For optimal performance, l is as given
66 : * by brent_kung_optpow */
67 : GEN
68 12467922 : gen_bkeval_powers(GEN P, long d, GEN V, void *E, const struct bb_algebra *ff,
69 : GEN cmul(void *E, GEN P, long a, GEN x))
70 : {
71 12467922 : pari_sp av = avma;
72 12467922 : long l = lg(V)-1;
73 : GEN z, u;
74 :
75 12467922 : if (d < 0) return ff->zero(E);
76 11860140 : if (d < l) return gc_upto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
77 1157631 : if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
78 1157631 : if (DEBUGLEVEL>=8)
79 : {
80 0 : long cnt = 1 + (d - l) / (l-1);
81 0 : err_printf("RgX_RgXQV_eval(%ld/%ld): %ld RgXQ_mul\n", d, l-1, cnt);
82 : }
83 1157631 : d -= l;
84 1157631 : z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
85 2700669 : while (d >= l-1)
86 : {
87 1541832 : d -= l-1;
88 1541832 : u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
89 1541782 : z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
90 1541862 : if (gc_needed(av,2))
91 91 : z = gc_upto(av, z);
92 : }
93 1158837 : u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
94 1158848 : z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
95 1158857 : return gc_upto(av, ff->red(E,z));
96 : }
97 :
98 : GEN
99 874067 : gen_bkeval(GEN Q, long d, GEN x, int use_sqr, void *E, const struct bb_algebra *ff,
100 : GEN cmul(void *E, GEN P, long a, GEN x))
101 : {
102 874067 : pari_sp av = avma;
103 : GEN z, V;
104 : long rtd;
105 874067 : if (d < 0) return ff->zero(E);
106 872772 : rtd = (long) sqrt((double)d);
107 872772 : V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
108 872776 : z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
109 872780 : return gc_upto(av, z);
110 : }
111 :
112 : static GEN
113 2027516 : _gen_nored(void *E, GEN x) { (void)E; return x; }
114 : static GEN
115 19233958 : _gen_add(void *E, GEN x, GEN y) { (void)E; return gadd(x, y); }
116 : static GEN
117 0 : _gen_sub(void *E, GEN x, GEN y) { (void)E; return gsub(x, y); }
118 : static GEN
119 1839631 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
120 : static GEN
121 591652 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
122 : static GEN
123 2068675 : _gen_one(void *E) { (void)E; return gen_1; }
124 : static GEN
125 24184 : _gen_zero(void *E) { (void)E; return gen_0; }
126 :
127 : static struct bb_algebra Rg_algebra = { _gen_nored, _gen_add, _gen_sub,
128 : _gen_mul, _gen_sqr,_gen_one,_gen_zero };
129 :
130 : static GEN
131 512818 : _gen_cmul(void *E, GEN P, long a, GEN x)
132 512818 : {(void)E; return gmul(gel(P,a+2), x);}
133 :
134 : GEN
135 166768 : RgX_RgV_eval(GEN Q, GEN x)
136 : {
137 166768 : return gen_bkeval_powers(Q, degpol(Q), x, NULL, &Rg_algebra, _gen_cmul);
138 : }
139 :
140 : GEN
141 0 : RgX_Rg_eval_bk(GEN Q, GEN x)
142 : {
143 0 : return gen_bkeval(Q, degpol(Q), x, 1, NULL, &Rg_algebra, _gen_cmul);
144 : }
145 :
146 : GEN
147 2947 : RgXV_RgV_eval(GEN Q, GEN x)
148 : {
149 2947 : long i, l = lg(Q), vQ = gvar(Q);
150 2947 : GEN v = cgetg(l, t_VEC);
151 248311 : for (i = 1; i < l; i++)
152 : {
153 245364 : GEN Qi = gel(Q, i);
154 245364 : gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
155 : }
156 2947 : return v;
157 : }
158 :
159 : GEN
160 668785 : RgX_homogenous_evalpow(GEN P, GEN A, GEN B)
161 : {
162 668785 : pari_sp av = avma, btop;
163 668785 : long i, d = degpol(P), o;
164 : GEN s;
165 668786 : if (signe(P)==0) return pol_0(varn(P));
166 668786 : s = gel(P, d+2);
167 668786 : if (d == 0) return gcopy(s);
168 665510 : o = RgX_deflate_order(P);
169 665524 : if (o > 1) A = gpowgs(A, o);
170 665528 : btop = avma;
171 2313816 : for (i = d-o; i >= 0; i-=o)
172 : {
173 1648290 : s = gadd(gmul(s, A), gmul(gel(B,d+1-i), gel(P,i+2)));
174 1648288 : if (gc_needed(btop,1))
175 : {
176 13 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_homogenous_eval(%ld)",i);
177 13 : s = gc_upto(btop, s);
178 : }
179 : }
180 665526 : return gc_upto(av, s);
181 : }
182 :
183 : GEN
184 1652 : QXQX_homogenous_evalpow(GEN P, GEN A, GEN B, GEN T)
185 : {
186 1652 : pari_sp av = avma;
187 1652 : long i, d = degpol(P), v = varn(A);
188 : GEN s;
189 1652 : if (signe(P)==0) return pol_0(v);
190 1652 : if (d == 0) return scalarpol(gel(P, d+2), v);
191 1232 : s = scalarpol_shallow(gel(P, d+2), v);
192 4963 : for (i = d-1; i >= 0; i--)
193 : {
194 3731 : GEN c = gel(P,i+2), b = gel(B,d+1-i);
195 3731 : s = RgX_add(QXQX_mul(s, A, T), typ(c)==t_POL ? QXQX_QXQ_mul(b, c, T): gmul(b, c));
196 3731 : if (gc_needed(av,1))
197 : {
198 0 : if(DEBUGMEM>1) pari_warn(warnmem,"QXQX_homogenous_eval(%ld)",i);
199 0 : s = gc_upto(av, s);
200 : }
201 : }
202 1232 : return gc_upto(av, s);
203 : }
204 :
205 : const struct bb_algebra *
206 281053 : get_Rg_algebra(void)
207 : {
208 281053 : return &Rg_algebra;
209 : }
210 :
211 : static struct bb_ring Rg_ring = { _gen_add, _gen_mul, _gen_sqr };
212 :
213 : static GEN
214 11333 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
215 : {
216 : (void) E;
217 11333 : return RgX_divrem(x, y, r);
218 : }
219 :
220 : GEN
221 3157 : RgX_digits(GEN x, GEN T)
222 : {
223 3157 : long d = degpol(T), n = (lgpol(x)+d-1)/d;
224 3157 : if (signe(x)==0) return(cgetg(1, t_VEC));
225 3157 : return gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
226 : }
227 :
228 : /*******************************************************************/
229 : /* */
230 : /* RgX */
231 : /* */
232 : /*******************************************************************/
233 :
234 : long
235 24895494 : RgX_equal(GEN x, GEN y)
236 : {
237 24895494 : long i = lg(x);
238 :
239 24895494 : if (i != lg(y)) return 0;
240 107145903 : for (i--; i > 1; i--)
241 82578367 : if (!gequal(gel(x,i),gel(y,i))) return 0;
242 24567536 : return 1;
243 : }
244 :
245 : /* Returns 1 in the base ring over which x is defined */
246 : /* HACK: this also works for t_SER */
247 : GEN
248 156947104 : Rg_get_1(GEN x)
249 : {
250 : GEN p, T;
251 156947104 : long i, lx, tx = Rg_type(x, &p, &T, &lx);
252 156947104 : if (RgX_type_is_composite(tx))
253 11657616 : RgX_type_decode(tx, &i /*junk*/, &tx);
254 156947104 : switch(tx)
255 : {
256 427406 : case t_INTMOD: retmkintmod(is_pm1(p)? gen_0: gen_1, icopy(p));
257 945 : case t_PADIC: return cvtop(gen_1, p, lx);
258 5950 : case t_FFELT: return FF_1(T);
259 156512803 : default: return gen_1;
260 : }
261 : }
262 : /* Returns 0 in the base ring over which x is defined */
263 : /* HACK: this also works for t_SER */
264 : GEN
265 6942919 : Rg_get_0(GEN x)
266 : {
267 : GEN p, T;
268 6942919 : long i, lx, tx = Rg_type(x, &p, &T, &lx);
269 6942919 : if (RgX_type_is_composite(tx))
270 61341 : RgX_type_decode(tx, &i /*junk*/, &tx);
271 6942919 : switch(tx)
272 : {
273 497 : case t_INTMOD: retmkintmod(gen_0, icopy(p));
274 42 : case t_PADIC: return zeropadic(p, lx);
275 210 : case t_FFELT: return FF_zero(T);
276 6942170 : default: return gen_0;
277 : }
278 : }
279 :
280 : GEN
281 6902 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
282 : {
283 6902 : long i, n = degpol(P);
284 : GEN z, dz, dP;
285 6902 : if (n < 0) return gen_0;
286 6902 : P = Q_remove_denom(P, &dP);
287 6902 : z = gel(P,2); if (n == 0) return icopy(z);
288 3892 : if (dV) z = mulii(dV, z); /* V[1] = dV */
289 3892 : z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
290 7497 : for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
291 3892 : dz = mul_denom(dP, dV);
292 3892 : return dz? RgX_Rg_div(z, dz): z;
293 : }
294 :
295 : /* Return P(h * x), not memory clean */
296 : GEN
297 51409 : RgX_unscale(GEN P, GEN h)
298 : {
299 51409 : long i, l = lg(P);
300 51409 : GEN hi = gen_1, Q = cgetg(l, t_POL);
301 51409 : Q[1] = P[1];
302 51409 : if (l == 2) return Q;
303 40167 : gel(Q,2) = gcopy(gel(P,2));
304 94662 : for (i=3; i<l; i++)
305 : {
306 54495 : hi = gmul(hi,h);
307 54495 : gel(Q,i) = gmul(gel(P,i), hi);
308 : }
309 40167 : return Q;
310 : }
311 : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
312 : GEN
313 1467285 : ZX_z_unscale(GEN P, long h)
314 : {
315 1467285 : long i, l = lg(P);
316 1467285 : GEN Q = cgetg(l, t_POL);
317 1467285 : Q[1] = P[1];
318 1467285 : if (l == 2) return Q;
319 1378739 : gel(Q,2) = gel(P,2);
320 1378739 : if (l == 3) return Q;
321 1352433 : if (h == -1)
322 237012 : for (i = 3; i < l; i++)
323 : {
324 196415 : gel(Q,i) = negi(gel(P,i));
325 196416 : if (++i == l) break;
326 145411 : gel(Q,i) = gel(P,i);
327 : }
328 : else
329 : {
330 : GEN hi;
331 1260832 : gel(Q,3) = mulis(gel(P,3), h);
332 1260832 : hi = sqrs(h);
333 5415795 : for (i = 4; i < l; i++)
334 : {
335 4154963 : gel(Q,i) = mulii(gel(P,i), hi);
336 4154963 : if (i != l-1) hi = mulis(hi,h);
337 : }
338 : }
339 1352434 : return Q;
340 : }
341 : /* P a ZX, h a t_INT. Return P(h * x), not memory clean; optimize for h = -1 */
342 : GEN
343 1013197 : ZX_unscale(GEN P, GEN h)
344 : {
345 : long i, l;
346 : GEN Q, hi;
347 1013197 : i = itos_or_0(h); if (i) return ZX_z_unscale(P, i);
348 881 : l = lg(P); Q = cgetg(l, t_POL);
349 881 : Q[1] = P[1];
350 881 : if (l == 2) return Q;
351 881 : gel(Q,2) = gel(P,2);
352 881 : if (l == 3) return Q;
353 881 : hi = h;
354 881 : gel(Q,3) = mulii(gel(P,3), hi);
355 2741 : for (i = 4; i < l; i++)
356 : {
357 1860 : hi = mulii(hi,h);
358 1860 : gel(Q,i) = mulii(gel(P,i), hi);
359 : }
360 881 : return Q;
361 : }
362 : /* P a ZX. Return P(x << n), not memory clean */
363 : GEN
364 924518 : ZX_unscale2n(GEN P, long n)
365 : {
366 924518 : long i, ni = n, l = lg(P);
367 924518 : GEN Q = cgetg(l, t_POL);
368 924518 : Q[1] = P[1];
369 924518 : if (l == 2) return Q;
370 924518 : gel(Q,2) = gel(P,2);
371 924518 : if (l == 3) return Q;
372 924518 : gel(Q,3) = shifti(gel(P,3), ni);
373 3167010 : for (i=4; i<l; i++)
374 : {
375 2242552 : ni += n;
376 2242552 : gel(Q,i) = shifti(gel(P,i), ni);
377 : }
378 924458 : return Q;
379 : }
380 : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
381 : GEN
382 12997 : ZX_unscale_div(GEN P, GEN h)
383 : {
384 12997 : long i, l = lg(P);
385 12997 : GEN hi, Q = cgetg(l, t_POL);
386 12997 : Q[1] = P[1];
387 12997 : if (l == 2) return Q;
388 12997 : gel(Q,2) = diviiexact(gel(P,2), h);
389 12997 : if (l == 3) return Q;
390 12997 : gel(Q,3) = gel(P,3);
391 12997 : if (l == 4) return Q;
392 12997 : hi = h;
393 12997 : gel(Q,4) = mulii(gel(P,4), hi);
394 64121 : for (i=5; i<l; i++)
395 : {
396 51124 : hi = mulii(hi,h);
397 51124 : gel(Q,i) = mulii(gel(P,i), hi);
398 : }
399 12997 : return Q;
400 : }
401 : /* P(h*X) / h^k, assuming the result is a ZX */
402 : GEN
403 1393 : ZX_unscale_divpow(GEN P, GEN h, long k)
404 : {
405 1393 : long i, j, l = lg(P);
406 1393 : GEN H, Q = cgetg(l, t_POL);
407 1393 : Q[1] = P[1]; if (l == 2) return Q;
408 1393 : H = gpowers(h, maxss(k, l - 3 - k));
409 5572 : for (i = 2, j = k+1; j > 1 && i < l; i++)
410 4179 : gel(Q, i) = diviiexact(gel(P, i), gel(H, j--));
411 1393 : if (i == l) return Q;
412 1393 : gel(Q, i) = gel(P, i); i++;
413 5082 : for (j = 2; i < l; i++) gel(Q, i) = mulii(gel(P, i), gel(H, j++));
414 1393 : return Q;
415 : }
416 :
417 : GEN
418 6223 : RgXV_unscale(GEN x, GEN h)
419 : {
420 6223 : if (isint1(h)) return gcopy(x);
421 18176 : pari_APPLY_same(RgX_unscale(gel(x,i), h));
422 : }
423 :
424 : /* Return h^degpol(P) P(x / h), not memory clean */
425 : GEN
426 4342279 : RgX_rescale(GEN P, GEN h)
427 : {
428 4342279 : long i, l = lg(P);
429 4342279 : GEN Q = cgetg(l,t_POL), hi = h;
430 4342269 : gel(Q,l-1) = gel(P,l-1);
431 11427414 : for (i=l-2; i>=2; i--)
432 : {
433 11424778 : gel(Q,i) = gmul(gel(P,i), hi);
434 11424717 : if (i == 2) break;
435 7084864 : hi = gmul(hi,h);
436 : }
437 4342489 : Q[1] = P[1]; return Q;
438 : }
439 :
440 : GEN
441 2401 : RgXV_rescale(GEN x, GEN h)
442 : {
443 2401 : if (isint1(h)) return RgX_copy(x);
444 16086 : pari_APPLY_same(RgX_rescale(gel(x,i), h));
445 : }
446 :
447 : /* A(X^d) --> A(X) */
448 : GEN
449 1175254 : RgX_deflate(GEN x0, long d)
450 : {
451 : GEN z, y, x;
452 1175254 : long i,id, dy, dx = degpol(x0);
453 1175253 : if (d == 1 || dx <= 0) return leafcopy(x0);
454 459138 : dy = dx/d;
455 459138 : y = cgetg(dy+3, t_POL); y[1] = x0[1];
456 459135 : z = y + 2;
457 459135 : x = x0+ 2;
458 1762820 : for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
459 459135 : return y;
460 : }
461 :
462 : GEN
463 1260 : RgX_homogenize_deg(GEN P, long d, long v)
464 : {
465 : long i, l;
466 1260 : GEN Q = cgetg_copy(P, &l);
467 1260 : Q[1] = P[1];
468 4018 : for (i = 2; i < l; i++) gel(Q,i) = monomial(gel(P,i), d--, v);
469 1260 : return Q;
470 : }
471 :
472 : GEN
473 18732 : RgX_homogenize(GEN P, long v)
474 : {
475 : long i, l, d;
476 18732 : GEN Q = cgetg_copy(P, &l);
477 18732 : Q[1] = P[1]; d = l-3;
478 165998 : for (i = 2; i < l; i++) gel(Q,i) = monomial(gel(P,i), d--, v);
479 18732 : return Q;
480 : }
481 :
482 : /* F a t_RFRAC */
483 : long
484 126 : rfrac_deflate_order(GEN F)
485 : {
486 126 : GEN N = gel(F,1), D = gel(F,2);
487 126 : long m = (degpol(D) <= 0)? 0: RgX_deflate_order(D);
488 126 : if (m == 1) return 1;
489 42 : if (typ(N) == t_POL && varn(N) == varn(D))
490 28 : m = cgcd(m, RgX_deflate_order(N));
491 42 : return m;
492 : }
493 : /* F a t_RFRAC */
494 : GEN
495 126 : rfrac_deflate_max(GEN F, long *m)
496 : {
497 126 : *m = rfrac_deflate_order(F);
498 126 : return rfrac_deflate(F, *m);
499 : }
500 : /* F a t_RFRAC */
501 : GEN
502 126 : rfrac_deflate(GEN F, long m)
503 : {
504 126 : GEN N = gel(F,1), D = gel(F,2);
505 126 : if (m == 1) return F;
506 42 : if (typ(N) == t_POL && varn(N) == varn(D)) N = RgX_deflate(N, m);
507 42 : D = RgX_deflate(D, m); return mkrfrac(N, D);
508 : }
509 :
510 : /* return x0(X^d) */
511 : GEN
512 881802 : RgX_inflate(GEN x0, long d)
513 : {
514 881802 : long i, id, dy, dx = degpol(x0);
515 881802 : GEN x = x0 + 2, z, y;
516 881802 : if (dx <= 0) return leafcopy(x0);
517 804512 : dy = dx*d;
518 804512 : y = cgetg(dy+3, t_POL); y[1] = x0[1];
519 804512 : z = y + 2;
520 28082848 : for (i=0; i<=dy; i++) gel(z,i) = gen_0;
521 11490106 : for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
522 804512 : return y;
523 : }
524 :
525 : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
526 : static GEN
527 5701640 : RgX_translate_basecase(GEN P, GEN c)
528 : {
529 5701640 : pari_sp av = avma;
530 : GEN Q, R;
531 : long i, k, n;
532 :
533 5701640 : if (!signe(P) || gequal0(c)) return RgX_copy(P);
534 5699710 : Q = leafcopy(P);
535 5699706 : R = Q+2; n = degpol(P);
536 5699701 : if (isint1(c))
537 : {
538 7595 : for (i=1; i<=n; i++)
539 : {
540 20265 : for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gel(R,k+1));
541 5390 : if (gc_needed(av,2))
542 : {
543 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate(1), i = %ld/%ld", i,n);
544 0 : Q = gc_GEN(av, Q); R = Q+2;
545 : }
546 : }
547 : }
548 5697493 : else if (isintm1(c))
549 : {
550 15973 : for (i=1; i<=n; i++)
551 : {
552 49629 : for (k=n-i; k<n; k++) gel(R,k) = gsub(gel(R,k), gel(R,k+1));
553 12298 : if (gc_needed(av,2))
554 : {
555 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate(-1), i = %ld/%ld", i,n);
556 0 : Q = gc_GEN(av, Q); R = Q+2;
557 : }
558 : }
559 : }
560 : else
561 : {
562 19424613 : for (i=1; i<=n; i++)
563 : {
564 45450324 : for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gmul(c, gel(R,k+1)));
565 13730795 : if (gc_needed(av,2))
566 : {
567 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate, i = %ld/%ld", i,n);
568 0 : Q = gc_GEN(av, Q); R = Q+2;
569 : }
570 : }
571 : }
572 5699464 : return gc_GEN(av, Q);
573 : }
574 :
575 : static GEN
576 102627 : zero_FpX_mod(GEN p, long v)
577 : {
578 102627 : GEN r = cgetg(3,t_POL);
579 102627 : r[1] = evalvarn(v);
580 102627 : gel(r,2) = mkintmod(gen_0, icopy(p));
581 102627 : return r;
582 : }
583 :
584 : static GEN
585 0 : RgX_translate_FpX(GEN P, GEN c, GEN p)
586 : {
587 0 : pari_sp av = avma;
588 : GEN r;
589 : #if 0
590 : if (lgefint(p) == 3)
591 : {
592 : ulong pp = uel(p, 2);
593 : r = Flx_to_ZX_inplace(Flx_translate(RgX_to_Flx(x, pp), Rg_to_Fl(c, pp), pp));
594 : }
595 : else
596 : #endif
597 0 : r = FpX_translate(RgX_to_FpX(P, p), Rg_to_Fp(c, p), p);
598 0 : if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(P)); }
599 0 : return gc_upto(av, FpX_to_mod(r, p));
600 : }
601 :
602 : static GEN
603 6063337 : RgX_translate_fast(GEN P, GEN c)
604 : {
605 : GEN p, pol;
606 : long pa;
607 6063337 : long t = RgX_Rg_type(P, c, &p,&pol,&pa);
608 6063344 : switch(t)
609 : {
610 361888 : case t_INT: return ZX_translate(P, c);
611 0 : case t_INTMOD: return RgX_translate_FpX(P, c, p);
612 5701457 : default: return NULL;
613 : }
614 : }
615 :
616 : static GEN
617 5701833 : RgX_translate_i(GEN P, GEN c)
618 : {
619 5701833 : pari_sp av = avma;
620 : long n;
621 5701833 : n = degpol(P);
622 5701829 : if (n < 40)
623 5701640 : return RgX_translate_basecase(P, c);
624 : else
625 : {
626 189 : long d = n >> 1;
627 189 : GEN Q = RgX_translate_i(RgX_shift_shallow(P, -d), c);
628 189 : GEN R = RgX_translate_i(RgXn_red_shallow(P, d), c);
629 189 : GEN S = gpowgs(deg1pol_shallow(gen_1, c, varn(P)), d);
630 189 : return gc_upto(av, RgX_add(RgX_mul(Q, S), R));
631 : }
632 : }
633 :
634 : GEN
635 6063339 : RgX_translate(GEN P, GEN c)
636 : {
637 6063339 : GEN R = RgX_translate_fast(P, c);
638 6063341 : return R ? R: RgX_translate_i(P,c);
639 : }
640 : /* P(ax + b) */
641 : GEN
642 30212 : RgX_affine(GEN P, GEN a, GEN b)
643 : {
644 30212 : if (!gequal0(b)) P = RgX_translate(P, b);
645 30212 : return RgX_unscale(P, a);
646 : }
647 :
648 : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
649 : GEN
650 32464 : RgXQX_translate(GEN P, GEN c, GEN T)
651 : {
652 32464 : pari_sp av = avma;
653 : GEN Q, R;
654 : long i, k, n;
655 :
656 32464 : if (!signe(P) || gequal0(c)) return RgX_copy(P);
657 32121 : Q = leafcopy(P);
658 32121 : R = Q+2; n = degpol(P);
659 103762 : for (i=1; i<=n; i++)
660 : {
661 301670 : for (k=n-i; k<n; k++)
662 : {
663 230029 : pari_sp av2 = avma;
664 230029 : gel(R,k) = gc_upto(av2,
665 230029 : RgX_rem(gadd(gel(R,k), gmul(c, gel(R,k+1))), T));
666 : }
667 71641 : if (gc_needed(av,2))
668 : {
669 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
670 0 : Q = gc_GEN(av, Q); R = Q+2;
671 : }
672 : }
673 32121 : return gc_GEN(av, Q);
674 : }
675 :
676 : /********************************************************************/
677 : /** **/
678 : /** CONVERSIONS **/
679 : /** (not memory clean) **/
680 : /** **/
681 : /********************************************************************/
682 : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
683 : * but everything else is */
684 : static GEN
685 166761 : QXQ_to_mod(GEN x, GEN T)
686 : {
687 : long d;
688 166761 : switch(typ(x))
689 : {
690 62437 : case t_INT: return icopy(x);
691 2079 : case t_FRAC: return gcopy(x);
692 102245 : case t_POL:
693 102245 : d = degpol(x);
694 102245 : if (d < 0) return gen_0;
695 99984 : if (d == 0) return gcopy(gel(x,2));
696 97873 : return mkpolmod(RgX_copy(x), T);
697 0 : default: pari_err_TYPE("QXQ_to_mod",x);
698 : return NULL;/* LCOV_EXCL_LINE */
699 : }
700 : }
701 : /* pure shallow version */
702 : GEN
703 840415 : QXQ_to_mod_shallow(GEN x, GEN T)
704 : {
705 : long d;
706 840415 : switch(typ(x))
707 : {
708 544308 : case t_INT:
709 544308 : case t_FRAC: return x;
710 296107 : case t_POL:
711 296107 : d = degpol(x);
712 296107 : if (d < 0) return gen_0;
713 249604 : if (d == 0) return gel(x,2);
714 232794 : return mkpolmod(x, T);
715 0 : default: pari_err_TYPE("QXQ_to_mod",x);
716 : return NULL;/* LCOV_EXCL_LINE */
717 : }
718 : }
719 : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod to all coeffs.
720 : * Not memory clean because T not copied, but everything else is */
721 : static GEN
722 37226 : QXQX_to_mod(GEN z, GEN T)
723 : {
724 37226 : long i,l = lg(z);
725 37226 : GEN x = cgetg(l,t_POL);
726 185738 : for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
727 37226 : x[1] = z[1]; return normalizepol_lg(x,l);
728 : }
729 : /* pure shallow version */
730 : GEN
731 203599 : QXQX_to_mod_shallow(GEN z, GEN T)
732 : {
733 203599 : long i,l = lg(z);
734 203599 : GEN x = cgetg(l,t_POL);
735 959930 : for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_shallow(gel(z,i), T);
736 203599 : x[1] = z[1]; return normalizepol_lg(x,l);
737 : }
738 : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
739 : GEN
740 12537 : QXQXV_to_mod(GEN V, GEN T)
741 : {
742 12537 : long i, l = lg(V);
743 12537 : GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
744 49763 : for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
745 12537 : return z;
746 : }
747 : /* Apply QXQ_to_mod to all entries. Memory-clean ! */
748 : GEN
749 18354 : QXQV_to_mod(GEN V, GEN T)
750 : {
751 18354 : long i, l = lg(V);
752 18354 : GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
753 36603 : for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
754 18354 : return z;
755 : }
756 :
757 : /* Apply QXQ_to_mod to all entries. Memory-clean ! */
758 : GEN
759 14938 : QXQC_to_mod_shallow(GEN V, GEN T)
760 : {
761 14938 : long i, l = lg(V);
762 14938 : GEN z = cgetg(l, t_COL);
763 99022 : for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_shallow(gel(V,i), T);
764 14938 : return z;
765 : }
766 :
767 : GEN
768 6734 : QXQM_to_mod_shallow(GEN V, GEN T)
769 : {
770 6734 : long i, l = lg(V);
771 6734 : GEN z = cgetg(l, t_MAT);
772 21672 : for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
773 6734 : return z;
774 : }
775 :
776 : GEN
777 7886137 : RgX_renormalize_lg(GEN x, long lx)
778 : {
779 : long i;
780 10928312 : for (i = lx-1; i>1; i--)
781 10475148 : if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
782 7886137 : stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
783 7886137 : setlg(x, i+1); setsigne(x, i != 1); return x;
784 : }
785 :
786 : GEN
787 1492470 : RgV_to_RgX(GEN x, long v)
788 : {
789 1492470 : long i, k = lg(x);
790 : GEN p;
791 :
792 4152927 : while (--k && gequal0(gel(x,k)));
793 1492469 : if (!k) return pol_0(v);
794 1485205 : i = k+2; p = cgetg(i,t_POL);
795 1485205 : p[1] = evalsigne(1) | evalvarn(v);
796 10229323 : x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
797 1485205 : return p;
798 : }
799 : GEN
800 188283 : RgV_to_RgX_reverse(GEN x, long v)
801 : {
802 188283 : long j, k, l = lg(x);
803 : GEN p;
804 :
805 189851 : for (k = 1; k < l; k++)
806 189851 : if (!gequal0(gel(x,k))) break;
807 188283 : if (k == l) return pol_0(v);
808 188283 : k -= 1;
809 188283 : l -= k;
810 188283 : x += k;
811 188283 : p = cgetg(l+1,t_POL);
812 188283 : p[1] = evalsigne(1) | evalvarn(v);
813 985038 : for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
814 188283 : return p;
815 : }
816 :
817 : /* return the (N-dimensional) vector of coeffs of p */
818 : GEN
819 13409065 : RgX_to_RgC(GEN x, long N)
820 : {
821 : long i, l;
822 : GEN z;
823 13409065 : l = lg(x)-1; x++;
824 13409065 : if (l > N+1) l = N+1; /* truncate higher degree terms */
825 13409065 : z = cgetg(N+1,t_COL);
826 80855346 : for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
827 24470759 : for ( ; i<=N; i++) gel(z,i) = gen_0;
828 13409141 : return z;
829 : }
830 : GEN
831 1355131 : Rg_to_RgC(GEN x, long N)
832 : {
833 1355131 : return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
834 : }
835 :
836 : /* vector of polynomials (in v) whose coefs are given by the columns of x */
837 : GEN
838 301993 : RgM_to_RgXV(GEN x, long v)
839 1284679 : { pari_APPLY_type(t_VEC, RgV_to_RgX(gel(x,i), v)) }
840 : GEN
841 7202 : RgM_to_RgXV_reverse(GEN x, long v)
842 28808 : { pari_APPLY_type(t_VEC, RgV_to_RgX_reverse(gel(x,i), v)) }
843 :
844 : /* matrix whose entries are given by the coeffs of the polynomials in
845 : * vector v (considered as degree n-1 polynomials) */
846 : GEN
847 335584 : RgV_to_RgM(GEN x, long n)
848 1686178 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
849 :
850 : GEN
851 78555 : RgXV_to_RgM(GEN x, long n)
852 399491 : { pari_APPLY_type(t_MAT, RgX_to_RgC(gel(x,i), n)) }
853 :
854 : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
855 : GEN
856 23476 : RgM_to_RgXX(GEN x, long v,long w)
857 : {
858 23476 : long j, lx = lg(x);
859 23476 : GEN y = cgetg(lx+1, t_POL);
860 23476 : y[1] = evalsigne(1) | evalvarn(v);
861 23476 : y++;
862 130468 : for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
863 23476 : return normalizepol_lg(--y, lx+1);
864 : }
865 :
866 : /* matrix whose entries are given by the coeffs of the polynomial v in
867 : * two variables (considered as degree n-1 polynomials) */
868 : GEN
869 322 : RgXX_to_RgM(GEN v, long n)
870 : {
871 322 : long j, N = lg(v)-1;
872 322 : GEN y = cgetg(N, t_MAT);
873 1043 : for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
874 322 : return y;
875 : }
876 :
877 : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
878 : GEN
879 32773 : RgXY_swapspec(GEN x, long n, long w, long nx)
880 : {
881 32773 : long j, ly = n+3;
882 32773 : GEN y = cgetg(ly, t_POL);
883 32773 : y[1] = evalsigne(1);
884 401048 : for (j=2; j<ly; j++)
885 : {
886 : long k;
887 368275 : GEN a = cgetg(nx+2,t_POL);
888 368275 : a[1] = evalsigne(1) | evalvarn(w);
889 2028311 : for (k=0; k<nx; k++)
890 : {
891 1660036 : GEN xk = gel(x,k);
892 1660036 : if (typ(xk)==t_POL && varn(xk)==w)
893 1565661 : gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
894 : else
895 94375 : gel(a,k+2) = j==2 ? xk: gen_0;
896 : }
897 368275 : gel(y,j) = normalizepol_lg(a, nx+2);
898 : }
899 32773 : return normalizepol_lg(y,ly);
900 : }
901 :
902 : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
903 : GEN
904 2051 : RgXY_swap(GEN x, long n, long w)
905 : {
906 2051 : GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
907 2051 : setvarn(z, varn(x)); return z;
908 : }
909 :
910 : long
911 1387 : RgXY_degreex(GEN b)
912 : {
913 1387 : long deg = 0, i;
914 1387 : if (!signe(b)) return -1;
915 15592 : for (i = 2; i < lg(b); ++i)
916 : {
917 14205 : GEN bi = gel(b, i);
918 14205 : if (typ(bi) == t_POL)
919 13742 : deg = maxss(deg, degpol(bi));
920 : }
921 1387 : return deg;
922 : }
923 :
924 : GEN
925 38738 : RgXY_derivx(GEN x) { pari_APPLY_pol(RgX_deriv(gel(x,i))); }
926 :
927 : /* return (x % X^n). Shallow */
928 : GEN
929 7943804 : RgXn_red_shallow(GEN a, long n)
930 : {
931 7943804 : long i, L = n+2, l = lg(a);
932 : GEN b;
933 7943804 : if (L >= l) return a; /* deg(x) < n */
934 5751553 : b = cgetg(L, t_POL); b[1] = a[1];
935 37323246 : for (i=2; i<L; i++) gel(b,i) = gel(a,i);
936 5751554 : return normalizepol_lg(b,L);
937 : }
938 :
939 : GEN
940 483 : RgXnV_red_shallow(GEN x, long n)
941 2268 : { pari_APPLY_type(t_VEC, RgXn_red_shallow(gel(x,i), n)) }
942 :
943 : /* return (x * X^n). Shallow */
944 : GEN
945 175572124 : RgX_shift_shallow(GEN a, long n)
946 : {
947 175572124 : long i, l = lg(a);
948 : GEN b;
949 175572124 : if (l == 2 || !n) return a;
950 109159167 : l += n;
951 109159167 : if (n < 0)
952 : {
953 54203333 : if (l <= 2) return pol_0(varn(a));
954 52729236 : b = cgetg(l, t_POL); b[1] = a[1];
955 52730003 : a -= n;
956 163993412 : for (i=2; i<l; i++) gel(b,i) = gel(a,i);
957 : } else {
958 54955834 : b = cgetg(l, t_POL); b[1] = a[1];
959 54960786 : a -= n; n += 2;
960 119456017 : for (i=2; i<n; i++) gel(b,i) = gen_0;
961 211976145 : for ( ; i<l; i++) gel(b,i) = gel(a,i);
962 : }
963 107690789 : return b;
964 : }
965 : /* return (x * X^n). */
966 : GEN
967 1578029 : RgX_shift(GEN a, long n)
968 : {
969 1578029 : long i, l = lg(a);
970 : GEN b;
971 1578029 : if (l == 2 || !n) return RgX_copy(a);
972 1577077 : l += n;
973 1577077 : if (n < 0)
974 : {
975 1442 : if (l <= 2) return pol_0(varn(a));
976 1372 : b = cgetg(l, t_POL); b[1] = a[1];
977 1372 : a -= n;
978 9534 : for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
979 : } else {
980 1575635 : b = cgetg(l, t_POL); b[1] = a[1];
981 1575635 : a -= n; n += 2;
982 3979076 : for (i=2; i<n; i++) gel(b,i) = gen_0;
983 4346090 : for ( ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
984 : }
985 1577007 : return b;
986 : }
987 :
988 : GEN
989 317037 : RgX_rotate_shallow(GEN P, long k, long p)
990 : {
991 317037 : long i, l = lgpol(P);
992 : GEN r;
993 317037 : if (signe(P)==0)
994 1365 : return pol_0(varn(P));
995 315672 : r = cgetg(p+2,t_POL); r[1] = P[1];
996 2100644 : for(i=0; i<p; i++)
997 : {
998 1784972 : long s = 2+(i+k)%p;
999 1784972 : gel(r,s) = i<l? gel(P,2+i): gen_0;
1000 : }
1001 315672 : return RgX_renormalize(r);
1002 : }
1003 :
1004 : GEN
1005 2997279 : RgX_mulXn(GEN x, long d)
1006 : {
1007 : pari_sp av;
1008 : GEN z;
1009 : long v;
1010 2997279 : if (d >= 0) return RgX_shift(x, d);
1011 1472517 : d = -d;
1012 1472517 : v = RgX_val(x);
1013 1472517 : if (v >= d) return RgX_shift(x, -d);
1014 1472503 : av = avma;
1015 1472503 : z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
1016 1472503 : return gc_upto(av, z);
1017 : }
1018 :
1019 : long
1020 588 : RgXV_maxdegree(GEN x)
1021 : {
1022 588 : long d = -1, i, l = lg(x);
1023 4494 : for (i = 1; i < l; i++)
1024 3906 : d = maxss(d, degpol(gel(x,i)));
1025 588 : return d;
1026 : }
1027 :
1028 : long
1029 3667765 : RgX_val(GEN x)
1030 : {
1031 3667765 : long i, lx = lg(x);
1032 3667765 : if (lx == 2) return LONG_MAX;
1033 4619541 : for (i = 2; i < lx; i++)
1034 4619485 : if (!isexactzero(gel(x,i))) break;
1035 3656838 : if (i == lx) return LONG_MAX;/* possible with nonrational zeros */
1036 3656782 : return i - 2;
1037 : }
1038 : long
1039 80770617 : RgX_valrem(GEN x, GEN *Z)
1040 : {
1041 80770617 : long v, i, lx = lg(x);
1042 80770617 : if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
1043 125180301 : for (i = 2; i < lx; i++)
1044 125182044 : if (!isexactzero(gel(x,i))) break;
1045 : /* possible with nonrational zeros */
1046 80770638 : if (i == lx)
1047 : {
1048 14 : *Z = scalarpol_shallow(Rg_get_0(x), varn(x));
1049 14 : return LONG_MAX;
1050 : }
1051 80770624 : v = i - 2;
1052 80770624 : *Z = RgX_shift_shallow(x, -v);
1053 80783893 : return v;
1054 : }
1055 : long
1056 852014 : RgX_valrem_inexact(GEN x, GEN *Z)
1057 : {
1058 : long v;
1059 852014 : if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
1060 869001 : for (v = 0;; v++)
1061 869001 : if (!gequal0(gel(x,2+v))) break;
1062 852007 : if (Z) *Z = RgX_shift_shallow(x, -v);
1063 852007 : return v;
1064 : }
1065 :
1066 : GEN
1067 68355 : RgXQC_red(GEN x, GEN T)
1068 415933 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
1069 :
1070 : GEN
1071 1211 : RgXQV_red(GEN x, GEN T)
1072 27832 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
1073 :
1074 : GEN
1075 13223 : RgXQM_red(GEN x, GEN T)
1076 81578 : { pari_APPLY_same(RgXQC_red(gel(x,i), T)) }
1077 :
1078 : GEN
1079 322 : RgXQM_mul(GEN P, GEN Q, GEN T)
1080 : {
1081 322 : return RgXQM_red(RgM_mul(P, Q), T);
1082 : }
1083 :
1084 : GEN
1085 508531 : RgXQX_red(GEN P, GEN T)
1086 : {
1087 508531 : long i, l = lg(P);
1088 508531 : GEN Q = cgetg(l, t_POL);
1089 508531 : Q[1] = P[1];
1090 2627874 : for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
1091 508531 : return normalizepol_lg(Q, l);
1092 : }
1093 :
1094 : GEN
1095 825319 : RgX_deriv(GEN x)
1096 : {
1097 825319 : long i,lx = lg(x)-1;
1098 : GEN y;
1099 :
1100 825319 : if (lx<3) return pol_0(varn(x));
1101 822218 : y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
1102 3639670 : for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
1103 822214 : y[1] = x[1]; return normalizepol_lg(y,i);
1104 : }
1105 :
1106 : GEN
1107 2583847 : RgX_recipspec_shallow(GEN x, long l, long n)
1108 : {
1109 : long i;
1110 2583847 : GEN z = cgetg(n+2,t_POL);
1111 2583851 : z[1] = 0; z += 2;
1112 144322581 : for(i=0; i<l; i++) gel(z,n-i-1) = gel(x,i);
1113 2833586 : for( ; i<n; i++) gel(z, n-i-1) = gen_0;
1114 2583851 : return normalizepol_lg(z-2,n+2);
1115 : }
1116 :
1117 : GEN
1118 642859 : RgXn_recip_shallow(GEN P, long n)
1119 : {
1120 642859 : GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
1121 642863 : setvarn(Q, varn(P));
1122 642863 : return Q;
1123 : }
1124 :
1125 : /* return coefficients s.t x = x_0 X^n + ... + x_n */
1126 : GEN
1127 31367 : RgX_recip(GEN x)
1128 : {
1129 : long lx, i, j;
1130 31367 : GEN y = cgetg_copy(x, &lx);
1131 274442 : y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
1132 31367 : return normalizepol_lg(y,lx);
1133 : }
1134 : /* shallow version */
1135 : GEN
1136 59388 : RgX_recip_shallow(GEN x)
1137 : {
1138 : long lx, i, j;
1139 59388 : GEN y = cgetg_copy(x, &lx);
1140 356118 : y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
1141 59388 : return normalizepol_lg(y,lx);
1142 : }
1143 :
1144 : GEN
1145 5112677 : RgX_recip_i(GEN x)
1146 : {
1147 : long lx, i, j;
1148 5112677 : GEN y = cgetg_copy(x, &lx);
1149 25943981 : y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
1150 5112678 : return y;
1151 : }
1152 : /*******************************************************************/
1153 : /* */
1154 : /* ADDITION / SUBTRACTION */
1155 : /* */
1156 : /*******************************************************************/
1157 : /* same variable */
1158 : GEN
1159 144847470 : RgX_add(GEN x, GEN y)
1160 : {
1161 144847470 : long i, lx = lg(x), ly = lg(y);
1162 : GEN z;
1163 144847470 : if (ly <= lx) {
1164 130595725 : z = cgetg(lx,t_POL); z[1] = x[1];
1165 508404096 : for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
1166 190351092 : for ( ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
1167 130574646 : z = normalizepol_lg(z, lx);
1168 : } else {
1169 14251745 : z = cgetg(ly,t_POL); z[1] = y[1];
1170 54483743 : for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
1171 41637125 : for ( ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
1172 14253043 : z = normalizepol_lg(z, ly);
1173 : }
1174 144840058 : return z;
1175 : }
1176 : GEN
1177 72801494 : RgX_sub(GEN x, GEN y)
1178 : {
1179 72801494 : long i, lx = lg(x), ly = lg(y);
1180 : GEN z;
1181 72801494 : if (ly <= lx) {
1182 38967777 : z = cgetg(lx,t_POL); z[1] = x[1];
1183 188286361 : for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
1184 66449760 : for ( ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
1185 38946504 : z = normalizepol_lg(z, lx);
1186 : } else {
1187 33833717 : z = cgetg(ly,t_POL); z[1] = y[1];
1188 125716156 : for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
1189 74938732 : for ( ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
1190 33801819 : z = normalizepol_lg(z, ly);
1191 : }
1192 72796541 : return z;
1193 : }
1194 : GEN
1195 8821261 : RgX_neg(GEN x)
1196 53146461 : { pari_APPLY_pol_normalized(gneg(gel(x,i))); }
1197 :
1198 : GEN
1199 44517153 : RgX_Rg_add(GEN y, GEN x)
1200 : {
1201 : GEN z;
1202 44517153 : long lz = lg(y), i;
1203 44517153 : if (lz == 2) return scalarpol(x,varn(y));
1204 22766699 : z = cgetg(lz,t_POL); z[1] = y[1];
1205 22766657 : gel(z,2) = gadd(gel(y,2),x);
1206 78931675 : for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
1207 : /* probably useless unless lz = 3, but cannot be skipped if y is
1208 : * an inexact 0 */
1209 22766634 : return normalizepol_lg(z,lz);
1210 : }
1211 : GEN
1212 65066 : RgX_Rg_add_shallow(GEN y, GEN x)
1213 : {
1214 : GEN z;
1215 65066 : long lz = lg(y), i;
1216 65066 : if (lz == 2) return scalarpol(x,varn(y));
1217 65066 : z = cgetg(lz,t_POL); z[1] = y[1];
1218 65066 : gel(z,2) = gadd(gel(y,2),x);
1219 130274 : for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
1220 65065 : return normalizepol_lg(z,lz);
1221 : }
1222 : GEN
1223 1222873 : RgX_Rg_sub(GEN y, GEN x)
1224 : {
1225 : GEN z;
1226 1222873 : long lz = lg(y), i;
1227 1222873 : if (lz == 2)
1228 : { /* scalarpol(gneg(x),varn(y)) optimized */
1229 922867 : long v = varn(y);
1230 922867 : if (isrationalzero(x)) return pol_0(v);
1231 2235 : z = cgetg(3,t_POL);
1232 2235 : z[1] = gequal0(x)? evalvarn(v)
1233 2235 : : evalvarn(v) | evalsigne(1);
1234 2235 : gel(z,2) = gneg(x); return z;
1235 : }
1236 300006 : z = cgetg(lz,t_POL); z[1] = y[1];
1237 300006 : gel(z,2) = gsub(gel(y,2),x);
1238 686789 : for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
1239 300004 : return normalizepol_lg(z,lz);
1240 : }
1241 : GEN
1242 4951574 : Rg_RgX_sub(GEN x, GEN y)
1243 : {
1244 : GEN z;
1245 4951574 : long lz = lg(y), i;
1246 4951574 : if (lz == 2) return scalarpol(x,varn(y));
1247 4881180 : z = cgetg(lz,t_POL); z[1] = y[1];
1248 4881155 : gel(z,2) = gsub(x, gel(y,2));
1249 7333178 : for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
1250 4881179 : return normalizepol_lg(z,lz);
1251 : }
1252 : /*******************************************************************/
1253 : /* */
1254 : /* KARATSUBA MULTIPLICATION */
1255 : /* */
1256 : /*******************************************************************/
1257 : #if 0
1258 : /* to debug Karatsuba-like routines */
1259 : GEN
1260 : zx_debug_spec(GEN x, long nx)
1261 : {
1262 : GEN z = cgetg(nx+2,t_POL);
1263 : long i;
1264 : for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
1265 : z[1] = evalsigne(1); return z;
1266 : }
1267 :
1268 : GEN
1269 : RgX_debug_spec(GEN x, long nx)
1270 : {
1271 : GEN z = cgetg(nx+2,t_POL);
1272 : long i;
1273 : for (i=0; i<nx; i++) z[i+2] = x[i];
1274 : z[1] = evalsigne(1); return z;
1275 : }
1276 : #endif
1277 :
1278 : /* generic multiplication */
1279 : GEN
1280 8907721 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
1281 : {
1282 : GEN z, t;
1283 : long i;
1284 8907721 : if (nx == ny) {
1285 1536118 : z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
1286 4953095 : for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
1287 1536113 : return normalizepol_lg(z, nx+2);
1288 : }
1289 7371603 : if (ny < nx) {
1290 7186425 : z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
1291 26453062 : for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
1292 17172427 : for ( ; i < nx; i++) gel(t,i) = gel(x,i);
1293 7185985 : return normalizepol_lg(z, nx+2);
1294 : } else {
1295 185178 : z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
1296 3671822 : for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
1297 447113 : for ( ; i < ny; i++) gel(t,i) = gel(y,i);
1298 185192 : return normalizepol_lg(z, ny+2);
1299 : }
1300 : }
1301 : GEN
1302 222287 : RgX_addspec(GEN x, GEN y, long nx, long ny)
1303 : {
1304 : GEN z, t;
1305 : long i;
1306 222287 : if (nx == ny) {
1307 12824 : z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
1308 2185778 : for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
1309 12824 : return normalizepol_lg(z, nx+2);
1310 : }
1311 209463 : if (ny < nx) {
1312 207685 : z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
1313 3724613 : for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
1314 2371419 : for ( ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
1315 207685 : return normalizepol_lg(z, nx+2);
1316 : } else {
1317 1778 : z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
1318 330904 : for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
1319 12222 : for ( ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
1320 1778 : return normalizepol_lg(z, ny+2);
1321 : }
1322 : }
1323 :
1324 : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
1325 : * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
1326 : * t_VECSMALL, to hold this, which can be left on stack: GC functions
1327 : * will not crash on it. The returned vector itself is not a proper GEN,
1328 : * we access the coefficients as x[i], i = 0..deg(x) */
1329 : static GEN
1330 81693988 : RgXspec_kill0(GEN x, long lx)
1331 : {
1332 81693988 : GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit GC-wise */
1333 : long i;
1334 246568359 : for (i=0; i <lx; i++)
1335 : {
1336 164874348 : GEN c = gel(x,i);
1337 164874348 : z[i] = (long)(isrationalzero(c)? NULL: c);
1338 : }
1339 81694011 : return z;
1340 : }
1341 :
1342 : INLINE GEN
1343 111053811 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
1344 : {
1345 111053811 : pari_sp av = avma;
1346 111053811 : GEN s = NULL;
1347 : long i;
1348 :
1349 391652715 : for (i=a; i<b; i++)
1350 280607609 : if (gel(y,i) && gel(x,-i))
1351 : {
1352 202282943 : GEN t = gmul(gel(y,i), gel(x,-i));
1353 202278284 : s = s? gadd(s, t): t;
1354 : }
1355 111045106 : return s? gc_upto(av, s): gen_0;
1356 : }
1357 :
1358 : /* assume nx >= ny > 0, return x * y * t^v */
1359 : static GEN
1360 34200119 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
1361 : {
1362 : long i, lz, nz;
1363 : GEN z;
1364 :
1365 34200119 : x = RgXspec_kill0(x,nx);
1366 34200097 : y = RgXspec_kill0(y,ny);
1367 34200107 : lz = nx + ny + 1; nz = lz-2;
1368 34200107 : lz += v;
1369 34200107 : z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
1370 72777271 : for (i=0; i<v; i++) gel(z++, 0) = gen_0;
1371 83192556 : for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
1372 56628465 : for ( ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
1373 48992487 : for ( ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
1374 34199474 : z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
1375 : }
1376 :
1377 : /* return (x * X^d) + y. Assume d > 0 */
1378 : GEN
1379 10066126 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
1380 : {
1381 : GEN x, y, xd, yd, zd;
1382 : long a, lz, nx, ny;
1383 :
1384 10066126 : if (!signe(x0)) return y0;
1385 9431290 : ny = lgpol(y0);
1386 9431293 : nx = lgpol(x0);
1387 9431404 : zd = (GEN)avma;
1388 9431404 : x = x0 + 2; y = y0 + 2; a = ny-d;
1389 9431404 : if (a <= 0)
1390 : {
1391 1479437 : lz = nx+d+2;
1392 1479437 : (void)new_chunk(lz); xd = x+nx; yd = y+ny;
1393 3397414 : while (xd > x) gel(--zd,0) = gel(--xd,0);
1394 1479439 : x = zd + a;
1395 1497136 : while (zd > x) gel(--zd,0) = gen_0;
1396 : }
1397 : else
1398 : {
1399 7951967 : xd = new_chunk(d); yd = y+d;
1400 7951983 : x = RgX_addspec_shallow(x,yd, nx,a);
1401 7951917 : lz = (a>nx)? ny+2: lg(x)+d;
1402 39625577 : x += 2; while (xd > x) *--zd = *--xd;
1403 : }
1404 22974112 : while (yd > y) *--zd = *--yd;
1405 9431356 : *--zd = x0[1];
1406 9431356 : *--zd = evaltyp(t_POL) | evallg(lz); return zd;
1407 : }
1408 : GEN
1409 514757 : RgX_addmulXn(GEN x0, GEN y0, long d)
1410 : {
1411 : GEN x, y, xd, yd, zd;
1412 : long a, lz, nx, ny;
1413 :
1414 514757 : if (!signe(x0)) return RgX_copy(y0);
1415 513973 : nx = lgpol(x0);
1416 513973 : ny = lgpol(y0);
1417 513973 : zd = (GEN)avma;
1418 513973 : x = x0 + 2; y = y0 + 2; a = ny-d;
1419 513973 : if (a <= 0)
1420 : {
1421 291686 : lz = nx+d+2;
1422 291686 : (void)new_chunk(lz); xd = x+nx; yd = y+ny;
1423 4293251 : while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
1424 291686 : x = zd + a;
1425 757796 : while (zd > x) gel(--zd,0) = gen_0;
1426 : }
1427 : else
1428 : {
1429 222287 : xd = new_chunk(d); yd = y+d;
1430 222287 : x = RgX_addspec(x,yd, nx,a);
1431 222287 : lz = (a>nx)? ny+2: lg(x)+d;
1432 8415473 : x += 2; while (xd > x) *--zd = *--xd;
1433 : }
1434 2614898 : while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
1435 513973 : *--zd = x0[1];
1436 513973 : *--zd = evaltyp(t_POL) | evallg(lz); return zd;
1437 : }
1438 :
1439 : /* return x * y mod t^n */
1440 : static GEN
1441 6627959 : RgXn_mul_basecase(GEN x, GEN y, long n)
1442 : {
1443 6627959 : long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
1444 : GEN z;
1445 6627959 : if (lx < 0) return pol_0(varn(x));
1446 6627959 : if (ly < 0) return pol_0(varn(x));
1447 6627959 : z = cgetg(lz, t_POL) + 2;
1448 6627959 : x+=2; if (lx > n) lx = n;
1449 6627959 : y+=2; if (ly > n) ly = n;
1450 6627959 : z[-1] = x[-1];
1451 6627959 : if (ly > lx) { swap(x,y); lswap(lx,ly); }
1452 6627959 : x = RgXspec_kill0(x, lx);
1453 6627959 : y = RgXspec_kill0(y, ly);
1454 : /* x:y:z [i] = term of degree i */
1455 26223885 : for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
1456 11826184 : for ( ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
1457 6674031 : for ( ; i<n; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
1458 6627959 : return normalizepol_lg(z - 2, lz);
1459 : }
1460 : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
1461 : static GEN
1462 9414318 : RgXn_mul2(GEN f, GEN g, long n)
1463 : {
1464 9414318 : pari_sp av = avma;
1465 : GEN fe,fo, ge,go, l,h,m;
1466 : long n0, n1;
1467 9414318 : if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
1468 6663449 : if (n < 80) return RgXn_mul_basecase(f,g,n);
1469 35490 : n0 = n>>1; n1 = n-n0;
1470 35490 : RgX_even_odd(f, &fe, &fo);
1471 35490 : RgX_even_odd(g, &ge, &go);
1472 35490 : l = RgXn_mul2(fe,ge,n1);
1473 35490 : h = RgXn_mul2(fo,go,n0);
1474 35490 : m = RgX_sub(RgXn_mul2(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
1475 : /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
1476 : * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
1477 35490 : l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
1478 : /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
1479 35490 : if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
1480 35490 : m = RgX_inflate(m,2);
1481 : /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
1482 35490 : if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
1483 35490 : h = RgX_inflate(h,2);
1484 35490 : h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
1485 35490 : return gc_upto(av, h);
1486 : }
1487 : /* (f*g) \/ x^n */
1488 : static GEN
1489 1589447 : RgX_mulhigh_i2(GEN f, GEN g, long n)
1490 : {
1491 1589447 : long d = degpol(f)+degpol(g) + 1 - n;
1492 : GEN h;
1493 1589447 : if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
1494 29654 : h = RgX_recip_i(RgXn_mul2(RgX_recip_i(f),
1495 : RgX_recip_i(g), d));
1496 29654 : return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
1497 : }
1498 :
1499 : /* (f*g) \/ x^n */
1500 : static GEN
1501 0 : RgX_sqrhigh_i2(GEN f, long n)
1502 : {
1503 0 : long d = 2*degpol(f)+ 1 - n;
1504 : GEN h;
1505 0 : if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
1506 0 : h = RgX_recip_i(RgXn_sqr(RgX_recip_i(f), d));
1507 0 : return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
1508 : }
1509 :
1510 : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
1511 : * b+2 were sent instead. na, nb = number of terms of a, b.
1512 : * Only c, c0, c1, c2 are genuine GEN.
1513 : */
1514 : GEN
1515 40266313 : RgX_mulspec(GEN a, GEN b, long na, long nb)
1516 : {
1517 : GEN a0, c, c0;
1518 40266313 : long n0, n0a, i, v = 0;
1519 : pari_sp av;
1520 :
1521 65264621 : while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
1522 61957021 : while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
1523 40266281 : if (na < nb) swapspec(a,b, na,nb);
1524 40266281 : if (!nb) return pol_0(0);
1525 :
1526 34679340 : if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
1527 479228 : RgX_shift_inplace_init(v);
1528 479228 : i = (na>>1); n0 = na-i; na = i;
1529 479228 : av = avma; a0 = a+n0; n0a = n0;
1530 1334087 : while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
1531 :
1532 479228 : if (nb > n0)
1533 : {
1534 : GEN b0,c1,c2;
1535 : long n0b;
1536 :
1537 477869 : nb -= n0; b0 = b+n0; n0b = n0;
1538 1444999 : while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
1539 477869 : c = RgX_mulspec(a,b,n0a,n0b);
1540 477869 : c0 = RgX_mulspec(a0,b0, na,nb);
1541 :
1542 477869 : c2 = RgX_addspec_shallow(a0,a, na,n0a);
1543 477869 : c1 = RgX_addspec_shallow(b0,b, nb,n0b);
1544 :
1545 477869 : c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
1546 477869 : c2 = RgX_sub(c1, RgX_add(c0,c));
1547 477869 : c0 = RgX_addmulXn_shallow(c0, c2, n0);
1548 : }
1549 : else
1550 : {
1551 1359 : c = RgX_mulspec(a,b,n0a,nb);
1552 1359 : c0 = RgX_mulspec(a0,b,na,nb);
1553 : }
1554 479228 : c0 = RgX_addmulXn(c0,c,n0);
1555 479228 : return RgX_shift_inplace(gc_upto(av,c0), v);
1556 : }
1557 :
1558 : INLINE GEN
1559 100082 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
1560 : {
1561 100082 : pari_sp av = avma;
1562 100082 : GEN s = NULL;
1563 100082 : long j, l = (i+1)>>1;
1564 203924 : for (j=a; j<l; j++)
1565 : {
1566 103842 : GEN xj = gel(x,j), xx = gel(x,i-j);
1567 103842 : if (xj && xx)
1568 : {
1569 94707 : GEN t = gmul(xj, xx);
1570 94707 : s = s? gadd(s, t): t;
1571 : }
1572 : }
1573 100082 : if (s) s = gshift(s,1);
1574 100082 : if ((i&1) == 0)
1575 : {
1576 69041 : GEN t = gel(x, i>>1);
1577 69041 : if (t) {
1578 65982 : t = gsqr(t);
1579 65982 : s = s? gadd(s, t): t;
1580 : }
1581 : }
1582 100082 : return s? gc_upto(av,s): gen_0;
1583 : }
1584 : static GEN
1585 38000 : RgX_sqrspec_basecase(GEN x, long nx, long v)
1586 : {
1587 : long i, lz, nz;
1588 : GEN z;
1589 :
1590 38000 : if (!nx) return pol_0(0);
1591 38000 : x = RgXspec_kill0(x,nx);
1592 38000 : lz = (nx << 1) + 1, nz = lz-2;
1593 38000 : lz += v;
1594 38000 : z = cgetg(lz,t_POL) + 2;
1595 94602 : for (i=0; i<v; i++) gel(z++, 0) = gen_0;
1596 107041 : for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
1597 69041 : for ( ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
1598 38000 : z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
1599 : }
1600 : /* return x^2 mod t^n */
1601 : static GEN
1602 0 : RgXn_sqr_basecase(GEN x, long n)
1603 : {
1604 0 : long i, lz = n+2, lx = lgpol(x);
1605 : GEN z;
1606 0 : if (lx < 0) return pol_0(varn(x));
1607 0 : z = cgetg(lz, t_POL);
1608 0 : z[1] = x[1];
1609 0 : x+=2; if (lx > n) lx = n;
1610 0 : x = RgXspec_kill0(x,lx);
1611 0 : z+=2;/* x:z [i] = term of degree i */
1612 0 : for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
1613 0 : for ( ; i<n; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
1614 0 : z -= 2; return normalizepol_lg(z, lz);
1615 : }
1616 : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
1617 : static GEN
1618 315 : RgXn_sqr2(GEN f, long n)
1619 : {
1620 315 : pari_sp av = avma;
1621 : GEN fe,fo, l,h,m;
1622 : long n0, n1;
1623 315 : if (2*degpol(f) < n) return RgX_sqr_i(f);
1624 0 : if (n < 80) return RgXn_sqr_basecase(f,n);
1625 0 : n0 = n>>1; n1 = n-n0;
1626 0 : RgX_even_odd(f, &fe, &fo);
1627 0 : l = RgXn_sqr(fe,n1);
1628 0 : h = RgXn_sqr(fo,n0);
1629 0 : m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
1630 : /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
1631 : * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
1632 0 : l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
1633 : /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
1634 0 : if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
1635 0 : m = RgX_inflate(m,2);
1636 : /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
1637 0 : if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
1638 0 : h = RgX_inflate(h,2);
1639 0 : h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
1640 0 : return gc_upto(av, h);
1641 : }
1642 : GEN
1643 38039 : RgX_sqrspec(GEN a, long na)
1644 : {
1645 : GEN a0, c, c0, c1;
1646 38039 : long n0, n0a, i, v = 0;
1647 : pari_sp av;
1648 :
1649 66340 : while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
1650 38039 : if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
1651 39 : RgX_shift_inplace_init(v);
1652 39 : i = (na>>1); n0 = na-i; na = i;
1653 39 : av = avma; a0 = a+n0; n0a = n0;
1654 39 : while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
1655 :
1656 39 : c = RgX_sqrspec(a,n0a);
1657 39 : c0 = RgX_sqrspec(a0,na);
1658 39 : c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
1659 39 : c0 = RgX_addmulXn_shallow(c0,c1, n0);
1660 39 : c0 = RgX_addmulXn(c0,c,n0);
1661 39 : return RgX_shift_inplace(gc_upto(av,c0), v);
1662 : }
1663 :
1664 : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
1665 : GEN
1666 1719093 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
1667 : {
1668 1719093 : GEN z = RgX_mul(A, B);
1669 1719091 : if (a < b)
1670 10375 : z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
1671 1708716 : else if (a > b)
1672 1104946 : z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
1673 : else
1674 603770 : z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
1675 1719094 : return z;
1676 : }
1677 :
1678 : GEN
1679 38830030 : RgX_mul_i(GEN x, GEN y)
1680 : {
1681 38830030 : GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
1682 38829627 : setvarn(z, varn(x)); return z;
1683 : }
1684 :
1685 : GEN
1686 37961 : RgX_sqr_i(GEN x)
1687 : {
1688 37961 : GEN z = RgX_sqrspec(x+2, lgpol(x));
1689 37961 : setvarn(z,varn(x)); return z;
1690 : }
1691 :
1692 : /*******************************************************************/
1693 : /* */
1694 : /* DIVISION */
1695 : /* */
1696 : /*******************************************************************/
1697 : GEN
1698 7177140 : RgX_Rg_divexact(GEN x, GEN y) {
1699 7177140 : long i, lx = lg(x);
1700 : GEN z;
1701 7177140 : if (lx == 2) return gcopy(x);
1702 7126177 : switch(typ(y))
1703 : {
1704 6964964 : case t_INT:
1705 6964964 : if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
1706 6589559 : break;
1707 5243 : case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
1708 : }
1709 6745529 : z = cgetg(lx, t_POL); z[1] = x[1];
1710 34437599 : for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
1711 6744510 : return z;
1712 : }
1713 : GEN
1714 30323283 : RgX_Rg_div(GEN x, GEN y) {
1715 30323283 : long i, lx = lg(x);
1716 : GEN z;
1717 30323283 : if (lx == 2) return gcopy(x);
1718 30049832 : switch(typ(y))
1719 : {
1720 20817270 : case t_INT:
1721 20817270 : if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
1722 4193690 : break;
1723 6069 : case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
1724 : }
1725 13420183 : z = cgetg(lx, t_POL); z[1] = x[1];
1726 50025079 : for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
1727 13419762 : return normalizepol_lg(z, lx);
1728 : }
1729 : GEN
1730 39130 : RgX_normalize(GEN x)
1731 : {
1732 39130 : GEN z, d = NULL;
1733 39130 : long i, n = lg(x)-1;
1734 39130 : for (i = n; i > 1; i--) { d = gel(x,i); if (!gequal0(d)) break; }
1735 39130 : if (i == 1) return pol_0(varn(x));
1736 39130 : if (i == n && isint1(d)) return x;
1737 17612 : n = i; z = cgetg(n+1, t_POL); z[1] = x[1];
1738 31612 : for (i=2; i<n; i++) gel(z,i) = gdiv(gel(x,i),d);
1739 17612 : gel(z,n) = Rg_get_1(d); return z;
1740 : }
1741 : GEN
1742 10458 : RgX_divs(GEN x, long y) { pari_APPLY_pol(gdivgs(gel(x,i),y)); }
1743 : GEN
1744 251070 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
1745 : {
1746 251070 : long l = lg(a), i;
1747 : GEN a0, z0, z;
1748 :
1749 251070 : if (l <= 3)
1750 : {
1751 0 : if (r) *r = l == 2? gen_0: gcopy(gel(a,2));
1752 0 : return pol_0(varn(a));
1753 : }
1754 251070 : z = cgetg(l-1, t_POL);
1755 251072 : z[1] = a[1];
1756 251072 : a0 = a + l-1;
1757 251072 : z0 = z + l-2; *z0 = *a0--;
1758 2954949 : for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
1759 : {
1760 2703882 : GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
1761 2703877 : gel(z0,0) = t;
1762 : }
1763 251067 : if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
1764 251067 : return z;
1765 : }
1766 : /* Polynomial division x / y:
1767 : * if pr = ONLY_REM return remainder, otherwise return quotient
1768 : * if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
1769 : * if pr != NULL set *pr to remainder, as the last object on stack */
1770 : /* assume, typ(x) = typ(y) = t_POL, same variable */
1771 : static GEN
1772 23512441 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
1773 : {
1774 : pari_sp avy, av, av1;
1775 : long dx,dy,dz,i,j,sx,lr;
1776 : GEN z,p1,p2,rem,y_lead,mod,p;
1777 : GEN (*f)(GEN,GEN);
1778 :
1779 23512441 : if (!signe(y)) pari_err_INV("RgX_divrem",y);
1780 :
1781 23512441 : dy = degpol(y);
1782 23512433 : y_lead = gel(y,dy+2);
1783 23512433 : if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
1784 : {
1785 0 : pari_warn(warner,"normalizing a polynomial with 0 leading term");
1786 0 : for (dy--; dy>=0; dy--)
1787 : {
1788 0 : y_lead = gel(y,dy+2);
1789 0 : if (!gequal0(y_lead)) break;
1790 : }
1791 : }
1792 23512425 : if (!dy) /* y is constant */
1793 : {
1794 6844 : if (pr == ONLY_REM) return pol_0(varn(x));
1795 6844 : z = RgX_Rg_div(x, y_lead);
1796 6844 : if (pr == ONLY_DIVIDES) return z;
1797 2630 : if (pr) *pr = pol_0(varn(x));
1798 2630 : return z;
1799 : }
1800 23505581 : dx = degpol(x);
1801 23505547 : if (dx < dy)
1802 : {
1803 3847489 : if (pr == ONLY_REM) return RgX_copy(x);
1804 348584 : if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
1805 348563 : z = pol_0(varn(x));
1806 348563 : if (pr) *pr = RgX_copy(x);
1807 348563 : return z;
1808 : }
1809 :
1810 : /* x,y in R[X], y non constant */
1811 19658058 : av = avma;
1812 19658058 : p = NULL;
1813 19658058 : if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
1814 : {
1815 130277 : z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
1816 130277 : if (!z) return gc_NULL(av);
1817 130277 : z = FpX_to_mod(z, p);
1818 130277 : if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
1819 71134 : return gc_upto(av, z);
1820 59143 : *pr = FpX_to_mod(*pr, p);
1821 59143 : return gc_all(av, 2, &z, pr);
1822 : }
1823 19527904 : switch(typ(y_lead))
1824 : {
1825 0 : case t_REAL:
1826 0 : y_lead = ginv(y_lead);
1827 0 : f = gmul; mod = NULL;
1828 0 : break;
1829 1878 : case t_INTMOD:
1830 1878 : case t_POLMOD: y_lead = ginv(y_lead);
1831 1878 : f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
1832 1878 : break;
1833 19526026 : default: if (gequal1(y_lead)) y_lead = NULL;
1834 19526009 : f = gdiv; mod = NULL;
1835 : }
1836 :
1837 19527887 : if (y_lead == NULL)
1838 17515851 : p2 = gel(x,dx+2);
1839 : else {
1840 : for(;;) {
1841 2012036 : p2 = f(gel(x,dx+2),y_lead);
1842 2012037 : p2 = simplify_shallow(p2);
1843 2012037 : if (!isexactzero(p2) || (--dx < 0)) break;
1844 : }
1845 2012037 : if (dx < dy) /* leading coeff of x was in fact zero */
1846 : {
1847 0 : if (pr == ONLY_DIVIDES) {
1848 0 : set_avma(av);
1849 0 : return (dx < 0)? pol_0(varn(x)) : NULL;
1850 : }
1851 0 : if (pr == ONLY_REM)
1852 : {
1853 0 : if (dx < 0)
1854 0 : return gc_GEN(av, scalarpol(p2, varn(x)));
1855 : else
1856 : {
1857 : GEN t;
1858 0 : set_avma(av);
1859 0 : t = cgetg(dx + 3, t_POL); t[1] = x[1];
1860 0 : for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
1861 0 : return t;
1862 : }
1863 : }
1864 0 : if (pr) /* cf ONLY_REM above */
1865 : {
1866 0 : if (dx < 0)
1867 : {
1868 0 : p2 = gclone(p2);
1869 0 : set_avma(av);
1870 0 : z = pol_0(varn(x));
1871 0 : x = scalarpol(p2, varn(x));
1872 0 : gunclone(p2);
1873 : }
1874 : else
1875 : {
1876 : GEN t;
1877 0 : set_avma(av);
1878 0 : z = pol_0(varn(x));
1879 0 : t = cgetg(dx + 3, t_POL); t[1] = x[1];
1880 0 : for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
1881 0 : x = t;
1882 : }
1883 0 : *pr = x;
1884 : }
1885 : else
1886 : {
1887 0 : set_avma(av);
1888 0 : z = pol_0(varn(x));
1889 : }
1890 0 : return z;
1891 : }
1892 : }
1893 : /* dx >= dy */
1894 19527888 : avy = avma;
1895 19527888 : dz = dx-dy;
1896 19527888 : z = cgetg(dz+3,t_POL); z[1] = x[1];
1897 19527861 : x += 2;
1898 19527861 : z += 2;
1899 19527861 : y += 2;
1900 19527861 : gel(z,dz) = gcopy(p2);
1901 :
1902 54086021 : for (i=dx-1; i>=dy; i--)
1903 : {
1904 34558324 : av1=avma; p1=gel(x,i);
1905 1140261135 : for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
1906 34553804 : if (y_lead) p1 = simplify(f(p1,y_lead));
1907 :
1908 34553802 : if (isrationalzero(p1)) { set_avma(av1); p1 = gen_0; }
1909 : else
1910 24564486 : p1 = avma==av1? gcopy(p1): gc_upto(av1,p1);
1911 34557736 : gel(z,i-dy) = p1;
1912 : }
1913 19527697 : if (!pr) return gc_upto(av,z-2);
1914 :
1915 12550320 : rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
1916 12907335 : for (sx=0; ; i--)
1917 : {
1918 12907335 : p1 = gel(x,i);
1919 : /* we always enter this loop at least once */
1920 32219408 : for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
1921 12906316 : if (mod && avma==av1) p1 = gmul(p1,mod);
1922 12906316 : if (!gequal0(p1)) { sx = 1; break; } /* remainder is nonzero */
1923 3576646 : if (!isexactzero(p1)) break;
1924 3426499 : if (!i) break;
1925 356982 : set_avma(av1);
1926 : }
1927 12549639 : if (pr == ONLY_DIVIDES)
1928 : {
1929 7931 : if (sx) return gc_NULL(av);
1930 7910 : set_avma((pari_sp)rem); return gc_upto(av,z-2);
1931 : }
1932 12541708 : lr=i+3; rem -= lr;
1933 12541708 : if (avma==av1) { set_avma((pari_sp)rem); p1 = gcopy(p1); }
1934 12499419 : else p1 = gc_upto((pari_sp)rem,p1);
1935 12542374 : rem[0] = evaltyp(t_POL) | _evallg(lr);
1936 12542374 : rem[1] = z[-1];
1937 12542374 : rem += 2;
1938 12542374 : gel(rem,i) = p1;
1939 17282808 : for (i--; i>=0; i--)
1940 : {
1941 4740430 : av1=avma; p1 = gel(x,i);
1942 13638521 : for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
1943 4740034 : if (mod && avma==av1) p1 = gmul(p1,mod);
1944 4740237 : gel(rem,i) = avma==av1? gcopy(p1):gc_upto(av1,p1);
1945 : }
1946 12542378 : rem -= 2;
1947 12542378 : if (!sx) (void)normalizepol_lg(rem, lr);
1948 12542552 : if (pr == ONLY_REM) return gc_upto(av,rem);
1949 6760726 : z -= 2; *pr = rem; return gc_all_unsafe(av,avy,2,&z,pr);
1950 : }
1951 :
1952 : GEN
1953 14231746 : RgX_divrem(GEN x, GEN y, GEN *pr)
1954 : {
1955 14231746 : if (pr == ONLY_REM) return RgX_rem(x, y);
1956 14231746 : return RgX_divrem_i(x, y, pr);
1957 : }
1958 :
1959 : /* x and y in (R[Y]/T)[X] (lifted), T in R[Y]. y preferably monic */
1960 : GEN
1961 155695 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
1962 : {
1963 155695 : long vx = varn(x), dx = degpol(x), dy = degpol(y), dz, i, j, sx, lr;
1964 : pari_sp av0, av;
1965 : GEN z, p1, rem, lead;
1966 :
1967 155695 : if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
1968 155695 : if (dx < dy)
1969 : {
1970 41196 : if (pr)
1971 : {
1972 41168 : av0 = avma; x = RgXQX_red(x, T);
1973 41168 : if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: gen_0; }
1974 41161 : if (pr == ONLY_REM) return x;
1975 0 : *pr = x;
1976 : }
1977 28 : return pol_0(vx);
1978 : }
1979 114499 : lead = leading_coeff(y);
1980 114499 : if (!dy) /* y is constant */
1981 : {
1982 602 : if (pr && pr != ONLY_DIVIDES)
1983 : {
1984 0 : if (pr == ONLY_REM) return pol_0(vx);
1985 0 : *pr = pol_0(vx);
1986 : }
1987 602 : if (gequal1(lead)) return RgX_copy(x);
1988 0 : av0 = avma; x = gmul(x, ginvmod(lead,T));
1989 0 : return gc_upto(av0, RgXQX_red(x,T));
1990 : }
1991 113897 : av0 = avma; dz = dx-dy;
1992 113897 : lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
1993 113897 : set_avma(av0);
1994 113897 : z = cgetg(dz+3,t_POL); z[1] = x[1];
1995 113897 : x += 2; y += 2; z += 2;
1996 :
1997 113897 : p1 = gel(x,dx); av = avma;
1998 113897 : gel(z,dz) = lead? gc_upto(av, grem(gmul(p1,lead), T)): gcopy(p1);
1999 579405 : for (i=dx-1; i>=dy; i--)
2000 : {
2001 465508 : av = avma; p1 = gel(x,i);
2002 2412676 : for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
2003 465484 : if (lead) p1 = gmul(grem(p1, T), lead);
2004 465484 : gel(z,i-dy) = gc_upto(av, grem(p1, T));
2005 : }
2006 113897 : if (!pr) { guncloneNULL(lead); return z-2; }
2007 :
2008 112623 : rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
2009 189321 : for (sx=0; ; i--)
2010 : {
2011 189321 : p1 = gel(x,i);
2012 655004 : for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
2013 189322 : p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
2014 103816 : if (!i) break;
2015 76698 : set_avma(av);
2016 : }
2017 112623 : if (pr == ONLY_DIVIDES)
2018 : {
2019 27087 : guncloneNULL(lead);
2020 27087 : if (sx) return gc_NULL(av0);
2021 24528 : return gc_const((pari_sp)rem, z-2);
2022 : }
2023 85536 : lr=i+3; rem -= lr; av = (pari_sp)rem;
2024 85536 : rem[0] = evaltyp(t_POL) | _evallg(lr);
2025 85536 : rem[1] = z[-1];
2026 85536 : rem += 2; gel(rem,i) = gc_upto(av, p1);
2027 186728 : for (i--; i>=0; i--)
2028 : {
2029 101192 : av = avma; p1 = gel(x,i);
2030 337089 : for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
2031 101192 : gel(rem,i) = gc_upto(av, grem(p1, T));
2032 : }
2033 85536 : rem -= 2;
2034 85536 : guncloneNULL(lead);
2035 85536 : if (!sx) (void)normalizepol_lg(rem, lr);
2036 85536 : if (pr == ONLY_REM) return gc_upto(av0,rem);
2037 168 : *pr = rem; return z-2;
2038 : }
2039 :
2040 : /*******************************************************************/
2041 : /* */
2042 : /* PSEUDO-DIVISION */
2043 : /* */
2044 : /*******************************************************************/
2045 : INLINE GEN
2046 4042821 : rem(GEN c, GEN T)
2047 : {
2048 4042821 : if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
2049 4042821 : return c;
2050 : }
2051 :
2052 : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
2053 : int
2054 17342 : ZXQX_dvd(GEN x, GEN y, GEN T)
2055 : {
2056 : long dx, dy, i, T_ismonic;
2057 17342 : pari_sp av = avma, av2;
2058 : GEN y_lead;
2059 :
2060 17342 : if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
2061 17342 : dy = degpol(y); y_lead = gel(y,dy+2);
2062 17342 : if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
2063 : /* if monic, no point in using pseudo-division */
2064 17342 : if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
2065 14794 : T_ismonic = gequal1(leading_coeff(T));
2066 14794 : dx = degpol(x);
2067 14794 : if (dx < dy) return !signe(x);
2068 14794 : (void)new_chunk(2);
2069 14794 : x = RgX_recip_i(x)+2;
2070 14794 : y = RgX_recip_i(y)+2;
2071 : /* pay attention to sparse divisors */
2072 30089 : for (i = 1; i <= dy; i++)
2073 15295 : if (!signe(gel(y,i))) gel(y,i) = NULL;
2074 14794 : av2 = avma;
2075 : for (;;)
2076 73009 : {
2077 87803 : GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
2078 87803 : x0 = gneg(x0);
2079 87803 : m = gcdii(cx, y0);
2080 87803 : if (!equali1(m))
2081 : {
2082 85301 : x0 = gdiv(x0, m);
2083 85301 : y0 = diviiexact(y0, m);
2084 85301 : if (equali1(y0)) y0 = NULL;
2085 : }
2086 179379 : for (i=1; i<=dy; i++)
2087 : {
2088 91576 : GEN c = gel(x,i); if (y0) c = gmul(y0, c);
2089 91576 : if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
2090 91576 : if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
2091 91576 : gel(x,i) = c;
2092 : }
2093 688168 : for ( ; i<=dx; i++)
2094 : {
2095 600365 : GEN c = gel(x,i); if (y0) c = gmul(y0, c);
2096 600365 : if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
2097 600365 : gel(x,i) = c;
2098 : }
2099 102349 : do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
2100 87803 : if (dx < dy) break;
2101 73009 : if (gc_needed(av2,1))
2102 : {
2103 28 : if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
2104 28 : gc_slice(av2,x,dx+1);
2105 : }
2106 : }
2107 14794 : return gc_bool(av, dx < 0);
2108 : }
2109 :
2110 : /* T either NULL or a t_POL. */
2111 : GEN
2112 627555 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
2113 : {
2114 627555 : long vx = varn(x), dx, dy, dz, i, lx, p;
2115 627555 : pari_sp av = avma, av2;
2116 : GEN y_lead;
2117 :
2118 627555 : if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
2119 627555 : dy = degpol(y); y_lead = gel(y,dy+2);
2120 : /* if monic, no point in using pseudo-division */
2121 627555 : if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
2122 579244 : dx = degpol(x);
2123 579244 : if (dx < dy) return RgX_copy(x);
2124 579237 : (void)new_chunk(2);
2125 579237 : x = RgX_recip_i(x)+2;
2126 579239 : y = RgX_recip_i(y)+2;
2127 : /* pay attention to sparse divisors */
2128 1951538 : for (i = 1; i <= dy; i++)
2129 1372298 : if (isexactzero(gel(y,i))) gel(y,i) = NULL;
2130 579240 : dz = dx-dy; p = dz+1;
2131 579240 : av2 = avma;
2132 : for (;;)
2133 : {
2134 1246560 : gel(x,0) = gneg(gel(x,0)); p--;
2135 4192195 : for (i=1; i<=dy; i++)
2136 : {
2137 2945655 : GEN c = gmul(y_lead, gel(x,i));
2138 2945565 : if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
2139 2945624 : gel(x,i) = rem(c, T);
2140 : }
2141 2182526 : for ( ; i<=dx; i++)
2142 : {
2143 935967 : GEN c = gmul(y_lead, gel(x,i));
2144 935956 : gel(x,i) = rem(c, T);
2145 : }
2146 1289362 : do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
2147 1246559 : if (dx < dy) break;
2148 667320 : if (gc_needed(av2,1))
2149 : {
2150 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
2151 0 : gc_slice(av2,x,dx+1);
2152 : }
2153 : }
2154 579239 : if (dx < 0) return pol_0(vx);
2155 579078 : lx = dx+3; x -= 2;
2156 579078 : x[0] = evaltyp(t_POL) | _evallg(lx);
2157 579078 : x[1] = evalsigne(1) | evalvarn(vx);
2158 579078 : x = RgX_recip_i(x);
2159 579079 : if (p)
2160 : { /* multiply by y[0]^p [beware dummy vars from FpX_FpXY_resultant] */
2161 28670 : GEN t = y_lead;
2162 28670 : if (T && typ(t) == t_POL && varn(t) == varn(T))
2163 0 : t = RgXQ_powu(t, p, T);
2164 : else
2165 28670 : t = gpowgs(t, p);
2166 88361 : for (i=2; i<lx; i++)
2167 : {
2168 59692 : GEN c = gmul(gel(x,i), t);
2169 59691 : gel(x,i) = rem(c,T);
2170 : }
2171 28669 : if (!T) return gc_upto(av, x);
2172 : }
2173 550409 : return gc_GEN(av, x);
2174 : }
2175 :
2176 : GEN
2177 627555 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
2178 :
2179 : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
2180 : GEN
2181 12063 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
2182 : {
2183 12063 : long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
2184 12063 : pari_sp av = avma, av2;
2185 : GEN z, r, ypow, y_lead;
2186 :
2187 12063 : if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
2188 12063 : dy = degpol(y); y_lead = gel(y,dy+2);
2189 12063 : if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
2190 7561 : dx = degpol(x);
2191 7561 : if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
2192 7561 : if (dx == dy)
2193 : {
2194 98 : GEN x_lead = gel(x,lg(x)-1);
2195 98 : x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
2196 98 : y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
2197 98 : r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
2198 98 : *ptr = gc_upto(av, r); return scalarpol(x_lead, vx);
2199 : }
2200 7463 : (void)new_chunk(2);
2201 7463 : x = RgX_recip_i(x)+2;
2202 7463 : y = RgX_recip_i(y)+2;
2203 : /* pay attention to sparse divisors */
2204 39000 : for (i = 1; i <= dy; i++)
2205 31537 : if (isexactzero(gel(y,i))) gel(y,i) = NULL;
2206 7463 : dz = dx-dy; p = dz+1;
2207 7463 : lz = dz+3;
2208 7463 : z = cgetg(lz, t_POL);
2209 7463 : z[1] = evalsigne(1) | evalvarn(vx);
2210 28640 : for (i = 2; i < lz; i++) gel(z,i) = gen_0;
2211 7463 : ypow = new_chunk(dz+1);
2212 7463 : gel(ypow,0) = gen_1;
2213 7463 : gel(ypow,1) = y_lead;
2214 13714 : for (i=2; i<=dz; i++)
2215 : {
2216 6251 : GEN c = gmul(gel(ypow,i-1), y_lead);
2217 6251 : gel(ypow,i) = rem(c,T);
2218 : }
2219 7463 : av2 = avma;
2220 7463 : for (iz=2;;)
2221 : {
2222 15605 : p--;
2223 15605 : gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
2224 69915 : for (i=1; i<=dy; i++)
2225 : {
2226 54310 : GEN c = gmul(y_lead, gel(x,i));
2227 54310 : if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
2228 54310 : gel(x,i) = rem(c, T);
2229 : }
2230 41058 : for ( ; i<=dx; i++)
2231 : {
2232 25453 : GEN c = gmul(y_lead, gel(x,i));
2233 25453 : gel(x,i) = rem(c,T);
2234 : }
2235 15605 : x++; dx--;
2236 21177 : while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
2237 15605 : if (dx < dy) break;
2238 8142 : if (gc_needed(av2,1))
2239 : {
2240 0 : GEN X = x-2;
2241 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
2242 0 : X[0] = evaltyp(t_POL)|_evallg(dx+3); X[1] = z[1]; /* hack */
2243 0 : (void)gc_all(av2,2, &X, &z); x = X+2;
2244 : }
2245 : }
2246 14008 : while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
2247 7463 : if (dx < 0)
2248 182 : x = pol_0(vx);
2249 : else
2250 : {
2251 7281 : lx = dx+3; x -= 2;
2252 7281 : x[0] = evaltyp(t_POL) | _evallg(lx);
2253 7281 : x[1] = evalsigne(1) | evalvarn(vx);
2254 7281 : x = RgX_recip_i(x);
2255 : }
2256 7463 : z = RgX_recip_i(z);
2257 7463 : r = x;
2258 7463 : if (p)
2259 : {
2260 3339 : GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
2261 3339 : if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
2262 : }
2263 7463 : *ptr = r; return gc_all(av, 2, &z, ptr);
2264 : }
2265 : GEN
2266 11818 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
2267 11818 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
2268 :
2269 : GEN
2270 0 : RgXQX_mul(GEN x, GEN y, GEN T)
2271 0 : { return RgXQX_red(RgX_mul(x,y), T); }
2272 : GEN
2273 748370705 : RgX_Rg_mul(GEN x, GEN y) { pari_APPLY_pol(gmul(y, gel(x,i))); }
2274 : GEN
2275 141351 : RgX_mul2n(GEN x, long n) { pari_APPLY_pol(gmul2n(gel(x,i), n)); }
2276 : GEN
2277 26320 : RgX_muls(GEN x, long y) { pari_APPLY_pol(gmulsg(y, gel(x,i))); }
2278 : GEN
2279 35 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T) { return RgXQX_red(RgX_Rg_mul(x,y), T); }
2280 : GEN
2281 133 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T) { return RgXQV_red(RgV_Rg_mul(v,x), T); }
2282 :
2283 : GEN
2284 0 : RgXQX_sqr(GEN x, GEN T) { return RgXQX_red(RgX_sqr(x), T); }
2285 :
2286 : GEN
2287 0 : RgXQX_powers(GEN P, long n, GEN T)
2288 : {
2289 0 : GEN v = cgetg(n+2, t_VEC);
2290 : long i;
2291 0 : gel(v, 1) = pol_1(varn(T));
2292 0 : if (n==0) return v;
2293 0 : gel(v, 2) = gcopy(P);
2294 0 : for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
2295 0 : return v;
2296 : }
2297 :
2298 : static GEN
2299 623029 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
2300 : static GEN
2301 0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
2302 : static GEN
2303 67313 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
2304 : static GEN
2305 88307 : _pow(void *data, GEN x, GEN n) { return RgXQ_pow(x, n, (GEN)data); }
2306 : static GEN
2307 276613 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
2308 : static GEN
2309 1039969 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
2310 : static GEN
2311 1018644 : _one(void *data) { return pol_1(varn((GEN)data)); }
2312 : static GEN
2313 1232 : _zero(void *data) { return pol_0(varn((GEN)data)); }
2314 : static GEN
2315 723914 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
2316 :
2317 : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
2318 : _mul, _sqr, _one, _zero };
2319 :
2320 : GEN
2321 0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
2322 : {
2323 0 : return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
2324 : }
2325 :
2326 : GEN
2327 417201 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
2328 : {
2329 417201 : int use_sqr = 2*degpol(x) >= degpol(T);
2330 417199 : return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
2331 : }
2332 :
2333 : /* mod X^n */
2334 : struct modXn {
2335 : long v; /* varn(X) */
2336 : long n;
2337 : } ;
2338 : static GEN
2339 11893 : _sqrXn(void *data, GEN x) {
2340 11893 : struct modXn *S = (struct modXn*)data;
2341 11893 : return RgXn_sqr(x, S->n);
2342 : }
2343 : static GEN
2344 4528 : _mulXn(void *data, GEN x, GEN y) {
2345 4528 : struct modXn *S = (struct modXn*)data;
2346 4528 : return RgXn_mul(x,y, S->n);
2347 : }
2348 : static GEN
2349 1939 : _oneXn(void *data) {
2350 1939 : struct modXn *S = (struct modXn*)data;
2351 1939 : return pol_1(S->v);
2352 : }
2353 : static GEN
2354 0 : _zeroXn(void *data) {
2355 0 : struct modXn *S = (struct modXn*)data;
2356 0 : return pol_0(S->v);
2357 : }
2358 : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
2359 : _oneXn, _zeroXn };
2360 :
2361 : GEN
2362 483 : RgXn_powers(GEN x, long m, long n)
2363 : {
2364 483 : long d = degpol(x);
2365 483 : int use_sqr = (d<<1) >= n;
2366 : struct modXn S;
2367 483 : S.v = varn(x); S.n = n;
2368 483 : return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
2369 : }
2370 :
2371 : GEN
2372 2286 : RgXn_powu_i(GEN x, ulong m, long n)
2373 : {
2374 : struct modXn S;
2375 : long v;
2376 2286 : if (n == 0) return x;
2377 2286 : v = RgX_valrem(x, &x);
2378 2286 : if (v) { n -= m * v; if (n <= 0) return pol_0(varn(x)); }
2379 2265 : S.v = varn(x); S.n = n;
2380 2265 : x = gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
2381 2265 : if (v) x = RgX_shift_shallow(x, m * v);
2382 2265 : return x;
2383 : }
2384 : GEN
2385 0 : RgXn_powu(GEN x, ulong m, long n)
2386 : {
2387 : pari_sp av;
2388 0 : if (n == 0) return gcopy(x);
2389 0 : av = avma; return gc_GEN(av, RgXn_powu_i(x, m, n));
2390 : }
2391 :
2392 : GEN
2393 966 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
2394 : {
2395 : struct modXn S;
2396 966 : S.v = varn(gel(x,2)); S.n = n;
2397 966 : return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
2398 : }
2399 :
2400 : GEN
2401 0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
2402 : {
2403 0 : int use_sqr = 2*degpol(x) >= n;
2404 : struct modXn S;
2405 0 : S.v = varn(x); S.n = n;
2406 0 : return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
2407 : }
2408 :
2409 : /* Q(x) mod t^n, x in R[t], n >= 1 */
2410 : GEN
2411 5159 : RgXn_eval(GEN Q, GEN x, long n)
2412 : {
2413 5159 : long d = degpol(x);
2414 : int use_sqr;
2415 : struct modXn S;
2416 5159 : if (d == 1 && isrationalzero(gel(x,2)))
2417 : {
2418 5152 : GEN y = RgX_unscale(Q, gel(x,3));
2419 5152 : setvarn(y, varn(x)); return y;
2420 : }
2421 7 : S.v = varn(x);
2422 7 : S.n = n;
2423 7 : use_sqr = (d<<1) >= n;
2424 7 : return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
2425 : }
2426 :
2427 : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
2428 : static GEN
2429 2614673 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
2430 : {
2431 2614673 : GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
2432 2614673 : return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
2433 : }
2434 :
2435 : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
2436 : static GEN
2437 14 : RgXn_sqrhigh(GEN f, long n2, long n)
2438 : {
2439 14 : GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
2440 14 : return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
2441 : }
2442 :
2443 : static GEN
2444 2188949 : RgXn_div_gen(GEN g, GEN f, long e)
2445 : {
2446 : pari_sp av;
2447 : ulong mask;
2448 : GEN W, a;
2449 2188949 : long v = varn(f), n = 1;
2450 :
2451 2188949 : if (!signe(f)) pari_err_INV("RgXn_inv",f);
2452 2188942 : a = ginv(gel(f,2));
2453 2188938 : if (e == 1 && !g) return scalarpol(a, v);
2454 2184052 : else if (e == 2 && !g)
2455 : {
2456 : GEN b;
2457 836935 : if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
2458 246253 : b = gneg(b);
2459 246252 : if (!gequal1(a)) b = gmul(b, gsqr(a));
2460 246252 : return deg1pol(b, a, v);
2461 : }
2462 1347117 : av = avma;
2463 1347117 : W = scalarpol_shallow(a,v);
2464 1347117 : mask = quadratic_prec_mask(e);
2465 3955833 : while (mask > 1)
2466 : {
2467 : GEN u, fr;
2468 2608716 : long n2 = n;
2469 2608716 : n<<=1; if (mask & 1) n--;
2470 2608716 : mask >>= 1;
2471 2608716 : fr = RgXn_red_shallow(f, n);
2472 2608716 : if (mask>1 || !g)
2473 : {
2474 1324736 : u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
2475 1324736 : W = RgX_sub(W, RgX_shift_shallow(u, n2));
2476 : }
2477 : else
2478 : {
2479 1283980 : GEN y = RgXn_mul(g, W, n), yt = RgXn_red_shallow(y, n-n2);
2480 1283980 : u = RgXn_mul(yt, RgXn_mulhigh(fr, W, n2, n), n-n2);
2481 1283980 : W = RgX_sub(y, RgX_shift_shallow(u, n2));
2482 : }
2483 2608716 : if (gc_needed(av,2))
2484 : {
2485 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
2486 0 : W = gc_upto(av, W);
2487 : }
2488 : }
2489 1347117 : return W;
2490 : }
2491 :
2492 : static GEN
2493 119 : RgXn_div_FpX(GEN x, GEN y, long e, GEN p)
2494 : {
2495 : GEN r;
2496 119 : if (lgefint(p) == 3)
2497 : {
2498 119 : ulong pp = uel(p, 2);
2499 119 : if (pp == 2)
2500 14 : r = F2x_to_ZX(F2xn_div(RgX_to_F2x(x), RgX_to_F2x(y), e));
2501 : else
2502 105 : r = Flx_to_ZX_inplace(Flxn_div(RgX_to_Flx(x, pp), RgX_to_Flx(y, pp), e, pp));
2503 : }
2504 : else
2505 0 : r = FpXn_div(RgX_to_FpX(x, p), RgX_to_FpX(y, p), e, p);
2506 119 : return FpX_to_mod(r, p);
2507 : }
2508 :
2509 : static GEN
2510 7 : RgXn_div_FpXQX(GEN x, GEN y, long n, GEN pol, GEN p)
2511 : {
2512 7 : GEN r, T = RgX_to_FpX(pol, p);
2513 7 : if (signe(T) == 0) pari_err_OP("/", x, y);
2514 7 : r = FpXQXn_div(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), n, T, p);
2515 7 : return FpXQX_to_mod(r, T, p);
2516 : }
2517 :
2518 : static GEN
2519 91 : RgXn_inv_FpX(GEN x, long e, GEN p)
2520 : {
2521 : GEN r;
2522 91 : if (lgefint(p) == 3)
2523 : {
2524 91 : ulong pp = uel(p, 2);
2525 91 : if (pp == 2)
2526 28 : r = F2x_to_ZX(F2xn_inv(RgX_to_F2x(x), e));
2527 : else
2528 63 : r = Flx_to_ZX_inplace(Flxn_inv(RgX_to_Flx(x, pp), e, pp));
2529 : }
2530 : else
2531 0 : r = FpXn_inv(RgX_to_FpX(x, p), e, p);
2532 91 : return FpX_to_mod(r, p);
2533 : }
2534 :
2535 : static GEN
2536 0 : RgXn_inv_FpXQX(GEN x, long n, GEN pol, GEN p)
2537 : {
2538 0 : GEN r, T = RgX_to_FpX(pol, p);
2539 0 : if (signe(T) == 0) pari_err_OP("/", gen_1, x);
2540 0 : r = FpXQXn_inv(RgX_to_FpXQX(x, T, p), n, T, p);
2541 0 : return FpXQX_to_mod(r, T, p);
2542 : }
2543 :
2544 : static GEN
2545 905054 : RgXn_inv_fast(GEN x, long e)
2546 : {
2547 : GEN p, pol;
2548 : long pa;
2549 905054 : long t = RgX_type(x,&p,&pol,&pa);
2550 905060 : switch(t)
2551 : {
2552 91 : case t_INTMOD: return RgXn_inv_FpX(x, e, p);
2553 0 : case RgX_type_code(t_POLMOD, t_INTMOD):
2554 0 : return RgXn_inv_FpXQX(x, e, pol, p);
2555 904969 : default: return NULL;
2556 : }
2557 : }
2558 :
2559 : static GEN
2560 1284106 : RgXn_div_fast(GEN x, GEN y, long e)
2561 : {
2562 : GEN p, pol;
2563 : long pa;
2564 1284106 : long t = RgX_type2(x,y,&p,&pol,&pa);
2565 1284106 : switch(t)
2566 : {
2567 119 : case t_INTMOD: return RgXn_div_FpX(x, y, e, p);
2568 7 : case RgX_type_code(t_POLMOD, t_INTMOD):
2569 7 : return RgXn_div_FpXQX(x, y, e, pol, p);
2570 1283980 : default: return NULL;
2571 : }
2572 : }
2573 :
2574 : GEN
2575 1284106 : RgXn_div_i(GEN g, GEN f, long e)
2576 : {
2577 1284106 : GEN h = RgXn_div_fast(g, f, e);
2578 1284106 : if (h) return h;
2579 1283980 : return RgXn_div_gen(g, f, e);
2580 : }
2581 :
2582 : GEN
2583 584443 : RgXn_div(GEN g, GEN f, long e)
2584 : {
2585 584443 : pari_sp av = avma;
2586 584443 : return gc_upto(av, RgXn_div_i(g, f, e));
2587 : }
2588 :
2589 : GEN
2590 905054 : RgXn_inv_i(GEN f, long e)
2591 : {
2592 905054 : GEN h = RgXn_inv_fast(f, e);
2593 905060 : if (h) return h;
2594 904969 : return RgXn_div_gen(NULL, f, e);
2595 : }
2596 :
2597 : GEN
2598 807474 : RgXn_inv(GEN f, long e)
2599 : {
2600 807474 : pari_sp av = avma;
2601 807474 : return gc_upto(av, RgXn_inv_i(f, e));
2602 : }
2603 :
2604 : /* intformal(x^n*S) / x^(n+1) */
2605 : static GEN
2606 56375 : RgX_integXn(GEN x, long n)
2607 114353 : { pari_APPLY_pol_normalized(gdivgs(gel(x,i), n+i-1)); }
2608 :
2609 : GEN
2610 52918 : RgXn_expint(GEN h, long e)
2611 : {
2612 52918 : pari_sp av = avma, av2;
2613 52918 : long v = varn(h), n;
2614 52918 : GEN f = pol_1(v), g;
2615 : ulong mask;
2616 :
2617 52918 : if (!signe(h)) return f;
2618 50426 : g = pol_1(v);
2619 50427 : n = 1; mask = quadratic_prec_mask(e);
2620 50427 : av2 = avma;
2621 56377 : for (;mask>1;)
2622 : {
2623 : GEN u, w;
2624 56377 : long n2 = n;
2625 56377 : n<<=1; if (mask & 1) n--;
2626 56377 : mask >>= 1;
2627 56377 : u = RgXn_mul(g, RgX_mulhigh_i(f, RgXn_red_shallow(h, n2-1), n2-1), n-n2);
2628 56375 : u = RgX_add(u, RgX_shift_shallow(RgXn_red_shallow(h, n-1), 1-n2));
2629 56375 : w = RgXn_mul(f, RgX_integXn(u, n2-1), n-n2);
2630 56377 : f = RgX_add(f, RgX_shift_shallow(w, n2));
2631 56376 : if (mask<=1) break;
2632 5950 : u = RgXn_mul(g, RgXn_mulhigh(f, g, n2, n), n-n2);
2633 5950 : g = RgX_sub(g, RgX_shift_shallow(u, n2));
2634 5950 : if (gc_needed(av2,2))
2635 : {
2636 0 : if (DEBUGMEM>1) pari_warn(warnmem,"RgXn_expint, e = %ld", n);
2637 0 : (void)gc_all(av2, 2, &f, &g);
2638 : }
2639 : }
2640 50426 : return gc_upto(av, f);
2641 : }
2642 :
2643 : GEN
2644 0 : RgXn_exp(GEN h, long e)
2645 : {
2646 0 : long d = degpol(h);
2647 0 : if (d < 0) return pol_1(varn(h));
2648 0 : if (!d || !gequal0(gel(h,2)))
2649 0 : pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
2650 0 : return RgXn_expint(RgX_deriv(h), e);
2651 : }
2652 :
2653 : GEN
2654 154 : RgXn_reverse(GEN f, long e)
2655 : {
2656 154 : pari_sp av = avma, av2;
2657 : ulong mask;
2658 : GEN fi, a, df, W, an;
2659 154 : long v = varn(f), n=1;
2660 154 : if (degpol(f)<1 || !gequal0(gel(f,2)))
2661 0 : pari_err_INV("serreverse",f);
2662 154 : fi = ginv(gel(f,3));
2663 154 : a = deg1pol_shallow(fi,gen_0,v);
2664 154 : if (e <= 2) return gc_GEN(av, a);
2665 133 : W = scalarpol(fi,v);
2666 133 : df = RgX_deriv(f);
2667 133 : mask = quadratic_prec_mask(e);
2668 133 : av2 = avma;
2669 616 : for (;mask>1;)
2670 : {
2671 : GEN u, fa, fr;
2672 483 : long n2 = n, rt;
2673 483 : n<<=1; if (mask & 1) n--;
2674 483 : mask >>= 1;
2675 483 : fr = RgXn_red_shallow(f, n);
2676 483 : rt = brent_kung_optpow(degpol(fr), 4, 3);
2677 483 : an = RgXn_powers(a, rt, n);
2678 483 : if (n>1)
2679 : {
2680 483 : long n4 = (n2+1)>>1;
2681 483 : GEN dfr = RgXn_red_shallow(df, n2);
2682 483 : dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
2683 483 : u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
2684 483 : W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
2685 : }
2686 483 : fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
2687 483 : fa = RgX_shift(fa, -n2);
2688 483 : a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
2689 483 : if (gc_needed(av2,2))
2690 : {
2691 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
2692 0 : (void)gc_all(av2, 2, &a, &W);
2693 : }
2694 : }
2695 133 : return gc_upto(av, a);
2696 : }
2697 :
2698 : GEN
2699 7 : RgXn_sqrt(GEN h, long e)
2700 : {
2701 7 : pari_sp av = avma, av2;
2702 7 : long v = varn(h), n = 1;
2703 7 : GEN f = scalarpol(gen_1, v), df = f;
2704 7 : ulong mask = quadratic_prec_mask(e);
2705 7 : if (degpol(h)<0 || !gequal1(gel(h,2)))
2706 0 : pari_err_SQRTN("RgXn_sqrt",h);
2707 7 : av2 = avma;
2708 : while(1)
2709 7 : {
2710 14 : long n2 = n, m;
2711 : GEN g;
2712 14 : n<<=1; if (mask & 1) n--;
2713 14 : mask >>= 1;
2714 14 : m = n-n2;
2715 14 : g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
2716 14 : f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
2717 14 : if (mask==1) return gc_upto(av, f);
2718 7 : g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
2719 7 : df = RgX_sub(df, RgX_shift_shallow(g, n2));
2720 7 : if (gc_needed(av2,2))
2721 : {
2722 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
2723 0 : (void)gc_all(av2, 2, &f, &df);
2724 : }
2725 : }
2726 : }
2727 :
2728 : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
2729 : GEN
2730 116693 : RgXQ_powu(GEN x, ulong n, GEN T)
2731 : {
2732 116693 : pari_sp av = avma;
2733 :
2734 116693 : if (!n) return pol_1(varn(x));
2735 68451 : if (n == 1) return RgX_copy(x);
2736 19824 : x = gen_powu_i(x, n, (void*)T, &_sqr, &_mul);
2737 19824 : return gc_GEN(av, x);
2738 : }
2739 : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
2740 : GEN
2741 102160 : RgXQ_pow(GEN x, GEN n, GEN T)
2742 : {
2743 : pari_sp av;
2744 102160 : long s = signe(n);
2745 :
2746 102160 : if (!s) return pol_1(varn(x));
2747 102160 : if (is_pm1(n) == 1)
2748 88307 : return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
2749 13853 : av = avma;
2750 13853 : if (s < 0) x = RgXQ_inv(x, T);
2751 13853 : x = gen_pow_i(x, n, (void*)T, &_sqr, &_mul);
2752 13853 : return gc_GEN(av, x);
2753 : }
2754 : static GEN
2755 200571 : _ZXQsqr(void *data, GEN x) { return ZXQ_sqr(x, (GEN)data); }
2756 : static GEN
2757 111154 : _ZXQmul(void *data, GEN x, GEN y) { return ZXQ_mul(x,y, (GEN)data); }
2758 :
2759 : /* generates the list of powers of x of degree 0,1,2,...,l*/
2760 : GEN
2761 11760 : ZXQ_powers(GEN x, long l, GEN T)
2762 : {
2763 11760 : int use_sqr = 2*degpol(x) >= degpol(T);
2764 11760 : return gen_powers(x, l, use_sqr, (void *)T,_ZXQsqr,_ZXQmul,_one);
2765 : }
2766 :
2767 : /* x,T in Z[X], n in N, compute lift(x^n mod T)) */
2768 : GEN
2769 168509 : ZXQ_powu(GEN x, ulong n, GEN T)
2770 : {
2771 168509 : pari_sp av = avma;
2772 :
2773 168509 : if (!n) return pol_1(varn(x));
2774 168509 : if (n == 1) return ZX_copy(x);
2775 113874 : x = gen_powu_i(x, n, (void*)T, &_ZXQsqr, &_ZXQmul);
2776 113875 : return gc_GEN(av, x);
2777 : }
2778 :
2779 : /* generates the list of powers of x of degree 0,1,2,...,l*/
2780 : GEN
2781 8582 : RgXQ_powers(GEN x, long l, GEN T)
2782 : {
2783 8582 : int use_sqr = 2*degpol(x) >= degpol(T);
2784 8582 : return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
2785 : }
2786 :
2787 : GEN
2788 7118 : RgXQV_factorback(GEN L, GEN e, GEN T)
2789 : {
2790 7118 : return gen_factorback(L, e, (void*)T, &_mul, &_pow, &_one);
2791 : }
2792 :
2793 : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
2794 : GEN
2795 9205 : QXQ_powers(GEN a, long n, GEN T)
2796 : {
2797 : GEN den, v;
2798 9205 : if (!isint1(leading_coeff(T))) return RgXQ_powers(a, n, T);
2799 9177 : v = ZXQ_powers(Q_remove_denom(a, &den), n, T);
2800 : /* den*a integral; v[i+1] = (den*a)^i in K */
2801 9177 : if (den)
2802 : { /* restore denominators */
2803 5634 : GEN d = den;
2804 : long i;
2805 5634 : gel(v,2) = a;
2806 27294 : for (i=3; i<=n+1; i++) {
2807 21660 : d = mulii(d,den);
2808 21658 : gel(v,i) = RgX_Rg_div(gel(v,i), d);
2809 : }
2810 : }
2811 9177 : return v;
2812 : }
2813 :
2814 : static GEN
2815 3255 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
2816 : {
2817 3255 : long l, i, m = 0;
2818 : GEN dz, z;
2819 3255 : GEN V = cgetg_copy(v, &l);
2820 10836 : for (i = imin; i < l; i++)
2821 : {
2822 7581 : GEN c = gel(v, i);
2823 7581 : if (typ(c) == t_POL) m = maxss(m, degpol(c));
2824 : }
2825 3255 : z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
2826 3493 : for (i = 1; i < imin; i++) V[i] = v[i];
2827 10836 : for (i = imin; i < l; i++)
2828 : {
2829 7581 : GEN c = gel(v,i);
2830 7581 : if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
2831 7581 : gel(V,i) = c;
2832 : }
2833 3255 : return V;
2834 : }
2835 : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
2836 : GEN
2837 3017 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
2838 3017 : { return do_QXQ_eval(v, 1, a, T); }
2839 :
2840 : GEN
2841 238 : QXY_QXQ_evalx(GEN v, GEN a, GEN T)
2842 238 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
2843 :
2844 : GEN
2845 2940 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
2846 : {
2847 2940 : return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
2848 : }
2849 :
2850 : GEN
2851 5158 : RgXQ_norm(GEN x, GEN T)
2852 : {
2853 : pari_sp av;
2854 5158 : long dx = degpol(x);
2855 : GEN L, y;
2856 5158 : if (degpol(T)==0) return gpowgs(x,0);
2857 5151 : av = avma; y = resultant(T, x);
2858 5151 : L = leading_coeff(T);
2859 5151 : if (gequal1(L) || !signe(x)) return y;
2860 0 : return gc_upto(av, gdiv(y, gpowgs(L, dx)));
2861 : }
2862 :
2863 : GEN
2864 476 : RgXQ_trace(GEN x, GEN T)
2865 : {
2866 476 : pari_sp av = avma;
2867 : GEN dT, z;
2868 : long n;
2869 476 : if (degpol(T)==0) return gmulgs(x,0);
2870 469 : dT = RgX_deriv(T); n = degpol(dT);
2871 469 : z = RgXQ_mul(x, dT, T);
2872 469 : if (degpol(z)<n) return gc_const(av, gen_0);
2873 420 : return gc_upto(av, gdiv(gel(z,2+n), gel(T,3+n)));
2874 : }
2875 :
2876 : GEN
2877 2788358 : RgX_blocks(GEN P, long n, long m)
2878 : {
2879 2788358 : GEN z = cgetg(m+1,t_VEC);
2880 2788358 : long i,j, k=2, l = lg(P);
2881 8558652 : for(i=1; i<=m; i++)
2882 : {
2883 5770294 : GEN zi = cgetg(n+2,t_POL);
2884 5770294 : zi[1] = P[1];
2885 5770294 : gel(z,i) = zi;
2886 17014348 : for(j=2; j<n+2; j++)
2887 11244054 : gel(zi, j) = k==l ? gen_0 : gel(P,k++);
2888 5770294 : zi = RgX_renormalize_lg(zi, n+2);
2889 : }
2890 2788358 : return z;
2891 : }
2892 :
2893 : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
2894 : void
2895 49735224 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
2896 : {
2897 49735224 : long n = degpol(p), v = varn(p), n0, n1, i;
2898 : GEN p0, p1;
2899 :
2900 49737709 : if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
2901 :
2902 49644432 : n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
2903 49644432 : p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
2904 49646880 : p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
2905 146726647 : for (i=0; i<n1; i++)
2906 : {
2907 97079968 : p0[2+i] = p[2+(i<<1)];
2908 97079968 : p1[2+i] = p[3+(i<<1)];
2909 : }
2910 49646679 : if (n1 != n0)
2911 18009848 : p0[2+i] = p[2+(i<<1)];
2912 49646679 : *pe = normalizepol(p0);
2913 49649751 : *po = normalizepol(p1);
2914 : }
2915 :
2916 : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
2917 : GEN
2918 43631 : RgX_splitting(GEN p, long k)
2919 : {
2920 43631 : long n = degpol(p), v = varn(p), m, i, j, l;
2921 : GEN r;
2922 :
2923 43631 : m = n/k;
2924 43631 : r = cgetg(k+1,t_VEC);
2925 234059 : for(i=1; i<=k; i++)
2926 : {
2927 190428 : gel(r,i) = cgetg(m+3, t_POL);
2928 190428 : mael(r,i,1) = evalvarn(v)|evalsigne(1);
2929 : }
2930 571991 : for (j=1, i=0, l=2; i<=n; i++)
2931 : {
2932 528360 : gmael(r,j,l) = gel(p,2+i);
2933 528360 : if (j==k) { j=1; l++; } else j++;
2934 : }
2935 234059 : for(i=1; i<=k; i++)
2936 190428 : gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
2937 43631 : return r;
2938 : }
2939 :
2940 : /*******************************************************************/
2941 : /* */
2942 : /* Kronecker form */
2943 : /* */
2944 : /*******************************************************************/
2945 :
2946 : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
2947 : * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
2948 : * normalized coefficients */
2949 : GEN
2950 186210 : Kronecker_to_mod(GEN z, GEN T)
2951 : {
2952 186210 : long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
2953 186210 : GEN x, t = cgetg(N,t_POL);
2954 186210 : t[1] = T[1];
2955 186210 : lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
2956 186210 : x[1] = z[1];
2957 186210 : T = RgX_copy(T);
2958 1399500 : for (i=2; i<lx+2; i++, z+= N-2)
2959 : {
2960 5761436 : for (j=2; j<N; j++) gel(t,j) = gel(z,j);
2961 1213290 : gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
2962 : }
2963 186210 : N = (l-2) % (N-2) + 2;
2964 472256 : for (j=2; j<N; j++) t[j] = z[j];
2965 186210 : gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
2966 186210 : return normalizepol_lg(x, i+1);
2967 : }
2968 :
2969 : /*******************************************************************/
2970 : /* */
2971 : /* Domain detection */
2972 : /* */
2973 : /*******************************************************************/
2974 :
2975 : static GEN
2976 609812 : RgX_mul_FpX(GEN x, GEN y, GEN p)
2977 : {
2978 609812 : pari_sp av = avma;
2979 : GEN r;
2980 609812 : if (lgefint(p) == 3)
2981 : {
2982 558456 : ulong pp = uel(p, 2);
2983 558456 : r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
2984 : RgX_to_Flx(y, pp), pp));
2985 : }
2986 : else
2987 51356 : r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
2988 609812 : if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
2989 544614 : return gc_upto(av, FpX_to_mod(r, p));
2990 : }
2991 :
2992 : static GEN
2993 616 : zero_FpXQX_mod(GEN pol, GEN p, long v)
2994 : {
2995 616 : GEN r = cgetg(3,t_POL);
2996 616 : r[1] = evalvarn(v);
2997 616 : gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
2998 616 : return r;
2999 : }
3000 :
3001 : static GEN
3002 2079 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
3003 : {
3004 2079 : pari_sp av = avma;
3005 : long dT;
3006 : GEN kx, ky, r;
3007 2079 : GEN T = RgX_to_FpX(pol, p);
3008 2079 : if (signe(T)==0) pari_err_OP("*", x, y);
3009 2072 : dT = degpol(T);
3010 2072 : kx = RgXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
3011 2072 : ky = RgXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
3012 2072 : r = FpX_mul(kx, ky, p);
3013 2072 : if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
3014 1463 : return gc_upto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
3015 : }
3016 :
3017 : static GEN
3018 455393 : RgX_liftred(GEN x, GEN T)
3019 455393 : { return RgXQX_red(liftpol_shallow(x), T); }
3020 :
3021 : static GEN
3022 171133 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
3023 : {
3024 171133 : pari_sp av = avma;
3025 171133 : long dT = degpol(T);
3026 171133 : GEN r = QX_mul(RgXX_to_Kronecker(RgX_liftred(x, T), dT),
3027 : RgXX_to_Kronecker(RgX_liftred(y, T), dT));
3028 171133 : return gc_upto(av, Kronecker_to_mod(r, T));
3029 : }
3030 :
3031 : static GEN
3032 1407 : RgX_sqr_FpX(GEN x, GEN p)
3033 : {
3034 1407 : pari_sp av = avma;
3035 : GEN r;
3036 1407 : if (lgefint(p) == 3)
3037 : {
3038 1203 : ulong pp = uel(p, 2);
3039 1203 : r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
3040 : }
3041 : else
3042 204 : r = FpX_sqr(RgX_to_FpX(x, p), p);
3043 1407 : if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
3044 1274 : return gc_upto(av, FpX_to_mod(r, p));
3045 : }
3046 :
3047 : static GEN
3048 196 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
3049 : {
3050 196 : pari_sp av = avma;
3051 : long dT;
3052 196 : GEN kx, r, T = RgX_to_FpX(pol, p);
3053 196 : if (signe(T)==0) pari_err_OP("*",x,x);
3054 189 : dT = degpol(T);
3055 189 : kx = RgXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
3056 189 : r = FpX_sqr(kx, p);
3057 189 : if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
3058 189 : return gc_upto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
3059 : }
3060 :
3061 : static GEN
3062 13425 : RgX_sqr_QXQX(GEN x, GEN T)
3063 : {
3064 13425 : pari_sp av = avma;
3065 13425 : long dT = degpol(T);
3066 13425 : GEN r = QX_sqr(RgXX_to_Kronecker(RgX_liftred(x, T), dT));
3067 13425 : return gc_upto(av, Kronecker_to_mod(r, T));
3068 : }
3069 :
3070 : static GEN
3071 68320 : RgX_rem_FpX(GEN x, GEN y, GEN p)
3072 : {
3073 68320 : pari_sp av = avma;
3074 : GEN r;
3075 68320 : if (lgefint(p) == 3)
3076 : {
3077 51684 : ulong pp = uel(p, 2);
3078 51684 : r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
3079 : RgX_to_Flx(y, pp), pp));
3080 : }
3081 : else
3082 16636 : r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
3083 68320 : if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
3084 31024 : return gc_upto(av, FpX_to_mod(r, p));
3085 : }
3086 :
3087 : static GEN
3088 49851 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
3089 : {
3090 49851 : pari_sp av = avma;
3091 : GEN r;
3092 49851 : r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
3093 49851 : return gc_GEN(av, QXQX_to_mod_shallow(r, T));
3094 : }
3095 : static GEN
3096 70 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
3097 : {
3098 70 : pari_sp av = avma;
3099 : GEN r;
3100 70 : GEN T = RgX_to_FpX(pol, p);
3101 70 : if (signe(T) == 0) pari_err_OP("%", x, y);
3102 63 : if (lgefint(p) == 3)
3103 : {
3104 55 : ulong pp = uel(p, 2);
3105 55 : GEN Tp = ZX_to_Flx(T, pp);
3106 55 : r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
3107 : RgX_to_FlxqX(y, Tp, pp), Tp, pp));
3108 : }
3109 : else
3110 8 : r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
3111 63 : if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
3112 56 : return gc_upto(av, FpXQX_to_mod(r, T, p));
3113 : }
3114 :
3115 : static GEN
3116 121141573 : RgX_mul_fast(GEN x, GEN y)
3117 : {
3118 : GEN p, pol;
3119 : long pa;
3120 121141573 : long t = RgX_type2(x,y, &p,&pol,&pa);
3121 121141882 : switch(t)
3122 : {
3123 67198360 : case t_INT: return ZX_mul(x,y);
3124 3357734 : case t_FRAC: return QX_mul(x,y);
3125 105127 : case t_FFELT: return FFX_mul(x, y, pol);
3126 609812 : case t_INTMOD: return RgX_mul_FpX(x, y, p);
3127 171133 : case RgX_type_code(t_POLMOD, t_INT):
3128 : case RgX_type_code(t_POLMOD, t_FRAC):
3129 171133 : return RgX_mul_QXQX(x, y, pol);
3130 2042 : case RgX_type_code(t_POLMOD, t_INTMOD):
3131 2042 : return RgX_mul_FpXQX(x, y, pol, p);
3132 49697674 : default: return NULL;
3133 : }
3134 : }
3135 : static GEN
3136 1384661 : RgX_sqr_fast(GEN x)
3137 : {
3138 : GEN p, pol;
3139 : long pa;
3140 1384661 : long t = RgX_type(x,&p,&pol,&pa);
3141 1384660 : switch(t)
3142 : {
3143 1245896 : case t_INT: return ZX_sqr(x);
3144 83279 : case t_FRAC: return QX_sqr(x);
3145 2499 : case t_FFELT: return FFX_sqr(x, pol);
3146 1407 : case t_INTMOD: return RgX_sqr_FpX(x, p);
3147 13425 : case RgX_type_code(t_POLMOD, t_INT):
3148 : case RgX_type_code(t_POLMOD, t_FRAC):
3149 13425 : return RgX_sqr_QXQX(x, pol);
3150 193 : case RgX_type_code(t_POLMOD, t_INTMOD):
3151 193 : return RgX_sqr_FpXQX(x, pol, p);
3152 37961 : default: return NULL;
3153 : }
3154 : }
3155 :
3156 : static GEN
3157 14704258 : RgX_rem_fast(GEN x, GEN y)
3158 : {
3159 : GEN p, pol;
3160 : long pa;
3161 14704258 : long t = RgX_type2(x,y, &p,&pol,&pa);
3162 14704418 : switch(t)
3163 : {
3164 3485981 : case t_INT: return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
3165 1835848 : case t_FRAC: return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
3166 84 : case t_FFELT: return FFX_rem(x, y, pol);
3167 68320 : case t_INTMOD: return RgX_rem_FpX(x, y, p);
3168 49851 : case RgX_type_code(t_POLMOD, t_INT):
3169 : case RgX_type_code(t_POLMOD, t_FRAC):
3170 49851 : return RgX_rem_QXQX(x, y, pol);
3171 65 : case RgX_type_code(t_POLMOD, t_INTMOD):
3172 65 : return RgX_rem_FpXQX(x, y, pol, p);
3173 9264269 : default: return NULL;
3174 : }
3175 : }
3176 :
3177 : GEN
3178 107408855 : RgX_mul(GEN x, GEN y)
3179 : {
3180 107408855 : GEN z = RgX_mul_fast(x,y);
3181 107409049 : if (!z) z = RgX_mul_i(x,y);
3182 107408601 : return z;
3183 : }
3184 :
3185 : GEN
3186 1372509 : RgX_sqr(GEN x)
3187 : {
3188 1372509 : GEN z = RgX_sqr_fast(x);
3189 1372505 : if (!z) z = RgX_sqr_i(x);
3190 1372505 : return z;
3191 : }
3192 :
3193 : GEN
3194 14704265 : RgX_rem(GEN x, GEN y)
3195 : {
3196 14704265 : GEN z = RgX_rem_fast(x, y);
3197 14704403 : if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
3198 14704298 : return z;
3199 : }
3200 :
3201 : static GEN
3202 0 : _RgX_mul(void* E, GEN x, GEN y)
3203 0 : { (void) E; return RgX_mul(x, y); }
3204 :
3205 : GEN
3206 0 : RgXV_prod(GEN V)
3207 0 : { return gen_product(V, NULL, &_RgX_mul); }
3208 :
3209 : GEN
3210 11061653 : RgXn_mul(GEN f, GEN g, long n)
3211 : {
3212 11061653 : pari_sp av = avma;
3213 11061653 : GEN h = RgX_mul_fast(f,g);
3214 11061655 : if (!h) return RgXn_mul2(f,g,n);
3215 1783461 : if (degpol(h) < n) return h;
3216 382976 : return gc_GEN(av, RgXn_red_shallow(h, n));
3217 : }
3218 :
3219 : GEN
3220 12152 : RgXn_sqr(GEN f, long n)
3221 : {
3222 12152 : pari_sp av = avma;
3223 12152 : GEN g = RgX_sqr_fast(f);
3224 12152 : if (!g) return RgXn_sqr2(f,n);
3225 11837 : if (degpol(g) < n) return g;
3226 10640 : return gc_GEN(av, RgXn_red_shallow(g, n));
3227 : }
3228 :
3229 : /* (f*g) \/ x^n */
3230 : GEN
3231 2671064 : RgX_mulhigh_i(GEN f, GEN g, long n)
3232 : {
3233 2671064 : GEN h = RgX_mul_fast(f,g);
3234 2671065 : return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
3235 : }
3236 :
3237 : /* (f*g) \/ x^n */
3238 : GEN
3239 0 : RgX_sqrhigh_i(GEN f, long n)
3240 : {
3241 0 : GEN h = RgX_sqr_fast(f);
3242 0 : return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
3243 : }
|