% \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