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


% SQMATRIX.htex

The top level matrix type in \Language{} is \spadtype{Matrix}
(see \xmpref{Matrix}), which
provides basic arithmetic and linear algebra functions.
However, since the matrices can be of any size, it is not true that any pair
can be added or multiplied.
Thus \spadtype{Matrix} has little algebraic structure.

Sometimes you want to use matrices as coefficients for polynomials
or in other algebraic contexts.  In this case, \spadtype{SquareMatrix}
\index{matrix!square}
should be used.  The domain \spadtype{SquareMatrix(n,R)} gives the ring of
\spad{n} by \spad{n} square matrices over \spad{R}.

\xtc{
Since \spadtype{SquareMatrix} is not normally exposed at the top level,
you must expose it before it can be used.
}{
\spadcommand{)set expose add constructor SquareMatrix \bound{SQ}}
}
\xtc{
Once \spad{SQMATRIX} has been exposed,
values can be created using the \spadfunFrom{squareMatrix}{SquareMatrix}
function.
}{
\spadcommand{m := squareMatrix [[1,-\%i],[\%i,4]] \bound{m}\free{SQ}}
}
\xtc{
The usual arithmetic operations are available.
}{
\spadcommand{m*m - m \free{m}}
}
\xtc{
Square matrices can be used where ring elements are required.
For example, here is a matrix with matrix entries.
}{
\spadcommand{mm := squareMatrix [[m, 1], [1-m, m^2]] \free{m}\bound{mm}}
}
\xtc{
Or you can construct a polynomial with  square matrix coefficients.
}{
\spadcommand{p := (x + m)^2 \free{m}\bound{p}}
}
\xtc{
This value can be converted to a square matrix with polynomial coefficients.
}{
\spadcommand{p::SquareMatrix(2, ?) \free{p}}
}

For more information on related topics, see
\spadref{ugTypesWritingModes},
\spadref{ugTypesExpose}, and
\xmpref{Matrix}.
\showBlurb{SquareMatrix}
