2
%CCOLAMD_TEST extensive test of ccolamd and csymamd
7
% See also csymamd, ccolamd, ccolamd_make.
9
% Copyright 1998-2007, Timothy A. Davis, Stefan Larimore, and Siva Rajamanickam
10
% Developed in collaboration with J. Gilbert and E. Ng.
14
global ccolamd_default_knobs csymamd_default_knobs
15
ccolamd_default_knobs = [0 10 10 1 0] ;
16
csymamd_default_knobs = [10 1 0] ;
19
'Compile ccolamd, csymand, and the test codes? (y/n, default is yes): ', 's') ;
23
if (s (1) == 'n' | s (1) == 'N') %#ok
29
fprintf ('Compiling ccolamd, csymamd, and test mexFunctions.\n') ;
33
if (~isempty (strfind (computer, '64')))
34
d = '-largeArrayDims' ;
36
src = '../Source/ccolamd.c ../Source/ccolamd_global.c' ;
37
cmd = sprintf ('mex -DDLONG -O %s -I../../UFconfig -I../Include ', d) ;
38
eval ([cmd 'ccolamdtestmex.c ' src]) ;
39
eval ([cmd 'csymamdtestmex.c ' src]) ;
40
fprintf ('Done compiling.\n') ;
44
fprintf ('\nThe following codes will be tested:\n') ;
50
fprintf ('\nStarting the tests. Please be patient.\n') ;
55
A = sprandn (500,500,0.4) ;
57
p = ccolamd (A, [0 10 10 1 1]) ; check_perm (p, A) ;
58
p = ccolamd (A, [1 2 7 1 1]) ; check_perm (p, A) ;
59
p = ccolamd (A, [1 2 10 0 1]) ; check_perm (p, A) ;
60
p = ccolamd (A, [9 2 3 1 1]) ; check_perm (p, A) ;
62
p = csymamd (A, [10 1 1]) ; check_perm (p, A) ;
63
p = csymamd (A, [4 1 1]) ; check_perm (p, A) ;
64
p = csymamd (A, [9 0 1]) ; check_perm (p, A) ;
66
fprintf ('Null matrices') ;
88
fprintf ('Matrices with a few dense row/cols\n') ;
91
% random square unsymmetric matrix
92
A = rand_matrix (1000, 1000, 1, 10, 20) ;
95
cmember = irand (min (trial,n), n) ;
97
for tol = [0:.1:2 3:20 1e6]
100
p = ccolamd (A, [ ]) ; check_perm (p, A) ;
101
p = ccolamd (A, [1 tol tol 1]) ; check_perm (p, A) ;
102
p = ccolamd (A, [0 tol tol 1]) ; check_perm (p, A) ;
103
p = ccolamd (A, [1 tol tol 0]) ; check_perm (p, A) ;
104
p = ccolamd (A, [0 tol tol 1]) ; check_perm (p, A) ;
105
p = csymamd (A, [tol 1]) ; check_perm (p, A) ;
106
p = csymamd (A, tol) ; check_perm (p, A) ;
107
p = csymamd (A, [ ]) ; check_perm (p, A) ;
108
p = csymamd (B, [tol 0]) ; check_perm (p, A) ;
109
p = ccolamd (A, [0 tol -1 1]) ; check_perm (p, A) ;
110
p = ccolamd (A, [0 -1 tol 1]) ; check_perm (p, A) ;
112
% check with non-null cmember
114
p = ccolamd (A, [ ], cmember) ; check_perm (p, A) ;
115
p = ccolamd (A, [1 tol tol 1], cmember) ; check_perm (p, A) ;
116
p = ccolamd (A, [0 tol tol 1], cmember) ; check_perm (p, A) ;
117
p = ccolamd (A, [1 tol tol 0], cmember) ; check_perm (p, A) ;
118
p = ccolamd (A, [0 tol tol 1], cmember) ; check_perm (p, A) ;
119
p = csymamd (A, [tol 1], cmember) ; check_perm (p, A) ;
120
p = csymamd (A, tol, cmember) ; check_perm (p, A) ;
121
p = csymamd (A, [ ], cmember) ; check_perm (p, A) ;
122
p = csymamd (B, [tol 0], cmember) ; check_perm (p, A) ;
123
p = ccolamd (A, [0 tol -1 1], cmember) ; check_perm (p, A) ;
124
p = ccolamd (A, [0 -1 tol 1], cmember) ; check_perm (p, A) ;
126
p = ccolamd (A, [ ], [ ]) ; check_perm (p, A) ;
127
p = ccolamd (A, [1 tol tol 1], [ ]) ; check_perm (p, A) ;
128
p = ccolamd (A, [0 tol tol 1], [ ]) ; check_perm (p, A) ;
129
p = ccolamd (A, [1 tol tol 0], [ ]) ; check_perm (p, A) ;
130
p = ccolamd (A, [0 tol tol 1], [ ]) ; check_perm (p, A) ;
131
p = csymamd (A, [tol 1], [ ]) ; check_perm (p, A) ;
132
p = csymamd (A, tol, [ ]) ; check_perm (p, A) ;
133
p = csymamd (A, [ ], [ ]) ; check_perm (p, A) ;
134
p = csymamd (B, [tol 0], [ ]) ; check_perm (p, A) ;
135
p = ccolamd (A, [0 tol -1 1], [ ]) ; check_perm (p, A) ;
136
p = ccolamd (A, [0 -1 tol 1], [ ]) ; check_perm (p, A) ;
144
fprintf ('General matrices\n') ;
147
% matrix of random mtype
149
A = rand_matrix (2000, 2000, mtype, 0, 0) ;
164
fprintf ('Test error handling with invalid inputs\n') ;
166
% Check different erroneous input.
169
A = rand_matrix (1000, 1000, 2, 0, 0) ;
173
p = Tcolamd (A, [ccolamd_default_knobs 1 err], [ ]) ;
179
% check different (valid) input args to ccolamd
181
p2 = Acolamd (A, [ccolamd_default_knobs 0 0]) ;
183
error ('ccolamd: mismatch 1!') ;
188
p = Tsymamd (B, [-1 1 0 err], [ ]) ;
195
% check different (valid) input args to csymamd
198
p2 = Asymamd (B, [csymamd_default_knobs 0]) ;
200
error ('symamd: mismatch 1!') ;
210
fprintf ('Matrices with a few empty columns\n') ;
214
% some are square, some are rectangular
217
A = rand_matrix (1000, 1000, irand (2), 0, 0) ;
221
% Add 5 null columns at random locations.
222
null_col = randperm (n) ;
223
A (:, null_col) = 0 ;
225
% Order the matrix and make sure that the null columns are ordered last.
226
p = ccolamd (A, [1 1e6 1e6 0]) ;
229
% find all null columns in A
230
null_col = find (sum (spones (A), 1) == 0) ;
231
nnull = length (null_col) ;
232
if (any (null_col ~= p ((n-nnull+1):n)))
233
error ('ccolamd: Null cols are not ordered last in natural order') ;
241
fprintf ('Matrices with a few empty rows and columns\n') ;
248
A = rand_matrix (1000, 1000, 3, 0, 0) ;
252
% Add 5 null columns and rows at random locations.
253
null_col = randperm (n) ;
254
A (:, null_col) = 0 ;
255
A (null_col, :) = 0 ;
257
% Order the matrix and make sure that the null rows/cols are ordered last.
258
p = csymamd (A, -1) ;
261
% find all null rows/columns in A
264
find ((sum (spones (Alo), 1) == 0) & (sum (spones (Alo), 2) == 0)') ;
265
nnull = length (null_col) ;
266
if (any (null_col ~= p ((n-nnull+1):n)))
267
error ('csymamd: Null cols are not ordered last in natural order') ;
275
fprintf ('Matrices with a few empty rows\n') ;
277
% Test matrices with null rows inserted.
283
A = rand_matrix (1000, 1000, 2, 0, 0) ;
287
% Add 5 null rows at random locations.
288
null_row = randperm (m) ;
289
null_row = sort (null_row (1:5)) ;
290
A (null_row, :) = 0 ;
298
fprintf ('\nccolamd and csymamd: all tests passed\n\n') ;
300
%-------------------------------------------------------------------------------
302
function p = Acolamd (S, knobs)
303
% Acolamd: compare ccolamd and Tcolamd results
305
global ccolamd_default_knobs
309
p1 = Tcolamd (S, [ccolamd_default_knobs 0 0], [ ]) ;
311
p = ccolamd (S, knobs) ;
312
p1 = Tcolamd (S, knobs, [ ]) ;
321
save bad S narg knobs
325
error ('Acolamd mismatch!') ;
328
%-------------------------------------------------------------------------------
330
function p = Asymamd (S, knobs)
331
% Asymamd: compare csymamd and Tsymamd results
333
global csymamd_default_knobs
337
p1 = Tsymamd (S, [csymamd_default_knobs 0], [ ]) ;
339
p = csymamd (S, knobs) ;
340
p1 = Tsymamd (S, knobs, [ ]) ;
344
error ('Asymamd mismatch!') ;
348
%-------------------------------------------------------------------------------
350
function check_perm (p, A, cmember)
351
% check_perm: check for a valid permutation vector
353
if (isempty (A) & isempty (p)) %#ok
354
% empty permutation vectors of empty matrices are OK
359
error ('Bad permutation: cannot be empty') ;
363
[p_m p_n] = size (p) ;
365
% force p to be a row vector
367
[p_m p_n] = size (p) ;
371
error ('Bad permutation: wrong size') ;
376
error ('Bad permutation: not a vector') ;
378
if (any (sort (p) - (1:p_n)))
379
error ('Bad permutation') ;
386
% c must be monotonically non-decreasing
389
error ('permutation breaks the cmember constraints') ;
393
%-------------------------------------------------------------------------------
395
function i = irand (n,s)
396
% irand: return a random vector of size s, with values between 1 and n
400
i = min (n, 1 + floor (rand (1,s) * n)) ;
402
%-------------------------------------------------------------------------------
404
function A = rand_matrix (n_max, m_max, mtype, d_rows, d_cols)
405
% rand_matrix: return a random sparse matrix
407
% A = rand_matrix (n_max, m_max, mtype, d_rows, d_cols)
409
% A binary matrix of random size, at most n_max-by-m_max, with d_rows dense rows
410
% and d_cols dense columns.
412
% mtype 1: square unsymmetric (m_max is ignored)
413
% mtype 2: rectangular
414
% mtype 3: symmetric (m_max is ignored)
424
A = sprand (m, n, 10 / max (m,n)) ;
450
% ensure that there are no empty columns
451
d = find (full (sum (A,1)) == 0) ; %#ok
454
% ensure that there are no empty rows
455
d = find (full (sum (A,2)) == 0) ; %#ok
460
A = A + A' + speye (n) ;
465
%-------------------------------------------------------------------------------
466
% Tcolamd: run ccolamd in a testing mode
467
%-------------------------------------------------------------------------------
469
function p = Tcolamd (S, knobs, cmember)
472
p = ccolamdtestmex (S, knobs, cmember) ;
479
%-------------------------------------------------------------------------------
480
% Tsymamd: run csymamd in a testing mode
481
%-------------------------------------------------------------------------------
483
function p = Tsymamd (S, knobs, cmember)
486
p = csymamdtestmex (S, knobs, cmember) ;