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
|
function test2
%TEST2 test cs_sparse, cs_permute, cs_pvec, cs_ipvec, cs_symperm
%
% Example:
% test2
% See also: testall
% CXSparse, Copyright (c) 2006-2022, Timothy A. Davis. All Rights Reserved.
% SPDX-License-Identifier: LGPL-2.1+
rand ('state', 0)
% clf
for trial = 1:100
m = fix (10 * rand (1)) ;
n = fix (10 * rand (1)) ;
nz = fix (100 * rand (1)) ;
i = 1 + fix (m * rand (nz,1)) ;
j = 1 + fix (n * rand (nz,1)) ;
x = rand (nz,1) ;
if (~ispc)
if (mod (trial, 2) == 1)
x = x + 1i * (2*rand(nz,1)-1) ;
end
end
A = sparse (i,j,x) ;
B = cs_sparse (i,j,x) ;
D = cs_sparse2 (i,j,x) ;
fprintf ('%3d %3d %6d : %6d %6d : %d\n', ...
m, n, nz, nnz (A), nnz(B), nz-nnz(A)) ;
err = norm (A-B,1) / max (1, norm (A,1)) ;
if (err > 0)
disp ('err = ') ;
disp (err) ;
end
if (err > 1e-14)
error ('!') ;
end
if (nnz (B-D) > 0)
error ('!') ;
end
if (nnz (A) ~= nnz (B))
error ('nz!') ;
end
if (max (1,nnz (B)) ~= max (1,nzmax (B)))
nnz (B)
nzmax (B)
error ('nzmax!') ;
end
% pack
[m n] = size (A) ;
p = randperm (m) ;
q = randperm (n) ;
C1 = A (p,q) ;
C2 = cs_permute (A,p,q) ;
err = norm (C1-C2,1) ;
if (err > 0)
error ('!') ;
end
% subplot (1,2,1) ; spy (A)
% subplot (1,2,2) ; spy (C2)
% drawnow
x = rand (m,1) ;
if (~ispc)
if (mod (trial, 2) == 1)
x = x + 1i * (2*rand(m,1)-1) ;
end
end
x1 = x (p) ;
x2 = cs_pvec (x, p) ;
err = norm (x1-x2,1) ;
if (err > 0)
error ('!') ;
end
x1 = zeros (m,1) ;
x1 (p) = x ; %#ok
x2 = cs_ipvec (x, p) ; %#ok
n = min (m,n) ;
B = A (1:n, 1:n) ;
p = randperm (n) ;
B = B+B' ;
C1 = triu (B (p,p)) ;
C2 = cs_symperm (B,p) ;
try
pp = amd (C2) ; %#ok
catch
pp = symamd (C2) ; %#ok
end
err = norm (C1-C2,1) ;
if (err > 0)
error ('!') ;
end
end
|