📄 test_mpoly_symbolic.m
字号:
% TEST_MPOLY_SYMBOLIC -- test the subroutines in the @mpoly directory for symbolic matrices
% 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
disp('testing routines in mw/@mpoly for symbolic matrices');
disp(' ');
% test mpoly
P2 = [112,122;212,222];
P3 = [113,123;213,223];
P23 = cat(3,P2,P3);
P = mpoly({P2,P3},2);
Q = mpoly({P2,P3});
PP = mpoly(P23,2,'polyphase',2,1);
P11 = mpoly({112,113},2);
N = sym(P);
NN = mpoly(sym(P23),2,'polyphase',2,1);
N11 = sym(P11);
syms a b c d real
S = mpoly({[a,0;0,b],eye(2)},2);
SS = mpoly({[a,0;0,b],eye(2)},2,'polyphase',2,1);
S11 = mpoly({a,1},2);
I = mpoly(eye(2));
Z = mpoly(zeros(2));
disp('OK - mpoly');
% test display
S
N
disp('OK - display');
% test trim
Nt = trim(N,[3,4]);
if (size(Nt.coef,3) ~= 2 | Nt.coef ~= cat(3,P3,zeros(2)))
error('failed - trim');
end
St = trim(S,[3,4]);
if (size(St.coef,3) ~= 2 | St.coef ~= cat(3,P3,zeros(2)))
error('failed - trim');
end
Npm = (eye(2) + N) - eye(2);
if (size(Npm.coef,3) ~= 2 | Npm.min ~= N.min)
error('failed - trim');
end
Spm = (eye(2) + S) - eye(2);
if (size(Spm.coef,3) ~= 2 | Spm.min ~= S.min)
error('failed - trim');
end
disp('OK - trim');
% test comparisons
if ((P ~= N) | (N == S) | (P == S))
error('failed - comparisons: eq, ne');
end
% test conversion
H = symbol(NN);
if (polyphase(H) ~= NN)
error('failed - conversion: symbol, polyphase');
end
if (double(N) ~= P)
error('failed - conversion: double');
end
if (reshape(N,1,4) ~= mpoly(reshape(N.coef,1,4,2),2))
error('failed - reshape');
end
if (reshape(S,1,4) ~= mpoly(reshape(S.coef,1,4,2),2))
error('failed - reshape');
end
disp('OK - conversions: symbol, polyphase, sym, double, reshape');
% test more comparisons
if (~isconstant(sym(I)) | isconstant(N))
error('failed - isconstant');
end
if (~isidentity(sym(I)) | isidentity(N))
error('failed - isidentity');
end
z = mpoly(1,1);
if (~ismonomial(sym(I)*z) | ismonomial(N))
error('failed - ismonomial');
end
if (~isnumeric(I) | isnumeric(N) | isnumeric(S))
error('failed - isnumeric');
end
if (issymbolic(I) | issymbolic(N) | ~issymbolic(S))
error('failed - issymbolic');
end
if (iszero(sym(I)) | ~iszero(sym(Z)))
error('failed - iszero');
end
disp('OK - comparisons: eq, ne, isconstant, isidentity');
disp(' ismonomial, isnumeric, issymbolic, iszero');
% test concatenation
z = mpoly(sym(1),1);
Q = [z,0;0,z];
if (Q ~= z * eye(2))
error('failed - concatenation');
end
disp('OK - concatenation: horzcat, vertcat');
% test right-hand subscripting
if (N.min ~= 2 | S.min ~= 2)
error('failed - subscripting P.min');
end
if (N.max ~= 3 | S.max ~= 3)
error('failed - subscripting P.max');
end
Scoef = [a,0;0,b];
Scoef(:,:,2) = sym(eye(2));
if (N.coef ~= P23 | S.coef ~= Scoef)
error('failed - subscripting P.coef');
end
if (N.length ~= 2 | S.length ~= 2)
error('failed - subscripting P.length');
end
if (N.degree ~= 1 | S.degree ~= 1)
error('failed - subscripting P.degree');
end
if (~iszero(N.size - [2,2]) | ~iszero(S.size - [2,2]))
error('failed - subscripting P.size');
end
if (~strcmp(NN.type,'polyphase') | ~strcmp(SS.type,'polyphase'))
error('failed - subscripting P.type');
end
if (NN.m ~= 2 | SS.m ~= 2)
error('failed - subscripting P.m');
end
if (NN.r ~= 1 | SS.r ~= 1)
error('failed - subscripting P.r');
end
if (~iszero(N{end} - P3) | ~iszero(S{end} - eye(2)))
error('failed - subscripting P{i}');
end
if (~iszero(N{4} - zeros(2)) | ~iszero(S{4} - zeros(2)))
error('failed - subscripting P{i}');
end
if (N(1,1) ~= N11 | S(1,1) ~= S11)
error('failed - subscripting P(i,j)');
end
disp('OK - right-hand subscripting: get, P.field, P(i,j), P{i}')
% test left-hand subscripting
Q = N;
Q.min = Q.min + 1;
if (Q ~= P*z)
error('failed - left-hand subscripting P.min');
end
Q = S;
Q.min = Q.min + 1;
if (Q ~= S*z)
error('failed - left-hand subscripting P.min');
end
Q = N;
Q{2} = zeros(2);
if (Q ~= mpoly(P3,3))
error('failed - left-hand subscripting P{i}');
end
Q = S;
Q{2} = zeros(2);
if (Q ~= mpoly(eye(2),3))
error('failed - left-hand subscripting P{i}');
end
Q = N;
Q(1,1) = z^2;
if (Q(1,1) ~= z^2)
error('failed - left-hand subscripting P(i,j)');
end
Q = S;
Q(1,1) = z^2;
if (Q(1,1) ~= z^2)
error('failed - left-hand subscripting P(i,j)');
end
disp('OK - left-hand subscripting: set, P.field, P(i,j), P{i}')
% test size
if (iszero(N.size - size(N)) & iszero(S.size - size(S)))
disp('OK - size')
else
error('failed - size');
end
% test addition/subtraction/scalar multiplication
if (N ~= +N | S ~= +S)
error('failed - unary plus');
end
if (N + P ~= 2*P)
error('failed - addition or scalar multiplication');
end
if (S + S ~= 2*S)
error('failed - addition or scalar multiplication');
end
if (~iszero(-N + N))
error('failed - unary minus');
end
if (~iszero(-S + S))
error('failed - unary minus');
end
Iz = mpoly(eye(2),1);
if (N + Iz - N ~= Iz)
error('failed - addition or subtraction');
end
if (S + Iz - S ~= Iz)
error('failed - addition or subtraction');
end
disp('OK - addition, subtraction, scalar multiplication');
% test elementwise multiplication/division/power
Two = 2 * mpoly(ones(2,2,2),2);
if (N .* Two ~= N * 2 | S .* Two ~= S * 2)
error('failed - times');
end
if (Two .\ N ~= 2 \ N | Two .\ S ~= 2 \ S)
error('failed - ldivide');
end
if (N ./ Two ~= N / 2 | S ./ Two ~= S / 2)
error('failed - rdivide');
end
if (N .^ 2 ~= mpoly(P23.^2,2) | S.^2 ~= mpoly({[a,0;0,b].^2,eye(2)},2))
error('failed - power');
end
disp('OK - pointwise operations: times, ldivide, rdivide, power');
% test multiplication/division/power
disp(' ');
disp('testing division');
disp('this is split into multiple parts, and will take a while');
disp(' ');
% matrix with nonsingular leading term
L = eye(2);
L(1,1,2) = 1;
L = mpoly(L);
% case 1: L numeric
NL = N*L;
[Q,method] = mrdivide(NL,L);
if (Q ~= N)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
LN = L*N;
[Q,method] = mldivide(L,LN);
if (Q ~= P)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
SL = S*L;
[Q,method] = mrdivide(SL,L);
if (Q ~= S)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
LS = L*S;
[Q,method] = mldivide(L,LS);
if (Q ~= S)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
disp('OK - multiplication/division: method 1 part 1, numeric');
% case 2: L symbolic constant
L = sym(L);
NL = N*L;
[Q,method] = mrdivide(NL,L);
if (Q ~= N)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
LN = L*N;
[Q,method] = mldivide(L,LN);
if (Q ~= P)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
SL = S*L;
[Q,method] = mrdivide(SL,L);
if (Q ~= S)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
LS = L*S;
[Q,method] = mldivide(L,LS);
if (Q ~= S)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
disp('OK - multiplication/division: method 1 part 1, symbolic constant');
% case 3: L symbolic variable
L(1,1) = L(1,1) + a;
NL = N*L;
[Q,method] = mrdivide(NL,L);
if (Q ~= N)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
LN = L*N;
[Q,method] = mldivide(L,LN);
if (Q ~= P)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
SL = S*L;
[Q,method] = mrdivide(SL,L);
if (Q ~= S)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
LS = L*S;
[Q,method] = mldivide(L,LS);
if (Q ~= S)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
disp('OK - multiplication/division: method 1 part 1, symbolic variable');
% matrix with nonsingular highest term
H = [1,0;0,0];
H(:,:,2) = eye(2);
H = mpoly(H);
% case 1: H numeric
NH = N*H;
[Q,method] = mrdivide(NH,H);
if (Q ~= N)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
HN = H*N;
[Q,method] = mldivide(H,HN);
if (Q ~= P)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
SH = S*H;
[Q,method] = mrdivide(SH,H);
if (Q ~= S)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
HS = H*S;
[Q,method] = mldivide(H,HS);
if (Q ~= S)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
disp('OK - multiplication/division: method 1 part 2, numeric');
% case 2: H symbolic constant
H = sym(H);
NH = N*H;
[Q,method] = mrdivide(NH,H);
if (Q ~= N)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
HN = H*N;
[Q,method] = mldivide(H,HN);
if (Q ~= P)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
SH = S*H;
[Q,method] = mrdivide(SH,H);
if (Q ~= S)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
HS = H*S;
[Q,method] = mldivide(H,HS);
if (Q ~= S)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
disp('OK - multiplication/division: method 1 part 2, symbolic constant');
% case 3: H symbolic variable
H(1,1) = H(1,1) + mpoly(a,1);
NH = N*H;
[Q,method] = mrdivide(NH,H);
if (Q ~= N)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
HN = H*N;
[Q,method] = mldivide(H,HN);
if (Q ~= P)
error(['failed - left multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
end
SH = S*H;
[Q,method] = mrdivide(SH,H);
if (Q ~= S)
error(['failed - right multiplication or division, method ',num2str(method)]);
end
if (method ~= 1)
warning('this should have been method 1');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -