1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
function test2 (nmat)
%test2: KLU test
% Example:
% test2
% See also klu
% KLU, Copyright (c) 2004-2022, University of Florida. All Rights Reserved.
% Authors: Timothy A. Davis and Ekanathan Palamadai.
% SPDX-License-Identifier: LGPL-2.1+
clear functions
% rand ('state', 0) ;
% warning ('off', 'MATLAB:singularMatrix') ;
% warning ('off', 'MATLAB:nearlySingularMatrix') ;
% warning ('off', 'MATLAB:divideByZero') ;
index = ssget ;
f = find (index.nrows == index.ncols) ;
[ignore i] = sort (index.nnz (f)) ; %#ok
f = f (i) ;
if (nargin < 1)
nmat = 500 ;
end
nmat = min (nmat, length (f)) ;
f = f (1:nmat) ;
if (~isempty (strfind (computer, '64')))
is64 = 1 ;
else
is64 = 0 ;
end
Tklu = 1e-6 * ones (2*nmat,1) ;
Tmatlab = zeros (2*nmat,1) ;
Tcsparse = zeros (2*nmat,1) ;
LUnz = zeros (2*nmat, 1) ;
k = 0 ;
h = waitbar (0, 'KLU test 2 of 5') ;
clf
% try
for kk = 1:nmat
Prob = ssget (f (kk), index) ;
waitbar (kk/nmat, h) ;
disp (Prob) ;
if (isfield (Prob, 'kind'))
if (~isempty (strfind (Prob.kind, 'subsequent')))
fprintf ('skip ...\n') ;
continue
end
end
A = Prob.A ;
for do_complex = 0:1
k = k + 1 ;
if (do_complex)
A = sprand (A) + 1i * sprand (A) ;
end
try
[L,U,p,q] = lu (A, 'vector') ;
catch %#ok
% older version of MATLAB, which doesn't have 'vector' option
[L,U,P,Q] = lu (A) ;
[p ignore1 ignore2] = find (P') ; %#ok
[q ignore1 ignore2] = find (Q) ; %#ok
clear ignore1 ignore2 P Q
end
LU.L = L ;
LU.U = U ;
if (is64)
LU.p = int64 (p) ;
LU.q = int64 (q) ;
else
LU.p = int32 (p) ;
LU.q = int32 (q) ;
end
LUnz (k) = nnz (L) + nnz (U) ;
n = size (A,1) ;
do_klu = (nnz (diag (U)) == n) ;
if (do_klu)
fprintf ('klu...\n') ;
err = 0 ;
erc = 0 ;
er2 = 0 ;
for nrhs = 10:-1:1
b = rand (n,nrhs) ;
tic ;
x = klu (LU,'\',b) ;
Tklu (k) = max (1e-6, toc) ;
tic ;
y = U \ (L \ b (p,:)) ;
y (q,:) = y ;
Tmatlab (k) = max (1e-6, toc) ;
if (nrhs == 1 & isreal (U) & isreal (L) & isreal (b)) %#ok
tic ;
z = cs_usolve (U, cs_lsolve (L, b (p))) ;
z (q) = z ;
Tcsparse (k) = max (1e-6, toc) ;
erc = norm (A*z-b,1) / norm (A,1) ;
end
err = max (err, norm (A*x-b,1) / norm (A,1)) ;
er2 = max (er2, norm (A*y-b,1) / norm (A,1)) ;
if (err > 100*er2)
fprintf ('error %g %g\n', err, er2) ;
error ('?') ;
end
end
fprintf ('klu... with randomized scaling for L\n') ;
er3 = 0 ;
er4 = 0 ;
D = spdiags (rand (n,1), 0, n, n) ;
LU.L = D * L ;
A2 = D * A (p,q) ;
if (is64)
LU.p = int64 (1:n) ;
LU.q = int64 (1:n) ;
else
LU.p = int32 (1:n) ;
LU.q = int32 (1:n) ;
end
for nrhs = 1:10
b = rand (n,nrhs) ;
x = klu (LU,'\',b) ;
y = U \ (LU.L \ b) ;
er3 = max (er3, norm (A2*x-b,1) / norm (A,1)) ;
er4 = max (er4, norm (A2*y-b,1) / norm (A,1)) ;
if (er3 > 1e3*er4)
fprintf ('error %g %g\n', er3, er4) ;
error ('?') ;
end
end
else
err = Inf ;
er2 = Inf ;
erc = Inf ;
end
lumax = max (LUnz (1:k)) ;
if (k > 1)
loglog (...
LUnz (1:k), Tmatlab (1:k) ./ Tklu (1:k), 'o', ...
LUnz (1:k), Tcsparse (1:k) ./ Tklu (1:k), 'x', ...
[20 lumax], [1 1], 'r-') ;
axis ([sort([20 lumax]) .1 20]) ;
drawnow
end
fprintf ('err %g %g %g\n', err, er2, erc) ;
end
end
% catch me
% disp (me.message) ;
% end
close (h) ;
|