\@primitives{amsmath}
\let\text\mbox
%% Equation tags
\renewcommand{\theequation}{\arabic{equation}}
\newif\ifams@star
\newcommand{\ams@yesnumber}
  {\@yesnumber\gdef\ams@currentlabel{\theequation}}
\newcommand{\ams@nonumber}{\global\let\@number\@empty}
\def\tag#1{\gdef\@number{\eqno{\text{(#1)}}}\gdef\ams@currentlabel{\text{#1}}%
\ifams@star\else\addtocounter{equation}{-1}\fi}%
\def\tag*#1{\gdef\@number{\eqno{\text{#1}}}\gdef\ams@currentlabel{\text{#1}}%
\ifams@star\else\addtocounter{equation}{-1}\fi}%
\def\notag
  {\ams@nonumber
  \ifams@star\else\addtocounter{equation}{-1}}%
\let\nonumber\notag%
%%%%% AMS equations
\newenvironment{ams@equation}[1]
  {\[\def\@currentlabel{\ams@currentlabel}#1}
  {\@number\]}
\newenvironment{equation*}
  {\ams@startrue\begin{ams@equation}{\ams@nonumber}}
  {\end{ams@equation}}
\renewenvironment{equation}
  {\ams@starfalse\begin{ams@equation}{\ams@yesnumber\stepcounter{equation}}}
  {\end{ams@equation}}
%%%%AMS align
\newcounter{@align@col}
\newcounter{@align@limit}
\newenvironment{ams@alignat}[2]
{\setcounter{@align@limit}{2*#1-1}%
\@ifundefined{@align@inside}{\def\@align@inside{}}{\warning{Nested align}}%
\[\@changelabel\def\@currentlabel{\ams@currentlabel}%
\setcounter{@align@col}{0}%
#2%
\def\@extra{\quad\quad}%
\let\@PBS=\@HEVEA@bsbs
\let\@PAM=\@HEVEA@amper
\newcommand{\@eqna@complete}
  {\whiledo{\value{@align@col}<\value{@align@limit}}{\def\@extra{}&}}
\renewcommand{\@hevea@amper}
  {\ifthenelse{\value{@align@col}<\value{@align@limit}}
    {\stepcounter{@align@col}%
    \ifthenelse{\isodd{\value{@align@col}}}{}{\@extra}%
    \@PAM}
    {\warning{Extra column in eqnarray}}}     
\renewcommand{\\}[1][]
  {\@eqna@complete% End line
  \@PAM\@number\@PBS% format equation number
  #2%step equation number
  \setcounter{@align@col}{0}}
\@array{*{#1}{rl}r}}
  {\\{}\end@array\]}
\newenvironment{alignat}[1]
  {\ams@starfalse\begin{ams@alignat}
     {#1}
     {\ams@yesnumber\stepcounter{equation}}}
  {\end{ams@alignat}\addtocounter{equation}{-1}}
\newenvironment{alignat*}[1]
  {\ams@startrue\begin{ams@alignat}{#1}{\ams@nonumber}}
  {\end{ams@alignat}}
\newenvironment{align}{\begin{alignat}{5}}{\end{alignat}}
\newenvironment{align*}{\begin{alignat*}{5}}{\end{alignat*}}
%%%%%%%%AMS gather
\newenvironment{ams@gather}[1]
  {\[\@changelabel\def\@currentlabel{\ams@currentlabel}%
  \@array{@{#1}c@{\@number}}}
  {\end@array\]}
\newenvironment{gather}
  {\ams@starfalse\begin{ams@gather}{\ams@yesnumber\stepcounter{equation}}}
  {\end{ams@gather}}
\newenvironment{gather*}
  {\ams@startrue\begin{ams@gather}{\ams@nonumber}}
  {\end{ams@gather}}
%%%%%%%AMS multline
\newcounter{ams@line}
\newenvironment{ams@multline}[1]
  {\@changelabel\def\@currentlabel{\ams@currentlabel}%
  #1%
  \setcounter{ams@line}{0}%
  \let\@PBS=\\%
  \renewcommand{\\}[1][]
     {\@close{DISPLAY}%
     \ifthenelse{\value{ams@line}>0}{\centering}{\raggedright}\)\endgroup\stepcounter{ams@line}\begingroup\(\@open{DISPLAY}{}}%
  \begingroup\(\@open{DISPLAY}{}}
  {\@close{DISPLAY}\)\raggedleft\@number\endgroup}
\newenvironment{multline}
  {\ams@starfalse\begin{ams@multline}{\ams@yesnumber\stepcounter{equation}}}
  {\end{ams@multline}}
\newenvironment{multline*}
  {\ams@startrue\begin{ams@multline}{\ams@nonumber}}
  {\end{ams@multline}}
%AMS split
\newenvironment{split}{\begin{array}{r}}{\end{array}}%
\renewenvironment{cases}{\left\{\begin{array}{ll}}{\end{array}\right.}%
\newcommand{\intertext}[1]{\qquad\mbox{#1}\\}%
%%Matrices
\newcounter{MaxMatrixCols}\setcounter{MaxMatrixCols}{10}%
\newenvironment{matrix}{\begin{array}{*{\value{MaxMatrixCols}}{c}}}{\end{array}}%
\newenvironment{pmatrix}{\left(\begin{array}{*{\value{MaxMatrixCols}}{c}}}{\end{array}\right)}%
\newenvironment{bmatrix}{\left[\begin{array}{*{\value{MaxMatrixCols}}{c}}}{\end{array}\right]}%
\newenvironment{vmatrix}{\left|\begin{array}{*{\value{MaxMatrixCols}}{c}}}{\end{array}\right|}%
\newenvironment{Vmatrix}{\left|\left|\begin{array}{*{\value{MaxMatrixCols}}{c}}}{\end{array}\right|\right|}%
\newcounter{@hdots}%
\newcommand{\hdotsfor}[2][]{%
\setcounter{@hdots}{#2}%
\whiledo{\value{@hdots}>1}{\ldots &\addtocounter{@hdots}{-1}}%
\ldots}%
\newenvironment{smallmatrix}{\begin{matrix}}{\end{matrix}}
%%%%%%%%%%%% Some commands by B. Salvy
\newcommand{\binom}[2]{\begin{pmatrix}#1\\#2\end{pmatrix}}
\newcommand{\boldmath}{\bf}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Other variants of \frac					%
%      	\dfrac makes no estimate of display-style in HEVEA	%
%       \tfrac makes no estimate of text-style in HEVEA         %
%       \cfrac takes an optional argument as well		%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\dfrac}[2]{\frac{#1}{#2}}
\newcommand{\tfrac}[2]{\frac{#1}{#2}}
\newcommand{\cfrac}[3][c]{%
\ifthenelse{\equal{#1}{c}}{{#2\over #3}}{%
\ifthenelse{\equal{#1}{l}}{{#2\@lover #3}}{%
\ifthenelse{\equal{#1}{r}}{{#2\@rover #3}}{%
\warning{Invalid Argument To \cfrac ignored}{#2\over #3}%
}}}}
\newcommand{\dotsb}{\cdots}
\newcommand{\dotsc}{\dots}
\newcommand{\dotsm}{\cdots}
\newcommand{\eqref}[1]{(\ref{#1})}
%\newcommand{\iiint}{\int\!\!\int\!\!\int}
\newcommand{\iiiint}{\int\!\!\int\!\!\int\!\!\int}
%\newcommand{\mathbb}[1]{{\bf #1}}
%\newcommand{\mathfrak}[1]{{\green #1}}
\newcommand{\mathnormal}[1]{#1}
\newcommand{\mod}{\text{mod}}
\newcommand{\operatorname}[1]{\text{#1}}
\newcommand{\smallint}{\int}
\newcommand{\substack}[1]{\begin{matrix}#1\end{matrix}}
\newenvironment{proof}{\par\noindent{\bf Proof.}}{\par\medskip}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Support for \xleftarrow and \xrightarrow			%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\newcommand{\xleftarrow}[2][]{\ifdisplay{#2\@xleft #1}\else%
\warning{\xleftarrow outside display mode, arguments ignored}\leftarrow\fi}%
\newcommand{\xrightarrow}[2][]{\ifdisplay{#2\@xright #1}\else%
\warning{\xrightarrow outside display mode, arguments ignored}\rightarrow\fi}%
