Debian Codegraph @

texmf-dist/tex/generic/polexpr/polexprcore.tex

raw
1 %% filename: polexprcore.tex
2 %% Part of the polexpr package (0.8.7a, 2022/05/19)
3 %%
4 %% Core routines for infix operators +, -, *, //, /:, ^, ** and functions
5 %%
6 %% Memo: the atoms representing polynomials inside \xintexpr are
7 %% - for constants: a numeric value (indistinguishable. from scalars)
8 %% - for degree at least 1: P<degree>.{c0}{c1}....{cN} with N = degree
9 %% Auxiliaries
10 \long\def\POL_Pfork #1P#2#3\krof{#2}%
11 \long\def\POL_PPfork #1PP#2#3\krof{#2}%
12 \long\def\POL_zeroPfork #10P#2#3\krof{#2}%
13 \long\def\POL_secondofthree#1#2#3{#2}%
14 % \long\def\POL_Apply:x #1#2%
15 % {%
16 % \POL_apply:x_loop {#1}#2%
17 % \xint_Bye\xint_Bye\xint_Bye\xint_Bye
18 % \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
19 % }%
20 \long\def\POL_bBye#1\xint_Bye{}%
21 \long\def\POL_apply:x_loop #1#2#3#4#5#6#7#8#9%
22 {%
23 \POL_bBye #2\xint_Bye{#1{#2}}%
24 \POL_bBye #3\xint_Bye{#1{#3}}%
25 \POL_bBye #4\xint_Bye{#1{#4}}%
26 \POL_bBye #5\xint_Bye{#1{#5}}%
27 \POL_bBye #6\xint_Bye{#1{#6}}%
28 \POL_bBye #7\xint_Bye{#1{#7}}%
29 \POL_bBye #8\xint_Bye{#1{#8}}%
30 \POL_bBye #9\xint_Bye{#1{#9}}%
31 \POL_apply:x_loop {#1}%
32 }%
33 \long\def\POL_apply:x_iloop #1#2#3#4#5#6#7#8#9%
34 {%
35 \POL_bBye #2\xint_Bye{#10{#2}}%
36 \POL_bBye #3\xint_Bye{#11{#3}}%
37 \POL_bBye #4\xint_Bye{#12{#4}}%
38 \POL_bBye #5\xint_Bye{#13{#5}}%
39 \POL_bBye #6\xint_Bye{#14{#6}}%
40 \POL_bBye #7\xint_Bye{#15{#7}}%
41 \POL_bBye #8\xint_Bye{#16{#8}}%
42 \POL_bBye #9\xint_Bye{#17{#9}}%
43 \POL_apply:x_iloop_a #1%
44 }%
45 \def\POL_apply:x_iloop_a#1#2.%
46 {%
47 \expandafter\POL_apply:x_iloop
48 % arrgggh, 0.8 had +#1 in place of +#2... again I did not test enough
49 \expandafter{\expandafter#1\the\numexpr\xint_c_viii+#2.}%
50 }%
51 %%
52 %% ADDITION
53 %%
54 \def\xintPolAdd #1%
55 {%
56 \expanded\expandafter\POL_add_in\romannumeral`&&@#1\xint:
57 }%
58 \def\POL_add_in #1\xint:#2%
59 {%
60 {%
61 \expandafter\POL_add_fork
62 % Fragile but this macro is not public anyhow and won't get arbitrary input
63 % At odds with systematic \xint: style further down
64 \romannumeral`&&@#2\xint_bye\xint_bye\xint_bye\xint_bye\empty
65 #1\xint_bye\xint_bye\xint_bye\xint_bye\empty
66 \empty
67 }%
68 }%
69 % Careful that first means "first here" i.e. the original second argument,
70 % and vice versa
71 \def\POL_add_fork #1#2\empty#3%
72 {%
73 \POL_PPfork
74 #1#3{\POL_add_a}%
75 #1P{\POL_add_second_is_scalar}%
76 #3P{\POL_add_first_is_scalar}%
77 PP{\POL_add_both_are_scalar}%
78 \krof #1#2\empty#3%
79 }%
80 \def\POL_add_first_is_scalar #1\xint_bye#2\empty#3.#4%
81 {%
82 #3.{\xintAdd{#1}{#4}}%
83 }%
84 \def\POL_add_second_is_scalar #1.#2#3\empty#4\xint_bye#5\empty\empty
85 {%
86 #1.{\xintAdd{#2}{#4}}#3%
87 }%
88 \def\POL_add_both_are_scalar #1\xint_bye#2\empty#3\xint_bye#4\empty\empty
89 {%
90 \xintAdd{#1}{#3}%
91 }%
92 \def\POL_add_a P#1.#2#3#4#5\empty P#6.#7#8#9%
93 {%
94 \expandafter\POL_add_b
95 \expanded\bgroup\unexpanded{#1.#6.}%
96 \xint_bye #2\POL_add_Eb\xint_bye
97 \xint_bye #7\POL_add_Fb\xint_bye {\xintAdd{#2}{#7}}%
98 \xint_bye #3\POL_add_Ec\xint_bye
99 \xint_bye #8\POL_add_Fc\xint_bye {\xintAdd{#3}{#8}}%
100 \xint_bye #4\POL_add_Ed\xint_bye
101 \xint_bye #9\POL_add_Fd\xint_bye {\xintAdd{#4}{#9}}%
102 \POL_add_A #5\empty
103 }%
104 \def\POL_add_b #1.#2.%
105 {%
106 \ifnum#1=#2 \expandafter\POL_add_c
107 \else
108 \ifnum#1>#2 P#1.\else P#2.\fi
109 \fi
110 }%
111 % No brace stripping possible, because constant polynomials are really
112 % represented by scalars in all those internal contexts, so real
113 % polynomials have at least two coefficients
114 \def\POL_add_c #1\empty
115 {%
116 \expandafter\POL_add_d
117 \romannumeral0\XINT_revwbr_loop {}%
118 #1\xint:\xint:\xint:\xint:%
119 \xint:\xint:\xint:\xint:\xint_bye
120 \xint_bye
121 }%
122 % Attention, reused in various other locations. It is all f-expandable.
123 \def\POL_add_d #1%
124 {%
125 % abuse of \XINT_Sgn internals compatible to #1 being \xint_bye
126 \if0\XINT_Sgn#1\xint:
127 \xint_dothis\POL_add_d
128 \fi
129 \xint_orthat{\POL_add_e {#1}}%
130 }%
131 \def\POL_add_e #1%
132 {%
133 \xint_bye#1\POL_add_e_zero\xint_bye \POL_add_f\empty{#1}%
134 }%
135 \def\POL_add_e_zero\xint_bye\POL_add_f\empty #1{0/1[0]}%
136 % #1 starts with \empty to avoid brace stripping.
137 \def\POL_add_f #1\xint_bye
138 {%
139 \expandafter\POL_add_g
140 \the\numexpr
141 \xintLength{#1}-\xint_c_ii\expandafter.%
142 \romannumeral0\expandafter
143 \XINT_revwbr_loop\expandafter {\expandafter}%
144 #1\xint:\xint:\xint:\xint:%
145 \xint:\xint:\xint:\xint:\xint_bye
146 }%
147 \def\POL_add_g #1.%
148 {%
149 \ifnum#1=\xint_c_\expandafter\POL_add_h\fi
150 P#1.%
151 }%
152 \def\POL_add_h P0.#1{#1}%
153 % Attention reused in \POL_mul_d and \POL_quorem_c
154 \def\POL_add_A #1#2#3#4#5\empty#6#7#8#9%
155 {%
156 \xint_bye #1\POL_add_Ea\xint_bye
157 \xint_bye #6\POL_add_Fa\xint_bye {\xintAdd{#1}{#6}}%
158 \xint_bye #2\POL_add_Eb\xint_bye
159 \xint_bye #7\POL_add_Fb\xint_bye {\xintAdd{#2}{#7}}%
160 \xint_bye #3\POL_add_Ec\xint_bye
161 \xint_bye #8\POL_add_Fc\xint_bye {\xintAdd{#3}{#8}}%
162 \xint_bye #4\POL_add_Ed\xint_bye
163 \xint_bye #9\POL_add_Fd\xint_bye {\xintAdd{#4}{#9}}%
164 \POL_add_A #5\empty
165 }%
166 \def\POL_add_Ea\xint_bye
167 \xint_bye #1\POL_add_Fa\xint_bye #2\xint_bye\xint_bye
168 \POL_add_Eb\xint_bye\xint_bye#3\POL_add_Fb\xint_bye #4\xint_bye\xint_bye
169 \POL_add_Ec\xint_bye\xint_bye#5\POL_add_Fc\xint_bye #6\xint_bye\xint_bye
170 \POL_add_Ed\xint_bye\xint_bye#7\POL_add_Fd\xint_bye #8%
171 \POL_add_A#9\empty
172 {%
173 \xint_bye #1\POL_add_G\xint_bye{#1}%
174 \xint_bye #3\POL_add_G\xint_bye{#3}%
175 \xint_bye #5\POL_add_G\xint_bye{#5}%
176 \xint_bye #7\POL_add_G\xint_bye{#7}%
177 \iffalse{\fi}%
178 }%
179 \def\POL_add_G#1\empty{\iffalse{\fi}}%
180 \def\POL_add_Fa\xint_bye #1%
181 \xint_bye #2\POL_add_Eb \xint_bye
182 \xint_bye\xint_bye\POL_add_Fb\xint_bye #3%
183 \xint_bye #4\POL_add_Ec \xint_bye
184 \xint_bye\xint_bye\POL_add_Fc\xint_bye #5%
185 \xint_bye #6\POL_add_Ed #7\POL_add_A
186 #8\empty#9\empty
187 {%
188 \expandafter\xint_bye\POL_secondofthree #1%
189 \POL_add_G\xint_bye{\POL_secondofthree#1}%
190 \xint_bye #2\POL_add_G\xint_bye{#2}%
191 \xint_bye #4\POL_add_G\xint_bye{#4}%
192 \xint_bye #6\POL_add_G\xint_bye{#6}%
193 \iffalse{\fi}#8\empty%
194 }%
195 \def\POL_add_Eb\xint_bye
196 \xint_bye #1\POL_add_Fb\xint_bye #2\xint_bye\xint_bye
197 \POL_add_Ec\xint_bye\xint_bye#3\POL_add_Fc\xint_bye #4\xint_bye\xint_bye
198 \POL_add_Ed\xint_bye\xint_bye#5\POL_add_Fd\xint_bye #6%
199 \POL_add_A#7\empty
200 {%
201 \xint_bye #1\POL_add_G\xint_bye{#1}%
202 \xint_bye #3\POL_add_G\xint_bye{#3}%
203 \xint_bye #5\POL_add_G\xint_bye{#5}%
204 \iffalse{\fi}%
205 }%
206 \def\POL_add_Fb\xint_bye #1%
207 \xint_bye #2\POL_add_Ec \xint_bye
208 \xint_bye\xint_bye\POL_add_Fc\xint_bye #3%
209 \xint_bye #4\POL_add_Ed #5\POL_add_A
210 #6\empty#7\empty
211 {%
212 \expandafter\xint_bye\POL_secondofthree #1%
213 \POL_add_G\xint_bye{\POL_secondofthree#1}%
214 \xint_bye #2\POL_add_G\xint_bye{#2}%
215 \xint_bye #4\POL_add_G\xint_bye{#4}%
216 \iffalse{\fi}#6\empty
217 }%
218 \def\POL_add_Ec\xint_bye
219 \xint_bye #1\POL_add_Fc\xint_bye #2\xint_bye\xint_bye
220 \POL_add_Ed\xint_bye\xint_bye#3\POL_add_Fd\xint_bye #4%
221 \POL_add_A#5\empty
222 {%
223 \xint_bye #1\POL_add_G\xint_bye{#1}%
224 \xint_bye #3\POL_add_G\xint_bye{#3}%
225 \iffalse{\fi}%
226 }%
227 \def\POL_add_Fc\xint_bye #1\xint_bye #2\POL_add_Ed #3\POL_add_A
228 #4\empty#5\empty
229 {%
230 \expandafter\xint_bye\POL_secondofthree #1%
231 \POL_add_G\xint_bye{\POL_secondofthree#1}%
232 \xint_bye #2\POL_add_G\xint_bye{#2}%
233 \iffalse{\fi}#4\empty
234 }%
235 \def\POL_add_Ed\xint_bye\xint_bye#1\POL_add_Fd\xint_bye#2\POL_add_A#3\empty
236 {%
237 \xint_bye #1\POL_add_G\xint_bye{#1}%
238 \iffalse{\fi}%
239 }%
240 \def\POL_add_Fd\xint_bye#1\POL_add_A #2\empty#3\empty
241 {%
242 \expandafter\xint_bye \POL_secondofthree #1%
243 \POL_add_G\xint_bye{\POL_secondofthree#1}%
244 \iffalse{\fi}#2\empty
245 }%
246 %%
247 %% OPPOSITE
248 %%
249 \def\xintPolOpp #1%
250 {%
251 \expanded{%
252 \expandafter\POL_opp_fork\romannumeral`&&@#1%
253 \xint_Bye\xint_Bye\xint_Bye\xint_Bye
254 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
255 }%
256 }%
257 \def\POL_opp_fork #1%
258 {%
259 \if P#1\xint_dothis\POL_opp_a\fi
260 \xint_orthat\POL_opp_scalar #1%
261 }%
262 \def\POL_opp_scalar #1\xint_Bye#2\xint_bye
263 {%
264 \XINT_Opp #1%
265 }%
266 \def\POL_opp_a #1.%
267 {%
268 #1.\POL_apply:x_loop{\XINT_Opp}%
269 }%
270 %%
271 %% SUBTRACTION
272 %%
273 \def\xintPolSub #1%
274 {%
275 \expanded\expandafter\POL@sub\romannumeral`&&@#1\xint:
276 }%
277 \def\POL@sub #1\xint:#2%
278 {%
279 {%
280 \expandafter
281 \POL_add_fork\expanded{%
282 \expandafter\POL_opp_fork \romannumeral`&&@#2%
283 \xint_Bye\xint_Bye\xint_Bye\xint_Bye
284 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
285 }%
286 \xint_bye\xint_bye\xint_bye\xint_bye\empty
287 #1\xint_bye\xint_bye\xint_bye\xint_bye\empty
288 \empty
289 }%
290 }%
291 %%
292 %% MULTIPLICATION
293 %%
294 \def\xintPolSqr #1%
295 {%
296 \expanded\expandafter\POL_sqr_in\romannumeral`&&@#1\xint:
297 }%
298 \def\POL_sqr_in #1\xint:
299 {%
300 {%
301 \expandafter\POL_mul_fork
302 #1\xint_bye
303 #1\xint_Bye\xint_Bye\xint_Bye\xint_Bye
304 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
305 }%
306 }%
307 %
308 \def\xintPolMul #1%
309 {%
310 \expanded\expandafter\POL_mul_in\romannumeral`&&@#1\xint:
311 }%
312 \def\POL_mul_in #1\xint:#2%
313 {%
314 {%
315 \expandafter\POL_mul_fork
316 \romannumeral`&&@#2\xint_bye
317 #1\xint_Bye\xint_Bye\xint_Bye\xint_Bye
318 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
319 }%
320 }%
321 \def\POL_mul_fork #1#2\xint_bye#3%
322 {%
323 \POL_PPfork
324 #1#3{\POL_mul_a}%
325 #1P{\POL_mul_second_is_scalar}%
326 #3P{\POL_mul_first_is_scalar}%
327 PP{\POL_mul_both_are_scalar}%
328 \krof #1#2\xint_bye#3%
329 }%
330 \def\POL_mul_both_are_scalar #1\xint_bye#2\xint_Bye#3\xint_bye
331 {%
332 \xintMul{#1}{#2}%
333 }%
334 \def\POL_mul_second_is_scalar #1\xint_bye#2\xint_Bye
335 {%
336 \POL_mul_first_is_scalar #2\xint_bye#1\xint_Bye
337 }%
338 \def\POL_mul_first_is_scalar #1%
339 {%
340 \xint_gob_til_zero#1\POL_mul_zero0\POL_mul_scalar #1%
341 }%
342 \def\POL_mul_zero0\POL_mul_scalar #1\xint_bye#2\xint_bye{0/1[0]}%
343 \def\POL_mul_scalar #1\xint_bye P#2.%
344 {%
345 P#2.\POL_apply:x_loop{\xintMul{#1}}%
346 }%
347 \def\POL_mul_a P#1.#2#3P#4.#5\xint_bye
348 {%
349 P\the\numexpr#1+#4.%
350 \expandafter\POL_mul_b
351 \expanded{\POL_apply:x_loop{\xintMul{#2}}#5\xint_bye}%
352 \xint:
353 #3\empty#5\xint_bye
354 }%
355 \def\POL_mul_b #1{{#1}\POL_mul_c\empty}%
356 \def\POL_mul_c #1\xint:#2%
357 {%
358 \xint_bye#2\POL_mul_E\xint_bye
359 \expandafter\POL_mul_d\expandafter{#1}{#2}%
360 }%
361 \def\POL_mul_d #1#2#3\empty#4\xint_bye
362 {%
363 \expandafter\POL_mul_b
364 \expanded\bgroup
365 \expandafter\POL_add_A
366 \expanded{\POL_apply:x_loop{\xintMul{#2}}#4\xint_bye}%
367 \xint_bye\xint_bye\xint_bye\xint_bye\empty
368 #1\xint_bye\xint_bye\xint_bye\xint_bye\empty
369 \xint:
370 #3\empty#4\xint_bye
371 }%
372 \def\POL_mul_E\xint_bye
373 \expandafter\POL_mul_d\expandafter#1#2\xint_bye
374 % This #1 starts with \empty
375 {%
376 #1%
377 }%
378 %%
379 %% POWERS
380 %%
381 \def\xintPolPow #1%
382 {%
383 \expanded\expandafter\POL_pow_in\romannumeral`&&@#1\xint:
384 }%
385 \def\POL_pow_in #1\xint:#2%
386 {%
387 {%
388 \expandafter\POL_pow_fork\romannumeral0\xintraw{#2}.#1\empty
389 }%
390 }%
391 \def\POL_pow_fork #1%#2.%
392 {%
393 \xint_UDzerominusfork
394 #1-\POL_pow_zero
395 0#1\POL_pow_neg
396 0-\POL_pow_pos
397 \krof
398 #1%#2.%
399 }%
400 \def\POL_pow_zero #1\empty{1/1[0]}%
401 \def\POL_pow_neg #1.#2%
402 {%
403 \POL_Pfork #2{\POL_pow_neg_pol}P{\POL_pow_scalar}\krof #1.#2%
404 }%
405 \def\POL_pow_pos #1.#2%
406 {%
407 \POL_Pfork #2{\POL_pow_aa}P{\POL_pow_scalar}\krof #1.#2%
408 }%
409 \def\POL_pow_scalar #1.#2\empty
410 {%
411 \xintPow{#2}{#1}%
412 }%
413 \def\POL_pow_neg_pol #1.#2\empty
414 {%
415 \XINT_signalcondition{InvalidOperation}%
416 {Not supported: polynomial to negative power #1}{}{1/1[0]}%
417 }%
418 \def\POL_pow_aa #1.{\expandafter\POL_pow_a\the\numexpr\xintNum{#1}.}%
419 \def\POL_pow_a #1.%
420 {%
421 % trailing \empty will disappear in expanded context (old comment)
422 \ifnum#1=\xint_c_i\xint_afterfi\xint_gob_til_dot\fi
423 \expandafter\POL_pow_b \the\numexpr#1-\xint_c_i.%
424 }%
425 \def\POL_pow_b #1.%
426 {%
427 \ifodd #1 \xint_dothis{\expandafter\POL_pow_even}\fi
428 \xint_orthat{\expandafter\POL_pow_odd}\the\numexpr#1/\xint_c_ii.%
429 }%
430 \def\POL_pow_even #1.#2\empty
431 {%
432 \expandafter\POL_pow_a
433 \expanded{\unexpanded{#1.}%
434 \POL_mul_a#2\xint_bye
435 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
436 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
437 }\empty
438 }%
439 \def\POL_pow_odd #1.#2\empty
440 {%
441 \expanded
442 {\unexpanded{\POL_mul_a #2\xint_bye}%
443 \expandafter\POL_pow_a
444 \expanded{\unexpanded{#1.}%
445 \POL_mul_a#2\xint_bye
446 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
447 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
448 }%
449 \empty
450 }%
451 \xint_Bye\xint_Bye\xint_Bye\xint_Bye
452 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
453 }%
454 %%
455 %% DIVISION
456 %%
457 %
458 % / is deprecated for polynomial Euclidean division
459 %
460 \def\xintPolQuo #1%
461 {%
462 \romannumeral0\expandafter\xint_stop_atfirstoftwo
463 \expanded\expandafter\POL_quorem_in\romannumeral`&&@#1\xint:
464 }%
465 % there is no operator, for lack of obvious best notation
466 \def\xintPolRem #1%
467 {%
468 \romannumeral0\expandafter\xint_stop_atsecondoftwo
469 \expanded\expandafter\POL_quorem_in\romannumeral`&&@#1\xint:
470 }%
471 % //
472 \def\xintPolDivModQ #1%
473 {%
474 \romannumeral0\expandafter\xint_stop_atfirstoftwo
475 \expanded\expandafter\POL_divmod_in\romannumeral`&&@#1\xint:
476 }%
477 % /:
478 \def\xintPolDivModR #1%
479 {%
480 \romannumeral0\expandafter\xint_stop_atsecondoftwo
481 \expanded\expandafter\POL_divmod_in\romannumeral`&&@#1\xint:
482 }%
483 % "divmod" will apply coefficient per coefficient when divisor is scalar
484 % I have found it convenient to treat constant polynomials
485 % as really being scalars. But I need perhaps to think more about it.
486 \def\xintPolDivMod #1%
487 {%
488 \expanded\expandafter\POL_divmod_in\romannumeral`&&@#1\xint:
489 }%
490 % the euclidean division
491 \def\xintPolQuoRem #1%
492 {%
493 \expanded\expandafter\POL_quorem_in\romannumeral`&&@#1\xint:
494 }%
495 \def\POL_quorem_in #1\xint:#2%
496 {%
497 {%
498 \expandafter\POL_quorem_fork
499 \romannumeral`&&@#2\xint_bye#1\xint_bye
500 }%
501 }%
502 % the overloading of divmod which does euclidean division if divisor is not a scalar
503 \def\POL_divmod_in #1\xint:#2%
504 {%
505 {%
506 \expandafter\POL_divmod_fork
507 \romannumeral`&&@#2\xint_bye#1\xint_bye
508 }%
509 }%
510 % "first" and "second" refer to the actual positions, permuted compared
511 % to original arguments
512 \def\POL_quorem_fork #1#2\xint_bye#3%
513 {%
514 \POL_PPfork
515 #1#3{\POL_quorem_a}% both polynomials -> {eucl. quotient}{remainder}
516 #1P{\POL_quorem_second_is_scalar}% -> {zero quotient}{scalar}
517 #3P{\POL_quorem_first_is_scalar}% -> {polynomial/scalar}{zero}
518 PP{\POL_quorem_both_are_scalar}% -> {scalar/scalar}{zero}
519 \krof #1#2\xint_bye#3%
520 }%
521 \def\POL_quorem_first_is_scalar #1\xint_bye#2\xint_bye
522 {%
523 {\expandafter\POL_quorem_first_is_scalar_i\expandafter
524 {\romannumeral0\xintinv{#1}}%
525 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
526 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye}{0/1[0]}%
527 }%
528 \def\POL_quorem_first_is_scalar_i #1#2.%
529 {%
530 #2.\POL_apply:x_loop{\xintMul{#1}}%
531 }%
532 % #2 was initial first argument and is scalar
533 \def\POL_quorem_second_is_scalar #1\xint_bye#2\xint_bye
534 {%
535 {0/1[0]}{#2}%
536 }%
537 \def\POL_quorem_both_are_scalar #1\xint_bye#2\xint_bye
538 {%
539 {\xintDiv{#2}{#1}}{0/1[0]}%
540 }%
541 % attention that "first", "second" refer to the actual arguments positions
542 \def\POL_divmod_fork #1#2\xint_bye#3%
543 {%
544 \POL_PPfork
545 #1#3{\POL_quorem_a}% both polynomials -> {eucl. quotient}{remainder}
546 #1P{\POL_quorem_second_is_scalar}% -> {zero quotient}{scalar}
547 #3P{\POL_divmod_first_is_scalar}% -> {per coeff//scalar}{per coeff/:scalar}
548 PP{\POL_divmod_both_are_scalar}% -> {s1//s2}{s1/:s2}
549 \krof #1#2\xint_bye#3%
550 }%
551 \def\POL_divmod_both_are_scalar #1\xint_bye#2\xint_bye
552 {%
553 \xintDivMod{#2}{#1}%
554 }%
555 \def\POL_divmod_first_is_scalar #1\xint_bye #2.#3\xint_bye
556 {%
557 \expandafter\POL_divmod_first_is_scalar_a
558 \expanded{\unexpanded{{#1}}\expandafter}%
559 \romannumeral0\XINT_revwbr_loop {}%
560 #3\xint:\xint:\xint:\xint:%
561 \xint:\xint:\xint:\xint:\xint_bye
562 \xint_Bye\xint_Bye\xint_Bye\xint_Bye
563 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
564 \iffalse{\fi}%
565 \xint:
566 }%
567 \long\def\POL_exchange_args#1#2#3{#1{#3}{#2}}%
568 \def\POL_divmod_first_is_scalar_a #1%
569 {%
570 \expandafter\POL_divmod_first_is_scalar_b
571 \expanded\bgroup
572 \POL_apply:x_loop{\POL_exchange_args\xintDivMod{#1}}%
573 }%
574 % attention re-use of \POL_add_d
575 \def\POL_divmod_first_is_scalar_b #1\xint:
576 {%
577 {\expandafter\POL_add_d\expanded{%
578 \POL_apply:x_loop{\expandafter\xint_firstoftwo\xint_firstofone}%
579 #1\xint_Bye\xint_Bye\xint_Bye\xint_Bye
580 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye}\xint_bye}%
581 {\expandafter\POL_add_d\expanded{%
582 \POL_apply:x_loop{\expandafter\xint_secondoftwo\xint_firstofone}%
583 #1\xint_Bye\xint_Bye\xint_Bye\xint_Bye
584 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye}\xint_bye}%
585 }%
586 \def\POL_quorem_a P#1.#2\xint_bye P#3.#4\xint_bye
587 {%
588 \ifnum#1>#3 \xint_dothis{\POL_quorem_easy #3.}\fi
589 \xint_orthat
590 {\expandafter\POL_quorem_EQuo
591 \expanded\bgroup
592 \expandafter\POL_quorem_b\the\numexpr#3-#1\expandafter.%
593 \expanded\bgroup
594 \xintRevWithBraces
595 }%
596 {#2}%
597 \noexpand\xint_Bye
598 \xint:
599 \expandafter\POL_placemark_loop
600 \the\numexpr#1-\xint_c_vii\expandafter.%
601 \romannumeral0\xintrevwithbraces{#4}%
602 % This added {1} is related to termination clean-up (a bit annoying) process
603 {1}%
604 \the\numexpr#3-#1.%
605 \iffalse{\fi}%
606 }%
607 \def\POL_quorem_easy #1.#2\xintrevwithbraces#3#4.#5#6%
608 {%
609 {0/1[0]}{P#1.#3}%
610 }%
611 \def\POL_placemark_loop #1#2.%
612 {%
613 \xint_gob_til_minus#1\POL_placemark_loop_end-%
614 \expandafter\POL_placemark_step\the\numexpr#1#2-\xint_c_viii.%
615 }%
616 \def\POL_placemark_step #1.#2#3#4#5#6#7#8#9%
617 {%
618 {#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}\POL_placemark_loop#1.%
619 }%
620 \def\POL_placemark_loop_end-%
621 \expandafter\POL_placemark_step\the\numexpr-#1-\xint_c_viii.%
622 {%
623 \csname POL_placemark_end#1\endcsname
624 }%
625 \expandafter\def\csname POL_placemark_end1\endcsname
626 #1#2#3#4#5#6#7{{#1}{#2}{#3}{#4}{#5}{#6}{#7}\noexpand\xint_bye\xint:}%
627 \expandafter\def\csname POL_placemark_end2\endcsname
628 #1#2#3#4#5#6{{#1}{#2}{#3}{#4}{#5}{#6}\noexpand\xint_bye\xint:}%
629 \expandafter\def\csname POL_placemark_end3\endcsname
630 #1#2#3#4#5{{#1}{#2}{#3}{#4}{#5}\noexpand\xint_bye\xint:}%
631 \expandafter\def\csname POL_placemark_end4\endcsname
632 #1#2#3#4{{#1}{#2}{#3}{#4}\noexpand\xint_bye\xint:}%
633 \expandafter\def\csname POL_placemark_end5\endcsname
634 #1#2#3{{#1}{#2}{#3}\noexpand\xint_bye\xint:}%
635 \expandafter\def\csname POL_placemark_end6\endcsname
636 #1#2{{#1}{#2}\noexpand\xint_bye\xint:}%
637 \expandafter\def\csname POL_placemark_end7\endcsname
638 #1{{#1}\noexpand\xint_bye\xint:}%
639 \expandafter\def\csname POL_placemark_end8\endcsname
640 {\noexpand\xint_bye\xint:}%
641 \def\POL_quorem_b #1.#2#3\xint:#4#5\xint:#6%
642 {%
643 % \xintDiv FG computes F/G
644 \expandafter\POL_quorem_c\romannumeral0\xintdiv{\XINT_Opp#4}{#2}.%
645 #1.{#2}#3\xint:
646 % there is already \xint_Bye at ends of #3
647 #3\xint_Bye\xint_Bye\xint_Bye
648 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
649 % this terminates the \expanded from \POL_apply:x_loop
650 \iffalse{\fi}%
651 \xint_bye\xint_bye\xint_bye\xint_bye\empty
652 #5\xint_bye\xint_bye\xint_bye\empty
653 % a \iffalse{\fi} will get inserted by \POL_add_A here
654 {#6}\xint_bye\xint:
655 }%
656 \def\POL_quorem_c #1.#2.#3\xint:%
657 {%
658 {\XINT_Opp#1}%
659 \expandafter\POL_quorem_d\the\numexpr#2-\xint_c_i\expandafter.%
660 \expanded\bgroup
661 \unexpanded{#3}\xint:
662 \expandafter\POL_add_A
663 \expanded\bgroup
664 \POL_apply:x_loop{\xintMul{#1}}%
665 }%
666 \def\POL_quorem_d #1#2.%
667 {%
668 \xint_gob_til_minus#1\POL_quorem_E-%
669 \POL_quorem_b #1#2.%
670 }%
671 \def\POL_quorem_E-\POL_quorem_b-1.#1\xint:#2\xint_bye\xint:#3.%
672 {%
673 % this terminates the \POL_quorem_a \expanded
674 \iffalse{\fi}\xint:#3.%
675 % recycling some termination code from addition
676 {\expandafter\POL_quorem_ERem_fix\expanded{\POL_add_d#2\xint_bye}}%
677 }%
678 \def\POL_quorem_ERem_fix #1%
679 {%
680 \if P#1\expandafter\POL_quorem_ERem_fix_a\fi 0/1[0]%
681 }%
682 \def\POL_quorem_ERem_fix_a 0/1[0]#1.#2%
683 {%
684 \ifcase #1 %
685 \or
686 \expandafter\xint_firstofone
687 \else
688 P\the\numexpr#1-\xint_c_i.%
689 \fi
690 }%
691 \def\POL_quorem_EQuo#1\xint:#2.%
692 {%
693 {\ifnum#2=\xint_c_
694 #1%
695 \else
696 P#2.\romannumeral0\XINT_revwbr_loop {}%
697 #1\xint:\xint:\xint:\xint:%
698 \xint:\xint:\xint:\xint:\xint_bye
699 \fi}%
700 }%
701 \def\xintPolPRem #1%
702 {%
703 \expanded\expandafter\POL_prem_in\romannumeral`&&@#1\xint:
704 }%
705 \def\POL_prem_in #1\xint:#2%
706 {%
707 \bgroup
708 \expandafter\POL_prem_fork
709 \romannumeral`&&@#2\xint:#1\xint:
710 \POL_prem_end
711 }%
712 \def\POL_prem_fork #1#2\xint:#3%
713 {%
714 \POL_PPfork
715 #1#3{\POL_prem_a}% both polynomials
716 #1P{\POL_prem_second_is_scalar}% -> scalar
717 #3P{\POL_prem_first_is_scalar}% -> zero
718 PP{\POL_prem_both_are_scalar}% -> zero
719 \krof #1#2\xint:#3%
720 }%
721 \def\POL_prem_first_is_scalar #1\xint:#2\xint:\POL_prem_end
722 {%
723 \iffalse{\fi}{1/1[0]}{0/1[0]}%
724 }%
725 \def\POL_prem_second_is_scalar #1\xint:#2\xint:\POL_prem_end
726 {%
727 \iffalse{\fi}{1/1[0]}{#2}%
728 }%
729 \def\POL_prem_both_are_scalar #1\xint:#2\xint:\POL_prem_end
730 {%
731 \iffalse{\fi}{1/1[0]}{0/1[0]}%
732 }%
733 \def\POL_prem_a P#1.#2\xint: P#3.#4\xint:
734 {%
735 \ifnum#1>#3 \xint_dothis{\POL_prem_easy #3.}\fi
736 \xint_orthat
737 {\expandafter\POL_prem_b\the\numexpr#3-#1\expandafter.%
738 \expanded\bgroup
739 \xintRevWithBraces
740 }%
741 {#2}%
742 \noexpand\xint_Bye
743 \xint:
744 \expandafter\POL_placeBye_loop
745 \the\numexpr#1-\xint_c_vii\expandafter.%
746 \romannumeral0\xintrevwithbraces{#4}%
747 {1/1[0]}%
748 \iffalse{\fi}%
749 }%
750 \def\POL_prem_easy #1.#2\xintrevwithbraces#3#4\POL_prem_end
751 {%
752 \iffalse{\fi}{1/1[0]}{P#1.#3}%
753 }%
754 \def\POL_placeBye_loop #1#2.%
755 {%
756 \xint_gob_til_minus#1\POL_placeBye_loop_end-%
757 \expandafter\POL_placeBye_step\the\numexpr#1#2-\xint_c_viii.%
758 }%
759 \def\POL_placeBye_step #1.#2#3#4#5#6#7#8#9%
760 {%
761 {#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}\POL_placeBye_loop#1.%
762 }%
763 \def\POL_placeBye_loop_end-%
764 \expandafter\POL_placeBye_step\the\numexpr-#1-\xint_c_viii.%
765 {%
766 \csname POL_placeBye_end#1\endcsname
767 }%
768 \expandafter\def\csname POL_placeBye_end1\endcsname
769 #1#2#3#4#5#6#7{{#1}{#2}{#3}{#4}{#5}{#6}{#7}\noexpand\xint_Bye\xint:{1}}%
770 \expandafter\def\csname POL_placeBye_end2\endcsname
771 #1#2#3#4#5#6{{#1}{#2}{#3}{#4}{#5}{#6}\noexpand\xint_Bye\xint:{1}}%
772 \expandafter\def\csname POL_placeBye_end3\endcsname
773 #1#2#3#4#5{{#1}{#2}{#3}{#4}{#5}\noexpand\xint_Bye\xint:{1}}%
774 \expandafter\def\csname POL_placeBye_end4\endcsname
775 #1#2#3#4{{#1}{#2}{#3}{#4}\noexpand\xint_Bye\xint:{1}}%
776 \expandafter\def\csname POL_placeBye_end5\endcsname
777 #1#2#3{{#1}{#2}{#3}\noexpand\xint_Bye\xint:{1}}%
778 \expandafter\def\csname POL_placeBye_end6\endcsname
779 #1#2{{#1}{#2}\noexpand\xint_Bye\xint:{1}}%
780 \expandafter\def\csname POL_placeBye_end7\endcsname
781 #1{{#1}\noexpand\xint_Bye\xint:{1}}%
782 \expandafter\def\csname POL_placeBye_end8\endcsname
783 {\noexpand\xint_Bye\xint:{1}}%
784 \def\POL_prem_b_skip#1#2\unexpanded#3#4#5\xint_Bye#6\xint:#7#8#9%
785 {%
786 \iffalse{\fi\expandafter}\xint_gobble_i#5#1%
787 }%
788 \def\POL_prem_b #1.#2#3\xint:#4#5\xint:#6#7%
789 {%
790 \expandafter\POL_prem_c\the\numexpr#1-\xint_c_i\expandafter.%
791 \expanded\bgroup
792 \unexpanded{{#2}#3}\xint:
793 \if0\XINT_Sgn#4\xint:\xint_afterfi
794 {\expandafter\POL_prem_b_skip\expandafter
795 {\expandafter{\romannumeral0\xintmul{#6}{#7}}\xint_Bye\xint:{#6}}%
796 }%
797 \fi
798 \expandafter\POL_add_A
799 \expanded\bgroup
800 \expanded{\noexpand\POL_apply:x_loop{\noexpand\xintMul
801 {\if1\XINT_Sgn#2\xint:\expandafter\XINT_Opp\fi#4}}}%
802 % there is already \xint_Bye at ends of #3
803 #3\xint_Bye\xint_Bye\xint_Bye
804 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
805 % separator for \POL_add_A
806 \unexpanded{\xint_bye\xint_bye\xint_bye\xint_bye\empty}%
807 % there is already \xint_Bye at ends of #5
808 \expanded{\noexpand\POL_apply:x_loop{\noexpand\xintMul{\XINT_Abs#2}}}%
809 #5\xint_Bye\xint_Bye\xint_Bye
810 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
811 \unexpanded{\xint_bye\xint_bye\xint_bye\xint_bye\empty}%
812 % a \iffalse{\fi} will get inserted by \POL_add_A exit routine and if will
813 % terminate the \expanded triggered here after \POL_prem_c
814 % what is next will have already have been expanded
815 {\xintMul{\xintMul{\XINT_Abs#2}{#6}}{#7}}\noexpand\xint_Bye\xint:
816 {\xintMul{\XINT_Abs#2}{#6}}%
817 % This terminates the \expanded following \POL_add_A
818 \iffalse{\fi}%
819 }%
820 \def\POL_prem_c #1%
821 {%
822 \xint_gob_til_minus#1\POL_prem_E_-\POL_prem_b#1%
823 }%
824 % attention that #2 here has a two dummies at end
825 % advantage is that \POL_add_a will always think it is non scalar
826 \def\POL_prem_E_-\POL_prem_b-1.#1\xint:#2\xint_Bye\xint:#3%
827 {%
828 \expandafter\POL_prem_E\expanded{\POL_add_d#2\xint_bye}%
829 }%
830 \def\POL_prem_E #1%
831 {%
832 \if P#1\expandafter\POL_prem_E_i
833 \else\expandafter\POL_prem_E_zero
834 \fi #1%
835 }%
836 \def\POL_prem_E_zero #1\POL_prem_end{\iffalse{\fi}{#1}{0/1[0]}}%
837 \def\POL_prem_E_i P#1.%
838 {%
839 \ifnum #1>\xint_c_i\POL_prem_E_ii#1.\fi
840 \POL_prem_E_iii%
841 }%
842 \def\POL_prem_E_iii#1\POL_prem_end{\iffalse{\fi}#1}%
843 \def\POL_prem_E_ii#1.#2\POL_prem_E_iii#3%
844 {#2{#3}{P\the\numexpr#1-\xint_c_i\iffalse}\fi.}%
845 \def\POL_prem_end{\iffalse{{\fi}}}%
846 %%
847 %% SUPPORT FOR FUNCTIONAL INTERFACE
848 %%
849 % should I do a qpol([]) ?, i.e. without testing for leading zeros, hence
850 % would be faster ? but advantage would arise only for very high degree
851 % pol([]) this one checks for zeros in the right most coeffs
852 \def\xintPolPol#1{\romannumeral`&&@\expandafter\POL_add_d
853 \romannumeral0\expandafter\XINT_revwbr_loop\expandafter
854 {\expandafter}%
855 \romannumeral`&&@#1\xint:\xint:\xint:\xint:
856 \xint:\xint:\xint:\xint:\xint_bye
857 \xint_bye
858 }%
859 % 0.8.1 adds \xintPolLPol. Not much to do... as long as I don't worry about
860 % empty input... (not motivated about this, there are other such places)
861 \def\xintPolLPol#1{\romannumeral`&&@\expandafter\POL_add_d
862 \romannumeral`&&@#1\xint_bye
863 }%
864 % attention to not overwrite macro names (there is a legacy \PolEvalAt)
865 \def\xintPolEvalAt#1#2%
866 {%
867 % generally, #2 will be scalar, but we allow also a polynomial here
868 % should I test for #2 being the monomial, hence handle it very quickly?
869 \romannumeral`&&@\expandafter\POL_evalat_in\romannumeral`&&@#2\xint:
870 #1\xint:\xint:\xint:\xint:
871 \xint:\xint:\xint:\xint:\xint_bye\xint:
872 }%
873 \def\POL_evalat_in #1\xint:
874 {%
875 \expandafter\POL_evalat_fork\expanded{\unexpanded{#1\xint:}\expandafter}%
876 \romannumeral`&&@%
877 }%
878 \def\POL_evalat_fork #1\xint:#2%
879 {%
880 \POL_Pfork
881 #2{\POL_evalat_pol}%
882 P{\POL_evalat_cst}%
883 \krof #1\xint:#2%
884 }%
885 \def\POL_evalat_cst #1\xint: #2\xint:#3\xint_bye\xint:{#2}%
886 \def\POL_evalat_pol #1\xint: P#2.%
887 {%
888 \expanded{\unexpanded{\POL_evalat_a#1\xint:}\expandafter}%
889 \romannumeral0\XINT_revwbr_loop{}%
890 }%
891 \def\POL_evalat_a#1\xint:#2%
892 {%
893 \POL_evalat_loop#2\xint:#1\xint:
894 }%
895 \def\POL_evalat_loop#1\xint:#2\xint:#3%
896 {%
897 \xint_gob_til_xint:#3\POL_evalat_E\xint:
898 % I have dropped here my old strict \xintFoo = \romannumeral0\xintfoo style
899 % ATTENTION! We must allow evaluating at a polynomial expression
900 \expandafter\POL_evalat_loop
901 \romannumeral`&&@\xintPolAdd{#3}{\xintPolMul{#2}{#1}}\xint:#2\xint:
902 }%
903 \def\POL_evalat_E\xint:\expandafter\POL_evalat_loop
904 \romannumeral`&&@\xintPolAdd #1#2\xint:#3\xint:
905 {%
906 \xint_thirdofthree#2%
907 }%
908 %
909 \def\xintPolDeg#1%
910 {%
911 \romannumeral`&&@\expandafter\POL_deg_fork\romannumeral`&&@#1\xint:
912 }%
913 \def\POL_deg_fork #1%
914 {%
915 \POL_zeroPfork
916 #1P{\POL_deg_zero}%
917 0#1{\POL_deg_pol}%
918 0P{\POL_deg_cst}%
919 \krof #1%
920 }%
921 % usual hesitations about using or not raw frac format
922 \def\POL_deg_zero#1\xint:{-1}%
923 \def\POL_deg_cst #1\xint:{0}%
924 \def\POL_deg_pol P#1.#2\xint:{#1}%
925 %
926 \def\xintPolCoeffs#1%
927 {%
928 \romannumeral`&&@\expandafter\POL_coeffs_fork\romannumeral`&&@#1\xint:
929 }%
930 \def\POL_coeffs_fork #1%
931 {%
932 \POL_Pfork
933 #1\POL_coeffs_pol
934 P\POL_coeffs_cst
935 \krof #1%
936 }%
937 % usual hesitations about using or not raw frac format
938 \def\POL_coeffs_cst #1\xint:{{#1}}%
939 % no brace stripping possible, at least two coefficients
940 % annoying that we had to put this delimiter \xint:
941 \def\POL_coeffs_pol P#1.#2\xint:{#2}%
942 %
943 \def\xintPolLCoeffs#1%
944 {%
945 \romannumeral`&&@\expandafter\POL_lcoeffs_fork
946 \romannumeral`&&@#1\xint:\xint:\xint:\xint:%
947 \xint:\xint:\xint:\xint:\xint_bye
948 }%
949 \def\POL_lcoeffs_fork #1%
950 {%
951 \POL_Pfork
952 #1\POL_lcoeffs_pol
953 P\POL_lcoeffs_cst
954 \krof #1%
955 }%
956 \def\POL_lcoeffs_cst #1\xint:#2\xint_bye{{#1}}%
957 \def\POL_lcoeffs_pol P#1.{\XINT_revwbr_loop {}}%
958 %
959 \def\xintPolCoeff#1#2%
960 {%
961 \romannumeral`&&@\expandafter\POL_coeff_fork
962 \the\numexpr\xintNum{#2}\expandafter.%
963 \romannumeral`&&@#1\xint:
964 }%
965 \def\POL_coeff_fork #1.#2%
966 {%
967 \POL_Pfork
968 #2\POL_coeff_pol
969 P\POL_coeff_cst
970 \krof #1.#2%
971 }%
972 \def\POL_coeff_cst#1%
973 {%
974 \xint_UDzerofork
975 #1\POL_coeff_itself
976 0\POL_coeff_zero
977 \krof #1%
978 }%
979 \def\POL_coeff_itself#1.#2\xint:{#2}%
980 \def\POL_coeff_zero#1\xint:{0/1[0]}%
981 \def\POL_coeff_pol #1.P#2.%
982 {%
983 \ifnum#1<\xint_c_\xint_dothis\POL_coeff_zero\fi
984 \ifnum#1>#2 \xint_dothis\POL_coeff_zero\fi
985 \xint_orthat\POL_coeff_a{#1}%
986 }%
987 \def\POL_coeff_a#1{\expandafter\POL_coeff_b\romannumeral\xintgobble{#1}}%
988 \def\POL_coeff_b#1#2\xint:{#1}%
989 %
990 \def\xintPolLC#1%
991 {%
992 \romannumeral`&&@\expandafter\POL_lc_fork
993 \romannumeral`&&@#1\xint:
994 }%
995 \def\POL_lc_fork #1%
996 {%
997 \POL_Pfork
998 #1\POL_lc_pol
999 P\POL_lc_cst
1000 \krof #1%
1001 }%
1002 \def\POL_lc_cst#1\xint:{#1}%
1003 \def\POL_lc_pol P#1.%
1004 {%
1005 \expandafter\POL_lc_a\romannumeral\xintgobble{#1}%
1006 }%
1007 \def\POL_lc_a#1\xint:{#1}%
1008 %
1009 \def\xintPolMonicPart#1%
1010 {%
1011 \romannumeral`&&@\expandafter\POL_monicpart_fork
1012 \romannumeral`&&@#1\xint:
1013 }%
1014 \def\POL_monicpart_fork #1%
1015 {%
1016 \POL_Pfork
1017 #1\POL_monicpart_pol
1018 P\POL_monicpart_cst
1019 \krof #1%
1020 }%
1021 % monicpart(0) must be 0 to avoid breaking algorithms
1022 \def\POL_monicpart_cst#1#2\xint:{\if#10\xint_dothis0\fi\xint_orthat1/1[0]}%
1023 \def\POL_monicpart_pol P#1.#2\xint:%
1024 {%
1025 \expanded{%
1026 P#1.%
1027 \expandafter\POL_monicpart_a\romannumeral\xintgobble{#1}%
1028 #2#2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1029 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1030 }%
1031 }%
1032 \def\POL@DivByFirstAndIrrAndREZ#1#2{\xintREZ{\xintIrr{\xintDiv{#2}{#1}}}}%
1033 \def\POL_monicpart_a#1%
1034 {%
1035 \POL_apply:x_loop{\POL@DivByFirstAndIrrAndREZ{#1}}%
1036 }%
1037 %
1038 \def\xintPolCont#1%
1039 {%
1040 \romannumeral`&&@\expandafter\POL_cont_fork
1041 \romannumeral`&&@#1^%
1042 }%
1043 \def\POL_cont_fork #1%
1044 {%
1045 \POL_Pfork
1046 #1\POL_cont_pol
1047 P\POL_cont_cst
1048 \krof #1%
1049 }%
1050 \def\POL_cont_cst #1^{\xintIrr{\xintAbs{#1}}[0]}%
1051 \def\POL_cont_pol P#1.%
1052 {%
1053 % 1.4d xintfrac \XINT_fgcdof much saner than 1.4 version !
1054 % \XINT_fgcd_out does \xintIrr
1055 \expandafter\XINT_fgcd_out\romannumeral0\XINT_fgcdof
1056 }%
1057 %
1058 \def\xintPolPrimPart#1%
1059 {%
1060 \romannumeral`&&@\expandafter\POL_primpart_fork
1061 \romannumeral`&&@#1\xint:
1062 }%
1063 \def\POL_primpart_fork #1%
1064 {%
1065 \POL_Pfork
1066 #1\POL_primpart_pol
1067 P\POL_primpart_cst
1068 \krof #1%
1069 }%
1070 \def\POL_primpart_cst#1#2\xint:{\if#10\xint_dothis0\fi\xint_orthat1/1[0]}%
1071 \def\POL_primpart_pol P#1.#2\xint:%
1072 {%
1073 \expanded{%
1074 P#1.\expandafter\POL_primpart_a
1075 \romannumeral0\expandafter\XINT_fgcd_out
1076 \romannumeral0\XINT_fgcdof#2^\xint:
1077 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1078 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1079 }%
1080 }%
1081 % cf legacy \POL@makeprim@macro
1082 \def\POL@DivByFirstAndNumAndREZ#1#2{\xintREZ{\xintNum{\xintDiv{#2}{#1}}}}%
1083 \def\POL_primpart_a#1\xint:{\POL_apply:x_loop{\POL@DivByFirstAndNumAndREZ{#1}}}%
1084 %
1085 \def\xintPolRedCoeffs#1%
1086 {%
1087 \romannumeral`&&@\expandafter\POL_redcoeffs_fork
1088 \romannumeral`&&@#1\xint:
1089 }%
1090 \def\POL_redcoeffs_fork #1%
1091 {%
1092 \POL_Pfork
1093 #1\POL_redcoeffs_pol
1094 P\POL_redcoeffs_cst
1095 \krof #1%
1096 }%
1097 \def\POL_redcoeffs_cst#1\xint:{\xintIrr{#1}[0]}%
1098 \def\POL_redcoeffs_pol P#1.#2\xint:%
1099 {%
1100 \expanded{%
1101 P#1.\POL_apply:x_loop\POL@xintIrr
1102 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1103 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1104 }%
1105 }%
1106 %
1107 \def\xintPolSRedCoeffs#1%
1108 {%
1109 \romannumeral`&&@\expandafter\POL_sredcoeffs_fork
1110 \romannumeral`&&@#1\xint:
1111 }%
1112 \def\POL_sredcoeffs_fork #1%
1113 {%
1114 \POL_Pfork
1115 #1\POL_sredcoeffs_pol
1116 P\POL_sredcoeffs_cst
1117 \krof #1%
1118 }%
1119 \def\POL_sredcoeffs_cst#1\xint:{\xintREZ{\xintIrr{#1}[0]}}%
1120 \def\POL_sredcoeffs_pol P#1.#2\xint:%
1121 {%
1122 \expanded{%
1123 P#1.\POL_apply:x_loop\POL@xintIrrAndREZ
1124 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1125 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1126 }%
1127 }%
1128 \def\POL@xintIrrAndREZ#1{\xintREZ{\xintIrr{#1}[0]}}%
1129 %
1130 \def\xintPolDiffOne#1%
1131 {%
1132 \romannumeral`&&@\expandafter\POL_diffone_fork
1133 \romannumeral`&&@#1\xint:
1134 }%
1135 \def\POL_diffone_fork #1%
1136 {%
1137 \POL_Pfork
1138 #1\POL_diffone_pol
1139 P\POL_diffone_cst
1140 \krof #1%
1141 }%
1142 \def\POL_diffone_cst#1\xint:{0/1[0]}%
1143 \def\POL_diffone_pol P#1.#2#3\xint:%
1144 {%
1145 \expanded{%
1146 \ifnum#1=\xint_c_i #3%
1147 \else
1148 P\the\numexpr#1-\xint_c_i.%
1149 \POL_apply:x_iloop{\POL_diffone_diff1.}%
1150 #3\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1151 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1152 \fi
1153 }%
1154 }%
1155 \def\POL_diffone_diff#1.#2#3{\xintMul{#1+#2}{#3}}%
1156 %
1157 \def\xintPolAntiOne#1%
1158 {%
1159 \romannumeral`&&@\expandafter\POL_antione_fork
1160 \romannumeral`&&@#1\xint:
1161 }%
1162 \def\POL_antione_fork #1%
1163 {%
1164 \POL_Pfork
1165 #1\POL_antione_pol
1166 P\POL_antione_cst
1167 \krof #1%
1168 }%
1169 \def\POL_antione_cst#1%
1170 {%
1171 \xint_gob_til_zero#1\POL_antione_zero0\POL_antione_cst_i#1%
1172 }%
1173 \def\POL_antione_cst_i#1\xint:{P1.{0/1[O]}{#1}}%
1174 \def\POL_antione_zero#1\xint:{0/1[0]}%
1175 \def\POL_antione_pol P#1.#2\xint:%
1176 {%
1177 \expanded{%
1178 P\the\numexpr#1+\xint_c_i.{0/1[0]}%
1179 \POL_apply:x_iloop{\POL_antione_anti1.}%
1180 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1181 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1182 }%
1183 }%
1184 \def\POL_antione_anti#1.#2#3{\xintDiv{#3}{#1+#2}}%
1185 %
1186 % #2 can be a polynomial
1187 \def\xintPolIntFrom#1%#2%
1188 {%
1189 \romannumeral`&&@\expandafter\POL_intfrom_a\expandafter
1190 {\romannumeral`&&@\xintPolAntiOne{#1}}%
1191 }%
1192 \def\POL_intfrom_a #1#2%
1193 {%
1194 \xintPolSub{#1}{\xintPolEvalAt{#1}{#2}}%
1195 }%
1196 %
1197 \def\xintPolIntegral#1#2%
1198 {%
1199 \romannumeral`&&@\expandafter\POL_integral_a\expanded
1200 {\xintPolAntiOne{#1}\xint:#2\xint:}%
1201 }%
1202 \def\POL_integral_a #1\xint:#2#3\xint:
1203 {%
1204 \xintPolSub{\xintPolEvalAt{#1}{#3}}{\xintPolEvalAt{#1}{#2}}%
1205 }%
1206 %
1207 \def\xintPolDiffTwo#1%
1208 {%
1209 \romannumeral`&&@\expandafter\POL_difftwo_fork
1210 \romannumeral`&&@#1\xint:
1211 }%
1212 \def\POL_difftwo_fork #1%
1213 {%
1214 \POL_Pfork
1215 #1\POL_difftwo_pol
1216 P\POL_difftwo_cst
1217 \krof #1%
1218 }%
1219 \def\POL_difftwo_cst#1\xint:{0/1[0]}%
1220 \def\POL_difftwo_pol P#1.%
1221 {%
1222 \ifcase #1 %
1223 \or \expandafter\POL_difftwo_zeroout
1224 \or \expandafter\POL_difftwo_cstout
1225 \else\expandafter\POL_difftwo_polout
1226 \fi #1.%
1227 }%
1228 \def\POL_difftwo_zeroout#1\xint:{0/1[0]}%
1229 \def\POL_difftwo_cstout 2.#1#2#3\xint:{\xintMul{2}{#3}}%
1230 \def\POL_difftwo_polout #1.#2#3#4\xint:%
1231 {%
1232 \expanded{%
1233 P\the\numexpr#1-\xint_c_ii.%
1234 \POL_apply:x_iloop{\POL_difftwo_diff2.}%
1235 #4\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1236 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1237 }%
1238 }%
1239 \def\POL_difftwo_diff#1.#2#3{\xintMul{\the\numexpr(#1+#2)*(#1+#2-\xint_c_i)\relax}{#3}}%
1240 %
1241 \def\POL_diffone_iter_fork #1%
1242 {%
1243 \POL_Pfork
1244 #1\POL_diffone_iter_pol
1245 P\POL_diffone_iter_cst
1246 \krof #1%
1247 }%
1248 \def\POL_diffone_iter_cst#1\xint:{0/1[0]\xint:}%
1249 \def\POL_diffone_iter_pol P#1.#2#3\xint:%
1250 {%
1251 \expanded{%
1252 \ifnum#1=\xint_c_i #3%
1253 \else
1254 P\the\numexpr#1-\xint_c_i.%
1255 \POL_apply:x_iloop{\POL_diffone_diff1.}%
1256 #3\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1257 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1258 \fi
1259 }\xint:
1260 }%
1261 %
1262 \def\POL_antione_iter_fork #1%
1263 {%
1264 \POL_Pfork
1265 #1\POL_antione_iter_pol
1266 P\POL_antione_iter_cst
1267 \krof #1%
1268 }%
1269 \def\POL_antione_iter_cst#1%
1270 {%
1271 \xint_gob_til_zero#1\POL_antione_iter_zero0\POL_antione_iter_cst_i#1%
1272 }%
1273 \def\POL_antione_iter_cst_i#1\xint:{P1.{0/1[O]}{#1}\xint:}%
1274 \def\POL_antione_iter_zero#1\xint:{0/1[0]\xint:}%
1275 \def\POL_antione_iter_pol P#1.#2\xint:%
1276 {%
1277 \expanded{%
1278 P\the\numexpr#1+\xint_c_i.{0/1[0]}%
1279 \POL_apply:x_iloop{\POL_antione_anti1.}%
1280 #2\xint_Bye\xint_Bye\xint_Bye\xint_Bye
1281 \xint_Bye\xint_Bye\xint_Bye\xint_Bye\xint_bye
1282 }\xint:
1283 }%
1284 %
1285 \def\xintPolDiffN#1#2%
1286 {%
1287 \romannumeral`&&@\expandafter\POL_diffn_fork
1288 \the\numexpr\xintNum{#2}\expandafter.%
1289 \romannumeral`&&@#1\xint:
1290 }%
1291 \def\POL_diffn_fork #1%
1292 {%
1293 \xint_UDzerominusfork
1294 #1-\POL_diffn_none
1295 0#1\POL_diffn_anti
1296 0-\POL_diffn_diff
1297 \krof #1%
1298 }%
1299 \def\POL_diffn_none0.#1\xint:{#1}%
1300 \def\POL_diffn_diff#1.%#2\xint:%
1301 {%
1302 \ifnum#1>\xint_c_i
1303 \expandafter\POL_diffn_diff\the\numexpr#1-\xint_c_i\expandafter.%
1304 \romannumeral`&&@\expandafter\POL_diffone_iter_fork
1305 \else
1306 \expandafter\POL_diffone_fork
1307 \fi
1308 }%
1309 \def\POL_diffn_anti#1.%#2\xint:%
1310 {%
1311 \ifnum#1<-\xint_c_i
1312 \expandafter\POL_diffn_anti\the\numexpr#1+\xint_c_i\expandafter.%
1313 \romannumeral`&&@\expandafter\POL_antione_iter_fork
1314 \else
1315 \expandafter\POL_antione_fork
1316 \fi
1317 }%
1318 %
1319 % Support for (multi-variable) polgcd
1320 %
1321 \def\xintPolGCDof #1%
1322 {%
1323 \romannumeral`&&@\expandafter\POL_polgcdof\romannumeral`&&@#1^%
1324 }%
1325 \def\XINT_PolGCDof{\romannumeral`&&@\POL_polgcdof}%
1326 \def\POL_polgcdof #1%
1327 {%
1328 \romannumeral`&&@\expandafter
1329 \POL_polgcdof_chkempty\romannumeral`&&@#1\xint:
1330 }%
1331 \def\POL_polgcdof_chkempty #1%
1332 {%
1333 \xint_gob_til_^#1\POL_polgcdof_empty ^\POL_polgcdof_in #1%
1334 }%
1335 \def\POL_polgcdof_empty #1\xint:{1/1[0]}% hesitation
1336 \def\POL_polgcdof_in #1\xint:
1337 {%
1338 \expandafter\POL_polgcdof_loop
1339 \romannumeral`&&@\xintPolPrimPart{#1}\xint:
1340 }%
1341 \def\POL_polgcdof_loop #1\xint:#2%
1342 {%
1343 \expandafter\POL_polgcdof_chkend\romannumeral`&&@#2\xint:#1\xint:\xint:
1344 }%
1345 \def\POL_polgcdof_chkend #1%
1346 {%
1347 \xint_gob_til_^#1\POL_polgcdof_end ^\POL_polgcdof_loop_pair #1%
1348 }%
1349 % hesitation with returning a monic polynomial
1350 %\def\POL_polgcdof_end #1\xint:#2\xint:\xint:{\xintPolMonicPart{#2}}%
1351 \def\POL_polgcdof_end #1\xint:#2\xint:\xint:{#2}%
1352 \def\POL_polgcdof_loop_pair #1\xint:
1353 {%
1354 \expandafter\POL_polgcdof_loop
1355 \romannumeral`&&@\expandafter\POL_polgcd_pair
1356 \romannumeral`&&@\xintPolPrimPart{#1}\xint:
1357 }%
1358 % MEMO comme le #2 sera au début le pgcd accumulé il sera souvent de plus
1359 % petit degré donc il y aura souvent un premier mod "easy" un peu inutile
1360 % J'hésite à faire une permutation avant de lancer le polgcd_pair
1361 \def\POL_polgcd_pair#1\xint:#2\xint:
1362 {%
1363 \xintiiifSgn {\xintPolDeg {#1}}%
1364 {#2}%
1365 {1}%
1366 {\expandafter\POL_polgcd_pair
1367 \romannumeral`&&@\xintPolPrimPart
1368 {\expandafter\xint_secondoftwo
1369 \romannumeral`&&@\xintPolPRem {#2}{#1}}\xint:
1370 #1\xint:
1371 }%
1372 }%
1373 %
1374 \endinput