Debian Codegraph @

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

raw
1 % author: Jean-François Burnol
2 % License: LPPL 1.3c (author-maintained)
3 % Usage: \input polexpr.sty (Plain or other macro formats)
4 % or \usepackage{polexpr} (LaTeX macro format)
5 % Release 0.8.7a (2022/05/19) of polexpr.sty. This file inputs
6 % polexprcore.tex
7 % polexprexpr.tex
8 % polexprsturm.tex
9 \begingroup\catcode61\catcode48\catcode32=10\relax%
10 \catcode13=5 % ^^M
11 \endlinechar=13 %
12 \catcode123=1 % {
13 \catcode125=2 % }
14 \catcode64=11 % @
15 \catcode35=6 % #
16 \catcode44=12 % ,
17 \catcode45=12 % -
18 \catcode46=12 % .
19 \catcode58=12 % :
20 \def\z {\endgroup}%
21 \expandafter\let\expandafter\x\csname [email protected]\endcsname
22 \expandafter\let\expandafter\w\csname [email protected]\endcsname
23 \expandafter
24 \ifx\csname PackageInfo\endcsname\relax
25 \def\y#1#2{\immediate\write-1{Package #1 Info: #2.}}%
26 \else
27 \def\y#1#2{\PackageInfo{#1}{#2}}%
28 \fi
29 \expandafter
30 % I don't think engine exists providing \expanded but not \numexpr
31 \ifx\csname expanded\endcsname\relax
32 \y{polexpr}{\expanded not available, aborting input}%
33 \aftergroup\endinput
34 \else
35 \ifx\x\relax % plain-TeX, first loading of polexpr.sty
36 \ifx\w\relax % but xintexpr.sty not yet loaded.
37 \expandafter\def\expandafter\z\expandafter
38 {\z\input xintexpr.sty\relax}%
39 \fi
40 \else
41 \def\empty {}%
42 \ifx\x\empty % LaTeX, first loading,
43 % variable is initialized, but \ProvidesPackage not yet seen
44 \ifx\w\relax % xintexpr.sty not yet loaded.
45 \expandafter\def\expandafter\z\expandafter
46 {\z\RequirePackage{xintexpr}[2021/03/29]}%
47 \fi
48 \else
49 \aftergroup\endinput % polexpr already loaded.
50 \fi
51 \fi
52 \fi
53 \z%
54 \XINTsetupcatcodes% (does \endlinechar13 in particular)
55 \XINT_providespackage
56 \ProvidesPackage{polexpr}%
57 [2022/05/19 v0.8.7a Polynomial expressions with rational coefficients (JFB)]%
58 \begingroup
59 \def\x#1/#2/#3 #4\xint:{#1#2#3}%
60 \ifnum\expandafter\x\expanded{\csname [email protected]\endcsname}\xint:
61 <20210527 % xint 1.4h
62 \errmessage{Package polexpr error: xintexpr too old, aborting input}%
63 \else\expandafter\xint_gobble_i
64 \fi
65 \endinput\endgroup
66 \let\PolDecToString\xintDecToString
67 \long\def\POL@ifstar#1#2%
68 {%
69 \begingroup\def\@tempa{#1}\def\@tempb{#2}%
70 \futurelet\@let@token\POL@@ifstar
71 }%
72 \def\POL@@ifstar
73 {%
74 \xint_firstofone{\ifx} \@let@token\def\next{\POL@@again\POL@@ifstar}\else
75 \ifx*\@let@token\def\next##1{\expandafter\endgroup\@tempa}\else
76 \def\next{\expandafter\endgroup\@tempb}\fi\fi\next
77 }%
78 \xint_firstofone{\def\POL@@again#1} {\futurelet\@let@token#1}%
79 \long\def\POL@chkopt#1[#2]%
80 {%
81 \begingroup\def\@tempa{#1}\def\@tempb{#1[#2]}%
82 \futurelet\@let@token\POL@@ifopt
83 }%
84 \def\POL@@ifopt
85 {%
86 \xint_firstofone{\ifx} \@let@token\def\next{\POL@@again\POL@@ifopt}\else
87 \ifx[\@let@token\def\next{\expandafter\endgroup\@tempa}\else %]
88 \def\next{\expandafter\endgroup\@tempb}\fi\fi\next
89 }%
90 % \polexprsetup added at 0.7
91 \catcode`! 3 %
92 \def\polexprsetup#1{\POL@setup_parsekeys #1,=!,\xint_bye}%
93 \def\POL@setup_parsekeys #1=#2#3,{%
94 \ifx!#2\expandafter\xint_bye\fi
95 \csname POL@setup_setkey_\xint_zapspaces #1 \xint_gobble_i\endcsname
96 \xint_firstoftwo
97 {\PackageWarning{polexpr}{The \detokenize{#1} key is unknown! ignoring}}%
98 {\xintZapLastSpaces{#2#3}}%
99 \POL@setup_parsekeys
100 }%
101 \def\POL@setup_setkey_norr #1#2{\edef\POL@norr}%
102 \def\POL@setup_setkey_sqfnorr #1#2{\edef\POL@sqfnorr}%
103 \polexprsetup{norr=_norr, sqfnorr=_sqf_norr}%
104 \catcode`! 11 % special catcode for ! as used in xintexpr.sty
105 %
106 \newif\ifxintveryverbose
107 \newif\ifpolnewpolverbose
108 \newif\ifpoltypesetall
109 \newif\ifpoltoexprall
110 %%
111 %% Main data format for non-expandable manipulations
112 %%
113 %% The main exchange structure is:
114 %% N.\empty{coeff0}{coeff1}....{coeffN}
115 %% It is stored in macros \POLuserpol@<name of polynomial>
116 %% The \empty is basically there to avoid brace-stripping
117 %% in some grabbing contexts (maybe I should revisit this)
118 %%
119 %% The zero polynomial is stored as -1.\empty{0/1[0]}
120 %% Degree zero polynomials are 0.\empty{numeric value}
121 %%
122 %% Depending on input path the numeric values coeff0, coeff1, ...., coeffN
123 %% may have been or not already converted into A/B[n] format.
124 %% As a rule, computations are not followed with reducing the fractions
125 %% to smallest terms; the innocent may be unaware that computing
126 %% with fractions quickly give gigantic numbers. There is \PolReduceCoeffs
127 %% to do that.
128 %%
129 %% This base structure is maintained at 0.8 for legacy reasons but perhaps I
130 %% need to revisit this. A characteristic of the package so far is that it
131 %% thus stores and manipulate polynomials basically as the complete sequence
132 %% of coefficients, (using the xintfrac "zero" for missing coefficients) which
133 %% means that it will handle poorly polynomials of high degrees such as X^500.
134 %%
135 %% Test if zero
136 \def\POL@ifZero#1{\expandafter\POL@ifZero@aux#1;}%
137 \def\POL@ifZero@aux #1#2;{\if-#1\expandafter\xint_firstoftwo
138 \else\expandafter\xint_secondoftwo
139 \fi}%
140 %% Split into degree and coefficients
141 % The \expandafter chain removes the \empty token
142 \def\POL@split#1.#2;#3#4%
143 {\def#3{#1}\expandafter\def\expandafter#4\expandafter{#2}}%
144 %% Define from values stored in a "macros-array"
145 \def\POL@resultfromarray #1{%
146 \edef\POL@result{\ifnum\count@>\z@
147 \the\numexpr\count@-\@ne.\noexpand\empty
148 \xintiloop [1+1]%
149 \expandafter\POL@braceit\csname POL@array#1\xintiloopindex\endcsname
150 \ifnum\xintiloopindex<\count@
151 \repeat
152 \else-1.\noexpand\empty{0/1[0]}\fi}%
153 }%
154 \def\POL@braceit#1{{#1}}% needed as \xintiloopindex can not "see" through braces
155 %%
156 %% Conversion between legacy data storage and the one used for the
157 %% the novel polexpr 0.8 notion of \xintexpr polynomial variables
158 %%
159 %% The 0.8 expandable implementation of core algebra is also manipulating
160 %% the complete list of coefficients. The internal data structure is
161 %% (this is the numeric leaf in xintexpr ople terminology) currently:
162 %% PN.{coeff0}{coeff1}....{coeffN}
163 %% where the P letter identifies the polynomial type.
164 %% Here the degree N is *always* at least 1: if some evaluation ends
165 %% up in a constant polynomial it will always be output as a genuine
166 %% scalar numeric variable, as a rule in in A/B[n] format
167 %%
168 %% This is not definitive and I need to think about it more (in particular
169 %% in the distant perspective of supporting multi-variable polynomials).
170 %% However modifying this will be costly labor at this stage.
171 %%
172 \input polexprcore.tex\relax % load expandable algebra
173 \def\POL@vartolegacy #1% \romannumeral\POL@vartolegacy ... \xint:
174 {%
175 \if 0#1\xint_dothis\POL@vartolegacy@zero\fi
176 \if P#1\xint_dothis\POL@vartolegacy@pol\fi
177 \xint_orthat\POL@vartolegacy@scalar #1%
178 }%
179 \def\POL@vartolegacy@zero #1\xint:{\xint_c_ -1.\empty{0/1[0]}}%
180 \def\POL@vartolegacy@scalar #1\xint:{\xint_c_ 0.\empty{#1}}%
181 \def\POL@vartolegacy@pol P#1.#2\xint:{\xint_c_ #1.\empty#2}%
182 %
183 \def\POL@tovar#1{\romannumeral\expandafter\expandafter\expandafter
184 \POL@legacytovar\csname POLuserpol@#1\endcsname}%
185 \def\POL@legacytovar #1.% \romannumeral\POL@legacytovar N.\empty{c0}...
186 {%
187 \ifnum #1<\xint_c_i\xint_dothis\POL@legacytovar@scalar\fi
188 \xint_orthat\POL@legacytovar@pol #1.%
189 }%
190 \def\POL@legacytovar@scalar #1.\empty#2{\xint_c_ #2}%
191 \def\POL@legacytovar@pol #1.\empty{\xint_c_ P#1.}%
192 %%
193 %% Extend \xintexpr (\xintdefvar, \xintdeffunc) to recognize the new
194 %% polynomial type
195 %%
196 %% **** It does NOT apply to \xintfloatexpr context
197 %%
198 \input polexprexpr.tex\relax
199 %%
200 %% \poldef
201 %%
202 %% Ever since 1.0, catcode sanitisation was minimal and only handled
203 %% the semicolon. At last 0.8.7 uses \xintexprSafeCatcodes to enhance
204 %% compatibility with hostile contexts such as babel+french. This
205 %% adds overhead but at least is coherent with \xintdefvar/\xintdeffunc
206 \def\PolDef{\xintexprSafeCatcodes\POL@chkopt\POL@oPolDef[x]}%
207 \def\POL@oPolDef[#1]#2#3{\POL@defpol #2(#1):={#3};}%
208 \def\poldef{\xintexprSafeCatcodes\POL@defpol}%
209 \def\POL@defpol #1(#2)#3=#4;{%
210 \xintexprRestoreCatcodes
211 \edef\POL@polname{\xint_zapspaces #1 \xint_gobble_i}%
212 \begingroup
213 \unless\ifxintveryverbose\xintverbosefalse\fi
214 %% RADICAL CHANGE AT 0.8:
215 %% we define a **variable** not a **function**
216 %% ever since polexpr initial version, a function was defined and
217 %% the associated macros was then deconstructed in further analysis
218 %% via non-expandable approach. At 0.8 the polynomial algebra has
219 %% been implemented expandably allowing direct plug-in into \xintexpr
220 \xintdefvar_a __pol = subs(#4,#2=qraw({{P1.{0/1[0]}{1/1[0]}}}));%
221 \expandafter
222 \endgroup
223 \expandafter\def\expandafter\POL@result\expandafter
224 {\romannumeral0\expandafter\xint_stop_atfirstofone
225 \romannumeral0\csname XINT_expr_varvalue___pol\endcsname}%
226 \XINT_global\expandafter\def\csname POLuserpol@\POL@polname\expandafter\endcsname
227 \expandafter{\romannumeral\expandafter\POL@vartolegacy\POL@result\xint:}%
228 \expandafter\POL@newpol\expandafter{\POL@polname}%
229 }%
230 \def\POL@newpol#1{%
231 % 0.7.5 had some complicated special handling of constant
232 % polynomials, but these are complications of the past
233 % First a variable usable in \poldef but not in \xintexpr for arithmetic
234 % only for special dedicated functions such as coeff(), deg()
235 % (when they will be implemented). In \poldef, composition of polynomials
236 % in P(Q) syntax will be more efficient than P(Q(x)).
237 % This will use \XINT_global and obey \xintverbose... setting
238 \XINT_expr_defvar_one{#1}{{\POL@tovar{#1}}}%
239 % Second a function usable not only in \poldef but also in \xintexpr
240 % Will use \XINT_global
241 \POL@newpolhorner{#1}%
242 \POL@defpolfunc{#1}{expr}%
243 \XINT_global\expandafter\let\csname XINT_flexpr_func_#1\endcsname\@undefined
244 \ifpolnewpolverbose\POL@info{#1}\fi
245 }%
246 \def\POL@newfloatpol#1{%
247 \POL@newfloatpolhorner{#1}%
248 \POL@defpolfunc{#1}{flexpr}%
249 \ifpolnewpolverbose\POL@floatinfo{#1}%
250 \else
251 \ifxintverbose\POL@floatinfo{#1}\fi
252 \fi
253 }%
254 \def\POL@info #1{%
255 \xintMessage {polexpr}{Info}%
256 {Function #1 for the \string\xintexpr\space parser is
257 \ifxintglobaldefs(globally) \fi
258 associated to \string\XINT_expr_polfunc_#1\space
259 with meaning:
260 \expandafter\meaning
261 \csname XINT_expr_polfunc_#1\endcsname}%
262 }%
263 \def\POL@floatinfo #1{%
264 \xintMessage {polexpr}{Info}%
265 {Function #1 for the \string\xintfloatexpr\space parser is
266 \ifxintglobaldefs(globally) \fi
267 associated to \string\XINT_flexpr_polfunc_#1\space
268 with meaning:
269 \expandafter\meaning
270 \csname XINT_flexpr_polfunc_#1\endcsname}%
271 }%
272 %
273 \def\POL@newpolhorner#1{%
274 \expandafter\expandafter\expandafter\POL@split
275 \csname POLuserpol@#1\endcsname;\POL@var@deg\POL@var@coeffs
276 \edef\POL@var@coeffs{\xintRevWithBraces{\POL@var@coeffs}}%
277 \begingroup
278 \expandafter\POL@newpol@horner\POL@var@coeffs\relax
279 \expandafter
280 \endgroup
281 \expandafter\XINT_global
282 \expandafter\def\csname XINT_expr_polfunc_#1\expandafter\endcsname
283 \expandafter##\expandafter1\expandafter{\POL@tmp{##1}}%
284 }%
285 \def\POL@newfloatpolhorner#1{%
286 %% redefine function to expand by Horner scheme. Is this useful?
287 %% perhaps bad idea for numerical evaluation of thing such as (1+x)^10?
288 % note: I added {0/1[0]} item to zero polynomial also to facilitate this
289 \expandafter\expandafter\expandafter\POL@split
290 \csname POLuserpol@#1\endcsname;\POL@var@deg\POL@var@coeffs
291 \edef\POL@var@coeffs{\xintRevWithBraces{\POL@var@coeffs}}%
292 \begingroup
293 \expandafter\POL@newpol@floathorner\POL@var@coeffs\relax
294 \expandafter
295 \endgroup
296 \expandafter\def\csname XINT_flexpr_polfunc_#1\expandafter\endcsname
297 \expandafter##\expandafter1\expandafter{\POL@tmp{##1}}%
298 }%
299 \def\POL@newpol@horner#1{\let\xintPolAdd\relax\let\xintPolMul\relax
300 \def\POL@tmp##1{#1}\POL@newpol@horner@loop.}%
301 \def\POL@newpol@horner@loop.#1{%
302 \if\relax#1\expandafter\xint_gob_til_dot\fi
303 \edef\POL@tmp##1{\xintiiifZero{#1}
304 {\xint_firstofone}{\xintPolAdd{#1}}%
305 {\xintPolMul{##1}{\POL@tmp{##1}}}}%
306 \POL@newpol@horner@loop.%
307 }%
308 \def\POL@newpol@floathorner#1{\let\XINTinFloatAdd\relax\let\XINTinFloatMul\relax
309 \edef\POL@tmp##1{\XINTinFloatdigits{#1}}%
310 \POL@newpol@floathorner@loop.}%
311 \def\POL@newpol@floathorner@loop.#1{%
312 \if\relax#1\expandafter\xint_gob_til_dot\fi
313 \edef\POL@tmp##1{\xintiiifZero{#1}
314 {\xint_firstofone}{\XINTinFloatAdd{\XINTinFloatdigits{#1}}}%
315 {\XINTinFloatMul{##1}{\POL@tmp{##1}}}}%
316 \POL@newpol@floathorner@loop.%
317 }%
318 %%
319 %% Non-expandable polynomial manipulations
320 %%
321 \def\PolGenFloatVariant#1{\POL@newfloatpol{#1}}%
322 %
323 \def\PolLet#1#2{\if=\noexpand#2\expandafter\xint_firstoftwo
324 \else\expandafter\xint_secondoftwo\fi
325 \POL@@let\POL@let{#1}{#2}}%
326 \def\POL@@let#1#2#3{\POL@let{#1}{#3}}%
327 \def\POL@let#1#2{%
328 \XINT_global
329 \expandafter\let\csname POLuserpol@#1\expandafter\endcsname
330 \csname POLuserpol@#2\endcsname
331 \XINT_expr_defvar_one{#1}{{\POL@tovar{#1}}}%
332 \XINT_global
333 \expandafter\let\csname XINT_expr_polfunc_#1\expandafter\endcsname
334 \csname XINT_expr_polfunc_#2\endcsname
335 \POL@defpolfunc{#1}{expr}%
336 \ifpolnewpolverbose\POL@info{#1}\fi
337 }%
338 \def\PolGlobalLet#1#2{\begingroup\xintglobaldefstrue\PolLet{#1}{#2}\endgroup}%
339 %
340 \def\PolAssign#1{\def\POL@polname{#1}\POL@assign}% zap spaces in #1?
341 \def\POL@assign#1\toarray#2{%
342 \expandafter\expandafter\expandafter\POL@split
343 \csname POLuserpol@\POL@polname\endcsname;\POL@var@deg\POL@var@coeffs
344 \xintAssignArray\POL@var@coeffs\to#2%
345 % modify \#200 macro to return 0/1[0] for out of range indices
346 \@namedef{\xint_arrayname00}##1##2##3{%
347 \@namedef{\xint_arrayname00}####1{%
348 \ifnum####1>##1 \xint_dothis{ 0/1[0]}\fi
349 \ifnum####1>\m@ne \xint_dothis
350 {\expandafter\expandafter\expandafter##3%
351 \csname##2####1\endcsname}\fi
352 \unless\ifnum-####1>##1 \xint_dothis
353 {\expandafter\expandafter\expandafter##3%
354 \csname##2\the\numexpr##1+####1+\@ne\endcsname}\fi
355 \xint_orthat{ 0/1[0]}}% space stops a \romannumeral0
356 }%
357 \csname\xint_arrayname00\expandafter\expandafter\expandafter\endcsname
358 \expandafter\expandafter\expandafter
359 {\csname\xint_arrayname0\expandafter\endcsname\expandafter}\expandafter
360 {\xint_arrayname}{ }%
361 }%
362 \def\PolGet{}%
363 \def\PolGet#1#2\fromarray#3{%
364 \begingroup % closed in \POL@getfromarray
365 \POL@getfromarray{#1}{#3}%
366 \POL@newpol{#1}%
367 }%
368 \def\POL@getfromarray#1#2{%
369 \count@=#2{0} %<- intentional space
370 \ifnum\count@=\z@
371 \def\POL@result{-1.\empty{0/1[0]}}% 0.5 fix for empty array
372 \else
373 \xintloop
374 \edef\POL@tmp{#2{\count@}}%
375 \edef\POL@tmp{\xintRaw{\POL@tmp}}%
376 % sadly xinttools (current 1.3a) arrays have no setters for individual items...
377 \expandafter\let\csname POL@tmparray\the\count@\endcsname\POL@tmp
378 \if0\xintiiSgn{\POL@tmp}%
379 \advance\count@\m@ne
380 \repeat
381 \count\tw@\count@
382 \xintloop
383 \ifnum\count@>\@ne
384 \advance\count@\m@ne
385 \edef\POL@tmp{#2{\count@}}%
386 \edef\POL@tmp{\xintRaw{\POL@tmp}}%
387 \expandafter\let\csname POL@tmparray\the\count@\endcsname\POL@tmp
388 \repeat
389 \count@\count\tw@
390 \def\POL@tmp##1.{{\csname POL@tmparray##1\endcsname}}%
391 \edef\POL@result{\the\numexpr\count@-\@ne.\noexpand\empty
392 \xintiloop[1+1]%
393 \expandafter\POL@tmp\xintiloopindex.%
394 \ifnum\xintiloopindex<\count@
395 \repeat}%
396 \fi
397 \expandafter
398 \endgroup
399 \expandafter
400 \XINT_global
401 \expandafter
402 \def\csname POLuserpol@#1\expandafter\endcsname
403 \expandafter{\POL@result}%
404 }%
405 %
406 \def\PolFromCSV#1#2{%
407 \begingroup % closed in \POL@getfromarray
408 \xintAssignArray\xintCSVtoList{#2}\to\POL@arrayA
409 \POL@getfromarray{#1}\POL@arrayA
410 \POL@newpol{#1}%
411 }%
412 %
413 \def\PolMapCoeffs#1#2{% #1 = macro, #2 = name
414 \POL@mapcoeffs{#1}{#2}%
415 \POL@newpol{#2}%
416 }%
417 \def\POL@mapcoeffs#1#2{%
418 \begingroup
419 \def\POL@mapcoeffs@macro{#1}%
420 \expandafter\expandafter\expandafter\POL@split
421 \csname POLuserpol@#2\endcsname;\POL@mapcoeffs@deg\POL@mapcoeffs@coeffs
422 % ATTENTION à ne pas faire un \expandafter ici, car brace removal si 1 item
423 \xintAssignArray\POL@mapcoeffs@coeffs\to\POL@arrayA
424 \def\index{0}%
425 \count@\z@
426 \expandafter\POL@map@loop\expandafter.\POL@mapcoeffs@coeffs\relax
427 \xintloop
428 % this abuses that \POL@arrayA0 is never 0.
429 \xintiiifZero{\csname POL@arrayA\the\count@\endcsname}%
430 {\iftrue}%
431 {\iffalse}%
432 \advance\count@\m@ne
433 \repeat
434 % donc en sortie \count@ est 0 ssi pol nul.
435 \POL@resultfromarray A%
436 \expandafter
437 \endgroup
438 \expandafter
439 \XINT_global
440 \expandafter
441 \def\csname POLuserpol@#2\expandafter\endcsname\expandafter{\POL@result}%
442 }%
443 \def\POL@map@loop.#1{\if\relax#1\expandafter\xint_gob_til_dot\fi
444 \advance\count@\@ne
445 \edef\POL@map@coeff{\POL@mapcoeffs@macro{#1}}%
446 \expandafter
447 \let\csname POL@arrayA\the\count@\endcsname\POL@map@coeff
448 \edef\index{\the\numexpr\index+\@ne}%
449 \POL@map@loop.}%
450 %
451 \def\POL@xintIrr#1{\xintIrr{#1}[0]}%
452 \def\PolReduceCoeffs{\POL@ifstar\POL@sreducecoeffs\POL@reducecoeffs}%
453 \def\POL@reducecoeffs#1{\PolMapCoeffs{\POL@xintIrr}{#1}}%
454 \def\POL@sreducecoeffs#1{\PolMapCoeffs{\xintPIrr}{#1}}%
455 %
456 \def\PolMakeMonic#1{%
457 \edef\POL@leadingcoeff{\PolLeadingCoeff{#1}}%
458 \edef\POL@leadingcoeff@inverse{\xintDiv{1/1[0]}{\POL@leadingcoeff}}%
459 \PolMapCoeffs{\xintMul{\POL@leadingcoeff@inverse}}{#1}%
460 }%
461 %
462 %% \PolMakePrimitive (0.5)
463 % This uses expandable \PolIContent
464 % Note: the integer coefficients stored in A/1[n] form with
465 % A not having trailing zeroes, due to usage of \xintREZ here.
466 \def\POL@makeprim@macro#1%
467 {\xintREZ{\xintNum{\xintDiv{#1}{\POL@makeprim@icontent}}}}%
468 \def\PolMakePrimitive#1{%
469 % This does not need a full user declared polynomial on input, only
470 % a \POLuserpol@name macro, but on output it is fully declared
471 \edef\POL@makeprim@icontent{\PolIContent{#1}}%
472 \PolMapCoeffs\POL@makeprim@macro{#1}%
473 }%
474 \def\POL@makeprimitive#1{%
475 % Avoids declaring the polynomial, internal usage in \PolToSturm
476 \edef\POL@makeprim@icontent{\PolIContent{#1}}%
477 \POL@mapcoeffs\POL@makeprim@macro{#1}%
478 }%
479 %
480 %% Euclidean division
481 % since 0.8 based on the expandable routine from polexprcore.tex
482 %
483 \def\PolDivide#1#2#3#4{% #3=quotient, #4=remainder of #1 by #2
484 \POL@divide{#1}{#2}%
485 \XINT_global\expandafter\let\csname POLuserpol@#3\endcsname\POL@Q
486 \POL@newpol{#3}%
487 \XINT_global\expandafter\let\csname POLuserpol@#4\endcsname\POL@R
488 \POL@newpol{#4}%
489 }%
490 \def\PolQuo#1#2#3{% #3=quotient of #1 by #2
491 \POL@divide{#1}{#2}%
492 \XINT_global\expandafter\let\csname POLuserpol@#3\endcsname\POL@Q
493 \POL@newpol{#3}%
494 }%
495 \def\PolRem#1#2#3{% #3=remainder of #1 by #2
496 \POL@divide{#1}{#2}%
497 \XINT_global\expandafter\let\csname POLuserpol@#3\endcsname\POL@R
498 \POL@newpol{#3}%
499 }%
500 \def\POL@divide#1#2{%
501 % much simpler at 0.8 thanks to our expandable macros
502 \xintAssign\xintPolQuoRem{\POL@tovar{#1}}{\POL@tovar{#2}}\to\POL@Q\POL@R
503 \odef\POL@Q{\romannumeral\expandafter\POL@vartolegacy\POL@Q\xint:}%
504 \odef\POL@R{\romannumeral\expandafter\POL@vartolegacy\POL@R\xint:}%
505 }%
506 %% Euclidean special pseudo-remainder
507 \def\POL@getprem#1#2{%
508 \let\POL@Q\undefined % trap errors in Sturm code update to use \POL@prem
509 % this was simpler before I converted \xintPolPRem into returning a tuple...
510 \odef\POL@R{\romannumeral\expandafter\POL@vartolegacy
511 \romannumeral0\expandafter\xint_stop_atsecondoftwo
512 \romannumeral`&&@\xintPolPRem{\POL@tovar{#1}}{\POL@tovar{#2}}%
513 \xint:}%
514 }%
515 %
516 %%%%%%%%%%%%
517 %%
518 %% Things are currenly implemented twice : here the legacy macros
519 %% such as GCD or Diff, and in polexprcore.tex the expandable
520 %% support macros for the \xinteval interface.
521 %%
522 %% Soon, I will probably remove all legacy code (like I did already
523 %% for division) and make the user macros simple wrappers to the
524 %% expandable code.
525 %%
526 %% But for 0.8 release, I preferred not to yet, as I did not have
527 %% really the time to compare speed. Usage of the "special
528 %% pseudo euclidean remainder" (expandable) code in Sturm chain
529 %% construction proved very beneficial as it divided by 3 the
530 %% \PolToSturm execution time on the Wilkinson perturbed type 1
531 %% example in the documentation.
532 %%
533 %%%%%%%%%%%%
534 %
535 %% GCD
536 %
537 % It seems I didn't even use here the (now deleted) macros implementing
538 % division, and I redid here what was needed: this code, which I leave
539 % standing as I have other priorities, does not use the \POL@divide !
540 %
541 \def\PolGCD#1#2#3{% sets #3 to the (unitary) G.C.D. of #1 and #2
542 \POL@GCD{#1}{#2}{#3}%
543 \POL@newpol{#3}%
544 }%
545 \def\POL@GCD #1#2#3{%
546 \begingroup
547 \expandafter\let\expandafter\POL@A\csname POLuserpol@#1\endcsname
548 \expandafter\let\expandafter\POL@B\csname POLuserpol@#2\endcsname
549 \expandafter\POL@split\POL@A;\POL@degA\POL@polA
550 \expandafter\POL@split\POL@B;\POL@degB\POL@polB
551 \ifnum\POL@degA<\z@
552 \expandafter\xint_firstoftwo\else\expandafter\xint_secondoftwo
553 \fi
554 {\ifnum\POL@degB<\z@
555 \expandafter\xint_firstoftwo\else\expandafter\xint_secondoftwo
556 \fi
557 {\def\POL@result{-1.\empty{0/1[0]}}}%
558 {\xintAssignArray\POL@polB\to\POL@arrayB
559 \POL@normalize{B}%
560 \POL@gcd@exit BA}}%
561 {\ifnum\POL@degB<\z@
562 \expandafter\xint_firstoftwo\else\expandafter\xint_secondoftwo
563 \fi
564 {\xintAssignArray\POL@polA\to\POL@arrayA
565 \POL@normalize{A}%
566 \POL@gcd@exit AB}%
567 {\ifnum\POL@degA<\POL@degB\space
568 \let\POL@tmp\POL@B\let\POL@B\POL@A\let\POL@A\POL@tmp
569 \let\POL@tmp\POL@degB\let\POL@degB\POL@degA\let\POL@degA\POL@tmp
570 \let\POL@tmp\POL@polB\let\POL@polB\POL@polA\let\POL@polA\POL@tmp
571 \fi
572 \xintAssignArray\POL@polA\to\POL@arrayA
573 \xintAssignArray\POL@polB\to\POL@arrayB
574 \POL@gcd AB%
575 }}%
576 \expandafter
577 \endgroup
578 \expandafter
579 \XINT_global
580 \expandafter\def\csname POLuserpol@#3\expandafter\endcsname
581 \expandafter{\POL@result}%
582 }%
583 \def\POL@normalize#1{%
584 \expandafter\def\expandafter\POL@tmp\expandafter
585 {\csname POL@array#1\csname POL@array#10\endcsname\endcsname}%
586 \edef\POL@normalize@leading{\POL@tmp}%
587 \expandafter\def\POL@tmp{1/1[0]}%
588 \count@\csname POL@deg#1\endcsname\space
589 \xintloop
590 \ifnum\count@>\z@
591 \expandafter\edef\csname POL@array#1\the\count@\endcsname
592 {\xintIrr{\xintDiv
593 {\csname POL@array#1\the\count@\endcsname}%
594 {\POL@normalize@leading}}[0]}%
595 \advance\count@\m@ne
596 \repeat
597 }%
598 \def\POL@gcd#1#2{%
599 \POL@normalize{#2}%
600 \edef\POL@degQ{\the\numexpr\csname POL@deg#1\endcsname
601 -\csname POL@deg#2\endcsname}%
602 \count@\numexpr\csname POL@deg#1\endcsname+\@ne\relax
603 \count\tw@\numexpr\POL@degQ+\@ne\relax
604 \xintloop
605 \POL@gcd@getremainder@loopbody#1#2%
606 \ifnum\count\tw@>\z@
607 \repeat
608 \expandafter\def\csname POL@array#10\endcsname{1}%
609 \xintloop
610 \xintiiifZero{\csname POL@array#1\the\count@\endcsname}%
611 {\iftrue}%
612 {\iffalse}%
613 \advance\count@\m@ne
614 \repeat
615 \expandafter\edef\csname POL@deg#1\endcsname{\the\numexpr\count@-\@ne}%
616 \ifnum\count@<\@ne
617 \expandafter\POL@gcd@exit
618 \else
619 \expandafter\edef\csname POL@array#10\endcsname{\the\count@}%
620 \expandafter\POL@gcd
621 \fi{#2}{#1}%
622 }%
623 \def\POL@gcd@getremainder@loopbody#1#2{%
624 \edef\POL@gcd@ratio{\csname POL@array#1\the\count@\endcsname}%
625 \advance\count@\m@ne
626 \advance\count\tw@\m@ne
627 \count4 \count@
628 \count6 \csname POL@deg#2\endcsname\space
629 \xintloop
630 \ifnum\count6>\z@
631 \expandafter\edef\csname POL@array#1\the\count4\endcsname
632 {\xintSub
633 {\csname POL@array#1\the\count4\endcsname}%
634 {\xintMul
635 {\POL@gcd@ratio}%
636 {\csname POL@array#2\the\count6\endcsname}}}%
637 \advance\count4 \m@ne
638 \advance\count6 \m@ne
639 \repeat
640 }%
641 \def\POL@gcd@exit#1#2{%
642 \count@\numexpr\csname POL@deg#1\endcsname+\@ne\relax
643 \POL@resultfromarray #1%
644 }%
645 %
646 %% DIFFERENTIATION
647 %
648 \def\POL@diff@loop@one #1/#2[#3]#4%
649 {\xintIrr{\xintiiMul{#4}{#1}/#2[0]}[#3]}%
650 \def\POL@diff#1{\POL@diff@loop1.}%
651 \def\POL@diff@loop#1.#2{%
652 \if\relax#2\expandafter\xint_gob_til_dot\fi
653 {\expandafter\POL@diff@loop@one\romannumeral0\xintraw{#2}{#1}}%
654 \expandafter\POL@diff@loop\the\numexpr#1+\@ne.%
655 }%
656 \def\PolDiff{\POL@chkopt\POL@oPolDiff[1]}%
657 \def\POL@oPolDiff[#1]{%
658 % optional parameter is how many times to derivate
659 % first mandatory arg is name of polynomial function to derivate,
660 % same name as in \NewPolExpr
661 % second mandatory arg name of derivative
662 \edef\POL@iterindex{\the\numexpr#1\relax}%
663 \ifnum\POL@iterindex<\z@
664 \expandafter\xint_firstoftwo
665 \else
666 \expandafter\xint_secondoftwo
667 \fi
668 {\PolAntiDiff[-\POL@iterindex]}{\POL@Diff}%
669 }%
670 \def\POL@Diff{%
671 \ifcase\POL@iterindex\space
672 \expandafter\POL@Diff@no
673 \or\expandafter\POL@Diff@one
674 \else\xint_afterfi{\POL@Iterate\POL@Diff@one}%
675 \fi
676 }%
677 \def\POL@Diff@no #1#2{\POL@let{#2}{#1}}%
678 \def\POL@Diff@one #1#2{\POL@Diff@@one {#1}{#2}\POL@newpol{#2}}%
679 \def\POL@Diff@@one#1#2{%
680 \expandafter\expandafter\expandafter\POL@split
681 \csname POLuserpol@#1\endcsname;\POL@var@deg\POL@var@coeffs
682 \ifnum\POL@var@deg<\@ne
683 \XINT_global\@namedef{POLuserpol@#2}{-1.\empty{0/1[0]}}%
684 \else
685 \edef\POL@var@coeffs{\expandafter\POL@diff\POL@var@coeffs\relax}%
686 \XINT_global\expandafter\edef\csname POLuserpol@#2\endcsname
687 {\the\numexpr\POL@var@deg-\@ne.\noexpand\empty\POL@var@coeffs}%
688 \fi
689 }%
690 % lazy way but allows to share with AntiDiff
691 \def\POL@Iterate#1#2#3{%
692 \begingroup
693 \xintverbosefalse
694 #1{#2}{#3}%
695 \xintloop
696 \ifnum\POL@iterindex>\tw@
697 #1{#3}{#3}%
698 \edef\POL@iterindex{\the\numexpr\POL@iterindex-\@ne}%
699 \repeat
700 \expandafter
701 \endgroup\expandafter
702 \XINT_global
703 \expandafter
704 \def\csname POLuserpol@#3\expandafter\endcsname
705 \expandafter{\romannumeral`&&@\csname POLuserpol@#3\endcsname}%
706 #1{#3}{#3}%
707 }%
708 %
709 %% ANTI-DIFFERENTIATION
710 %
711 \def\POL@antidiff@loop@one #1/#2[#3]#4%
712 {\xintIrr{#1/\xintiiMul{#4}{#2}[0]}[#3]}%
713 \def\POL@antidiff{\POL@antidiff@loop1.}%
714 \def\POL@antidiff@loop#1.#2{%
715 \if\relax#2\expandafter\xint_gob_til_dot\fi
716 {\expandafter\POL@antidiff@loop@one\romannumeral0\xintraw{#2}{#1}}%
717 \expandafter\POL@antidiff@loop\the\numexpr#1+\@ne.%
718 }%
719 \def\PolAntiDiff{\POL@chkopt\POL@oPolAntiDiff[1]}%
720 \def\POL@oPolAntiDiff[#1]{%
721 % optional parameter is how many times to derivate
722 % first mandatory arg is name of polynomial function to derivate,
723 % same name as in \NewPolExpr
724 % second mandatory arg name of derivative
725 \edef\POL@iterindex{\the\numexpr#1\relax}%
726 \ifnum\POL@iterindex<\z@
727 \expandafter\xint_firstoftwo
728 \else
729 \expandafter\xint_secondoftwo
730 \fi
731 {\PolDiff[-\POL@iterindex]}{\POL@AntiDiff}%
732 }%
733 \def\POL@AntiDiff{%
734 \ifcase\POL@iterindex\space
735 \expandafter\POL@AntiDiff@no
736 \or\expandafter\POL@AntiDiff@one
737 \else\xint_afterfi{\POL@Iterate\POL@AntiDiff@one}%
738 \fi
739 }%
740 \let\POL@AntiDiff@no\POL@Diff@no
741 \def\POL@AntiDiff@one #1#2{\POL@AntiDiff@@one{#1}{#2}\POL@newpol{#2}}%
742 \def\POL@AntiDiff@@one#1#2{%
743 \expandafter\expandafter\expandafter\POL@split
744 \csname POLuserpol@#1\endcsname;\POL@var@deg\POL@var@coeffs
745 \ifnum\POL@var@deg<\z@
746 \XINT_global\@namedef{POLuserpol@#2}{-1.\empty{0/1[0]}}%
747 \else
748 \edef\POL@var@coeffs{\expandafter\POL@antidiff\POL@var@coeffs\relax}%
749 \XINT_global\expandafter\edef\csname POLuserpol@#2\endcsname
750 {\the\numexpr\POL@var@deg+\@ne.\noexpand\empty{0/1[0]}\POL@var@coeffs}%
751 \fi
752 }%
753 %
754 %%
755 %% Localization of roots
756 %%
757 % this is big. It provides also output macros, of both expandable and
758 % non-expandable type
759 \input polexprsturm.tex\relax
760 %
761 %
762 %% Non-expandable output macros
763 %
764 \def\PolTypesetCmdPrefix#1{\xintiiifSgn{#1}{}{+}{+}}%
765 \def\PolTypesetCmd#1{\xintifOne{\xintiiAbs{#1}}%
766 {\ifnum\PolIndex=\z@\xintiiSgn{#1}\else
767 \xintiiifSgn{#1}{-}{}{}\fi
768 \let\PolIfCoeffIsPlusOrMinusOne\xint_firstoftwo}%
769 {\PolTypesetOne{#1}%
770 \let\PolIfCoeffIsPlusOrMinusOne\xint_secondoftwo}%
771 }%
772 \ifdefined\frac
773 \def\PolTypesetOne{\xintTeXsignedFrac}%
774 \else
775 \def\PolTypesetOne{\xintTeXsignedOver}%
776 \fi
777 \catcode`^ 7 %
778 \def\PolTypesetMonomialCmd{%
779 \ifcase\PolIndex\space
780 %
781 \or\PolVar
782 \else\PolVar^{\PolIndex}%
783 \fi
784 }%
785 \catcode`^ 11 % normal xint catcode
786 \def\PolTypeset{\POL@ifstar
787 {\def\POL@ts@ascending{1}\POL@Typeset}%
788 {\def\POL@ts@ascending{0}\POL@Typeset}%
789 }%
790 %%
791 %% \PolTypeset
792 %%
793 %% extended at 0.8 to handle arbitrary expressions on input
794 %%
795 \def\POL@Typeset{\POL@chkopt\POL@oPOL@Typeset[x]}%
796 \def\POL@oPOL@Typeset[#1]#2{%
797 \ifmmode\let\POL@endtypeset\empty\else$\def\POL@endtypeset{$}\fi
798 \ifcsname POLuserpol@#2\endcsname
799 \expandafter\expandafter\expandafter\POL@split
800 \csname POLuserpol@#2\endcsname;\POL@var@deg\POL@var@coeffs
801 \else
802 \xintAssign\expandafter\xint_firstofone\romannumeral0\xintbareeval
803 subs((deg(x),coeffs(x)),x=subs(#2,\PolToExprInVar=pol([0,1])))\relax
804 \to\POL@var@deg\POL@var@coeffs
805 \fi
806 \if\POL@ts@ascending1%
807 \def\PolIndex{0}%
808 \let\POL@ts@reverse\xint_firstofone
809 \let\POL@@ne@or@m@ne\@ne
810 \else
811 \let\PolIndex\POL@var@deg
812 \ifnum\PolIndex<\z@\def\PolIndex{0}\fi
813 \let\POL@ts@reverse\xintRevWithBraces
814 \let\POL@@ne@or@m@ne\m@ne
815 \fi
816 \def\PolVar{#1}%
817 \ifnum\POL@var@deg<\z@
818 \PolTypesetCmd{0/1[0]}\PolTypesetMonomialCmd
819 \else
820 \ifnum\POL@var@deg=\z@
821 \expandafter\PolTypesetCmd\POL@var@coeffs\PolTypesetMonomialCmd
822 \else
823 \def\POL@ts@prefix##1{\let\POL@ts@prefix\PolTypesetCmdPrefix}%
824 \expandafter\POL@ts@loop
825 \romannumeral-`0\POL@ts@reverse{\POL@var@coeffs}\relax
826 \fi
827 \fi
828 \POL@endtypeset
829 }%
830 \def\POL@ts@loop{\ifpoltypesetall\expandafter\xint_firstoftwo
831 \else\expandafter\xint_secondoftwo\fi
832 {\POL@ts@nocheck}{\POL@ts@check}.%
833 }%
834 \def\POL@ts@check.#1{%
835 \if\relax#1\expandafter\xint_gob_til_dot\fi
836 \xintiiifZero{#1}%
837 {}%
838 {\POL@ts@prefix{#1}\PolTypesetCmd{#1}\PolTypesetMonomialCmd}%
839 \edef\PolIndex{\the\numexpr\PolIndex+\POL@@ne@or@m@ne}\POL@ts@check.%
840 }%
841 \def\POL@ts@nocheck.#1{%
842 \if\relax#1\expandafter\xint_gob_til_dot\fi
843 \POL@ts@prefix{#1}\PolTypesetCmd{#1}\PolTypesetMonomialCmd
844 \edef\PolIndex{\the\numexpr\PolIndex+\POL@@ne@or@m@ne}\POL@ts@nocheck.%
845 }%
846 %
847 %%
848 %% Expandable output macros (legacy)
849 %%
850 \def\POL@eval@fork#1\At#2#3\krof{#2}%
851 \def\PolEval#1#2#3{\romannumeral`&&@\POL@eval@fork
852 #2\PolEvalAt
853 \At\PolEvalAtExpr\krof {#1}{#3}%
854 }%
855 \def\PolEvalAt#1#2{%
856 \xintpraw{\csname XINT_expr_polfunc_#1\endcsname{#2}}%
857 }%
858 \def\POL@eval#1#2{%
859 \csname XINT_expr_polfunc_#1\endcsname{#2}%
860 }%
861 \def\PolEvalAtExpr#1#2{\xinttheexpr #1(#2)\relax}%
862 %
863 \def\PolEvalReduced#1#2#3{\romannumeral`&&@\POL@eval@fork
864 #2\PolEvalReducedAt
865 \At\PolEvalReducedAtExpr\krof {#1}{#3}%
866 }%
867 \def\PolEvalReducedAt#1#2{%
868 \xintpraw % in order not to print denominator if the latter equals 1
869 {\xintIrr{\csname XINT_expr_polfunc_#1\endcsname{#2}}[0]}%
870 }%
871 \def\PolEvalReducedAtExpr#1#2{%
872 \xintpraw
873 {\expandafter\xintIrr\romannumeral`&&@\xintthebareeval#1(#2)\relax[0]}%
874 }%
875 %
876 \def\PolFloatEval#1#2#3{\romannumeral`&&@\POL@eval@fork
877 #2\PolFloatEvalAt
878 \At\PolFloatEvalAtExpr\krof {#1}{#3}%
879 }%
880 \def\PolFloatEvalAt#1#2{%
881 \xintpfloat{\csname XINT_flexpr_polfunc_#1\endcsname{#2}}%
882 }%
883 \def\PolFloatEvalAtExpr#1#2{\xintthefloatexpr #1(#2)\relax}%
884 \def\PolLeadingCoeff#1{%
885 \romannumeral`&&@\expandafter\expandafter\expandafter\xintlastitem
886 \expandafter\expandafter\expandafter
887 {\csname POLuserpol@#1\endcsname}%
888 }%
889 %
890 \def\PolNthCoeff#1#2{\romannumeral`&&@%
891 \expandafter\POL@nthcoeff
892 \romannumeral0\xintnthelt{\ifnum\numexpr#2<\z@#2\else(#2)+1\fi}%
893 {\expandafter\expandafter\expandafter
894 \xint_gob_til_dot\csname POLuserpol@#1\endcsname}@%
895 }%
896 \def\POL@nthcoeff#1@{\if @#1@\expandafter\xint_firstoftwo
897 \else\expandafter\xint_secondoftwo\fi
898 {0/1[0]}{#1}}%
899 %
900 % returns -1 for zero polynomial for context of numerical expression
901 % should it return -\infty?
902 \def\PolDegree#1{\romannumeral`&&@\expandafter\expandafter\expandafter
903 \POL@degree\csname POLuserpol@#1\endcsname;}%
904 \def\POL@degree #1.#2;{#1}%
905 %
906 \def\PolToList#1{\romannumeral`&&@\expandafter\expandafter\expandafter
907 \xint_gob_til_dot\csname POLuserpol@#1\endcsname}%
908 %
909 \def\PolToCSV#1{\romannumeral0\xintlistwithsep{, }{\PolToList{#1}}}%
910 %
911 % \PolIContent (0.5)
912 % Why did I call this IContent and not Content? Ah, I see, Maple terminology!
913 % But I realize now I misread in 2018 the Maple doc, its icontent() is the gcd
914 % of all coeffs of a multivariate polynomial. Whereas content(,) second argument
915 % specifies which variable to consider expression as being univariate in it.
916 % Refactored at 0.8 as xint 1.4 has a backported fractional gcd
917 % (itself refactored at 1.4d)
918 \def\POL@icontent#1{\romannumeral0\expandafter\XINT_fgcd_out
919 \romannumeral0\expandafter\XINT_fgcdof\romannumeral`&&@#1^}%
920 % Since xintexpr 1.4d, \xintGCDof always outputs an irreducible fraction A/B.
921 % (with B=1 if A/B integer).
922 \def\PolIContent#1{\xintGCDof{\PolToList{#1}}}%
923 %
924 \def\PolToExprCmd#1{\xintPRaw{\xintRawWithZeros{#1}}}%
925 \def\PolToFloatExprCmd#1{\xintPFloat{#1}}% CHANGED AT 0.8.2! was \xintFloat
926 % \def\PolTypesetCmdPrefix#1{\xintiiifSgn{#1}{}{+}{+}}%
927 \let\PolToExprTermPrefix\PolTypesetCmdPrefix
928 \def\PolToExprOneTermStyleA#1#2{%
929 \ifnum#2=\z@
930 \PolToExprCmd{#1}%
931 \else
932 \xintifOne{\xintiiAbs{#1}}
933 {\xintiiifSgn{#1}{-}{}{}}% + from \PolToExprTermPrefix
934 {\PolToExprCmd{#1}\PolToExprTimes}%
935 \fi
936 \ifcase\xintiiAbs{#2} %<-- space here mandatory
937 \or\PolToExprVar
938 \else\PolToExprVar\PolToExprCaret\xintiiAbs{#2}%
939 \fi
940 }%
941 \let\PolToExprOneTerm\PolToExprOneTermStyleA
942 \def\PolToExprOneTermStyleB#1#2{%
943 \ifnum#2=\z@
944 \xintNumerator{#1}%
945 \else
946 \xintifOne{\xintiiAbs{\xintNumerator{#1}}}
947 {\xintiiifSgn{#1}{-}{}{}}% + from \PolToExprTermPrefix
948 {\xintNumerator{#1}\PolToExprTimes}%
949 \fi
950 \ifcase\xintiiAbs{#2} %<-- space here mandatory
951 \or\PolToExprVar
952 \else\PolToExprVar\PolToExprCaret\xintiiAbs{#2}%
953 \fi
954 \xintiiifOne{\xintDenominator{#1}}{}{/\xintDenominator{#1}}%
955 }%
956 \def\PolToFloatExprOneTerm#1#2{%
957 \ifnum#2=\z@
958 \PolToFloatExprCmd{#1}%
959 \else
960 \PolToFloatExprCmd{#1}\PolToExprTimes
961 \fi
962 \ifcase\xintiiAbs{#2} %<-- space here mandatory
963 \or\PolToExprVar
964 \else\PolToExprVar\PolToExprCaret\xintiiAbs{#2}%
965 \fi
966 }%
967 \def\PolToExprTimes{*}%
968 \def\PolToExprVar{x}%
969 \def\PolToExprInVar{x}%
970 \edef\PolToExprCaret{\string ^}%
971 %%
972 %% \PolToExpr
973 %%
974 %% extended at 0.8 to handle arbitrary expressions on input
975 %%
976 \def\PolToExpr#1{%
977 \if*\noexpand#1\expandafter\xint_firstoftwo\else
978 \expandafter\xint_secondoftwo\fi
979 \PolToExprAscending\PolToExprDescending{#1}}%
980 \def\PolToFloatExpr#1{%
981 \if*\noexpand#1\expandafter\xint_firstoftwo\else
982 \expandafter\xint_secondoftwo\fi
983 \PolToFloatExprAscending\PolToFloatExprDescending{#1}}%
984 \def\PolToExpr@getit#1%
985 {%
986 \ifcsname XINT_expr_varvalue_#1\endcsname
987 \csname XINT_expr_varvalue_#1\expandafter\endcsname
988 \else
989 \expandafter\xint_firstofone\romannumeral0%
990 \xintbareeval subs(#1,\PolToExprInVar=pol([0,1]))\expandafter\relax
991 \fi
992 }%
993 \def\PolToExprAscending#1#2{%
994 \expandafter\POL@toexpr\romannumeral0\PolToExpr@getit{#2}%
995 \PolToExprOneTerm\POL@toexprA
996 }%
997 \def\PolToFloatExprAscending#1#2{%
998 \expandafter\POL@toexpr\romannumeral0\PolToExpr@getit{#2}%
999 \PolToFloatExprOneTerm\POL@toexprA
1000 }%
1001 \def\PolToExprDescending#1{%
1002 \expandafter\POL@toexpr\romannumeral0\PolToExpr@getit{#1}%
1003 \PolToExprOneTerm\POL@toexprD
1004 }%
1005 \def\PolToFloatExprDescending#1{%
1006 \expandafter\POL@toexpr\romannumeral0\PolToExpr@getit{#1}%
1007 \PolToFloatExprOneTerm\POL@toexprD
1008 }%
1009 \def\POL@toexpr#1#2#3{\POL@toexpr@fork#3#2#1\relax}%
1010 \def\POL@toexpr@fork #1#2#3{%
1011 \POL_Pfork
1012 #3\POL@toexpr@pol
1013 P\POL@toexpr@cst
1014 \krof #1#2#3%
1015 }%
1016 \def\POL@toexpr@cst#1#2#3\relax{#2{#3}{0}}%
1017 \def\POL@toexpr@pol#1#2P#3.{#1{#3}#2\empty}%
1018 % now back to legacy pre 0.8 code
1019 \def\POL@toexprA #1#2\empty#3{%
1020 \ifpoltoexprall\expandafter\POL@toexprall@b
1021 \else\expandafter\POL@toexpr@b
1022 \fi {#3}#2{0}1.%
1023 }%
1024 \def\POL@toexprD #1#2#3\relax{% #3 has \empty to prevent brace removal
1025 \expandafter\POL@toexprD@a\expandafter#2%
1026 \the\numexpr #1\expandafter.\romannumeral0\xintrevwithbraces{#3}\relax
1027 }%
1028 \def\POL@toexprD@a #1#2.#3{%
1029 \ifpoltoexprall\expandafter\POL@toexprall@b
1030 \else\expandafter\POL@toexpr@b
1031 \fi{#3}#1{-#2}\the\numexpr\@ne+-#2.%
1032 }%
1033 \def\POL@toexpr@b #1#2#3{%
1034 \xintiiifZero{#1}%
1035 {\expandafter\POL@toexpr@loop\expandafter\POL@toexpr@b}%
1036 {#2{#1}{#3}%
1037 \expandafter\POL@toexpr@loop\expandafter\POL@toexpr@c}%
1038 \expandafter#2%
1039 }%
1040 \def\POL@toexpr@c #1#2#3{%
1041 \xintiiifZero{#1}%
1042 {}%
1043 {\PolToExprTermPrefix{#1}#2{#1}{#3}}%
1044 \expandafter\POL@toexpr@loop\expandafter\POL@toexpr@c
1045 \expandafter#2%
1046 }%
1047 \def\POL@toexprall@b #1#2#3{%
1048 #2{#1}{#3}%
1049 \expandafter\POL@toexpr@loop\expandafter\POL@toexprall@c
1050 \expandafter#2%
1051 }%
1052 \def\POL@toexprall@c #1#2#3{%
1053 \PolToExprTermPrefix{#1}#2{#1}{#3}%
1054 \expandafter\POL@toexpr@loop\expandafter\POL@toexprall@c
1055 \expandafter#2%
1056 }%
1057 \def\POL@toexpr@loop#1#2#3.#4{%
1058 \if\relax#4\expandafter\xint_gob_til_dot\fi
1059 #1{#4}#2{#3}\the\numexpr\@ne+#3.%
1060 }%
1061 \XINTrestorecatcodesendinput%