5590
|
1 #!/bin/sh |
|
2 |
7019
|
3 # Copyright (C) 2006, 2007 David Bateman |
|
4 # |
|
5 # This file is part of Octave. |
|
6 # |
|
7 # Octave is free software; you can redistribute it and/or modify it |
|
8 # under the terms of the GNU General Public License as published by the |
|
9 # Free Software Foundation; either version 3 of the License, or (at |
|
10 # your option) any later version. |
|
11 # |
|
12 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 # for more details. |
|
16 # |
|
17 # You should have received a copy of the GNU General Public License |
|
18 # along with Octave; see the file COPYING. If not, see |
|
19 # <http://www.gnu.org/licenses/>. |
|
20 |
5590
|
21 # Some tests are commented out because they are known to be broken! |
|
22 # Search for "# fails" |
|
23 |
|
24 # ./buildtest.sh preset |
|
25 # creates test_sparse.m with preset tests. |
|
26 # Use "test test_sparse" from octave to run the tests. |
|
27 # |
|
28 # ./buildtest.sh random |
|
29 # Creates test_sprandom.m with randomly generated matrices. |
|
30 |
|
31 # buildtest.sh generates tests for real and complex sparse matrices. |
|
32 # Also, we want to run both fixed tests with known outputs (quick tests) |
|
33 # and longer tests with unknown outputs (thorough tests). This requires |
|
34 # two sets of tests --- one which uses preset matrices and another which |
|
35 # uses randomly generated matrices. |
|
36 # |
|
37 # The tests are mostly identical for each case but the code is different, |
|
38 # so it is important that the tests be run on all cases. Because our test |
|
39 # harness doesn't have support for looping or macros (it is only needed |
|
40 # for new data types), but sh does, we use sh to generate inline versions of |
|
41 # the tests for each case. |
|
42 # |
|
43 # Our 'macros' use shared variables as parameters. This allows us to |
|
44 # for example define A complex and include all the unary ops tests, |
|
45 # then set A=real(A) and include all the unary ops tests. Thus the |
|
46 # same tests work for real and complex. For binary tests it is even |
|
47 # more complicated because we want full X sparse, sparse X full and |
|
48 # sparse X sparse tested. |
|
49 # |
|
50 # We use the following macros: |
|
51 # |
|
52 # gen_section |
|
53 # place a separator in the test file |
|
54 # gen_function |
|
55 # define the function definion |
|
56 # helper gen_specific |
|
57 # specific tests such as error handling and null input |
|
58 # helper gen_eat_zeros |
|
59 # make sure sparse-scalar ops which generate 0 work |
|
60 # gen_specific_tests |
|
61 # specific and eat zeros tests |
|
62 # helper gen_ordering_tests |
|
63 # ordered comparison operators for real valued tests |
|
64 # helper gen_sparsesparse_ordering_tests |
|
65 # ordered comparison operators for real valued sparse-sparse tests |
|
66 # helper gen_elementop_tests |
|
67 # element-wise matrix binary operators, including scalar-matrix ops. |
|
68 # horizontal/vertical concatenation are here as well. |
|
69 # helper gen_sparsesparse_elementop_tests |
|
70 # element-wise matrix binary operators, for sparse-sparse ops. |
|
71 # horizontal/vertical concatenation are here as well. |
|
72 # helper gen_divop_tests |
|
73 # left and right matrix division operators of rectangular matrices. |
|
74 # Needs QR solvers |
|
75 # helper gen_square_divop_tests |
|
76 # left and right matrix division operators of square matrices. |
|
77 # helper gen_matrixop_tests |
|
78 # rectangular matrix binary operators: * |
|
79 # helper gen_matrixdiag_tests |
|
80 # Tests extract of diag and creation of diagonal matrices using |
|
81 # diag and spdiags functions |
|
82 # helper gen_matrixreshape_tests |
|
83 # Test the reshape function on sparse matrices |
|
84 # helper print_mapper_test |
|
85 # sub-helper function of gen_mapper_tests to print individual tests |
|
86 # helper gen_mapper_tests |
|
87 # Tests all of the one argument mapper functions. There are a few |
|
88 # specific tests that abs, real and imag return real values. |
|
89 # helper gen_unaryop_tests |
|
90 # functions and operators which transform a single matrix |
|
91 # helper gen_save_tests |
|
92 # Tests the load/save functionality for ascii/binary and hdf5 formats |
|
93 # gen_scalar_tests |
|
94 # element ops for real and complex scalar and sparse |
|
95 # gen_rectangular_tests |
|
96 # unary, element, and matrix tests for a and full/sparse b |
|
97 # gen_square_tests |
|
98 # operations which require square matrices: lu, inv, \ |
|
99 # A square non-singular matrix is defined from the rectangular |
|
100 # inputs A and B. |
|
101 # gen_assembly_tests |
|
102 # test for sparse constructors with 'sum' vs. 'unique' |
|
103 # gen_select_tests |
|
104 # indexing tests |
|
105 # gen_solver_tests |
|
106 # Tests the solve function with triangular/banded, etc matrices |
|
107 |
|
108 case $1 in |
|
109 random) preset=false ;; |
|
110 preset) preset=true ;; |
|
111 '') preset=true ;; |
|
112 *) echo "buildtest.sh random|preset" && exit 1 ;; |
|
113 esac |
|
114 |
|
115 if $preset; then |
|
116 TESTS=test_sparse.m |
|
117 else |
|
118 TESTS=test_sprandom.m |
|
119 fi |
|
120 |
|
121 # create initial file |
|
122 cat >$TESTS <<EOF |
|
123 ## THIS IS AN AUTOMATICALLY GENERATED FILE --- DO NOT EDIT --- |
|
124 ## instead modify build_sparse_tests.sh to generate the tests that you want. |
|
125 EOF |
|
126 |
|
127 |
|
128 # define all functions |
|
129 |
|
130 |
|
131 # ======================================================= |
|
132 # Section separator |
|
133 |
|
134 gen_section() { |
|
135 cat >>$TESTS <<EOF |
|
136 |
|
137 # ============================================================== |
|
138 |
|
139 EOF |
|
140 } |
|
141 |
|
142 |
|
143 # ======================================================= |
|
144 # Specific preset tests |
|
145 |
|
146 # ======================================================= |
|
147 # If a sparse operation yields zeros, then those elements |
|
148 # of the returned sparse matrix should be eaten. |
|
149 gen_eat_zeros() { |
|
150 cat >>$TESTS <<EOF |
|
151 %% Make sure newly introduced zeros get eaten |
|
152 %!assert(nnz(sparse([bf,bf,1]).^realmax),1); |
|
153 %!assert(nnz(sparse([1,bf,bf]).^realmax),1); |
|
154 %!assert(nnz(sparse([bf,bf,bf]).^realmax),0); |
|
155 |
|
156 %!assert(nnz(sparse([bf;bf;1]).^realmax),1); |
|
157 %!assert(nnz(sparse([1;bf;bf]).^realmax),1); |
|
158 %!assert(nnz(sparse([0.5;bf;bf]).^realmax),0); |
|
159 |
|
160 %!assert(nnz(sparse([bf,bf,1])*realmin),1); |
|
161 %!assert(nnz(sparse([1,bf,bf])*realmin),1); |
|
162 %!assert(nnz(sparse([bf,bf,bf])*realmin),0); |
|
163 |
|
164 %!assert(nnz(sparse([bf;bf;1])*realmin),1); |
|
165 %!assert(nnz(sparse([1;bf;bf])*realmin),1); |
|
166 %!assert(nnz(sparse([bf;bf;bf])*realmin),0); |
|
167 |
|
168 EOF |
|
169 } |
|
170 |
|
171 gen_specific() { |
|
172 cat >>$TESTS <<EOF |
|
173 |
|
174 %!test # segfault test from edd@debian.org |
|
175 %! n = 510; |
|
176 %! sparse(kron((1:n)', ones(n,1)), kron(ones(n,1), (1:n)'), ones(n)); |
|
177 |
|
178 %% segfault tests from Fabian@isas-berlin.de |
|
179 %% Note that the last four do not fail, but rather give a warning |
|
180 %% of a singular matrix, which is consistent with the full matrix |
|
181 %% behaviour. They are therefore disabled.. |
7243
|
182 %!testif HAVE_UMFPACK |
|
183 %! assert(spinv(sparse([1,1;1,1+i])),sparse([1-1i,1i;1i,-1i]),10*eps); |
5590
|
184 % !error spinv( sparse( [1,1;1,1] ) ); |
|
185 % !error spinv( sparse( [0,0;0,1] ) ); |
|
186 % !error spinv( sparse( [0,0;0,1+i] ) ); |
|
187 % !error spinv( sparse( [0,0;0,0] ) ); |
|
188 |
|
189 %% error handling in constructor |
|
190 %!error sparse(1,[2,3],[1,2,3]); |
|
191 %!error sparse([1,1],[1,1],[1,2],3,3,"bogus"); |
|
192 %!error sparse([1,3],[1,-4],[3,5],2,2); |
|
193 %!error sparse([1,3],[1,-4],[3,5i],2,2); |
|
194 %!error sparse(-1,-1,1); |
|
195 EOF |
|
196 } |
|
197 |
|
198 |
|
199 gen_specific_tests() { |
|
200 gen_section |
|
201 gen_specific |
|
202 gen_section |
|
203 echo '%!shared bf' >> $TESTS |
|
204 echo '%!test bf=realmin;' >> $TESTS |
|
205 gen_eat_zeros |
|
206 echo '%!test bf=realmin+realmin*1i;' >> $TESTS |
|
207 gen_eat_zeros |
|
208 cat >>$TESTS <<EOF |
|
209 %!assert(nnz(sparse([-1,realmin,realmin]).^1.5),1); |
|
210 %!assert(nnz(sparse([-1,realmin,realmin,1]).^1.5),2); |
|
211 |
|
212 %!assert(nnz(sparse(1,1,0)),0); # Make sure scalar v==0 doesn't confuse matters |
|
213 %!assert(nnz(sparse(eye(3))*0),0); |
|
214 %!assert(nnz(sparse(eye(3))-sparse(eye(3))),0); |
|
215 |
|
216 %!test |
5781
|
217 %! wdbz = warning ("query", "Octave:divide-by-zero"); |
|
218 %! warning ("off", "Octave:divide-by-zero"); |
7197
|
219 %! assert(full(sparse(eye(3))/0),eye(3)/0); |
5781
|
220 %! warning (wdbz.state, "Octave:divide-by-zero"); |
5590
|
221 |
|
222 EOF |
|
223 } |
|
224 |
|
225 |
|
226 # ======================================================= |
|
227 # Main function definition |
|
228 |
|
229 gen_function() { |
|
230 if $preset; then |
|
231 cat >>$TESTS <<EOF |
|
232 ## |
|
233 ## test_sparse |
|
234 ## |
|
235 ## run preset sparse tests. All should pass. |
|
236 function [passes,tests] = test_sparse |
|
237 disp("writing test output to sptest.log"); |
|
238 test("test_sparse","normal","sptest.log"); |
|
239 endfunction |
|
240 |
|
241 EOF |
|
242 else |
|
243 cat >>$TESTS <<EOF |
|
244 ## |
|
245 ## test_sprandom |
|
246 ## |
|
247 ## total_passes=0; total_tests=0; |
|
248 ## for i=1:10 |
|
249 ## [passes,tests] = sprandomtest; |
|
250 ## total_passes += passes; |
|
251 ## total_tests += tests; |
|
252 ## end |
|
253 ## The test log is appended to sprandomtest.log |
|
254 function [passes,total] = test_sprandom |
|
255 warning("untested --- fix the source in buildtests.sh"); |
|
256 disp("appending test output to sprandomtest.log"); |
|
257 fid = fopen("sprandomtest.log","at"); |
|
258 test("test_sprandom","normal",fid); |
|
259 ##[passes, total] = test("sprandomtest","normal",fid); |
|
260 fclose(fid); |
|
261 endfunction |
|
262 |
|
263 EOF |
|
264 fi |
|
265 |
|
266 } |
|
267 |
|
268 |
|
269 # ======================================================= |
|
270 # matrix ops |
|
271 |
|
272 # test ordered comparisons: uses as,af,bs,bf |
|
273 gen_ordering_tests() { |
|
274 cat >>$TESTS <<EOF |
|
275 %% real values can be ordered (uses as,af) |
|
276 %!assert(as<=bf,sparse(af<=bf,true)) |
|
277 %!assert(bf<=as,sparse(bf<=af,true)) |
|
278 |
|
279 %!assert(as>=bf,sparse(af>=bf,true)) |
|
280 %!assert(bf>=as,sparse(bf>=af,true)) |
|
281 |
|
282 %!assert(as<bf,sparse(af<bf,true)) |
|
283 %!assert(bf<as,sparse(bf<af,true)) |
|
284 |
|
285 %!assert(as>bf,sparse(af>bf,true)) |
|
286 %!assert(bf>as,sparse(bf>af,true)) |
|
287 |
|
288 EOF |
|
289 } |
|
290 |
|
291 gen_sparsesparse_ordering_tests() { |
|
292 cat >>$TESTS <<EOF |
|
293 %!assert(as<=bs,sparse(af<=bf,true)) |
|
294 %!assert(as>=bs,sparse(af>=bf,true)) |
|
295 %!assert(as<bs,sparse(af<bf,true)) |
|
296 %!assert(as>bs,sparse(af>bf,true)) |
|
297 EOF |
|
298 } |
|
299 |
|
300 # test element-wise binary operations: uses as,af,bs,bf,scalar |
|
301 gen_elementop_tests() { |
|
302 cat >>$TESTS <<EOF |
|
303 %% Elementwise binary tests (uses as,af,bs,bf,scalar) |
|
304 %!assert(as==bs,sparse(af==bf,true)) |
|
305 %!assert(bf==as,sparse(bf==af,true)) |
|
306 |
|
307 %!assert(as!=bf,sparse(af!=bf,true)) |
|
308 %!assert(bf!=as,sparse(bf!=af,true)) |
|
309 |
|
310 %!assert(as+bf,af+bf) |
|
311 %!assert(bf+as,bf+af) |
|
312 |
|
313 %!assert(as-bf,af-bf) |
|
314 %!assert(bf-as,bf-af) |
|
315 |
|
316 %!assert(as.*bf,sparse(af.*bf,true)) |
|
317 %!assert(bf.*as,sparse(bf.*af,true)) |
|
318 |
|
319 %!assert(as./bf,sparse(af./bf,true),100*eps) |
|
320 %!assert(bf.\as,sparse(bf.\af,true),100*eps) |
|
321 |
|
322 %!test |
|
323 %! sv = as.^bf; |
|
324 %! fv = af.^bf; |
|
325 %! idx = find(af~=0); |
|
326 %! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps) |
|
327 |
|
328 EOF |
|
329 } |
|
330 |
|
331 gen_sparsesparse_elementop_tests() { |
|
332 cat >>$TESTS <<EOF |
|
333 %!assert(as==bs,sparse(af==bf,true)) |
|
334 %!assert(as!=bs,sparse(af!=bf,true)) |
|
335 %!assert(as+bs,sparse(af+bf,true)) |
|
336 %!assert(as-bs,sparse(af-bf,true)) |
|
337 %!assert(as.*bs,sparse(af.*bf,true)) |
7055
|
338 %!xtest assert(as./bs,sparse(af./bf,true),100*eps); |
5590
|
339 %!test |
|
340 %! sv = as.^bs; |
|
341 %! fv = af.^bf; |
|
342 %! idx = find(af~=0); |
|
343 %! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps) |
|
344 |
|
345 EOF |
|
346 } |
|
347 |
|
348 # test matrix-matrix left and right division: uses as,af,bs,bf |
|
349 gen_divop_tests() { |
|
350 cat >>$TESTS <<EOF |
|
351 %% Matrix-matrix operators (uses af,as,bs,bf) |
|
352 %!assert(as/bf,af/bf,100*eps) |
|
353 %!assert(af/bs,af/bf,100*eps) |
|
354 %!assert(as/bs,sparse(af/bf,true),100*eps) |
|
355 %!assert(bs\af',bf\af',100*eps) |
|
356 %!assert(bf\as',bf\af',100*eps) |
|
357 %!assert(bs\as',sparse(bf\af',true),100*eps) |
|
358 |
|
359 EOF |
|
360 } |
|
361 |
|
362 # test matrix-matrix left and right division: uses as,af,bs,bf |
|
363 gen_square_divop_tests() { |
|
364 cat >>$TESTS <<EOF |
|
365 %% Matrix-matrix operators (uses af,as,bs,bf) |
|
366 %!assert(as/bf,af/bf,100*eps) |
|
367 %!assert(af/bs,af/bf,100*eps) |
|
368 %!assert(as/bs,sparse(af/bf,true),100*eps) |
|
369 %!assert(bs\af',bf\af',100*eps) |
|
370 %!assert(bf\as',bf\af',100*eps) |
|
371 %!assert(bs\as',sparse(bf\af',true),100*eps) |
|
372 |
|
373 EOF |
|
374 } |
|
375 |
|
376 # test matrix-matrix operations: uses as,af,bs,bf |
|
377 gen_matrixop_tests() { |
|
378 cat >>$TESTS <<EOF |
|
379 %% Matrix-matrix operators (uses af,as,bs,bf) |
|
380 %!assert(as*bf',af*bf') |
|
381 %!assert(af*bs',af*bf') |
|
382 %!assert(as*bs',sparse(af*bf',true)) |
|
383 |
|
384 EOF |
|
385 } |
|
386 |
|
387 # test diagonal operations |
|
388 gen_matrixdiag_tests() { |
|
389 cat >>$TESTS <<EOF |
|
390 %% Matrix diagonal tests (uses af,as,bf,bs) |
|
391 %!assert(spdiag(as),sparse(diag(af),true)) |
|
392 %!assert(spdiag(bs),sparse(diag(bf),true)) |
|
393 %!assert(spdiag(as,1),sparse(diag(af,1),true)) |
|
394 %!assert(spdiag(bs,1),sparse(diag(bf,1),true)) |
|
395 %!assert(spdiag(as,-1),sparse(diag(af,-1),true)) |
|
396 %!assert(spdiag(bs,-1),sparse(diag(bf,-1),true)) |
|
397 %!assert(spdiag(as(:)),sparse(diag(af(:)),true)) |
|
398 %!assert(spdiag(as(:),1),sparse(diag(af(:),1),true)) |
|
399 %!assert(spdiag(as(:),-1),sparse(diag(af(:),-1),true)) |
|
400 %!assert(spdiag(as(:)'),sparse(diag(af(:)'),true)) |
|
401 %!assert(spdiag(as(:)',1),sparse(diag(af(:)',1),true)) |
|
402 %!assert(spdiag(as(:)',-1),sparse(diag(af(:)',-1),true)) |
|
403 %!assert(spdiags(as,[0,1]),[diag(af,0),diag(af,1)]) |
|
404 %!test [tb,tc]=spdiags(as); |
|
405 %! assert(spdiags(tb,tc,sparse(zeros(size(as)))),as) |
|
406 %! assert(spdiags(tb,tc,size(as,1),size(as,2)),as) |
|
407 |
|
408 EOF |
|
409 } |
|
410 |
|
411 # test matrix reshape operations |
|
412 gen_matrixreshape_tests() { |
|
413 cat >>$TESTS <<EOF |
|
414 %% Matrix diagonal tests (uses af,as,bf,bs) |
|
415 %!assert(reshape(as,1,prod(size(as))),sparse(reshape(af,1,prod(size(af))),true)) |
|
416 %!assert(reshape(as,prod(size(as)),1),sparse(reshape(af,prod(size(af)),1),true)) |
|
417 %!assert(reshape(as,fliplr(size(as))),sparse(reshape(af,fliplr(size(af))),true)) |
|
418 %!assert(reshape(bs,1,prod(size(as))),sparse(reshape(bf,1,prod(size(af))),true)) |
|
419 %!assert(reshape(bs,prod(size(as)),1),sparse(reshape(bf,prod(size(af)),1),true)) |
|
420 %!assert(reshape(bs,fliplr(size(as))),sparse(reshape(bf,fliplr(size(af))),true)) |
|
421 |
|
422 EOF |
|
423 } |
|
424 |
|
425 # test mapper matrix operations: uses as,af |
|
426 print_mapper_test() { |
|
427 echo "%!assert($1(as),sparse($1(af),1))" >>$TESTS |
|
428 } |
|
429 |
|
430 print_real_mapper_test() { |
|
431 cat >>$TESTS <<EOF |
|
432 %!test |
5781
|
433 %! wn2s = warning ("query", "Octave:num-to-str"); |
|
434 %! warning ("off", "Octave:num-to-str"); |
5590
|
435 %! if isreal(af) |
5953
|
436 %! if ($2) |
|
437 %! assert($1(as),sparse($1(af),1)) |
|
438 %! else |
|
439 %! assert($1(as),$1(af)) |
|
440 %! endif |
5590
|
441 %! endif |
5781
|
442 %! warning (wn2s.state, "Octave:num-to-str"); |
5590
|
443 |
|
444 EOF |
|
445 } |
|
446 |
|
447 gen_mapper_tests() { |
|
448 echo "%% Unary matrix tests (uses af,as)">>$TESTS |
|
449 print_mapper_test abs |
|
450 print_mapper_test acos |
|
451 print_mapper_test acosh |
|
452 print_mapper_test angle |
|
453 print_mapper_test arg |
|
454 print_mapper_test asin |
|
455 print_mapper_test asinh |
|
456 print_mapper_test atan |
|
457 print_mapper_test atanh |
|
458 print_mapper_test ceil |
|
459 print_mapper_test conj |
|
460 print_mapper_test cos |
|
461 print_mapper_test cosh |
|
462 print_mapper_test exp |
|
463 print_mapper_test finite |
|
464 print_mapper_test fix |
|
465 print_mapper_test floor |
|
466 print_mapper_test imag |
|
467 print_mapper_test isinf |
|
468 print_mapper_test isna |
|
469 print_mapper_test isnan |
|
470 print_mapper_test log |
|
471 #print_mapper_test log10 ## fails with different NaN, not a problem |
|
472 print_mapper_test real |
|
473 print_mapper_test round |
|
474 print_mapper_test sign |
|
475 print_mapper_test sin |
|
476 print_mapper_test sinh |
|
477 print_mapper_test sqrt |
|
478 print_mapper_test tan |
|
479 print_mapper_test tanh |
|
480 |
|
481 # Specific tests for certain mapper functions |
|
482 cat >>$TESTS <<EOF |
|
483 %!assert(issparse(abs(as))&&isreal(abs(as))) |
|
484 %!assert(issparse(real(as))&&isreal(real(as))) |
|
485 %!assert(issparse(imag(as))&&isreal(imag(as))) |
|
486 |
|
487 EOF |
|
488 } |
|
489 |
|
490 gen_real_mapper_tests() { |
|
491 echo "%% Unary matrix tests (uses af,as)">>$TESTS |
5953
|
492 print_real_mapper_test erf 1 |
|
493 print_real_mapper_test erfc 1 |
|
494 #print_real_mapper_test gamma 1 |
|
495 print_real_mapper_test isalnum 0 |
|
496 print_real_mapper_test isalpha 0 |
|
497 print_real_mapper_test isascii 0 |
|
498 print_real_mapper_test iscntrl 0 |
|
499 print_real_mapper_test isdigit 0 |
|
500 print_real_mapper_test isgraph 0 |
|
501 print_real_mapper_test islower 0 |
|
502 print_real_mapper_test isprint 0 |
|
503 print_real_mapper_test ispunct 0 |
|
504 print_real_mapper_test isspace 0 |
|
505 print_real_mapper_test isupper 0 |
|
506 print_real_mapper_test isxdigit 0 |
|
507 #print_real_mapper_test lgamma 1 |
5590
|
508 |
|
509 # Specific tests for certain mapper functions |
|
510 cat >>$TESTS <<EOF |
|
511 |
|
512 %% These mapper functions always return a full matrix |
|
513 %!test |
5781
|
514 %! wn2s = warning ("query", "Octave:num-to-str"); |
|
515 %! warning ("off", "Octave:num-to-str"); |
5590
|
516 %! if isreal(af) |
|
517 %! assert(toascii(as),toascii(af)) |
|
518 %! assert(tolower(as),tolower(af)) |
|
519 %! assert(toupper(as),toupper(af)) |
|
520 %! endif |
5781
|
521 %! warning (wn2s.state, "Octave:num-to-str"); |
5590
|
522 |
|
523 EOF |
|
524 } |
|
525 |
|
526 # test matrix operations: uses as,af |
|
527 gen_unaryop_tests() { |
|
528 cat >>$TESTS <<EOF |
|
529 %% Unary matrix tests (uses af,as) |
|
530 %!assert(issparse(as)) |
|
531 %!assert(!issparse(af)) |
|
532 %!assert(!(issparse(af)&&iscomplex(af))) |
|
533 %!assert(!(issparse(af)&&isreal(af))) |
|
534 %!assert(spsum(as),sparse(sum(af),true)) |
|
535 %!assert(spsum(as,1),sparse(sum(af,1),true)) |
|
536 %!assert(spsum(as,2),sparse(sum(af,2),true)) |
|
537 %!assert(spcumsum(as),sparse(cumsum(af),true)) |
|
538 %!assert(spcumsum(as,1),sparse(cumsum(af,1),true)) |
|
539 %!assert(spcumsum(as,2),sparse(cumsum(af,2),true)) |
|
540 %!assert(spsumsq(as),sparse(sumsq(af),true)) |
|
541 %!assert(spsumsq(as,1),sparse(sumsq(af,1),true)) |
|
542 %!assert(spsumsq(as,2),sparse(sumsq(af,2),true)) |
|
543 %!assert(spprod(as),sparse(prod(af),true)) |
|
544 %!assert(spprod(as,1),sparse(prod(af,1),true)) |
|
545 %!assert(spprod(as,2),sparse(prod(af,2),true)) |
|
546 %!assert(spcumprod(as),sparse(cumprod(af),true)) |
|
547 %!assert(spcumprod(as,1),sparse(cumprod(af,1),true)) |
|
548 %!assert(spcumprod(as,2),sparse(cumprod(af,2),true)) |
|
549 |
|
550 %!assert(spmin(as),sparse(min(af),true)) |
7197
|
551 %!assert(full(spmin(as(:))),min(af(:))) |
5590
|
552 %!assert(spmin(as,[],1),sparse(min(af,[],1),true)) |
|
553 %!assert(spmin(as,[],2),sparse(min(af,[],2),true)) |
|
554 %!assert(spmin(as,[],1),sparse(min(af,[],1),true)) |
|
555 %!assert(spmin(as,0),sparse(min(af,0),true)) |
|
556 %!assert(spmin(as,bs),sparse(min(af,bf),true)) |
|
557 %!assert(spmax(as),sparse(max(af),true)) |
7197
|
558 %!assert(full(spmax(as(:))),max(af(:))) |
5590
|
559 %!assert(spmax(as,[],1),sparse(max(af,[],1),true)) |
|
560 %!assert(spmax(as,[],2),sparse(max(af,[],2),true)) |
|
561 %!assert(spmax(as,[],1),sparse(max(af,[],1),true)) |
|
562 %!assert(spmax(as,0),sparse(max(af,0),true)) |
|
563 %!assert(spmax(as,bs),sparse(max(af,bf),true)) |
|
564 |
|
565 %!assert(as==as) |
|
566 %!assert(as==af) |
|
567 %!assert(af==as) |
|
568 %!test |
|
569 %! [ii,jj,vv,nr,nc] = spfind(as); |
|
570 %! assert(af,full(sparse(ii,jj,vv,nr,nc))); |
|
571 %!assert(nnz(as),sum(af(:)!=0)) |
|
572 %!assert(nnz(as),nnz(af)) |
|
573 %!assert(issparse(as.')) |
|
574 %!assert(issparse(as')) |
|
575 %!assert(issparse(-as)) |
|
576 %!assert(~as,sparse(~af,true)) |
|
577 %!assert(as.', sparse(af.',true)); |
|
578 %!assert(as', sparse(af',true)); |
|
579 %!assert(-as, sparse(-af,true)); |
|
580 %!assert(~as, sparse(~af,true)); |
|
581 %!error [i,j]=size(af);as(i-1,j+1); |
|
582 %!error [i,j]=size(af);as(i+1,j-1); |
|
583 %!test |
|
584 %! [Is,Js,Vs] = spfind(as); |
|
585 %! [If,Jf,Vf] = find(af); |
|
586 %! assert(Is,If); |
|
587 %! assert(Js,Jf); |
|
588 %! assert(Vs,Vf); |
|
589 %!error as(0,1); |
|
590 %!error as(1,0); |
|
591 %!assert(spfind(as),find(af)) |
|
592 %!test |
|
593 %! [i,j,v] = spfind(as); |
|
594 %! [m,n] = size(as); |
|
595 %! x = sparse(i,j,v,m,n); |
|
596 %! assert(x,as); |
|
597 %!test |
|
598 %! [i,j,v,m,n] = spfind(as); |
|
599 %! x = sparse(i,j,v,m,n); |
|
600 %! assert(x,as); |
|
601 %!assert(issparse(horzcat(as,as))); |
|
602 %!assert(issparse(vertcat(as,as))); |
|
603 %!assert(issparse(cat(1,as,as))); |
|
604 %!assert(issparse(cat(2,as,as))); |
|
605 %!assert(issparse([as,as])); |
|
606 %!assert(issparse([as;as])); |
|
607 %!assert(horzcat(as,as), sparse([af,af])); |
|
608 %!assert(vertcat(as,as), sparse([af;af])); |
|
609 %!assert(horzcat(as,as,as), sparse([af,af,af])); |
|
610 %!assert(vertcat(as,as,as), sparse([af;af;af])); |
|
611 %!assert([as,as], sparse([af,af])); |
|
612 %!assert([as;as], sparse([af;af])); |
|
613 %!assert([as,as,as], sparse([af,af,af])); |
|
614 %!assert([as;as;as], sparse([af;af;af])); |
|
615 %!assert(cat(2,as,as), sparse([af,af])); |
|
616 %!assert(cat(1,as,as), sparse([af;af])); |
|
617 %!assert(cat(2,as,as,as), sparse([af,af,af])); |
|
618 %!assert(cat(1,as,as,as), sparse([af;af;af])); |
|
619 %!assert(issparse([as,af])); |
|
620 %!assert(issparse([af,as])); |
|
621 %!assert([as,af], sparse([af,af])); |
|
622 %!assert([as;af], sparse([af;af])); |
|
623 |
|
624 EOF |
|
625 } |
|
626 |
|
627 # operations which require square matrices. |
|
628 gen_square_tests() { |
|
629 # The \ and / operator tests on square matrices |
|
630 gen_square_divop_tests |
|
631 |
|
632 cat >>$TESTS <<EOF |
7243
|
633 %!testif HAVE_UMFPACK |
|
634 %! assert(spdet(bs+speye(size(bs))),det(bf+eye(size(bf))),100*eps*abs(det(bf+eye(size(bf))))) |
5590
|
635 |
7243
|
636 %!testif HAVE_UMFPACK |
5590
|
637 %! [l,u]=splu(sparse([1,1;1,1])); |
|
638 %! assert(l*u,[1,1;1,1],10*eps); |
|
639 |
7243
|
640 %!testif HAVE_UMFPACK |
5590
|
641 %! [l,u]=splu(sparse([1,1;1,1+i])); |
|
642 %! assert(l,sparse([1,2,2],[1,1,2],1),10*eps); |
|
643 %! assert(u,sparse([1,1,2],[1,2,2],[1,1,1i]),10*eps); |
|
644 |
7243
|
645 %!testif HAVE_UMFPACK ;# permuted LU |
5590
|
646 %! [L,U] = splu(bs); |
|
647 %! assert(L*U,bs,1e-10); |
|
648 |
7243
|
649 %!testif HAVE_UMFPACK ;# simple LU + row permutations |
5590
|
650 %! [L,U,P] = splu(bs); |
|
651 %! assert(P'*L*U,bs,1e-10); |
|
652 %! # triangularity |
|
653 %! [i,j,v]=spfind(L); |
|
654 %! assert(i-j>=0); |
|
655 %! [i,j,v]=spfind(U); |
|
656 %! assert(j-i>=0); |
|
657 |
7243
|
658 %!testif HAVE_UMFPACK ;# simple LU + row/col permutations |
5590
|
659 %! [L,U,P,Q] = splu(bs); |
|
660 %! assert(P'*L*U*Q',bs,1e-10); |
|
661 %! # triangularity |
|
662 %! [i,j,v]=spfind(L); |
|
663 %! assert(i-j>=0); |
|
664 %! [i,j,v]=spfind(U); |
|
665 %! assert(j-i>=0); |
|
666 |
7243
|
667 %!testif HAVE_UMFPACK ;# LU with fixed column permutation |
5590
|
668 %! [L,U,P] = splu(bs,colamd(bs)); |
|
669 %! assert(P'*L*U,bs,1e-10); |
|
670 %! # triangularity |
|
671 %! [i,j,v]=spfind(L); |
|
672 %! assert(i-j>=0); |
|
673 %! [i,j,v]=spfind(U(:,colamd(bs))); |
|
674 %! assert(j-i>=0); |
|
675 |
7243
|
676 %!testif HAVE_UMFPACK ;# LU with initial column permutation |
5590
|
677 %! [L,U,P,Q] = splu(bs,colamd(bs)); |
|
678 %! assert(P'*L*U*Q',bs,1e-10); |
|
679 %! # triangularity |
|
680 %! [i,j,v]=spfind(L); |
|
681 %! assert(i-j>=0); |
|
682 %! [i,j,v]=spfind(U); |
|
683 %! assert(j-i>=0); |
|
684 |
7243
|
685 %!testif HAVE_UMFPACK ;# inverse |
5590
|
686 %! assert(spinv(bs)*bs,sparse(eye(rows(bs))),1e-10); |
|
687 |
|
688 %!assert(bf\as',bf\af',100*eps); |
|
689 %!assert(bs\af',bf\af',100*eps); |
|
690 %!assert(bs\as',sparse(bf\af'),100*eps); |
|
691 |
|
692 EOF |
|
693 } |
|
694 |
|
695 # Cholesky tests |
|
696 gen_cholesky_tests() { |
|
697 cat >>$TESTS <<EOF |
7243
|
698 %!testif HAVE_CHOLMOD |
|
699 %! assert(spchol(bs)'*spchol(bs),bs,1e-10); |
|
700 %!testif HAVE_CHOLMOD |
|
701 %! assert(splchol(bs)*splchol(bs)',bs,1e-10); |
|
702 %!testif HAVE_CHOLMOD |
|
703 %! assert(splchol(bs),spchol(bs)',1e-10); |
5590
|
704 |
7243
|
705 %!testif HAVE_CHOLMOD ;# Return Partial Cholesky factorization |
5590
|
706 %! [RS,PS] = spchol(bs); |
|
707 %! assert(RS'*RS,bs,1e-10); |
|
708 %! assert(PS,0); |
|
709 %! [LS,PS] = splchol(bs); |
|
710 %! assert(LS*LS',bs,1e-10); |
|
711 %! assert(PS,0); |
|
712 |
7243
|
713 %!testif HAVE_CHOLMOD ;# Permuted Cholesky factorization |
5590
|
714 %! [RS,PS,QS] = spchol(bs); |
|
715 %! assert(RS'*RS,QS*bs*QS',1e-10); |
|
716 %! assert(PS,0); |
|
717 %! [LS,PS,QS] = splchol(bs); |
|
718 %! assert(LS*LS',QS*bs*QS',1e-10); |
|
719 %! assert(PS,0); |
|
720 |
|
721 EOF |
|
722 } |
|
723 |
|
724 # test scalar operations: uses af and real scalar bf; modifies as,bf,bs |
|
725 gen_scalar_tests() { |
|
726 echo '%!test as=sparse(af);' >> $TESTS |
|
727 echo '%!test bs=bf;' >> $TESTS |
|
728 gen_elementop_tests |
|
729 gen_ordering_tests |
|
730 echo '%!test bf=bf+1i;' >>$TESTS |
|
731 echo '%!test bs=bf;' >> $TESTS |
|
732 gen_elementop_tests |
|
733 } |
|
734 |
|
735 # test matrix operations: uses af and bf; modifies as,bs |
|
736 gen_rectangular_tests() { |
|
737 echo '%!test as=sparse(af);' >> $TESTS |
|
738 echo '%!test bs=sparse(bf);' >>$TESTS |
|
739 gen_mapper_tests |
|
740 gen_real_mapper_tests |
|
741 gen_unaryop_tests |
|
742 gen_elementop_tests |
|
743 gen_sparsesparse_elementop_tests |
|
744 gen_matrixop_tests |
|
745 # gen_divop_tests # Disable rectangular \ and / for now |
|
746 gen_matrixdiag_tests |
|
747 gen_matrixreshape_tests |
|
748 } |
|
749 |
|
750 |
|
751 # ======================================================= |
|
752 # sparse assembly tests |
|
753 |
|
754 gen_assembly_tests() { |
|
755 cat >>$TESTS <<EOF |
|
756 %%Assembly tests |
|
757 %!test |
|
758 %! m=max([m;r(:)]); |
|
759 %! n=max([n;c(:)]); |
|
760 %! funiq=fsum=zeros(m,n); |
|
761 %! funiq(r(:) + m*(c(:)-1) ) = ones(size(r(:))); |
|
762 %! funiq = sparse(funiq); |
|
763 %! for k=1:length(r), fsum(r(k),c(k)) += 1; end |
|
764 %! fsum = sparse(fsum); |
|
765 %!assert(sparse(r,c,1),sparse(fsum(1:max(r),1:max(c)))); |
|
766 %!assert(sparse(r,c,1,"sum"),sparse(fsum(1:max(r),1:max(c)))); |
|
767 %!assert(sparse(r,c,1,"unique"),sparse(funiq(1:max(r),1:max(c)))); |
|
768 %!assert(sparse(r,c,1,m,n),sparse(fsum)); |
|
769 %!assert(sparse(r,c,1,m,n,"sum"),sparse(fsum)); |
|
770 %!assert(sparse(r,c,1,m,n,"unique"),sparse(funiq)); |
|
771 |
|
772 %!assert(sparse(r,c,1i),sparse(fsum(1:max(r),1:max(c))*1i)); |
|
773 %!assert(sparse(r,c,1i,"sum"),sparse(fsum(1:max(r),1:max(c))*1i)); |
|
774 %!assert(sparse(r,c,1i,"unique"),sparse(funiq(1:max(r),1:max(c))*1i)); |
|
775 %!assert(sparse(r,c,1i,m,n),sparse(fsum*1i)); |
|
776 %!assert(sparse(r,c,1i,m,n,"sum"),sparse(fsum*1i)); |
|
777 %!assert(sparse(r,c,1i,m,n,"unique"),sparse(funiq*1i)); |
|
778 |
|
779 %!test |
|
780 %! if (issparse(funiq)) |
|
781 %! assert(sparse(full(1i*funiq)),sparse(1i*funiq)); |
|
782 %! endif |
|
783 |
|
784 %!assert(sparse(full(funiq)),funiq); |
|
785 |
|
786 |
|
787 EOF |
|
788 } |
|
789 |
|
790 # ======================================================= |
|
791 # sparse selection tests |
|
792 |
|
793 gen_select_tests() { |
|
794 cat >>$TESTS <<EOF |
|
795 %!test as=sparse(af); |
|
796 |
|
797 %% Point tests |
|
798 %!test idx=ridx(:)+rows(as)*(cidx(:)-1); |
|
799 %!assert(sparse(as(idx),true),sparse(af(idx),true)); |
|
800 %!assert(as(idx),sparse(af(idx),true)); |
|
801 %!assert(as(idx'),sparse(af(idx'),true)); |
5603
|
802 %!assert(as(flipud(idx(:))),sparse(af(flipud(idx(:))),true)) |
5590
|
803 %!assert(as([idx,idx]),sparse(af([idx,idx]),true)); |
|
804 %!error(as(reshape([idx;idx],[1,length(idx),2]))); |
|
805 |
|
806 %% Slice tests |
|
807 %!assert(as(ridx,cidx), sparse(af(ridx,cidx),true)) |
|
808 %!assert(as(ridx,:), sparse(af(ridx,:),true)) |
|
809 %!assert(as(:,cidx), sparse(af(:,cidx),true)) |
|
810 %!assert(as(:,:), sparse(af(:,:),true)) |
5603
|
811 %!assert(as((size(as,1):-1:1),:),sparse(af((size(af,1):-1:1),:),true)) |
|
812 %!assert(as(:,(size(as,2):-1:1)),sparse(af(:,(size(af,2):-1:1)),true)) |
|
813 |
|
814 %% Assignment test |
|
815 %!test |
|
816 %! ts=as;ts(:,:)=ts(fliplr(1:size(as,1)),:); |
|
817 %! tf=af;tf(:,:)=tf(fliplr(1:size(af,1)),:); |
|
818 %! assert(ts,sparse(tf,true)); |
|
819 %!test |
|
820 %! ts=as;ts(fliplr(1:size(as,1)),:)=ts; |
|
821 %! tf=af;tf(fliplr(1:size(af,1)),:)=tf; |
|
822 %! assert(ts,sparse(tf,true)); |
|
823 %!test |
|
824 %! ts=as;ts(:,fliplr(1:size(as,2)))=ts; |
|
825 %! tf=af;tf(:,fliplr(1:size(af,2)))=tf; |
|
826 %! assert(ts,sparse(tf,true)); |
|
827 %!test |
|
828 %! ts(fliplr(1:size(as,1)))=as(:,1);tf(fliplr(1:size(af,1)))=af(:,1); |
|
829 %! assert(ts,sparse(tf,true)); |
|
830 |
|
831 %% Deletion tests |
|
832 %!test |
|
833 %! ts=as;ts(1,:)=[];tf=af;tf(1,:)=[]; |
|
834 %! assert(ts,sparse(tf,true)); |
|
835 %!test |
|
836 %! ts=as;ts(:,1)=[];tf=af;tf(:,1)=[]; |
|
837 %! assert(ts,sparse(tf,true)); |
5590
|
838 |
|
839 %% Test 'end' keyword |
7197
|
840 %!assert(full(as(end)), af(end)) |
|
841 %!assert(full(as(1,end)), af(1,end)) |
|
842 %!assert(full(as(end,1)), af(end,1)) |
|
843 %!assert(full(as(end,end)), af(end,end)) |
5590
|
844 %!assert(as(2:end,2:end), sparse(af(2:end,2:end),true)) |
|
845 %!assert(as(1:end-1,1:end-1), sparse(af(1:end-1,1:end-1),true)) |
|
846 EOF |
|
847 } |
|
848 |
|
849 # ======================================================= |
|
850 # sparse save and load tests |
|
851 |
|
852 gen_save_tests() { |
|
853 cat >>$TESTS <<EOF |
|
854 %!test # save ascii |
|
855 %! savefile= tmpnam(); |
|
856 %! as_save=as; save("-text",savefile,"bf","as_save","af"); |
|
857 %! clear as_save; |
|
858 %! load(savefile,"as_save"); |
|
859 %! unlink(savefile); |
|
860 %! assert(as_save,sparse(af)); |
|
861 %!test # save binary |
|
862 %! savefile= tmpnam(); |
|
863 %! as_save=as; save("-binary",savefile,"bf","as_save","af"); |
|
864 %! clear as_save; |
|
865 %! load(savefile,"as_save"); |
|
866 %! unlink(savefile); |
|
867 %! assert(as_save,sparse(af)); |
|
868 %!test # save hdf5 |
|
869 %! savefile= tmpnam(); |
|
870 %! as_save=as; save("-hdf5",savefile,"bf","as_save","af"); |
|
871 %! clear as_save; |
|
872 %! load(savefile,"as_save"); |
|
873 %! unlink(savefile); |
|
874 %! assert(as_save,sparse(af)); |
|
875 %!test # save matlab |
|
876 %! savefile= tmpnam(); |
|
877 %! as_save=as; save("-mat",savefile,"bf","as_save","af"); |
|
878 %! clear as_save; |
|
879 %! load(savefile,"as_save"); |
|
880 %! unlink(savefile); |
|
881 %! assert(as_save,sparse(af)); |
|
882 EOF |
|
883 } |
|
884 |
|
885 # ============================================================= |
|
886 # Specific solver tests for matrices that will test all of the solver |
|
887 # code. Uses alpha and beta |
|
888 gen_solver_tests() { |
|
889 |
|
890 if $preset; then |
|
891 cat >>$TESTS <<EOF |
|
892 %! n=8; |
|
893 %! lf=diag(1:n);lf(n-1,1)=0.5*alpha;lf(n,2)=0.25*alpha;ls=sparse(lf); |
|
894 %! uf=diag(1:n);uf(1,n-1)=2*alpha;uf(2,n)=alpha;us=sparse(uf); |
|
895 %! ts=spdiags(ones(n,3),-1:1,n,n)+diag(1:n); tf = full(ts); |
|
896 EOF |
|
897 else |
|
898 cat >>$TESTS <<EOF |
|
899 %! n=floor(lognormal_rnd(8,2)+1)' |
|
900 %! ls = tril(sprandn(8,8,0.2),-1).*alpha + n*speye(8); lf = full(ls); |
|
901 %! us = triu(sprandn(8,8,0.2),1).*alpha + n*speye(8); uf = full(us); |
|
902 %! ts = spdiags(randn(8,3),-1:1,8,8).*alpha; tf = full(ts); |
|
903 EOF |
|
904 fi |
|
905 |
|
906 cat >>$TESTS <<EOF |
|
907 %! df = diag(1:n).* alpha; ds = sparse(df); |
|
908 %! pdf = df(randperm(n),randperm(n)); pds = sparse(pdf); |
|
909 %! plf = lf(randperm(n),randperm(n)); pls = sparse(plf); |
|
910 %! puf = uf(randperm(n),randperm(n)); pus = sparse(puf); |
|
911 %! bs = spdiags(repmat([1:n]',1,4),-2:1,n,n).*alpha; bf = full(bs); |
|
912 %! cf = lf + lf'; cs = sparse(cf); |
|
913 %! bcf = bf + bf'; bcs = sparse(bcf); |
|
914 %! tcf = tf + tf'; tcs = sparse(tcf); |
|
915 %! xf = diag(1:n) + fliplr(diag(1:n)).*beta; xs = sparse(xf); |
7080
|
916 %!assert(ds\xf,df\xf,1e-10); |
5590
|
917 %!assert(ds\xs,sparse(df\xf,1),1e-10); |
|
918 %!assert(pds\xf,pdf\xf,1e-10); |
|
919 %!assert(pds\xs,sparse(pdf\xf,1),1e-10); |
|
920 %!assert(ls\xf,lf\xf,1e-10); |
|
921 %!assert(sparse(ls\xs),sparse(lf\xf),1e-10); |
7243
|
922 %!testif HAVE_UMFPACK |
|
923 %! assert(pls\xf,plf\xf,1e-10); |
|
924 %!testif HAVE_UMFPACK |
|
925 %! assert(sparse(pls\xs),sparse(plf\xf),1e-10); |
5590
|
926 %!assert(us\xf,uf\xf,1e-10); |
|
927 %!assert(sparse(us\xs),sparse(uf\xf),1e-10); |
7243
|
928 %!testif HAVE_UMFPACK |
|
929 %! assert(pus\xf,puf\xf,1e-10); |
|
930 %!testif HAVE_UMFPACK |
|
931 %! assert(sparse(pus\xs),sparse(puf\xf),1e-10); |
5590
|
932 %!assert(bs\xf,bf\xf,1e-10); |
|
933 %!assert(sparse(bs\xs),sparse(bf\xf),1e-10); |
7243
|
934 %!testif HAVE_UMFPACK |
|
935 %! assert(cs\xf,cf\xf,1e-10); |
|
936 %!testif HAVE_UMFPACK |
|
937 %! assert(sparse(cs\xs),sparse(cf\xf),1e-10); |
|
938 %!testif HAVE_UMFPACK |
|
939 %! assert(bcs\xf,bcf\xf,1e-10); |
|
940 %!testif HAVE_UMFPACk |
|
941 %! assert(sparse(bcs\xs),sparse(bcf\xf),1e-10); |
5590
|
942 %!assert(ts\xf,tf\xf,1e-10); |
|
943 %!assert(sparse(ts\xs),sparse(tf\xf),1e-10); |
|
944 %!assert(tcs\xf,tcf\xf,1e-10); |
|
945 %!assert(sparse(tcs\xs),sparse(tcf\xf),1e-10); |
|
946 |
|
947 EOF |
5610
|
948 |
|
949 cat >>$TESTS <<EOF |
|
950 %% QR solver tests |
|
951 |
|
952 %!function f(a, sz, feps) |
|
953 %! b = randn(sz); x = a \b; |
|
954 %! assert (a * x, b, feps); |
|
955 %! b = randn(sz)+1i*randn(sz); x = a \ b; |
|
956 %! assert (a * x, b, feps); |
|
957 %! b = sprandn(sz(1),sz(2),0.2); x = a \b; |
|
958 %! assert (sparse(a * x), b, feps); |
|
959 %! b = sprandn(sz(1),sz(2),0.2)+1i*sprandn(sz(1),sz(2),0.2); x = a \b; |
|
960 %! assert (sparse(a * x), b, feps); |
7243
|
961 %!testif HAVE_CXSPARSE |
5610
|
962 %! a = alpha*sprandn(10,11,0.2)+speye(10,11); f(a,[10,2],1e-10); |
5630
|
963 %! ## Test this by forcing matrix_type, as can't get a certain |
|
964 %! ## result for over-determined systems. |
5610
|
965 %! a = alpha*sprandn(10,10,0.2)+speye(10,10); matrix_type(a, "Singular"); |
|
966 %! f(a,[10,2],1e-10); |
|
967 |
5630
|
968 %% Rectanguar solver tests that don't use QR |
|
969 |
|
970 %!test |
|
971 %! ds = alpha * spdiags([1:11]',0,10,11); |
|
972 %! df = full(ds); |
5681
|
973 %! xf = beta * ones(10,2); |
5630
|
974 %! xs = speye(10,10); |
|
975 %!assert(ds\xf,df\xf,100*eps) |
|
976 %!assert(ds\xs,sparse(df\xs,true),100*eps) |
|
977 %!test |
|
978 %! pds = ds([2,1,3:10],:); |
|
979 %! pdf = full(pds); |
|
980 %!assert(pds\xf,pdf\xf,100*eps) |
|
981 %!assert(pds\xs,sparse(pdf\xs,true),100*eps) |
|
982 %!test |
|
983 %! ds = alpha * spdiags([1:11]',0,11,10); |
|
984 %! df = full(ds); |
5681
|
985 %! xf = beta * ones(11,2); |
5630
|
986 %! xs = speye(11,11); |
|
987 %!assert(ds\xf,df\xf,100*eps) |
|
988 %!assert(ds\xs,sparse(df\xs,true),100*eps) |
|
989 %!test |
|
990 %! pds = ds([2,1,3:11],:); |
|
991 %! pdf = full(pds); |
|
992 %!assert(pds\xf,pdf\xf,100*eps) |
|
993 %!assert(pds\xs,sparse(pdf\xs,true),100*eps) |
|
994 %!test |
|
995 %! us = alpha*[[speye(10,10);sparse(1,10)],[[1,1];sparse(9,2);[1,1]]]; |
7243
|
996 %!testif HAVE_UMFPACK |
|
997 %! assert(us*(us\xf),xf,100*eps) |
|
998 %!testif HAVE_UMFPACK |
|
999 %! assert(us*(us\xs),xs,100*eps) |
5630
|
1000 %!test |
|
1001 %! pus = us(:,[2,1,3:12]); |
7243
|
1002 %!testif HAVE_UMFPACK |
|
1003 %! assert(pus*(pus\xf),xf,100*eps) |
|
1004 %!testif HAVE_UMFPACK |
|
1005 %! assert(pus*(pus\xs),xs,100*eps) |
5630
|
1006 %!test |
|
1007 %! us = alpha*[speye(11,9),[1;sparse(8,1);1;0]]; |
7243
|
1008 %!testif HAVE_CXSPARSE |
5630
|
1009 %! [c,r] = spqr (us, xf); |
|
1010 %! assert(us\xf,r\c,100*eps) |
7243
|
1011 %!testif HAVE_CXSPARSE |
5630
|
1012 %! [c,r] = spqr (us, xs); |
5681
|
1013 %! r = matrix_type(r,"Singular"); ## Force Matrix Type |
5630
|
1014 %! assert(us\xs,r\c,100*eps) |
|
1015 %!test |
|
1016 %! pus = us(:,[1:8,10,9]); |
7243
|
1017 %!testif HAVE_CXSPARSE |
5630
|
1018 %! [c,r] = spqr (pus, xf); |
5681
|
1019 %! r = matrix_type(r,"Singular"); ## Force Matrix Type |
5630
|
1020 %! assert(pus\xf,r\c,100*eps) |
7243
|
1021 %!testif HAVE_CXSPARSE |
5630
|
1022 %! [c,r] = spqr (pus, xs); |
5681
|
1023 %! r = matrix_type(r,"Singular"); ## Force Matrix Type |
5630
|
1024 %! assert(pus\xs,r\c,100*eps) |
|
1025 %!test |
|
1026 %! ls = alpha*[speye(9,11);[1,sparse(1,8),1,0]]; |
5681
|
1027 %! xf = beta * ones(10,2); |
5630
|
1028 %! xs = speye(10,10); |
|
1029 %!assert(ls*(ls\xf),xf,100*eps) |
|
1030 %!assert(ls*(ls\xs),xs,100*eps) |
|
1031 %!test |
|
1032 %! pls = ls([1:8,10,9],:); |
|
1033 %!assert(pls*(pls\xf),xf,100*eps) |
|
1034 %!assert(pls*(pls\xs),xs,100*eps) |
|
1035 %!test |
|
1036 %! ls = alpha*[speye(10,10),sparse(10,1);[1;1],sparse(2,9),[1;1]]; |
5681
|
1037 %! xf = beta * ones(12,2); |
5630
|
1038 %! xs = speye(12,12); |
7243
|
1039 %!testif HAVE_CXSPARSE |
5630
|
1040 %! [c,r] = spqr (ls, xf); |
|
1041 %! assert(ls\xf,r\c,100*eps) |
7243
|
1042 %!testif HAVE_CXSPARSE |
5630
|
1043 %! [c,r] = spqr (ls, xs); |
5681
|
1044 %! r = matrix_type(r,"Singular"); ## Force Matrix Type |
5630
|
1045 %! assert(ls\xs,r\c,100*eps) |
7243
|
1046 %!testif HAVE_CXSPARSE |
5630
|
1047 %! pls = ls(:,[1:8,10,9]); |
7243
|
1048 %!testif HAVE_CXSPARSE |
5630
|
1049 %! [c,r] = spqr (pls, xf); |
5681
|
1050 %! r = matrix_type(r,"Singular"); ## Force Matrix Type |
5630
|
1051 %! assert(pls\xf,r\c,100*eps) |
7243
|
1052 %!testif HAVE_CXSPARSE |
5630
|
1053 %! [c,r] = spqr (pls, xs); |
5681
|
1054 %! r = matrix_type(r,"Singular"); ## Force Matrix Type |
5630
|
1055 %! assert(pls\xs,r\c,100*eps) |
|
1056 |
5610
|
1057 EOF |
5590
|
1058 } |
|
1059 |
|
1060 |
|
1061 # ============================================================= |
|
1062 # Putting it all together: defining the combined tests |
|
1063 |
|
1064 |
|
1065 # initial function |
|
1066 gen_function |
|
1067 gen_section |
|
1068 |
|
1069 # specific tests |
|
1070 if $preset; then |
|
1071 gen_specific_tests |
|
1072 gen_section |
|
1073 fi |
|
1074 |
|
1075 # scalar operations |
|
1076 echo '%!shared as,af,bs,bf' >> $TESTS |
|
1077 if $preset; then |
|
1078 echo '%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];' >> $TESTS |
|
1079 echo '%!test bf=3;' >>$TESTS |
|
1080 else |
|
1081 cat >>$TESTS <<EOF |
|
1082 %!test |
|
1083 %! % generate m,n from 1 to <5000 |
|
1084 %! m=floor(lognormal_rnd(8,2)+1); |
|
1085 %! n=floor(lognormal_rnd(8,2)+1); |
|
1086 %! as=sprandn(m,n,0.3); af = full(as+1i*sprandn(as)); |
|
1087 %! bf = randn; |
|
1088 EOF |
|
1089 fi |
|
1090 |
|
1091 gen_scalar_tests |
|
1092 gen_section |
|
1093 |
|
1094 # rectangular operations |
|
1095 if $preset; then |
|
1096 echo '%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];' >> $TESTS |
|
1097 echo '%!test bf=[0,1-1i,0,0;2+1i,0,0,0;3-1i,2+3i,0,0];' >> $TESTS |
|
1098 else |
|
1099 cat >>$TESTS <<EOF |
|
1100 %!test |
|
1101 %! m=floor(lognormal_rnd(8,2)+1); |
|
1102 %! n=floor(lognormal_rnd(8,2)+1); |
|
1103 %! as=sprandn(m,n,0.3); af = full(as+1i*sprandn(as)); |
|
1104 %! bs=sprandn(m,n,0.3); bf = full(bs+1i*sprandn(bs)); |
|
1105 EOF |
|
1106 fi |
|
1107 |
|
1108 gen_rectangular_tests |
|
1109 gen_section |
|
1110 gen_save_tests |
|
1111 gen_section |
|
1112 echo '%!test bf=real(bf);' >> $TESTS |
|
1113 gen_rectangular_tests |
|
1114 gen_section |
|
1115 gen_sparsesparse_ordering_tests |
|
1116 gen_section |
|
1117 echo '%!test af=real(af);' >> $TESTS |
|
1118 gen_rectangular_tests |
|
1119 gen_section |
|
1120 gen_save_tests |
|
1121 gen_section |
|
1122 echo '%!test bf=bf+1i*(bf~=0);' >> $TESTS |
|
1123 gen_rectangular_tests |
|
1124 gen_section |
|
1125 |
|
1126 # square operations |
|
1127 if $preset; then |
|
1128 echo '%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];' >> $TESTS |
|
1129 echo '%! as=sparse(af);' >> $TESTS |
|
1130 echo '%!test bf=[0,1-1i,0,0;2+1i,0,0,0;3-1i,2+3i,0,0];' >> $TESTS |
|
1131 else |
|
1132 cat >>$TESTS <<EOF |
|
1133 %!test |
|
1134 %! m=floor(lognormal_rnd(8,2)+1); |
|
1135 %! n=floor(lognormal_rnd(8,2)+1); |
|
1136 %! as=sprandn(m,n,0.3); af = full(as+1i*sprandn(as)); |
|
1137 %! bs=sprandn(m,n,0.3); bf = full(bs+1i*sprandn(bs)); |
|
1138 EOF |
|
1139 fi |
|
1140 |
|
1141 cat >>$TESTS <<EOF |
|
1142 %!test ;# invertible matrix |
|
1143 %! bf=af'*bf+max(abs([af(:);bf(:)]))*sparse(eye(columns(as))); |
|
1144 %! bs=sparse(bf); |
|
1145 |
|
1146 EOF |
|
1147 |
|
1148 gen_square_tests |
|
1149 gen_section |
|
1150 echo '%!test bf=real(bf);' >> $TESTS |
|
1151 echo '%! bs=sparse(bf);' >> $TESTS |
|
1152 gen_square_tests |
|
1153 gen_section |
|
1154 echo '%!test af=real(af);' >> $TESTS |
|
1155 echo '%! as=sparse(af);' >> $TESTS |
|
1156 gen_square_tests |
|
1157 gen_section |
|
1158 echo '%!test bf=bf+1i*(bf~=0);' >> $TESTS |
|
1159 echo '%! bs=sparse(bf);' >> $TESTS |
|
1160 gen_square_tests |
|
1161 gen_section |
|
1162 |
|
1163 # cholesky tests |
|
1164 if $preset; then |
|
1165 echo '%!test bf=[5,0,1+1i,0;0,5,0,1-2i;1-1i,0,5,0;0,1+2i,0,5];' >> $TESTS |
|
1166 echo '%! bs=sparse(bf);' >> $TESTS |
|
1167 else |
|
1168 echo '# This has a small chance of failing to create a positive definite matrix' >> $TESTS |
|
1169 echo '%!test n=floor(lognormal_rnd(8,2)+1)' >> $TESTS |
|
1170 echo '%! bs = n*speye(n,n) + sprandn(n,n,0.3); bf = full(bs);' >> $TESTS |
|
1171 fi |
|
1172 |
|
1173 gen_cholesky_tests |
|
1174 gen_section |
|
1175 echo '%!test bf=real(bf);' >> $TESTS |
|
1176 echo '%! bs=sparse(bf);' >> $TESTS |
|
1177 gen_cholesky_tests |
|
1178 gen_section |
|
1179 |
|
1180 # assembly tests |
|
1181 echo '%!shared r,c,m,n,fsum,funiq' >>$TESTS |
|
1182 if $use_preset; then |
|
1183 cat >>$TESTS <<EOF |
|
1184 %!test |
|
1185 %! r=[1,1,2,1,2,3]; |
|
1186 %! c=[2,1,1,1,2,1]; |
|
1187 %! m=n=0; |
|
1188 EOF |
|
1189 else |
|
1190 cat >>$TESTS <<EOF |
|
1191 %!test |
|
1192 %! % generate m,n from 1 to <5000 |
|
1193 %! m=floor(lognormal_rnd(8,2)+1); |
|
1194 %! n=floor(lognormal_rnd(8,2)+1); |
|
1195 %! nz=ceil((m+n)/2); |
|
1196 %! r=floor(rand(5,nz)*n)+1; |
|
1197 %! c=floor(rand(5,nn)*m)+1; |
|
1198 EOF |
|
1199 fi |
|
1200 gen_assembly_tests #includes real and complex tests |
|
1201 gen_section |
|
1202 |
|
1203 # slicing tests |
|
1204 echo '%!shared ridx,cidx,idx,as,af' >>$TESTS |
|
1205 if $use_preset; then |
|
1206 cat >>$TESTS <<EOF |
|
1207 %!test |
|
1208 %! af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4]; |
|
1209 %! ridx=[1,3]; cidx=[2,3]; |
|
1210 EOF |
|
1211 else |
|
1212 cat >>$TESTS <<EOF |
|
1213 %!test |
|
1214 %! % generate m,n from 1 to <5000 |
|
1215 %! m=floor(lognormal_rnd(8,2)+1); |
|
1216 %! n=floor(lognormal_rnd(8,2)+1); |
|
1217 %! as=sprandn(m,n,0.3); af = full(as+1i*sprandn(as)); |
|
1218 %! ridx = ceil(m*rand(1,ceil(rand*m)) |
|
1219 %! cidx = ceil(n*rand(1,ceil(rand*n)) |
|
1220 EOF |
|
1221 fi |
|
1222 gen_select_tests |
|
1223 echo '%!test af=real(af);' >> $TESTS |
|
1224 gen_select_tests |
|
1225 gen_section |
|
1226 echo '%!shared alpha,beta,df,pdf,lf,plf,uf,puf,bf,cf,bcf,tf,tcf,xf,ds,pds,ls,pls,us,pus,bs,cs,bcs,ts,tcs,xs' >>$TESTS |
|
1227 echo '%!test alpha=1;beta=1;' >> $TESTS |
|
1228 gen_solver_tests |
|
1229 echo '%!test alpha=1;beta=1i;' >> $TESTS |
|
1230 gen_solver_tests |
|
1231 echo '%!test alpha=1i;beta=1;' >> $TESTS |
|
1232 gen_solver_tests |
|
1233 echo '%!test alpha=1i;beta=1i;' >> $TESTS |
|
1234 gen_solver_tests |
|
1235 gen_section |