-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitTests.m
5189 lines (5020 loc) · 203 KB
/
unitTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function tests = unitTests
% This file is part of GPCCA.
%
% Copyright (c) 2018, 2017 Bernhard Reuter
%
% If you use this code or parts of it, cite the following reference:
%
% Reuter, B., Weber, M., Fackeldey, K., R?blitz, S., & Garcia, M. E. (2018). Generalized
% Markov State Modeling Method for Nonequilibrium Biomolecular Dynamics: Exemplified on
% Amyloid beta Conformational Dynamics Driven by an Oscillating Electric Field. Journal of
% Chemical Theory and Computation, 14(7), 3579-3594. https://doi.org/10.1021/acs.jctc.8b00079
%
% GPCCA is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published
% by the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% -------------------------------------------------------------------------
% All test functions written by Bernhard Reuter, Theoretical Physics II,
% University of Kassel, 2017
tests = functiontests(localfunctions) ;
end
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% test do_schur (depends on gram_schmidt_mod, SRSchur_num_t, numeric_t)
% (logic paths: 3) (assertions: 2+12)
% The 12 assertions here can't be tested (its also not necessary, since they
% are tested elsewhere)
function do_Schur_MatrixShapeError1(testCase) % a1
% test for the assertion if P matrix isnt quadratic
d1 = numeric_t('[0,0,0]') ;
d2 = numeric_t('[0,0,0]') ;
d3 = numeric_t('[0,0,0]') ;
d4 = numeric_t('[0,0,0]') ;
P = [ d1; d2; d3; d4 ] ;
sd = numeric_t('[ 0, 0, 0, 0 ]') ;
fileid = 'test_do_schur_MatrixShapeError1' ;
try
[ ~, ~ ] = do_schur( P, sd, fileid, 0 ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'do_schur:MatrixShapeError1'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_do_schur_report.txt','w') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function do_Schur_MatrixShapeError2(testCase) % a1
% test for the assertion if sd vector length doesnt match with the
% shape of P
d1 = numeric_t('[0,0,0]') ;
d2 = numeric_t('[0,0,0]') ;
d3 = numeric_t('[0,0,0]') ;
P = [ d1; d2; d3 ] ;
sd = numeric_t('[ 0, 0, 0, 0 ]') ;
fileid = 'test_do_schur_MatrixShapeError2' ;
try
[ ~, ~ ] = do_schur( P, sd, fileid, 0 ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'do_schur:MatrixShapeError2'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_do_schur_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_do_schur(testCase) % l1
% test normal execution
% specify the test case data
matrixfile = 'example_matrix_mu0.txt' ;
% get the known test case data
[ P, sd ] = get_knownInput( matrixfile ) ;
fileid = 'test_do_schur' ;
[ X, RR ] = do_schur( P, sd, fileid, 0 ) ;
N = 9 ;
verifyTrue(testCase,all(size(P)==[N,N]))
verifyTrue(testCase,all(size(X)==[N,N]))
verifyTrue(testCase,all(size(RR)==[N,N]))
% test, if P*X=X*RR (Schur decomposition)
dummy = ( abs(X*RR - P*X) < (testCase.TestData.abstolfac * eps(numeric_t)) ) ;
verifyTrue(testCase,all(dummy(:)))
% test, if the first column of X is 1
dummy = (abs(X(:,1) - 1) < (testCase.TestData.abstolfac * eps(numeric_t))) ;
verifyTrue(testCase,all(dummy(:)))
end
function test_do_schur_DataTypeError(testCase) % l2
% test the error for the case, that class(P)~=class(sd)
% specify the test case data
matrixfile = 'example_matrix_mu0.txt' ;
% get the known test case data
[ P, sd ] = get_knownInput( matrixfile ) ;
fileid = 'test_do_schur_DataTypeError' ;
if isa(P,'mp') && isa(sd,'mp')
sd = double(sd) ;
elseif isa(P,'double') && isa(sd,'double')
sd = single(sd) ;
else
error('test_do_schur_DataTypeError:DataTypeError', ...
['class(P)=',class(P),' class(sd)=',class(sd)])
end
try
[ ~, ~ ] = do_schur( P, sd, fileid, 0 ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'do_schur:DataTypeError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_do_schur_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_do_schur_b_pos(testCase) % l3a
% test normal execution with b>0
% specify the test case data
matrixfile = 'example_matrix_mu0.txt' ;
% get the known test case data
[ P, sd ] = get_knownInput( matrixfile ) ;
fileid = 'test_do_schur' ;
[ X, RR ] = do_schur( P, sd, fileid, 3 ) ;
N = 9 ;
verifyTrue(testCase,all(size(P)==[N,N]))
verifyTrue(testCase,all(size(X)==[N,3]))
verifyTrue(testCase,all(size(RR)==[N,N]))
% test, if P*X=X*RR (Schur decomposition)
dummy = ( abs(X*RR(1:3,1:3) - P*X) < (testCase.TestData.abstolfac * eps(numeric_t)) ) ;
verifyTrue(testCase,all(dummy(:)))
% test, if the first column of X is 1
dummy = (abs(X(:,1) - 1) < (testCase.TestData.abstolfac * eps(numeric_t))) ;
verifyTrue(testCase,all(dummy(:)))
end
function test_do_schur_b_neg(testCase) % l3b
% test normal execution with b<0
% specify the test case data
matrixfile = 'example_matrix_mu0.txt' ;
% get the known test case data
[ P, sd ] = get_knownInput( matrixfile ) ;
fileid = 'test_do_schur' ;
[ X, RR ] = do_schur( P, sd, fileid, -3 ) ;
N = 9 ;
verifyTrue(testCase,all(size(P)==[N,N]))
verifyTrue(testCase,all(size(X)==[N,3]))
verifyTrue(testCase,all(size(RR)==[N,N]))
% test, if P*X=X*RR (Schur decomposition)
dummy = ( abs(X*RR(1:3,1:3) - P*X) < (testCase.TestData.abstolfac * eps(numeric_t)) ) ;
verifyTrue(testCase,all(dummy(:)))
% test, if the first column of X is 1
dummy = (abs(X(:,1) - 1) < (testCase.TestData.abstolfac * eps(numeric_t))) ;
verifyTrue(testCase,all(dummy(:)))
end
% -------------------------------------------------------------------------
% test fillA (depends on do_schur, initialize_A, numeric_t) (logic paths: 6)
% (assertions: 6)
function test_fillA_MatrixShapeError1(testCase) % a1
% test assertion, if A matrix isnt quadratic
d1 = [1,0,0,0] ;
d2 = [1,0,0,0] ;
d3 = [1,0,0,0] ;
svecs = [ d1; d2; d3 ] ;
A = svecs ;
try
[ ~ ] = fillA( A, svecs ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'fillA:MatrixShapeError1'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_fillA_report.txt','w') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_fillA_MatchError(testCase) % a2
% test assertion, if second dimension of schur vector matrix doesnt
% match to the dimensions of the A matrix
d1 = [1,0,0,0] ;
d2 = [1,0,0,0] ;
d3 = [1,0,0,0] ;
svecs = [ d1; d2; d3 ] ;
A = zeros(3) ;
try
[ ~ ] = fillA( A, svecs ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'fillA:MatchError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_fillA_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_fillA_MatrixShapeError2(testCase) % a3
% test assertion for A smaller 2x2
svecs = [ 1, 1 ]' ;
A = 0 ;
try
[ ~ ] = fillA( A, svecs ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'fillA:MatrixShapeError2'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_fillA_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_fillA_MatrixShapeError3(testCase) % a4
% test assertion for (N,k)-matrix with k>N
d1 = [1,0,0,0] ;
d2 = [1,0,0,0] ;
d3 = [1,0,0,0] ;
svecs = [ d1; d2; d3 ] ;
A = zeros(4) ;
try
[ ~ ] = fillA( A, svecs ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'fillA:MatrixShapeError3'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_fillA_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_fillA_MatrixShapeError4(testCase) % a5
% test assertion for (N,k)-matrix with k=N
d1 = [1,0,0] ;
d2 = [1,0,0] ;
d3 = [1,0,0] ;
svecs = [ d1; d2; d3 ] ;
A = zeros(3) ;
try
[ ~ ] = fillA( A, svecs ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'fillA:MatrixShapeError4'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_fillA_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_fillA_FirstColumnError(testCase) % a6
% test for the assertion if first column of schur vector matrix isnt
% constantly equal 1
d1 = [0,0,0] ;
d2 = [0,0,0] ;
d3 = [0,0,0] ;
d4 = [0,0,0] ;
svecs = [ d1; d2; d3; d4 ] ;
A = zeros(3) ;
try
[ ~ ] = fillA( A, svecs ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'fillA:FirstColumnError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_fillA_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_fillA_mu0(testCase) % l
% specify the test case data
matrixfile = 'example_matrix_mu0.txt' ;
% get the known test case data
[ P, sd ] = get_knownInput( matrixfile ) ;
fileid = 'test_fillA_mu0' ;
% get Schur vectors
[ X, ~ ] = do_schur( P, sd, fileid, 0 ) ;
svecs = X(:,1:3) ;
% initialize A
[ T, A ] = evalc('initialize_A( svecs, 1 )') ;
% just save
name = 'test_fillA_mu0-initial-A.txt' ;
save_t(name, A, '-ascii')
% fill A
[ T1, A ] = evalc('fillA( A, double(svecs))') ;
% T, T1 is stored to a file
fileID = fopen('test_fillA_report.txt','a') ;
fprintf(fileID,'%s\n','test_fillA_mu0') ;
fprintf(fileID,'%s\n',T) ;
fprintf(fileID,'%s\n\n',T1) ;
fclose(fileID) ;
% just save
name = 'test_fillA_mu0-A.txt' ;
save_t(name, A, '-ascii')
name = 'test_fillA_mu0-svecs(N,1-3).txt' ;
save_t(name, double(svecs), '-ascii')
% actaul testing
import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.AbsoluteTolerance;
import matlab.unittest.constraints.RelativeTolerance;
% set absolute and relative tolerance for verification
abstol = testCase.TestData.abstolfac * eps ;
reltol = testCase.TestData.reltolfac * eps ;
% check positivity
[ m, k ] = size(A) ;
[ N, n ] = size(svecs) ;
verifyTrue( testCase, m == k )
verifyTrue( testCase, n == k )
A_exp = inf(k) ;
for j = 1:k
dummy = [] ;
for l = 1:N
dummy(l) = svecs(l,2:k)*A(2:k,j) ;
end
A_exp(1,j) = -min(dummy) ;
end
testCase.verifyThat(A(1,:), IsEqualTo(A_exp(1,:), ...
'Within', AbsoluteTolerance(abstol) | RelativeTolerance(reltol)))
% check partition of unity
A_exp(1,1) = 1 - sum(A(1,2:k)) ;
for i = 2:k
A_exp(i,1) = - sum(A(i,2:k)) ;
end
testCase.verifyThat(A(:,1), IsEqualTo(A_exp(:,1), ...
'Within', AbsoluteTolerance(abstol) | RelativeTolerance(reltol)))
end
function test_fillA_mu1000(testCase) % l
% specify the test case data
matrixfile = 'example_matrix_mu1000.txt' ;
% get the known test case data
[ P, sd ] = get_knownInput( matrixfile ) ;
fileid = 'test_fillA_mu1000' ;
% get Schur vectors
[ X, ~ ] = do_schur( P, sd, fileid, 0 ) ;
svecs = X(:,1:5) ;
% initialize A
[ T, A ] = evalc('initialize_A( svecs, 1 )') ;
% just save
name = 'test_fillA_mu1000-initial-A.txt' ;
save_t(name, A, '-ascii')
% fill A
[ T1, A ] = evalc('fillA( A, double(svecs))') ;
% T, T1 is stored to a file
fileID = fopen('test_fillA_report.txt','a') ;
fprintf(fileID,'%s\n','test_fillA_mu1000') ;
fprintf(fileID,'%s\n',T) ;
fprintf(fileID,'%s\n\n',T1) ;
fclose(fileID) ;
% just save
name = 'test_fillA_mu1000-A.txt' ;
save_t(name, A, '-ascii')
name = 'test_fillA_mu1000-svecs(N,1-5).txt' ;
save_t(name, double(svecs), '-ascii')
% actaul testing
import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.AbsoluteTolerance;
import matlab.unittest.constraints.RelativeTolerance;
% set absolute and relative tolerance for verification
abstol = testCase.TestData.abstolfac * eps ;
reltol = testCase.TestData.reltolfac * eps ;
% check positivity
[ m, k ] = size(A) ;
[ N, n ] = size(svecs) ;
verifyTrue( testCase, m == k )
verifyTrue( testCase, n == k )
A_exp = inf(k) ;
for j = 1:k
dummy = [] ;
for l = 1:N
dummy(l) = svecs(l,2:k)*A(2:k,j) ;
end
A_exp(1,j) = -min(dummy) ;
end
testCase.verifyThat(A(1,:), IsEqualTo(A_exp(1,:), ...
'Within', AbsoluteTolerance(abstol) | RelativeTolerance(reltol)))
% check partition of unity
A_exp(1,1) = 1 - sum(A(1,2:k)) ;
for i = 2:k
A_exp(i,1) = - sum(A(i,2:k)) ;
end
testCase.verifyThat(A(:,1), IsEqualTo(A_exp(:,1), ...
'Within', AbsoluteTolerance(abstol) | RelativeTolerance(reltol)))
end
% -------------------------------------------------------------------------
% test getopt (depends on none) (logic paths: 2) (assertions: 0)
function test_getopt(testCase) % l1 + l2
dummy.field = 111 ;
[ field, dummy ] = getopt( dummy, 'field', 999 ) ;
verifyEqual(testCase,field,111)
verifyTrue(testCase,isstruct(dummy))
verifyEqual(testCase,dummy.field,111)
clearvars dummy
dummy = [] ;
[ field, dummy ] = getopt( dummy, 'field', 999 ) ;
verifyEqual(testCase,field,999)
verifyTrue(testCase,isstruct(dummy))
verifyEqual(testCase,dummy.field,999)
end
% -------------------------------------------------------------------------
% test gram_schmidt_mod (depends on nearly_equal, numeric_t) (logic paths: 3)
% (assertions: 2)
function test_gram_schmidt_mod_MatrixShapeError1(testCase) % a1
% test assertion for matrix with only one row
sys = numeric_t('[ 3, 1 ]') ;
sd = numeric_t('1') ;
try
[ ~ ] = gram_schmidt_mod( sys, sd ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'gram_schmidt_mod:MatrixShapeError1'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_gram_schmidt_mod_report.txt','w') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_gram_schmidt_mod_MatrixShapeError2(testCase) % a2
% test assertion for matrix with only one column
sys = numeric_t('[ 3, 1 ]') ;
sys = sys' ;
sd = numeric_t('[ 9/sqrt(10), 1/sqrt(10) ]') ;
try
[ ~ ] = gram_schmidt_mod( sys, sd ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'gram_schmidt_mod:MatrixShapeError2'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_gram_schmidt_mod_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_gram_schmidt_mod_R2_mp(testCase) % l1a
% test basis of R^2, example taken from:
% https://de.wikipedia.org/wiki/Gram-Schmidtsches_Orthogonalisierungsverfahren
prec = testCase.TestData.precision ;
assumeEqual(testCase, prec, 'mp')
x1 = numeric_t('[ 3, 1 ]') ;
x2 = numeric_t('[ 2, 2 ]') ;
sys = [ x1; x2 ] ;
sys = sys' ;
sd = (x1.^2)/norm(x1) ;
Q = gram_schmidt_mod( sys, sd ) ;
u1 = numeric_t('[ 3/sqrt(10), 1/sqrt(10) ]') ;
u2 = numeric_t('[ -1/sqrt(10), 3/sqrt(10) ]') ;
orthosys = [ u1; u2 ] ;
orthosys = orthosys' ;
abstol = testCase.TestData.abstolfac * eps(numeric_t) ;
reltol = testCase.TestData.reltolfac * eps(numeric_t) ;
[ ~, ~, c ] = verifyAlmostEqual(Q, orthosys, abstol, ...
reltol, true) ;
verifyTrue(testCase, c)
end
function test_gram_schmidt_mod_R2_double(testCase) % l1b
% test basis of R^2, example taken from:
% https://de.wikipedia.org/wiki/Gram-Schmidtsches_Orthogonalisierungsverfahren
prec = testCase.TestData.precision ;
assumeEqual(testCase, prec, 'double')
import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.AbsoluteTolerance;
import matlab.unittest.constraints.RelativeTolerance;
% set absolute and relative tolerance for verification
abstol = testCase.TestData.abstolfac * eps ;
reltol = testCase.TestData.reltolfac * eps ;
x1 = numeric_t('[ 3, 1 ]') ;
x2 = numeric_t('[ 2, 2 ]') ;
sys = [ x1; x2 ] ;
sys = sys' ;
sd = numeric_t('[ 0.5, 0.5 ]') ;
Q = gram_schmidt_mod( sys, sd ) ;
u1 = numeric_t('[ sqrt(0.5), sqrt(0.5) ]') ;
u2 = numeric_t('[ sqrt(0.5), -sqrt(0.5) ]') ;
orthosys = [ u1; u2 ] ;
orthosys = orthosys' ;
testCase.verifyThat(Q, IsEqualTo(orthosys, ...
'Within', AbsoluteTolerance(abstol) & RelativeTolerance(reltol)))
end
function test_gram_schmidt_mod_R4(testCase) % l1
% test for subspace of R^4, example taken from:
% http://math.bard.edu/~mbelk/math601/GramSchmidtExamples.pdf
x1 = numeric_t('[ 1, 1, 1, 1 ]') ;
x2 = numeric_t('[ -1, 4, 4, 1 ]') ;
x3 = numeric_t('[ 4, -2, 2, 0 ]') ;
sys = [ x1; x2; x3 ] ;
sys = sys' ;
sd = numeric_t('[ 0.25, 0.25, 0.25, 0.25 ]') ;
Q = gram_schmidt_mod( sys, sd ) ;
u1 = numeric_t('[ 1/2, 1/2, 1/2, 1/2 ]') ;
u2 = numeric_t('[ -1/sqrt(2), sqrt(2)/3, sqrt(2)/3, -1/(3*sqrt(2)) ]') ;
u3 = numeric_t('[ 1/(2*sqrt(3)), -5/(6*sqrt(3)), 7/(6*sqrt(3)), -5/(6*sqrt(3)) ]') ;
orthosys = [ u1; u2; u3 ] ;
orthosys = orthosys' ;
abstol = testCase.TestData.abstolfac * eps(numeric_t) ;
reltol = testCase.TestData.reltolfac * eps(numeric_t) ;
[ ~, ~, c ] = verifyAlmostEqual(Q, orthosys, abstol, ...
reltol, true) ;
verifyTrue(testCase, c)
end
% -------------------------------------------------------------------------
% test indexsearch (depends on numeric_t) (logic paths: 6) (assertions: 1)
function test_indexsearch_MatrixShapeError(testCase) % a1
% test assertion for (N,k)-matrix with k>N
v1 = numeric_t('[1,0,0,0]') ;
v2 = numeric_t('[0,1,0,0]') ;
v3 = numeric_t('[0,0,1,0]') ;
sys = [ v1; v2; v3 ] ;
try
[ ~ ] = indexsearch( sys ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'indexsearch:MatrixShapeError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_indexsearch_report.txt','w') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_indexsearch(testCase) % l
% test correct function for standard simplex in 6D first
v0 = numeric_t('[0,0,0,0,0,0]') ;
v1 = numeric_t('[1,0,0,0,0,0]') ;
v2 = numeric_t('[0,1,0,0,0,0]') ;
v3 = numeric_t('[0,0,1,0,0,0]') ;
v4 = numeric_t('[0,0,0,1,0,0]') ;
v5 = numeric_t('[0,0,0,0,1,0]') ;
v6 = numeric_t('[0,0,0,0,0,1]') ;
sys = [ v0; v1; v0; v2; v0; v3; v0; v4; v0; v5; v0; v6 ] ;
% find indices
index = indexsearch( sys ) ;
index_exp = numeric_t('[2,4,6,8,10,12]') ;
dummy = ( index == index_exp ) ;
verifyTrue(testCase, all(dummy(:)) )
% define simple system of points in 3D, which all are inside (or on the
% surface) of the 3D-simplex with vertices v0=[0,0,0], v1=[1.5,0,0],
% v2=[0,2,0], v3=[0,0,3]
v3 = numeric_t('[0,0,3]') ;
p1 = numeric_t('[0.75,1,0]') ;
v1 = numeric_t('[1.5,0,0]') ;
v0 = numeric_t('[0,0,0]') ;
p3 = numeric_t('[0.375,0.5,0.75]') ;
v2 = numeric_t('[0,2,0]') ;
p2 = numeric_t('[0,1.2,1.2]') ;
p4 = numeric_t('[0.15,0.2,0.6]') ;
p5 = numeric_t('[0,0.6,0.3]') ;
sys = [ v3; p1; v1; v0; p3; v2; p2; p4; p5 ] ;
% find indices
index = indexsearch( sys ) ;
index_exp = numeric_t('[1,6,3]') ;
dummy = ( index == index_exp ) ;
verifyTrue(testCase, all(dummy(:)) )
end
% -------------------------------------------------------------------------
% test initialize_A (depends on do_schur, indexsearch, numeric_t) (logic paths: 4)
% (assertions: 4)
function test_initialize_A_FirstColumnError(testCase) % a1
% test for the assertion if first column of schur vector matrix isnt
% constantly equal 1
d1 = numeric_t('[0,0,0]') ;
d2 = numeric_t('[0,0,0]') ;
d3 = numeric_t('[0,0,0]') ;
d4 = numeric_t('[0,0,0]') ;
dummy = [ d1; d2; d3; d4 ] ;
try
[ ~ ] = initialize_A( dummy, 1 ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'initialize_A:FirstColumnError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_initialize_A_report.txt','w') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_initialize_A_MatrixShapeError1(testCase) % a2
% test assertion for (N,k)-matrix with k>N
d1 = numeric_t('[1,0,0,0]') ;
d2 = numeric_t('[1,0,0,0]') ;
d3 = numeric_t('[1,0,0,0]') ;
dummy = [ d1; d2; d3 ] ;
try
[ ~ ] = initialize_A( dummy, 1 ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'initialize_A:MatrixShapeError1'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_initialize_A_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_initialize_A_MatrixShapeError2(testCase) % a3
% test assertion for (N,k)-matrix with k=N
d1 = numeric_t('[1,0,0]') ;
d2 = numeric_t('[1,0,0]') ;
d3 = numeric_t('[1,0,0]') ;
dummy = [ d1; d2; d3 ] ;
try
[ ~ ] = initialize_A( dummy, 1 ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'initialize_A:MatrixShapeError2'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_initialize_A_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_initialize_A_condition(testCase) % a4
% test assertion for too high matrix condition
dummy = hilb(14) ;
dummy(:,14) = [] ;
dummy(:,1) = numeric_t('1') ;
try
[ ~ ] = initialize_A( dummy, 1 ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'initialize_A:A_ConditionError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_initialize_A_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_initialize_A_init(testCase) % l1
% test for the error if init isnt 0 or 1
d1 = numeric_t('[1,0,0]') ;
d2 = numeric_t('[1,0,0]') ;
d3 = numeric_t('[1,0,0]') ;
d4 = numeric_t('[1,0,0]') ;
dummy = [ d1; d2; d3; d4 ] ;
init = 2 ;
try
[ ~ ] = initialize_A( dummy, init ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'initialize_A:InputError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_initialize_A_report.txt','a') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
function test_initialize_A(testCase) % l2
import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.AbsoluteTolerance;
import matlab.unittest.constraints.RelativeTolerance;
% specify the test case data
matrixfile = 'example_matrix_mu0.txt' ;
% get the known test case data
[ P, sd ] = get_knownInput( matrixfile ) ;
fileid = 'test_fillA' ;
% get Schur vectors
[ X, ~ ] = do_schur( P, sd, fileid, 0 ) ;
evs = X(:,1:4) ;
% initialize A
[ T, A ] = evalc('initialize_A( evs, 1 )') ;
fileID = fopen('test_initialize_A_report.txt','a') ;
fprintf(fileID,'%s\n','test_initialize_A') ;
fprintf(fileID,'%s\n\n',T) ;
fclose(fileID) ;
% set absolute and relative tolerance for verification
abstol = testCase.TestData.abstolfac * eps ;
reltol = testCase.TestData.reltolfac * eps ;
% find indices
index = indexsearch( evs ) ;
A_exp = double( pinv( X(index,1:4) ) ) ;
testCase.verifyThat(A, IsEqualTo(A_exp, ...
'Within', AbsoluteTolerance(abstol) | RelativeTolerance(reltol)))
end
function test_initialize_A_conditionWarning(testCase) % l3
% test warning for high matrix condition
dummy = hilb(6) ;
dummy(:,6) = [] ;
dummy(:,1) = numeric_t('1') ;
[ T, ~ ] = evalc('initialize_A( dummy, 1 )') ;
fileID = fopen('test_initialize_A_report.txt','a') ;
fprintf(fileID,'%s\n','test_initialize_A_conditionWarning') ;
fprintf(fileID,'%s\n\n',T) ;
fclose(fileID) ;
warning = ['Warning for 5 clusters: The condition ' ...
'number of the initial guess for A is > 1e4'] ;
C = strsplit(T, '\n') ;
verifyTrue( testCase, strcmp(C(1), warning) )
end
function test_initialize_A_loadOldfile(testCase) % l4
% test initialization by loading of an already existing matrix from
% file.
% set global variable to indicate testrun (no display output, no
% interactive input): 'minChiON' indicates that minChi criterion is
% used and input is taken from testInput_minChiON.mat. 'minChiOFF'
% indicates that minChi criterion isnt used and input is taken from
% testInput_minChiOFF.mat.
global testmode
testmode = 'minChiON' ;
d1 = [1,0,0] ;
d2 = [1,0,0] ;
d3 = [1,0,0] ;
d4 = [1,0,0] ;
dummy = [ d1; d2; d3 ; d4 ] ;
actVal = initialize_A( dummy, 0 ) ;
e1 = [0,1,0] ;
e2 = [0,1,0] ;
e3 = [0,1,0] ;
e4 = [0,1,0] ;
expVal = [ e1; e2; e3 ; e4 ] ;
verifyEqual(testCase, actVal, expVal, 'AbsTol', eps)
end
% -------------------------------------------------------------------------
% test initialize_optional (depends on none) (logic paths: 5) (assertions: 0)
% test for valid initialization of iopt in case of empty iopt
function test_initialize_optional_empty_iopt(testCase) % l1
% set the ID-string for file-naming
id = 'test_initialize_optional_empty_iopt' ;
[ iopt, fileid, final_opt ] = initialize_optional( [], id ) ;
verifyEqual(testCase, isempty(iopt), true)
verifyEqual(testCase, final_opt, false)
verifyEqual(testCase, fileid, '')
end
function test_initialize_optional_SolverError(testCase)
id = 'test_initialize_optional_SolverError' ;
iopt.solver = 'Newton' ;
try
[ ~, ~, ~ ] = initialize_optional( iopt, id ) ;
actVal = 0 ;
catch ME
switch ME.identifier
case 'initialize_optional:SolverError'
actVal = 1 ;
otherwise
rethrow(ME)
end
end
fileID = fopen('test_initialize_optional_report.txt','w') ;
fprintf(fileID,'%s',ME.identifier) ;
fprintf(fileID,'%s',': ') ;
fprintf(fileID,'%s\n\n',ME.message) ;
fclose(fileID) ;
expVal = 1 ;
verifyEqual(testCase, actVal, expVal)
end
% test for valid initialization of iopt in case of only
% iopt.solver='gauss-newton' defined
function test_initialize_optional_gauss_newton(testCase) % l3
% set the optional solver and ID-string for file-naming
iopt.solver = 'gauss-newton' ;
id = 'test_initialize_optional_gauss_newton' ;
[ iopt, fileid, final_opt ] = initialize_optional( iopt, id ) ;
verifyEqual(testCase, isstruct(iopt), true)
verifyEqual(testCase, iopt.solver, 'gauss-newton')
verifyEqual(testCase, iopt.init, 1)
verifyEqual(testCase, iopt.parallel, 0)
verifyEqual(testCase, iopt.maxiter, 100)
verifyEqual(testCase, iopt.display, 0)
verifyEqual(testCase, iopt.xtol, 1e-4)
verifyEqual(testCase, iopt.xscale, 1e-06)
fileid_exp = strcat(id,'-',iopt.solver,'-','maxiter','_', ...
num2str(iopt.maxiter),'-','xscale','_', num2str(iopt.xscale), ...
'-','xtol','_',num2str(iopt.xtol),'-',numeric_t) ;
verifyEqual(testCase, fileid, fileid_exp)
verifyEqual(testCase, final_opt, true)
end
% test for valid initialization of iopt in case of only
% iopt.solver='nelder-mead' defined
function test_initialize_optional_nelder_mead(testCase) % l4
% set the optional solver and ID-string for file-naming
iopt.solver = 'nelder-mead' ;
id = 'test_initialize_optional_nelder_mead' ;
[ iopt, fileid, final_opt ] = initialize_optional( iopt, id ) ;
verifyEqual(testCase, isstruct(iopt), true)
verifyEqual(testCase, iopt.solver, 'nelder-mead')
verifyEqual(testCase, iopt.init, 1)
verifyEqual(testCase, iopt.parallel, 0)
verifyEqual(testCase, iopt.maxiter, 2000)
verifyEqual(testCase, iopt.display, 0)
verifyEqual(testCase, iopt.tolfun, 1e-8)
verifyEqual(testCase, iopt.tolx, 1e-8)
fileid_exp = strcat(id,'-',iopt.solver,'-','maxiter','_', ...
num2str(iopt.maxiter),'-','tolfun','_', num2str(iopt.tolfun), ...
'-','tolx','_',num2str(iopt.tolx),'-',numeric_t) ;
verifyEqual(testCase, fileid, fileid_exp)
verifyEqual(testCase, final_opt, true)
end
% test for valid initialization of iopt in case of only
% iopt.solver='levenberg-marquardt' defined
function test_initialize_optional_levenberg_marquardt(testCase) % l5
% set the optional solver and ID-string for file-naming
iopt.solver = 'levenberg-marquardt' ;
id = 'test_initialize_optional_levenberg_marquardt' ;
[ iopt, fileid, final_opt ] = initialize_optional( iopt, id ) ;
verifyEqual(testCase, isstruct(iopt), true)
verifyEqual(testCase, iopt.solver, 'levenberg-marquardt')
verifyEqual(testCase, iopt.init, 1)
verifyEqual(testCase, iopt.parallel, 0)
verifyEqual(testCase, iopt.maxiter, 500)
verifyEqual(testCase, iopt.display, 0)
verifyEqual(testCase, iopt.tolfun, 1e-8)
verifyEqual(testCase, iopt.tolx, 1e-10)
fileid_exp = strcat(id,'-',iopt.solver,'-','maxiter','_', ...
num2str(iopt.maxiter),'-','tolfun','_', num2str(iopt.tolfun), ...
'-','tolx','_',num2str(iopt.tolx),'-',numeric_t) ;
verifyEqual(testCase, fileid, fileid_exp)
verifyEqual(testCase, final_opt, true)
end
% -------------------------------------------------------------------------
% test initialize_workspace (depends on none) (logic paths: 4) (assertions: 0)
% test for valid initialization of wk in case of empty wk
function test_initialize_workspace_empty_wk(testCase)
% set the solver, optional solver and ID-string for file-naming
[ wk, fileid ] = initialize_workspace( [] ) ;
verifyEqual(testCase, isstruct(wk), true)
verifyEqual(testCase, wk.id, 'gpcca')
verifyEqual(testCase, wk.schur, 1)
verifyEqual(testCase, wk.b, 0)
verifyEqual(testCase, wk.init, 1)
verifyEqual(testCase, wk.solver, 'gauss-newton')
verifyEqual(testCase, wk.maxiter, 100)
verifyEqual(testCase, wk.display, 0)
verifyEqual(testCase, wk.xtol, 1e-4)
verifyEqual(testCase, wk.xscale, 1e-06)
fileid_exp = strcat(wk.id,'-',wk.solver,'-','maxiter','_', ...
num2str(wk.maxiter),'-','xscale','_', num2str(wk.xscale), ...
'-','xtol','_',num2str(wk.xtol),'-',numeric_t) ;