| 1 |
% \iffalse |
| 2 |
% |
| 3 |
% The files polynom.dtx and polynom.ins and all files generated |
| 4 |
% from these two files are referred to as `this work'. |
| 5 |
% |
| 6 |
% This work is copyright 2000-2017 Carsten Heinz, Hendri Adriaens. |
| 7 |
% |
| 8 |
% This work may be distributed and/or modified under the conditions |
| 9 |
% of the LaTeX Project Public License, either version 1.3 of this |
| 10 |
% license or (at your option) any later version. |
| 11 |
% The latest version of this license is in |
| 12 |
% http://www.latex-project.org/lppl.txt |
| 13 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 14 |
% version 2003/12/01 or later. |
| 15 |
% |
| 16 |
% This work has the LPPL maintenance status "maintained". |
| 17 |
% |
| 18 |
% The Current Maintainer of this work is Hendri Adriaens. |
| 19 |
% |
| 20 |
%<*driver> |
| 21 |
\documentclass{ltxdoc} |
| 22 |
\usepackage{hyperref,polynom} |
| 23 |
|
| 24 |
\DisableCrossrefs |
| 25 |
\OnlyDescription |
| 26 |
|
| 27 |
\begin{document} |
| 28 |
\DocInput{polynom.dtx} |
| 29 |
\end{document} |
| 30 |
%</driver> |
| 31 |
% \fi |
| 32 |
% |
| 33 |
%^^A |
| 34 |
%^^A Some definitions used for documentation. |
| 35 |
%^^A |
| 36 |
% \let\packagename\textsf |
| 37 |
% \newenvironment{describe}{\trivlist\item[]}{\endtrivlist} |
| 38 |
% \makeatletter |
| 39 |
% \def\fps@figure{htbp} |
| 40 |
% \let\c@table\c@figure |
| 41 |
% \let\fps@table\fps@figure |
| 42 |
% \makeatother |
| 43 |
%^^A |
| 44 |
%^^A end of these definitions |
| 45 |
%^^A |
| 46 |
% |
| 47 |
%\newbox\abstractbox |
| 48 |
%\setbox\abstractbox=\vbox{ |
| 49 |
% \begin{abstract} |
| 50 |
% The \packagename{polynom} package implements macros for manipulating |
| 51 |
% polynomials. For example, it can typeset polynomial long divisions and |
| 52 |
% synthetic divisions (Horner's scheme), which can be shown step by step. |
| 53 |
% The main test case and application is the polynomial ring in one variable |
| 54 |
% with rational coefficients. |
| 55 |
% \emph{Please note that this is work in progress. Multivariate polynomials |
| 56 |
% are \emph{currently} not supported.} |
| 57 |
% \end{abstract}} |
| 58 |
% |
| 59 |
% \title{The \packagename{Polynom} Package} |
| 60 |
% \author{Copyright 2000--2017\\ Carsten Heinz \texttt{<\,[email protected]\,>}, Hendri Adriaens} |
| 61 |
% \date{2017/07/17\enspace Version 0.19\\ \box\abstractbox} |
| 62 |
% \maketitle |
| 63 |
% \section{Preface} |
| 64 |
% |
| 65 |
%Because Carsten Heinz could not be reached anymore for a long time, |
| 66 |
%this package has been taken over according to the LPPL for |
| 67 |
%maintenance by Hendri Adriaens. This package was using and |
| 68 |
%redefining internals of the \packagename{keyval} package and hence |
| 69 |
%it was incompatible with \packagename{xkeyval}. This problem has |
| 70 |
%been solved and the processing of the \texttt{vars} key has been |
| 71 |
%simplified. All following text is the original by Carsten Heinz. |
| 72 |
%\hfill\emph{Hendri Adriaens, 2006/04/20}\\ |
| 73 |
%We thank Walter Daems for providing the \texttt D style.\hfill\emph{2016/12/09}\\ |
| 74 |
%And Hendrik Vogt for providing a bug fix on \texttt{\textbackslash polylongdiv}.\hfill\emph{2017/07/17} |
| 75 |
% |
| 76 |
% \section{Introduction} |
| 77 |
% |
| 78 |
% Donald Arseneau has contributed a lot of packages to the \TeX\ community. |
| 79 |
% In particular, he posted macros for long division on \texttt{comp.text.tex}, |
| 80 |
% which were also published in the TUGboat \cite{TUGboat} and eventually as |
| 81 |
% \texttt{longdiv.tex} on CTAN. The \packagename{polynom} package allows to do |
| 82 |
% the job with polynomials, see figure~\ref{division}. There you can also |
| 83 |
% see an example of Horner's scheme for synthetic division. |
| 84 |
% \begin{figure} |
| 85 |
% \centering |
| 86 |
% \begin{minipage}{.42\linewidth} |
| 87 |
% \[\polylongdiv{(X-1)(X^2+2X+2)+1}{X-1}\] |
| 88 |
% \end{minipage} |
| 89 |
% \hfil |
| 90 |
% \begin{minipage}{.5\linewidth} |
| 91 |
% \[\polyhornerscheme[x=1]{x^3+x^2-1}\] |
| 92 |
% \end{minipage} |
| 93 |
% |
| 94 |
% \begin{minipage}{.42\linewidth} |
| 95 |
% \centering |\polylongdiv{X^3+X^2-1}{X-1}| |
| 96 |
% \end{minipage} |
| 97 |
% \hfil |
| 98 |
% \begin{minipage}{.5\linewidth} |
| 99 |
% \centering |\polyhornerscheme[x=1]{x^3+x^2-1}| |
| 100 |
% \end{minipage} |
| 101 |
% \caption{Polynomial long division and synthetic division. The commands both |
| 102 |
% are able to generate partial output, see \href{polydemo.pdf}{polydemo.pdf} |
| 103 |
% in fullscreen mode.} |
| 104 |
% \label{division} |
| 105 |
% \end{figure} |
| 106 |
% |
| 107 |
% \begin{figure} |
| 108 |
% \[\polylonggcd {(X-1)(X-1)(X^2+1)} {(X-1)(X+1)(X+1)}\] |
| 109 |
% \centering |\polylonggcd {(X-1)(X-1)(X^2+1)} {(X-1)(X+1)(X+1)}| |
| 110 |
% \caption{Euclidean algorithm with polynomials; the last nonzero remainder |
| 111 |
% is a greatest common divisor. In the case here, it is uniquely |
| 112 |
% determined up to a scalar factor, so \(X-1\) and \(\frac49X-\frac49\) |
| 113 |
% are both greatest common divisors}\label{euclidean} |
| 114 |
% \end{figure} |
| 115 |
% |
| 116 |
% \begin{figure} |
| 117 |
% \centering |
| 118 |
% \begin{tabular}{ll} |
| 119 |
% |\polyfactorize {(X-1)(X-1)(X^2+1)}|&\polyfactorize{(X-1)(X-1)(X^2+1)}\\ \\ |
| 120 |
% |\polyfactorize {2X^3+X^2-7X+3}|\\ |
| 121 |
% \multicolumn{2}{l}{\hspace*{.3\linewidth}\polyfactorize{2X^3+X^2-7X+3}}\\ \\ |
| 122 |
% \multicolumn{2}{l}{\makeatletter\ttfamily |
| 123 |
% \def\temp{\polyfactorize{120X^5-274X^4+225X^3-85X^2+15X-1}}^^A |
| 124 |
% \csname strip@prefix\expandafter\endcsname\meaning\temp}\\ |
| 125 |
% \multicolumn{2}{l}{\hspace*{.3\linewidth}^^A |
| 126 |
% \polyfactorize{120X^5-274X^4+225X^3-85X^2+15X-1}}\\ |
| 127 |
% \end{tabular} |
| 128 |
% \caption{Factorizations of some polynomials}\label{factorize} |
| 129 |
% \end{figure} |
| 130 |
% |
| 131 |
% Figures~\ref{euclidean} and \ref{factorize} show applications of polynomial |
| 132 |
% division. On the one hand the Euclidean algorithm to determine a greatest |
| 133 |
% common divisor of two polynomials, and on the other the factorization of |
| 134 |
% a polynomial with at most two nonrational zeros. This should suffice for many |
| 135 |
% teaching aids. |
| 136 |
% |
| 137 |
% |
| 138 |
% \section{Hints} |
| 139 |
% |
| 140 |
% As the examples show, the commands get their data through mandatory and |
| 141 |
% optional arguments. Polynomials are entered as you would type them in math |
| 142 |
% mode:\footnote{The scanner is based on the scanner of the \texttt{calc} |
| 143 |
% package \cite{calc}. Read its documentation and the implementation part here |
| 144 |
% if you want to know more.} you may use |+|, |-|, |*|, |\cdot|, |/|, |\frac|, |
| 145 |
% |(|, |)|, natural numbers, symbols like |e|, |\pi|, |\chi|, |\lambda|, and |
| 146 |
% variables; the power operator |^| with integer exponents can be used on |
| 147 |
% symbols, variables, and parenthesized expressions. |
| 148 |
% Never use variables in a nominator, denominator or divisor. |
| 149 |
% |
| 150 |
% The support of symbols is very limited and there is neither support of |
| 151 |
% functions like \(\sin(x)\) or \(\exp(x)\), nor of roots or exponents other |
| 152 |
% than integers, for example \(\sqrt\pi\) or \(e^x\). For teaching purposes |
| 153 |
% this shouldn't be a major drawback. Particularly because there is a simple |
| 154 |
% workaround in some cases: the package doesn't look at symbols closely, |
| 155 |
% so define a function like \(e^x\) or `composed symbol' like \(\sqrt\pi\) |
| 156 |
% as a symbol. Take a look at figure~\ref{epowerx} for an example. |
| 157 |
% \begin{figure} |
| 158 |
% \newcommand\epowerx{e^x} |
| 159 |
% \[\polylongdiv[style=C,div=/]{\epowerx x^3-\epowerx x^2+\epowerx x-\epowerx}{x-1}\] |
| 160 |
% \begin{verbatim} |
| 161 |
% \newcommand\epowerx{e^x} |
| 162 |
% \[\polylongdiv{\epowerx x^3-\epowerx x^2+\epowerx x-\epowerx}{x-1}\]\end{verbatim} |
| 163 |
% \caption{Avoiding problems with \(e^x\). Be particularly careful in such |
| 164 |
% cases. \emph{You} have to take care of the correct result \emph{since} the |
| 165 |
% package does the computation. And by the way, it's always good to keep an |
| 166 |
% eye on plausibility of the results} |
| 167 |
% \label{epowerx} |
| 168 |
% \end{figure} |
| 169 |
% |
| 170 |
% \medbreak |
| 171 |
% |
| 172 |
% Optional arguments are used to specify more general options (and also for |
| 173 |
% the evaluation point for Horner's scheme). The options are entered in |
| 174 |
% key=value fashion using the \packagename{keyval} package \cite{keyval}. |
| 175 |
% The available options are listed in the respective sections below. |
| 176 |
% |
| 177 |
% |
| 178 |
% \section{Commands} |
| 179 |
% |
| 180 |
% |
| 181 |
% \subsection[\texttt{\textbackslash polyset}] |
| 182 |
% {\normalfont\texttt{\textbackslash polyset}\marg{key=value list}} |
| 183 |
% |
| 184 |
% Keys and values in optional arguments affect only that particular operation. |
| 185 |
% |\polyset| changes the settings for the rest of the current environment or |
| 186 |
% group. This could be a single figure or the whole document. Almost every key |
| 187 |
% described in this manual is allowed\,---\,just try it and you'll see. |
| 188 |
% Table~\ref{keys} lists all keys, which are not connected to a particular |
| 189 |
% command. An example is |
| 190 |
% \begin{verbatim} |
| 191 |
% \polyset{vars=XYZ\xi, % make X, Y, Z, and \xi into variables |
| 192 |
% delims={[}{]}}% nongrowing brackets\end{verbatim} |
| 193 |
% Note that is essential to use \texttt{vars}-declared variables only. |
| 194 |
% The package can't guess your intention and |
| 195 |
% |\polylongdiv{\zeta^3+\zeta^2-1}{\zeta-1}| |
| 196 |
% would divide a constant by a constant without the information $\zeta$ being |
| 197 |
% a variable. |
| 198 |
% |
| 199 |
% \begin{table} |
| 200 |
% \centering |
| 201 |
% \begin{tabular}{p{.4\textwidth}p{.5\textwidth}} |
| 202 |
% \texttt{vars=}\meta{token string} |
| 203 |
% & make each token a variable\\ |
| 204 |
% &\\ |
| 205 |
% \texttt{delims=}\marg{left}\marg{right} |
| 206 |
% & define delimiters used for printing\\ |
| 207 |
% & parenthesized expressions\\ |
| 208 |
% \end{tabular} |
| 209 |
% \caption{General keys. Default for \texttt{vars} is \texttt{Xx}. The key |
| 210 |
% \texttt{delims} has in fact an optional argument which takes |
| 211 |
% invisible versions of the left and right delimiter. The default is |
| 212 |
% \texttt{delims=[\{\textbackslash left.\}\{\textbackslash right.\}]\{\textbackslash left(\}\{\textbackslash right)\}} |
| 213 |
% }\label{keys} |
| 214 |
% \end{table} |
| 215 |
% |
| 216 |
% |
| 217 |
% \subsection[\texttt{\textbackslash polylongdiv}] |
| 218 |
% {\normalfont\texttt{\textbackslash polylongdiv}\oarg{key=value list}\meta{polynomial \(a\)}\meta{polynomial \(b\)}} |
| 219 |
% |
| 220 |
% The command prints the polynomial long division of $a/b$. Applicable keys |
| 221 |
% are listed in table~\ref{keys:longdiv}. Of course, \texttt{vars} and |
| 222 |
% \texttt{delims} can be used, too. |
| 223 |
% |
| 224 |
% \begin{table} |
| 225 |
% \centering |
| 226 |
% \begin{tabular}{p{.4\textwidth}p{.5\textwidth}} |
| 227 |
% \texttt{stage=}\meta{number} |
| 228 |
% & print long division up to stage \meta{number} (starting with 1)\\ |
| 229 |
% &\\ |
| 230 |
% \texttt{style=}\texttt{A$\vert$B$\vert$C$\vert$D} |
| 231 |
% & define output scheme for long division, refer \href{polydemo.pdf}{polydemo.pdf}\\ |
| 232 |
% &\\ |
| 233 |
% \texttt{div=}\meta{token} |
| 234 |
% & define division sign for \texttt{style=C}, default is $\div$\\ |
| 235 |
% \end{tabular} |
| 236 |
% \caption{Keys and values for polynomial long division. \texttt{style=A} |
| 237 |
% requires \texttt{stage=}\({}3\times(\#\)quotient's summands\()+1\) |
| 238 |
% to be carried out fully. The other styles \texttt{B} and \texttt{C} |
| 239 |
% need one more stage if the remainder is nonzero} |
| 240 |
% \label{keys:longdiv} |
| 241 |
% \end{table} |
| 242 |
% |
| 243 |
% |
| 244 |
% \subsection[\texttt{\textbackslash polyhornerscheme}] |
| 245 |
% {\normalfont\texttt{\textbackslash polyhornerscheme}\oarg{key=value list}\meta{polynomial}} |
| 246 |
% |
| 247 |
% The command prints Horner's scheme for the given polynomial with respect to |
| 248 |
% the specified evaluation point. Note that the latter one is entered as a |
| 249 |
% key=value pair in the form \meta{variable}\texttt{=}\meta{value}. |
| 250 |
% Table~\ref{keys:horner} lists other keys and their respective values. |
| 251 |
% |
| 252 |
% \begin{table} |
| 253 |
% \centering |
| 254 |
% \begin{tabular}{p{.4\textwidth}p{.5\textwidth}} |
| 255 |
% \meta{variable}\texttt{=}\meta{value} |
| 256 |
% & The definition of the evaluation point is \emph{mandatory}!\\ |
| 257 |
% &\\ |
| 258 |
% \texttt{stage=}\meta{number} |
| 259 |
% & print Horner's scheme up to stage \meta{number} (starting with 1)\\ |
| 260 |
% &\\ |
| 261 |
% \texttt{tutor=}\texttt{true$\vert$false} |
| 262 |
% &turn on and off tutorial comments\\ |
| 263 |
% \texttt{tutorlimit=}\meta{number} |
| 264 |
% & illustrate the recent \meta{number} steps\\ |
| 265 |
% \texttt{tutorstyle=}\meta{font selection} |
| 266 |
% & define appearance of tutorial comments\\ |
| 267 |
% &\\ |
| 268 |
% \texttt{resultstyle=}\meta{font selection} |
| 269 |
% & define appearance of the result\\ |
| 270 |
% \texttt{resultleftrule=}\texttt{true$\vert$false}\newline |
| 271 |
% \texttt{resultrightrule=}\texttt{true$\vert$false}\newline |
| 272 |
% \texttt{resultbottomrule=}\texttt{true$\vert$false} |
| 273 |
% & control rules left to, right to, and at the bottom of the result\\ |
| 274 |
% &\\ |
| 275 |
% \texttt{showbase=}\texttt{false$\vert$}\newline\phantom{\texttt{showbase=}}\texttt{top$\vert$middle$\vert$bottom} |
| 276 |
% & define whether and in which row the base (the value) is printed\\ |
| 277 |
% \texttt{showvar=}\texttt{true$\vert$false} |
| 278 |
% & print or suppress the variable name (additionally to the base)\\ |
| 279 |
% \texttt{showbasesep=}\texttt{true$\vert$false} |
| 280 |
% & print or suppress the vertical rule\\ |
| 281 |
% &\\ |
| 282 |
% \texttt{equalcolwidth=}\texttt{true$\vert$false} |
| 283 |
% & use the same width for all columns or use their individual widths\\ |
| 284 |
% \texttt{arraycolsep=}\meta{dimension} |
| 285 |
% & space between columns\\ |
| 286 |
% \texttt{arrayrowsep=}\meta{dimension} |
| 287 |
% & space between rows\\ |
| 288 |
% &\\ |
| 289 |
% \texttt{showmiddlerow=}\texttt{true$\vert$false} |
| 290 |
% & print or suppress the middle row\\ |
| 291 |
% \end{tabular} |
| 292 |
% \caption{Keys and values for Horner's scheme. Don't use \texttt{showmiddlerow=false} |
| 293 |
% with \texttt{tutor=true}.} |
| 294 |
% \label{keys:horner} |
| 295 |
% \end{table} |
| 296 |
% |
| 297 |
% \iffalse |
| 298 |
% The following key are not listed above: |
| 299 |
% |
| 300 |
% mul=<math tokens> \cdot |
| 301 |
% plusface=left|right right |
| 302 |
% plusyoffset=<dimension> 0pt |
| 303 |
% |
| 304 |
% downarrow=<picture tokens> {\vector(0,-1){2.5}} |
| 305 |
% diagarrow=<picture tokens> {\vector(2,1){1.6}} |
| 306 |
% downarrowxoffset=<dimension> 0pt |
| 307 |
% diagarrowxoffset=<dimension> 0pt |
| 308 |
% \fi |
| 309 |
% |
| 310 |
% |
| 311 |
% \subsection[\texttt{\textbackslash polylonggcd}] |
| 312 |
% {\normalfont\texttt{\textbackslash polylonggcd}\oarg{key=value list}\meta{polynomial \(a\)}\meta{polynomial \(b\)}} |
| 313 |
% |
| 314 |
% The command prints equations of the Euclidean algorithm used to determine |
| 315 |
% the greatest common divisor of the polynomials \(a\) and \(b\), refer |
| 316 |
% figure~\ref{euclidean}. |
| 317 |
% |
| 318 |
% |
| 319 |
% \subsection[\texttt{\textbackslash polyfactorize}] |
| 320 |
% {\normalfont\texttt{\textbackslash polyfactorize}\oarg{key=value list}\meta{polynomial}} |
| 321 |
% |
| 322 |
% The command prints a factorization of the polynomial as long as all except |
| 323 |
% two roots are rational, see figure \ref{factorize}. |
| 324 |
% |
| 325 |
% |
| 326 |
% \subsection{Low-level commands} |
| 327 |
% |
| 328 |
% To tell the whole truth, the commands above don't need the polynomials typed |
| 329 |
% in verbatim. The internal representation of polynomials can be stored as |
| 330 |
% replacement texts of control sequences and such control sequences can take |
| 331 |
% the role of verbatim polynomials. This is also the case for \meta{\(a\)} and |
| 332 |
% \meta{\(b\)} in table~\ref{low}, but each \meta{cs$_{\ldots}$} must be a |
| 333 |
% control sequence, in which the result is saved. |
| 334 |
% |
| 335 |
% The command in table~\ref{low} can be used for low level calculations, and in |
| 336 |
% particular to store polynomials for later use with the high-level commands. |
| 337 |
% For example one could write the following. |
| 338 |
% \begin{verbatim} |
| 339 |
% \polyadd\polya {(X^2+X+1)(X-1)-\frac\pi2}{0}% trick |
| 340 |
% \polymul\polyb {X-1}{1} % another trick |
| 341 |
% Let's see how to divide \polyprint\polya{} by \polyprint\polyb. |
| 342 |
% \[\polylongdiv\polya\polyb\]\end{verbatim} |
| 343 |
% |
| 344 |
% \begin{table} |
| 345 |
% \centering |
| 346 |
% \begin{tabular}{r@{\enspace}ll} |
| 347 |
% \meta{cs$_{a+b}$}&$\gets a+b$ |
| 348 |
% & \cs{polyadd}\meta{cs$_{a+b}$}\meta{\(a\)}\meta{\(b\)}\\ |
| 349 |
% &&\\ |
| 350 |
% \meta{cs$_{a-b}$}&$\gets a-b$ |
| 351 |
% & \cs{polysub}\meta{cs$_{a-b}$}\meta{\(a\)}\meta{\(b\)}\\ |
| 352 |
% &&\\ |
| 353 |
% \meta{cs$_{ab}$}&$\gets a\cdot b$ |
| 354 |
% & \cs{polymul}\meta{cs$_{ab}$}\meta{\(a\)}\meta{\(b\)}\\ |
| 355 |
% &&\\ |
| 356 |
% \meta{cs$_{a/b}$}&$\gets \lfloor a/b\rfloor$ |
| 357 |
% & \cs{polydiv}\meta{cs$_{a/b}$}\meta{\(a\)}\meta{\(b\)}\\ |
| 358 |
% \cs{polyremainder}&$\gets a\bmod b$ |
| 359 |
% &\\ |
| 360 |
% &&\\ |
| 361 |
% \meta{cs$_{\gcd}$}&$\gets \gcd(a,b)$ |
| 362 |
% & \cs{polygcd}\meta{cs$_{\gcd}$}\meta{\(a\)}\meta{\(b\)}\\ |
| 363 |
% &&\\ |
| 364 |
% \multicolumn{2}{r}{print polynomial $a$} |
| 365 |
% & \cs{polyprint}\meta{\(a\)}\\ |
| 366 |
% \end{tabular} |
| 367 |
% \caption{Low-level user commands}\label{low} |
| 368 |
% \end{table} |
| 369 |
% |
| 370 |
% |
| 371 |
% \section{Acknowledgments} |
| 372 |
% |
| 373 |
% I wish to thank |
| 374 |
% Ludger Humbert, |
| 375 |
% Karl Heinz Marbaise, and |
| 376 |
% Elke Niedermair |
| 377 |
% for their tests and error reports. |
| 378 |
% |
| 379 |
% |
| 380 |
% \StopEventually{^^A |
| 381 |
% \begin{thebibliography}{1} |
| 382 |
% \bibitem{TUGboat} |
| 383 |
% \textsc{Barbara Beeton} and \textsc{Donald Arseneau}. |
| 384 |
% |
| 385 |
% \textit{Long division}. |
| 386 |
% |
| 387 |
% In Jeremy Gibbons' \textit{Hey --- it works!}, |
| 388 |
% TUGboat 18(2), June 1997, p.~75. |
| 389 |
% |
| 390 |
% \bibitem{calc} |
| 391 |
% \textsc{Kresten Krab Thorup}, \textsc{Frank Jensen}, and \textsc{Chris Rowley}. |
| 392 |
% |
| 393 |
% \textit{The \texttt{calc} package, Infix notation arithmetic in \LaTeX}, 1998/07/07. |
| 394 |
% |
| 395 |
% Available from \texttt{CTAN:} \texttt{macros/latex/required/tools}. |
| 396 |
% |
| 397 |
% \bibitem{keyval} |
| 398 |
% \textsc{David Carlisle}. |
| 399 |
% |
| 400 |
% \textit{The \textsf{keyval} package}, 1999/03/16. |
| 401 |
% |
| 402 |
% Available from \texttt{CTAN:} \texttt{macros/latex/required/graphics}. |
| 403 |
%\end{thebibliography}} |
| 404 |
% |
| 405 |
% |
| 406 |
% \CheckSum{4500} |
| 407 |
% |
| 408 |
% |
| 409 |
% \section{Preliminaries} |
| 410 |
% |
| 411 |
% Let's start with identification. |
| 412 |
% \begin{macrocode} |
| 413 |
%<*package> |
| 414 |
\NeedsTeXFormat{LaTeX2e} |
| 415 |
\ProvidesPackage{polynom}[2017/07/17 0.19 (CH,HA)] |
| 416 |
% \end{macrocode} |
| 417 |
% Now follow two frequently used definitions. |
| 418 |
% |
| 419 |
% \begin{macro}{\pld@AddTo} |
| 420 |
% \begin{macro}{\pld@Extend} |
| 421 |
% \meta{macro}\marg{contents} |
| 422 |
% \begin{describe} |
| 423 |
% adds \meta{contents} to the macro respectively does an |\expandafter| on the |
| 424 |
% first token of \meta{contents} before doing so. |
| 425 |
% \end{describe} |
| 426 |
% \begin{macrocode} |
| 427 |
\def\pld@AddTo#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}} |
| 428 |
\def\pld@Extend#1#2{% |
| 429 |
\expandafter\pld@AddTo\expandafter#1\expandafter{#2}} |
| 430 |
% \end{macrocode} |
| 431 |
% \end{macro} |
| 432 |
% \end{macro} |
| 433 |
% |
| 434 |
% \begin{macro}{\pld@ExpandTwo} |
| 435 |
% expands the respectively first tokens of |#2| and |#3| and puts all as |
| 436 |
% argument after |#1|. Note that |#2| and |#3| need not to be single tokens. |
| 437 |
% \begin{macrocode} |
| 438 |
\def\pld@ExpandTwo#1#2#3{% |
| 439 |
\expandafter\def\expandafter\pld@temp\expandafter{#2}% |
| 440 |
\pld@Extend\pld@temp{#3}% |
| 441 |
\expandafter#1\pld@temp} |
| 442 |
% \end{macrocode} |
| 443 |
% \end{macro} |
| 444 |
% |
| 445 |
% \begin{macro}{\pld@if} |
| 446 |
% is used as a temporary and local switch. |
| 447 |
% \begin{macrocode} |
| 448 |
\def\pld@true{\let\pld@if\iftrue} |
| 449 |
\def\pld@false{\let\pld@if\iffalse} |
| 450 |
\pld@false |
| 451 |
% \end{macrocode} |
| 452 |
% \end{macro} |
| 453 |
% |
| 454 |
% |
| 455 |
% \section{The user interface} |
| 456 |
% |
| 457 |
% \begin{macro}{\polyset} |
| 458 |
% This command just `inserts' the family name |pld| and requires the |
| 459 |
% \packagename{keyval} package. |
| 460 |
% \begin{macrocode} |
| 461 |
\RequirePackage{keyval}[1997/11/10] |
| 462 |
\def\polyset{\setkeys{pld}} |
| 463 |
% \end{macrocode} |
| 464 |
% \end{macro} |
| 465 |
% |
| 466 |
% \begin{macro}{\pld@IfVar} |
| 467 |
% The variables are stored in a comma separated list. Here we look after |
| 468 |
% |#1| being an element and execute the second argument \meta{then} or the |
| 469 |
% third argument \meta{else}. |
| 470 |
% \begin{macrocode} |
| 471 |
\def\pld@IfVar#1{% |
| 472 |
\def\pld@temp##1,#1,##2##3\relax{% |
| 473 |
\ifx\@empty##3\@empty \expandafter\@secondoftwo |
| 474 |
\else \expandafter\@firstoftwo \fi}% |
| 475 |
\expandafter\pld@temp\pld@variables,#1,\@empty\relax} |
| 476 |
% \end{macrocode} |
| 477 |
% \end{macro} |
| 478 |
% The key iterates down the tokens and expands the list, making a new |
| 479 |
% key for every variable. |
| 480 |
% \begin{macrocode} |
| 481 |
\define@key{pld}{vars}{% |
| 482 |
\let\pld@variables\@empty |
| 483 |
\@tfor\pld@temp:=#1\do{% |
| 484 |
\pld@Extend\pld@variables{\expandafter,\pld@temp}% |
| 485 |
\edef\pld@temp{% |
| 486 |
\noexpand\define@key{pld}{\pld@temp}% |
| 487 |
{\noexpand\pld@GetValue{\pld@temp}{####1}}% |
| 488 |
}% |
| 489 |
\pld@temp |
| 490 |
}% |
| 491 |
} |
| 492 |
% \end{macrocode} |
| 493 |
% \begin{macro}{\pld@GetValue} |
| 494 |
% Helper macro to retrieve the value of a variable. |
| 495 |
% \begin{macrocode} |
| 496 |
\def\pld@GetValue#1#2{% |
| 497 |
\pld@GetPoly{\pld@polya}{}{#2}% |
| 498 |
\ifx\pld@polya\@empty\def\pld@polya{\pld@R 01}\fi |
| 499 |
\expandafter\let\csname pld@value@#1\endcsname\pld@polya |
| 500 |
} |
| 501 |
% \end{macrocode} |
| 502 |
% \begin{macrocode} |
| 503 |
\polyset{vars=Xx} |
| 504 |
% \end{macrocode} |
| 505 |
% \end{macro} |
| 506 |
% |
| 507 |
% \begin{macro}{\pld@iftopresult} |
| 508 |
% determines the printing style for long divisions. The key checks for the |
| 509 |
% macro definition |\pld@style|\meta{name}, \ldots |
| 510 |
% \begin{macrocode} |
| 511 |
\define@key{pld}{style} |
| 512 |
{\@ifundefined{pld@style#1}% |
| 513 |
{\PackageError{polynom}{Unknown style `#1'}% |
| 514 |
{Arguments can be `A' or `B' or `C' or `D'.}}% |
| 515 |
{\let\pld@style=#1% |
| 516 |
\@nameuse{pld@style#1}}} |
| 517 |
% \end{macrocode} |
| 518 |
% which are defined here. |
| 519 |
% \begin{macrocode} |
| 520 |
\def\pld@styleA{\let\pld@iftopresult\iftrue} |
| 521 |
\def\pld@styleB{\let\pld@iftopresult\iffalse} |
| 522 |
\let\pld@styleC\pld@styleB |
| 523 |
\let\pld@styleD\pld@styleB |
| 524 |
% \end{macrocode} |
| 525 |
% \begin{macrocode} |
| 526 |
\polyset{style=A} |
| 527 |
% \end{macrocode} |
| 528 |
% \end{macro} |
| 529 |
% |
| 530 |
% \begin{macro}{\pld@leftdelim} |
| 531 |
% \begin{macro}{\pld@rightdelim} |
| 532 |
% \begin{macro}{\pld@leftxdelim} |
| 533 |
% \begin{macro}{\pld@rightxdelim} |
| 534 |
% We make left and right delimiters definable. |
| 535 |
% \begin{macrocode} |
| 536 |
\define@key{pld}{delims} |
| 537 |
{\@ifnextchar[\pld@delims |
| 538 |
{\pld@delims[{}{}]}#1{}{}} |
| 539 |
\def\pld@delims[#1#2]#3#4{% |
| 540 |
\def\pld@leftxdelim{#1}\def\pld@rightxdelim{#2}% |
| 541 |
\def\pld@leftdelim{#3}\def\pld@rightdelim{#4}} |
| 542 |
\polyset{delims=[{\left.}{\right.}]{\left(}{\right)}} |
| 543 |
% \end{macrocode} |
| 544 |
% \end{macro} |
| 545 |
% \end{macro} |
| 546 |
% \end{macro} |
| 547 |
% \end{macro} |
| 548 |
% |
| 549 |
% \begin{macro}{\pld@div} |
| 550 |
% Moreover one can customize the division sign for the C style. |
| 551 |
% \begin{macrocode} |
| 552 |
\define@key{pld}{div}{\def\pld@div{#1}} |
| 553 |
\polyset{div=\div} |
| 554 |
% \end{macrocode} |
| 555 |
% \end{macro} |
| 556 |
% |
| 557 |
% \begin{macro}{\pld@stage} |
| 558 |
% \begin{macro}{\pld@currstage} |
| 559 |
% Ensure a positive value. |
| 560 |
% \begin{macrocode} |
| 561 |
\define@key{pld}{stage}{% |
| 562 |
\@tempcnta#1\relax \ifnum\@tempcnta<\@ne \@tempcnta\@ne \fi |
| 563 |
\edef\pld@stage{\the\@tempcnta}} |
| 564 |
% \end{macrocode} |
| 565 |
% \begin{macrocode} |
| 566 |
\newcount\pld@currstage |
| 567 |
% \end{macrocode} |
| 568 |
% \end{macro} |
| 569 |
% \end{macro} |
| 570 |
% |
| 571 |
% The following definitions all have the same scheme: get the polynomial(s), |
| 572 |
% do the operation, and assign or print the result. And they all use macros |
| 573 |
% which are defined in later sections. |
| 574 |
% |
| 575 |
% \begin{macro}{\polymul} |
| 576 |
% Just the things stated. |
| 577 |
% \begin{macrocode} |
| 578 |
\newcommand*\polymul[1]{% |
| 579 |
\pld@GetPoly{\pld@polya\pld@polyb}% |
| 580 |
{\pld@MultiplyPoly#1\pld@polya\pld@polyb |
| 581 |
\ignorespaces}} |
| 582 |
% \end{macrocode} |
| 583 |
% \end{macro} |
| 584 |
% |
| 585 |
% \begin{macro}{\polydiv} |
| 586 |
% Ditto. |
| 587 |
% \begin{macrocode} |
| 588 |
\newcommand*\polydiv[1]{% |
| 589 |
\begingroup |
| 590 |
\let\pld@stage\maxdimen |
| 591 |
\pld@GetPoly{\pld@polya\pld@polyb}% |
| 592 |
{\pld@DividePoly\pld@polya\pld@polyb |
| 593 |
\let#1\pld@quotient |
| 594 |
\let\polyremainder\pld@remainder |
| 595 |
\pld@RestoreAftergroup#1\polyremainder\relax |
| 596 |
\endgroup\ignorespaces}} |
| 597 |
% \end{macrocode} |
| 598 |
% \end{macro} |
| 599 |
% |
| 600 |
% \begin{macro}{\polylongdiv} |
| 601 |
% Ditto. We use an if to use the fix by Hendrik Vogt only inside this macro. |
| 602 |
% \begin{macrocode} |
| 603 |
\newif\ifpld@InsidePolylongdiv |
| 604 |
\newcommand*\polylongdiv[1][]{% |
| 605 |
\begingroup\pld@InsidePolylongdivtrue |
| 606 |
\let\pld@stage\maxdimen \polyset{#1}% |
| 607 |
\pld@GetPoly{\pld@polya\pld@polyb}% |
| 608 |
{\pld@LongDividePoly\pld@polya\pld@polyb |
| 609 |
\pld@PrintLongDiv |
| 610 |
\endgroup \ignorespaces}} |
| 611 |
% \end{macrocode} |
| 612 |
% \end{macro} |
| 613 |
% |
| 614 |
% \begin{macro}{\polylonggcd} |
| 615 |
% Ditto. |
| 616 |
% \begin{macrocode} |
| 617 |
\newcommand*\polylonggcd[1][]{% |
| 618 |
\begingroup |
| 619 |
\let\pld@stage\maxdimen \polyset{#1}% |
| 620 |
\pld@GetPoly{\pld@polya\pld@polyb}% |
| 621 |
{\pld@LongEuclideanPoly\pld@polya\pld@polyb |
| 622 |
\pld@PrintLongEuclidean |
| 623 |
\endgroup \ignorespaces}} |
| 624 |
% \end{macrocode} |
| 625 |
% \end{macro} |
| 626 |
% |
| 627 |
% \begin{macro}{\polygcd} |
| 628 |
% Ditto. |
| 629 |
% \begin{macrocode} |
| 630 |
\newcommand*\polygcd[1]{% |
| 631 |
\begingroup |
| 632 |
\let\pld@stage\maxdimen |
| 633 |
\pld@GetPoly{\pld@polya\pld@polyb}% |
| 634 |
{\pld@LongEuclideanPoly\pld@polya\pld@polyb |
| 635 |
\global\let\@gtempa\pld@vb |
| 636 |
\endgroup \let#1\@gtempa \ignorespaces}} |
| 637 |
% \end{macrocode} |
| 638 |
% A bug report by Elke Niedermair ^^A {[email protected]}{2002/10/29}{undefined control sequence \pld@stage} |
| 639 |
% led to the initialization of |\pld@stage| -- and the surrounding |
| 640 |
%|\begingroup| and |\endgroup|. |
| 641 |
% \end{macro} |
| 642 |
% |
| 643 |
% \begin{macro}{\polyfactorize} |
| 644 |
% Ditto. |
| 645 |
% \begin{macrocode} |
| 646 |
\newcommand*\polyfactorize{% |
| 647 |
\pld@GetPoly\pld@current |
| 648 |
{\pld@Factorize\pld@current \ensuremath{\pld@allines}}} |
| 649 |
% \end{macrocode} |
| 650 |
% \end{macro} |
| 651 |
% |
| 652 |
% \begin{macro}{\polyprint} |
| 653 |
% Ditto. |
| 654 |
% \begin{macrocode} |
| 655 |
\newcommand*\polyprint{% |
| 656 |
\pld@GetPoly{\pld@polya}% |
| 657 |
{\ensuremath{\pld@PrintPoly\pld@polya}}} |
| 658 |
% \end{macrocode} |
| 659 |
% \end{macro} |
| 660 |
% |
| 661 |
% \begin{macro}{\polyadd} |
| 662 |
% Get the polynomials, add them via appending the representation, and |
| 663 |
% normalize the result via simplification. |
| 664 |
% \begin{macrocode} |
| 665 |
\newcommand*\polyadd[1]{% |
| 666 |
\pld@GetPoly{\pld@polya\pld@polyb}% |
| 667 |
{\let#1\pld@polya \pld@ExtendPoly#1\pld@polyb |
| 668 |
\pld@Simplify#1% |
| 669 |
\ignorespaces}} |
| 670 |
% \end{macrocode} |
| 671 |
% \end{macro} |
| 672 |
% |
| 673 |
% \begin{macro}{\polysub} |
| 674 |
% Ditto. |
| 675 |
% \begin{macrocode} |
| 676 |
\newcommand*\polysub[1]{% |
| 677 |
\pld@GetPoly{\pld@polya\pld@polyb}% |
| 678 |
{\def\pld@tempoly{\pld@R{-1}1}% |
| 679 |
\pld@MultiplyPoly\pld@polyb\pld@polyb\pld@tempoly |
| 680 |
\let#1\pld@polya \pld@ExtendPoly#1\pld@polyb |
| 681 |
\pld@Simplify#1% |
| 682 |
\ignorespaces}} |
| 683 |
% \end{macrocode} |
| 684 |
% \end{macro} |
| 685 |
% |
| 686 |
% \begin{macro}{\pld@RestoreAftergroup} |
| 687 |
% We just iterate down the control sequences, add |\def#1{|\meta{contents of |
| 688 |
% \texttt{\#1}}|}| to |\@gtempa|, and execute it \cs{aftergroup}. |
| 689 |
% \begin{macrocode} |
| 690 |
\def\pld@RestoreAftergroup{% |
| 691 |
\global\let\@gtempa\@empty |
| 692 |
\pld@RestoreAfter@} |
| 693 |
\def\pld@RestoreAfter@#1{% |
| 694 |
\ifx\relax#1% |
| 695 |
\aftergroup\@gtempa |
| 696 |
\else |
| 697 |
\global\pld@Extend\@gtempa{\expandafter\def\expandafter#1% |
| 698 |
\expandafter{#1}}% |
| 699 |
\expandafter\pld@RestoreAfter@ |
| 700 |
\fi} |
| 701 |
% \end{macrocode} |
| 702 |
% \end{macro} |
| 703 |
% |
| 704 |
% |
| 705 |
% \section{Internal data format}\label{sInternalDataFormat} |
| 706 |
% |
| 707 |
% A polynomial is a finite sum $\meta{$\mathrm{monomial}_1$}+\ldots+ |
| 708 |
% \meta{$\mathrm{monomial}_n$}$ of monomials. In the internal data format, the |
| 709 |
% monomials will be sorted by degree---in the multivariate case not by the |
| 710 |
% total degree but by the degree of the first variable, then by the degree of |
| 711 |
% the second variable, and so on. |
| 712 |
% |
| 713 |
% Each monomial is a product of \emph{rationals}, general \emph{fractions}, |
| 714 |
% \emph{symbolic factors}, and \emph{variables}. The factors are represented |
| 715 |
% in the following format. |
| 716 |
% \begin{enumerate} |
| 717 |
% \item |\pld@R|\marg{integer nominator}\marg{integer denominator} for rationals, |
| 718 |
% \item |\pld@F|\marg{nominator}\marg{denominator} for general fractions, |
| 719 |
% \item |\pld@S|\marg{symbol}\marg{exponent} for symbolic factors, and |
| 720 |
% \item |\pld@V|\marg{symbol}\marg{exponent} for variables. |
| 721 |
% \end{enumerate} |
| 722 |
% As a special case \meta{nominator} and/or \meta{denominator} may be empty, |
| 723 |
% which stands for a factor $1={}$|\pld@R{1}{1}|. |
| 724 |
% |
| 725 |
% \begin{table}[tp] |
| 726 |
% \begin{tabular}{cl} |
| 727 |
% \emph{mathematians write}&\multicolumn{1}{c}{\emph{internal representation}}\\[1ex] |
| 728 |
% $X^2-1$ & |\pld@V{X}{2}+\pld@R{-1}{1}|\\[1ex] |
| 729 |
% $\frac1e X$ & |\pld@F{\pld@R{1}{1}}{\pld@S{e}{1}}\pld@V{X}{1}|\\ |
| 730 |
% & |\pld@S{e}{-1}\pld@V{X}{1}|\\[1ex] |
| 731 |
% $\frac1{10}$ & |\pld@F{\pld@R{1}{1}}{\pld@R{10}{1}}|\\ |
| 732 |
% & |\pld@R{1}{10}| |
| 733 |
% \end{tabular} |
| 734 |
% \caption{Mathematical notation versus internal representation}\label{mvi} |
| 735 |
% \end{table} |
| 736 |
% Table \ref{mvi} shows examples of the internal data format. As you can see, |
| 737 |
% sometimes there are various ways to represent the same polynomial. The |
| 738 |
% exact internal data depends on how you enter the factors and which state has |
| 739 |
% been reached in the division algorithm, for example. |
| 740 |
% |
| 741 |
% And now some definitions which work on representations of polynomials, first |
| 742 |
% macros to `look at' polynomials and then macros to build them. |
| 743 |
% |
| 744 |
% \begin{macro}{\pld@SplitMonom} |
| 745 |
% \meta{|\#1\#2| submacro}\marg{monomial representation} |
| 746 |
% \begin{describe} |
| 747 |
% calls the given macro with the `nonvariable' part as first and the `variable' |
| 748 |
% part as second argument. Each of them can be empty. Note that this definition |
| 749 |
% makes an assumption on the order of the factors in the representation, namely |
| 750 |
% that the variable part comes at the end. |
| 751 |
% \end{describe} |
| 752 |
% \begin{macrocode} |
| 753 |
\def\pld@SplitMonom#1#2{% |
| 754 |
\pld@SplitMonom@#2\pld@V\relax {\pld@SplitMonom@V#1#2\relax}% |
| 755 |
{#1{#2}{}}} |
| 756 |
\def\pld@SplitMonom@#1\pld@V#2\relax{% |
| 757 |
\ifx\@empty#2\@empty \expandafter\@secondoftwo |
| 758 |
\else \expandafter\@firstoftwo \fi} |
| 759 |
\def\pld@SplitMonom@V#1#2\pld@V#3\relax{#1{#2}{\pld@V#3}} |
| 760 |
% \end{macrocode} |
| 761 |
% \end{macro} |
| 762 |
% |
| 763 |
% \begin{macro}{\pld@SplitMonomS} |
| 764 |
% \meta{|\#1\#2| submacro}\marg{`monomial representation'} |
| 765 |
% \begin{describe} |
| 766 |
% does the same but splits at |\pld@S| to separate numerals and symbols. |
| 767 |
% \end{describe} |
| 768 |
% \begin{macrocode} |
| 769 |
\def\pld@SplitMonomS#1#2{% |
| 770 |
\pld@SplitMonomS@#2\pld@S\relax {\pld@SplitMonomS@S#1#2\relax}% |
| 771 |
{#1{#2}{}}} |
| 772 |
\def\pld@SplitMonomS@#1\pld@S#2\relax{% |
| 773 |
\ifx\@empty#2\@empty \expandafter\@secondoftwo |
| 774 |
\else \expandafter\@firstoftwo \fi} |
| 775 |
\def\pld@SplitMonomS@S#1#2\pld@S#3\relax{#1{#2}{\pld@S#3}} |
| 776 |
% \end{macrocode} |
| 777 |
% \end{macro} |
| 778 |
% |
| 779 |
% \begin{macro}{\pld@IfSum} |
| 780 |
% \marg{polynomial representation}\marg{then}\marg{else} |
| 781 |
% \begin{describe} |
| 782 |
% executes \meta{then} if and only if the polynomial is a sum (of more than |
| 783 |
% one monomial). |
| 784 |
% \end{describe} |
| 785 |
% \begin{macrocode} |
| 786 |
\def\pld@IfSum#1{\pld@IfSum@#1+\@empty+\relax+} |
| 787 |
\def\pld@IfSum@#1+#2+\relax+{% |
| 788 |
\ifx\@empty#2\@empty \expandafter\@secondoftwo |
| 789 |
\else \expandafter\@firstoftwo \fi} |
| 790 |
% \end{macrocode} |
| 791 |
% \end{macro} |
| 792 |
% |
| 793 |
% \begin{macro}{\pld@DefNegative} |
| 794 |
% \meta{macro}\marg{monomial representation} |
| 795 |
% \begin{describe} |
| 796 |
% negates the monomial and puts it into the macro. |
| 797 |
% \end{describe} |
| 798 |
% \begin{macrocode} |
| 799 |
\def\pld@DefNegative#1#2{\pld@DefNegative@#1#2\@empty} |
| 800 |
\def\pld@DefNegative@#1#2#3#4#5\@empty{% |
| 801 |
\ifx #2\pld@R\def#1{\pld@R{-#3}{#4}#5}% |
| 802 |
\else \def#1{\pld@R{-1}1#2{#3}{#4}#5}\fi} |
| 803 |
% \end{macrocode} |
| 804 |
% \end{macro} |
| 805 |
% |
| 806 |
% \begin{macro}{\pld@DefInverse} |
| 807 |
% \meta{inverse macro}\marg{monomial representation} |
| 808 |
% \begin{describe} |
| 809 |
% puts a representation of the monomials' reciprocal into the macro. |
| 810 |
% \end{describe} |
| 811 |
% \begin{macrocode} |
| 812 |
\def\pld@DefInverse#1#2{% |
| 813 |
\let#1\@empty |
| 814 |
\pld@DefInverse@#1#2\relax\@empty\@empty} |
| 815 |
% \end{macrocode} |
| 816 |
% Here we just interchange nominator and denominator or negate the exponent. |
| 817 |
% \begin{macrocode} |
| 818 |
\def\pld@DefInverse@#1#2#3#4{% |
| 819 |
\ifx\relax#2\relax \expandafter\@gobbletwo \else |
| 820 |
\ifx #2\pld@R \pld@AddTo#1{\pld@R{#4}{#3}}\else |
| 821 |
\ifx #2\pld@F \pld@AddTo#1{\pld@F{#4}{#3}}\else |
| 822 |
\ifx #2\pld@S \pld@AddTo#1{\pld@S{#3}{-#4}}\else |
| 823 |
\ifx #2\pld@V \pld@AddTo#1{\pld@V{#3}{-#4}}\else |
| 824 |
\pld@DIError |
| 825 |
\fi \fi \fi \fi |
| 826 |
\fi |
| 827 |
\pld@DefInverse@#1} |
| 828 |
% \end{macrocode} |
| 829 |
% \end{macro} |
| 830 |
% |
| 831 |
% \begin{macro}{\pld@AddToPoly} |
| 832 |
% \begin{macro}{\pld@ExtendPoly} |
| 833 |
% \meta{polynomial}\marg{monomial} |
| 834 |
% \begin{describe} |
| 835 |
% adds \meta{monomial} as a new summand to \meta{polynomial} or does an |
| 836 |
% |\expandafter| on the first token before doing so. |
| 837 |
% \end{describe} |
| 838 |
% \begin{macrocode} |
| 839 |
\def\pld@AddToPoly#1#2{% |
| 840 |
\ifx #1\@empty \def#1{#2}\else |
| 841 |
\pld@AddTo#1{+#2}\fi} |
| 842 |
\def\pld@ExtendPoly#1#2{% |
| 843 |
\ifx #1\@empty \pld@Extend#1{#2}\else |
| 844 |
\ifx #2\@empty |
| 845 |
\else \pld@Extend#1{\expandafter+#2}\fi \fi} |
| 846 |
% \end{macrocode} |
| 847 |
% \end{macro} |
| 848 |
% \end{macro} |
| 849 |
% |
| 850 |
% \begin{macro}{\pld@R} |
| 851 |
% \begin{macro}{\pld@F} |
| 852 |
% \begin{macro}{\pld@S} |
| 853 |
% \begin{macro}{\pld@V} |
| 854 |
% These macros just contain distinct single characters. We will change the |
| 855 |
% definitions locally when we output a polynomial, for example. |
| 856 |
% \begin{macrocode} |
| 857 |
\def\pld@R{r} |
| 858 |
\def\pld@F{f} |
| 859 |
\def\pld@S{s} |
| 860 |
\def\pld@V{v} |
| 861 |
% \end{macrocode} |
| 862 |
% \end{macro} |
| 863 |
% \end{macro} |
| 864 |
% \end{macro} |
| 865 |
% \end{macro} |
| 866 |
% |
| 867 |
% |
| 868 |
% \section{Scanning input} |
| 869 |
% |
| 870 |
% \begin{macro}{\pld@GetPoly} |
| 871 |
% |{|\meta{macro$_1$}\ldots\meta{macro$_k$}|}| \marg{do after} \marg{polynomial$_1$}\ldots\marg{polynomial$_k$} |
| 872 |
% \begin{describe} |
| 873 |
% This definition parses all user supplied polynomials. For $i=1,\ldots,k$, |
| 874 |
% it assigns the internal representation of \meta{polynomial$_i$} to |
| 875 |
% \meta{macro$_i$} and executes \meta{do after}wards. `\marg{polynomial$_i$}' |
| 876 |
% may be a stored polynomial---that means a control sequence---in which case |
| 877 |
% the argument braces \emph{must} be omitted. |
| 878 |
% \end{describe} |
| 879 |
% First we initialize data and check whether the user provides an explicit |
| 880 |
% polynomial. |
| 881 |
% \begin{macrocode} |
| 882 |
\def\pld@GetPoly#1#2{% |
| 883 |
\def\pld@pool{#1}\def\pld@aftermacro{#2}% |
| 884 |
\pld@GetPoly@} |
| 885 |
\def\pld@GetPoly@{% |
| 886 |
\@ifnextchar\bgroup \pld@GetPolyArg\pld@GetPolyLet} |
| 887 |
% \end{macrocode} |
| 888 |
% Such a polynomial is scanned and then assigned to a macro from the pool. |
| 889 |
% \begin{macrocode} |
| 890 |
\def\pld@GetPolyArg#1{% |
| 891 |
\pld@Scan{#1}% |
| 892 |
\pld@GetPolyLet\pld@tempoly} |
| 893 |
% \end{macrocode} |
| 894 |
% Here we get one macro from the pool, assign the polynomial, and continue if |
| 895 |
% the pool isn't empty. |
| 896 |
% \begin{macrocode} |
| 897 |
\def\pld@GetPolyLet{\expandafter\pld@GetPolyLet@\pld@pool\relax} |
| 898 |
\def\pld@GetPolyLet@#1#2\relax#3{% |
| 899 |
\let#1#3\def\pld@pool{#2}% |
| 900 |
\pld@Simplify#1% |
| 901 |
\ifx\pld@pool\@empty \expandafter\pld@aftermacro |
| 902 |
\else \expandafter\pld@GetPoly@ \fi} |
| 903 |
% \end{macrocode} |
| 904 |
% \end{macro} |
| 905 |
% |
| 906 |
% Now we actually scan the input. Section \emph{4 The evaluation scheme} of |
| 907 |
% the \texttt{calc} package \cite{calc} explains how this is done in general. |
| 908 |
% Together with the implementation part of the that package, it's an excellent |
| 909 |
% introduction---if you are familiar with \TeX. However, some things are |
| 910 |
% different: |
| 911 |
% \begin{itemize} |
| 912 |
% \item We additionally detect |\frac|, |\cdot|, symbols, and variables. |
| 913 |
% \item To write $XY$ instead of $X\cdot Y$ or $X*Y$, we provide an implicit |
| 914 |
% multiplication. |
| 915 |
% \item |\pld@ScanIt| does what |\calc@pre@scan| and |\calc@post@scan| do. |
| 916 |
% \item In terms of the \texttt{calc} package, we clear the local `register B' |
| 917 |
% |\pld@tempoly| after each |\begingroup| (for providing a faster |
| 918 |
% multiplication, see below) and we assign the value of that register |
| 919 |
% to the global `register A' |\@gtempa| before each |\endgroup|. |
| 920 |
% \item Multiplication with a single factor (symbol, variable, fraction, |
| 921 |
% number) makes group level changes only if the current term is a sum. |
| 922 |
% Otherwise it just adds the factor to |\pld@tempoly|. This saves many |
| 923 |
% multiplications of polynomials. |
| 924 |
% \end{itemize} |
| 925 |
% We begin with basic definitions. |
| 926 |
% |
| 927 |
% \begin{macro}{\pld@ScanBegingroup} |
| 928 |
% \begin{macro}{\pld@ScanEndgruop} |
| 929 |
% Just what was stated above. |
| 930 |
% \begin{macrocode} |
| 931 |
\def\pld@ScanBegingroup{\begingroup \let\pld@tempoly\@empty} |
| 932 |
\def\pld@ScanEndgroup{\pld@ScanSetA \endgroup} |
| 933 |
% \end{macrocode} |
| 934 |
% \end{macro} |
| 935 |
% \end{macro} |
| 936 |
% |
| 937 |
% \begin{macro}{\pld@ScanSetA} |
| 938 |
% \begin{macro}{\pld@ScanSetB} |
| 939 |
% In the \texttt{calc} package the second routine is called |\scan@initB| (but |
| 940 |
% with registers instead of macros, of course) |
| 941 |
% \begin{macrocode} |
| 942 |
\def\pld@ScanSetA{\global\let\@gtempa\pld@tempoly} |
| 943 |
\def\pld@ScanSetB{\let\pld@tempoly\@gtempa} |
| 944 |
% \end{macrocode} |
| 945 |
% \end{macro} |
| 946 |
% \end{macro} |
| 947 |
% |
| 948 |
% \begin{macro}{\pld@Scan} |
| 949 |
% corresponds to |\calc@assign@generic|. |
| 950 |
% \begin{macrocode} |
| 951 |
\def\pld@Scan#1{% |
| 952 |
\let\pld@tempoly\@empty |
| 953 |
\pld@ScanOpen(#1\relax |
| 954 |
\pld@ScanEndgroup \pld@ScanEndgroup} |
| 955 |
% \end{macrocode} |
| 956 |
% The brake |\relax| terminates the main scanning loop. |
| 957 |
% \begin{macrocode} |
| 958 |
\def\pld@ScanIt#1{% |
| 959 |
\ifx \relax#1\let\pld@next\@gobble \else |
| 960 |
% \end{macrocode} |
| 961 |
% The tokens |+|, |-|, |*|, |\cdot|, |/|, and |)| let us make the according |
| 962 |
% translations. |
| 963 |
% \begin{macrocode} |
| 964 |
\ifx +#1\let\pld@next\pld@ScanAdd\else |
| 965 |
\ifx -#1\let\pld@next\pld@ScanSubtract\else |
| 966 |
\ifx *#1\let\pld@next\pld@ScanMultiply\else |
| 967 |
\ifx \cdot#1\let\pld@next\pld@ScanMultiply\else |
| 968 |
\ifx /#1\let\pld@next\pld@ScanDivide\else |
| 969 |
\ifx )#1\let\pld@next\pld@ScanClose\else |
| 970 |
\ifx ^#1\let\pld@next\pld@ScanPower\else |
| 971 |
% \end{macrocode} |
| 972 |
% Other tokens are `preceeded' by an implicit multiplication, as you will see |
| 973 |
% below. |
| 974 |
% \begin{macrocode} |
| 975 |
\ifx \frac#1\let\pld@next\pld@ScanFrac\else |
| 976 |
\ifx (#1\let\pld@next\pld@ScanOpen\else |
| 977 |
% \end{macrocode} |
| 978 |
% Now we check for a digit and a variable, respectively. If none of them is |
| 979 |
% given, we treat the argument as a symbol. |
| 980 |
% \begin{macrocode} |
| 981 |
\pld@IfNumber{#1}% |
| 982 |
{\let\pld@next\pld@ScanNumeric}% |
| 983 |
{\pld@IfVar{#1}{\let\pld@next\pld@ScanVar}% |
| 984 |
{\let\pld@next\pld@ScanSymbol}}% |
| 985 |
\fi \fi |
| 986 |
\fi \fi \fi \fi \fi \fi \fi \fi |
| 987 |
\pld@next #1} |
| 988 |
% \end{macrocode} |
| 989 |
% For speedy scanning we could alternatively define |
| 990 |
% \begin{verbatim} |
| 991 |
% \def\pld@ScanIt#1{% |
| 992 |
% \expandafter\let\expandafter\pld@next\csname |
| 993 |
% pld@Scan@\string#1\endcsname |
| 994 |
% \ifx\relax\pld@next |
| 995 |
% \pld@IfVar{#1}{\let\pld@next\pld@ScanVar}% |
| 996 |
% {\let\pld@next\pld@ScanSymbol}% |
| 997 |
% \fi |
| 998 |
% \pld@next #1}\end{verbatim} |
| 999 |
% and make appropriate definitions |\pld@Scan@\relax| (one single control |
| 1000 |
% sequence), |\pld@Scan@|$\langle$\texttt{+$\vert$-$\vert$*$\vert$\bslash |
| 1001 |
% cdot$\vert$/$\vert$)$\vert$\textasciicircum$\vert$\bslash frac$\vert$($\vert |
| 1002 |
% $0$\vert\ldots\vert$9}$\rangle$ instead of |\pld@ScanAdd| and all the other |
| 1003 |
% definitions below. |
| 1004 |
% \end{macro} |
| 1005 |
% |
| 1006 |
% \begin{macro}{\pld@IfNumber} |
| 1007 |
% execute the first or second argument depending on whether |#1| is found in |
| 1008 |
% |0123456789|. |
| 1009 |
% \begin{macrocode} |
| 1010 |
\def\pld@IfNumber#1{% |
| 1011 |
\def\pld@temp##1#1##2##3\relax{% |
| 1012 |
\ifx\@empty##2\@empty \expandafter\@secondoftwo |
| 1013 |
\else \expandafter\@firstoftwo \fi}% |
| 1014 |
\pld@temp 0123456789#1\@empty\relax} |
| 1015 |
% \end{macrocode} |
| 1016 |
% \end{macro} |
| 1017 |
% |
| 1018 |
% \begin{macro}{\pld@ScanOpen} |
| 1019 |
% \begin{macro}{\pld@ScanClose} |
| 1020 |
% correspond to |\calc@open| and |\calc@close|. A left parentheses implicitly |
| 1021 |
% inserts a multiplication in many (or even most) cases since the parenthesized |
| 1022 |
% expression must be viewed as a potential sum. |
| 1023 |
% The simple |\pld@ScanImplicitMultiply| in version 0.11 didn't insert a |
| 1024 |
% multiplication when scanning |2(X+1)|, for example. |
| 1025 |
% \begin{macrocode} |
| 1026 |
\def\pld@ScanOpen({% |
| 1027 |
\ifx\@empty\pld@tempoly\else |
| 1028 |
\pld@ScanMultiplyBase\pld@ScanBbyA |
| 1029 |
\fi |
| 1030 |
\pld@ScanBegingroup \aftergroup\pld@ScanSetB |
| 1031 |
\pld@ScanBegingroup \aftergroup\pld@ScanSetB |
| 1032 |
\pld@ScanIt} |
| 1033 |
% \end{macrocode} |
| 1034 |
% \begin{macrocode} |
| 1035 |
\def\pld@ScanClose){% |
| 1036 |
\pld@ScanEndgroup \pld@ScanEndgroup \pld@ScanSetA |
| 1037 |
\pld@ScanIt} |
| 1038 |
% \end{macrocode} |
| 1039 |
% \end{macro} |
| 1040 |
% \end{macro} |
| 1041 |
% |
| 1042 |
% \begin{macro}{\pld@ScanAdd} |
| 1043 |
% \begin{macro}{\pld@ScanSubtract} |
| 1044 |
% correspond to |\calc@add| and |\calc@subtract|. |
| 1045 |
% \begin{macrocode} |
| 1046 |
\def\pld@ScanAdd+{\pld@ScanAddBase\pld@ScanAtoB} |
| 1047 |
\def\pld@ScanSubtract-{\pld@ScanAddBase\pld@ScanAfromB} |
| 1048 |
\def\pld@ScanAddBase#1{% |
| 1049 |
\pld@ScanEndgroup \pld@ScanEndgroup |
| 1050 |
\pld@ScanBegingroup \aftergroup#1% |
| 1051 |
\pld@ScanBegingroup \aftergroup\pld@ScanSetB |
| 1052 |
\pld@ScanIt} |
| 1053 |
% \end{macrocode} |
| 1054 |
% For a subtraction we just add a factor |\pld@R{-1}{1}|. |
| 1055 |
% \begin{macrocode} |
| 1056 |
\def\pld@ScanAtoB{\pld@ExtendPoly\pld@tempoly\@gtempa} |
| 1057 |
\def\pld@ScanAfromB{\pld@ExtendPoly\pld@tempoly{\@gtempa\pld@R{-1}1}} |
| 1058 |
% \end{macrocode} |
| 1059 |
% \end{macro} |
| 1060 |
% \end{macro} |
| 1061 |
% |
| 1062 |
% \begin{macro}{\pld@ScanMultiply} |
| 1063 |
% \begin{macro}{\pld@ScanDivide} |
| 1064 |
% instead of |\calc@multiply| and |\calc@divide|. |
| 1065 |
% \begin{macrocode} |
| 1066 |
\def\pld@ScanMultiply#1{\pld@ScanMultiplyBase\pld@ScanBbyA \pld@ScanIt} |
| 1067 |
\def\pld@ScanDivide/{\pld@ScanMultiplyBase\pld@ScanDivBbyA \pld@ScanIt} |
| 1068 |
\def\pld@ScanMultiplyBase{% |
| 1069 |
\pld@ScanEndgroup \pld@ScanBegingroup \aftergroup} |
| 1070 |
% \end{macrocode} |
| 1071 |
% Division is here adding a fraction, thus this is limited to numbers and |
| 1072 |
% symbols. No variables should appear in the expression after |/|. |
| 1073 |
% \begin{macrocode} |
| 1074 |
\def\pld@ScanBbyA{\pld@MultiplyPoly\pld@tempoly\pld@tempoly\@gtempa} |
| 1075 |
\def\pld@ScanDivBbyA{% |
| 1076 |
\def\pld@temp{\pld@F{}}% |
| 1077 |
\pld@Extend\pld@temp{\expandafter{\@gtempa}}% |
| 1078 |
\pld@Extend\pld@tempoly\pld@temp} |
| 1079 |
% \end{macrocode} |
| 1080 |
% \end{macro} |
| 1081 |
% \end{macro} |
| 1082 |
% |
| 1083 |
% \begin{macro}{\pld@ScanPower} |
| 1084 |
% We calculate the |#1|-th power of |\pld@tempoly| by successive |
| 1085 |
% multiplication. Note that this code---as most code of this package---is not |
| 1086 |
% optimized for speed. |
| 1087 |
% \begin{macrocode} |
| 1088 |
\def\pld@ScanPower^#1{% |
| 1089 |
\let\pld@polya\pld@tempoly |
| 1090 |
\@multicnt#1\relax |
| 1091 |
\loop \ifnum\@multicnt>\@ne |
| 1092 |
\advance\@multicnt\m@ne |
| 1093 |
\pld@MultiplyPoly\pld@tempoly\pld@tempoly\pld@polya |
| 1094 |
\repeat |
| 1095 |
\pld@ScanIt} |
| 1096 |
% \end{macrocode} |
| 1097 |
% \end{macro} |
| 1098 |
% |
| 1099 |
% \begin{macro}{\pld@ScanImplicitMultiply} |
| 1100 |
% inserts a multiplication if and only if the local register B is a sum. |
| 1101 |
% \begin{macrocode} |
| 1102 |
\def\pld@ScanImplicitMultiply{% |
| 1103 |
\expandafter\pld@IfSum\expandafter{\pld@tempoly}% |
| 1104 |
{\pld@ScanMultiplyBase\pld@ScanBbyA}% |
| 1105 |
{}} |
| 1106 |
% \end{macrocode} |
| 1107 |
% \end{macro} |
| 1108 |
% |
| 1109 |
% \begin{macro}{\pld@ScanNumeric} |
| 1110 |
% We assign the integer to a count register and issue an error if it's |
| 1111 |
% fractional. |
| 1112 |
% \begin{macrocode} |
| 1113 |
\def\pld@ScanNumeric{% |
| 1114 |
\pld@ScanImplicitMultiply |
| 1115 |
\let\pld@temp\frac \let\frac\relax |
| 1116 |
\afterassignment\pld@ScanNumeric@ \@tempcnta} |
| 1117 |
\def\pld@ScanNumeric@#1{% |
| 1118 |
\let\frac\pld@temp |
| 1119 |
\ifx #1.% |
| 1120 |
\PackageError{polynom}{noninteger constants not supported}% |
| 1121 |
{Constants must be integers in TeX's word range.\MessageBreak |
| 1122 |
The fractional part will be lost.}% |
| 1123 |
\def\pld@next##1{\afterassignment\pld@ScanIt\@tempcnta}% |
| 1124 |
\else |
| 1125 |
\let\pld@next\pld@ScanIt |
| 1126 |
\fi |
| 1127 |
% \end{macrocode} |
| 1128 |
% We add the integer to the polynomial and continue the scan. |
| 1129 |
% \begin{macrocode} |
| 1130 |
\pld@Extend\pld@tempoly |
| 1131 |
{\expandafter\pld@R\expandafter{\the\@tempcnta}1}% |
| 1132 |
\pld@next #1} |
| 1133 |
% \end{macrocode} |
| 1134 |
% \end{macro} |
| 1135 |
% |
| 1136 |
% \begin{macro}{\pld@ScanVar} |
| 1137 |
% \begin{macro}{\pld@ScanSymbol} |
| 1138 |
% are defined in terms of a submacro. |
| 1139 |
% \begin{macrocode} |
| 1140 |
\def\pld@ScanVar{\pld@ScanImplicitMultiply \pld@ScanVS\pld@V} |
| 1141 |
\def\pld@ScanSymbol{\pld@ScanImplicitMultiply \pld@ScanVS\pld@S} |
| 1142 |
% \end{macrocode} |
| 1143 |
% The submacro checks all super- and subscript variations and adds |
| 1144 |
% the data to the polynomial. The suffixes |u| and |l| stand for |
| 1145 |
% upper and lower. |
| 1146 |
% \begin{macrocode} |
| 1147 |
\def\pld@ScanVS#1#2{% |
| 1148 |
\@ifnextchar^{\pld@ScanVS@u#1{#2}}% |
| 1149 |
{\@ifnextchar_{\pld@ScanVar@l#1{#2}}% |
| 1150 |
{\pld@AddTo\pld@tempoly{#1{#2}1}% |
| 1151 |
\pld@ScanIt}}} |
| 1152 |
% \end{macrocode} |
| 1153 |
% \begin{macrocode} |
| 1154 |
\def\pld@ScanVS@u#1#2^#3{% |
| 1155 |
\@ifnextchar_{\pld@ScanVS@ul#1{#2}{#3}}% |
| 1156 |
{\pld@AddTo\pld@tempoly{#1{#2}{#3}}\pld@ScanIt}} |
| 1157 |
\def\pld@ScanVS@l#1#2_#3{% |
| 1158 |
\@ifnextchar^{\pld@ScanVS@lu#1{#2}{#3}}% |
| 1159 |
{\pld@AddTo\pld@tempoly{#1{#2_{#3}}1}\pld@ScanIt}} |
| 1160 |
% \end{macrocode} |
| 1161 |
% \begin{macrocode} |
| 1162 |
\def\pld@ScanVS@ul#1#2#3_#4{% |
| 1163 |
\pld@AddTo\pld@tempoly{#1{#2_{#4}}{#3}}\pld@ScanIt} |
| 1164 |
\def\pld@ScanVS@lu#1#2#3^#4{% |
| 1165 |
\pld@AddTo\pld@tempoly{#1{#2_{#3}}{#4}}\pld@ScanIt} |
| 1166 |
% \end{macrocode} |
| 1167 |
% \end{macro} |
| 1168 |
% \end{macro} |
| 1169 |
% |
| 1170 |
% \begin{macro}{\pld@ScanFrac} |
| 1171 |
% A fraction scans nominator and denominator seperately and add them to the |
| 1172 |
% current |\pld@tempoly|. |
| 1173 |
% \begin{macrocode} |
| 1174 |
\def\pld@ScanFrac#1#2#3{% |
| 1175 |
\pld@ScanImplicitMultiply |
| 1176 |
\begingroup |
| 1177 |
\pld@Scan{#2}\pld@AddTo\pld@tempoly\relax |
| 1178 |
\global\let\@gtempa\pld@tempoly |
| 1179 |
\endgroup |
| 1180 |
\pld@Extend\pld@tempoly{\expandafter\pld@F\expandafter{\@gtempa}}% |
| 1181 |
% \end{macrocode} |
| 1182 |
% \begin{macrocode} |
| 1183 |
\begingroup |
| 1184 |
\pld@Scan{#3}\pld@AddTo\pld@tempoly\relax |
| 1185 |
\global\let\@gtempa\pld@tempoly |
| 1186 |
\endgroup |
| 1187 |
\pld@Extend\pld@tempoly{\expandafter{\@gtempa}}% |
| 1188 |
% \end{macrocode} |
| 1189 |
% \begin{macrocode} |
| 1190 |
\pld@ScanIt} |
| 1191 |
% \end{macrocode} |
| 1192 |
% \end{macro} |
| 1193 |
% |
| 1194 |
% |
| 1195 |
% \section{Basic printing} |
| 1196 |
% |
| 1197 |
% \begin{macro}{\pld@PrintPoly} |
| 1198 |
% \meta{polynomial macro} |
| 1199 |
% \begin{describe} |
| 1200 |
% prints the polynomial represented by the macro contents. An empty macro |
| 1201 |
% stands for `0'. |
| 1202 |
% \end{describe} |
| 1203 |
% The implementation follows that description and uses |\pld@PrintMonoms|. |
| 1204 |
% \begin{macrocode} |
| 1205 |
\def\pld@PrintPolyArg#1{% |
| 1206 |
\def\pld@temp{#1}\pld@PrintPoly\pld@temp} |
| 1207 |
\def\pld@PrintPoly#1{% |
| 1208 |
\ifx\@empty#1\@empty 0\else |
| 1209 |
\pld@firsttrue \expandafter\pld@PrintMonoms#1+\relax+% |
| 1210 |
\fi} |
| 1211 |
% \end{macrocode} |
| 1212 |
% \end{macro} |
| 1213 |
% |
| 1214 |
% \begin{macro}{\pld@PrintPolyWithDelims} |
| 1215 |
% \meta{polynomial macro} |
| 1216 |
% \begin{describe} |
| 1217 |
% prints the polynomial represented by the macro contents. The polynomial is |
| 1218 |
% enclosed in the user defined delimiters except when the polynomial is a |
| 1219 |
% single summand. Then just that summand is printed. |
| 1220 |
% \end{describe} |
| 1221 |
% According to the result of |\pld@IfSum| we insert the delimiters. |
| 1222 |
% \begin{macrocode} |
| 1223 |
\def\pld@PrintPolyWithDelimsArg#1{% |
| 1224 |
\def\pld@temp{#1}\pld@PrintPolyWithDelims\pld@temp} |
| 1225 |
\def\pld@PrintPolyWithDelims#1{% |
| 1226 |
\ifx\@empty#1\@empty 0\else |
| 1227 |
\pld@firsttrue |
| 1228 |
\expandafter\pld@IfSum\expandafter{#1}\pld@true\pld@false |
| 1229 |
\pld@if \pld@leftdelim |
| 1230 |
\expandafter\pld@PrintMonoms#1+\relax+% |
| 1231 |
\pld@rightdelim |
| 1232 |
\else \expandafter\pld@PrintMonoms#1+\relax+\fi |
| 1233 |
\fi} |
| 1234 |
% \end{macrocode} |
| 1235 |
% \end{macro} |
| 1236 |
% |
| 1237 |
% \begin{macro}{\pld@PrintInit} |
| 1238 |
% is called before we print a factor of a monomial. First we have to `reset' |
| 1239 |
% |\pld@R| to its primary definition. Its special definition below suppresses |
| 1240 |
% the output of a factor `1' if this factor is not required. |
| 1241 |
% \begin{macrocode} |
| 1242 |
\def\pld@PrintInit{% |
| 1243 |
\let\pld@R\pld@PrintRational \strut |
| 1244 |
% \end{macrocode} |
| 1245 |
% The switch |\pld@iffirst| indicates whether we are working on the first |
| 1246 |
% summand of a polynomial, that means whether or not we can omit a plus. |
| 1247 |
% According to the value of the accumulator, we print its sign and/or value. |
| 1248 |
% \begin{macrocode} |
| 1249 |
\pld@AccuIfNegative |
| 1250 |
{\pld@AccuNegate \pld@iffirst\pld@minustrue\else{}\fi -}% |
| 1251 |
{\pld@iffirst\pld@minusfalse\else{}+\fi}% |
| 1252 |
\pld@firstfalse |
| 1253 |
\pld@AccuIfAbsOne{}{\pld@AccuPrint \pld@true}% |
| 1254 |
% \end{macrocode} |
| 1255 |
% The switch |\pld@if| is set true if and only if we have printed something |
| 1256 |
% for the monomial (except the sign). So we know when to insert the omitted |
| 1257 |
% factor `1'. |
| 1258 |
% |
| 1259 |
% At the end of |\pld@PrintInit|, the macro throws away itself since all the |
| 1260 |
% `initialisation' done here is necessary only once for a summand. Note that |
| 1261 |
% this is done inside a group below, thus the meaning is not lost. |
| 1262 |
% \begin{macrocode} |
| 1263 |
\let\pld@PrintInit\@empty} |
| 1264 |
% \end{macrocode} |
| 1265 |
% \begin{macrocode} |
| 1266 |
\def\pld@minustrue{\global\let\pld@ifminus\iftrue} |
| 1267 |
\def\pld@minusfalse{\global\let\pld@ifminus\iffalse} |
| 1268 |
% \end{macrocode} |
| 1269 |
% \end{macro} |
| 1270 |
% |
| 1271 |
% \begin{macro}{\pld@iffirst} |
| 1272 |
% from above. |
| 1273 |
% \begin{macrocode} |
| 1274 |
\def\pld@firsttrue{\global\let\pld@iffirst\iftrue} |
| 1275 |
\def\pld@firstfalse{\global\let\pld@iffirst\iffalse} |
| 1276 |
\pld@firstfalse |
| 1277 |
% \end{macrocode} |
| 1278 |
% \end{macro} |
| 1279 |
% |
| 1280 |
% \begin{macro}{\pld@PrintMonoms} |
| 1281 |
% While not reaching the end of the polynomial, the accumulator gets `1' and |
| 1282 |
% we redefine |\pld@R|,\ldots,|\pld@V|. For example, a rational is just saved |
| 1283 |
% by multiplying it with the current accumulator. |
| 1284 |
% \begin{macrocode} |
| 1285 |
\def\pld@PrintMonoms#1+{% |
| 1286 |
\ifx\relax#1\else |
| 1287 |
\begingroup |
| 1288 |
\pld@AccuSetX11% |
| 1289 |
\let\pld@R\pld@AccuMul |
| 1290 |
\let\pld@F\pld@PrintFrac |
| 1291 |
\let\pld@S\pld@PrintSymbol |
| 1292 |
\let\pld@V\pld@PrintSymbol |
| 1293 |
% \end{macrocode} |
| 1294 |
% Then we indicate that nothing has been printed so far, print the factors (if |
| 1295 |
% any) by executing the code, and finally print the accumulator if necessary. |
| 1296 |
% \begin{macrocode} |
| 1297 |
\pld@false |
| 1298 |
#1% |
| 1299 |
\pld@PrintInit |
| 1300 |
\pld@if\else \pld@AccuPrint \fi |
| 1301 |
\endgroup |
| 1302 |
\expandafter\pld@PrintMonoms |
| 1303 |
\fi} |
| 1304 |
% \end{macrocode} |
| 1305 |
% \end{macro} |
| 1306 |
% |
| 1307 |
% \begin{macro}{\pld@PrintRational} |
| 1308 |
% is |\pld@R|: indicate that we print something, load the accumulator, and |
| 1309 |
% print it. Note that we don't need to call |\pld@PrintInit| since it has |
| 1310 |
% already been done! |
| 1311 |
% \begin{macrocode} |
| 1312 |
\def\pld@PrintRational#1#2{% |
| 1313 |
\pld@true \pld@AccuSetX{#1}{#2}\pld@AccuPrint} |
| 1314 |
% \end{macrocode} |
| 1315 |
% \end{macro} |
| 1316 |
% |
| 1317 |
% \begin{macro}{\pld@PrintSymbol} |
| 1318 |
% is |\pld@S| or |\pld@V|: init, indicate, and print the symbol with exponent. |
| 1319 |
% \begin{macrocode} |
| 1320 |
\def\pld@PrintSymbol#1#2{% |
| 1321 |
\pld@PrintInit \pld@true #1\ifnum#2=\@ne\else^{#2}\fi} |
| 1322 |
% \end{macrocode} |
| 1323 |
% \end{macro} |
| 1324 |
% |
| 1325 |
% \begin{macro}{\pld@PrintFrac} |
| 1326 |
% is |\pld@F|: init, indicate, and print the fraction \ldots\space no, wait! |
| 1327 |
% First we check whether the denominator is `1'. In that case we don't use a |
| 1328 |
% fraction; we enclose the nominator in delimiters if necessary. |
| 1329 |
% \begin{macrocode} |
| 1330 |
\def\pld@PrintFrac#1#2{% |
| 1331 |
\pld@PrintInit \pld@true |
| 1332 |
\ifx\@empty#2\@empty |
| 1333 |
\begingroup |
| 1334 |
\pld@IfSum{#1}\pld@true\pld@false |
| 1335 |
\pld@if\pld@leftdelim #1\pld@rightdelim\else #1\fi |
| 1336 |
\endgroup |
| 1337 |
\expandafter\@gobbletwo |
| 1338 |
\else |
| 1339 |
\expandafter\pld@PrintFrac@ |
| 1340 |
\fi |
| 1341 |
{#1}{#2}} |
| 1342 |
% \end{macrocode} |
| 1343 |
% Otherwise we check the nominator. |
| 1344 |
% \begin{macrocode} |
| 1345 |
\def\pld@PrintFrac@#1#2{% |
| 1346 |
\ifx\@empty#1\@empty \frac1{#2}\else \frac{#1}{#2}\fi} |
| 1347 |
% \end{macrocode} |
| 1348 |
% \end{macro} |
| 1349 |
% |
| 1350 |
% These printing routines do \emph{not} handle all representations which are |
| 1351 |
% legal in the sense of section \ref{sInternalDataFormat}. For example, |
| 1352 |
% |\pld@R{1}{1}\pld@V{X}{1}\pld@R{-1}{1}| would be printed as $X-1$. |
| 1353 |
% A correct output is guaranteed if there is at most one rational at the |
| 1354 |
% very beginning of the representation. Thus we normalize the internal data: |
| 1355 |
% condense, sort and simplify factors and summands. This will keep us busy |
| 1356 |
% for the next (p)ages. |
| 1357 |
% |
| 1358 |
% |
| 1359 |
% \section{Simplifying} |
| 1360 |
% |
| 1361 |
% |
| 1362 |
% \subsection{Phase I: Condense factors of summands} |
| 1363 |
% |
| 1364 |
% \begin{macro}{\pld@CondenseFactors} |
| 1365 |
% \meta{polynomial macro} |
| 1366 |
% \begin{describe} |
| 1367 |
% Here we condense rationals, fractions, and the exponents of symbolic factors |
| 1368 |
% and variables of each monomial in \meta{polynomial macro}. Afterwards the |
| 1369 |
% factors appear exactly in this order: if present a rational number comes |
| 1370 |
% first, then fractions if any, then symbols, and finally the variables. |
| 1371 |
% For example, the representation of |
| 1372 |
% {\makeatletter\pld@Scan{\frac23\frac1e5X^3\frac32X} |
| 1373 |
% $\pld@PrintPoly\pld@tempoly$ |
| 1374 |
% becomes \pld@CondenseFactors\pld@tempoly |
| 1375 |
% $\pld@PrintPoly\pld@tempoly$}. |
| 1376 |
% \end{describe} |
| 1377 |
% As for printing, we redefine |\pld@R|,\ldots,|\pld@V| and iterate through |
| 1378 |
% the monomials. |
| 1379 |
% \begin{macrocode} |
| 1380 |
\def\pld@CondenseFactors#1{% |
| 1381 |
\ifx\@empty#1\else |
| 1382 |
\begingroup |
| 1383 |
\let\pld@R\pld@AccuMul |
| 1384 |
\let\pld@F\pld@CondenseFrac |
| 1385 |
\let\pld@S\pld@CFSymbol |
| 1386 |
\let\pld@V\pld@CFVar |
| 1387 |
% \end{macrocode} |
| 1388 |
% The following two assignments allow |#1| to be |\pld@temp| or |\pld@tempoly|. |
| 1389 |
% \begin{macrocode} |
| 1390 |
\let\pld@temp#1\let\pld@tempoly\@empty |
| 1391 |
\expandafter\pld@CF@loop\pld@temp+\relax+% |
| 1392 |
\global\let\@gtempa\pld@tempoly |
| 1393 |
\endgroup |
| 1394 |
\let#1\@gtempa |
| 1395 |
\fi} |
| 1396 |
% \end{macrocode} |
| 1397 |
% For each monomial the factors are kept in separate `registers': rationals in |
| 1398 |
% the accumulator, fractions in |\pld@frac|, symbols in |\pld@symbols|, and |
| 1399 |
% variables in |\pld@vars|. |\pld@if| is set true if and only if there is a |
| 1400 |
% `general' fraction. |
| 1401 |
% \begin{macrocode} |
| 1402 |
\def\pld@CF@loop#1+{% |
| 1403 |
\ifx\relax#1\else |
| 1404 |
\begingroup |
| 1405 |
\pld@AccuSetX11% |
| 1406 |
\def\pld@frac{{}{}}\let\pld@symbols\@empty\let\pld@vars\@empty |
| 1407 |
\pld@false |
| 1408 |
#1% |
| 1409 |
\let\pld@temp\@empty |
| 1410 |
% \end{macrocode} |
| 1411 |
% Now we put together the collected data (if the rational constant is not |
| 1412 |
% zero): If the rational constant does not equal one, we place it in front of |
| 1413 |
% all other factors. |
| 1414 |
% \begin{macrocode} |
| 1415 |
\pld@AccuIfZero{}% |
| 1416 |
{\pld@AccuIfOne{}{\pld@AccuGet\pld@temp |
| 1417 |
\edef\pld@temp{\noexpand\pld@R\pld@temp}}% |
| 1418 |
% \end{macrocode} |
| 1419 |
% Then follow fractions, symbols and variables.^^A |
| 1420 |
% \footnote{This is the right place to \emph{simplify} general fractions and |
| 1421 |
% symbols. Here we are in the special case that they don't contain |
| 1422 |
% any rationals as `over all' factors except `$1=|\{\}|={}$ empty |
| 1423 |
% argument' in the nominator or denominator, e.g.~$\frac{a+b}{b+a}$ is |
| 1424 |
% now represented as $\frac{a+b}1\frac1{b+a}$.} |
| 1425 |
% \begin{macrocode} |
| 1426 |
\pld@if \pld@Extend\pld@temp{\expandafter\pld@F\pld@frac}\fi |
| 1427 |
\expandafter\pld@CF@loop@\pld@symbols\relax\@empty |
| 1428 |
\expandafter\pld@CF@loop@\pld@vars\relax\@empty |
| 1429 |
% \end{macrocode} |
| 1430 |
% Finally we add the result to |\pld@tempoly|. Note that |\endgroup| destroys |
| 1431 |
% all temporary garbage, for example the exponents of symbols and variables. |
| 1432 |
% \begin{macrocode} |
| 1433 |
\ifx\@empty\pld@temp |
| 1434 |
\def\pld@temp{\pld@R11}% |
| 1435 |
\fi}% |
| 1436 |
\global\let\@gtempa\pld@temp |
| 1437 |
\endgroup |
| 1438 |
\ifx\@empty\@gtempa\else |
| 1439 |
\pld@ExtendPoly\pld@tempoly\@gtempa |
| 1440 |
\fi |
| 1441 |
\expandafter\pld@CF@loop |
| 1442 |
\fi} |
| 1443 |
% \end{macrocode} |
| 1444 |
% For each symbol or variable, we look up the exponent |\pld@@|\meta{symbol} |
| 1445 |
% and add it together with |\pld@|\meta{\textup{\texttt{S}$\vert$\texttt{V}}} |
| 1446 |
% and the symbol to the summand |\pld@temp|. |
| 1447 |
% \begin{macrocode} |
| 1448 |
\def\pld@CF@loop@#1#2{% |
| 1449 |
\ifx\relax#1\else |
| 1450 |
\xdef\@gtempa{\csname pld@@\string#2\endcsname}% |
| 1451 |
\ifnum \@gtempa=\z@ \else |
| 1452 |
\pld@AddTo\pld@temp{#1{#2}}% |
| 1453 |
\pld@Extend\pld@temp{\expandafter{\@gtempa}}% |
| 1454 |
\fi |
| 1455 |
\expandafter\pld@CF@loop@ |
| 1456 |
\fi} |
| 1457 |
% \end{macrocode} |
| 1458 |
% \end{macro} |
| 1459 |
% |
| 1460 |
% \begin{macro}{\pld@CFSymbol} |
| 1461 |
% \begin{macro}{\pld@CFVar} |
| 1462 |
% These definitions collect the exponents. Here we only insert type arguments. |
| 1463 |
% \begin{macrocode} |
| 1464 |
\def\pld@CFSymbol{\pld@CFSV\pld@symbols\pld@S} |
| 1465 |
\def\pld@CFVar{\pld@CFSV\pld@vars\pld@V} |
| 1466 |
% \end{macrocode} |
| 1467 |
% A new symbol initializes |\pld@@|\meta{symbol} to the current exponent and |
| 1468 |
% adds the symbol to the list, whereas \ldots |
| 1469 |
% \begin{macrocode} |
| 1470 |
\def\pld@CFSV#1#2#3#4{% |
| 1471 |
\@ifundefined{pld@@\string#3}% |
| 1472 |
{\@namedef{pld@@\string#3}{#4}% |
| 1473 |
\pld@AddTo#1{#2{#3}}}% |
| 1474 |
% \end{macrocode} |
| 1475 |
% an existing one increases |\pld@@|\meta{symbol}. |
| 1476 |
% \begin{macrocode} |
| 1477 |
{\@tempcnta\csname pld@@\string#3\endcsname\relax |
| 1478 |
\advance\@tempcnta#4\relax |
| 1479 |
\expandafter\edef\csname pld@@\string#3\endcsname{\the\@tempcnta}}} |
| 1480 |
% \end{macrocode} |
| 1481 |
% \end{macro} |
| 1482 |
% \end{macro} |
| 1483 |
% |
| 1484 |
% \begin{macro}{\pld@CondenseFrac} |
| 1485 |
% For a fraction, we work on the nominator and denominator separately: a sum |
| 1486 |
% is added to |\pld@frac|, otherwise we `execute' the code---but of course the |
| 1487 |
% reciprocal of the denominator. This adds the appropriate data. |
| 1488 |
% \begin{macrocode} |
| 1489 |
\def\pld@CondenseFrac#1#2{% |
| 1490 |
\pld@IfSum{#1}{\pld@CFFracAdd{\pld@F{#1}{}}{}}% |
| 1491 |
{#1}% |
| 1492 |
\pld@IfSum{#2}{\pld@CFFracAdd{}{\pld@F{}{#2}}}% |
| 1493 |
{\begingroup |
| 1494 |
\pld@DefInverse\pld@temp{#2}% |
| 1495 |
\global\let\@gtempa\pld@temp |
| 1496 |
\endgroup |
| 1497 |
\@gtempa}} |
| 1498 |
% \end{macrocode} |
| 1499 |
% \end{macro} |
| 1500 |
% |
| 1501 |
% \begin{macro}{\pld@CFFracAdd} |
| 1502 |
% We just add the nominator and denominator to |\pld@frac| and indicate this |
| 1503 |
% by setting |\pld@if| true. |
| 1504 |
% \begin{macrocode} |
| 1505 |
\def\pld@CFFracAdd{\pld@true \expandafter\pld@CFFracAdd@\pld@frac} |
| 1506 |
\def\pld@CFFracAdd@#1#2#3#4{\def\pld@frac{{#1#3}{#2#4}}} |
| 1507 |
% \end{macrocode} |
| 1508 |
% \end{macro} |
| 1509 |
% |
| 1510 |
% |
| 1511 |
% \subsection{Phase III: Condense monomials of same type} |
| 1512 |
% |
| 1513 |
% \begin{macro}{\pld@CondenseMonomials} |
| 1514 |
% $\langle$|\pld@true|$\vert$|pld@false|$\rangle$\meta{polynomial macro} |
| 1515 |
% \begin{describe} |
| 1516 |
% This definition sums up the monomials of \meta{polynomial macro}. |
| 1517 |
% For example, the representation of |
| 1518 |
% {\makeatletter\pld@Scan{X^2+e^{-1}X^2}^^A |
| 1519 |
% $\pld@PrintPoly\pld@tempoly$ |
| 1520 |
% becomes \pld@CondenseMonomials\pld@true\pld@tempoly |
| 1521 |
% $\pld@PrintPoly\pld@tempoly$}. |
| 1522 |
% If and only if the first argument is |\pld@false|, the macro works on |
| 1523 |
% symbols instead of variables, for example |
| 1524 |
% {\makeatletter\pld@Scan{1-\pi+2\pi+e^{-1}+e^{-1}}^^A |
| 1525 |
% \pld@CondenseFactors\pld@tempoly |
| 1526 |
% $\pld@PrintPoly\pld@tempoly$ |
| 1527 |
% becomes \pld@CondenseMonomials\pld@false\pld@tempoly |
| 1528 |
% $\pld@PrintPoly\pld@tempoly$}. |
| 1529 |
% \end{describe} |
| 1530 |
% Again we redefine |\pld@R|,\ldots,|\pld@V|. Here they will add their |
| 1531 |
% arguments to the current summand. To condense a sum of constants, i.e.~to |
| 1532 |
% work on symbols, we need to redefine two more macros and sort the constants |
| 1533 |
% first. To understand this, read ahead and notice the paragraph at the end |
| 1534 |
% of this subsection. |
| 1535 |
% \begin{macrocode} |
| 1536 |
\def\pld@CondenseMonomials#1#2{% |
| 1537 |
\ifx\@empty#2\else |
| 1538 |
\begingroup |
| 1539 |
#1% |
| 1540 |
\pld@if\else |
| 1541 |
\let\pld@SortVars@V\pld@SortVars@S |
| 1542 |
\let\pld@SplitMonom\pld@SplitMonomS |
| 1543 |
\pld@SortMonomials#2% |
| 1544 |
\fi |
| 1545 |
\let\pld@R\pld@CMRational |
| 1546 |
\let\pld@F\pld@CMFrac |
| 1547 |
\let\pld@S\pld@CMSymbol |
| 1548 |
\let\pld@V\pld@CMError |
| 1549 |
% \end{macrocode} |
| 1550 |
% Initialize temporary macros, expand the polynomial and work on it, and |
| 1551 |
% assign the result back to |#1|. |
| 1552 |
% \begin{macrocode} |
| 1553 |
\let\pld@temp#2\let\pld@tempoly\@empty |
| 1554 |
\pld@AccuSetX01\let\pld@symbols\@empty \let\pld@monom\relax |
| 1555 |
\expandafter\pld@CM@\pld@temp+\relax+% |
| 1556 |
\global\let\@gtempa\pld@tempoly |
| 1557 |
\endgroup |
| 1558 |
\let#2\@gtempa |
| 1559 |
\fi} |
| 1560 |
% \end{macrocode} |
| 1561 |
% Reaching the end of the polynomial, we just add the last summand to the |
| 1562 |
% temporary polynomial. Otherwise the monomial is split into `factors' and |
| 1563 |
% `variables', which are handled by |\pld@CM@do|. Afterwards we proceed to |
| 1564 |
% the next summand. |
| 1565 |
% \begin{macrocode} |
| 1566 |
\def\pld@CM@#1+{% |
| 1567 |
\ifx\relax#1\relax |
| 1568 |
\pld@CMAddToTempoly |
| 1569 |
\else |
| 1570 |
\pld@SplitMonom\pld@CM@do{#1}% |
| 1571 |
\expandafter\pld@CM@ |
| 1572 |
\fi} |
| 1573 |
% \end{macrocode} |
| 1574 |
% The following macro gets the nonvariable and variable part as arguments. If |
| 1575 |
% we haven't worked on a summand yet, we don't need to do anything special. At |
| 1576 |
% the end of the macro we will add the nonvariable part to the currently empty |
| 1577 |
% nonvariable part. |
| 1578 |
% \begin{macrocode} |
| 1579 |
\def\pld@CM@do#1#2{% |
| 1580 |
\ifx\pld@monom\relax \else |
| 1581 |
% \end{macrocode} |
| 1582 |
% Otherwise we check whether the last and current monomials are of same type. |
| 1583 |
% Note that this simple |\ifx| requires the variables being in the same order.^^A |
| 1584 |
% \footnote{It's better to use |\bslash pld@IfMonomE|, but even this requires |
| 1585 |
% the mentioned restriction.} |
| 1586 |
% If the monomials are different, we add the last monomial to the temporary |
| 1587 |
% polynomial and initialize some macros again. |
| 1588 |
% \begin{macrocode} |
| 1589 |
\def\pld@temp{#2}% |
| 1590 |
\ifx\pld@temp\pld@monom \else |
| 1591 |
\pld@CMAddToTempoly |
| 1592 |
\pld@AccuSetX01\let\pld@symbols\@empty \let\pld@monom\relax |
| 1593 |
\fi |
| 1594 |
\fi |
| 1595 |
% \end{macrocode} |
| 1596 |
% In any case we add the nonvariable part to the current (possibly cleared) |
| 1597 |
% one. |
| 1598 |
% \begin{macrocode} |
| 1599 |
\let\pld@op+% |
| 1600 |
\ifx\@empty#1\@empty \pld@R11\relax \else #1\relax \fi |
| 1601 |
\def\pld@monom{#2}} |
| 1602 |
% \end{macrocode} |
| 1603 |
% \end{macro} |
| 1604 |
% |
| 1605 |
% \begin{macro}{\pld@CMAddToTempoly} |
| 1606 |
% According to whether the accumulator is zero, we save the value in |
| 1607 |
% |\pld@temp| or just empty that macro. |
| 1608 |
% \begin{macrocode} |
| 1609 |
\def\pld@CMAddToTempoly{% |
| 1610 |
\pld@AccuIfZero{\let\pld@temp\@empty}% |
| 1611 |
{\pld@AccuGet\pld@temp |
| 1612 |
\edef\pld@temp{\noexpand\pld@R\pld@temp}}% |
| 1613 |
% \end{macrocode} |
| 1614 |
% Then we we simplify the (possible) sum of symbols by calling the main |
| 1615 |
% definition of this section with |\pld@false|. Afterwards we append the |
| 1616 |
% symbols if necessary. |
| 1617 |
% \begin{macrocode} |
| 1618 |
\pld@CondenseMonomials\pld@false\pld@symbols |
| 1619 |
\ifx\pld@symbols\@empty \else |
| 1620 |
\pld@ExtendPoly\pld@temp\pld@symbols |
| 1621 |
\fi |
| 1622 |
% \end{macrocode} |
| 1623 |
% Now depending on the contents of |\pld@temp|, we do nothing---the sum of the |
| 1624 |
% monomials is zero---or add the factors together with the `variable part' to |
| 1625 |
% the polynomial. Note that we put |\pld@F{| and |}{}| around a sum if and |
| 1626 |
% only if |\pld@if| is true.\footnote{Why we don't need a fraction and also |
| 1627 |
% don't want it in the other case? We don't want it since it would make things |
| 1628 |
% more complex. We don't need it since, if we strip off both variables and |
| 1629 |
% symbols, there are only rationals left and these are evaluated completely |
| 1630 |
% and condensed in one single rational---no sum, no need for a fraction.} |
| 1631 |
% \begin{macrocode} |
| 1632 |
\ifx\pld@temp\@empty \else |
| 1633 |
\pld@if |
| 1634 |
\expandafter\pld@IfSum\expandafter{\pld@temp}% |
| 1635 |
{\expandafter\def\expandafter\pld@temp\expandafter |
| 1636 |
{\expandafter\pld@F\expandafter{\pld@temp}{}}}% |
| 1637 |
{}% |
| 1638 |
\fi |
| 1639 |
\pld@ExtendPoly\pld@tempoly\pld@temp |
| 1640 |
\pld@Extend\pld@tempoly{\pld@monom}% |
| 1641 |
\fi} |
| 1642 |
% \end{macrocode} |
| 1643 |
% \end{macro} |
| 1644 |
% |
| 1645 |
% \begin{macro}{\pld@CMFracAdd} |
| 1646 |
% Depending on the current operator |\pld@op|, we add a new summand to |
| 1647 |
% |\pld@symbols| or extend the last summand by another factor. |
| 1648 |
% \begin{macrocode} |
| 1649 |
\def\pld@CMFracAdd{% |
| 1650 |
\ifx +\pld@op \let\pld@op\@empty |
| 1651 |
\expandafter\pld@AddToPoly |
| 1652 |
\else \expandafter\pld@AddTo \fi |
| 1653 |
\pld@symbols} |
| 1654 |
% \end{macrocode} |
| 1655 |
% \end{macro} |
| 1656 |
% |
| 1657 |
% \begin{macro}{\pld@CMRational} |
| 1658 |
% We add the rational to the accumulator if and only if there is no other |
| 1659 |
% (symbolic or fractional) factor. This is the reason for some |\relax|es |
| 1660 |
% above and below. |
| 1661 |
% \begin{macrocode} |
| 1662 |
\def\pld@CMRational#1#2#3{% |
| 1663 |
\ifx\relax#3% |
| 1664 |
\pld@AccuAdd{#1}{#2}% |
| 1665 |
\else |
| 1666 |
% \end{macrocode} |
| 1667 |
% If the rational belongs to a more complex factor, we add it to |
| 1668 |
% |\pld@symbols|. Note that the used macro was redefined above. |
| 1669 |
% \begin{macrocode} |
| 1670 |
\pld@CMFracAdd{\pld@R{#1}{#2}}% |
| 1671 |
\expandafter#3% |
| 1672 |
\fi} |
| 1673 |
% \end{macrocode} |
| 1674 |
% \end{macro} |
| 1675 |
% |
| 1676 |
% \begin{macro}{\pld@CMSymbol} |
| 1677 |
% A symbol is just copied. |
| 1678 |
% \begin{macrocode} |
| 1679 |
\def\pld@CMSymbol#1#2{\pld@CMFracAdd{\pld@S{#1}{#2}}}% |
| 1680 |
% \end{macrocode} |
| 1681 |
% \end{macro} |
| 1682 |
% |
| 1683 |
% \begin{macro}{\pld@CMFrac} |
| 1684 |
% Here we remove a possibly inserted |\pld@F{| and |}{}| around a sum and |
| 1685 |
% execute the nominator---or we add the (condensed) fraction. |
| 1686 |
% \begin{macrocode} |
| 1687 |
\def\pld@CMFrac#1#2{% |
| 1688 |
\ifx\@empty#2\@empty |
| 1689 |
\pld@CMFrac@nom#1+\relax+% |
| 1690 |
\else |
| 1691 |
\pld@CMFrac@{#1}{#2}% |
| 1692 |
\fi} |
| 1693 |
% \end{macrocode} |
| 1694 |
% \begin{macrocode} |
| 1695 |
\def\pld@CMFrac@#1#2{% |
| 1696 |
\pld@IfSum{#1}{\pld@CMFracAdd{\pld@F{#1}{}}}% |
| 1697 |
{#1}% |
| 1698 |
\pld@IfSum{#2}{\pld@CMFracAdd{\pld@F{}{#2}}}% |
| 1699 |
{\begingroup |
| 1700 |
\pld@DefInverse\pld@temp{#2}% |
| 1701 |
\global\let\@gtempa\pld@temp |
| 1702 |
\endgroup |
| 1703 |
\@gtempa}} |
| 1704 |
% \end{macrocode} |
| 1705 |
% A nominator is executed summand by summand. |
| 1706 |
% \begin{macrocode} |
| 1707 |
\def\pld@CMFrac@nom#1+{% |
| 1708 |
\ifx\relax#1\else |
| 1709 |
#1\relax |
| 1710 |
\expandafter\pld@CMFrac@nom |
| 1711 |
\fi} |
| 1712 |
% \end{macrocode} |
| 1713 |
% \end{macro} |
| 1714 |
% |
| 1715 |
% In this section we have made two assumptions: (a) \emph{variables have |
| 1716 |
% always the same order in monomials} and (b) \emph{monomials of the same |
| 1717 |
% type}---that means at most different in the nonvariable part---\emph{must |
| 1718 |
% follow each other}. The first condition has already been mentioned, the |
| 1719 |
% second is necessary for looking only at the next monomial to check whether |
| 1720 |
% we have to summarize their preceeding factors. Both are established in the |
| 1721 |
% following section. |
| 1722 |
% |
| 1723 |
% |
| 1724 |
% \subsection{Phase II: Sort monomials by type} |
| 1725 |
% |
| 1726 |
% \begin{macro}{\pld@SortMonomials} |
| 1727 |
% We first sort the variables of each monomial and then the monomials. |
| 1728 |
% \begin{macrocode} |
| 1729 |
\def\pld@SortMonomials#1{% |
| 1730 |
\ifx #1\@empty \else |
| 1731 |
\begingroup |
| 1732 |
% \end{macrocode} |
| 1733 |
% \begin{macrocode} |
| 1734 |
\let\pld@temp#1\let\pld@tempoly\@empty |
| 1735 |
\expandafter\pld@SortVars\pld@temp+\relax+% |
| 1736 |
% \end{macrocode} |
| 1737 |
% \begin{macrocode} |
| 1738 |
\let\pld@temp\pld@tempoly \let\pld@tempoly\@empty |
| 1739 |
\expandafter\pld@SortSummands\pld@temp+\relax+% |
| 1740 |
% \end{macrocode} |
| 1741 |
% \begin{macrocode} |
| 1742 |
\global\let\@gtempa\pld@tempoly |
| 1743 |
\endgroup |
| 1744 |
\let#1\@gtempa |
| 1745 |
\fi} |
| 1746 |
% \end{macrocode} |
| 1747 |
% \end{macro} |
| 1748 |
% |
| 1749 |
% \begin{macro}{\pld@SortVars} |
| 1750 |
% While not reaching the end \ldots |
| 1751 |
% \begin{macrocode} |
| 1752 |
\def\pld@SortVars#1+{% |
| 1753 |
\ifx\relax#1\relax\else |
| 1754 |
\pld@SplitMonom\pld@SortVars@{#1}% |
| 1755 |
\expandafter\pld@SortVars |
| 1756 |
\fi} |
| 1757 |
% \end{macrocode} |
| 1758 |
% we sort the variables if necessary---note the redefinitions of |\pld@V| and |
| 1759 |
% |\pld@S|--- |
| 1760 |
% \begin{macrocode} |
| 1761 |
\def\pld@SortVars@#1#2{% |
| 1762 |
\begingroup |
| 1763 |
\def\pld@monom{#2}% |
| 1764 |
\ifx\@empty\pld@monom\else |
| 1765 |
\let\pld@V\pld@SVVar |
| 1766 |
\let\pld@S\pld@SVSymbol |
| 1767 |
\pld@SortVars@V |
| 1768 |
\fi |
| 1769 |
\global\let\@gtempa\pld@monom |
| 1770 |
\endgroup |
| 1771 |
% \end{macrocode} |
| 1772 |
% and put the things together again. |
| 1773 |
% \begin{macrocode} |
| 1774 |
\def\pld@factor{#1}% |
| 1775 |
\pld@Extend\pld@factor\@gtempa |
| 1776 |
\pld@ExtendPoly\pld@tempoly\pld@factor} |
| 1777 |
% \end{macrocode} |
| 1778 |
% We use good old bubble sort on the contents of |\pld@monom|. This macro |
| 1779 |
% terminates if no items have been interchanged. |
| 1780 |
% \begin{macrocode} |
| 1781 |
\def\pld@SortVars@V{% |
| 1782 |
\pld@false \let\pld@temp\pld@monom \let\pld@monom\@empty |
| 1783 |
\pld@temp\pld@V\relax\relax |
| 1784 |
\pld@if \expandafter\pld@SortVars@V \fi} |
| 1785 |
% \end{macrocode} |
| 1786 |
% The redefinition of |\pld@V| first checks whether the end of the variables |
| 1787 |
% has been reached. If this is not the case, we either add the current |
| 1788 |
% variable to |\pld@monom| and continue with the next one, or we interchange |
| 1789 |
% the variables and indicate this by |\pld@true|. |
| 1790 |
% \begin{macrocode} |
| 1791 |
\def\pld@SVVar#1#2\pld@V#3#4{% |
| 1792 |
\ifx\relax#3\relax |
| 1793 |
\pld@AddTo\pld@monom{\pld@V{#1}{#2}}% |
| 1794 |
\else |
| 1795 |
\pld@IfVarL{#1}{#3}{\pld@AddTo\pld@monom{\pld@V{#1}{#2}}% |
| 1796 |
\def\pld@next{\pld@V{#3}{#4}}}% |
| 1797 |
{\pld@true |
| 1798 |
\pld@AddTo\pld@monom{\pld@V{#3}{#4}}% |
| 1799 |
\def\pld@next{\pld@V{#1}{#2}}}% |
| 1800 |
\expandafter\pld@next |
| 1801 |
\fi} |
| 1802 |
% \end{macrocode} |
| 1803 |
% The similar for |\pld@S| instead of |\pld@V| for sorting symbols. |
| 1804 |
% \begin{macrocode} |
| 1805 |
\def\pld@SortVars@S{% |
| 1806 |
\pld@false \let\pld@temp\pld@monom \let\pld@monom\@empty |
| 1807 |
\pld@temp\pld@S\relax\relax |
| 1808 |
\pld@if \expandafter\pld@SortVars@S \fi} |
| 1809 |
% \end{macrocode} |
| 1810 |
% \begin{macrocode} |
| 1811 |
\def\pld@SVSymbol#1#2\pld@S#3#4{% |
| 1812 |
\ifx\relax#3\relax |
| 1813 |
\pld@AddTo\pld@monom{\pld@S{#1}{#2}}% |
| 1814 |
\else |
| 1815 |
\pld@IfVarL{#1}{#3}{\pld@AddTo\pld@monom{\pld@S{#1}{#2}}% |
| 1816 |
\def\pld@next{\pld@S{#3}{#4}}}% |
| 1817 |
{\pld@true |
| 1818 |
\pld@AddTo\pld@monom{\pld@S{#3}{#4}}% |
| 1819 |
\def\pld@next{\pld@S{#1}{#2}}}% |
| 1820 |
\expandafter\pld@next |
| 1821 |
\fi} |
| 1822 |
% \end{macrocode} |
| 1823 |
% \end{macro} |
| 1824 |
% |
| 1825 |
% \begin{macro}{\pld@SortSummands} |
| 1826 |
% Now comes the same for whole monomials except that we can't redefine any |
| 1827 |
% kind of |\pld@V| and that we will use insertion sort. So, while not reaching |
| 1828 |
% the end \ldots |
| 1829 |
% \begin{macrocode} |
| 1830 |
\def\pld@SortSummands#1+{% |
| 1831 |
\ifx\relax#1\relax\else |
| 1832 |
% \end{macrocode} |
| 1833 |
% we find the right place for |\pld@monom| in |\pld@tempoly|. |
| 1834 |
% \begin{macrocode} |
| 1835 |
\ifx\@empty\pld@tempoly |
| 1836 |
\def\pld@tempoly{#1}% |
| 1837 |
\else |
| 1838 |
\def\pld@monom{#1}% |
| 1839 |
\let\pld@temp\pld@tempoly \let\pld@tempoly\@empty |
| 1840 |
\expandafter\pld@SortSummands@i\pld@temp+\relax+% |
| 1841 |
\fi |
| 1842 |
\expandafter\pld@SortSummands |
| 1843 |
\fi} |
| 1844 |
% \end{macrocode} |
| 1845 |
% For this, we iterate down the intermediate result and \ldots |
| 1846 |
% \begin{macrocode} |
| 1847 |
\def\pld@SortSummands@i#1+{% |
| 1848 |
\ifx\relax#1\relax |
| 1849 |
\pld@ExtendPoly\pld@tempoly\pld@monom |
| 1850 |
\expandafter\@gobble |
| 1851 |
\else |
| 1852 |
\expandafter\pld@SortSummands@j |
| 1853 |
\fi |
| 1854 |
{#1}} |
| 1855 |
% \end{macrocode} |
| 1856 |
% we test whether we've found the right place and insert the monomial if |
| 1857 |
% necessary. |
| 1858 |
% \begin{macrocode} |
| 1859 |
\def\pld@SortSummands@j#1{% |
| 1860 |
\expandafter\pld@IfMonomL\expandafter{\pld@monom}{#1}% |
| 1861 |
{\pld@AddToPoly\pld@tempoly{#1}% |
| 1862 |
\pld@SortSummands@i}% |
| 1863 |
{\pld@SortSummands@k\pld@monom+#1+}} |
| 1864 |
\def\pld@SortSummands@k#1+\relax+{\pld@ExtendPoly\pld@tempoly{#1}} |
| 1865 |
% \end{macrocode} |
| 1866 |
% \end{macro} |
| 1867 |
% |
| 1868 |
% |
| 1869 |
% \subsection{Phase II: Lexicographical order} |
| 1870 |
% |
| 1871 |
% \begin{macro}{\pld@IfVarL} |
| 1872 |
% \marg{varibale 1}\marg{variable 2}\marg{then}\marg{else} |
| 1873 |
% \begin{describe} |
| 1874 |
% This macro executes \meta{then} if and only if \meta{variable 1} is less |
| 1875 |
% than \meta{variable 2} (with respect to the lexicographical order defined |
| 1876 |
% by this macro). |
| 1877 |
% \end{describe} |
| 1878 |
% First we check whether the variables are equal. |
| 1879 |
% \begin{macrocode} |
| 1880 |
\def\pld@IfVarL#1#2{% |
| 1881 |
\begingroup |
| 1882 |
\def\pld@va{#1}\def\pld@vb{#2}% |
| 1883 |
\ifx\pld@va\pld@vb |
| 1884 |
\aftergroup\@secondoftwo |
| 1885 |
\else |
| 1886 |
% \end{macrocode} |
| 1887 |
% If the variables are not equal, we use their `|\meaning| expansion' for a |
| 1888 |
% string comparison. |
| 1889 |
% \begin{macrocode} |
| 1890 |
\edef\pld@next{\expandafter\strip@prefix\meaning\pld@va |
| 1891 |
\relax\noexpand\@empty |
| 1892 |
\expandafter\strip@prefix\meaning\pld@vb |
| 1893 |
\relax\noexpand\@empty}% |
| 1894 |
\expandafter\pld@IfVarL@\pld@next |
| 1895 |
\fi |
| 1896 |
\endgroup} |
| 1897 |
% \end{macrocode} |
| 1898 |
% If we've reached the end of a variable, we call the appropriate macro |
| 1899 |
% |\aftergroup|. |
| 1900 |
% \begin{macrocode} |
| 1901 |
\def\pld@IfVarL@#1#2\@empty#3#4\@empty{% |
| 1902 |
\let\pld@next\@empty |
| 1903 |
\ifx #3\relax \aftergroup\@secondoftwo |
| 1904 |
\else \ifx #1\relax \aftergroup\@firstoftwo |
| 1905 |
\else |
| 1906 |
% \end{macrocode} |
| 1907 |
% Otherwise we either need to look at the next characters or compare the two |
| 1908 |
% ones. |
| 1909 |
% \begin{macrocode} |
| 1910 |
\ifx#1#3% |
| 1911 |
\def\pld@next{\pld@IfVarL#2\@empty#4\@empty}% |
| 1912 |
\else |
| 1913 |
\ifnum`#1<`#3\relax \aftergroup\@firstoftwo |
| 1914 |
\else \aftergroup\@secondoftwo \fi |
| 1915 |
\fi |
| 1916 |
\fi \fi |
| 1917 |
\pld@next} |
| 1918 |
% \end{macrocode} |
| 1919 |
% \end{macro} |
| 1920 |
% |
| 1921 |
% \begin{macro}{\pld@IfMonomL} |
| 1922 |
% \marg{monomial 1}\marg{monomial 2}\marg{then}\marg{else} |
| 1923 |
% \begin{describe} |
| 1924 |
% This macro executes \meta{then} if and only if \meta{monomial 1} is less |
| 1925 |
% than \meta{monomial 2}. It is required that both monomials' variables are |
| 1926 |
% sorted. |
| 1927 |
% \end{describe} |
| 1928 |
% The implementation is straight forward if you know the last definitions. |
| 1929 |
% \begin{macrocode} |
| 1930 |
\def\pld@IfMonomL#1#2{% |
| 1931 |
\begingroup |
| 1932 |
\pld@IfMonomL@#1\pld@V\relax\relax\@empty |
| 1933 |
#2\pld@V\relax\relax\@empty |
| 1934 |
\endgroup} |
| 1935 |
% \end{macrocode} |
| 1936 |
% If we've reached the end of the variables, we call the appropriate macro. |
| 1937 |
% \begin{macrocode} |
| 1938 |
\def\pld@IfMonomL@#1\pld@V#2#3#4\@empty#5\pld@V#6#7#8\@empty{% |
| 1939 |
\let\pld@next\@empty |
| 1940 |
\ifx #6\relax \aftergroup\@secondoftwo |
| 1941 |
\else \ifx #2\relax \aftergroup\@firstoftwo |
| 1942 |
\else |
| 1943 |
% \end{macrocode} |
| 1944 |
% If we have two variables, there are two main cases in which the first |
| 1945 |
% monomial is smaller: the variable of the first one is smaller or the |
| 1946 |
% variables are equal but the first exponent is smaller. If both variable and |
| 1947 |
% exponent match, we have test the next variables. |
| 1948 |
% \begin{macrocode} |
| 1949 |
\def\pld@va{#2}\def\pld@vb{#6}% |
| 1950 |
\ifx\pld@va\pld@vb |
| 1951 |
\ifnum#3=#7\relax |
| 1952 |
\def\pld@next{\pld@IfMonomL@#4\@empty#8\@empty}% |
| 1953 |
\else |
| 1954 |
\ifnum#3<#7\relax \aftergroup\@firstoftwo |
| 1955 |
\else \aftergroup\@secondoftwo \fi |
| 1956 |
\fi |
| 1957 |
\else |
| 1958 |
\pld@IfVarL#2\relax\@empty#6\relax\@empty |
| 1959 |
{\aftergroup\@firstoftwo}% |
| 1960 |
{\aftergroup\@secondoftwo}% |
| 1961 |
\fi |
| 1962 |
\fi \fi |
| 1963 |
\pld@next} |
| 1964 |
% \end{macrocode} |
| 1965 |
% \end{macro} |
| 1966 |
% |
| 1967 |
% \begin{macro}{\pld@IfMonomE} |
| 1968 |
% \marg{monomial 1}\marg{monomial 2}\marg{then}\marg{else} |
| 1969 |
% \begin{describe} |
| 1970 |
% This macro executes \meta{then} if and only if \meta{monomial 1} has the |
| 1971 |
% same variables with identical exponents as \meta{monomial 2}. It is required |
| 1972 |
% that both monomials' variables are sorted. |
| 1973 |
% \end{describe} |
| 1974 |
% We just extract the `variable parts' and compare them with |\ifx|. |
| 1975 |
% \begin{macrocode} |
| 1976 |
\def\pld@IfMonomE#1#2{\pld@IfMonomE@#1\pld@V\@empty#2\pld@V\@empty} |
| 1977 |
% \end{macrocode} |
| 1978 |
% \begin{macrocode} |
| 1979 |
\def\pld@IfMonomE@#1\pld@V#2\@empty#3\pld@V#4\@empty{% |
| 1980 |
\begingroup |
| 1981 |
\def\pld@va{#2}\def\pld@vb{#4}% |
| 1982 |
\ifx\pld@va\pld@vb \aftergroup\@firstoftwo |
| 1983 |
\else \aftergroup\@secondoftwo \fi |
| 1984 |
\endgroup} |
| 1985 |
% \end{macrocode} |
| 1986 |
% \end{macro} |
| 1987 |
% |
| 1988 |
% |
| 1989 |
% \subsection{Putting together the ingredients} |
| 1990 |
% |
| 1991 |
% \begin{macro}{\pld@Simplify} |
| 1992 |
% \meta{polynomial macro} |
| 1993 |
% \begin{describe} |
| 1994 |
% just calls the definitions of the last sections in the correct order. |
| 1995 |
% \end{describe} |
| 1996 |
% \begin{macrocode} |
| 1997 |
\def\pld@Simplify#1{% |
| 1998 |
\pld@CondenseFactors#1% |
| 1999 |
\pld@SortMonomials#1% |
| 2000 |
\pld@CondenseMonomials\pld@true#1} |
| 2001 |
% \end{macrocode} |
| 2002 |
% \end{macro} |
| 2003 |
% |
| 2004 |
% |
| 2005 |
% \section{Multiplication} |
| 2006 |
% |
| 2007 |
% \begin{macro}{\pld@MultiplyPoly} |
| 2008 |
% \begin{macro}{\pld@NMultiplyPoly} |
| 2009 |
% \meta{macro a}\meta{macro b}\meta{macro c} |
| 2010 |
% \begin{describe} |
| 2011 |
% \meta{macro a} gets \meta{macro b}${}\cdot{}$\meta{macro c} respectively |
| 2012 |
% the negative polynomial in the second case \texttt{N}. |
| 2013 |
% \end{describe} |
| 2014 |
% We use a switch to distinguish the positive and negative form. |
| 2015 |
% \begin{macrocode} |
| 2016 |
\def\pld@MultiplyPoly{\begingroup\pld@true \pld@MultiplyPoly@} |
| 2017 |
\def\pld@NMultiplyPoly{\begingroup\pld@false \pld@MultiplyPoly@} |
| 2018 |
% \end{macrocode} |
| 2019 |
% Multiply the polynomials and condense the result. Note that the latter is |
| 2020 |
% the main working procedure. To avoid problems with |\polyhornerscheme| (it |
| 2021 |
% uses empty macros for the representation of the number 0), we check for |
| 2022 |
% empty macros here---thanks go to Ludger Humbert. |
| 2023 |
% \begin{macrocode} |
| 2024 |
\def\pld@MultiplyPoly@#1#2#3{% |
| 2025 |
\let\pld@temp\@empty |
| 2026 |
\ifx\@empty#2\@empty\else \ifx\@empty#3\@empty\else |
| 2027 |
\expandafter\pld@MultiplyPoly@a\expandafter#2#3+\relax+% |
| 2028 |
\fi \fi |
| 2029 |
\global\let\@gtempa\pld@temp |
| 2030 |
\endgroup |
| 2031 |
\let#1\@gtempa \pld@CondenseFactors#1} |
| 2032 |
% \end{macrocode} |
| 2033 |
% Here we combine each (negated) summand |#2| of the second polynomial with |
| 2034 |
% \ldots |
| 2035 |
% \begin{macrocode} |
| 2036 |
\def\pld@MultiplyPoly@a#1#2+{% |
| 2037 |
\ifx\relax#2\else |
| 2038 |
\pld@if \def\pld@va{#2}\else \def\pld@va{#2\pld@R{-1}1}\fi |
| 2039 |
\expandafter\pld@MultiplyPoly@b#1+\relax+% |
| 2040 |
\expandafter\pld@MultiplyPoly@a\expandafter#1% |
| 2041 |
\fi} |
| 2042 |
% \end{macrocode} |
| 2043 |
% each summand |#1| of the first one. |
| 2044 |
% \begin{macrocode} |
| 2045 |
\def\pld@MultiplyPoly@b#1+{% |
| 2046 |
\ifx\relax#1\else |
| 2047 |
\pld@ExtendPoly\pld@temp{\pld@va#1}% |
| 2048 |
\expandafter\pld@MultiplyPoly@b |
| 2049 |
\fi} |
| 2050 |
% \end{macrocode} |
| 2051 |
% \end{macro} |
| 2052 |
% \end{macro} |
| 2053 |
% |
| 2054 |
% |
| 2055 |
% \section{Division} |
| 2056 |
% |
| 2057 |
% |
| 2058 |
% \subsection{The algorithm} |
| 2059 |
% |
| 2060 |
% \begin{macro}{\pld@DividePoly} |
| 2061 |
% \begin{macro}{\pld@LongDividePoly} |
| 2062 |
% A polynomial long division is indicated by |\pld@true|. In this case we also |
| 2063 |
% need to initialize some macros. |
| 2064 |
% \begin{macrocode} |
| 2065 |
\def\pld@DividePoly{\pld@false \pld@DivPoly} |
| 2066 |
% \end{macrocode} |
| 2067 |
% \begin{macrocode} |
| 2068 |
\def\pld@LongDividePoly#1#2{% |
| 2069 |
\let\pld@pattern\@empty \let\pld@lastline\@empty |
| 2070 |
\let\pld@subline\@empty \let\pld@currentline\@empty |
| 2071 |
\let\pld@allines\@empty \let\pld@maxcol\z@ |
| 2072 |
\let\pld@maxcolplustwo\z@ \let\pld@linepos\z@ |
| 2073 |
\pld@true \pld@DivPoly#1#2% |
| 2074 |
\pld@ArrangeResult#1} |
| 2075 |
% \end{macrocode} |
| 2076 |
% \end{macro} |
| 2077 |
% \end{macro} |
| 2078 |
% |
| 2079 |
% The division algorithm has three main components: the division of two |
| 2080 |
% monomials, the subtraction of two polynomials, and a loop putting together |
| 2081 |
% both things. |
| 2082 |
% |
| 2083 |
% \begin{macro}{\pld@DivPoly} |
| 2084 |
% The loop: We initialize remainder, divisor, and quotient. |
| 2085 |
% \begin{macrocode} |
| 2086 |
\def\pld@DivPoly#1#2{% |
| 2087 |
\pld@currstage\pld@stage\relax |
| 2088 |
\let\pld@remainder#1\let\pld@divisor#2\let\pld@quotient\@empty |
| 2089 |
\pld@DivPoly@l} |
| 2090 |
% \end{macrocode} |
| 2091 |
% While the remainder isn't zero and needs to be divided, we extend the |
| 2092 |
% quotient and subtract the appropriate polynomial from the remainder. |
| 2093 |
% \begin{macrocode} |
| 2094 |
\def\pld@DivPoly@l{% |
| 2095 |
\ifx\pld@remainder\@empty\else |
| 2096 |
\pld@IfNeedsDivision\pld@remainder\pld@divisor |
| 2097 |
{\pld@ExtendPoly\pld@quotient\pld@factor |
| 2098 |
\pld@NMultiplyPoly\pld@sub\pld@divisor\pld@factor |
| 2099 |
\pld@SubtractPoly\pld@remainder\pld@sub |
| 2100 |
\expandafter\pld@DivPoly@l}% |
| 2101 |
{\ifpld@InsidePolylongdiv\expandafter\pld@insert@remainder |
| 2102 |
\pld@last@remainder+\relax\relax\fi}% |
| 2103 |
\fi} |
| 2104 |
% \end{macrocode} |
| 2105 |
% The fix by Hendrik Vogt. |
| 2106 |
% \begin{macrocode} |
| 2107 |
\def\pld@insert@remainder#1+#2\relax{% |
| 2108 |
\ifx\relax#1\relax\else\pld@InsertItems\@empty\@empty{#1}\fi |
| 2109 |
\ifx\relax#2\relax\else\pld@insert@remainder#2\relax\fi} |
| 2110 |
% \end{macrocode} |
| 2111 |
% \end{macro} |
| 2112 |
% |
| 2113 |
% \begin{macro}{\pld@IfNeedsDivision} |
| 2114 |
% \meta{polynomial 1}\meta{polynomial 2}\marg{then}\marg{else} |
| 2115 |
% \begin{describe} |
| 2116 |
% executes \meta{then} if and only if \meta{polynomial 1} must be divided by |
| 2117 |
% \meta{polynomial 2} (with possibly nonzero remainder!). |\pld@factor| can be |
| 2118 |
% used in \meta{then} and holds the quotient of the first monomials. Note that |
| 2119 |
% this macro requires the polynomials to be sorted. |
| 2120 |
% \end{describe} |
| 2121 |
% We expand the two polynomials and add terminators |+\@empty|. |
| 2122 |
% \begin{macrocode} |
| 2123 |
\def\pld@IfNeedsDivision#1#2{% |
| 2124 |
\pld@ExpandTwo\pld@IfND{#1+\@empty}{#2+\@empty}} |
| 2125 |
% \end{macrocode} |
| 2126 |
% Now we can divide the first summands of the two polynomials and \ldots |
| 2127 |
% \begin{macrocode} |
| 2128 |
\def\pld@IfND#1+#2\@empty#3+#4\@empty{% |
| 2129 |
\pld@DefInverse\pld@factor{#3}% |
| 2130 |
\pld@AddTo\pld@factor{#1}% |
| 2131 |
\pld@CondenseFactors\pld@factor |
| 2132 |
% \end{macrocode} |
| 2133 |
% check whether all variables have a non-negative exponent. |
| 2134 |
% Depending on that, we choose the correct argument. |
| 2135 |
% \begin{macrocode} |
| 2136 |
\begingroup |
| 2137 |
\pld@true |
| 2138 |
\expandafter\pld@IfND@\pld@factor\pld@V\relax\z@ |
| 2139 |
\pld@if \aftergroup\@firstoftwo |
| 2140 |
\else \aftergroup\@secondoftwo \fi |
| 2141 |
\endgroup} |
| 2142 |
% \end{macrocode} |
| 2143 |
% And here we check for (non-)negative exponents. |
| 2144 |
% \begin{macrocode} |
| 2145 |
\def\pld@IfND@#1\pld@V#2#3{% |
| 2146 |
\ifx\relax#2\@empty \expandafter\@gobble |
| 2147 |
\else \ifnum#3<\z@ \pld@false \fi |
| 2148 |
\fi \pld@IfND@} |
| 2149 |
% \end{macrocode} |
| 2150 |
% \end{macro} |
| 2151 |
% |
| 2152 |
% \begin{macro}{\pld@SubtractPoly} |
| 2153 |
% Here we perform the subtraction. We could define one very short macro for |
| 2154 |
% `short' and another for polynomial long division, but this macro covers both |
| 2155 |
% cases. We do nothing if |#2| is empty. This test shouldn't be necessary, but |
| 2156 |
% who knows how I'll change |\pld@DivPoly| in future. |
| 2157 |
% \begin{macrocode} |
| 2158 |
\def\pld@SubtractPoly#1#2{% |
| 2159 |
\ifx#2\@empty\else |
| 2160 |
% \end{macrocode} |
| 2161 |
% For long division, we initialize the horizontal rule's first and last column. |
| 2162 |
% \begin{macrocode} |
| 2163 |
\pld@if |
| 2164 |
\let\pld@firstcol\maxdimen \let\pld@lastcol\z@ |
| 2165 |
\fi |
| 2166 |
% \end{macrocode} |
| 2167 |
% The submacro does the subtraction and defines appropriate data |
| 2168 |
% |\pld@lastline|, |\pld@subline|, \ldots\space. |
| 2169 |
% \begin{macrocode} |
| 2170 |
\let\pld@tempoly\@empty |
| 2171 |
\pld@ExpandTwo\pld@SubtractPoly@ |
| 2172 |
{#1+\relax+\@empty}{#2+\relax+\@empty}% |
| 2173 |
\let#1\pld@tempoly |
| 2174 |
% \end{macrocode} |
| 2175 |
% For long divisions, we now add the calculated lines and a horizontal rule |
| 2176 |
% to |\pld@allines| (if the current stage allows it). Eventually we reset data. |
| 2177 |
% \begin{macrocode} |
| 2178 |
\pld@if |
| 2179 |
\ifnum\pld@currstage>\z@ |
| 2180 |
\pld@Extend\pld@allines{\pld@lastline\cr}% |
| 2181 |
\else |
| 2182 |
\pld@InsertFake\pld@lastline |
| 2183 |
\fi |
| 2184 |
\advance\pld@currstage-\tw@ |
| 2185 |
\ifnum\pld@currstage>\z@ |
| 2186 |
\pld@Extend\pld@allines{\pld@subline\cr}% |
| 2187 |
\edef\pld@subline{% |
| 2188 |
\noexpand\cline{\pld@firstcol-\pld@lastcol} |
| 2189 |
\noalign{\vskip\jot}}% |
| 2190 |
\pld@Extend\pld@allines\pld@subline |
| 2191 |
\else |
| 2192 |
\pld@InsertFake\pld@subline |
| 2193 |
\fi |
| 2194 |
\advance\pld@currstage\m@ne |
| 2195 |
\let\pld@lastline\pld@currentline |
| 2196 |
\let\pld@subline\@empty |
| 2197 |
\let\pld@currentline\@empty |
| 2198 |
\fi |
| 2199 |
\fi} |
| 2200 |
% \end{macrocode} |
| 2201 |
% The following submacro reads both first monomials. The difference must be |
| 2202 |
% zero, so we just gobble the monomials except for long division. This is |
| 2203 |
% coded explicitly to always reduce the degree---no matter what the |
| 2204 |
% calculations in behind say is true. For long division, we take the |
| 2205 |
% original monomial, negate it, and use these two for the visualization. |
| 2206 |
% \begin{macrocode} |
| 2207 |
\def\pld@SubtractPoly@#1+#2\@empty#3+{% |
| 2208 |
\pld@if |
| 2209 |
\pld@DefNegative\pld@monom{#1}% |
| 2210 |
\expandafter\pld@InsertItems\expandafter\@empty |
| 2211 |
\expandafter{\pld@monom}{#1}% |
| 2212 |
\fi |
| 2213 |
\pld@SubtractPoly@l#2\@empty} |
| 2214 |
% \end{macrocode} |
| 2215 |
% All other monomials are read here. We have to distinguish several cases. |
| 2216 |
% If we've reached the end of both polynomials, the next operation is empty. |
| 2217 |
% \begin{macrocode} |
| 2218 |
\def\pld@SubtractPoly@l#1+#2\@empty#3+#4\@empty{% |
| 2219 |
\ifx\relax#1\relax |
| 2220 |
\let\pld@last@remainder\@empty |
| 2221 |
\ifx\relax#3\relax \let\pld@next\@empty \else |
| 2222 |
% \end{macrocode} |
| 2223 |
% If we've reached the end of the first polynomial, we add the monomial of the |
| 2224 |
% second polynomial, subtract it from the result, use it for visualization, and |
| 2225 |
% call this macro again to read the rest of the polynomial. |
| 2226 |
% \begin{macrocode} |
| 2227 |
\pld@AddToPoly\pld@tempoly{#3}% |
| 2228 |
\pld@if \pld@InsertItems{#3}{#3}{}\fi |
| 2229 |
\def\pld@next{\pld@SubtractPoly@l\relax+\@empty#4\@empty}% |
| 2230 |
\fi |
| 2231 |
\else |
| 2232 |
\ifx\relax#3\relax |
| 2233 |
% \end{macrocode} |
| 2234 |
% If we've reached the end of the second polynomial, we just add the rest of |
| 2235 |
% the first polynomial to the result |\pld@tempoly|. |
| 2236 |
% \begin{macrocode} |
| 2237 |
\pld@SubtractPoly@r#1+#2\@empty |
| 2238 |
\let\pld@next\@empty |
| 2239 |
\else |
| 2240 |
% \end{macrocode} |
| 2241 |
% There are three cases if we have two monomials. If they are equal---which |
| 2242 |
% means that the variables and their exponents match---, we add the monomials |
| 2243 |
% and use the result to extend the temporary polynomial as well as for the |
| 2244 |
% visualization. |
| 2245 |
% \begin{macrocode} |
| 2246 |
\pld@IfMonomE{#1}{#3}% |
| 2247 |
{\def\pld@temp{#1+#3}% |
| 2248 |
\pld@CondenseMonomials\pld@true\pld@temp |
| 2249 |
\ifx\pld@temp\@empty\else |
| 2250 |
\pld@ExtendPoly\pld@tempoly\pld@temp |
| 2251 |
\fi |
| 2252 |
\pld@if \expandafter\pld@InsertItems\expandafter |
| 2253 |
{\pld@temp}{#3}{#1}\fi |
| 2254 |
\def\pld@next{\pld@SubtractPoly@l#2\@empty#4\@empty}}% |
| 2255 |
% \end{macrocode} |
| 2256 |
% If the second monomial is stricly smaller, we extend the temporary polynomial |
| 2257 |
% and the visualization by this monomial and re-insert the first monomial to be |
| 2258 |
% read again. |
| 2259 |
% \begin{macrocode} |
| 2260 |
{\pld@IfMonomL{#1}{#3}% |
| 2261 |
{\pld@AddToPoly\pld@tempoly{#3}% |
| 2262 |
\pld@if \pld@InsertItems{#3}{#3}{}\fi |
| 2263 |
\def\pld@next{\pld@SubtractPoly@l#1+#2\@empty#4\@empty}}% |
| 2264 |
% \end{macrocode} |
| 2265 |
% If the first monomial is stricly smaller, we extend the temporary polynomial |
| 2266 |
% and the visualization by this monomial and re-insert the other to be read |
| 2267 |
% again (since we haven't reached the correct place in the table yet). |
| 2268 |
% Note that these two last cases are some kind of insertion sort. |
| 2269 |
% \begin{macrocode} |
| 2270 |
{\pld@AddToPoly\pld@tempoly{#1}% |
| 2271 |
\pld@if \pld@InsertItems{#1}{}{#1}\fi |
| 2272 |
\def\pld@next{\pld@SubtractPoly@l#2\@empty#3+#4\@empty}}% |
| 2273 |
}% |
| 2274 |
\fi \fi |
| 2275 |
\pld@next} |
| 2276 |
% \end{macrocode} |
| 2277 |
% Finally the macro used to add the rest of the first polynomial. |
| 2278 |
% \begin{macrocode} |
| 2279 |
\def\pld@SubtractPoly@r#1+\relax+\@empty{% |
| 2280 |
\pld@AddToPoly\pld@tempoly{#1}% |
| 2281 |
\def\pld@last@remainder{#1}} |
| 2282 |
% \end{macrocode} |
| 2283 |
% \end{macro} |
| 2284 |
% |
| 2285 |
% |
| 2286 |
% \subsection{Tweaking the alignment} |
| 2287 |
% |
| 2288 |
% Here comes important code for partial output of long divisions. |
| 2289 |
% |
| 2290 |
% \begin{macro}{\pld@InsertFake} |
| 2291 |
% This macro is somewhat like |\pld@InsertItems| but gets a whole line. Thus |
| 2292 |
% we iterate down each entry and compare its width with the next dimension |
| 2293 |
% from |\pld@fakeline| (which is the current width of the column). In detail: |
| 2294 |
% We iterate down each entry and \ldots |
| 2295 |
% \begin{macrocode} |
| 2296 |
\def\pld@InsertFake#1{% |
| 2297 |
\let\pld@temp\@empty |
| 2298 |
\expandafter\pld@InsertFake@l#1&\relax&} |
| 2299 |
% \end{macrocode} |
| 2300 |
% \ldots\space either append the rest of |\pld@fakeline| or get the next |
| 2301 |
% dimension from the macro. |
| 2302 |
% \begin{macrocode} |
| 2303 |
\def\pld@InsertFake@l#1&{% |
| 2304 |
\ifx\relax#1\@empty |
| 2305 |
\pld@Extend\pld@temp{\expandafter&\pld@fakeline}% |
| 2306 |
\let\pld@fakeline\pld@temp |
| 2307 |
\else |
| 2308 |
\expandafter\pld@InsertFake@do\pld@fakeline\relax{#1}% |
| 2309 |
\expandafter\pld@InsertFake@l |
| 2310 |
\fi} |
| 2311 |
\def\pld@InsertFake@do#1\relax#3{% |
| 2312 |
% \end{macrocode} |
| 2313 |
% We assign the remaining column dimensions or, if there is no dimension left, |
| 2314 |
% we insert 0pt. |
| 2315 |
% \begin{macrocode} |
| 2316 |
\ifx\@empty#2\@empty \def\pld@fakeline{0pt&}% |
| 2317 |
\else \def\pld@fakeline{#2}\fi |
| 2318 |
\@tempdima#1\relax |
| 2319 |
\setbox\z@=\hbox{\ensuremath{#3}}% |
| 2320 |
% \end{macrocode} |
| 2321 |
% Then we add the maximum of the current dimension and the width of |#3| to |
| 2322 |
% |\pld@temp| (which will be assigned to |\pld@fakeline| as seen above). |
| 2323 |
% \begin{macrocode} |
| 2324 |
\ifdim\@tempdima<\wd\z@ \@tempdima=\wd\z@ \fi |
| 2325 |
\ifx\pld@temp\@empty |
| 2326 |
\edef\pld@temp{\the\@tempdima}% |
| 2327 |
\else |
| 2328 |
\pld@Extend\pld@temp{\expandafter&\the\@tempdima}% |
| 2329 |
\fi} |
| 2330 |
% \end{macrocode} |
| 2331 |
% \begin{macrocode} |
| 2332 |
\def\pld@fakeline{0pt&}% init |
| 2333 |
% \end{macrocode} |
| 2334 |
% \end{macro} |
| 2335 |
% |
| 2336 |
% \begin{macro}{\pld@ConvertFake} |
| 2337 |
% The contents of |\pld@fakeline| are converted into an appropriate sequence |
| 2338 |
% of |\vrule|\texttt{ height 0pt depth 0pt width }\meta{column dimension}. |
| 2339 |
% This put inside the |\halign| below will ensure stable column widths. |
| 2340 |
% \begin{macrocode} |
| 2341 |
\def\pld@ConvertFake#1&{% |
| 2342 |
\ifx\relax#1\@empty\else |
| 2343 |
\ifx\@empty#1\@empty |
| 2344 |
&% |
| 2345 |
\else |
| 2346 |
\noexpand\vrule\noexpand\@height\z@\noexpand\@depth\z@ |
| 2347 |
\noexpand\@width#1\relax&% |
| 2348 |
\fi |
| 2349 |
\expandafter\pld@ConvertFake |
| 2350 |
\fi} |
| 2351 |
% \end{macrocode} |
| 2352 |
% \end{macro} |
| 2353 |
% |
| 2354 |
% \begin{macro}{\pld@SplitQuotient} |
| 2355 |
% splits |\pld@quotient| into the visible |\pld@real| and invisible |
| 2356 |
% |\pld@shadow|---according to the given |\pld@stage|. We just iterate down |
| 2357 |
% the summands: |
| 2358 |
% \begin{macrocode} |
| 2359 |
\def\pld@SplitQuotient{% |
| 2360 |
\let\pld@real\@empty \let\pld@shadow\empty |
| 2361 |
\pld@currstage\pld@stage\relax |
| 2362 |
\expandafter\pld@SplitQuotient@\pld@quotient+\relax+} |
| 2363 |
\def\pld@SplitQuotient@#1+{% |
| 2364 |
\ifx\relax#1\@empty |
| 2365 |
% \end{macrocode} |
| 2366 |
% reaching the end, we check whether the remainder needs to be printed; |
| 2367 |
% \begin{macrocode} |
| 2368 |
\advance\pld@currstage-\tw@ |
| 2369 |
\ifnum\pld@currstage<\z@ |
| 2370 |
\let\pld@PrintRemain\pld@XPLD |
| 2371 |
\else |
| 2372 |
\let\pld@PrintRemain\pld@PLD |
| 2373 |
\fi |
| 2374 |
\else |
| 2375 |
% \end{macrocode} |
| 2376 |
% otherwise we either add the current summand to |\pld@real| or |\pld@shadow|. |
| 2377 |
% \begin{macrocode} |
| 2378 |
\ifx\@empty#1\@empty\else |
| 2379 |
\advance\pld@currstage-\tw@ |
| 2380 |
\ifnum\pld@currstage<\z@ |
| 2381 |
\pld@AddToPoly\pld@shadow{#1}% |
| 2382 |
\else |
| 2383 |
\pld@AddToPoly\pld@real{#1}% |
| 2384 |
\fi |
| 2385 |
\advance\pld@currstage\m@ne |
| 2386 |
\fi |
| 2387 |
\expandafter\pld@SplitQuotient@ |
| 2388 |
\fi} |
| 2389 |
% \end{macrocode} |
| 2390 |
% \end{macro} |
| 2391 |
% |
| 2392 |
% \begin{macro}{\pld@PrintPolyShadow} |
| 2393 |
% prints |\pld@real| and leaves space for |\pld@shadow|. |
| 2394 |
% \begin{macrocode} |
| 2395 |
\def\pld@PrintPolyShadow{% |
| 2396 |
\pld@firsttrue |
| 2397 |
\ifx\pld@real\@empty\else |
| 2398 |
\expandafter\pld@PrintMonoms\pld@real+\relax+% |
| 2399 |
\fi |
| 2400 |
\ifx\pld@shadow\@empty\else |
| 2401 |
\setbox\z@\hbox{$\expandafter\pld@PrintMonoms\pld@shadow |
| 2402 |
+\relax+$}% |
| 2403 |
\phantom{\copy\z@}% |
| 2404 |
\fi} |
| 2405 |
% \end{macrocode} |
| 2406 |
% \end{macro} |
| 2407 |
% |
| 2408 |
% \subsection{Aligning long division}\label{iAligningLongDivision} |
| 2409 |
% |
| 2410 |
% \begin{macro}{\pld@PrintLongDiv} |
| 2411 |
% does the horizontal alignment. It puts |\pld@allines| into |\halign|. |
| 2412 |
% \begin{macrocode} |
| 2413 |
\def\pld@PrintLongDiv{% |
| 2414 |
\ensuremath{\hbox{\vtop{\begingroup |
| 2415 |
\offinterlineskip \tabskip=\z@ |
| 2416 |
\edef\pld@fakeline{\expandafter\pld@ConvertFake\pld@fakeline&\relax&}% |
| 2417 |
\halign{\strut\pld@firsttrue\hfil$##$% |
| 2418 |
&\pld@firsttrue\hfil$##$% |
| 2419 |
&&\hfil$##$\cr |
| 2420 |
\pld@fakeline\cr \noalign{\vskip-\normalbaselineskip}% |
| 2421 |
\pld@allines}% |
| 2422 |
\endgroup}}}} |
| 2423 |
% \end{macrocode} |
| 2424 |
% \end{macro} |
| 2425 |
% |
| 2426 |
% \begin{macro}{\pld@InsertItems} |
| 2427 |
% Here now we place the monomials. |\pld@pattern| gives the columns in which |
| 2428 |
% monomials have been put and thus has to be put now. So we first define the |
| 2429 |
% monomial and look for it in |\pld@pattern|. |
| 2430 |
% \begin{macrocode} |
| 2431 |
\def\pld@InsertItems#1#2#3{% |
| 2432 |
\ifx\@empty#1\@empty |
| 2433 |
\ifx\@empty#2\@empty \def\pld@monom{#3}% |
| 2434 |
\else \def\pld@monom{#2}\fi |
| 2435 |
\else \def\pld@monom{#1}\fi |
| 2436 |
\@tempcnta\@ne \let\pld@recentmonom\@empty |
| 2437 |
\expandafter\pld@InsertItems@find\pld@pattern\relax&% |
| 2438 |
% \end{macrocode} |
| 2439 |
% This column |\@tempcnta| must not exceed the current column range, which is |
| 2440 |
% used to draw the horizontal line: we change the range if necessary. |
| 2441 |
% \begin{macrocode} |
| 2442 |
\ifnum\pld@firstcol>\@tempcnta \edef\pld@firstcol{\the\@tempcnta}\fi |
| 2443 |
\ifnum\pld@lastcol<\@tempcnta \edef\pld@lastcol{\the\@tempcnta}\fi |
| 2444 |
\ifnum\pld@maxcol<\@tempcnta |
| 2445 |
\edef\pld@maxcol{\the\@tempcnta} |
| 2446 |
\@tempcntb\pld@maxcol\relax\advance\@tempcntb\tw@ |
| 2447 |
\edef\pld@maxcolplustwo{\the\@tempcntb} |
| 2448 |
\fi |
| 2449 |
% \end{macrocode} |
| 2450 |
% Finally we insert the arguments. |
| 2451 |
% \begin{macrocode} |
| 2452 |
\pld@InsertItems@do\pld@lastline{\pld@PLD{#3}}% |
| 2453 |
\pld@InsertItems@do\pld@subline{\pld@PLD{#2}}% |
| 2454 |
\pld@InsertItems@do\pld@currentline{\pld@PLD{#1}}} |
| 2455 |
% \end{macrocode} |
| 2456 |
% \end{macro} |
| 2457 |
% |
| 2458 |
% \begin{macro}{\pld@InsertItems@do} |
| 2459 |
% For this, we iterate down the specified line |#1|---five items at the same |
| 2460 |
% time---\ldots |
| 2461 |
% \begin{macrocode} |
| 2462 |
\def\pld@InsertItems@do#1#2{% |
| 2463 |
\let\pld@temp\@empty \@tempcntb\@tempcnta |
| 2464 |
\expandafter\pld@InsertItems@do@a#1&&&&&\relax{#2}% |
| 2465 |
\let#1\pld@temp} |
| 2466 |
% \end{macrocode} |
| 2467 |
% until we've found the item number |\@tempcnta|$=$|\@tempcntb|$-k\cdot 5$. |
| 2468 |
% \begin{macrocode} |
| 2469 |
\def\pld@InsertItems@do@a#1\relax{% |
| 2470 |
\ifcase\@tempcntb \or |
| 2471 |
\or \pld@AddTo\pld@temp{#1&}% |
| 2472 |
\or \pld@AddTo\pld@temp{#1&}% |
| 2473 |
\or \pld@AddTo\pld@temp{#1&}% |
| 2474 |
\or \pld@AddTo\pld@temp{#1&}% |
| 2475 |
\else |
| 2476 |
% \end{macrocode} |
| 2477 |
% If this is not the case, we call this macro again. |
| 2478 |
% \begin{macrocode} |
| 2479 |
\pld@AddTo\pld@temp{#1&}% |
| 2480 |
\advance\@tempcntb-5\relax |
| 2481 |
\def\pld@next{\pld@InsertItems@do@a#6&&&&&\relax}% |
| 2482 |
\expandafter\@firstoftwo\expandafter\pld@next |
| 2483 |
\fi |
| 2484 |
% \end{macrocode} |
| 2485 |
% Otherwise we add the monomial to |\pld@temp|, which is assigned to the |
| 2486 |
% correct macro above. |
| 2487 |
% \begin{macrocode} |
| 2488 |
\pld@InsertItems@do@b} |
| 2489 |
\def\pld@InsertItems@do@b#1{\pld@AddTo\pld@temp{#1}} |
| 2490 |
% \end{macrocode} |
| 2491 |
% \end{macro} |
| 2492 |
% |
| 2493 |
% \begin{macro}{\pld@InsertItems@find} |
| 2494 |
% To find the monomial in |\pld@pattern|, we just test each monomial against |
| 2495 |
% the defined |\pld@monom|. If we've reached the end of the pattern, we `append' |
| 2496 |
% (see below) the monomial to the pattern and we're done for. |
| 2497 |
% \begin{macrocode} |
| 2498 |
\def\pld@InsertItems@find#1&{% |
| 2499 |
\ifx\relax#1\relax |
| 2500 |
\expandafter\pld@InsertItems@find@fill\pld@recentmonom\pld@V{}0\@empty |
| 2501 |
\else |
| 2502 |
% \end{macrocode} |
| 2503 |
% Otherwise we either drop the rest of the pattern since we've found the |
| 2504 |
% monomial, or we advance the temporary counter and continue. |
| 2505 |
% \begin{macrocode} |
| 2506 |
\def\pld@recentmonom{#1}% |
| 2507 |
\expandafter\pld@IfMonomE\expandafter{\pld@monom}{#1}% |
| 2508 |
{\expandafter\pld@InsertItems@find@\expandafter&}% |
| 2509 |
{\advance\@tempcnta\@ne \expandafter\pld@InsertItems@find}% |
| 2510 |
\fi} |
| 2511 |
\def\pld@InsertItems@find@#1&\relax&{} |
| 2512 |
% \end{macrocode} |
| 2513 |
% And now for `appending' a monomial to the pattern. Thanks to Karl Heinz |
| 2514 |
% Marbaise wrong implementation has been replaced by filling in also the |
| 2515 |
% monomials between the most recent and current monomial---if that wouldn't be |
| 2516 |
% done, a higher degree monomial could be preceded by a lower degree one and |
| 2517 |
% thus would never get printed as $x^7$ in |\polylongdiv{x^{15}+1}{x^5+x^3+x+1}|. |
| 2518 |
% \begin{macrocode} |
| 2519 |
\def\pld@InsertItems@find@fill#1\pld@V#2#3#4\@empty{% |
| 2520 |
\expandafter\pld@InsertItems@find@fill@\pld@monom\pld@V{}0\@empty{#3}} |
| 2521 |
\def\pld@InsertItems@find@fill@#1\pld@V#2#3#4\@empty#5{% |
| 2522 |
\ifx\pld@pattern\@empty |
| 2523 |
\def\pld@pattern{\pld@V&\pld@V{#2}{#3}&}% |
| 2524 |
\@tempcnta\tw@ |
| 2525 |
\else |
| 2526 |
\@tempcntb#5\relax |
| 2527 |
\loop \ifnum #3<\@tempcntb |
| 2528 |
\advance\@tempcnta\@ne |
| 2529 |
\advance\@tempcntb\m@ne |
| 2530 |
\ifnum\@tempcntb=\z@ |
| 2531 |
\def\pld@temp{#1}% |
| 2532 |
\else |
| 2533 |
\edef\pld@temp{\noexpand\pld@V{#2}{\the\@tempcntb}}% |
| 2534 |
\fi |
| 2535 |
\pld@Extend\pld@pattern{\pld@temp&}% |
| 2536 |
\repeat |
| 2537 |
\advance\@tempcnta\m@ne |
| 2538 |
\fi} |
| 2539 |
% \end{macrocode} |
| 2540 |
% \end{macro} |
| 2541 |
% |
| 2542 |
% \begin{macro}{\pld@ArrangeResult} |
| 2543 |
% Here the dividend, divisor, and quotient are added to the `|\halign|' data |
| 2544 |
% macro. First we add a $0$ below the last horizontal rule if the remainder is |
| 2545 |
% zero. |
| 2546 |
% \begin{macrocode} |
| 2547 |
\def\pld@ArrangeResult#1{% |
| 2548 |
\ifx\pld@remainder\@empty |
| 2549 |
\@tempcnta\pld@maxcol\relax |
| 2550 |
\pld@InsertItems@do\pld@lastline |
| 2551 |
{\pld@firsttrue\pld@PLD{\pld@R{0}{1}}}% |
| 2552 |
\fi |
| 2553 |
\ifnum\pld@currstage>\z@ |
| 2554 |
\pld@Extend\pld@allines{\pld@lastline\cr}% |
| 2555 |
\else |
| 2556 |
\pld@InsertFake\pld@lastline |
| 2557 |
\fi |
| 2558 |
% \end{macrocode} |
| 2559 |
% We begin to build the first line. For the quotient printed atop, the divisor |
| 2560 |
% is the first element. Otherwise we either use a left parentheses or just let |
| 2561 |
% the first element empty. Note that the size of the parentheses is hard-wired. |
| 2562 |
% \begin{macrocode} |
| 2563 |
\pld@iftopresult |
| 2564 |
\def\pld@lastline{\pld@PrintPoly\pld@divisor\bigr)&}% |
| 2565 |
\else |
| 2566 |
\let\pld@lastline\@empty |
| 2567 |
\ifx C\pld@style |
| 2568 |
\def\pld@lastline{\pld@leftdelim\strut\pld@rightxdelim&}% |
| 2569 |
\fi |
| 2570 |
\fi |
| 2571 |
% \end{macrocode} |
| 2572 |
% Now we put the monomials of the dividend in the correct columns and split the |
| 2573 |
% quotient into its visible and invisible part. |
| 2574 |
% \begin{macrocode} |
| 2575 |
\expandafter\pld@AR@col\expandafter\pld@PLD |
| 2576 |
\expandafter\pld@lastline#1+\relax+% |
| 2577 |
\pld@SplitQuotient |
| 2578 |
% \end{macrocode} |
| 2579 |
% For a result at top, we put the quotient into |\pld@currentline| and add a |
| 2580 |
% horizontal line below it. |
| 2581 |
% \begin{macrocode} |
| 2582 |
\pld@iftopresult |
| 2583 |
\let\pld@currentline\@empty |
| 2584 |
\expandafter\pld@AR@col\expandafter\pld@PLD |
| 2585 |
\expandafter\pld@currentline |
| 2586 |
\pld@quotient+\relax+% |
| 2587 |
\expandafter\pld@AR@col\expandafter\pld@XPLD |
| 2588 |
\expandafter\pld@currentline |
| 2589 |
\pld@shadow+\relax+% |
| 2590 |
\edef\pld@subline{% |
| 2591 |
\noexpand\cline{\tw@-\pld@maxcol}% |
| 2592 |
\noalign{\vskip\jot}}% |
| 2593 |
\pld@Extend\pld@currentline{\expandafter\cr\pld@subline}% |
| 2594 |
\else |
| 2595 |
% \end{macrocode} |
| 2596 |
% For a result next to the dividend, we first calculate the number of columns |
| 2597 |
% it spans. It's the maximal column minus the last column of the dividend |
| 2598 |
% (which is |\@tempcnta|) plus one extra column not to squeeze it all into |
| 2599 |
% the last column of the `normal' table. |
| 2600 |
% \begin{macrocode} |
| 2601 |
\@tempcnta-\@tempcnta |
| 2602 |
\advance\@tempcnta\pld@maxcol\relax \advance\@tempcnta\@ne |
| 2603 |
\edef\pld@span{\the\@tempcnta}% |
| 2604 |
\@tempcntb\pld@maxcol\relax\advance\@tempcntb\pld@span% |
| 2605 |
\advance\@tempcntb\@ne% |
| 2606 |
\edef\pld@linepos{\the\@tempcntb}% |
| 2607 |
% \end{macrocode} |
| 2608 |
% Then we can add divisor, quotient, and remainder. First we go for style B. |
| 2609 |
% \begin{macrocode} |
| 2610 |
\ifx B\pld@style |
| 2611 |
\pld@AddTo\pld@lastline{% |
| 2612 |
&\multispan\pld@span${}=% |
| 2613 |
\pld@PrintPolyWithDelims\pld@divisor |
| 2614 |
\expandafter\pld@IfSum\expandafter{\pld@divisor}{}{\cdot}% |
| 2615 |
\expandafter\pld@IfSum\expandafter{\pld@quotient}\pld@true |
| 2616 |
\pld@false |
| 2617 |
\pld@if \pld@leftdelim |
| 2618 |
\pld@PrintPolyShadow |
| 2619 |
\pld@rightdelim |
| 2620 |
\else \pld@PrintPolyShadow \fi |
| 2621 |
\pld@firstfalse |
| 2622 |
\expandafter\pld@PrintRemain\expandafter{\pld@remainder}$}% |
| 2623 |
\else |
| 2624 |
% \end{macrocode} |
| 2625 |
% And now for style C. Note that we `smash' the depth of the fraction. |
| 2626 |
% \begin{macrocode} |
| 2627 |
\if C\pld@style |
| 2628 |
\pld@AddTo\pld@lastline{% |
| 2629 |
&\multispan\pld@span$\pld@leftxdelim\strut\pld@rightdelim |
| 2630 |
\pld@div |
| 2631 |
\pld@PrintPolyWithDelims\pld@divisor= |
| 2632 |
\pld@PrintPolyShadow |
| 2633 |
\ifx\pld@remainder\@empty\else |
| 2634 |
+{}% |
| 2635 |
\setbox\z@=\hbox{$\displaystyle |
| 2636 |
\frac{\let\strut\@empty\pld@firsttrue \expandafter |
| 2637 |
\pld@PrintRemain\expandafter{\pld@remainder}}% |
| 2638 |
{\let\strut\@empty\pld@PrintPoly\pld@divisor}$}% |
| 2639 |
\dp\z@=\z@\box\z@ |
| 2640 |
\fi |
| 2641 |
$}% |
| 2642 |
\else |
| 2643 |
% \end{macrocode} |
| 2644 |
% Finally, style D. |
| 2645 |
% \begin{macrocode} |
| 2646 |
\pld@AddTo\pld@lastline{% |
| 2647 |
\cr% |
| 2648 |
\noalign{\vskip-\normalbaselineskip}% |
| 2649 |
\multispan\pld@maxcol~&~&\multispan\pld@span${}\vrule~% |
| 2650 |
\pld@PrintPoly\pld@divisor\hfil\hfil$\cr% |
| 2651 |
\cline{\pld@maxcolplustwo-\pld@linepos}% |
| 2652 |
\multispan\pld@maxcol~&~&\multispan\pld@span${}\vrule height |
| 2653 |
2.25ex~\pld@PrintPolyShadow$\hfil\cr\noalign{\vskip-2\normalbaselineskip}}% |
| 2654 |
\fi |
| 2655 |
\fi |
| 2656 |
\fi |
| 2657 |
% \end{macrocode} |
| 2658 |
% Eventually we replace the first line in |\pld@allines| by |\pld@lastline| |
| 2659 |
% or add |\pld@currentline| before doing so. |
| 2660 |
% \begin{macrocode} |
| 2661 |
\expandafter\pld@AR@\pld@allines\relax} |
| 2662 |
\def\pld@AR@#1\cr#2\relax{% |
| 2663 |
\pld@iftopresult |
| 2664 |
\let\pld@allines\pld@currentline |
| 2665 |
\pld@AddTo\pld@allines{\pld@lastline\cr #2}% |
| 2666 |
\else |
| 2667 |
\let\pld@allines\pld@lastline |
| 2668 |
\pld@AddTo\pld@allines{\cr #2}% |
| 2669 |
\fi} |
| 2670 |
% \end{macrocode} |
| 2671 |
% The dividend and quotient above are built by looking up the position of each |
| 2672 |
% monomial in |\pld@pattern| and inserting these monomials. |#1|, which is |
| 2673 |
% |\pld@PLD| or |\pld@XPLD|, is used to print the monomial. |
| 2674 |
% \begin{macrocode} |
| 2675 |
\def\pld@AR@col#1#2#3+{% |
| 2676 |
\ifx\relax#3\@empty\else |
| 2677 |
\ifx\@empty#3\@empty\else |
| 2678 |
\def\pld@monom{#3}\@tempcnta\@ne |
| 2679 |
\expandafter\pld@InsertItems@find\pld@pattern\relax&% |
| 2680 |
\pld@InsertItems@do#2{#1{#3}}% |
| 2681 |
\fi |
| 2682 |
\expandafter\pld@AR@col\expandafter#1\expandafter#2% |
| 2683 |
\fi} |
| 2684 |
% \end{macrocode} |
| 2685 |
% \end{macro} |
| 2686 |
% |
| 2687 |
% \begin{macro}{\pld@PLD} |
| 2688 |
% \begin{macro}{\pld@XPLD} |
| 2689 |
% have been used above several times. They print single monomials in the |
| 2690 |
% horizontal alignment of a long division or put a |\phantom| around it. |
| 2691 |
% \begin{macrocode} |
| 2692 |
\def\pld@PLD#1{\ifx\@empty#1\@empty\else\pld@PrintMonoms#1+\relax+\fi} |
| 2693 |
\def\pld@XPLD#1{\phantom{\pld@PLD{#1}}} |
| 2694 |
% \end{macrocode} |
| 2695 |
% \end{macro} |
| 2696 |
% \end{macro} |
| 2697 |
% |
| 2698 |
% |
| 2699 |
% \section{Euclidean algorithm} |
| 2700 |
% |
| 2701 |
% \begin{macro}{\pld@LongEuclideanPoly} |
| 2702 |
% Assign the `smaller' polynom to |\pld@remainder| and the other to |
| 2703 |
% |\pld@divisor| by doing one or two divisions. Additionally |\pld@vb| is |
| 2704 |
% initialized for the case that no further division is needed (v0.15). |
| 2705 |
% \begin{macrocode} |
| 2706 |
\def\pld@LongEuclideanPoly#1#2{% |
| 2707 |
\pld@false \let\pld@allines\@empty |
| 2708 |
\pld@DivPoly#1#2% |
| 2709 |
\ifx\pld@quotient\@empty |
| 2710 |
\pld@DivPoly#2#1% |
| 2711 |
\pld@InsertEuclidean#2#1% |
| 2712 |
\let\pld@vb#1% |
| 2713 |
\else |
| 2714 |
\pld@InsertEuclidean#1#2% |
| 2715 |
\let\pld@vb#2% |
| 2716 |
\fi |
| 2717 |
% \end{macrocode} |
| 2718 |
% Now we start the well known Euclidean algorithm. |\pld@va| and |\pld@vb| |
| 2719 |
% are used as temporary scratch `registers'. |
| 2720 |
% \begin{macrocode} |
| 2721 |
\pld@LongEuclideanPoly@l} |
| 2722 |
\def\pld@LongEuclideanPoly@l{% |
| 2723 |
\ifx\pld@remainder\@empty \else |
| 2724 |
\let\pld@va\pld@divisor |
| 2725 |
\let\pld@vb\pld@remainder |
| 2726 |
% \end{macrocode} |
| 2727 |
% \begin{macrocode} |
| 2728 |
\pld@DivPoly\pld@va\pld@vb |
| 2729 |
\pld@Simplify\pld@quotient \pld@Simplify\pld@remainder |
| 2730 |
\pld@InsertEuclidean\pld@va\pld@vb |
| 2731 |
% \end{macrocode} |
| 2732 |
% \begin{macrocode} |
| 2733 |
\expandafter\pld@LongEuclideanPoly@l |
| 2734 |
\fi} |
| 2735 |
% \end{macrocode} |
| 2736 |
% \end{macro} |
| 2737 |
% |
| 2738 |
% \begin{macro}{\pld@InsertEuclidean} |
| 2739 |
% Each step inserts one line with dividend, divisor, quotient, and remainder. |
| 2740 |
% \begin{macrocode} |
| 2741 |
\def\pld@InsertEuclidean#1#2{% |
| 2742 |
\ifx \pld@allines\@empty \else |
| 2743 |
\pld@AddTo\pld@allines{\noalign{\vskip\jot}}% |
| 2744 |
\fi |
| 2745 |
\pld@Extend\pld@allines{\expandafter\pld@PrintPolyArg |
| 2746 |
\expandafter{#1}&}% |
| 2747 |
\pld@Extend\pld@allines{\expandafter\pld@PrintPolyWithDelimsArg |
| 2748 |
\expandafter{#2}\hfil\cdot\hfil}% |
| 2749 |
\pld@Extend\pld@allines{\expandafter\pld@PrintPolyWithDelimsArg |
| 2750 |
\expandafter{\pld@quotient}&}% |
| 2751 |
\pld@Extend\pld@allines{\expandafter\pld@PrintPolyWithDelimsArg |
| 2752 |
\expandafter{\pld@remainder}\cr}} |
| 2753 |
% \end{macrocode} |
| 2754 |
% \end{macro} |
| 2755 |
% |
| 2756 |
% \begin{macro}{\pld@PrintLongEuclidean} |
| 2757 |
% just like |\pld@PrintLongDiv|. |
| 2758 |
% \begin{macrocode} |
| 2759 |
\def\pld@PrintLongEuclidean{ |
| 2760 |
\ensuremath{\hbox{\vtop{\begingroup |
| 2761 |
\offinterlineskip \tabskip=\z@ |
| 2762 |
\halign{\strut\pld@firsttrue\hfil$##$% |
| 2763 |
&${}={}$\hfil$##$\hfil |
| 2764 |
&${}+##$\hfil\cr \pld@allines}% |
| 2765 |
\endgroup}}}} |
| 2766 |
% \end{macrocode} |
| 2767 |
% \end{macro} |
| 2768 |
% |
| 2769 |
% |
| 2770 |
% \section{Factorization} |
| 2771 |
% |
| 2772 |
% The algorithm is based on the following proposition: |
| 2773 |
% All \emph{rational} zeros of a polynomial $a_nX^n+\ldots+a_1X+a_0$ with |
| 2774 |
% \emph{integer} coefficients are among the fractions $\pm\frac\beta\alpha$ |
| 2775 |
% where $\beta$ is a divisor of $a_0$ and $\alpha$ a divisor of the leading |
| 2776 |
% coefficient $a_n$. |
| 2777 |
% So our first tasks are to iterate through divisors and to test for zeros. |
| 2778 |
% |
| 2779 |
% \begin{macro}{\pld@NextDivisorPair} |
| 2780 |
% \marg{integer $a$}\marg{integer $b$} |
| 2781 |
% \begin{describe} |
| 2782 |
% |\@tempcnta| and |\@tempcntb| get the next divisors of \meta{integer $a$} |
| 2783 |
% and \meta{integer $b$}. |\pld@if| is set false if and only if all divisor |
| 2784 |
% pairs has been iterated through. At the beginning we must initialize |
| 2785 |
% |\@tempcnta=\z@| and |\@tempcntb=\@ne|. |
| 2786 |
% \end{describe} |
| 2787 |
% First |\@tempcnta| becomes the next divisor of |#1| and then |\@tempcntb| |
| 2788 |
% the next one of |#2| if and only if |#1| has no more divisors (which resets |
| 2789 |
% |\@tempcnta| automatically). |
| 2790 |
% \begin{macrocode} |
| 2791 |
\def\pld@NextDivisorPair#1#2{% |
| 2792 |
\pld@NextDivisor\@tempcnta{#1}% |
| 2793 |
\pld@if\else |
| 2794 |
\pld@NextDivisor\@tempcntb{#2}% |
| 2795 |
\fi} |
| 2796 |
% \end{macrocode} |
| 2797 |
% Here we advance the counter by one until the counter gets too big (note that |
| 2798 |
% this `$>$' requires the arguments to |\pld@NextDivisorPair| being positive) |
| 2799 |
% \ldots |
| 2800 |
% \begin{macrocode} |
| 2801 |
\def\pld@NextDivisor#1#2{% |
| 2802 |
\advance#1\@ne |
| 2803 |
\ifnum #1>#2\relax |
| 2804 |
#1\@ne \pld@false |
| 2805 |
\expandafter\@gobbletwo |
| 2806 |
\else |
| 2807 |
% \end{macrocode} |
| 2808 |
% or a divisor of |#2|. |
| 2809 |
% \begin{macrocode} |
| 2810 |
\@multicnt #2\relax |
| 2811 |
\divide\@multicnt#1\multiply\@multicnt#1% |
| 2812 |
\advance\@multicnt-#2\relax |
| 2813 |
\ifnum \@multicnt=\z@ |
| 2814 |
\pld@true |
| 2815 |
\expandafter\expandafter\expandafter\@gobbletwo |
| 2816 |
\else |
| 2817 |
\expandafter\expandafter\expandafter\pld@NextDivisor |
| 2818 |
\fi |
| 2819 |
\fi |
| 2820 |
#1{#2}} |
| 2821 |
% \end{macrocode} |
| 2822 |
% \end{macro} |
| 2823 |
% |
| 2824 |
% \begin{macro}{\pld@FindZeros} |
| 2825 |
% \marg{integer $a$}\marg{integer $b$} |
| 2826 |
% \begin{describe} |
| 2827 |
% is the main loop: while not all divisor pairs has been processed, we check |
| 2828 |
% whether $\pm$|\@tempcntb|$/$|\@tempcnta| is a zero. |
| 2829 |
% \end{describe} |
| 2830 |
% \begin{macrocode} |
| 2831 |
\def\pld@FindZeros#1#2{% |
| 2832 |
\pld@NextDivisorPair{#1}{#2}% |
| 2833 |
\pld@if |
| 2834 |
\pld@CheckZeros |
| 2835 |
\def\pld@next{\pld@FindZeros{#1}{#2}}% |
| 2836 |
\expandafter\pld@next |
| 2837 |
\fi} |
| 2838 |
% \end{macrocode} |
| 2839 |
% \end{macro} |
| 2840 |
% |
| 2841 |
% \begin{macro}{\pld@CheckZeros} |
| 2842 |
% When $\frac\beta\alpha$ isn't a zero any more, we add the zero with |
| 2843 |
% multiplicity |\@multicnt| \ldots |
| 2844 |
% \begin{macrocode} |
| 2845 |
\def\pld@CheckZeros{% |
| 2846 |
\pld@true \@multicnt\z@ |
| 2847 |
\loop \pld@if |
| 2848 |
\pld@CheckZero{\the\@tempcnta}{\the\@tempcntb}% |
| 2849 |
\repeat |
| 2850 |
\pld@AddRationalZero{\the\@tempcnta}{\the\@tempcntb}% |
| 2851 |
% \end{macrocode} |
| 2852 |
% and do the same for $-\frac\beta\alpha$. Note that the multiplicity might be |
| 2853 |
% zero. |
| 2854 |
% \begin{macrocode} |
| 2855 |
\pld@true \@multicnt\z@ |
| 2856 |
\loop \pld@if |
| 2857 |
\pld@CheckZero{-\the\@tempcnta}{\the\@tempcntb}% |
| 2858 |
\repeat |
| 2859 |
\pld@AddRationalZero{-\the\@tempcnta}{\the\@tempcntb}} |
| 2860 |
% \end{macrocode} |
| 2861 |
% To check for the zero $\frac\beta\alpha$, we divide |\pld@current| by the |
| 2862 |
% linear factor $X-\frac\beta\alpha$. Note that |\pld@tempoly| contains the |
| 2863 |
% string |\pld@V{X}1| where |X| is replaced by the actual variable; so we just |
| 2864 |
% need to append the fraction. |
| 2865 |
% \begin{macrocode} |
| 2866 |
\def\pld@CheckZero#1#2{% |
| 2867 |
\begingroup |
| 2868 |
\edef\pld@temp{{-#2}{#1}}% |
| 2869 |
\pld@Extend\pld@tempoly{\expandafter+\expandafter\pld@R\pld@temp}% |
| 2870 |
\let\pld@stage\maxdimen \pld@DividePoly\pld@current\pld@tempoly |
| 2871 |
\ifx\pld@remainder\@empty |
| 2872 |
\global\let\@gtempa\pld@quotient |
| 2873 |
\aftergroup\pld@true |
| 2874 |
\else |
| 2875 |
\aftergroup\pld@false |
| 2876 |
\fi |
| 2877 |
\endgroup |
| 2878 |
% \end{macrocode} |
| 2879 |
% If the division was successful, we advance the multiplicity and assign the |
| 2880 |
% new polynomial. |
| 2881 |
% \begin{macrocode} |
| 2882 |
\pld@if |
| 2883 |
\advance\@multicnt\@ne |
| 2884 |
\let\pld@current\@gtempa |
| 2885 |
\fi} |
| 2886 |
% \end{macrocode} |
| 2887 |
% \end{macro} |
| 2888 |
% |
| 2889 |
% \begin{macro}{\pld@AddRationalZero} |
| 2890 |
% Here we add code to |\pld@allines| to \emph{print} the factor with its |
| 2891 |
% multiplicity. |
| 2892 |
% \begin{macrocode} |
| 2893 |
\def\pld@AddRationalZero#1#2{% |
| 2894 |
\ifnum\@multicnt=\z@\else |
| 2895 |
\pld@AccuSetX{#2}{-#1}% |
| 2896 |
\pld@AccuGet\pld@temp |
| 2897 |
\edef\pld@temp{\noexpand\pld@R\pld@temp}% |
| 2898 |
% \end{macrocode} |
| 2899 |
% Yet |\pld@temp| contains the rational. Note that the `accumulator detour' is |
| 2900 |
% needed to get rid of |\@tempcnta| and |b|. Eventually append the zero with |
| 2901 |
% the exponent if necessary. |
| 2902 |
% \begin{macrocode} |
| 2903 |
\expandafter\pld@AddZero\expandafter{\pld@temp}% |
| 2904 |
\ifnum\@multicnt=\@ne\else |
| 2905 |
\edef\pld@temp{^{\the\@multicnt}}% |
| 2906 |
\pld@Extend\pld@allines\pld@temp |
| 2907 |
\fi |
| 2908 |
\fi} |
| 2909 |
% \end{macrocode} |
| 2910 |
% \end{macro} |
| 2911 |
% |
| 2912 |
% \begin{macro}{\pld@AddZero} |
| 2913 |
% We add |\pld@leftdelim| |\pld@firsttrue| |\pld@PLD{X+#1}| |\pld@rightdelim| |
| 2914 |
% to the current factorization |\pld@allines|. |
| 2915 |
% \begin{macrocode} |
| 2916 |
\def\pld@AddZero#1{% |
| 2917 |
\pld@Extend\pld@allines{\expandafter\pld@leftdelim |
| 2918 |
\expandafter\pld@firsttrue |
| 2919 |
\expandafter\pld@PLD |
| 2920 |
\expandafter{\pld@tempoly+#1}% |
| 2921 |
\pld@rightdelim}} |
| 2922 |
% \end{macrocode} |
| 2923 |
% \end{macro} |
| 2924 |
% |
| 2925 |
% These are the basic definitions. Now remember that the proposition above |
| 2926 |
% requires integers coefficients, but we want to support rationals. To do |
| 2927 |
% this, we multiply the polynomial virtually by the least common multiple of |
| 2928 |
% all denominators. `Virtually' means that we only multiply the leading |
| 2929 |
% coefficient and the absolute term to get the correct divisors, but not the |
| 2930 |
% real polynomial. |
| 2931 |
% |
| 2932 |
% \begin{macro}{\pld@FactorizeInit} |
| 2933 |
% And this is done here. The argument is a definition to be executed with |
| 2934 |
% appropriate data (the multiplied coeffients) as arguments at the end of this |
| 2935 |
% macro. Again we redefine |\pld@R|,\ldots,|\pld@V| and iterate through the |
| 2936 |
% monomials. The accumulator holds the least common multiple and |\@multicnt| |
| 2937 |
% the least exponent of the variable (since we need to divide by $X^k$ to get |
| 2938 |
% an absolute term). |
| 2939 |
% \begin{macrocode} |
| 2940 |
\def\pld@FactorizeInit#1{% |
| 2941 |
\begingroup |
| 2942 |
\pld@firsttrue \let\pld@sub\@empty |
| 2943 |
\pld@AccuSetX11% |
| 2944 |
\let\pld@R\pld@FRational |
| 2945 |
\let\pld@F\@gobbletwo |
| 2946 |
\let\pld@S\@gobbletwo |
| 2947 |
\let\pld@V\pld@FVar |
| 2948 |
\expandafter\pld@FactorizeInit@\pld@current+\relax+% |
| 2949 |
% \end{macrocode} |
| 2950 |
% Below you'll see |\@gtemp| $=$ leading coeffients and |\pld@lastline| $=$ |
| 2951 |
% coefficient of absolute term (after division by $X^k$). Here we multiply by |
| 2952 |
% the accumulator and make the results positive (if we're advised to do this) |
| 2953 |
% and \ldots |
| 2954 |
% \begin{macrocode} |
| 2955 |
\pld@if |
| 2956 |
\pld@AccuGet\pld@temp |
| 2957 |
\expandafter\pld@AccuMul\@gtempa |
| 2958 |
\pld@AccuIfNegative{\pld@AccuNegate}{}% |
| 2959 |
\pld@AccuGet\pld@va |
| 2960 |
\expandafter\pld@AccuSetX\pld@temp |
| 2961 |
\expandafter\pld@AccuMul\pld@lastline |
| 2962 |
\pld@AccuIfNegative{\pld@AccuNegate}{}% |
| 2963 |
\pld@AccuGet\pld@vb |
| 2964 |
\else |
| 2965 |
\let\pld@va\@gtempa |
| 2966 |
\let\pld@vb\pld@lastline |
| 2967 |
\fi |
| 2968 |
% \end{macrocode} |
| 2969 |
% set the coefficient of $X^1$ if necessary---or any other variable power 1. |
| 2970 |
% \begin{macrocode} |
| 2971 |
\ifx\pld@sub\@empty \def\pld@sub{01}\fi |
| 2972 |
% \end{macrocode} |
| 2973 |
% Then we prepare the arguments for the macro to be executed at the end. In |
| 2974 |
% particular, |\pld@tempoly| is defined to define |\def\pld@tempoly{\pld@V{X}}| |
| 2975 |
% below, and this definition is cared out before we execute the macro |#2|. |
| 2976 |
% \begin{macrocode} |
| 2977 |
\edef\pld@temp{\noexpand#1\pld@va\pld@vb{\the\@multicnt}\pld@sub}% |
| 2978 |
\pld@Extend\pld@tempoly{\pld@temp}% |
| 2979 |
\global\let\@gtempa\pld@tempoly |
| 2980 |
\endgroup |
| 2981 |
\@gtempa} |
| 2982 |
% \end{macrocode} |
| 2983 |
% The submacro just iterates down the monomials. |
| 2984 |
% \begin{macrocode} |
| 2985 |
\def\pld@FactorizeInit@#1+{% |
| 2986 |
\ifx\relax#1\else |
| 2987 |
\def\pld@lastline{11}% |
| 2988 |
#1% |
| 2989 |
\expandafter\pld@FactorizeInit@ |
| 2990 |
\fi} |
| 2991 |
% \end{macrocode} |
| 2992 |
% The following two definitions store the leading coefficient in |\@gtempa|, |
| 2993 |
% the last in |\pld@lastline|, the coefficient of $X^1$ in |\pld@sub|, update |
| 2994 |
% the least common multiple, \ldots |
| 2995 |
% \begin{macrocode} |
| 2996 |
\def\pld@FRational#1#2{% |
| 2997 |
\def\pld@lastline{{#1}{#2}}% |
| 2998 |
\pld@iffirst |
| 2999 |
\global\let\@gtempa\pld@lastline |
| 3000 |
\def\pld@tempoly{\@multicnt\z@}% |
| 3001 |
\fi |
| 3002 |
\pld@LCM{#2}% |
| 3003 |
\@multicnt\z@} |
| 3004 |
% \end{macrocode} |
| 3005 |
% and save the variable and its `leading' exponent in |\@multicnt|. Note that |
| 3006 |
% these two definitions are cared out later on, and the assignment of |
| 3007 |
% |\@multicnt| here saves the exponent of the last monomial. |
| 3008 |
% \begin{macrocode} |
| 3009 |
\def\pld@FVar#1#2{% |
| 3010 |
\pld@iffirst |
| 3011 |
\pld@firstfalse |
| 3012 |
\global\let\@gtempa\pld@lastline |
| 3013 |
\def\pld@tempoly{\def\pld@tempoly{\pld@V{#1}}% |
| 3014 |
\@multicnt#2\relax}% |
| 3015 |
\fi |
| 3016 |
\@multicnt#2\relax |
| 3017 |
% \end{macrocode} |
| 3018 |
% \begin{macrocode} |
| 3019 |
\ifnum\@multicnt=\@ne |
| 3020 |
\let\pld@sub\pld@lastline |
| 3021 |
\fi} |
| 3022 |
% \end{macrocode} |
| 3023 |
% \end{macro} |
| 3024 |
% |
| 3025 |
% \begin{macro}{\pld@Factorize} |
| 3026 |
% If the given polynomial is zero, the factorization is `$0$'. Otherwise we |
| 3027 |
% initialize data and start the algorithm. Note that |\pld@Factorize@| is an |
| 3028 |
% argument to |\pld@FactorizeInit| and called from inside with appropriate |
| 3029 |
% arguments. |
| 3030 |
% \begin{macrocode} |
| 3031 |
\def\pld@Factorize#1{% |
| 3032 |
\ifx\@empty#1\@empty |
| 3033 |
\def\pld@allines{\pld@PrintPolyWithDelims\@empty}% |
| 3034 |
\else |
| 3035 |
\let\pld@allines\@empty |
| 3036 |
\let\pld@current#1% |
| 3037 |
\pld@true \pld@FactorizeInit |
| 3038 |
\pld@Factorize@ |
| 3039 |
\fi} |
| 3040 |
% \end{macrocode} |
| 3041 |
% Now the arguments are |
| 3042 |
% |#1#2|$=$\meta{`leading coefficient'}, |
| 3043 |
% |#3#4|$=$\meta{`coefficient of least monomial'}, |
| 3044 |
% |#5|$=$\meta{least exponent}, |
| 3045 |
% |#6#7|$=$\meta{coefficient of linear summand}. |
| 3046 |
% Here the first two coefficient have been multiplied by the least common |
| 3047 |
% multiple of all denominators. The factorization gets `variable power |#5|' |
| 3048 |
% and the current polynomial is divided this. |
| 3049 |
% \begin{macrocode} |
| 3050 |
\def\pld@Factorize@#1#2#3#4#5#6#7{% |
| 3051 |
\ifnum #5=\z@\else |
| 3052 |
\pld@Extend\pld@allines{\expandafter\pld@firsttrue |
| 3053 |
\expandafter\pld@PLD |
| 3054 |
\expandafter{\pld@tempoly{#5}}}% |
| 3055 |
\let\pld@va\pld@tempoly |
| 3056 |
\pld@AddTo\pld@va{{-#5}}% |
| 3057 |
\pld@MultiplyPoly\pld@current\pld@current\pld@va |
| 3058 |
\fi |
| 3059 |
% \end{macrocode} |
| 3060 |
% Then we initialize the variable's exponent and the two divisors and really |
| 3061 |
% start the algorithm. |
| 3062 |
% \begin{macrocode} |
| 3063 |
\pld@AddTo\pld@tempoly{1}% |
| 3064 |
\@tempcnta\z@ \@tempcntb\@ne |
| 3065 |
\pld@FindZeros{#1}{#3}% |
| 3066 |
% \end{macrocode} |
| 3067 |
% Eventually we scan the remaining polynomial without multiplying the |
| 3068 |
% coefficient by the least common multiple of the denominators. |
| 3069 |
% \begin{macrocode} |
| 3070 |
\pld@false \pld@FactorizeInit |
| 3071 |
\pld@FactorizeFinal} |
| 3072 |
% \end{macrocode} |
| 3073 |
% \end{macro} |
| 3074 |
% |
| 3075 |
% \begin{macro}{\pld@FactorizeFinal} |
| 3076 |
% Thus we might find nonrational zeros here. Let $a={}$|#1#2|, $b={}$|#6#7|, |
| 3077 |
% and $c={}$|#3#4|. Then we have to calculate |
| 3078 |
% $\frac b{2a}\pm\sqrt{\frac{b^2}{4a^2}-\frac ca}$. |
| 3079 |
% \begin{macrocode} |
| 3080 |
\def\pld@FactorizeFinal#1#2#3#4#5#6#7{% |
| 3081 |
\ifnum\@multicnt=\tw@ |
| 3082 |
\pld@AddTo\pld@tempoly{1}% |
| 3083 |
\pld@AccuSetX{#6}{#7}% |
| 3084 |
\pld@AccuIfZero{\let\pld@va\@empty}% |
| 3085 |
{\pld@AccuMul12% |
| 3086 |
\pld@AccuMul{#2}{#1}% |
| 3087 |
\pld@AccuGet\pld@sub |
| 3088 |
\edef\pld@va{\noexpand\pld@R\pld@sub+}% |
| 3089 |
% \end{macrocode} |
| 3090 |
% That's $\frac b{2a}$ so far, stored away in |\pld@va|. |
| 3091 |
% \begin{macrocode} |
| 3092 |
\expandafter\pld@AccuMul\pld@sub}% |
| 3093 |
\begingroup |
| 3094 |
\pld@AccuSetX{#3}{#4}% |
| 3095 |
\pld@AccuMul{-#2}{#1}% |
| 3096 |
\pld@AccuGet\pld@temp |
| 3097 |
\global\let\@gtempa\pld@temp |
| 3098 |
\endgroup |
| 3099 |
\expandafter\pld@AccuAdd\@gtempa |
| 3100 |
% \end{macrocode} |
| 3101 |
% And now the accumulator holds $\frac{b^2}{4a^2}-\frac ca$. Depending on the |
| 3102 |
% sign---complex zeros are not supported, even though the complex analysis was |
| 3103 |
% my field of activity for some years---we do nothing more or get a printable |
| 3104 |
% version of the square root and \ldots |
| 3105 |
% \begin{macrocode} |
| 3106 |
\pld@AccuIfNegative |
| 3107 |
{\@multicnt\tw@}% |
| 3108 |
{\pld@AccuGet\pld@temp |
| 3109 |
\expandafter\pld@FDefSqrt\pld@temp |
| 3110 |
% \end{macrocode} |
| 3111 |
% append two nonrational zeros. |
| 3112 |
% \begin{macrocode} |
| 3113 |
\let\pld@vb\pld@va |
| 3114 |
\pld@AddTo\pld@vb{\pld@R{-1}1}% |
| 3115 |
\pld@Extend\pld@va{\pld@temp}% |
| 3116 |
\pld@Extend\pld@vb{\pld@temp}% |
| 3117 |
\expandafter\pld@AddZero\expandafter{\pld@va}% |
| 3118 |
\expandafter\pld@AddZero\expandafter{\pld@vb}% |
| 3119 |
\@multicnt\z@ |
| 3120 |
}% |
| 3121 |
\fi |
| 3122 |
% \end{macrocode} |
| 3123 |
% In this latter case or if the polynomial's degree has been zero from the |
| 3124 |
% beginning of this macro, we check whether we can omit the leading coeffient. |
| 3125 |
% \begin{macrocode} |
| 3126 |
\ifnum\@multicnt=\z@ |
| 3127 |
\pld@AccuSetX{#1}{#2}% |
| 3128 |
\pld@AccuIfOne{\let\pld@current\@empty}% |
| 3129 |
{\def\pld@current{\pld@R{#1}{#2}}}% |
| 3130 |
\fi |
| 3131 |
\ifx\pld@current\@empty\else |
| 3132 |
\let\pld@temp\pld@allines |
| 3133 |
\def\pld@allines{\pld@PrintPolyWithDelims\pld@current}% |
| 3134 |
\pld@Extend\pld@allines{\pld@temp}% |
| 3135 |
\fi} |
| 3136 |
% \end{macrocode} |
| 3137 |
% \end{macro} |
| 3138 |
% |
| 3139 |
% \begin{macro}{\pld@FDefSqrt} |
| 3140 |
% Finally we need a printable version of the square root of |#1|$/$|#2|. |
| 3141 |
% We use the fact, that the root is not rational, thus only one of nominator |
| 3142 |
% and denominator can be a square. Depending on the actual numbers, the |
| 3143 |
% submacro defines |\pld@temp| correctly. Note that this submacro is used to |
| 3144 |
% make the code more readable. |
| 3145 |
% \begin{macrocode} |
| 3146 |
\def\pld@FDefSqrt#1#2{% |
| 3147 |
\pld@IfSquare{#1}% |
| 3148 |
{\pld@FDefSqrt@{\pld@R{\pld@temp}1}% |
| 3149 |
{\sqrt{\noexpand\pld@R{#2}1}}}% |
| 3150 |
{\pld@IfSquare{#2}% |
| 3151 |
{\ifnum\pld@temp=\@ne |
| 3152 |
\pld@FDefSqrt@{\sqrt{\noexpand\pld@R{#1}1}}{}% |
| 3153 |
\else |
| 3154 |
\pld@FDefSqrt@{\sqrt{\noexpand\pld@R{#1}1}}% |
| 3155 |
{\pld@R{\pld@temp}1}% |
| 3156 |
\fi}% |
| 3157 |
{\def\pld@temp{\pld@F{\sqrt{\pld@R{#1}{#2}}}{}}}% |
| 3158 |
}} |
| 3159 |
% \end{macrocode} |
| 3160 |
% It just (e)defines a general fraction without expanding some control |
| 3161 |
% sequences. |
| 3162 |
% \begin{macrocode} |
| 3163 |
\def\pld@FDefSqrt@#1#2{% |
| 3164 |
\edef\pld@temp{\noexpand\pld@F |
| 3165 |
{\noexpand#1}% |
| 3166 |
{\ifx\@empty#2\@empty\else \noexpand#2\fi}}} |
| 3167 |
% \end{macrocode} |
| 3168 |
% \end{macro} |
| 3169 |
% |
| 3170 |
% |
| 3171 |
% \section{Arithmetic} |
| 3172 |
% |
| 3173 |
% \begin{macro}{\pld@IfSquare} |
| 3174 |
% Let's begin with the macro used in the last section. As always, we initialize |
| 3175 |
% data. |
| 3176 |
% \begin{macrocode} |
| 3177 |
\def\pld@IfSquare#1{% |
| 3178 |
\@tempcnta=#1\relax |
| 3179 |
\@multicnt\@tempcnta \@tempcntb\@tempcnta |
| 3180 |
\divide\@tempcntb\tw@ \advance\@tempcntb\@ne |
| 3181 |
% \end{macrocode} |
| 3182 |
% Then we use the iteration |
| 3183 |
% $x_{n+1}=\left\lfloor \frac12\left(a+\lfloor\frac a{x_n}\rfloor\right) |
| 3184 |
% \right\rfloor$ |
| 3185 |
% to calculate $\lfloor \sqrt{|#1|}\rfloor$. In version 0.11 there was a bug |
| 3186 |
% in the loop condition. |
| 3187 |
% \begin{macrocode} |
| 3188 |
\loop \ifnum\@tempcntb<\@multicnt |
| 3189 |
\@multicnt\@tempcntb |
| 3190 |
\@tempcntb\@tempcnta |
| 3191 |
\divide\@tempcntb\@multicnt |
| 3192 |
\advance\@tempcntb\@multicnt |
| 3193 |
\divide\@tempcntb\tw@ |
| 3194 |
\repeat |
| 3195 |
% \end{macrocode} |
| 3196 |
% Now it is easy to decide whether |#1| is a square. |
| 3197 |
% \begin{macrocode} |
| 3198 |
\edef\pld@temp{\the\@multicnt}% |
| 3199 |
\multiply\@multicnt\@multicnt |
| 3200 |
\ifnum \@multicnt=\@tempcnta |
| 3201 |
\expandafter\@firstoftwo |
| 3202 |
\else |
| 3203 |
\expandafter\@secondoftwo |
| 3204 |
\fi} |
| 3205 |
% \end{macrocode} |
| 3206 |
% \end{macro} |
| 3207 |
% |
| 3208 |
% \begin{macro}{\pld@Euclidean} |
| 3209 |
% \begin{macro}{\pld@XEuclidean} |
| 3210 |
% \meta{macro}\marg{integer $a$}\marg{integer $b$} |
| 3211 |
% \begin{describe} |
| 3212 |
% The base of our rational arithmetic is the Euclidean algorithm. The contents |
| 3213 |
% of \meta{macro} becomes |{|$\frac a{\gcd(a,b)}$|}{|$\frac b{\gcd(a,b)}$|}| |
| 3214 |
% in the first case. The eXtended version adds the greatest common divisor: |
| 3215 |
% |{|$\frac a{\gcd(a,b)}$|}{|$\frac b{\gcd(a,b)}$|}{|$\gcd(a,b)$|}|. |
| 3216 |
% \end{describe} |
| 3217 |
% As described, the second version extends the first definition. |
| 3218 |
% \begin{macrocode} |
| 3219 |
\def\pld@XEuclidean#1#2#3{\pld@Euclidean#1{#2}{#3}% |
| 3220 |
\edef#1{#1{\the\@tempcntb}}} |
| 3221 |
% \end{macrocode} |
| 3222 |
% Here we assign the number smaller in size (in fact not bigger) to |
| 3223 |
% |\@tempcnta| and the other to |\@tempcntb|, and make both nonnegative. |
| 3224 |
% \begin{macrocode} |
| 3225 |
\def\pld@Euclidean#1#2#3{% |
| 3226 |
\@tempcnta#2\relax \divide\@tempcnta#3\relax |
| 3227 |
\ifnum\@tempcnta=\z@ \@tempcnta#2\relax \@tempcntb#3\relax |
| 3228 |
\else \@tempcnta#3\relax \@tempcntb#2\relax \fi |
| 3229 |
\ifnum\@tempcnta<\z@ \@tempcnta -\@tempcnta \fi |
| 3230 |
\ifnum\@tempcntb<\z@ \@tempcntb -\@tempcntb \fi |
| 3231 |
% \end{macrocode} |
| 3232 |
% The loop leaves the greatest common divisor in |\@tempcntb|. |
| 3233 |
% \begin{macrocode} |
| 3234 |
\pld@Euclidean@l |
| 3235 |
% \end{macrocode} |
| 3236 |
% Now we only have to divide the numbers and define the macro |#1|. |
| 3237 |
% \begin{macrocode} |
| 3238 |
\@tempcnta#3\relax \divide\@tempcnta\@tempcntb |
| 3239 |
\edef#1{{\the\@tempcnta}}% |
| 3240 |
\@tempcnta#2\relax \divide\@tempcnta\@tempcntb |
| 3241 |
\edef#1{{\the\@tempcnta}#1}} |
| 3242 |
% \end{macrocode} |
| 3243 |
% And here is the usual Euclidean algorithm.\footnote{Note that |
| 3244 |
% \texttt{\bslash @multicnt} is used as a third scratch counter.} |
| 3245 |
% \begin{macrocode} |
| 3246 |
\def\pld@Euclidean@l{% |
| 3247 |
\ifnum\@tempcnta=\z@\else |
| 3248 |
\@multicnt\@tempcntb |
| 3249 |
\divide\@tempcntb\@tempcnta |
| 3250 |
\multiply\@tempcntb\@tempcnta |
| 3251 |
\advance\@multicnt -\@tempcntb |
| 3252 |
\@tempcntb\@tempcnta |
| 3253 |
\@tempcnta\@multicnt |
| 3254 |
\expandafter\pld@Euclidean@l |
| 3255 |
\fi} |
| 3256 |
% \end{macrocode} |
| 3257 |
% \end{macro} |
| 3258 |
% \end{macro} |
| 3259 |
% |
| 3260 |
% \begin{macro}{\pld@AccuGet} |
| 3261 |
% A rational number is stored as \marg{nominator}\marg{denominator} in |
| 3262 |
% the accumulator |\pld@accu|. Here we ensure that the denominator is |
| 3263 |
% positive. |
| 3264 |
% \begin{macrocode} |
| 3265 |
\def\pld@AccuGet{\expandafter\pld@AccuGet@\pld@accu} |
| 3266 |
\def\pld@AccuGet@#1#2#3{% |
| 3267 |
\ifnum #2<\z@ \edef#3{{-#1}{-#2}}\else\edef#3{{#1}{#2}}\fi} |
| 3268 |
% \end{macrocode} |
| 3269 |
% \end{macro} |
| 3270 |
% |
| 3271 |
% \begin{macro}{\pld@AccuSet} |
| 3272 |
% \begin{macro}{\pld@AccuSetX} |
| 3273 |
% Setting the accumulator is also simple. We divide the nominator and |
| 3274 |
% denominator by their greatest common divisor only in the first case. |
| 3275 |
% \begin{macrocode} |
| 3276 |
\def\pld@AccuSet#1#2{% |
| 3277 |
\def\pld@accu{{#1}{#2}}% |
| 3278 |
\expandafter\pld@Euclidean\expandafter\pld@accu\pld@accu |
| 3279 |
\expandafter\pld@AccuGet@\pld@accu\pld@accu} |
| 3280 |
\def\pld@AccuSetX#1#2{\pld@AccuGet@{#1}{#2}\pld@accu} |
| 3281 |
% \end{macrocode} |
| 3282 |
% \end{macro} |
| 3283 |
% \end{macro} |
| 3284 |
% |
| 3285 |
% \begin{macro}{\pld@AccuPrint} |
| 3286 |
% Here we typeset the rational via |\frac| only if necessary. |
| 3287 |
% \begin{macrocode} |
| 3288 |
\def\pld@AccuPrint{\expandafter\pld@AccuPrint@\pld@accu} |
| 3289 |
\def\pld@AccuPrint@#1#2{% |
| 3290 |
\ifnum #2=\@ne \number#1\else \frac{\number#1}{\number#2}\fi} |
| 3291 |
% \end{macrocode} |
| 3292 |
% \end{macro} |
| 3293 |
% |
| 3294 |
% \begin{macro}{\pld@AccuNegate} |
| 3295 |
% We just negate the nominator. |
| 3296 |
% \begin{macrocode} |
| 3297 |
\def\pld@AccuNegate{\expandafter\pld@AccuNegate@\pld@accu} |
| 3298 |
\def\pld@AccuNegate@#1#2{\def\pld@accu{{-#1}{#2}}} |
| 3299 |
% \end{macrocode} |
| 3300 |
% \end{macro} |
| 3301 |
% |
| 3302 |
% \begin{macro}{\pld@AccuIfZero} |
| 3303 |
% \begin{macro}{\pld@AccuIfOne} |
| 3304 |
% \begin{macro}{\pld@AccuIfAbsOne} |
| 3305 |
% \begin{macro}{\pld@AccuIfNegative} |
| 3306 |
% All these definitions work the same way: expand |\pld@accu|, do the test, |
| 3307 |
% and either execute the first \meta{then} or the second \meta{else} part. |
| 3308 |
% \begin{macrocode} |
| 3309 |
\def\pld@AccuIfZero{\expandafter\pld@AccuIfZero@\pld@accu} |
| 3310 |
\def\pld@AccuIfZero@#1#2{% |
| 3311 |
\ifnum #1=\z@ \expandafter\@firstoftwo |
| 3312 |
\else \expandafter\@secondoftwo \fi} |
| 3313 |
% \end{macrocode} |
| 3314 |
% \begin{macrocode} |
| 3315 |
\def\pld@AccuIfOne{\expandafter\pld@AccuIfOne@\pld@accu} |
| 3316 |
\def\pld@AccuIfOne@#1#2{% |
| 3317 |
\ifnum #1=#2\relax \expandafter\@firstoftwo |
| 3318 |
\else \expandafter\@secondoftwo \fi} |
| 3319 |
% \end{macrocode} |
| 3320 |
% \begin{macrocode} |
| 3321 |
\def\pld@AccuIfAbsOne{\expandafter\pld@AccuIfAbsOne@\pld@accu} |
| 3322 |
\def\pld@AccuIfAbsOne@#1#2{% |
| 3323 |
\ifnum #1=#2\relax \expandafter\@firstoftwo \else |
| 3324 |
\ifnum -#1=#2\relax |
| 3325 |
\expandafter\expandafter\expandafter\@firstoftwo |
| 3326 |
\else |
| 3327 |
\expandafter\expandafter\expandafter\@secondoftwo |
| 3328 |
\fi |
| 3329 |
\fi} |
| 3330 |
% \end{macrocode} |
| 3331 |
% \begin{macrocode} |
| 3332 |
\def\pld@AccuIfNegative{\expandafter\pld@AccuIfNegative@\pld@accu} |
| 3333 |
\def\pld@AccuIfNegative@#1#2{% |
| 3334 |
\ifnum #1<\z@ \@tempcnta\m@ne \else \@tempcnta\@ne \fi |
| 3335 |
\ifnum #2<\z@ \@tempcnta -\@tempcnta \fi |
| 3336 |
\ifnum \@tempcnta<\z@ \expandafter\@firstoftwo |
| 3337 |
\else \expandafter\@secondoftwo \fi} |
| 3338 |
% \end{macrocode} |
| 3339 |
% \end{macro} |
| 3340 |
% \end{macro} |
| 3341 |
% \end{macro} |
| 3342 |
% \end{macro} |
| 3343 |
% |
| 3344 |
% \begin{macro}{\pld@LCM} |
| 3345 |
% \marg{integer} |
| 3346 |
% \begin{describe} |
| 3347 |
% puts the least common multiple of \meta{integer} and \meta{nominator} into |
| 3348 |
% the accumulator. |
| 3349 |
% \end{describe} |
| 3350 |
% We use $\mathop{\mathrm{lcm}}(a,b)=\frac{a\cdot b}{\gcd(a,b)}= |
| 3351 |
% \frac{|\#1|}{\gcd(|\#1|,|\#3|)}\cdot |#3|$. |
| 3352 |
% \begin{macrocode} |
| 3353 |
\def\pld@LCM{\expandafter\pld@LCM@\pld@accu} |
| 3354 |
\def\pld@LCM@#1#2#3{% |
| 3355 |
\pld@Euclidean\pld@accu{#1}{#3}% |
| 3356 |
\@tempcnta\expandafter\@firstoftwo\pld@accu\relax |
| 3357 |
\multiply\@tempcnta#3\relax |
| 3358 |
\edef\pld@accu{{\the\@tempcnta}1}} |
| 3359 |
% \end{macrocode} |
| 3360 |
% \end{macro} |
| 3361 |
% |
| 3362 |
% \begin{macro}{\pld@AccuMul} |
| 3363 |
% We use the Euclidean algorithm before \ldots |
| 3364 |
% \begin{macrocode} |
| 3365 |
\def\pld@AccuMul{\expandafter\pld@AccuMul@\pld@accu} |
| 3366 |
\def\pld@AccuMul@#1#2#3#4{% |
| 3367 |
\begingroup |
| 3368 |
\pld@Euclidean\pld@va{#1}{#4}% |
| 3369 |
\pld@Euclidean\pld@vb{#3}{#2}% |
| 3370 |
\pld@ExpandTwo\pld@AccuMul@m\pld@va\pld@vb |
| 3371 |
\xdef\@gtempa{{\the\@tempcnta}{\the\@tempcntb}}% |
| 3372 |
\endgroup |
| 3373 |
\let\pld@accu\@gtempa} |
| 3374 |
% \end{macrocode} |
| 3375 |
% we multiply nominators and denominators. |
| 3376 |
% \begin{macrocode} |
| 3377 |
\def\pld@AccuMul@m#1#2#3#4{% |
| 3378 |
\@tempcnta#1\relax \multiply\@tempcnta#3\relax |
| 3379 |
\@tempcntb#2\relax \multiply\@tempcntb#4\relax} |
| 3380 |
% \end{macrocode} |
| 3381 |
% \end{macro} |
| 3382 |
% |
| 3383 |
% \begin{macro}{\pld@AccuAdd} |
| 3384 |
% The addition of two rationals is the most interesting part in this section. |
| 3385 |
% It is based upon the fact that $\frac ab+\frac cd=\frac{ad+bc}{bd}$ has |
| 3386 |
% \begin{eqnarray*} |
| 3387 |
% \meta{nominator}&=&\left(\textstyle\frac a{\gcd(a,c)}\cdot\frac d{\gcd(b,d)}+\frac b{\gcd(b,d)}\cdot\frac c{\gcd(a,c)}\right)\cdot\gcd(a,c),\\ |
| 3388 |
% \meta{denominator}&=&\frac{bd}{\gcd(b,d)}, |
| 3389 |
% \end{eqnarray*} |
| 3390 |
% where the factors and sums are all integers and potentially smaller in size |
| 3391 |
% than in $\frac{ad+bc}{db}$. As one quickly verifies\footnote{Sorry for that |
| 3392 |
% phrase, I'm a mathematician $:\!-)$}, the nominator and denominator has the |
| 3393 |
% greatest common divisor |
| 3394 |
% \[\gcd(-\cdot-,b)\cdot\gcd\left(\textstyle-\cdot-,\frac d{\gcd(b,d)}\right),\] |
| 3395 |
% where $-\cdot-$ stands for the big parenthesized sum of the nominator. |
| 3396 |
%^^A The greatest common divisor is even \[\gcd(-\cdot-,b)\gcd(-\cdot-,d)\), |
| 3397 |
%^^A but we don't need either of this explicitly, the Euclidean algorithm will |
| 3398 |
%^^A take care of this. |
| 3399 |
% |
| 3400 |
% The implementation again expands |\pld@accu|, \ldots |
| 3401 |
% \begin{macrocode} |
| 3402 |
\def\pld@AccuAdd{\expandafter\pld@AccuAdd@a\pld@accu} |
| 3403 |
% \end{macrocode} |
| 3404 |
% and provides another submacro with the necessary fractions. |
| 3405 |
% \begin{macrocode} |
| 3406 |
\def\pld@AccuAdd@a#1#2#3#4{% |
| 3407 |
\ifnum#3=\z@\else |
| 3408 |
\pld@AccuAdd@c{#1}{#2}{#3}{#4}% |
| 3409 |
\fi} |
| 3410 |
\def\pld@AccuAdd@c#1#2#3#4{% |
| 3411 |
\begingroup |
| 3412 |
\pld@XEuclidean\pld@va{#1}{#3}% |
| 3413 |
\pld@XEuclidean\pld@vb{#2}{#4}% |
| 3414 |
\edef\pld@va{\pld@va\pld@vb}% |
| 3415 |
\expandafter\pld@AccuAdd@b\pld@va{#2}{#4}} |
| 3416 |
% \end{macrocode} |
| 3417 |
% We now have |
| 3418 |
% \begin{eqnarray*} |
| 3419 |
% \meta{nominator}&=&\left(|#1|\cdot|#5|+|#4|\cdot|#2|\right)\cdot |#3|,\\ |
| 3420 |
% \meta{denominator}&=&|#7|\cdot|#5|. |
| 3421 |
% \end{eqnarray*} |
| 3422 |
% \begin{macrocode} |
| 3423 |
\def\pld@AccuAdd@b#1#2#3#4#5#6#7#8{% |
| 3424 |
\endgroup |
| 3425 |
\@tempcnta#1\relax \multiply\@tempcnta#5\relax |
| 3426 |
\@tempcntb#2\relax \multiply\@tempcntb#4\relax |
| 3427 |
\advance\@tempcnta\@tempcntb |
| 3428 |
% \end{macrocode} |
| 3429 |
% Finally we divide by $\gcd(-\cdot-,b)$ and multiply with |
| 3430 |
% $\frac{|\#3|}{|\#5|}$, which implicitly divides the result by |
| 3431 |
% $\gcd\left(-\cdot-,\frac d{\gcd(b,d)}\right)$. |
| 3432 |
% \begin{macrocode} |
| 3433 |
\expandafter\pld@Euclidean\expandafter\pld@accu\expandafter |
| 3434 |
{\the\@tempcnta}{#7}% |
| 3435 |
\pld@AccuMul{#3}{#5}} |
| 3436 |
% \end{macrocode} |
| 3437 |
% \end{macro} |
| 3438 |
% |
| 3439 |
% |
| 3440 |
% \section{Horner's scheme} |
| 3441 |
% |
| 3442 |
% The following code lines come without comments. Good luck! |
| 3443 |
% |
| 3444 |
% \begin{macrocode} |
| 3445 |
\def\pld@KVCases#1#2#3{% |
| 3446 |
\@ifundefined{pld@#1@#2}% |
| 3447 |
{\PackageError{Polynom}{Unknown value #2}{Try #3.}}% |
| 3448 |
{\csname pld@#1@#2\endcsname}} |
| 3449 |
\def\pld@KVIf#1#2{% |
| 3450 |
\@ifundefined{if#2}% |
| 3451 |
{\PackageError{Polynom}{Unknown value #2}{Try `true' or `false'.}}% |
| 3452 |
{\expandafter\let\expandafter#1\csname if#2\endcsname}} |
| 3453 |
% \end{macrocode} |
| 3454 |
% \begin{macrocode} |
| 3455 |
\define@key{pld}{showbase}[middle]{\pld@KVCases{showbase}{#1}{`false', `top', `middle', or `bottom'}}% |
| 3456 |
\def\pld@showbase@false{\let\pld@basepos=f} |
| 3457 |
\def\pld@showbase@top{\let\pld@basepos=t} |
| 3458 |
\def\pld@showbase@middle{\let\pld@basepos=m} |
| 3459 |
\def\pld@showbase@bottom{\let\pld@basepos=b} |
| 3460 |
\define@key{pld}{showvar}[true]{\pld@KVIf\pld@ifshowvar{#1}} |
| 3461 |
\define@key{pld}{showbasesep}[true]{\pld@KVIf\pld@ifshowbasesep{#1}} |
| 3462 |
\define@key{pld}{showmiddlerow}[true]{\pld@KVIf\pld@ifshowmiddlerow{#1}} |
| 3463 |
|
| 3464 |
\define@key{pld}{resultstyle}{\def\pld@resultstyle{#1}} |
| 3465 |
\define@key{pld}{resultleftrule}[true]{\pld@KVIf\pld@ifhornerresultleftrule{#1}} |
| 3466 |
\define@key{pld}{resultrightrule}[true]{\pld@KVIf\pld@ifhornerresultrightrule{#1}} |
| 3467 |
\define@key{pld}{resultbottomrule}[true]{\pld@KVIf\pld@ifhornerresultbottomrule{#1}} |
| 3468 |
|
| 3469 |
\define@key{pld}{tutor}[true]{\pld@KVCases{tutor}{#1}{`true', or `false'}}% |
| 3470 |
\def\pld@tutor@true{\let\pld@iftutor\iftrue} |
| 3471 |
\def\pld@tutor@false{\let\pld@iftutor\iffalse} |
| 3472 |
\define@key{pld}{tutorstyle}{\def\pld@tutorstyle{#1}} |
| 3473 |
\define@key{pld}{tutorlimit}{\@tempcnta#1\relax \advance\@tempcnta\@ne |
| 3474 |
\edef\pld@tutorlimit{\the\@tempcnta}} |
| 3475 |
|
| 3476 |
\define@key{pld}{equalcolwidths}[true]{\pld@KVIf\pld@ifhornerequalcolwidths{#1}} |
| 3477 |
\define@key{pld}{arraycolsep}{\def\pld@hornerarraycolsep{#1\relax}} |
| 3478 |
\define@key{pld}{arrayrowsep}{\def\pld@hornerarrayrowsep{#1\relax}} |
| 3479 |
|
| 3480 |
\polyset{showbase, |
| 3481 |
showvar=false, |
| 3482 |
showbasesep=true, |
| 3483 |
showmiddlerow=true, |
| 3484 |
tutor=false, |
| 3485 |
tutorlimit=1, |
| 3486 |
tutorstyle=\scriptscriptstyle, |
| 3487 |
resultstyle=, |
| 3488 |
resultleftrule=false, |
| 3489 |
resultrightrule=false, |
| 3490 |
resultbottomrule=false, |
| 3491 |
equalcolwidths=true, |
| 3492 |
arraycolsep=\arraycolsep, |
| 3493 |
arrayrowsep=.5\arraycolsep} |
| 3494 |
|
| 3495 |
\define@key{pld}{mul}{\def\pld@mul{#1}}% |
| 3496 |
\define@key{pld}{plusface}{\pld@KVCases{hornerplusface}{#1}{`left' or 'right'}}% |
| 3497 |
\define@key{pld}{plusyoffset}{\@tempdima#1\relax \edef\pld@hornerplusyoffset{\the\@tempdima}} |
| 3498 |
\define@key{pld}{downarrowxoffset}{\@tempdima#1\relax \edef\pld@hornerdownarrowxoffset{\the\@tempdima}} |
| 3499 |
\define@key{pld}{diagarrowxoffset}{\@tempdima#1\relax \edef\pld@hornerdiagarrowxoffset{\the\@tempdima}} |
| 3500 |
\define@key{pld}{downarrow}{\def\pld@hornerdownarrow{#1}} |
| 3501 |
\define@key{pld}{diagarrow}{\def\pld@hornerdiagarrow{#1}} |
| 3502 |
\def\pld@hornerplusface@left{\let\pld@hornerplusface\llap} |
| 3503 |
\def\pld@hornerplusface@right{\let\pld@hornerplusface\rlap} |
| 3504 |
\polyset{mul=\cdot, |
| 3505 |
plusface=right, |
| 3506 |
plusyoffset=\z@, |
| 3507 |
downarrowxoffset=\z@, |
| 3508 |
diagarrowxoffset=\z@, |
| 3509 |
downarrow={\vector(0,-1){2.5}}, |
| 3510 |
diagarrow={\vector(2,1){1.6}}} |
| 3511 |
% \end{macrocode} |
| 3512 |
% \begin{macrocode} |
| 3513 |
\newcommand*\polyhornerscheme[1][]{% |
| 3514 |
\begingroup |
| 3515 |
\let\pld@stage\maxdimen \polyset{#1}% |
| 3516 |
\pld@GetPoly{\pld@polya}% |
| 3517 |
{\expandafter\pld@Horner\expandafter{\pld@polya}% |
| 3518 |
\endgroup \ignorespaces}} |
| 3519 |
|
| 3520 |
\def\pld@Horner#1{% |
| 3521 |
\pld@GetTotalDegree\pld@degree{#1}% |
| 3522 |
\pld@Horner@#1++% |
| 3523 |
\pld@ArrangeHorner} |
| 3524 |
|
| 3525 |
\def\pld@Horner@#1+{% |
| 3526 |
\pld@SplitMonom\pld@HornerInit{#1}% |
| 3527 |
\pld@HornerIterate} |
| 3528 |
|
| 3529 |
\def\pld@HornerIterate#1+{% |
| 3530 |
\advance\@tempcnta\m@ne |
| 3531 |
\ifx\@empty#1\@empty |
| 3532 |
\ifnum \@tempcnta<\z@ |
| 3533 |
\let\pld@next\@empty |
| 3534 |
\else |
| 3535 |
\pld@HornerStep{\pld@R01}{}% |
| 3536 |
\def\pld@next{\pld@HornerIterate+}% |
| 3537 |
\fi |
| 3538 |
\else |
| 3539 |
\pld@GetTotalDegree\pld@degree{#1}% |
| 3540 |
\ifnum \pld@degree=\@tempcnta |
| 3541 |
\pld@SplitMonom\pld@HornerStep{#1}% |
| 3542 |
\let\pld@next\pld@HornerIterate |
| 3543 |
\else |
| 3544 |
\pld@HornerStep{\pld@R01}{}% |
| 3545 |
\def\pld@next{\pld@HornerIterate#1+}% |
| 3546 |
\fi |
| 3547 |
\fi |
| 3548 |
\pld@next} |
| 3549 |
|
| 3550 |
\def\pld@HornerStep#1#2{% |
| 3551 |
\pld@AddTo\pld@lastline{&\pld@PrintPolyArg{#1}}% |
| 3552 |
\pld@MultiplyPoly\pld@lastsum\pld@lastsum\pld@value |
| 3553 |
\pld@Simplify\pld@lastsum |
| 3554 |
\ifx\pld@lastsum\@empty \def\pld@lastsum{\pld@R 01}\fi |
| 3555 |
\pld@AddTo\pld@subline{&}% |
| 3556 |
\pld@HornerExtendLine\pld@subline |
| 3557 |
\pld@AddTo\pld@lastsum{+#1}% |
| 3558 |
\pld@Simplify\pld@lastsum |
| 3559 |
\pld@AddTo\pld@currentline{&}% |
| 3560 |
\pld@iftutor |
| 3561 |
\pld@HornerExtendCurrentLine |
| 3562 |
\advance\@multicnt\@ne |
| 3563 |
\pld@HornerIfTutorStage{\pld@HornerExtendTutor\pld@HornerOtherDown}% |
| 3564 |
\advance\@multicnt\m@ne |
| 3565 |
\ifnum\@tempcnta>\z@ |
| 3566 |
\pld@HornerIfTutorStage{\pld@HornerExtendTutor\pld@HornerDiag}% |
| 3567 |
\fi |
| 3568 |
\else |
| 3569 |
\pld@HornerExtendCurrentLine |
| 3570 |
\fi} |
| 3571 |
|
| 3572 |
\def\pld@HornerExtendTutor#1{% |
| 3573 |
\ifnum\@tempcnta=\z@ \pld@AddTo\pld@hornerresult#1% |
| 3574 |
\else \pld@AddTo\pld@currentline#1\fi} |
| 3575 |
\def\pld@HornerExtendCurrentLine{% |
| 3576 |
\ifnum\@tempcnta=\z@ |
| 3577 |
\let\pld@hornerresult\@empty |
| 3578 |
\pld@Extend\pld@hornerresult{\expandafter{\expandafter\pld@resultstyle\expandafter{% |
| 3579 |
\expandafter\pld@PrintPolyArg\expandafter{\pld@lastsum}}}}% |
| 3580 |
\pld@HornerIfStage{}% |
| 3581 |
{\let\pld@lastsum\@empty |
| 3582 |
\pld@Extend\pld@lastsum{\expandafter\phantom\expandafter{\pld@hornerresult}}% |
| 3583 |
\let\pld@hornerresult\pld@lastsum}% |
| 3584 |
\expandafter\@gobbletwo |
| 3585 |
\fi |
| 3586 |
\pld@HornerExtendLine\pld@currentline} |
| 3587 |
\def\pld@HornerExtendLine#1{% |
| 3588 |
\pld@HornerIfStage{\pld@Extend#1{\expandafter\pld@PrintPolyArg\expandafter{\pld@lastsum}}}% |
| 3589 |
{\pld@Extend#1{\expandafter\phantom\expandafter{% |
| 3590 |
\expandafter\pld@PrintPolyArg\expandafter{\pld@lastsum}}}}% |
| 3591 |
} |
| 3592 |
|
| 3593 |
\def\pld@HornerFirstDown{% |
| 3594 |
\rlap{\kern\pld@hornerdownarrowxoffset\relax |
| 3595 |
\unitlength\ht\@arstrutbox |
| 3596 |
\begin{picture}(0,0)% |
| 3597 |
\setbox\z@\hbox{$\pld@tutorstyle{\pld@hornerdownarrow}$}% |
| 3598 |
\put(0,.5){\raise\ht\z@\hbox{\raise\dp\z@\copy\z@}}% |
| 3599 |
\end{picture}}} |
| 3600 |
\def\pld@HornerOtherDown{% |
| 3601 |
\pld@HornerFirstDown |
| 3602 |
\pld@hornerplusface{\kern\pld@hornerdownarrowxoffset\relax |
| 3603 |
\smash{\raise\pld@hornerplusyoffset |
| 3604 |
\hbox{\raise.5\ht\@arstrutbox |
| 3605 |
\vbox to 2.5\ht\@arstrutbox |
| 3606 |
{\vss$\pld@tutorstyle{+}$\vss}}}}} |
| 3607 |
\def\pld@HornerDiag{% |
| 3608 |
\rlap{\kern\pld@hornerdiagarrowxoffset\relax |
| 3609 |
\unitlength\ht\@arstrutbox |
| 3610 |
\begin{picture}(0,0)% |
| 3611 |
\setbox\z@\hbox{$\pld@tutorstyle{\pld@hornerdiagarrow}$}% |
| 3612 |
\put(0,.5){\box\z@}% |
| 3613 |
\put(0,.5){\kern.55\ht\@arstrutbox |
| 3614 |
$\pld@tutorstyle{\pld@mul \pld@hornerleftdelim |
| 3615 |
\pld@PrintPolyWithDelims\pld@value |
| 3616 |
\pld@hornerrightdelim}$}% |
| 3617 |
\end{picture}}} |
| 3618 |
|
| 3619 |
\def\pld@HornerIfStage{% |
| 3620 |
\advance\@multicnt\m@ne |
| 3621 |
\ifnum\@multicnt>\z@ \expandafter\@firstoftwo |
| 3622 |
\else \expandafter\@secondoftwo \fi} |
| 3623 |
\def\pld@HornerIfTutorStage{% |
| 3624 |
\ifnum\@multicnt>\@ne |
| 3625 |
\ifnum\@multicnt>\pld@tutorlimit |
| 3626 |
\expandafter\expandafter\expandafter\@gobble |
| 3627 |
\else |
| 3628 |
\expandafter\expandafter\expandafter\@firstofone |
| 3629 |
\fi |
| 3630 |
\else |
| 3631 |
\expandafter\@gobble |
| 3632 |
\fi} |
| 3633 |
|
| 3634 |
\def\pld@HornerInit#1#2{% |
| 3635 |
\let\pld@V\@firstoftwo |
| 3636 |
\ifx\@empty#2\@empty\else |
| 3637 |
\edef\pld@var{#2}% |
| 3638 |
\@ifundefined{pld@value@\pld@var}% |
| 3639 |
{\PackageError{Polynom}{Missing value for variable \pld@var}{}% |
| 3640 |
\@namedef{pld@value@\pld@var}{\pld@R01}}% |
| 3641 |
{}% |
| 3642 |
\expandafter\let\expandafter\pld@value\csname pld@value@\pld@var\endcsname |
| 3643 |
\fi |
| 3644 |
% |
| 3645 |
\setbox\@tempboxa\hbox{$\pld@PrintPoly\pld@value$}% |
| 3646 |
\pld@ifminus |
| 3647 |
\let\pld@hornerleftdelim(% |
| 3648 |
\let\pld@hornerrightdelim)% |
| 3649 |
\else |
| 3650 |
\let\pld@hornerleftdelim\@empty |
| 3651 |
\let\pld@hornerrightdelim\@empty |
| 3652 |
\fi |
| 3653 |
% |
| 3654 |
\@multicnt\pld@stage\relax |
| 3655 |
\@tempcnta\pld@degree\relax |
| 3656 |
\def\pld@lastline{\pld@PrintPolyArg{#1}}% |
| 3657 |
\let\pld@subline\@empty |
| 3658 |
\let\pld@currentline\@empty |
| 3659 |
\def\pld@lastsum{#1}% |
| 3660 |
\pld@iftutor |
| 3661 |
\pld@HornerExtendCurrentLine |
| 3662 |
\advance\@multicnt\@ne |
| 3663 |
\pld@HornerIfTutorStage{\pld@AddTo\pld@currentline\pld@HornerFirstDown}% |
| 3664 |
\advance\@multicnt\m@ne |
| 3665 |
\ifnum\@tempcnta>\z@ |
| 3666 |
\pld@HornerIfTutorStage{\pld@AddTo\pld@currentline\pld@HornerDiag}% |
| 3667 |
\fi |
| 3668 |
\else |
| 3669 |
\pld@HornerExtendCurrentLine |
| 3670 |
\fi |
| 3671 |
\def\pld@lastsum{#1}% |
| 3672 |
\@tempcnta\pld@degree\relax % init moved up, delete this? |
| 3673 |
\advance\@tempcnta\thr@@ |
| 3674 |
\edef\pld@hornermaxcol{\the\@tempcnta}% |
| 3675 |
\@tempcnta\pld@degree\relax} |
| 3676 |
% \end{macrocode} |
| 3677 |
% \begin{macrocode} |
| 3678 |
\def\pld@ArrangeHorner{% |
| 3679 |
\begingroup |
| 3680 |
\@tempdima\z@ |
| 3681 |
\pld@MeasureCells\pld@lastline |
| 3682 |
\pld@MeasureCells\pld@subline |
| 3683 |
\pld@MeasureCells\pld@currentline |
| 3684 |
\pld@MeasureCells\pld@hornerresult |
| 3685 |
\everycr{}\tabskip\z@skip |
| 3686 |
\@tempdimb\ht\strutbox \advance\@tempdimb\pld@hornerarrayrowsep |
| 3687 |
\@tempdimc\dp\strutbox \advance\@tempdimc\pld@hornerarrayrowsep |
| 3688 |
\setbox\@arstrutbox\hbox{\vrule \@height\@tempdimb |
| 3689 |
\@depth\@tempdimc |
| 3690 |
\@width\z@}% |
| 3691 |
\pld@ifhornerequalcolwidths\else |
| 3692 |
\def\@startpbox##1{\hfil\vtop\bgroup \hbox\bgroup \@arrayparboxrestore}% |
| 3693 |
\def\@endpbox{\@finalstrut\@arstrutbox \egroup\par\egroup}% |
| 3694 |
\fi |
| 3695 |
\def\pld@leftdelim{(}\def\pld@rightdelim{)}% |
| 3696 |
\leavevmode |
| 3697 |
\hbox{$\vcenter{\offinterlineskip \arraycolsep\pld@hornerarraycolsep |
| 3698 |
\halign{\@arstrut |
| 3699 |
\hskip\arraycolsep \hfill\ensuremath{##}\hskip\arraycolsep |
| 3700 |
&##&&% |
| 3701 |
\hskip\arraycolsep \@startpbox\@tempdima\hfill\ensuremath{##}\@endpbox \hskip\arraycolsep\cr |
| 3702 |
\pld@ShowBase t&\pld@ifshowbasesep\vrule\fi&\pld@lastline\cr |
| 3703 |
\pld@ifshowmiddlerow \pld@ShowBase m&\pld@ifshowbasesep\vrule\fi&\pld@subline\cr \fi \cline{2-\pld@hornermaxcol}% |
| 3704 |
\pld@ShowBase b&&\pld@currentline\omit |
| 3705 |
\pld@ifhornerresultleftrule \vrule \fi |
| 3706 |
\hskip\arraycolsep \@startpbox\@tempdima\relax\hfill\ensuremath{\pld@hornerresult}\@endpbox \hskip\arraycolsep |
| 3707 |
\pld@ifhornerresultrightrule \vrule \fi \cr |
| 3708 |
\pld@ifhornerresultbottomrule \cline{\pld@hornermaxcol-\pld@hornermaxcol} \fi |
| 3709 |
}% |
| 3710 |
}$}% |
| 3711 |
\endgroup} |
| 3712 |
% \end{macrocode} |
| 3713 |
% \begin{macrocode} |
| 3714 |
\def\pld@ShowBase#1{% |
| 3715 |
\ifx#1\pld@basepos |
| 3716 |
\pld@ifshowvar x=\fi\pld@PrintPoly\pld@value |
| 3717 |
\fi} |
| 3718 |
% \end{macrocode} |
| 3719 |
% \begin{macrocode} |
| 3720 |
\def\pld@MeasureCells#1{\expandafter\pld@MeasureCells@#1&\@nil&} |
| 3721 |
\def\pld@MeasureCells@#1&{% |
| 3722 |
\ifx\@nil#1\relax\else |
| 3723 |
\setbox\@tempboxa\hbox{\ensuremath{#1}}% |
| 3724 |
\ifdim\wd\@tempboxa>\@tempdima |
| 3725 |
\@tempdima\wd\@tempboxa |
| 3726 |
\fi |
| 3727 |
\expandafter\pld@MeasureCells@ |
| 3728 |
\fi} |
| 3729 |
% \end{macrocode} |
| 3730 |
% \begin{macrocode} |
| 3731 |
\def\pld@GetTotalDegree#1#2{% |
| 3732 |
\begingroup |
| 3733 |
\let\pld@R\@gobbletwo \let\pld@F\@gobbletwo \let\pld@S\@gobbletwo |
| 3734 |
\def\pld@V##1##2{\advance\@tempcnta##2\relax}% |
| 3735 |
\def#1##1+##2\@nil{##1}% |
| 3736 |
\edef#1{\@tempcnta\z@#1#2+\@nil}% |
| 3737 |
#1\xdef\@gtempa{\the\@tempcnta}% |
| 3738 |
\endgroup |
| 3739 |
\let#1\@gtempa} |
| 3740 |
% \end{macrocode} |
| 3741 |
% |
| 3742 |
% \begin{macrocode} |
| 3743 |
%</package> |
| 3744 |
% \end{macrocode} |
| 3745 |
% |
| 3746 |
% |
| 3747 |
% \begingroup\small |
| 3748 |
% \section{History} |
| 3749 |
% \renewcommand\labelitemi{--} |
| 3750 |
% \begin{itemize} |
| 3751 |
% \item[0.1] from 2000/04/18 (private test version) |
| 3752 |
% \item long division algorithm et al, basic scanner, basic simplification |
| 3753 |
% \item[0.11] from 2001/03/23 |
| 3754 |
% \item total reimplementation except division algorithm et al |
| 3755 |
% \item improved: scanner, simplification, handling of symbols |
| 3756 |
% \item new: gcd, factorization, rational arithmetic, key $=$ value interface |
| 3757 |
% \item[0.12] from 2001/04/11 |
| 3758 |
% \item bugs in |\pld@IfSquare| and |\pld@ScanOpen| removed |
| 3759 |
% \item slightly improved scanner (|^| on expressions) and new key \texttt{delims} |
| 3760 |
% \item[0.13] from 2001/09/27 |
| 3761 |
% \item new \texttt{stage} key allows stepwise printing of polynomial long divisions |
| 3762 |
% \item[0.14] from 2002/01/10 |
| 3763 |
% \item added \texttt{style=C}; this led to the new \texttt{div} key and the optional argument of \texttt{delims} |
| 3764 |
% \item[0.15] from 2002/10/29 |
| 3765 |
% \item bugs fixed in |\polygcd| and |\pld@LongEuclideanPoly| |
| 3766 |
% \item[0.16] from 2004/08/12 |
| 3767 |
% \item added (bugfixed version of) Horner's scheme and fixed bug in |\pld@InsertItems@find| |
| 3768 |
% \end{itemize} |
| 3769 |
% The phrase `et al' stands for the definitions directly related to the |
| 3770 |
% division algorithm: polynomial multiplication, |\pld@IfNeedsDivision|, |
| 3771 |
% subtraction, and alignment. |
| 3772 |
% \medskip |
| 3773 |
% |
| 3774 |
% \noindent TODO: |
| 3775 |
% \begin{itemize} |
| 3776 |
% \item PBZ |
| 3777 |
% \item use \texttt{stage} also on \cs{polylonggcd} |
| 3778 |
% \item possibility to highlight the most recent \texttt{stage} |
| 3779 |
% \item remove problems inside array and tabular |
| 3780 |
% \item carry out dependencies in the implementation part (or remove them) |
| 3781 |
% \item internal data format: introduce linear, square factors? |
| 3782 |
% \item generalize exponents for printing $y^{(4)}-y^{(2)}+\ldots$ ? |
| 3783 |
% \item define derivatives? |
| 3784 |
% \end{itemize} |
| 3785 |
% \endgroup |
| 3786 |
% |
| 3787 |
% |
| 3788 |
% \Finale |
| 3789 |
% |
| 3790 |
%% |
| 3791 |
%% |
| 3792 |
\endinput |