summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/polynomial/polynomial.dtx
blob: 041759449e0dcc5d29bc25155c3dd7b340ff964d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
% \iffalse meta-comment
% 
% by Stefan Höst 2007
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.2 of this license or (at your option) any later
% version. The latest version of this license is in:
%
% http://www.latex-project.org/lppl.txt
%
% and version 1.2 or later is part of all distributions of
% LaTeX version 1999/12/01 or later.
%
% \fi
% 
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{polynomial}
%<*driver>
\documentclass{ltxdoc}
\usepackage{polynomial}
\usepackage{amsmath}
\usepackage{t1enc}
\usepackage[a4paper,body={150mm,230mm}]{geometry}
\def\labelitemi{$\blacktriangleright$}
\def\labelitemii{$\triangleright$}
\newdimen\tabsepadd
\tabsepadd=2mm
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\OnlyDescription
\begin{document}
\DocInput{polynomial.dtx}
\end{document}
%</driver>
% \fi
% \CheckSum{0}
% \changes{v1.0}{2007/02/04}{Initial version}
% \changes{v1.1}{2007/03/17}{Test}
% \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
% \title{The \textsf{polynomial.sty} package\thanks{Version 1.1, 2007/03/17}}
% \author{Stefan Höst}
% \date{}
% \maketitle
% \noindent
% The package \texttt{polynomial.sty} offers an easy way to write
% (univariate) polynomials and rational functions. It defines two commands,
% one for polynomials \verb|\polynomial{coeffs}| and one for rational functions
% \verb|\polynomialfrac{Numerator}{Denominator}|. The first of them,
% \verb|\polynomial|, prints a polynomial with the coefficients in the
% comma separated list of the argument. The second, \verb|\polynomialfrac|,
% prints a fraction of two polynomials. There is also an optional
% argument to the commands that changees the properties of the
% polynomials. The default values can be changed
% by the command \verb|\polynomialstyle|. In the following the commands
% are described by a set of explained examples. 

% The coefficients of the polynomials  are given as a comma separated
% lists. If a coefficient is a zero it is not printed, and if it is 1
% only the monomial is printed. By default there is a plus in
% between two terms, but if the first character of the coefficient is
% minus it is changed to minus. 
% \par\strut\par
% \noindent
% \textbf{Examples:}
% \newline\strut
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial{1,2,3,4,5}| 
%   & $\polynomial{1,2,3,4,5}$\\[\tabsepadd]
%   \verb|\polynomial{3,35,0,0,45}|
%   & $\polynomial{3,35,0,0,45}$\\[\tabsepadd]
%   \verb|\polynomial{c_0,-c_1,c_2,-c_3,c_4}| 
%   & $\polynomial{c_0,-c_1,c_2,-c_3,c_4}$\\[\tabsepadd]
%   \verb|\polynomial{0,0,0,1,-1,1,0,1,0,0,1,0,0}| 
%   & $\polynomial{0,0,0,1,-1,1,0,1,0,0,1,0,0}$\\[\tabsepadd]
%   \verb|\polynomial{-3,A\sin(\alpha t), e^{j\phi},|\newline 
%     \verb| \left(\sum_{k=0}^{\infty}a^k\right)}|
%   & $\displaystyle\polynomial{-3,A\sin(\alpha t),e^{j\phi},
%     \left(\sum_{k=0}^{\infty}a^k\right)}$\\[\tabsepadd]
%   \verb|\polynomialfrac{a,b,c,-d,e}{f,g,h,i}| 
%   & $\displaystyle\polynomialfrac{a,b,c,-d,e}{f,g,h,i}$
% \end{tabular}
% \par\strut\par
% There is a set of variables that can be used in an optional
% argument, using the keyval-style. By default the the exponents
% increases from left to right. There are two Boolean variables,
% \verb|falling| and \verb|reciprocal|, that change this. Both are used 
% as \verb|falling=true| or \verb|falling=false|. The default is
% \verb|true| for both variables, meaning, e.g., that \verb|falling| is
% the same as the first variant. The difference between the commands
% is that the first uses decreasing exponents from left to right instead
% of increasing, while the second uses increasing exponents from right
% to left (hence giving the reciprocal polynomial). If both are true the
% polynomial will be written with decreasing exponents from right to
% left. 
% \par\strut\par
% \noindent
% \textbf{Examples:}
% \newline\strut
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial[falling]{a,b,c,-d,e}| 
%   & $\polynomial[falling]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[reciprocal]{a,b,c,-d,e}| 
%   & $\polynomial[reciprocal]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[reciprocal,falling]{a,b,c,-d,e}| 
%   & $\polynomial[reciprocal,falling]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomialfrac[falling]{a,b,c,-d,e}{f,g,h,i}| 
%   & $\displaystyle\polynomialfrac[falling]{a,b,c,-d,e}{f,g,h,i}$\\[\tabsepadd]
%   \verb|\polynomialfrac[reciprocal]{a,b,c,-d,e}{f,g,h,i}| 
%   & $\displaystyle\polynomialfrac[reciprocal]{a,b,c,-d,e}{f,g,h,i}$
% \end{tabular}
% \par\strut\par
% It is also possible to change the polynomial variable through the
% optional argument \verb|var=<nbr>|. The starting value of the
% exponents can be changed through \verb|start=<nbr>|. There is also a
% variable \verb|step=<nbr>| that changes the incrementation steps of the
% exponents. 
% \par\strut\par
% \noindent
% \textbf{Examples:}
% \newline\strut
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial[var=t]{a,b,c,-d,e}| 
%   & $\polynomial[var=t]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[var=\Phi,start=2]{a,b,c,-d,e}| 
%   & $\polynomial[var=\Phi,start=2]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[var=z,falling]{a,b,c,-d,e}| 
%   & $\polynomial[var=z,falling]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomialfrac[var=\varphi,reciprocal,start=-3]|\newline
%   \verb|  {a,b,c,-d,e}{f,g,h,i}| 
%   & $\displaystyle\polynomialfrac[var=\varphi,reciprocal,start=-3]
%   {a,b,c,-d,e}{f,g,h,i}$\\[\tabsepadd]
%   \verb|\polynomial[step=3,var=\pi]{a,b,c,-d,e}| 
%   & $\polynomial[step=3,var=\pi]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[step=2,reciprocal,falling,start=3]|\newline
%   \verb|  {a,b,c,-d,e}| 
%   & $\polynomial[step=2,reciprocal,falling,start=3]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[step=-3,start=2]{a,b,c,-d,e}| 
%   & $\polynomial[step=-3,start=2]{a,b,c,-d,e}$\\[\tabsepadd]
% \end{tabular}
% \par\strut\par
% Sometimes, it is desirable to use other addition and subtraction
% symbols than the default. This is done by the variables \verb|add| and
% \verb|sub|. If the first coefficient is negative this will also have
% the subtraction sign specified in \verb|sub|. In some cases the
% additive inverse is denoted by a normal minus, while the 
% subtraction (if defined) something else, e.g., $\ominus$. For this
% purpose there is a third variable \verb|firstsub|.
% \par\strut\par
% \noindent
% \textbf{Examples:}
% \newline\strut
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial[add=\oplus,sub=\ominus]|\newline
%   \verb|  {a,-b,c,-d,e}| 
%   & $\polynomial[add=\oplus,sub=\ominus]{a,-b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[add=\oplus,sub=\ominus]|\newline
%   \verb|  {-a,b,-c,d,-e}| 
%   & $\polynomial[add=\oplus,sub=\ominus]{-a,b,-c,d,-e}$\\[\tabsepadd]
%   \verb|\polynomial[add=\oplus,sub=\ominus,firstsub=-]|\newline
%   \verb|  {-a,b,-c,d,-e}| 
%   & $\polynomial[add=\oplus,sub=\ominus,firstsub=-]{-a,b,-c,d,-e}$
% \end{tabular}
% \par\strut\par
% All of the above variables can be set either for individual
% commands, as shown, or for the rest of the document with the command
% \verb|\polynomialstyle|. In this case there is also an option that
% resets all values to the starting values, called \verb|default|. 
% \par\strut\par
% \noindent
% \textbf{Examples:}
% \newline\strut
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial{a,b,c,-d,e}| 
%   & $\polynomial{a,b,c,-d,e}$
% \end{tabular}\vspace*{\tabsepadd}\newline
%   \verb|\polynomialstyle{var=z,falling}| 
%   \polynomialstyle{var=z,falling}\vspace*{\tabsepadd}\newline
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial{a,b,c,-d,e}| 
%   & $\polynomial{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[reciprocal]{a,b,c,-d,e}| 
%   & $\polynomial[reciprocal]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomial[start=3,falling=false]{a,b,c,-d,e}| 
%   & $\polynomial[start=3,falling=false]{a,b,c,-d,e}$\\[\tabsepadd]
%   \verb|\polynomialfrac{a,b,c,-d,e}{f,g,h,i}| 
%   & $\displaystyle\polynomialfrac{a,b,c,-d,e}{f,g,h,i}$
% \end{tabular}\vspace*{\tabsepadd}\newline
% \verb|\polynomialstyle{add=\oplus,sub=\ominus}| 
% \polynomialstyle{add=\oplus,sub=\ominus}\vspace*{\tabsepadd}\newline
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial{a,b,c,-d,e}| 
%   & $\polynomial{a,b,c,-d,e}$
% \end{tabular}\vspace*{\tabsepadd}\newline
% \verb|\polynomialstyle{default}| 
% \polynomialstyle{default}\vspace*{\tabsepadd}\newline
% \begin{tabular}{@{}p{0.6\linewidth}p{0.4\linewidth}}
%   \verb|\polynomial{a,b,c,-d,e}| 
%   & $\polynomial{a,b,c,-d,e}$
% \end{tabular}

% \StopEventually{}
%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% polynomial.sty
%%
%% v1.1
%% 2007-03-17
%%
%% Stefan Höst
%% (stefan.host@it.lth.se)
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Problems:
%% * Very long numbers in coefficient result in overflow.
%%
%% Fixes
%% 2007-03-17: Removed allocation of counter for each call of \polynomial.
%% 2007-03-17: Replaced some other counters with \def.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Counters
\newcount\shpol@numcoeff% Number of coeffs parsed
\newcount\shpol@coeffnum% loop var for coeffs
\newcount\shpol@exponent% loop var for exponents (not same as coeffnum)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ifs
\newif\if@shpol@firstterm% If first term no '+'
\newif\if@shpol@falling% If exponents falling
\newif\if@shpol@reciprocal% If reciprocal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% variables
\def\shpol@var{x}% keyval: poly var
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% keyval
\RequirePackage{keyval}
%% in function
\define@key{shpol}{start}[0]{\def\shpol@start{#1}}%{\shpol@start=#1}
\define@key{shpol}{var}[x]{\def\shpol@tmpvar{#1}}
\define@key{shpol}{step}[1]{\def\shpol@expstep{#1}}%{\shpol@expstep=#1}
\define@key{shpol}{falling}[true]{\csname @shpol@falling#1\endcsname}
\define@key{shpol}{reciprocal}[true]{\csname @shpol@reciprocal#1\endcsname}
\define@key{shpol}{add}[+]{\def\shpol@add{#1}}
\define@key{shpol}{sub}[-]{\def\shpol@sub{#1}\def\shpol@firstsub{#1}}
\define@key{shpol}{firstsub}[-]{\def\shpol@firstsub{#1}}
%% default values
\define@key{shpoldefault}{start}[0]{\def\shpol@start{#1}}%{\shpol@start=#1}
\define@key{shpoldefault}{var}[x]{\def\shpol@var{#1}}
\define@key{shpoldefault}{step}[1]{\def\shpol@expstep{#1}}%{\shpol@expstep=#1}
\define@key{shpoldefault}{falling}[true]{\csname @shpol@falling#1\endcsname}
\define@key{shpoldefault}{reciprocal}[true]{\csname @shpol@reciprocal#1\endcsname}
\define@key{shpoldefault}{add}[+]{\def\shpol@add@default{#1}}
\define@key{shpoldefault}{sub}[-]{%
  \def\shpol@sub@default{#1}\def\shpol@firstsub@default{#1}}
\define@key{shpoldefault}{firstsub}[-]{\def\shpol@firstsub@default{#1}}
%%
\define@key{shpoldefault}{default}[true]{%
  \setkeys{shpoldefault}{start,var,step,falling=false,reciprocal=false,add,sub,firstsub}}
\setkeys{shpoldefault}{default}
\def\polynomialstyle#1{\setkeys{shpoldefault}{#1}}
%%%%%%%%%%%%%%%%%%%%%%%%
%% help defs
\def\shpol@splitcoeff#1{\shpol@@splitcoeff#1\@nil}
\def\shpol@@splitcoeff#1#2\@nil{%
  \def\shpol@firstofcoeff{#1}%
  \def\shpol@restofcoeff{#2}
}
\def\shpol@minus{-}
%% If #1 is a number that is =1 then #2 else #3
%% see www.tex.ac.uk/cgi-bin/texfaq2html?label=isitanum
\def\if@@one#1#2#3{%
  \ifcat_\ifnum1=0#1 _\else A\fi #2\else #3\fi}
%% If #1 a number that is =0 then #2 else #3
\def\if@@zero#1#2#3{%
  \ifcat_\ifnum0=0#1 _\else A\fi #2\else #3\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% set one term in polynomial
\def\shpol@setterm[#1]#2#3{% [variable]{koefficient}{exponent}
  \def\@shpol@koeff{#2} %% To make it more clear
  \ifnum#3=0 %% x^0
    \@shpol@koeff
  \else
    \if@@one{#2}{}{\@shpol@koeff}
    #1
    \ifnum#3=1\else
      ^{#3}
    \fi
  \fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\shpol@getcoeff#1{% Pars the coeffs and store in #1-vars
  \shpol@numcoeff=0%
  \@for\shpol@coeff:=#1\do{%
    \advance\shpol@numcoeff by 1\relax%
    \expandafter\let\csname shpol@coeff\romannumeral\shpol@numcoeff\endcsname\shpol@coeff%
  }%
}
\def\shpol@writepoly{% Write the #1-vars as polynomial
  \shpol@coeffnum=1
  \shpol@exponent=0
  \if@shpol@reciprocal
    \if@shpol@falling
      \advance\shpol@exponent by -\shpol@numcoeff
      \advance\shpol@exponent by 1
    \else
      \advance\shpol@exponent by \shpol@numcoeff
      \advance\shpol@exponent by -1
    \fi
    \multiply\shpol@exponent by \shpol@expstep
  \fi
  \advance\shpol@exponent by \shpol@start
  \loop%
    \expandafter\let\expandafter\shpol@coeff%
      \csname shpol@coeff\romannumeral\shpol@coeffnum\endcsname
    \if@@zero{\shpol@coeff}{}{% coeff not zero
      %% Check if first char is '-'. Then remove it and replace + with -.
      \expandafter\shpol@splitcoeff\expandafter{\shpol@coeff}
      \ifx\shpol@firstofcoeff\shpol@minus
        \if@shpol@firstterm\shpol@firstsub\else\shpol@sub\fi
        \let\shpol@coeff\shpol@restofcoeff
      \else
        \if@shpol@firstterm\else\shpol@add\fi
      \fi
      %%\fi
      \@shpol@firsttermfalse
      \shpol@setterm[\shpol@tmpvar]%
      {\shpol@coeff}%
      {\the\shpol@exponent}%
    }
  \ifnum\shpol@coeffnum<\shpol@numcoeff
    \advance\shpol@coeffnum by 1\relax%
    \advance\shpol@exponent by
      \if@shpol@falling
        \if@shpol@reciprocal \shpol@expstep \else -\shpol@expstep \fi
      \else
        \if@shpol@reciprocal -\shpol@expstep \else \shpol@expstep \fi
      \fi\relax%
  \repeat
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\shpol@defaultvalues{% Set default values for keyval
  \let\shpol@tmpvar\shpol@var
  \let\shpol@add\shpol@add@default
  \let\shpol@sub\shpol@sub@default
  \let\shpol@firstsub\shpol@firstsub@default
  \@shpol@firsttermtrue
}
\def\polynomial{%
  \shpol@defaultvalues
  \@ifnextchar[%]
  {\opt@shpol@polynomial}{\shpol@polynomial}}
\def\opt@shpol@polynomial[#1]{%
  \setkeys{shpol}{#1}
  \shpol@polynomial}
\def\shpol@polynomial#1{%
  \shpol@getcoeff{#1}
  \shpol@writepoly
}
\def\polynomialfrac{%
  \@ifnextchar[%]
  {\opt@shpol@rational}{\@shpol@rational}}
\def\@shpol@rational#1#2{%
  \frac{\polynomial{#1}}{\polynomial{#2}}}
\def\opt@shpol@rational[#1]#2#3{%
  \frac{\polynomial[#1]{#2}}{\polynomial[#1]{#3}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
% \Finale