Debian Codegraph @

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

raw
1 % -*- sentence-end-double-space: t -*-
2 \documentclass[a4paper,svgnames,dvipsnames,dvipdfmx]{article}
3 \usepackage{geometry}
4 \usepackage{shortvrb}
5 \usepackage{xcolor}
6 \usepackage{graphicx}
7 \usepackage{polexpr}
8 \usepackage{hyperref}
9 \usepackage{bookmark}
10 % \usepackage{amsmath}
11 \usepackage{framed}
12 \usepackage{newtxtext,newtxmath}
13 \title{\pkg{polexpr} root localization examples}
14 \author{Jean-François Burnol}
15 \date{To access the reference documentation:\\
16 % faut-il vraiment le ./ ?
17 \texttt{texdoc} \href{run:./polexpr.html}{polexpr.html}}
18 %\usepackage{parskip}
19 \MakeShortVerb{\|}
20
21 % Note 25 juin 2021 et 8 janvier 2022 pour polexpr-examples.tex
22 %
23 % Ceci est extrait quasi verbatim de xint.dtx
24 %
25 % Le code est pour LaTeX, pas pour Plain TeX.
26 % Prévu pour latex+dvipdfmx, et j'ai supprimé ajouts spécifiques pour
27 % pdflatex/xetex/lualatex en ce qui concerne les couleurs et polices.
28 %
29 % Regarding latex+dvipdfmx, a document with many more usage
30 % of everbatim* could run into color stack overflow problems
31 % Refer to xint.dtx for how this problem is avoided there
32 % via direct usage of \special rather than \color in the
33 % \everbatimxprehook
34
35 % verbatim macros and environments
36 % ================================
37 %
38 % June 2013, then October 2014.
39 % -----------------------------
40 %
41 \makeatletter
42 \catcode`_ 11
43 % some of my verbatim environments do not make the space active (\lverb e.g.). Then
44 % \do@noligs must be modified, \char`#1 must be followed by a space token, else,
45 % the `#1 expansion will swallow one space.
46 \def\do@noligs #1{%
47 \catcode`#1\active
48 \begingroup
49 \lccode`~`#1\relax
50 \lowercase{%
51 \endgroup\def~{\leavevmode\kern\z@\char`#1 }}%
52 }
53 % \lowast
54 % Pas forcément adapté à toutes les polices
55 \def\lowast{\raisebox{-.25\height}{*}}
56 \catcode`* 13
57 \def\makestarlowast {\let*\lowast\catcode`\*\active}%
58 \catcode`* 12
59
60
61 %--- straight quotes, added (finally...) Nov 2, 2014
62 %--- obsolete with use of newtxtt 1.05, late 2014
63 \begingroup\makeatletter
64 \catcode`\'\active
65 \catcode`\`\active
66 \@firstofone {\endgroup
67 \def\makequotesstraight{% assumes textcomp package
68 % à propos textcomp est automatique avec pdflatex depuis Février 2020
69 \let`\textasciigrave
70 \let'\textquotesingle
71 \catcode39\active
72 \catcode96\active }%
73 }
74
75 % \verb
76 % =====
77 % Initially, June 2013, then Sep 9, 2014, and Oct 9-12 2014
78 %
79 % pour les short verb |...|
80
81 \def\MicroFont{\ttfamily\makestarlowast\makequotesstraight}% default
82 \def\verb
83 {%
84 \relax \ifmmode\else\leavevmode\null\fi
85 \bgroup
86 \let\do\@makeother \dospecials
87 \@ifstar{\@sverb}% unused
88 {\MicroFont
89 \catcode 32 10 \endlinechar 32 % allows to fetch across line breaks
90 \frenchspacing
91 \@@jfverb}%
92 }%
93 % Note (Oct 12, 2014): in the improbable situation a newlinechar is
94 % found in the ##1, \scantokens will convert this to an end of line in
95 % its "write" phase, which will be then ignored in its "read" phase due
96 % to \endlinechar-1. This also avoids possible creation of \par which
97 % would defeat \@@jfverb@@. Thus it is good.
98 \def\@@jfverb #1{%
99 \ifcat\noexpand#1\noexpand~\catcode`#1\active\fi
100 % No problem with the EOL for the line where the short verb delimiter stands.
101 \def\next ##1#1{%
102 \@vobeyspaces\everyeof{\relax}\endlinechar\m@ne
103 \expandafter\@@jfverb_a\scantokens\expandafter{##1}}%
104 % hack with \@empty to prevent brace stripping if catcodes have been
105 % frozen earlier, like in footnotes.
106 \next \@empty
107 }
108 % We don't want a \discretionary at the very start.
109 % But then an empty argument is forbidden!
110 \def\@@jfverb_a #1{#1\@@jfverb_b }
111
112 \def\@@jfverb_b #1{\ifx\relax #1%
113 \egroup
114 \else
115 % \penalty\z@, or rather (Oct 11, 2014) but I then adjust the textwidth
116 % precisely:
117 \discretionary{\copy\SoftWrapIcon}{}{}%
118 #1\expandafter\@@jfverb_b\fi
119 }
120 % \SoftWrapIcon box for line-breaking using discretionaries
121 % =========================================================
122 \DeclareFontFamily{U}{MdSymbolC}{}
123 \DeclareFontShape {U}{MdSymbolC}{m}{n}{<-> MdSymbolC-Regular}{}
124 \newbox\SoftWrapIcon
125 % Emacs/AUCTeX uses very strange comment-like highlighting for \usefont{U}...
126 \def\SetSoftWrapIcon{%
127 \global\setbox\SoftWrapIcon\hb@xt@\z@
128 {\hb@xt@\fontdimen2\font
129 {\hss{\color{verbsoftwrapiconcolor}%
130 \usefont{U}{MdSymbolC}{m}{n}\char"97}\hss}%
131 \hss}%
132 }
133 \AtBeginDocument {{\ttfamily\SetSoftWrapIcon}}%
134
135 \catcode`_ 8
136 \makeatother
137
138 % everbatim environment
139 % =====================
140
141 % October 13-14, 2014
142 % Verbatim with an \everypar hook, mainly to have background color, followed by
143 % execution of the contents (not limited by a group-scope)
144
145 \makeatletter
146 \catcode`_ 11
147
148 \def\everbatim {\s@everbatim\@everbatim }
149 %! ancienne méthode sans doute motivé par possibilité code ferme un groupe?
150 %! \@namedef{everbatim*}{\s@everbatim\@everbatimx\expandafter
151 %! {\the\newlinechar}}
152 \@namedef{everbatim*}{\s@everbatim\@everbatimx }
153
154 % Note 25 juin 2021
155 % On ne peut pas emboîter un everbatim à l'intérieur d'un everbatim
156 % ou un everbatim* à l'intérieur d'un everbatim*...
157 \def\s@everbatim {%
158 % \ineverbtrue
159 \everbatimtop % put there size changes
160 \topsep \z@skip
161 \partopsep \z@skip
162 \itemsep \z@skip
163 \parsep \z@skip
164 \parskip \z@skip
165 \lineskip \z@skip
166 \let\do\@makeother \dospecials
167 \let\do\do@noligs \verbatim@nolig@list
168 \makestarlowast
169 \makequotesstraight
170 \everbatimhook
171 \trivlist
172 \@topsepadd \z@skip
173 \item\relax
174 \leftskip \@totalleftmargin
175 \rightskip \z@skip
176 \parindent \z@
177 \parfillskip\@flushglue
178 \parskip \z@skip
179 \@@par
180 \def\par{\leavevmode\null\@@par\pagebreak[1]}%
181 \everypar\expandafter{\the\everypar \unpenalty
182 \everbatimeverypar
183 \everypar \expandafter{\the\everypar\everbatimeverypar}%
184 }%
185 \obeylines \@vobeyspaces
186 }
187 \def\everbatimtop {\MacroFont\small}% default
188 \let\everbatimhook\empty
189 \def\everbatimeverypar{\strut
190 {\everbatimbgcolorcmd\vrule\@width\linewidth }%
191 \kern-\linewidth
192 \kern\everbatimindent }
193 \let\everbatimbgcolorcmd\empty
194 \def\everbatimindent {\z@}
195
196 \begingroup
197 \lccode`X 13
198 \catcode`X \active
199 \lccode`Y `* % this is because of \makestarlowast.
200 % I have to think whether this is useful: obviously if I were to provide
201 % everbatim and everbatim* in a package I wouldn't do that.
202 \catcode`Y \active
203 \catcode`| 0 \catcode`[ 1 \catcode`] 2 \catcode`* 12
204 \catcode`{ 12 \catcode`} 12 |catcode`\\ 12
205 |lowercase[|endgroup% both freezes catcodes and converts X to active ^^M
206 |def|@everbatim #1X#2\end{everbatim}%
207 [#2|end[everbatim]|everbatimbottom ]
208 |def|@everbatimx #1X#2\end{everbatimY}]%
209 {#2\end{everbatim*}%
210 % refactored 2022/01/11, rather than passing \newlinechar value
211 % as was done formerly via everbatim* (see above) and fetching it here as #1
212 % it is thus assumed executed contents will not terminate a scope
213 \edef\everbatimrestorenewlinechar{\newlinechar\the\newlinechar\relax}%
214 \newlinechar 13
215 % refactored 2022/01/11:
216 % attention, \parskip set to zero for execution of contents
217 % reason: avoid extra space if everbatim* is in an \item of a list
218 % between verbatim and output of execution, if it starts a paragraph
219 % \vskip-\parskip would be no good in case contents create a display
220 \edef\everbatimrestoreparskip{\parskip\the\parskip}%
221 \parskip\z@skip
222 % execution as LaTeX code of contents
223 \everbatimxprehook
224 \scantokens {#2}%
225 \everbatimrestorenewlinechar
226 \everbatimrestoreparskip
227 \everbatimxposthook
228 }%
229 % L'espace venant du endofline final mis par \scantokens sera inhibé si #3 se
230 % termine par un % ou un \x, etc...
231
232 \let\everbatimbottom\empty
233
234 \def\endeverbatim {\if@newlist \leavevmode\fi\endtrivlist }
235 \@namedef{endeverbatim*}{\endeverbatim}
236 % \@namedef{endeverbatim*}{\endeverbatim\aftergroup\everbatimundoparskip}
237 % \def\everbatimundoparskip{\vbox{}\kern-\baselineskip\kern-\parskip}
238 % \let\everbatimundoparskip\empty
239
240 % rationale: we do not want a group
241 % see xint.dtx for better way avoiding colorstack overflow problem
242 % with latex+dvipdfmx
243 \def\everbatimxprehook {\colorlet{everbsavedcolor}{.}%
244 \everbatimxfgcolorcmd}%
245 \let\everbatimxfgcolorcmd\empty % default
246
247 \def\everbatimxposthook {\color{everbsavedcolor}}
248
249 \catcode`_ 8
250 \makeatother
251
252 \newcommand\pkg[2][]{\if\relax\detokenize{#1}\relax
253 \href{https://www.ctan.org/pkg/#2}{#2}%
254 \else
255 \href{https://www.ctan.org/pkg/#1}{#2}%
256 \fi
257 }
258
259 % Colors for \verb and everbatim
260 % \MacroFont and \MicroFont
261 % font size in verbatim blocks
262
263 % \verb
264 %\colorlet{verbcolor}{DarkCyan}
265 \colorlet{verbcolor}{Maroon}
266 \colorlet{verbsoftwrapiconcolor}{DarkBlue}
267 \def\MicroFont{\ttfamily\color{verbcolor}
268 \makestarlowast\makequotesstraight}%
269
270 % everbatim/everbatim*
271 \def\everbatimtop{\MacroFont\small}
272 % the \small is not in \MacroFont in case of a document with macrocode (doc.sty)
273 % and some customization is desired
274 %\colorlet{everbatimfgcolor}{Olive}
275 \colorlet{everbatimfgcolor}{DarkBlue}
276 \def\MacroFont{\ttfamily\color{everbatimfgcolor}}
277
278 %\colorlet{everbatimbgcolor}{WhiteSmoke}
279 %\colorlet{everbatimbgcolor}{Ivory}
280 \colorlet{everbatimbgcolor}{Beige}
281 \def\everbatimbgcolorcmd{\color{everbatimbgcolor}}
282
283 %\colorlet{everbatimxfgcolor}{MidnightBlue}
284 %\colorlet{everbatimxfgcolor}{OliveDrab}
285 \colorlet{everbatimxfgcolor}{Maroon}
286 \def\everbatimxfgcolorcmd{\color{everbatimxfgcolor}}
287
288 % Notice that \macrocode uses \macro@font which stores the \MacroFont meaning
289 % in force at \begin{document}. But doc.sty's verbatim uses current \MacroFont
290 % not the meaning at \begin{document}. Comprenne qui pourra...
291
292 \begin{document}
293 \maketitle
294
295
296 The package provides a parser |\poldef| of algebraic polynomial
297 expressions.
298
299 Once defined, a polynomial is usable by its name either as a numerical
300 function in |\xintexpr/\xinteval|, or for additional polynomial
301 definitions, or as argument to the package macros.
302
303 % The localization of
304 % real roots to arbitrary precision as well as the determination of all
305 % rational roots is implemented via such macros.
306
307 % Since release |0.8|, polexpr extends the \pkg{xintexpr}
308 % syntax to recognize
309 % polynomials as a new variable type (and not only as functions).
310 % Functionality which previously was implemented via macros such as the
311 % computation of a greatest common divisor is now available directly in
312 % |\xintexpr|, |\xinteval| or |\poldef| via infix or functional
313 % syntax.
314
315 This document illustrates root localization via usage of macros such as
316 |\PolToSturm| and |\PolSturmIsolateZeros| which implement the
317 \href{https://en.wikipedia.org/wiki/Sturm%27s_theorem}{Sturm theorem}:
318 \begin{itemize}
319 \item Root localization based on
320 \href{https://en.wikipedia.org/wiki/Sturm%27s_theorem}{Sturm theorem} was
321 added at release |0.4| (2018/02/16).
322 \item Ability to find all rational roots was added at release |0.7.2|
323 (2018/12/09).
324 \end{itemize}
325 As of |0.8| (2021/03/29), \pkg{polexpr} is usable with Plain \TeX\ and not
326 only with \LaTeX. The examples here use most of the time a syntax which works
327 with both.
328
329 Copying-pasting from |pdf| the example source may lose formatting.
330 Formerly, they were included verbatim in the |html| documentation. Here
331 they are both rendered verbatim and got executed during the \LaTeX\ run
332 which created this |pdf| file, with the output shown after the source code.
333
334 % Perhaps future releases will implement other approaches, which are known
335 % to be generically computationally more efficient, at least in high
336 % degrees, than the \href{https://en.wikipedia.org/wiki/Sturm%27s_theorem}{Sturm theorem} based approach. This is not
337 % immediate priority though (perhaps support of multivariate polynomials
338 % would be more important feature; or localization of complex roots).
339
340 Regarding how polynomial coefficients are printed on the typeset page by
341 |\PolTypeset|:
342 \begin{itemize}\def\everbatimtop {\MacroFont}% sans le \small
343 \item The default for |\PolTypesetOne| is to use |\xintTeXsignedFrac| with
344 \LaTeX, |\xintTeXsignedOver| with Plain. See the \pkg{xintexpr}
345 documentation for a description of what these macros do. A sensible
346 definition is:
347 \begin{everbatim}
348 \def\PolTypesetOne#1{\PolDecToString{\xintREZ{#1}}}%
349 \end{everbatim}
350 % le \smallskip est ennuyeux ici
351 It means to use decimal notation, with perhaps a trailing denominator if the
352 argument is a fraction, and will suppress trailing zeros after the decimal
353 mark.
354
355 \item
356 As these are expandable macros, they are usable to redefine
357 |\PolToExprCmd| as well:
358 \begin{everbatim}
359 \def\PolToExprCmd#1{\PolDecToString{\xintREZ{#1}}}%
360 \end{everbatim}
361 % le \smallskip est ennuyeux ici
362 This will customize the output of |\PolToExpr| (which a priori is
363 destined for writes to external files but may also be used on the typeset
364 page).
365 \end{itemize}
366
367 With |\xintverbosetrue| in the \TeX\ source extra information relative
368 to the internal data manipulated by the macros will be written to the |.log|
369 file.
370
371 \begin{framed}
372 Package macros related to root localization create (user-level) new
373 polynomials, or numeric variables, via a naming scheme using the given
374 |<sturmname>| as prefix. It is thus advisable to keep this
375 |<sturmname>| name-space separate from the one used to name polynomial or
376 scalar variables.
377 \end{framed}
378
379 \begin{framed}
380 Regrettably all examples here use the condemnable |\PolToSturm{f}{f}|
381 practice which means that internally defined polynomials will use as prefix
382 the original polynomial name. This merge of namespaces may cause
383 overwriting previously defined data and may lead to hard-to-debug problems.
384 \end{framed}
385
386
387 \section{A first example}
388
389 In this example the polynomial is square-free.
390 \begin{everbatim*}
391 \poldef f(x) := x^7 - x^6 - 2x + 1;
392 %
393 \PolToSturm{f}{f}
394 \PolSturmIsolateZeros{f}
395 The \PolTypeset{f} polynomial has \PolSturmNbOfIsolatedZeros{f} distinct real
396 roots which are located in the following intervals:
397 \PolPrintIntervals{f}
398 \end{everbatim*}
399
400 \begin{everbatim*}
401 Here is the second root with ten more decimal digits:
402 \PolRefineInterval[10]{f}{2}
403 $$\PolSturmIsolatedZeroLeft{f}{2}<Z_2<\PolSturmIsolatedZeroRight{f}{2}$$
404 \end{everbatim*}
405
406 \begin{everbatim*}
407 And here is the first root with twenty digits after decimal mark:
408 \PolEnsureIntervalLength{f}{1}{-20}
409 $$\PolSturmIsolatedZeroLeft{f}{1}<Z_1<\PolSturmIsolatedZeroRight{f}{1}$$
410 \end{everbatim*}
411
412 \begin{everbatim*}
413 The first element of the Sturm chain has degree $\PolDegree{f_0}$. As
414 this is the original degreee $\PolDegree{f}$ we know that $f$ is square free.
415 Its derivative is up to a constant \PolTypeset{f_1} (in this example
416 it is identical with it).
417 \PolToSturm{f_1}{f_1}\PolSturmIsolateZeros{f_1}%
418 The derivative has \PolSturmNbOfIsolatedZeros{f_1} distinct real
419 roots:
420 \PolPrintIntervals[W]{f_1}%
421 \PolEnsureIntervalLengths{f_1}{-10}%
422 Here they are with ten digits after decimal mark:
423 \PolPrintIntervals[W]{f_1}
424 \end{everbatim*}
425
426 \begin{everbatim*}
427 \PolDiff{f_1}{f''}
428 \PolToSturm{f''}{f''}
429 \PolSturmIsolateZeros{f''}
430 The second derivative is \PolTypeset{f''}.
431 It has \PolSturmNbOfIsolatedZeros{f''} distinct real
432 roots:
433 \PolPrintIntervals[X]{f''}%
434 Here is the positive one with 20 digits after decimal mark:
435 \PolEnsureIntervalLength{f''}{2}{-20}%
436 $$X_2 = \PolSturmIsolatedZeroLeft{f''}{2}\dots$$
437 \end{everbatim*}
438 %The more mathematically advanced among our dear readers will be able
439 % to give the exact value for $X_2$!
440
441 \section{A degree four polynomial with nearby roots}
442
443
444 Notice that this example is a bit outdated as |0.7| release has
445 added |\PolSturmIsolateZeros**{<sturmname>}| which would find exactly
446 the roots. The steps here retain their interest when one is interested
447 in finding isolating intervals for example to prepare some demonstration
448 of dichotomy method.
449
450
451 \begin{everbatim*}
452 \PolDef{Q}{(x-1.050001)(x-1.105001)(x-1.110501)(x-1.111051)}
453 \PolTypeset{Q}
454 \PolToSturm{Q}{Q} % it is allowed to use same prefix for Sturm chain
455 \PolSturmIsolateZeros{Q}
456 \PolPrintIntervals{Q}
457 \end{everbatim*}
458
459 \begin{everbatim*}
460 \PolRefineInterval*{Q}{1}
461 \PolRefineInterval*{Q}{2}
462 \PolRefineInterval*{Q}{3}
463 \PolRefineInterval*{Q}{4}
464 \PolPrintIntervals{Q}
465 \end{everbatim*}
466
467 \begin{everbatim*}
468 \PolEnsureIntervalLengths{Q}{-6}
469 \PolPrintIntervals{Q}
470 % finds here all roots exactly
471 \end{everbatim*}
472
473 \section{The degree nine polynomial with 0.99, 0.999, 0.9999 as triple roots}
474
475 Define a user command (\pkg[xint]{xinttools} is loaded automatically by
476 \pkg{polexpr}):
477
478 \begin{everbatim*}
479 \def\showmultiplicities#1{% #1 = "sturmname"
480 \xintFor* ##1 in {\xintSeq{1}{\PolSturmNbOfIsolatedZeros{#1}}}\do{%
481 The multiplicity is \PolSturmIsolatedZeroMultiplicity{#1}{##1}
482 \PolSturmIfZeroExactlyKnown{#1}{##1}%
483 {at the root $x=\PolSturmIsolatedZeroLeft{#1}{##1}$}
484 {for the root such that
485 $\PolSturmIsolatedZeroLeft{#1}{##1}<x<\PolSturmIsolatedZeroRight{#1}{##1}$}
486 \par
487 }}%
488 \end{everbatim*}
489
490 \begin{everbatim*}
491 \PolDef{f}{(x-0.99)^3(x-0.999)^3(x-0.9999)^3}
492 \def\PolTypesetOne#1{\PolDecToString{\xintREZ{#1}}}
493 \PolTypeset{f}\par
494 \end{everbatim*}
495
496 \begin{everbatim*}
497 \PolToSturm{f}{f}% it is allowed to use "polname" as "sturmname" too
498 \PolSturmIsolateZerosAndGetMultiplicities{f}% use the "sturmname" here
499 % or \PolSturmIsolateZeros*{f} which is exactly the same, but shorter..
500
501 \showmultiplicities{f}
502 \end{everbatim*}
503 % In this example, the output will look like this (but using math mode)::
504
505 % x^9 - 8.9667x^8 + 35.73400293x^7 - 83.070418400109x^6 + 124.143648875193123x^5
506 % - 123.683070924326075877x^4 + 82.149260397553075617891x^3
507 % - 35.07602992699900159127007x^2 + 8.7364078733314648368671733x
508 % - 0.967100824643585986488103299
509
510 % The multiplicity is 3 at the root x = 0.99
511 % The multiplicity is 3 at the root x = 0.999
512 % The multiplicity is 3 at the root x = 0.9999
513
514 % On first pass, these rational roots were found (due to their relative
515 % magnitudes, using |\PolSturmIsolateZeros**| was not needed here). But
516 % multiplicity computation works also with (decimal) roots not yet
517 % identified or with non-decimal or irrational roots.
518
519 It is fun to modify only a tiny bit the polynomial and see if polexpr
520 survives:
521
522 \begin{everbatim*}
523 \PolDef{g}{f(x)+1e-27}
524 \PolTypeset{g}\par
525 \PolToSturm{g}{g}
526 \PolSturmIsolateZeros*{g}
527
528 \showmultiplicities{g}
529 \end{everbatim*}
530 % This produces::
531
532 % x^9 - 8.9667x^8 + 35.73400293x^7 - 83.070418400109x^6 + 124.143648875193123x^5
533 % - 123.683070924326075877x^4 + 82.149260397553075617891x^3
534 % - 35.07602992699900159127007x^2 + 8.7364078733314648368671733x
535 % - 0.967100824643585986488103298
536
537 % The multiplicity is 1 for the root such that 0.98 < x < 0.99
538 % The multiplicity is 1 for the root such that 0.9991 < x < 0.9992
539 % The multiplicity is 1 for the root such that 0.9997 < x < 0.9998
540
541 This means that the multiplicity-3 roots each became a real and a pair of
542 complex ones. Let's see them better:
543
544 \begin{everbatim*}
545 \PolEnsureIntervalLengths{g}{-10}
546
547 \showmultiplicities{g}
548 \end{everbatim*}
549 % which produces::
550
551 % The multiplicity is 1 for the root such that 0.9899888032 < x < 0.9899888033
552 % The multiplicity is 1 for the root such that 0.9991447980 < x < 0.9991447981
553 % The multiplicity is 1 for the root such that 0.9997663986 < x < 0.9997663987
554
555 \section{A degree five polynomial with three rational roots}
556
557 \begin{everbatim*}
558 \poldef Q(x) := 1581755751184441 x^5
559 -14907697165025339 x^4
560 +48415668972339336 x^3
561 -63952057791306264 x^2
562 +46833913221154895 x
563 -49044360626280925;
564
565 \PolToSturm{Q}{Q}
566 \def\PolTypesetCmdPrefix#1{\allowbreak\xintiiifSgn{#1}{}{+}{+}}%
567 $Q_0(x) = \PolTypeset{Q_0}$
568 \PolSturmIsolateZeros**{Q}
569 \PolPrintIntervals{Q}
570
571 $Q_{norr}(x) = \PolTypeset{Q_norr}$
572 \end{everbatim*}
573
574 Here, all real roots are rational. Let's get their decimal expansion too:
575 \begin{everbatim*}
576 \begingroup
577 % print decimal expansion of the found roots
578 \def\PolPrintIntervalsPrintExactZero
579 {\xintTrunc{20}{\PolPrintIntervalsTheLeftEndPoint}\dots}
580 \PolPrintIntervals{Q}
581 \endgroup % we localized the modified \PolPrintIntervalsPrintExactZero
582 \end{everbatim*}
583
584
585 % Z_1 = 3.14159265358107777120...
586 % Z_2 = 3.14159265358979340254...
587 % Z_3 = 3.14159292035398230088...
588
589 \section{A Mignotte type polynomial}
590
591
592 \begin{everbatim*}
593 \PolDef{P}{x^10 - (10x-1)^2}%
594 \PolTypeset{P} % prints it in expanded form
595 \PolToSturm{P}{P} % we can use same prefix for Sturm chain
596 \PolSturmIsolateZeros{P} % finds 4 real roots
597 This polynomial has \PolSturmNbOfIsolatedZeros{P} distinct real roots:
598 \PolPrintIntervals{P}%
599 \end{everbatim*}
600 Let us refine the second and third intervals to separate the corresponding
601 roots:
602 \begin{everbatim*}
603 \PolRefineInterval*{P}{2}% will refine to 0.0999990 < Z_2 < 0.0999991
604 \PolRefineInterval*{P}{3}% will refine to 0.100001 < Z_3 < 0.100002
605 \PolPrintIntervals{P}%
606 \end{everbatim*}
607 Let us now get to know all roots with 10 digits after decimal mark:
608 \begin{everbatim*}
609 \PolEnsureIntervalLengths{P}{-10}%
610 \PolPrintIntervals{P}% now all roots are known 10 decimal digits after mark
611 \end{everbatim*}
612 Finally, we display 20 digits of the second root:
613 \begin{everbatim*}
614 \PolEnsureIntervalLength{P}{2}{-20}% makes Z_2 known with 20 digits after mark
615 $$\PolSturmIsolatedZeroLeft{P}{2}<Z_2<\PolSturmIsolatedZeroRight{P}{2}$$
616 \end{everbatim*}
617 % The last line produces::
618
619 % 0.09999900004999650028 < Z_2 < 0.09999900004999650029
620
621 \section{The Wilkinson polynomial}
622
623
624 See \url{https://en.wikipedia.org/wiki/Wilkinson%27s_polynomial}.
625
626
627 \begin{everbatim*}
628 %\xintverbosetrue % for the curious...
629
630 \poldef f(x) := mul((x - i), i = 1..20);
631
632 \def\PolTypesetCmdPrefix#1{\allowbreak\xintiiifSgn{#1}{}{+}{+}}%
633 \def\PolTypesetOne#1{\xintDecToString{#1}}%
634
635 \noindent\PolTypeset{f}
636
637 \PolToSturm{f}{f}
638 \PolSturmIsolateZeros{f}
639 \PolPrintIntervals{f}
640 % \vfill\eject
641
642 % This page is commented out because it takes about 30s on a 2GHz CPU
643 % \poldef g(x) := f(x) - 2**{-23} x**19;
644
645 % \PolToSturm{g}{g}
646 % \noindent\PolTypeset{g_0}% integer coefficient primitive polynomial
647
648 % \PolSturmIsolateZeros{g}
649 % \PolEnsureIntervalLengths{g}{-10}
650
651 % \PolPrintIntervals*{g}
652 \end{everbatim*}
653
654 The first polynomial
655 %
656 % f(x) = x**20
657 % - 210 x**19
658 % + 20615 x**18
659 % - 1256850 x**17
660 % + 53327946 x**16
661 % - 1672280820 x**15
662 % + 40171771630 x**14
663 % - 756111184500 x**13
664 % + 11310276995381 x**12
665 % - 135585182899530 x**11
666 % + 1307535010540395 x**10
667 % - 10142299865511450 x**9
668 % + 63030812099294896 x**8
669 % - 311333643161390640 x**7
670 % + 1206647803780373360 x**6
671 % - 3599979517947607200 x**5
672 % + 8037811822645051776 x**4
673 % - 12870931245150988800 x**3
674 % + 13803759753640704000 x**2
675 % - 8752948036761600000 x
676 % + 2432902008176640000
677 %
678 is handled fast enough, but the modified one |f(x) -
679 2**-23 x**19| takes about 20x longer.
680
681 Its Sturm chain polynomials
682 have integer coefficients with up to 321 digits, whereas (surprisingly
683 perhaps) those of the Sturm chain polynomials derived from |f| never
684 have more than 21 digits ...
685
686 Once the Sturm chain is computed and the zeros isolated, obtaining their
687 decimal digits is relatively faster. Here are the ten real roots of
688 |f(x) - 2**-23 x**19| which would be computed by the commented-out code above:
689 \begin{everbatim}
690 Z_1 = 0.9999999999...
691 Z_2 = 2.0000000000...
692 Z_3 = 2.9999999999...
693 Z_4 = 4.0000000002...
694 Z_5 = 4.9999999275...
695 Z_6 = 6.0000069439...
696 Z_7 = 6.9996972339...
697 Z_8 = 8.0072676034...
698 Z_9 = 8.9172502485...
699 Z_10 = 20.8469081014...
700 \end{everbatim}
701
702 \section{The second Wilkinson polynomial}
703
704
705 \begin{everbatim*}
706 \poldef f(x) := mul(x - 2^-i, i = 1..20);
707
708 %\PolTypeset{f}
709
710 \PolToSturm{f}{f}
711 \PolSturmIsolateZeros**{f}
712 \PolPrintIntervals{f}
713 \end{everbatim*}
714
715 This takes more time than the polynomial with 1, 2, .., 20 as roots but
716 less than the latter modified by the |2**-23| tiny change to one of its
717 coefficient.
718
719 % Here is the output (with release 0.7.2)::
720
721 % Z_1 = 0.00000095367431640625
722 % Z_2 = 0.0000019073486328125
723 % Z_3 = 0.000003814697265625
724 % Z_4 = 0.00000762939453125
725 % Z_5 = 0.0000152587890625
726 % Z_6 = 0.000030517578125
727 % Z_7 = 0.00006103515625
728 % Z_8 = 0.0001220703125
729 % Z_9 = 1/4096
730 % Z_10 = 1/2048
731 % Z_11 = 1/1024
732 % Z_12 = 1/512
733 % Z_13 = 1/256
734 % Z_14 = 1/128
735 % Z_15 = 0.015625
736 % Z_16 = 0.03125
737 % Z_17 = 0.0625
738 % Z_18 = 0.125
739 % Z_19 = 0.25
740 % Z_20 = 0.5
741
742 There is some incoherence in output format which has its source in the
743 fact that some roots are found in branches which can only find decimal
744 roots, whereas some are found in branches which could find general
745 fractions and they use |\xintIrr| before storage of the found root.
746 This may evolve in future.
747
748
749 \section{The degree 41 polynomial with -2, -1.9, -1.8, ..., 0, 0.1, ..., 1.9, 2 as roots}
750
751
752
753 \begin{everbatim*}
754 \PolDef{P}{mul((x-i*1e-1), i=-20..20)}% i/10 is same but less efficient
755 \end{everbatim*}
756 In the defining expression we could have used |i/10| but this gives
757 less efficient internal form for the coefficients (the |10|'s end up
758 in denominators).
759
760 \begin{everbatim*}
761 \begingroup
762 \def\PolToExprCmd#1{\PolDecToString{\xintREZ{#1}}}
763 \def\PolToExprTermPrefix#1{\newline\xintiiifSgn{#1}{}{+}{+}}
764 \def\PolToExprTimes{${}\cdot{}$}
765 \ttfamily
766 \PolToExpr{P}
767 \endgroup
768 \end{everbatim*}
769 % x^41
770 % -28.7*x^39
771 % +375.7117*x^37
772 % -2975.11006*x^35
773 % +15935.28150578*x^33
774 % -61167.527674162*x^31
775 % +173944.259366417394*x^29
776 % -373686.963560544648*x^27
777 % +613012.0665016658846445*x^25
778 % -771182.31133138163125495*x^23
779 % +743263.86672885754888959569*x^21
780 % -545609.076599482896371978698*x^19
781 % +301748.325708943677229642930528*x^17
782 % -123655.8987669450434698869844544*x^15
783 % +36666.1782054884005855608205864192*x^13
784 % -7607.85821367459445649518380016128*x^11
785 % +1053.15135918687298508885950223794176*x^9
786 % -90.6380005918141132650786081964032*x^7
787 % +4.33701563847327366842552218288128*x^5
788 % -0.0944770968420804735498178265088*x^3
789 % +0.00059190121813899276854174416896*x
790
791 which shows coefficients with up to 36 significant digits...
792
793 Stress test: not a hard challenge to \pkg{xint} + \pkg{polexpr}, but be a bit
794 patient!
795
796 \begin{everbatim*}
797 \PolDef{P}{mul((x-i*1e-1), i=-20..20)}%
798 \PolToSturm{P}{S} % dutifully computes S_0, ..., S_{41}
799 % the [1] optional argument limits the search to interval (-10,10)
800 \PolSturmIsolateZeros[1]{S} % finds *exactly* (but a bit slowly) all 41 roots!
801 \PolPrintIntervals{S} % nice, isn't it?
802 % Unfortunately \PolPrintIntervals uses a non-breakable array environment
803 % But see next section on how to customize \PolPrintIntervals and let it
804 % allow pagebreaks
805 \end{everbatim*}
806
807 \begin{quote}
808 Release |0.5| has \emph{experimental} addition of optional argument |E| to
809 |\PolSturmIsolateZeros|. It instructs to search roots only in interval
810 |(-10^E, 10^E)|. Important: the extremities are \emph{assumed to not be roots}.
811 In this example, the |[1]| in |\PolSturmIsolateZeros[1]{S}| gives some speed
812 gain; without it, it turns out in this case that \pkg{polexpr} would have
813 started with |(-10^6, 10^6)| interval.
814
815 Please note that this feature may be removed or modified.
816 \end{quote}
817
818 \section{Roots of a Chebyshev polynomial}
819
820
821 \begin{everbatim*}
822 \poldef T_0(x) := 1;
823 \poldef T_1(x) := x;
824 \catcode`@ 11
825 \count@ 2
826 \xintloop
827 \poldef T_\the\count@(x) :=
828 2x*T_\the\numexpr\count@-1\relax
829 - T_\the\numexpr\count@-2\relax;
830 \ifnum\count@<15
831 \advance\count@ 1
832 \repeat
833 \catcode`@ 12
834
835 $$T_{15} = \PolTypeset[X]{T_15}$$
836 \PolToSturm{T_15}{T_15}
837 \PolSturmIsolateZeros*{T_15}% "*" as we will want to confirm multiplicity one
838 % takes time (each next decimal digit is obtained by dichotomy)
839 \PolEnsureIntervalLengths{T_15}{-20}% ensure 20 decimal digits for each root
840 \end{everbatim*}
841
842 Here is now an example of customization. Indeed
843 |\PolPrintIntervals| default uses |array| and thus does not allow page breaks.
844 And it uses |Left < Z < Right| as presentation of roots and we would like here
845 rather |Z = decimal expansion...|.
846 \begin{everbatim*}
847 % 0.8.6 adds an internal patch which would allow usage of amsmath environments
848 % like this:
849 % \def\PolPrintIntervalsBeginEnv{\begin{align*}}
850 % \def\PolPrintIntervalsEndEnv{\end{align*}}
851 % (the problem was that align evaluates twice its contents so global variables
852 % need a reset at the end of first pass, which is what 0.8.6 took care of)
853 %
854 % Let's simply do this:
855 \def\PolPrintIntervalsBeginEnv{\begingroup\leftskip3cm\relax}
856 \def\PolPrintIntervalsEndEnv{\par\endgroup}
857 %
858 % The rows are separated by \PolPrintIntervalsRowSeparator which defaults to \\
859 % with LaTeX and \cr with Plain. (prior to 0.8.6 it was hardcoded)
860 \def\PolPrintIntervalsRowSeparator{\\[\jot]}
861 % And we enter math mode manually at each row, copying pasting from package
862 % defaults with some added mathon/mathoff:
863 \def\PolPrintIntervalsKnownRoot{%
864 $\PolPrintIntervalsPrintMultiplicity\quad
865 \PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}=%
866 \PolPrintIntervalsPrintExactZero
867 $
868 }%
869 \def\PolPrintIntervalsUnknownRoot{%
870 $\PolPrintIntervalsPrintMultiplicity\quad
871 \xintifSgn{\PolPrintIntervalsTheLeftEndPoint}%
872 {\xintifSgn{\PolPrintIntervalsTheRightEndPoint}
873 {\PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}=%
874 \PolPrintIntervalsPrintRightEndPoint\dots}%
875 {0>\PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}>%
876 \PolPrintIntervalsPrintLeftEndPoint}%
877 {\impossibleA}}%
878 {\xintifSgn{\PolPrintIntervalsTheRightEndPoint}
879 {\impossibleB}%
880 {\impossibleC}%
881 {0<\PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}<%
882 \PolPrintIntervalsPrintRightEndPoint}}%
883 {\xintifSgn{\PolPrintIntervalsTheRightEndPoint}
884 {\impossibleD}%
885 {\impossibleE}%
886 {\PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}=%
887 \PolPrintIntervalsPrintLeftEndPoint\dots}}%
888 $
889 }%
890 \PolPrintIntervals{T_15}
891 \end{everbatim*}
892 \end{document}
893