1function test29
2%TEST29 test spsym
3% Example:
4%   spsym
5% See also cholmod_test
6
7% Copyright 2015, Timothy A. Davis, http://www.suitesparse.com
8
9rand ('state', 0) ;
10
11r = zeros (0,2) ;
12for n = 0:5
13
14    % real unsymmetric, diagonal all nonzero
15    A = sparse (rand (n,n)) ;
16    r = [r ; test_spsym(A)] ;
17
18    % real symmetric, diagonal all nonzero
19    A = A+A' ;
20    r = [r ; test_spsym(A)] ;
21
22    % real unsymmetric, diagonal all nonzero
23    A (2,1) = 0 ;
24    r = [r ; test_spsym(A)] ;
25
26    % real symmetric, diagonal all zero
27    A = sparse (n,n) ;
28    r = [r ; test_spsym(A)] ;
29
30    % real symmetric, diagonal all nonzero
31    A = speye (n,n) ;
32    r = [r ; test_spsym(A)] ;
33
34    % real symmetric, diagonal mostly nonzero
35    A (2,2) = 0 ;
36    r = [r ; test_spsym(A)] ;
37
38    % real rectangular, or square unsymmetric
39    for m = 0:5
40        A = sparse (rand (n,m)) ;
41        r = [r ; test_spsym(A)] ;
42    end
43
44    % skew symmetric when n > 1
45    A = tril (sparse (rand (n,n)), -1) ;
46    A = A-A' ;
47    c = test_spsym(A) ;
48    r = [r ; c ] ;
49
50    % complex Hermitian (when n > 1)
51    A = sparse (rand (n,n)) + 1i * sparse (rand (n,n)) ;
52    A = A+A' ;
53    c = test_spsym(A) ;
54    r = [r ; c ] ;
55
56    % complex Hermitian but with non-positive diagonal (when n > 1)
57    A (3,3) = -1 ;
58    c = test_spsym(A) ;
59    r = [r ; c] ;
60
61end
62
63r = unique (r, 'rows') ;
64rtrue =  [
65     1     1
66     2     2
67     3     2
68     4     2
69     5     2
70     6     6
71     7     7 ] ;
72if (~isequal (r, rtrue))
73    error ('failed.  Incomplete test cases') ;
74end
75
76% test with the UF sparse matrix collection
77r = zeros (0,2) ;
78index = ssget ;
79for i = [168 27 2137 56 231 1621 -1621] ;
80    Prob = ssget (abs (i),index)
81    A = Prob.A ;
82    if (i < 0)
83        % UF collection does not contain any matrices for which spsym(A) = 4.
84        % (complex Hermitian with zero nonpos. diagonal).  So make one.
85        fprintf ('setting A (5,5) = 0\n') ;
86        A (5,5) = 0 ;
87    end
88    c = test_spsym (A) ;
89    c
90    fprintf ('full  test:') ; print_result (c (1)) ;
91    fprintf ('quick test:') ; print_result (c (2)) ;
92    r = [r ; c] ;
93end
94
95r = unique (r, 'rows') ;
96if (~isequal (r, rtrue))
97    error ('failed.  Incomplete test cases') ;
98end
99
100%-------------------------------------------------------------------------------
101
102function r = test_spsym (A)
103s1 = spsym (A) ;
104s2 = get_symmetry (A) ;
105if (s1 ~= s2)
106    error ('failed!')
107end
108s3 = spsym (A,0) ;
109s4 = get_symmetry (A,0) ;
110if (s3 ~= s1 || s4 ~= s1)
111    error ('failed!')
112end
113s5 = spsym (A,1) ;
114s6 = get_symmetry (A,1) ;
115if (s5 ~= s6)
116    error ('failed!')
117end
118r = [s1 s5] ;   % r(1) is the full test, r(2) is the quick test
119
120%-------------------------------------------------------------------------------
121
122function print_result (s)
123switch (s)
124case 1
125    fprintf ('rectangular\n') ;
126case 2
127    fprintf ('unsymmetric (or not Cholesky candidate for quick test)\n') ;
128case 3
129    fprintf ('symmetric, but with one or more A(j,j) <= 0\n') ;
130case 4
131    fprintf ('Hermitian, but with one or more A(j,j) <= 0 or with nonzero imaginary part\n') ;
132case 5
133    fprintf ('skew symmetric\n') ;
134case 6
135    fprintf ('symmetric with real positive diagonal\n') ;
136case 7
137    fprintf ('Hermitian with real positive diagonal\n') ;
138otherwise
139    error ('unknown result') ;
140end
141
142