function P = mpoly(varargin)
% MPOLY -- create matrix polynomial
%
% P = mpoly(C,kmin,'type',m,r)
% P = mpoly(P)
%
% FIRST FORM: P = mpoly(C,kmin,'type',m,r)
%
% This is the constructor method. C represents the matrix polynomial
% in one of the following forms:
%
% 1. A three-dimensional matrix, where C(:,:,k) represents
% the kth coefficient matrix.
% 2. A cell vector of two-dimensional matrices, where
% C{k} represents the kth coefficient matrix.
%
% KMIN is the starting exponent. 'TYPE' is '', 'symbol' or 'polyphase'.
% M is the dilation factor, R is the multiplicity. If 'TYPE' is set,
% M and R must also be given.
%
% Default input arguments are C=0, KMIN=0, 'TYPE' = '', M = R = 0.
%
% For example, the matrix polynomial
%
% (1 2) (5 6) 2
% P(z) = (3 4) z + (7 8) z
%
% could be created in one of the following two ways:
%
% P = mpoly({[1,2;3,4],[5,6;7,8]},1)
%
% or
%
% C = [1,2;3,4];
% C(:,:,2) = [5,6;7,8];
% P = mpoly(C,1);
%
% SECOND FORM: P = mpoly(P)
%
% This is the conversion routine.
%
% If P is of type '' do nothing
% 'polyphase' convert it to a matrix polynomial
% of type '' by rearranging the coefficients.
% 'symbol' convert it to a matrix polynomial
% of type '' by scaling by sqrt(m).
% Copyright (c) 2004 by Fritz Keinert (keinert@iastate.edu),
% Dept. of Mathematics, Iowa State University, Ames, IA 50011.
% This software may be freely used and distributed for non-commercial
% purposes, provided this copyright statement is preserved, and
% appropriate credit for its use is given.
%
% Last update: Feb 20, 2004
% The following statement insures that operations between
% symbolic constants and matrix polynomials work properly
superiorto('sym');
switch class(varargin{1})
case 'mpoly'
% First form: P = mpoly(P)
P = varargin{1};
switch P.type
case ''
% nothing to do
case 'polyphase'
P.type = '';
[n1,n2] = size(P);
P = reshape(P,n1,P.r);
case 'symbol'
P.type = '';
P = P * sqrt(m);
otherwise
disp('this should not happen');
keyboard;
end
otherwise
% Second form: P = mpoly(C,kmin,'type',m,r)
if (nargin < 1)
C = 0;
else
C = varargin{1};
end
if (nargin < 2)
kmin = 0;
else
kmin = varargin{2};
end
if (nargin == 3 | nargin == 4)
error('MPOLY must have 1, 2, or 5 arguments');
end
if (nargin >= 5)
type = varargin{3};
m = varargin{4};
r = varargin{5};
else
type = '';
m = 0;
r = 0;
end
% check type of C, do appropriate conversion
switch class(C)
case {'double','sym'}
% check that a third dimension is provided
if (size(C,3) == 0)
C = zeros(size(C,1),size(C,2));
end
case 'cell'
% convert cell array to 3d matrix
if (length(C) == 0)
C = 0;
else
C = cat(3,C{:});
end
otherwise
error(['I don''t know how to convert ''',class(C),''' to ''mpoly''']);
end
% build P
P = struct('coef',{C},'min',{kmin},'type',{type},'m',{m},'r',{r});
P = class(P,'mpoly');
end
% don't do any trimming here
% sometimes we need to create an empty matrix polynomial
% with a known number of zero entries