% polynomial part 12.2.91 hk
% distributive system

%fixed notations
\def\Z{\mbox{\bf Z}} % math objects
\def\N{\mbox{\bf N}}
\def\Q{\mbox{\bf Q}}
\def\HT{\mbox{\rm HT}}
\def\I{{\cal I}} % computer objects
\def\R{{\cal R}}
\def\A{{\cal A}}
\def\L{{\cal L}}
\def\O{{\cal O}}
\def\tmi{t^{-}} %time and space
\def\tma{t^{+}}
\def\ta{t^{*}}
\def\cmi{c^{-}}
\def\cma{c^{+}}
\def\ca{c^{*}}

%\setcounter{chapter}{6}


\section{Distributive Polynomial System} %----------------------

Again let $R$ be a commutative ring with 1 and let 
$S = R \lbrack X_1, \ldots, X_r \rbrack$ denote a
(commutative) polynomial ring in $r \geq 0$ variables
(indeterminates) $X_1, \ldots, X_r$.
The elements of $S$ are sums of {\bf monomials}, 
where each monomial is a product of a {\bf base coefficient} 
and a {\bf term} (power product).

{\bf Definition:}
Let $A(X_1, \ldots, X_r) \in S$, $A \neq 0$ and $r \geq 1$,
then 
$$ A(X_1, \ldots, X_r) = 
  \sum_{i=1}^{k} a_i X_1^{e_{i1}} \cdot \ldots \cdot X_r^{e_{ir}}
  = \sum_{i=1}^{k} a_i X^{e_i}
$$ 
with $a_i \neq 0$ for $i=1,\ldots,k$ and 
natural numbers $e_{ij}$ for $i=1,\ldots,k$ and $j=1,\ldots,r$. 
$X^{e_i}$ is an abreviation for 
$X_1^{e_{i1}} \cdot \ldots \cdot X_r^{e_{ir}}$.
$k$ is the number of terms of $A$.
For $r > 0$ the representation of an {\bf exponent vector} 
$e_i = ( e_{i1}, \ldots, e_{i,r-1}, e_{ir} )$ is the list 
$$
  \epsilon_i = ( e_{ir}, \ldots, e_{i2}, e_{i1} ).
$$
For $r = 0$ let $\epsilon = ()$, the empty list. 
The {\bf distributive representation} of $A$ is the list
$$
  \alpha = ( \epsilon_k, \alpha_k, \ldots,
       \epsilon_2, \alpha_2, \epsilon_1, \alpha_1 ) 
$$
where the $\alpha_i$ denote the
representations of the $a_i$ and 
the $\epsilon_i$ are the representation of the exponent vectors, 
$i=1,\ldots,k$.
If $A = 0$ then $\alpha = 0$ and 
if $r = 0$ then $\alpha = ( (), \alpha_1 )$.

{\bf Notes:}
\begin{enumerate}
\item The variables $X_1, \ldots, X_r$ are not stored 
      in the representing list. This is different to 
      other computer algebra systems like REDUCE or muMATH.
\item The representation is sparse in the sence, that 
      only base coefficients $\neq 0$ are stored.
\item The representation of the exponent vectors is dense 
      in the sence, that also exponents $= 0$ are stored.
\item The number of variables $r$ can be determined from the 
      length of an exponent vector (representation).
\end{enumerate}

{\bf Examples:}
\begin{enumerate}
\item Let $S = \Z \lbrack X \rbrack$, that is $R = \Z$ and $r = 1$.
      Let $$A = 3 X^4 + 5,$$
      then $k = 2$ and $e_2 = (4), a_1 = 3, e_1 = (0), a_2 = 5$. 
      The representation is then 
      $$ \alpha = ( ( 4 ), 3, ( 0 ), 5 ). $$
\item Let $S = \Z \lbrack X, Y \rbrack$, 
      that is $R = \Z$ and $r = 2$.
      Let $$A = ( 3 X + 2 ) Y^2 + 5 X = 3 X Y^2 + 2 Y^2 + 5 X,$$
      then $k = 3$ and $e_3 = ( 1, 2), a_3 = 3, 
           e_2 = ( 0, 2 ), a_2 = 2, e_1 = ( 1, 0 ), a_1 = 5$. 
      The representation is then 
      $$ \alpha = ( ( 2, 1 ), 3, ( 2, 0 ), 2, ( 0, 1 ), 5 ). $$
\item Let $S = \Q \lbrack X, Y \rbrack$, 
      that is $R = \Q$ and $r = 2$.
      Let $$A = \frac{1}{4} X^2 Y - \frac{3}{5},$$
      then $k = 2$ and $e_2 = ( 2, 1 ), a_2 = \frac{1}{4}, 
           e_1 = ( 0, 0 ), a_1 = \frac{-3}{5}$. 
      The representation is then 
      $$ \alpha = ( ( 1, 2 ), ( 1, 4 ), ( 0, 0 ), ( -3, 5 ) ). $$
\item Let $S = \Z \lbrack X_1, X_2, X_3, X_4, X_5 \rbrack$, 
      that is $R = \Z$ and $r = 5$.
      Let $$A = 5 X_2 X_3 + 7 X_1^2,$$
      then $k = 2$ and $e_2 = ( 0, 1, 1, 0, 0 ), a_2 = 5, 
                        e_1 = ( 2, 0, 0, 0, 0 ), a_1 = 7$. 
      The representation is then 
      $$ \alpha = ( ( 0, 0, 1, 1, 0 ), 5, 
                    ( 0, 0, 0, 0, 2 ), 7 ). $$
\end{enumerate}

Up to now we have assumed that there is 
a unique way to determine the ordering and 
the sequence of the terms in a polynomial. 
In general there are many (contiuum many) 
total orderings of the set of terms, which are in addition 
compatible with the multiplication of terms.
Such term orders are called admissible.
We will now turn to the explicit definition of 
some important term orders and then 
give a general characterization by linear forms.


\subsection{Term Orders} % ---------------------

Recall that $T$ denotes the set of terms 
$u = X_1^{e_1} \cdot \ldots \cdot X_r^{e_r} = X^e$
where the $e_i, (i=1,\ldots,r) $ are nonnegative integers.

The degree of a term 
$ u = X_1^{e_1} \cdot \ldots \cdot X_r^{e_r} $
is defined as
$ deg(u) = \sum_{i=1}^{r} e_i.$
For exponent vectors $e$ and $l$ define 
$ e - l = (e_1 - l_1, \ldots, e_r - l_r) $
(an vector of integers).
The degree of an exponent vector
is defined as
$ \deg (e) = \deg( X^{e} ). $

For an arbitrary total ordering $ >_T $ on power products we define
$         u >_T v    \Longleftrightarrow
    X^{e} >_T X^{l}  \Longleftrightarrow
        e >_T l      \Longleftrightarrow
    e - l >_T 0 = (0,\ldots,0) $.
We write $ u <_T v $ if $ v >_T u $.
The term orderings are called {\bf admissible}, if they
are monoton with respect to multiplication of power products
and if $1 = X^0$ is the smallest element.
That is 
\begin{enumerate}
\item For $u \in T$, $u \neq 1$ it holds $1 <_T u$.
\item For $u, v, t \in T$ with $u <_T v$ it holds $u t <_T v t$.
\end{enumerate}
In the sequel $ > $ denotes the natural ordering on the integers.
The currently implemented term orders can be checked 
with the procedure \verb/EVOWRITE/. 

\subsubsection{Buchberger's term order}

In 1965 Buchberger gave the definiton
of the term ordering used by him for Gr\"obner bases calculation
\index{Buchbergers graduated term order}
\cite{Buchb65,Buchb70}.
It can be defined as:
\begin{displaymath}
 e >_B  0  \Longleftrightarrow
 deg(e) > 0 \ \mbox{ or } \
   \left\{
   \begin{array}{l}
   deg(e) = 0  \\
   \mbox{ and } \\
   e >_{BL} 0
   \end{array}
   \right.
\end{displaymath}
Where $>_{BL}$ is defined in the following way:
\begin{displaymath}
 e >_{BL} 0 \Longleftrightarrow
   \left\{
   \begin{array}{l}
   \exists k \in \{1,\ldots,r\} \\
   {\rm with} \ e_j = 0 \ {\rm for} \ j=1,\ldots,k-1 \\
   {\rm and} \ e_k < 0
   \end{array}
   \right.
\end{displaymath}
Note that $>_{BL}$ alone is not an admissible term order. 
This ordering was implemented by Winkler in the
SAC-1 computer algebra system \cite{Winkler85}.


\subsubsection{DIP term order}

If the ordering on the power products
in distributed representation
is choosen to be compatible with the ordering
induced by recursive representation one arrives 
at the so called 'inverse lexicographical' term order 
\index{inverse lexicographical term order}
\cite{GK83}.
\begin{displaymath}
 e >_L 0 \Longleftrightarrow
   \left\{
   \begin{array}{l}
   \exists k \in \{1,\ldots,r\} \\
   {\rm with} \ e_j = 0 \ {\rm for} \ j=r,r-1,\ldots,k+1 \\
   {\rm and} \ e_k > 0
   \end{array}
   \right.
\end{displaymath}
From this we derive the `inverse graduated' ordering:
\index{inverse graduated term order}
\begin{displaymath}
 e >_G 0 \Longleftrightarrow
 deg(e) > 0 \ \mbox{ or } \
   \left\{
   \begin{array}{l}
     deg(e) = 0 \\
     \mbox{ and } \\
     e >_L 0
   \end{array}
   \right.
\end{displaymath}
Which is the same as:
\begin{displaymath}
 e >_G 0
 \Longleftrightarrow
 (e,deg(e)) >_L (0,deg(0))
\end{displaymath}
The examples in \cite{BGK86}
are based on these orderings.
And they are also used in REDUCE 3.3 Gr\"obner basis package,
and by Trinks, \cite{Hearn87,Trinks78}.

\subsubsection{Scratchpad II term order}

The `newdistributed polynomial' representation in
the Scratchpad II System uses the
following `lexicographical' termordering:
\index{Scratchpad II term order}
\begin{displaymath}
 e >_S 0
 \Longleftrightarrow
   \left\{
   \begin{array}{l}
     \exists k \in \{1,\ldots,r\} \ {\rm with} \\
     e_j = 0 \ {\rm for} \ j=1,\ldots,k-1 \\
     {\rm and} \ e_k > 0
   \end{array}
   \right.
\end{displaymath}
\cite{Jenks84}.
This termordering is also used by Robbiano \cite{Rob85}.
With a `graduation' it was also implemented by Schrader 
\cite{Sch76} in ALGOL 60.
\begin{displaymath}
 e >_{SG} 0
 \Longleftrightarrow
 deg(e) > 0 \ \mbox{ or }
   \left\{
   \begin{array}{l}
     deg(e) = 0 \\
     \mbox{ and } \\
     e >_S 0
   \end{array}
   \right.
\end{displaymath}
Which is the same as:
\begin{displaymath}
 e >_{SG} 0
 \Longleftrightarrow
 (deg(e),e) >_S (deg(0),0)
\end{displaymath}


\subsubsection{Example}

We include an example from Jenks 
\cite{Jenks84}, to illustrate the different
Gr\"obner bases with respect to the different
term orderings.
The computation was done using the SAC--2 computer algebra system
on an IBM 3090--200 VF.
Let $R = \Q(A,B)$ and let $S = R\lbrack X, Y, Z, T \rbrack$.
Consider the following set of polynomials
\begin{verbatim}
 ( ( (A+1)/B X**2 Y - 1 ),
   ( (-B-1)/A X**2 Z + Y**3 ),
   ( - 12 X**2 T + Z**3 ) ).
\end{verbatim}

The Gr\"obner bases with respect to different term orderings are:

\begin{description}
\item inverse lexicographical term order
{\footnotesize
\begin{verbatim}
      (  X**2 Y -  B /(  A +1 ) )
      (  Z -(  A**2 + A )/(  B**2 + B ) Y**4 )
      (  T -(  A**7 +4 A**6 +6 A**5 +4 A**4 + A**3 )/
            ( 12 B**7 +36 B**6 +36 B**5 +12 B**4 ) Y**13 )
      executed in 750 milliseconds, 16058 cells used.
\end{verbatim}
}
\item inverse graduated term order 
{\footnotesize
\begin{verbatim}
      (  X**2 Y -  B /(  A +1 ) )
      (  X**2 Z -  A /(  B +1 ) Y**3 )
      (  X**2 T - 1 / 12  Z**3 )
      (  Y**4 -(  B**2 + B )/(  A**2 + A ) Z )
      (  Y Z**3 - 12 B /(  A +1 ) T )
      (  Y**3 T -(  B +1 )/ 12 A  Z**4 )
      (  Z**7 - 144 A B /(  A B + B + A +1 ) Y**2 T**2 )
      executed in 960 milliseconds, 17154 cells used.
\end{verbatim}
}
\item Buchberger total degree term order 
{\footnotesize
\begin{verbatim}
      (  X**2 Y -  B /(  A +1 ) )
      (  Y**3 -(  B +1 )/  A  X**2 Z )
      (  Z**3 - 12  X**2 T )
      (  X**4 Z -  A B /(  A B + B + A +1 ) Y**2 )
      (  X**6 T -  A B /( 12 A B +12 B +12 A +12 ) Y**2 Z**2 )
      executed in 530 milliseconds, 7965 cells used.
\end{verbatim}
}
\end{description}

Note also that the computing times vary for different 
term orders. 
A rule of thumb how to obtain `optimal' term orders will 
be discussed in a seperate section.


\subsection{Description of term orders by linear forms} %---------

The explicit characterizations of admissible 
term orders in the previous section are easy to 
implement and run fast. 
However there exist much more admissible 
orders on the set of terms.
In this section a uniform way to describe 
{\em all} admissible term orders will be discussed.
Although the implementation is much more involved, 
the running time is still acceptable.
The increase of computing time is 
`only' 50 $\%$ in typical examples. 

Observe that the admissible linear orders $<_{\N}$ on 
exponent vectors $e \in \N^n$ can be extended 
to admissible linear orders $<_{\Q}$ on $\Q^n$ 
such that $<_{\N} = <_{\Q} \cap \N^n$.

Let $S = {\bf R}\lbrack t \rbrack$ be the 
polynomial ring in one variable $t$ over the 
real numbers ${\bf R}$. 
Define a linear order on $S$ by 
$$
  f > 0 \Longleftrightarrow {\rm ldcf}(f) >_{{\bf R}} 0 \ \ 
        \  \mbox{\rm and}  \ \ \
  f > g \Longleftrightarrow f - g >_{{\bf R}} 0,
$$
for $f, g \in S$.
For this order $t \in S$ is larger than all elements of $\bf R$, 
that is $t > a$ for all $a \in \bf R$.

Let ${\cal AL} = \{ a = ( a_1, \ldots, a_n ) \in S^n \}$ such 
that the $a_i$, $i=1,\ldots,n$ are strictly positive and 
linear independent over the rational 
numbers $\Q$ ($\subset \bf R$). 

Then it has been shown by Robbiano and 
Weispfenning that
any $a \in {\cal AL}$ induces an admissible linear order 
$<$ on $\Q^n$ and any addmissible linear order on $\Q^n$ 
is induced by such an $a \in {\cal AL}$ in the following way
$$
   x < y \Longleftrightarrow a \cdot x < a \cdot y, 
$$
where $x = ( x_1, \ldots, x_n )$, 
$y = ( y_1, \ldots, y_n ) \in \Q^n$ and 
$$
  a \cdot x = \sum_{i=1}^{n} a_i x_i \ \ \  \in S.
$$
\cite{Rob85,Wei87}

For the term orders $<_T$ defined in the previous section 
the correspending $a_T \in {\cal AL}$ are as follows:

\begin{enumerate}
\item Buchberger's graduated (total degree) lexicographical 
      term order
      $<_B$:
      $$a_B = ( t^n - t^{n-1}, t^n - t^{n-2}, \ldots, 
             t^n - t^2, t^n - t, t^n - 1 ).$$ 
\item Inverse lexicographical term order
      $<_L$:
      $$a_L = ( 1, t, t^2, \ldots, t^{n-2}, t^{n-1} ).$$ 
\item Inverse graduated (total degree) term order
      $<_G$:
      $$a_G = ( 1 + t^n, t + t^n, t^2 + t^n, \ldots, 
                t^{n-2} + t^n, t^{n-1} + t^n ).$$ 
\item Scratchpad II lexicographical term order
      $<_S$:
      $$a_S = ( t^{n-1}, t^{n-2}, \ldots, t^2, t, 1 ).$$
\item Scratchpad II graduated (total degree) lexicographical 
      term order
      $<_{SG}$:
      $$a_{SG} = ( t^{n-1} + t^n, t^{n-2} + t^n, \ldots, 
             t^2 + t^n, t + t^n, 1 + t^n ).$$
\item Splitted inverse lexicographical term orders 
      $<_{T} = ( <_{T_1}, <_{T_2} )$ on $T = T_1 * T_2$, 
      defined as 
      $u_1 u_2 \leq_{T} v_1 v_2$ $\Longleftrightarrow$   
      $u_2 <_{T_2} v_2$ or 
      ( $u_2 = v_2$ and $u_1 \leq_{T_1} v_1$), 
      where $u_1, v_1 \in T_1$ and $u_2, v_2 \in T_2$.  
      If $a_{T_1} = ( a_1, \ldots, a_i )$ and 
      $a_{T_2} = ( a_{i+1}, \ldots, a_n )$ 
      for some $1 \leq i \leq n$, then 
      $$a_{T} = ( a_1, \ldots, a_i, 
                   a_{i+1} * t^{n'}, \ldots, a_n * t^{n'} ),$$
      where $n' > \deg(a_j)$, $j=1, \ldots, i$.
\item Varying term orders for 
      $<_{T}$ with $a_{T} = ( a_1, \ldots, a_n )$ and
      where $n' > \deg(a_j)$, $j=1, \ldots, n$:
      for $i = 0, \ldots, n'$ let 
      $$a_{T_i} = ( a_1 + b_i t^{i}, \ldots, a_n + b_i t^{i} ),$$
      where $b_i \in {\bf R}$ such that $a_{T_i} \in {\cal AL}$.
      Then we have $a_{T_0} = a_T$ (for $b_0 = 0$) and
      $a_{T_{n'}}$ a total degree term order (for $b_{n'} = 1$).
\end{enumerate}

The currently implemented term orders can be checked 
with the procedure \verb/EVOWRITE/. 
How these term orders can be specified in MAS
is discussed in the next section.



\subsection{Algorithms} %----------------------

\label{dip.alg}
The programs of the most important distributive polynomial algorithms 
and their complexity are summarized in the following.
The main complexity numbers are as defined in 
the previous section.
Only rational polynomial programs are discussed.

As the definition of the polynomial ring 
and the distributive representation suggests 
the algorithms will be constructed in the following way:
\begin{enumerate}
\item Loop on the monomials in the polynomials:
      \begin{enumerate}
      \item perform operations on the base coefficients,
      \item perform operations on exponent vectors, 
      \end{enumerate}
      Construct resulting polynomials.
\item Return the results. 
\end{enumerate}

The complexity of polynomial algorithms will 
therefore depend mainly on three factors:
\begin{enumerate}
\item the size of the base coefficients,
\item the number of terms,
\item the number of variables. 
\end{enumerate}
These quantities are as defined in the section on the
recursive representation algorithms.
The number of terms is now easily determined as 
half of the length of the polynomial in distributive 
representation.

We will continue to write $L(a)$ for $O(L(a))$, 
that means we will not count for constant factors.
The computing time functions $t, \tma, \tmi, \ta$ 
are defined as before in section \ref{secCOMPL}.
\index{complexity}

\def\DIR{{\cal DR}}
\def\DI{{\cal D}}
\def\B{{\cal B}}
Let
$\A$ be the set of atoms,
$\L$ be the set of lists,
$\O = \A \cup \L$ be the set of objects,
$\R = \{ x \in \O: \ x$ represents an element of ${\rm \bf Q} \}$ 
be the set of rational numbers, 
$\B = \{ x \in \O: \ x$ represents an element of a 
         base coefficient ring $\}$ 
be the set of base coefficients, 
$\DI_r = \{ x \in \O: \ x$ represents a multivariate polynomial 
         in $r$ variables $\}$ 
be the set of distributive polynomials,
$\DIR_r = \{ x \in \O: \ x$ represents a multivariate polynomial over
           ${\rm \bf Q}$ in $r$ variables $\}$ 
be the set of distributive rational polynomials. 
\index{rational polynomial}\index{rational number}
\index{polynomial}\index{distributive polynomial}

We will first summarize selector %----- and constructor 
functions which are independent of the base coefficient ring
and then turn to the algorithms for rational polynomials.

\begin{deflist}{$C \gets DIRPPR(A,B)$}
\item[$e \gets DIPDEG(A)$] $A \in \DI_r$, $e \in \A$. 
     $e = \deg_r (A) = e_{kr}$ is the degree of $A$ in the
     main variable. 
     In distributive representation $t = 2$, $c = 0$. 
     \index{DIPDEG}
\item[$e \gets DIPEVL(A)$] $A \in \DI_r$, $e \in \A^r$. 
     $e$ is the exponent vector of the highest term. 
     In distributive representation $t = 1$, $c = 0$. 
     \index{DIPEVL}
\item[$a \gets DIPLBC(A)$] $A \in \DI_r$, $a \in \B$. 
     $a = a_k$ is the leading base coefficient.
     $t = 2$, $c = 0$. 
     \index{DIPLBC}
\item[$DIPMAD(A;a,e,A')$] $A, A' \in \DI_r$, $e \in \A$, 
     $a \in \B$. Monomial advance.
     $e = e_k$ is the exponent vector of the highest term. 
     $a = a_k$ is the leading base coefficient.
     $t = 2$, $c = 0$. 
     \index{DIPMAD}
\item[$A' \gets DIPMCP(a,e,A)$] $A, A' \in \DI_r$, $e \in \A$, 
     $a \in \B$. Monomial composition.
     $e$ becomes the exponent vector of the highest term of $A'$. 
     $a$ becomes the leading base coefficient of $A'$.
     $t = 2$, $c = 2$. 
     \index{DIPMCP}
\item[$A \gets DIPFMO(a,e)$] $A \in \DI_r$, $e \in \A$, 
     $a \in \B$. Polynomial from monomial. 
     $e$ becomes the exponent vector of the highest term of $A$. 
     $a$ becomes the leading base coefficient of $A$.
     $t = 2$, $c = 2$. 
     \index{DIPFMO}
\item[$DIPBSO(A)$] $A \in \DI_r$. 
     Polynomial (bubble) sort. 
     The terms (which must be distinct)
     in $A$ are sorted by the actual term order, 
     the representing list is modified.
     There are other sorting algorithms which also allow 
     for equal terms in $A$, but then coefficient arithmetic 
     is required. 
     \index{DIPBSO}
\end{deflist}

We turn now to the discussion of
polynomial algorithms which depend on the 
base coefficient field. 
Only rational numbers as base coefficients will be treated. 

\begin{deflist}{$C \gets DIRPPR(A,B)$}
\item[$s \gets DIRPSG(A)$] $A \in \DIR_r$, $s \in \{ -1, 0, +1\}$. 
     $s$ is the sign of the leading base coefficient 
     of $A$.
     In our representation 
     $t^+ = 2 + t^+_{RNSIGN} = 2 + L(A)$, 
     $t^- = 2 + t^-_{RNSIGN} = 2 + 2 = 4$ , $c = 0$.
     \index{DIRPSG}
\item[$A' \gets DIRPNG(A)$] $A, A' \in \DIR_r$. 
     $A' = - A$ is the negative of $A$.
     $t^+ = {\rm term}(A) \cdot t^+_{RNNEG} = k L(A)$, 
     $c^+ = {\rm term}(A) \cdot c^+_{RNNEG} = k L(A)$.
     (In distributive representation no operations on 
     exponent vectors are required.)
     \index{DIRPNG}
\item[$A' \gets DIRPAB(A)$] $A, A' \in \DIR_r$. 
     $A' = \vert A \vert$ is the absolute value of $A$, 
     that is ${\rm sign}(A') \geq 0$.
     $t^+ = t^+_{DIRPNG} = k L(A)$, 
     $c^+ = c^+_{DIRPNG} = k L(A)$;
     $t^- = 2 + t^-_{RNSIGN} = 2 + 1 = 3$, 
     $c^- = 0$.
     \index{DIRPAB}
\item[$C \gets DIRPSM(A,B)$] $A, B, C \in \DIR_r$. 
     $C = A + B$ is the sum of $A$ and $B$.
     Let $l = \max \{ {\rm term}(A), {\rm term}(B) \}$. 
     $t^+ = 2 l \cdot t^+_{RNSUM} + t^+_{expon} 
     = 2 l L(A,B)^2 + 2 l r$, 
     $c^+ = 2 l \cdot c^+_{RNSUM}  + c^+_{expon} 
     = 2 l L(A,B)^2 + 2 l$. 
     $t^+_{ecomp}$ denotes the time for comparing exponent 
     vectors for term merge.
     If the set of terms of $A$ and $B$ are disjoint 
     a pure merge is performed. In this case no operations 
     on base coefficients are required 
     and then $t^+ = 2 l r$, $c^+ = 2 l$.
     Although the exponent vectors are processed, they need not 
     be reconstructed, thus $c^+ = 2 l$ instead of $2 l r$.
     \index{DIRPSM}
\item[$C \gets DIRPDF(A,B)$] $A, B, C \in \DIR_r$. 
     $C = A - B$ is the difference of $A$ and $B$.
     The computing times are the same as for $DIRPSM$.
     \index{DIRPDF}
\item[$C \gets DIRPPR(A,B)$] $A, B, C \in \DIR_r$. 
     $C = A * B$ is the product of $A$ and $B$.
     Let $l = \max \{ {\rm term}(A), {\rm term}(B) \}$. 
     $t^+ = l^2 \cdot t^+_{RNPROD} + t^+_{expon} 
     = l^2 L(A,B)^2 + l^2 r$, 
     $c^+ = l^2 \cdot c^+_{RNPROD} + c^+_{expon} 
     = l^2 L(A,B)^2 + l^2 r$.
     The summations of intermediate polynomials 
     are aranged to need only $l \log_2(l)$ summations
     of terms.
     \index{DIRPPR}
\item[$B \gets DIRPNF(P,A)$] $A, B \in \DIR_r$, $P \in \L\DIR_r$. 
     $B = {\rm normal \ form}_P(A) $ is the normal form
     or completely reduced form of $A$ with respect to $P$.
     Let $l = {\rm term}(A)$. 
     If $A$ is irreducible then $t^- = l$, $c^- = l$.
     A one step reduction requires a monomial with 
     polynomial product and one polynomial difference,
     thus $t_1^+ = 2 l L(A,P)^2 + lr$, $c_1^+ = 2 l L(A,P)^2 +lr$.
     The maximal computing time 
     depends strongly on the used term order.
     As a worst case estimate
     consider $d = \max \{ \deg( \HT(p) ) \mid p \in P \}$, 
     $d' = \max \{ \deg( p ) \mid p \in P \}$
     and let $d'' = \deg(A)$. 
     Then $B$ will contain at most $d^r$ terms. 
     In case of a total degree term order
     probably $m = d''^r - d^r$ terms will need reduction.
     So $t^+ = m t_1^+ = m 2 l L(A,P) \leq d''^r 2 l L(A,P)$,
     $c^+ \leq d''^r 2 l L(A,P)$. 
     In case of a lexicographical termorder 
     after each reduction step $d''$ may increase so 
     termination can only be assured by Noetherian induction.
     \index{DIRPNF}
\item[$Q \gets DIRLIS(P)$] $P, Q \in \L\DIR_r$. 
     $Q$ is the irreducible set of $P$, that is every 
     $p \in Q$ is irreducible with respect to 
     $Q \setminus \{ p \}$.
     \index{DIRLIS}
\item[$G \gets DIRPGB(P,t)$] $P, G \in \L\DIR_r$, 
     $t \in \{ 0, 1, 2 \}$. 
     $G$ is the Gr\"obner base of $P$.
     $t$ is the `trace flag', $t = 0$ if no intermediate 
     output is required, $t = 1$ if the reduced S--polynomials 
     are to be listed and $t = 2$ for maximal information.
     \index{DIRPGB}
\end{deflist}

This concludes the summary of 
distributive rational polynomial arithmetic functions.
Finally we will summarize input / output functions and 
some often needed conversion functions. 

\begin{deflist}{$C \gets DIRPPR(A,B)$}
\item[$P \gets PREAD()$] $P \in \L\DIR_r$. 
     A list of distributive rational polynomials 
     together with the variable list and a term order are 
     read from the actual input stream.
     The accepted syntax (with start symbol \verb/bunch/) is:
     \label{poly.ebnf}
     \begin{verbatim}
bunch    = varlist termord polylist 
varlist  = "(" ident { "," ident } ")"
termord  = ( "L" | "G" | linform )
linform  = "(" univpoly { "," univpoly } ")"
polylist = "(" poly { "," poly } ")"
poly     = "(" term { ("+"|"-") term } ")"
term     = power { [ "*" ] power }
power    = factor [ "**" atom ]
factor   = ( rational | ident | "(" poly ")" )
     \end{verbatim}
     With the context conditions:
     \begin{enumerate}
     \item the \verb/atoms/ must be nonnegative,
     \item \verb/ident/s appearing in \verb/poly/ must 
           be declared in \verb/varlist/, 
     \item the right most variable in the variable list 
           denotes the main variable, 
     \item \verb/univpoly/ must be an univariate 
           \verb/poly/ in the variable \verb/"T"/ 
           (=\verb/ident/) over the rational numbers,
     \item \verb/linform/ must be a valid linear form 
           $\in {\cal AL}$, 
     \item the number of univariate polynomials must be equal 
           to the number of variables in the variable list.
     \end{enumerate}
     Examples will be discussed in the exercise section. 
     \index{PREAD}
\item[$PWRITE(P)$] $P \in \L\DIR_r$. 
     A list of distributive rational polynomials 
     together with the variable list and a term order are 
     written to the actual output stream.
     The output syntax of the polynomial list is equal 
     to the input syntax of $PREAD$.
     \index{PWRITE}
\item[$B \gets DIIFRP(A)$] $A \in \DIR_r$, $B \in \DI_r$. 
     $B = d \cdot A$ is converted to integral 
     distributive representation, 
     where $d$ is the least common multiple of all 
     denominators of base coefficients of $A$. 
     Let $l = {\rm term}(A)$. 
     $t^+ = l \cdot t^+_{ILCM} + l \cdot t^+_{RNPROD} 
          = l 2 L(A)^2 + l L(A)^2 = 3 l LA()^2$, 
     $c^+ = l \cdot c^+_{ILCM} + l \cdot c^+_{RNPROD} 
          = l 2 L(A)^2 + l L(A)^2 = 3 l LA()^2$.
     \index{DIIFRP}
\item[$B \gets DIRFIP(A)$] $A \in \DI_r$, $B \in \DIR_r$. 
     $B = \frac{1}{d} \cdot A$ is converted to rational 
     distributive representation, 
     where $d = {\rm lbcf}(A)$.
     Let $l = {\rm term}(A)$. 
     $t^+ = l \cdot t^+_{RNRED} = l L(A)^2$, 
     $c^+ = l \cdot c^+_{RNRED} = l L(A)^2$.
     \index{DIRFIP}
\item[$B \gets DIPFP(r,A)$] $A \in \Po_r$, $B \in \DI_r$. 
     $A$ in recursive representation is converted 
     to $B$ in distributive representation.
     Let $l = {\rm term}(A)$. 
     $t^+ = 2 l r$, $c^+ = 2 l r$ since no operations on 
     base coefficients are required.
     \index{DIPFP}
\item[$PFDIP(A;r,B)$] $A \in \DI_r$, $B \in \Po_r$. 
     $A$ in distributed representation is 
     converted to $B$ in recursive representation.
     Let $l = {\rm term}(A)$. 
     $t^+ = 2 l r$, $c^+ = 2 l r$ since no operations on 
     base coefficients are required.
     \index{PFDIP}
\end{deflist}

This concludes the summary of 
distributive polynomial functions.

For illustration we list 
the algorithms \verb/DIRPNF/ and \verb/DIRLIS/ in Modula--2
in MAS.
The function of the algorithms should be clear from the 
step comments and the polynomial representation 
discussed before.

{\footnotesize
\begin{verbatim}
  PROCEDURE DIRPNF(P,S: LIST): LIST; 
  (*Distributive rational polynomial normal form. P is a list
  of non zero polynomials in distributive rational
  representation in r variables. S is a distributive rational
  polynomial. R is a polynomial such that S is reducible to R
  modulo P and R is in normalform with respect to P. *)
  VAR  AP, APP, BL, FL, PP, Q, QA, QE, QP, R, SL, SP, TA, TE: LIST; 
  BEGIN
  (*1*) (*s=0. *) 
        IF (S = 0) OR (P = SIL) THEN R:=S; RETURN(R); END; 
  (*2*) (*reduction step.*) R:=SIL; SP:=S; 
        REPEAT DIPMAD(SP, TA,TE,SP); 
               IF SP = SIL THEN SP:=0; END; 
               PP:=P; 
               REPEAT ADV(PP, Q,PP); DIPMAD(Q, QA,QE,QP); 
                      SL:=EVMT(TE,QE); 
                      UNTIL (PP = SIL) OR (SL = 1); 
               IF SL = 0 THEN R:=DIPMCP(TE,TA,R); 
                  ELSE
                  IF QP <> SIL THEN FL:=EVDIF(TE,QE); 
                     BL:=RNQ(TA,QA); 
                     AP:=DIPFMO(BL,FL); APP:=DIRPPR(QP,AP); 
                     SP:=DIRPDF(SP,APP); END; 
                  END; 
               UNTIL SP = 0; 
  (*3*) (*finish.*) 
        IF R = SIL THEN R:=0; ELSE R:=INV(R); END; 
  (*6*) RETURN(R); END DIRPNF; 
\end{verbatim}
}

In the outer REPEAT--loop all terms of the 
polynomial \verb/S/ are considered for reduction. 
In the inner REPEAT--loop the head terms of polynomials 
in \verb/P/ are tested if they divide 
(\verb/EVMT/ exponent vector multiple test)
the actual term of \verb/S/. 
The IF--statement checks if a divisor was found, 
in which case the reduction is applied, or if 
the term was irreducible, in which case the term is copied 
to the result polynomial \verb/R/.


{
\footnotesize
\begin{verbatim}
  PROCEDURE DIRLIS(P: LIST): LIST; 
  (*Distributive rational polynomial list irreducible set.
  P is a list of distributive rational polynomials,
  PP is the result of reducing each p element of P modulo P-(p)
  until no further reductions are possible. *)
  VAR  EL, FL, IRR, LL, PL, PP, PS, RL, RP, SL: LIST; 
  BEGIN
  (*1*) (*initialise. *) PP:=P; PS:=SIL; 
        WHILE PP <> SIL DO ADV(PP, PL,PP); PL:=DIRPMC(PL); 
              IF PL <> 0 THEN PS:=COMP(PL,PS); END; 
              END; 
        RP:=PS; PP:=INV(PS); LL:=LENGTH(PP); IRR:=0; 
        IF LL <= 1 THEN RETURN(PP); END; 
  (*2*) (*reduce until all polynomials are irreducible. *) 
        LOOP ADV(PP, PL,PP); EL:=DIPEVL(PL); PL:=DIRPNF(PP,PL); 
             IF PL = 0 
                THEN LL:=LL-1; 
                     IF LL <= 1 THEN EXIT END; 
                ELSE FL:=DIPEVL(PL); SL:=EVSIGN(FL); 
                     IF SL = 0 THEN PP:=LIST1(PL); EXIT END; 
                     SL:=EQUAL(EL,FL); 
                     IF SL = 1 THEN IRR:=IRR+1; ELSE IRR:=0; 
                        PL:=DIRPMC(PL); END; 
                     PS:=LIST1(PL); SRED(RP,PS); RP:=PS; END; 
             IF IRR = LL THEN EXIT END;
             END; 
  (*3*) (*finish. *) RETURN(PP); 
  (*6*) END DIRLIS; 
\end{verbatim}
}

In the first step the polynomials in \verb/P/ are made 
monic (\verb/DIRPMC/), that is their leading base coefficient
is $1$. 
In the main loop in step 2 all polynomials 
are reduced with respect to the rest of the polynomials. 
Then several case distinctions are made to 
check if the polynomial reduced to $0$ or if its 
head term \verb/EL/, \verb/FL/ was reduced or not. 
The loop terminates if the number of 
head term irreducible polynomials (\verb/IRR/) is equal to the 
total number of polynomials (\verb/LL/).
(The correctness proof is not totally trivial.) 


\subsection{Exercises} %----------------------

Let $S = \Q \lbrack X_1, X_2, X_3, X_4 \rbrack$ and
let $p_1 = X_1 + X_2 + X_3 + X_4 \in S$.
\begin{enumerate}
\item Read the polynomial $p_1$ in distributive rational 
      representation (using the 
      inverse lexicographical term order).
\item Compute $p_2 = p_1^3$ and print the result.
\item Convert $p_1$ to distributive integral representation and 
      then to integral recursive re\-presentation $q_1$.
\item Compute $q_2 = q_1^3$.
      Compute the polynomial gcd $q$ of $q_1$ and $q_2$. 
      Is $q$ equal to $q_1$ $?$
\item Convert $q_2$ to distributive integral representation 
      and then to distributive rational representation $p_3$.
      Is $p_2$ equal to $p_3$ $?$
\item Use the following linear from $L = (1, t, t^2, t^3, t^4, t^5 )$
      to compute a Gr\"obner base of 
      $(
          45 P +35 S -165 B -36,           
          35 P +40 Z +25 T -27 S,
          15 W +25 S P +30 Z -18 T -165 B**2,
          -9 W +15 T P +20 S Z,
           P W +2 T Z -11 B**3,
          99 W -11 B S +3 B**2
      $)
      with respect to this term order 
      in $\Q \lbrack B, S, T, Z, P, W \rbrack$
\end{enumerate}

Solution to step 1:

We use the function \verb/PREAD/ to input a list 
of polynomials, namely $( p_1 )$,
in distributive rational representation. 
Then we take the first element of this list $p_1$. 
In addition we print the polynomial list. 
For the term order we use the 
inverse lexicographical term order \verb/L/. 
The polynomial list can be printed by \verb/PWRITE/.
The variable list and the term order are remembered 
from the last call of \verb/PREAD/.

{\footnotesize
\begin{verbatim}
  P:=PREAD().
  (x1,x2,x3,x4) L
  (
  ( x1 + x2 + x3 + x4 )
  )

  PWRITE(P).
  p1:=FIRST(P).
\end{verbatim}
}

The result shows also the polynomials in 
distributive representation.

{\footnotesize
\begin{verbatim}
  Enter polynomial list: 
  ANS: (((1 0 0 0) (1 1) (0 1 0 0) (1 1) 
         (0 0 1 0) (1 1) (0 0 0 1) (1 1)))
 
  Polynomial in the variables: (x1,x2,x3,x4)
  Term ordering:  inverse lexicographical.
  Polynomial list: 
       (  x4 + x3 + x2 + x1 )

  ANS: ((1 0 0 0) (1 1) (0 1 0 0) (1 1) (0 0 1 0) (1 1) 
        (0 0 0 1) (1 1))
\end{verbatim}
}

Solution to step 2:

The polynomial exponentiation function is 
called \verb/DIRPEX/, \verb/t/ is the
desired power. 
To print the result we reconstruct a polynomial list 
and use \verb/PWRITE/.

{\footnotesize
\begin{verbatim}
  t:=3.
  p2:=DIRPEX(p1,t).
  Q:=LIST(p2).
  PWRITE(Q).
\end{verbatim}
}

And this is the output of the second and fourth statement:

{\footnotesize
\begin{verbatim}
ANS: ((3 0 0 0) (1 1) (2 1 0 0) (3 1) (2 0 1 0) (3 1) 
      (2 0 0 1) (3 1) (1 2 0 0) (3 1) (1 1 1 0) (6 1) 
      (1 1 0 1) (6 1) (1 0 2 0) (3 1) (1 0 1 1) (6 1) 
      (1 0 0 2) (3 1) (0 3 0 0) (1 1) (0 2 1 0) (3 1) 
      (0 2 0 1) (3 1) (0 1 2 0) (3 1) (0 1 1 1) (6 1) 
      (0 1 0 2) (3 1) (0 0 3 0) (1 1) (0 0 2 1) (3 1) 
      (0 0 1 2) (3 1) (0 0 0 3) (1 1))

   Polynomial in the variables: (x1,x2,x3,x4)
   Term ordering:  inverse lexicographical.
   Polynomial list: 
      (  x4**3 +3 x3 x4**2 +3 x2 x4**2 +3 x1 x4**2 +3 x3
      **2 x4 +6 x2 x3 x4 +6 x1 x3 x4 +3 x2**2 x4 +6 x1 x
      2 x4 +3 x1**2 x4 + x3**3 +3 x2 x3**2 +3 x1 x3**2 +
      3 x2**2 x3 +6 x1 x2 x3 +3 x1**2 x3 + x2**3 +3 x1 x
      2**2 +3 x1**2 x2 + x1**3 )
\end{verbatim}
}

Solution to step 3:

\verb/DIIFRP/ converts a distributive rational polynomial 
into a distributive integral polynomial. 
\verb/PFDIP/ converts then to a recursive integral polynomial.
Since \verb/PFDIP/ is no function, we must use the sequence 
\verb/r:=r. q1:=q1./ to display the results.

{\footnotesize
\begin{verbatim}
  q1:=DIIFRP(p1).
  PFDIP(q1,r,q1). r:=r. q1:=q1.
\end{verbatim}
}

The resulting output is:

{\footnotesize
\begin{verbatim}
  ANS: ((1 0 0 0) 1 (0 1 0 0) 1 (0 0 1 0) 1 (0 0 0 1) 1)
  ANS: 4
  ANS: (1 (0 (0 (0 1))) 0 (1 (0 (0 1)) 0 (1 (0 1) 0 (1 1))))
\end{verbatim}
}

Solution to step 4:

\verb/IPEXP/ denotes the integral polynomial exponentiation 
function. \verb/r/ is the number of variables as 
determined by \verb/PFDIP/. 
The polynomial gcd function is called 
\verb/IPGCDC/. It computes the $\gcd (q_1, q_2) = q$
and the cofactors of the gcd 
(that is $q_1 = q y$ and $q_2 = q z$).
To display the results we need the sequence 
\verb/q:=q. y:=y. z:=z./

{\footnotesize
\begin{verbatim}
  q2:=IPEXP(r,q1,t). 
  IPGCDC(r,q1,q2,q,y,z).
  q:=q. y:=y. z:=z.
\end{verbatim}
}

The output shows that $\gcd(q_1,q_2) = q_1$ as
expected. The cofactors are $z = p_1^2$ and
$y = 1$.

{\footnotesize
\begin{verbatim}
  ANS: (3 (0 (0 (0 1))) 2 (1 (0 (0 3)) 0 (1 (0 3) 
        0 (1 3))) 1 (2 (0 (0 3)) 1 (1 (0 6) 0 (1 6)) 
        0 (2 (0 3) 1 (1 6) 0 (2 3))) 0 (3 (0 (0 1)) 
        2 (1 (0 3) 0 (1 3)) 1 (2 (0 3) 1 (1 6) 0 (2 3)) 
        0 (3 (0 1) 2 (1 3) 1 (2 3) 0 (3 1))))

  ANS: (1 (0 (0 (0 1))) 0 (1 (0 (0 1)) 0 (1 (0 1) 0 (1 1))))
  ANS: (0 (0 (0 (0 1))))
  ANS: (2 (0 (0 (0 1))) 1 (1 (0 (0 2)) 0 (1 (0 2) 
        0 (1 2))) 0 (2 (0 (0 1)) 1 (1 (0 2) 0 (1 2)) 0 
        (2 (0 1) 1 (1 2) 0 (2 1))))
\end{verbatim}
}

Solution to step 5:

To suppress unwanted output we use a BEGIN--block.
\verb/DIFIP/ converts a recursive polynomial to 
a distributive polynomial and \verb/DIRFIP/ converts
to distributive rational representation. 
The resulting polynomial is put into a list 
and writen by \verb/PWRITE/. 
Finally we test the equality of 
$p_2$ and $q_3$ with the function \verb/EQUAL/. 

{\footnotesize
\begin{verbatim}
  BEGIN p3:=DIPFP(r,q2);
        p3:=DIRFIP(p3);
        Q:=LIST(p3);
        PWRITE(Q); 
        IF EQUAL(p2,p3) = 1 THEN CLOUT("equal") 
                            ELSE CLOUT("not equal") END;
        END.
\end{verbatim}
}

The resulting output is as follows:

{\footnotesize
\begin{verbatim}
  Polynomial in the variables: (x1,x2,x3,x4)
  Term ordering:  inverse lexicographical.
  Polynomial list: 
          (  x4**3 +3 x3 x4**2 +3 x2 x4**2 +3 x1 x4**2 +3 x3
          **2 x4 +6 x2 x3 x4 +6 x1 x3 x4 +3 x2**2 x4 +6 x1 x
          2 x4 +3 x1**2 x4 + x3**3 +3 x2 x3**2 +3 x1 x3**2 +
          3 x2**2 x3 +6 x1 x2 x3 +3 x1**2 x3 + x2**3 +3 x1 x
          2**2 +3 x1**2 x2 + x1**3 ) 
  equal
\end{verbatim}
}


Solution to step 6:

We use the function \verb/PREAD/ to input a list 
of polynomials together with the linear form
in distributive rational representation. 
The name \verb/T/ for the variable in the linear form 
is fixed and cannot changed. 
\index{linear form}
\index{PREAD}
With the given linear form, the variable \verb/B/ becomes  
the lowest variable. 

{\footnotesize
\begin{verbatim}
P:=PREAD(). 
 
(B,S,T,Z,P,W) ( 1, T, T**2, T**3, T**4, T**5 ) 
( 
( 45 P + 35 S - 165 B - 36 ), 
( 35 P + 40 Z + 25 T - 27 S ), 
( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), 
( - 9 W + 15 T P + 20 S Z ), 
( P W + 2 T Z - 11 B**3 ), 
( 99 W - 11 B S + 3 B**2 ) 
( B**2 + 33/50 B + 2673/10000 ) 
) 

PWRITE(P).  
Q:=DIRPGB(P,1). 
PWRITE(Q). 
\end{verbatim}
}

The resulting output is as follows:
\index{LFCHECK}

{\footnotesize
\begin{verbatim}
       LFCHECK: LF linearly independent.
       Polynomial in the variables: (B,S,T,Z,P,W)
       Term ordering: 
           1 
            T 
            T**2 
            T**3 
            T**4 
            T**5 
           
       Polynomial list: 
          ( 45 P +35 S -165 B -36 )
          ( 35 P +40 Z +25 T -27 S )
          ( 15 W +25 S P +30 Z -18 T -165 B**2 )
          ( -9 W +15 T P +20 S Z )
          (  P W +2 T Z -11 B**3 )
          ( 99 W -11 B S +3 B**2 )
\end{verbatim}
}
Note, that the linear form is checked if it is really 
linearly independent over the rational numbers to avoid mistakes. 

The Gr\"obner base is as follows:           

{\footnotesize
\begin{verbatim}
       Polynomial in the variables: (B,S,T,Z,P,W)
       Term ordering: 
           1 
            T 
            T**2 
            T**3 
            T**4 
            T**5 
           
       Polynomial list: 
          (  B**2 +33/50 B +2673/10000 )
          (  S -5/2 B -9/200 )
          (  T -37/15 B +27/250 )
          (  Z +49/36 B +1143/2000 )
          (  P -31/18 B -153/200 )
          (  W +19/120 B +1323/20000 )
\end{verbatim}
}

This concludes the discussion of the 
exercises. 


