0001 function [status] = testmextrdsdsmat(verbose,tol)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 if (nargin<2)
0023 tol = 1e-8;
0024 end
0025 if (nargin<1)
0026 verbose = 1;
0027 end
0028 status = 1;
0029
0030 if (verbose)
0031 disp(' testing functionality of mextrdsdsmat()...')
0032 end
0033
0034
0035 try
0036 mextrdsdsmat(1,sparse(1),1,sparse(1));
0037 catch
0038 disp(' Error: mextrdsdsmat - cannot call')
0039 status = 0;
0040 return;
0041 end
0042
0043 status = status && onematrixtest(verbose,tol,'n=0, all empty', [],[],[],[],0);
0044 status = status && onematrixtest(verbose,tol,'n=1, all ones', 1,1,1,1,2);
0045 status = status && onematrixtest(verbose,tol,'n=1, all nonzeros', 2,3,-4,5,-120*2);
0046 status = status && onematrixtest(verbose,tol,'n=1, A=0', 0,3,-4,5,0);
0047 status = status && onematrixtest(verbose,tol,'n=1, S1=0', 2,0,-4,5,0);
0048 status = status && onematrixtest(verbose,tol,'n=1, B=0', 2,3,0,5,0);
0049 status = status && onematrixtest(verbose,tol,'n=1, S2=0', 2,3,-4,0,0);
0050 status = status && onematrixtest(verbose,tol,'n=2, all numbers, A diag', diag([2 3]),[-2.1,3.2;3.2,4.4],[4,6;6,5],[-13,-9;-9,-16]);
0051
0052 status = status && onematrixtest(verbose,tol,'n=2, all numbers, A diag, B=[]', diag([2 3]),[-2.1,3.2;3.2,4.4],magic(2)+magic(2)'+3,sparse(2,2),0);
0053
0054
0055 n=5;
0056 A=diag(1:n);
0057 B=magic(n)+magic(n)';
0058 S1=sparse(n,n); S1(2,1)=2;
0059 S1=S1+S1';
0060 S2=sparse(n,n); S2(4,4)=9;
0061 status = status && onematrixtest(verbose,tol,'n=5, very sparse', A,S1,B,S2);
0062
0063 n=5;
0064 A=diag(1:n);
0065 B=magic(n)+magic(n)';
0066 S1=sparse(n,n); S1(2,1)=2;
0067 S1=S1+S1';
0068 S2=sparse(n,n); S2(2,2)=9;
0069 status = status && onematrixtest(verbose,tol,'n=5, very sparse2', A,S1,B,S2);
0070
0071 n=5;
0072 A=zeros(n,n);
0073 A(:)=[1:n^2]-10;
0074 A=A+A';
0075 B=magic(n)+magic(n)';
0076 S1=sparse(n,n); S1(2,1)=2;
0077 S1=S1+S1';
0078 S2=sparse(n,n); S2(2,2)=9;
0079 status = status && onematrixtest(verbose,tol,'n=5, very sparse3', A,S1,B,S2);
0080
0081 n=5;
0082 A=zeros(n,n);
0083 A(:)=[1:n^2]-10;
0084 A=A+A';
0085 B=magic(n)+magic(n)';
0086 S1=sparse(n,n); S1(2,1)=2;
0087 S1=S1+S1';
0088 S2=sparse(diag([5:9]));
0089 status = status && onematrixtest(verbose,tol,'n=5, sparse, S2 spdiag', A,S1,B,S2);
0090
0091
0092 for n=[5 5 5 10 10 10 100 100 101 101]
0093
0094 A=rand(n,n);
0095 A=A+A';
0096 B=rand(n,n);
0097 B=B+B';
0098 S1=sprandsym(n,0.2);
0099 S2=sprandsym(n,0.1);
0100 testname=sprintf('n=%i all random',n);
0101 status = status && onematrixtest(verbose,tol,testname, A,S1,B,S2);
0102 end
0103
0104 end
0105
0106 function [status] = onematrixtest(verbose,tol,testname,A,S1,B,S2,ref)
0107
0108
0109
0110
0111
0112 status = 1;
0113
0114 if (~issparse(S1))
0115 S1=sparse(S1);
0116 end
0117 if (~issparse(S2))
0118 S2=sparse(S2);
0119 end
0120
0121 if (nargin==7)
0122
0123 ref = trace(A*S1*B*S2) + trace(A*S2*B*S1);
0124 end
0125
0126 try
0127 result = mextrdsdsmat(A,S1,B,S2);
0128 err = abs(result-ref);
0129 catch
0130 err = [];
0131 end
0132
0133 if (isempty(err))
0134 fprintf(' test %-30s: HARD FAILED\n', testname);
0135 status = 0;
0136 elseif (err>tol*abs(ref))
0137 fprintf(' test %-30s: FAILED %15.6e %15.6e\n', testname,err,ref);
0138 status = 0;
0139 elseif (verbose)
0140 fprintf(' test %-30s: ok (tol) %15.6e %15.6e\n', testname,err,ref);
0141 end
0142
0143 end
0144