1function test6
2%TEST6 test sparse with large matrix, both real and complex
3% compare times with MATLAB
4% Example:
5%   test6
6% See also cholmod_test
7
8% Copyright 2007, Timothy A. Davis, http://www.suitesparse.com
9
10fprintf ('=================================================================\n');
11fprintf ('test6: test sparse with large matrix, both real and complex\n') ;
12
13for do_complex = 0:1
14
15    fprintf ('do_complex = %d\n', do_complex) ;
16    randn ('state', 0) ;
17    rand  ('state', 0) ;
18
19    % Prob = ssget (437)
20    Prob = ssget (749)							    %#ok
21    A = Prob.A ;
22    [m n] = size (A) ;							    %#ok
23
24    if (do_complex)
25%	A = A + 1i*sprand(A) ;
26%	patch for MATLAB 7.2
27	A = A + sparse(1:m,1:m,1i)*sprand(A) ;
28    end
29
30    tic
31    [i j x] = find (A) ;
32    t = toc ;
33    fprintf ('find time %8.4f\n', t) ;
34
35    % tic
36    % [i1 j1 x1] = cholmod_find (A) ;
37    % t = toc ;
38    % fprintf ('cholmod_find time %8.4f (for testing only, it should be slow)\n', t) ;
39
40    % if (any (i ~= i1))
41    %     error ('i!') ;
42    % end
43    % if (any (j ~= j1))
44    %     error ('j!') ;
45    % end
46    % if (any (x ~= x1))
47    %     error ('x!') ;
48    % end
49
50    [m n ] = size (A) ;
51
52    tic ;
53    B = sparse2 (i,j,x,m,n) ;
54    t1 = toc ;
55    tic ;
56    C = sparse (i,j,x,m,n) ;
57    t2 = toc ;
58
59    fprintf ('dtri time: cholmod2 %8.6f  matlab %8.6f\n', t1, t2) ;
60
61    err = norm(A-B,1) ;
62    if (err > 0)
63	error ('dtri2 1') ;
64    end
65
66    err = norm(A-C,1) ;
67    if (err > 0)
68	error ('dtri2 1') ;
69    end
70
71    nz = length (x) ;
72    p = randperm (nz) ;
73
74    i2 = i(p) ;
75    j2 = j(p) ;
76    x2 = x(p) ;							    %#ok
77
78    tic ;
79    B = sparse2 (i,j,x,m,n) ;
80    t1 = toc ;
81    tic ;
82    C = sparse (i,j,x,m,n) ;
83    t2 = toc ;
84
85    fprintf ('dtri time: cholmod2 %8.6f  matlab %8.6f (jumbled)\n', t1, t2) ;
86
87    err = norm(A-B,1) ;
88    if (err > 0)
89	error ('dtri2 2') ;
90    end
91
92    err = norm(A-C,1) ;
93    if (err > 0)
94	error ('dtri2 1') ;
95    end
96
97    ii = [i2 ; i2] ;
98    jj = [j2 ; j2] ;
99    xx = rand (2*nz,1) ;
100    if (do_complex)
101	xx = xx + 1i* rand (2*nz,1) ;
102    end
103
104    tic ;
105    D = sparse2 (ii,jj,xx,m,n) ;
106    t1 = toc ;
107    tic ;
108    C = sparse (ii,jj,xx,m,n) ;
109    t2 = toc ;
110    err = norm (C-D,1) ;
111    if (err > 0)
112	error ('dtri2 3') ;
113    end
114    fprintf ('dtri time: cholmod2 %8.6f  matlab %8.6f (duplicates)\n', t1, t2) ;
115
116    fprintf ('length %d nz %d\n', length (xx), nnz(D)) ;
117
118    i2 = min (ii,jj) ;
119    j2 = max (ii,jj) ;
120
121    tic ;
122    E = sparse2 (i2,j2,xx,n,n) ;
123    t1 = toc ;
124    tic ;
125    F = sparse (i2, j2, xx, n, n) ;
126    t2 = toc ;
127    err = norm (E-F,1)							    %#ok
128    if (err > 1e-13)
129	error ('dtri2 4') ;
130    end
131    fprintf ('dtri time: cholmod2 %8.6f  matlab %8.6f (upper)\n', t1, t2) ;
132
133    i2 = max (ii,jj) ;
134    j2 = min (ii,jj) ;
135
136    tic ;
137    E = sparse2 (i2,j2,xx,n,n) ;
138    t1 = toc ;
139    tic ;
140    F = sparse (i2, j2, xx, n, n) ;
141    t2 = toc ;
142    err = norm (E-F,1)							    %#ok
143    if (err > 1e-13)
144	error ('dtri2 5') ;
145    end
146    fprintf ('dtri time: cholmod2 %8.6f  matlab %8.6f (lower)\n', t1, t2) ;
147
148    [ignore, i] = sort (ii) ;
149    ii = ii (i) ;
150    jj = jj (i) ;
151    xx = xx (i) ;
152
153    tic ;
154    D = sparse2 (ii,jj,xx,m,n) ;
155    t1 = toc ;
156    tic ;
157    C = sparse (ii,jj,xx,m,n) ;
158    t2 = toc ;
159    err = norm (C-D,1) ;
160    if (err > 0)
161	error ('dtri2 6') ;
162    end
163    fprintf ('dtri time: cholmod2 %8.6f  matlab %8.6f (sorted, dupl)\n', t1, t2) ;
164
165end
166
167fprintf ('test6 passed\n') ;
168