
% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     - Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%
%     - Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the
%       distribution.
%
%     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR
% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


The \spadtype{XPolynomialRing} domain constructor implements
generalized polynomials with coefficients from an arbitrary \spadtype{Ring}
(not necessarily commutative) and whose exponents are
words from an arbitrary \spadtype{OrderedMonoid}
(not necessarily commutative too).
Thus these polynomials are (finite) linear combinations of words.

This constructor takes two arguments.
The first one is a \spadtype{Ring}
and the second is an \spadtype{OrderedMonoid}.
The abbreviation for \spadtype{XPolynomialRing} is \spadtype{XPR}.

Other constructors like \spadtype{XPolynomial},
\spadtype{XRecursivePolynomial},
\spadtype{XDistributedPolynomial},
\spadtype{LiePolynomial} and
\spadtype{XPBWPolynomial}
implement multivariate polynomials
in non-commutative variables.

We illustrate now some of the facilities of the \spadtype{XPR} domain constructor.

\xtc{
Define the free ordered monoid generated by the symbols.
}{
\spadcommand{Word := FreeMonoid(Symbol) \bound{Word}}
}

\xtc{
Define the linear combinations of these words with integer coefficients.
}{
\spadcommand{poly:= XPR(Integer,Word) \free{Word} \bound{poly}}
}


\xtc{
Then we define a first element from {\bf poly}.
}{
\spadcommand{p:poly := 2 * x - 3 * y + 1 \free{poly} \bound{p}}
}

\xtc{
And a second one.
}{
\spadcommand{q:poly := 2 * x + 1 \free{poly} \bound{q}}
}


\xtc{
We compute their sum,
}{
\spadcommand{p + q  \free{p}\free{q} }
}

\xtc{
their product,
}{
\spadcommand{p * q  \free{p}\free{q} }
}

\xtc{
and see that variables do not commute.
}{
\spadcommand{(p +q)^2 -p^2 -q^2 - 2*p*q  \free{p}\free{q} }
}



\xtc{
Now we define a ring of square matrices,
}{
\spadcommand{M := SquareMatrix(2,Fraction Integer)  \bound{M}}
}

\xtc{
and the linear combinations of words with these  matrices as coefficients.
}{
\spadcommand{poly1:= XPR(M,Word) \free{Word} \free{M} \bound{poly1}}
}


\xtc{
Define a first matrix,
}{
\spadcommand{m1:M := matrix [[i*j^2 for i in 1..2] for j in 1..2] \free{M} \bound{m1}}
}

\xtc{
a second one,
}{
\spadcommand{m2:M := m1 - 5/4 \free{M} \free{m1} \bound{m2}}
}

\xtc{
and a third one.
}{
\spadcommand{m3: M := m2^2 \free{M} \free{m2} \bound{m3}}
}

\xtc{
Define a polynomial,
}{
\spadcommand{pm:poly1   := m1*x + m2*y + m3*z - 2/3 \free{poly1} \free{m1} \free{m2} \free{m3} \bound{pm}}
}


\xtc{
a second one,
}{
\spadcommand{qm:poly1 := pm - m1*x \free{m1} \free{pm} \bound{qm}}
}

\xtc{
and the following power.
}{
\spadcommand{qm^3 \bound{qm}}
}
