% -*- sentence-end-double-space: t -*- \documentclass[a4paper,svgnames,dvipsnames,dvipdfmx]{article} \usepackage{geometry} \usepackage{shortvrb} \usepackage{xcolor} \usepackage{graphicx} \usepackage{polexpr} \usepackage{hyperref} \usepackage{bookmark} % \usepackage{amsmath} \usepackage{framed} \usepackage{newtxtext,newtxmath} \title{\pkg{polexpr} root localization examples} \author{Jean-François Burnol} \date{To access the reference documentation:\\ % faut-il vraiment le ./ ? \texttt{texdoc} \href{run:./polexpr.html}{polexpr.html}} %\usepackage{parskip} \MakeShortVerb{\|} % Note 25 juin 2021 et 8 janvier 2022 pour polexpr-examples.tex % % Ceci est extrait quasi verbatim de xint.dtx % % Le code est pour LaTeX, pas pour Plain TeX. % Prévu pour latex+dvipdfmx, et j'ai supprimé ajouts spécifiques pour % pdflatex/xetex/lualatex en ce qui concerne les couleurs et polices. % % Regarding latex+dvipdfmx, a document with many more usage % of everbatim* could run into color stack overflow problems % Refer to xint.dtx for how this problem is avoided there % via direct usage of \special rather than \color in the % \everbatimxprehook % verbatim macros and environments % ================================ % % June 2013, then October 2014. % ----------------------------- % \makeatletter \catcode`_ 11 % some of my verbatim environments do not make the space active (\lverb e.g.). Then % \do@noligs must be modified, \char`#1 must be followed by a space token, else, % the `#1 expansion will swallow one space. \def\do@noligs #1{% \catcode`#1\active \begingroup \lccode`~`#1\relax \lowercase{% \endgroup\def~{\leavevmode\kern\z@\char`#1 }}% } % \lowast % Pas forcément adapté à toutes les polices \def\lowast{\raisebox{-.25\height}{*}} \catcode`* 13 \def\makestarlowast {\let*\lowast\catcode`\*\active}% \catcode`* 12 %--- straight quotes, added (finally...) Nov 2, 2014 %--- obsolete with use of newtxtt 1.05, late 2014 \begingroup\makeatletter \catcode`\'\active \catcode`\`\active \@firstofone {\endgroup \def\makequotesstraight{% assumes textcomp package % à propos textcomp est automatique avec pdflatex depuis Février 2020 \let`\textasciigrave \let'\textquotesingle \catcode39\active \catcode96\active }% } % \verb % ===== % Initially, June 2013, then Sep 9, 2014, and Oct 9-12 2014 % % pour les short verb |...| \def\MicroFont{\ttfamily\makestarlowast\makequotesstraight}% default \def\verb {% \relax \ifmmode\else\leavevmode\null\fi \bgroup \let\do\@makeother \dospecials \@ifstar{\@sverb}% unused {\MicroFont \catcode 32 10 \endlinechar 32 % allows to fetch across line breaks \frenchspacing \@@jfverb}% }% % Note (Oct 12, 2014): in the improbable situation a newlinechar is % found in the ##1, \scantokens will convert this to an end of line in % its "write" phase, which will be then ignored in its "read" phase due % to \endlinechar-1. This also avoids possible creation of \par which % would defeat \@@jfverb@@. Thus it is good. \def\@@jfverb #1{% \ifcat\noexpand#1\noexpand~\catcode`#1\active\fi % No problem with the EOL for the line where the short verb delimiter stands. \def\next ##1#1{% \@vobeyspaces\everyeof{\relax}\endlinechar\m@ne \expandafter\@@jfverb_a\scantokens\expandafter{##1}}% % hack with \@empty to prevent brace stripping if catcodes have been % frozen earlier, like in footnotes. \next \@empty } % We don't want a \discretionary at the very start. % But then an empty argument is forbidden! \def\@@jfverb_a #1{#1\@@jfverb_b } \def\@@jfverb_b #1{\ifx\relax #1% \egroup \else % \penalty\z@, or rather (Oct 11, 2014) but I then adjust the textwidth % precisely: \discretionary{\copy\SoftWrapIcon}{}{}% #1\expandafter\@@jfverb_b\fi } % \SoftWrapIcon box for line-breaking using discretionaries % ========================================================= \DeclareFontFamily{U}{MdSymbolC}{} \DeclareFontShape {U}{MdSymbolC}{m}{n}{<-> MdSymbolC-Regular}{} \newbox\SoftWrapIcon % Emacs/AUCTeX uses very strange comment-like highlighting for \usefont{U}... \def\SetSoftWrapIcon{% \global\setbox\SoftWrapIcon\hb@xt@\z@ {\hb@xt@\fontdimen2\font {\hss{\color{verbsoftwrapiconcolor}% \usefont{U}{MdSymbolC}{m}{n}\char"97}\hss}% \hss}% } \AtBeginDocument {{\ttfamily\SetSoftWrapIcon}}% \catcode`_ 8 \makeatother % everbatim environment % ===================== % October 13-14, 2014 % Verbatim with an \everypar hook, mainly to have background color, followed by % execution of the contents (not limited by a group-scope) \makeatletter \catcode`_ 11 \def\everbatim {\s@everbatim\@everbatim } %! ancienne méthode sans doute motivé par possibilité code ferme un groupe? %! \@namedef{everbatim*}{\s@everbatim\@everbatimx\expandafter %! {\the\newlinechar}} \@namedef{everbatim*}{\s@everbatim\@everbatimx } % Note 25 juin 2021 % On ne peut pas emboîter un everbatim à l'intérieur d'un everbatim % ou un everbatim* à l'intérieur d'un everbatim*... \def\s@everbatim {% % \ineverbtrue \everbatimtop % put there size changes \topsep \z@skip \partopsep \z@skip \itemsep \z@skip \parsep \z@skip \parskip \z@skip \lineskip \z@skip \let\do\@makeother \dospecials \let\do\do@noligs \verbatim@nolig@list \makestarlowast \makequotesstraight \everbatimhook \trivlist \@topsepadd \z@skip \item\relax \leftskip \@totalleftmargin \rightskip \z@skip \parindent \z@ \parfillskip\@flushglue \parskip \z@skip \@@par \def\par{\leavevmode\null\@@par\pagebreak[1]}% \everypar\expandafter{\the\everypar \unpenalty \everbatimeverypar \everypar \expandafter{\the\everypar\everbatimeverypar}% }% \obeylines \@vobeyspaces } \def\everbatimtop {\MacroFont\small}% default \let\everbatimhook\empty \def\everbatimeverypar{\strut {\everbatimbgcolorcmd\vrule\@width\linewidth }% \kern-\linewidth \kern\everbatimindent } \let\everbatimbgcolorcmd\empty \def\everbatimindent {\z@} \begingroup \lccode`X 13 \catcode`X \active \lccode`Y `* % this is because of \makestarlowast. % I have to think whether this is useful: obviously if I were to provide % everbatim and everbatim* in a package I wouldn't do that. \catcode`Y \active \catcode`| 0 \catcode`[ 1 \catcode`] 2 \catcode`* 12 \catcode`{ 12 \catcode`} 12 |catcode`\\ 12 |lowercase[|endgroup% both freezes catcodes and converts X to active ^^M |def|@everbatim #1X#2\end{everbatim}% [#2|end[everbatim]|everbatimbottom ] |def|@everbatimx #1X#2\end{everbatimY}]% {#2\end{everbatim*}% % refactored 2022/01/11, rather than passing \newlinechar value % as was done formerly via everbatim* (see above) and fetching it here as #1 % it is thus assumed executed contents will not terminate a scope \edef\everbatimrestorenewlinechar{\newlinechar\the\newlinechar\relax}% \newlinechar 13 % refactored 2022/01/11: % attention, \parskip set to zero for execution of contents % reason: avoid extra space if everbatim* is in an \item of a list % between verbatim and output of execution, if it starts a paragraph % \vskip-\parskip would be no good in case contents create a display \edef\everbatimrestoreparskip{\parskip\the\parskip}% \parskip\z@skip % execution as LaTeX code of contents \everbatimxprehook \scantokens {#2}% \everbatimrestorenewlinechar \everbatimrestoreparskip \everbatimxposthook }% % L'espace venant du endofline final mis par \scantokens sera inhibé si #3 se % termine par un % ou un \x, etc... \let\everbatimbottom\empty \def\endeverbatim {\if@newlist \leavevmode\fi\endtrivlist } \@namedef{endeverbatim*}{\endeverbatim} % \@namedef{endeverbatim*}{\endeverbatim\aftergroup\everbatimundoparskip} % \def\everbatimundoparskip{\vbox{}\kern-\baselineskip\kern-\parskip} % \let\everbatimundoparskip\empty % rationale: we do not want a group % see xint.dtx for better way avoiding colorstack overflow problem % with latex+dvipdfmx \def\everbatimxprehook {\colorlet{everbsavedcolor}{.}% \everbatimxfgcolorcmd}% \let\everbatimxfgcolorcmd\empty % default \def\everbatimxposthook {\color{everbsavedcolor}} \catcode`_ 8 \makeatother \newcommand\pkg[2][]{\if\relax\detokenize{#1}\relax \href{https://www.ctan.org/pkg/#2}{#2}% \else \href{https://www.ctan.org/pkg/#1}{#2}% \fi } % Colors for \verb and everbatim % \MacroFont and \MicroFont % font size in verbatim blocks % \verb %\colorlet{verbcolor}{DarkCyan} \colorlet{verbcolor}{Maroon} \colorlet{verbsoftwrapiconcolor}{DarkBlue} \def\MicroFont{\ttfamily\color{verbcolor} \makestarlowast\makequotesstraight}% % everbatim/everbatim* \def\everbatimtop{\MacroFont\small} % the \small is not in \MacroFont in case of a document with macrocode (doc.sty) % and some customization is desired %\colorlet{everbatimfgcolor}{Olive} \colorlet{everbatimfgcolor}{DarkBlue} \def\MacroFont{\ttfamily\color{everbatimfgcolor}} %\colorlet{everbatimbgcolor}{WhiteSmoke} %\colorlet{everbatimbgcolor}{Ivory} \colorlet{everbatimbgcolor}{Beige} \def\everbatimbgcolorcmd{\color{everbatimbgcolor}} %\colorlet{everbatimxfgcolor}{MidnightBlue} %\colorlet{everbatimxfgcolor}{OliveDrab} \colorlet{everbatimxfgcolor}{Maroon} \def\everbatimxfgcolorcmd{\color{everbatimxfgcolor}} % Notice that \macrocode uses \macro@font which stores the \MacroFont meaning % in force at \begin{document}. But doc.sty's verbatim uses current \MacroFont % not the meaning at \begin{document}. Comprenne qui pourra... \begin{document} \maketitle The package provides a parser |\poldef| of algebraic polynomial expressions. Once defined, a polynomial is usable by its name either as a numerical function in |\xintexpr/\xinteval|, or for additional polynomial definitions, or as argument to the package macros. % The localization of % real roots to arbitrary precision as well as the determination of all % rational roots is implemented via such macros. % Since release |0.8|, polexpr extends the \pkg{xintexpr} % syntax to recognize % polynomials as a new variable type (and not only as functions). % Functionality which previously was implemented via macros such as the % computation of a greatest common divisor is now available directly in % |\xintexpr|, |\xinteval| or |\poldef| via infix or functional % syntax. This document illustrates root localization via usage of macros such as |\PolToSturm| and |\PolSturmIsolateZeros| which implement the \href{https://en.wikipedia.org/wiki/Sturm%27s_theorem}{Sturm theorem}: \begin{itemize} \item Root localization based on \href{https://en.wikipedia.org/wiki/Sturm%27s_theorem}{Sturm theorem} was added at release |0.4| (2018/02/16). \item Ability to find all rational roots was added at release |0.7.2| (2018/12/09). \end{itemize} As of |0.8| (2021/03/29), \pkg{polexpr} is usable with Plain \TeX\ and not only with \LaTeX. The examples here use most of the time a syntax which works with both. Copying-pasting from |pdf| the example source may lose formatting. Formerly, they were included verbatim in the |html| documentation. Here they are both rendered verbatim and got executed during the \LaTeX\ run which created this |pdf| file, with the output shown after the source code. % Perhaps future releases will implement other approaches, which are known % to be generically computationally more efficient, at least in high % degrees, than the \href{https://en.wikipedia.org/wiki/Sturm%27s_theorem}{Sturm theorem} based approach. This is not % immediate priority though (perhaps support of multivariate polynomials % would be more important feature; or localization of complex roots). Regarding how polynomial coefficients are printed on the typeset page by |\PolTypeset|: \begin{itemize}\def\everbatimtop {\MacroFont}% sans le \small \item The default for |\PolTypesetOne| is to use |\xintTeXsignedFrac| with \LaTeX, |\xintTeXsignedOver| with Plain. See the \pkg{xintexpr} documentation for a description of what these macros do. A sensible definition is: \begin{everbatim} \def\PolTypesetOne#1{\PolDecToString{\xintREZ{#1}}}% \end{everbatim} % le \smallskip est ennuyeux ici It means to use decimal notation, with perhaps a trailing denominator if the argument is a fraction, and will suppress trailing zeros after the decimal mark. \item As these are expandable macros, they are usable to redefine |\PolToExprCmd| as well: \begin{everbatim} \def\PolToExprCmd#1{\PolDecToString{\xintREZ{#1}}}% \end{everbatim} % le \smallskip est ennuyeux ici This will customize the output of |\PolToExpr| (which a priori is destined for writes to external files but may also be used on the typeset page). \end{itemize} With |\xintverbosetrue| in the \TeX\ source extra information relative to the internal data manipulated by the macros will be written to the |.log| file. \begin{framed} Package macros related to root localization create (user-level) new polynomials, or numeric variables, via a naming scheme using the given || as prefix. It is thus advisable to keep this || name-space separate from the one used to name polynomial or scalar variables. \end{framed} \begin{framed} Regrettably all examples here use the condemnable |\PolToSturm{f}{f}| practice which means that internally defined polynomials will use as prefix the original polynomial name. This merge of namespaces may cause overwriting previously defined data and may lead to hard-to-debug problems. \end{framed} \section{A first example} In this example the polynomial is square-free. \begin{everbatim*} \poldef f(x) := x^7 - x^6 - 2x + 1; % \PolToSturm{f}{f} \PolSturmIsolateZeros{f} The \PolTypeset{f} polynomial has \PolSturmNbOfIsolatedZeros{f} distinct real roots which are located in the following intervals: \PolPrintIntervals{f} \end{everbatim*} \begin{everbatim*} Here is the second root with ten more decimal digits: \PolRefineInterval[10]{f}{2} $$\PolSturmIsolatedZeroLeft{f}{2}}| which would find exactly the roots. The steps here retain their interest when one is interested in finding isolating intervals for example to prepare some demonstration of dichotomy method. \begin{everbatim*} \PolDef{Q}{(x-1.050001)(x-1.105001)(x-1.110501)(x-1.111051)} \PolTypeset{Q} \PolToSturm{Q}{Q} % it is allowed to use same prefix for Sturm chain \PolSturmIsolateZeros{Q} \PolPrintIntervals{Q} \end{everbatim*} \begin{everbatim*} \PolRefineInterval*{Q}{1} \PolRefineInterval*{Q}{2} \PolRefineInterval*{Q}{3} \PolRefineInterval*{Q}{4} \PolPrintIntervals{Q} \end{everbatim*} \begin{everbatim*} \PolEnsureIntervalLengths{Q}{-6} \PolPrintIntervals{Q} % finds here all roots exactly \end{everbatim*} \section{The degree nine polynomial with 0.99, 0.999, 0.9999 as triple roots} Define a user command (\pkg[xint]{xinttools} is loaded automatically by \pkg{polexpr}): \begin{everbatim*} \def\showmultiplicities#1{% #1 = "sturmname" \xintFor* ##1 in {\xintSeq{1}{\PolSturmNbOfIsolatedZeros{#1}}}\do{% The multiplicity is \PolSturmIsolatedZeroMultiplicity{#1}{##1} \PolSturmIfZeroExactlyKnown{#1}{##1}% {at the root $x=\PolSturmIsolatedZeroLeft{#1}{##1}$} {for the root such that $\PolSturmIsolatedZeroLeft{#1}{##1}\PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}>% \PolPrintIntervalsPrintLeftEndPoint}% {\impossibleA}}% {\xintifSgn{\PolPrintIntervalsTheRightEndPoint} {\impossibleB}% {\impossibleC}% {0<\PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}<% \PolPrintIntervalsPrintRightEndPoint}}% {\xintifSgn{\PolPrintIntervalsTheRightEndPoint} {\impossibleD}% {\impossibleE}% {\PolPrintIntervalsTheVar_{\PolPrintIntervalsTheIndex}=% \PolPrintIntervalsPrintLeftEndPoint\dots}}% $ }% \PolPrintIntervals{T_15} \end{everbatim*} \end{document}