% polynomial part 12.2.91 hk
% distributive system to MAS symbol interface 

%fixed notations
\def\Z{\mbox{\bf Z}} % math objects
\def\N{\mbox{\bf N}}
\def\Q{\mbox{\bf Q}}
\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{Interface to the MAS language} %----------------------

In section \ref{dip.alg} on algorithms 
for distributive polynomials we introduced 
the procedures $PREAD$ and $PWRITE$.
These procedures perform conversions between 
strings (input / output streams) and 
(lists) of distributive rational polynomials.

In this section we will discuss mainly two other
conversion procedures:
\begin{itemize}
\item $POLY$ converts a list of MAS expressions 
      to a list of distributive rational polynomials, 
      if the MAS expressions have a meaning as polynomials.
\item $TERM$ converts a list of distributive 
      rational polynomials to a (quoted) MAS expression,
      if the polynomials have a meaning as MAS expression. 
\end{itemize}

These two procedures improve the interactive usage 
of the distributive polynomial system. 
As a distinguishing feature the user of $POLY$ need 
no more worry about specifying a variable list,
since all variables in the expressions are automaticaly
added to the variable list, if not allready there.
Acompanying procedures are available which
set the desired term order and the variable list.


\def\S{\mbox{$\cal S$}}
Recall some definitions: 
Let $\L$ be the set of lists,
$\O$ be the set of objects,
$\S = \{ x \in \O: \ x$ represents a S--expression $\}$ 
be the set of S--expressions (they are the output of the MAS parser),
$\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}
\index{S-expression}\index{MAS! expression}


The interface functions are defined as follows:

\begin{deflist}{$P \gets POLY(p_1, \ldots, p_n)$} 
\item[$P \gets POLY(p_1, \ldots, p_n)$] 
      $p_i \in \S$ for $i=1, \ldots, n$ and $P \in \L\DIR_r$. 
      The MAS expressions $p_i$ are converted to 
      distributive rational polynomials. All variables 
      occuring in the $p_i$'s, which are not in the 
      system variable list are added to it as small variables. 
      The actual defined term order is used. 
      The syntax definition of $POLY$ is:
      \begin{verbatim}
ident ":=" "POLY(" expr { "," expr } ")"
      \end{verbatim}
      Where \verb/expr/ is an ordinary 
      MAS expression which is interpretable as a polynomial 
      over the rational numbers. 
      That is the following context conditions apply:
      \begin{enumerate}
      \item Only the operators \verb/"+"/, \verb/"-"/, 
            \verb/"*"/, \verb/"**"/, \verb/"^"/ and \verb."/". 
            may appear.
      \item The division operator \verb."/". may 
            only by used between numbers (atoms).
      \item String constants may occur and 
            (the string contents) are interpreted as 
            rational numbers.
      \item The identifiers occuring in \verb/expr/ 
            are per definition disjoint to identifiers 
            used as MAS variables even if they have the 
            same name.
      \item No function names may apear in the MAS expressions.
      \end{enumerate}
      $POLY$ is implemented by $DIP2SYM$\index{DIP2SYM}.
      \index{POLY}
\item[$T \gets TERM(P)$] 
      $P \in \L\DIR_r$ and $T \in \S$. 
      $P$ is a list of distributive rational polynomials. 
      $T$ is a quoted list of S--expressions, that is
      a list of terms, which are marked that they are 
      not to be evaluated by the MAS interpreter.
      For the variable names the names from the 
      actual variable list are used. 
      $TERM$ is implemented by $SYM2DIP$\index{SYM2DIP}.
      \index{TERM}
\item[$V' \gets DIPVDEF(V)$] $V, V' \in \L$. 
      $V$ and $V'$ are variable lists. 
      The list $V$ is converted to a variable list and stored in the 
      global variable $VALIS$. 
      The new variable list is returned in $V'$.
      \index{DIPVDEF}
\item[$t' \gets DIPTODEF(t)$] $t, t' \in \L$. 
      $t$ and $t'$ are term order indicators as
      defined in the global variable $EVORD$. 
      $t$ becomes the new term order, 
      the old term order $t'$ is returned. 
      Possible values for $t$ are:
      \begin{description}
      \item[1] invers lexicographical term order 
               ascending order,
      \item[2] invers lexicographical term order 
               descending order,
               $2$ corresponds to the term order letter \verb/"L"/
               in $PREAD$,
      \item[3] invers graded lexicographical term order 
               ascending order,
      \item[4] invers graded lexicographical term order 
               descending order,
               $4$ corresponds to the term order letter \verb/"G"/
               in $PREAD$,
      \item[5] lexicographical term order ascending order,
      \item[6] lexicographical term order descending order,
      \item[7] total degree Buchberger lexicographical term order
               ascending order,
      \item[8] total degree Buchberger lexicographical term order
               descending order,
      \item[$L$] a linear form, $L$ is a list 
               $L = ( l_1, \ldots, l_n )$ where 
               each $l_i \in \IP_1$, $i=1,\ldots,n$ 
               is an 
               {\em univariate recursive integral} polynomial. 
      \end{description}
      \index{DIPTODEF}
      {\bf Note:} The orders with even indicator numbers are 
      admissible term orders, the others are in general not admissible.  
      \label{diptodef}
\item[$EVOWRITE(t)$] $t \in \L$. 
      $t$ is a term order indicator, the meaning of this 
      indicator as term order is written to the output stream. 
      \index{EVOWRITE}
\end{deflist}
Since the argument expressions to $POLY$ are not 
evaluated, there is no way substitute a value for 
a variable in such an expression. 
(Substitution in polynomials can only performed by 
the library functions like $DIRPSM$ or $DIRPSV$.) 
This is different 
to other computer algebra systems like REDUCE where the 
polynomial variables are identified with the 
programming language variables.

The reason for this behavior lies in the 
MAS view of polynomials: 
they are constants from some algebraic structure 
and do not belong to the programming (meta) language.


{\bf Examples:} 

Let $S = \Q\lbrack X_1, X_2, X_3, X_4 \rbrack$
and let $p, q \in S$, $p = X_1 + X_2 + X_3 + X_4$, 
$q = 3/4 X_1^2$. 
We will first discuss some examples for the use 
of $POLY$, then of $TERM$ and finaly some examples for 
$DIPVDEF$ and $DIPTODEF$.
\begin{verbatim}
       P:=POLY(x1+x2+x3+x4).
       Variable(s) added to VALIS:  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)))

       p:=FIRST(POLY( x1 + x2 + x3 + x4 )).
       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))

       q:=SECOND(POLY( x1 + x2 + x3 + x4, 3/4 x1^2 )).
       ANS: ((2 0 0 0) (3 4))
\end{verbatim}
In the first example the variables \verb/x1, x2, x3, x4/ 
have not been in the variable list.
A message is printed that they are added to the variable list.
Note that the order of the (self detected) variables 
can not be precisely determined, since it depends on the 
expression tree structure of the parsed MAS expression. 
If the order is important use $DIPVDEF$ to set the 
variables explicitly.
Observe that $POLY$ allways produces a list of 
distributive polynomials. So in order to obtain one 
polynomial any list selector function can be used. 
The operands of \verb."/". must be atoms, so \verb.3/4. is ok. 
\begin{verbatim}
       P:=POLY( "11111/33" ).
       ANS: (((0 0 0 0) (11111 33))) 
\end{verbatim}
String constants can be used in $POLY$. The string contents 
are interpreted as rational numbers (read by $RNDRD$).
\begin{verbatim}
       P:=POLY( (x1+x2)^2 ).
       ANS: (((2 0 0 0) (1 1) (1 1 0 0) (2 1) 
              (0 2 0 0) (1 1))) 
\end{verbatim}
During the conversion from MAS expressions 
the distributive polynomials are expanded 
(or distributed as their name suggests). 

In the opposite direction we will convert \verb/P/ back 
to a MAS expression.
\begin{verbatim}
       T:=TERM(P).
       ANS: QUOTE(((x1^2+2*x1*x2)+x2^2)) 
\end{verbatim}
Observe that the MAS expressions have a tree structure, 
which is visible by the (useless) inner parenthesis 
around the first two summands.
\begin{verbatim}
       T:=TERM( POLY( 3/4 ) ).
       ANS: QUOTE((3/4)) 
\end{verbatim}
This shows, that atoms are printed correctly, 
however integers are not printed correctly:
\begin{verbatim}
       T:=TERM( POLY( "111111111111111111111111111111/2" ) ).
       ANS: QUOTE(((197423559 458992985 3)/2)) 
\end{verbatim}
Lists of atoms are not printed as integers but 
as lists of atoms.

Some further examples for the use of 
$DIPVDEF$ and $DIPTODEF$.
\begin{verbatim}
       V:=DIPVDEF(LIST("x4", "x9", "x12", "x3")).
       ANS: ((62 4) (62 9) (62 12) (62 3)) 
\end{verbatim}
Polynomial variable names can be specified as
character strings.
\verb/"x3"/ now becomes the new main variable.
The new variable list is returned.
To extend (or modify) an existing variable list 
any list processing function can be used. 
\begin{verbatim}
       V:=CCONC(V,LIST("x5", "x6", "x7", "x8")).
       ANS: ((62 4) (62 9) (62 1 2) (62 3) 
             (62 5) (62 6) (62 7) (62 8)) 
       DIPVDEF(V).
       ANS: ((62 4) (62 9) (62 1 2) (62 3) 
             (62 5) (62 6) (62 7) (62 8)) 
\end{verbatim}

A final example is concerned with 
selection of term orders.
\begin{verbatim}
       DIPTODEF(6).
       ANS: 2 
\end{verbatim}
This call of $DIPTODEF$ switches to lexicographical term order.
The old term order was $2$ = inverse lexicographical.
Setting a linear form defined term order 
is a bit more involved since there is at the moment 
no function wich converts a list of rational distributive 
polynomials into a list of integral recursive polynomials. 
But this will soon be available.

This concludes the overview of the MAS language 
to distributive polynomial interface.


\section{Optimization of the Term Order} %---------------------

\label{sec:topt}
In most applications the computating time for a 
Gr\"obner Basis is strongly dependend
on the chosen variable ordering and term ordering.
(See the example below, table \ref{tab:timeto}.)
To find an `optimal' variable ordering one looks
at the reduced univariate polynomials:
\index{reduced univariate polynomial}

{\bf Definition:} 
Let $f(x_1,\ldots,x_r) \in$ \R, then the
{\em reduced univariate polynomial} corresponding to $f$
for the variable $x_i ( 1 \leq i \leq r )$
is defined by:
\begin{displaymath}
     p_i (x_i) = g(1,...,1,x_i,1,...,1) \in \N \lbrack x_i \rbrack
\end{displaymath}
with $g = \sum x^{(i)}$ when $f = \sum a_{(i)} x^{(i)}$.
The reduced polynomial for a set of
polynomials is the sum over all
reduced polynomials
corresponding to the elements of the set.

{\bf Experience:}
Tests for computing times of Gr\"obner bases and
factorizing of multivariate polynomials have shown:
The variable ordering is optimal if
\index{optimal variable ordering}
\begin{displaymath}
        p_1 (x) \geq \ldots \geq p_r (x).
\end{displaymath}
where the univariate polynomials are ordered according
to the inverse lexicographical ordering of their
coefficient vectors, that means:
\begin{displaymath}
        h(x) > 0     \Longleftrightarrow  ldcf(h) > 0
        \mbox{\ \  and \ \ }
        h(x) > k(x)  \Longleftrightarrow  h(x) - k(x) > 0%
\end{displaymath}
The computation of the reduced univariate polynomials itself 
and the reordering of the variables is not much time consuming.
So this optimization will in many cases lead to 
faster Gr\"obner bases computation. 

There is an algorithm $DIPVOP$ in the distributive polynomial 
system which does this kind of term order optimization. 
Its specification is 

\begin{deflist}{$DIPVOP(P,V; P', V')$}
\item[$DIPVOP(P,V; P', V')$] with $P, P' \in \DI_r$ and 
      $V, V'$ are variable lists. The representation of the 
      polynomials in $P$ and the variable list $V$ are changed 
      according to the optimization heuristics. 
      $P'$ is the new polynomial list and $V'$ is the new 
      variable list.
\item[$M \gets DIPDEM(p)$] for one polynomial $p \in \DI_r$,
      and 
\item[$M \gets DIPLDM(P)$] for a list of polynomials 
      $P \in \L\DI_r$ determine the tuples 
      of reduced univariate polynomials $M \in \L\IP_1$.
\end{deflist}


{\bf Example:}

\label{ex:trinks}
To demonstrate the influence of the term order on the computation
of Gr\"obner bases we include the following example of an
ideal generated by
7 polynomials in 6 variables \cite{Trinks78}.
Let $R = \Q$, $S = R \lbrack B, S, T, Z, P, W \rbrack$ 
and let the polynomials be
\begin{verbatim}
      ( + 45 P + 35S -165B -36 ,
        + 35 P +40 Z + 25 T -27 S ,
        + 15 W + 25 P S + 30 Z - 18 T - 165 B**2 ,
        - 9 W + 15 P T + 20 Z S ,
          W P + 2 Z T - 11 B**3 ,
        99 W - 11 S B + 3 B**2 ,
        B**2 + 33/50 B + 2673/10000   )
\end{verbatim}
In table \ref{tab:timeto} the dependence is clearly visible.
The reduced univariate polynomial for variable \verb/B/ 
has degree $3$, so a term order where \verb/B/ is smaller 
than all other variables will be most desirable. 
Observe that as more right \verb/B/ stands in the 
variable list as worst become the computing times.

Note that especially the inverse lexicograpbical term order 
is very sensitive against reordering of variables, 
the inverse graduated term order is less sensitive.
However in some applications (like ideal elimination theory)
Gr\"obner bases are required with respect to the 
inverse lexicographical term order .

\begin{table}
\centering
\begin{tabular}{|l|r|r|}
\hline
      variable  &      time    &      time    \\
      ordering  &   term order = L   &  term order = G \\
\hline
       BSTZPW &               1.95   &      10.92   \\
       SBTZPW &              27.99   &      16.37   \\
       STBZPW &             110.27   &      18.74   \\
       STZPBW &             115.35   &      16.37   \\
       STZPWB &             247.03   &      20.81   \\
       SZPWBT &              67.68   &      19.78   \\
       PWBTSZ &             103.16   &      20.61   \\
       ZWBSTP &       $>$ 3 600.00   &      32.31   \\
       TZPWBS &                tfc   &      50.50   \\
       ZPWBST &       $>$ 3 600.00   &      39.32   \\
       PWBSTZ &              37.18   &      20.88   \\
       WBSTZP &              34.98   &      10.81   \\
\hline
\end{tabular}
\\
\begin{minipage}{8cm}
\footnotesize
Computing time in seconds on IBM 370/168. 
'tfc' = 'to few cells reclaimed', that means 
not enough storage was available. From \cite{BGK86}.
\end{minipage}
\label{tab:timeto}
\caption{Computing times for different term orderings}
\end{table}



