comparison test/build_sparse_tests.sh @ 5590:1ad66ea35fe5

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