Debian Codegraph @

dwtest/test_SVD.praat

raw
1 # test_SVD.praat
2 # djmw 20171208
3
4 appendInfoLine: "test_SVD.praat"
5
6 n = 4
7 eps = 1.5e-5
8 appendInfoLine: tab$, "Hilbert matrix order 4"
9 t = Create TableOfReal: "hilbert", n, n
10 for i to n
11 for j to n
12 Set value: i, j, 1 / (i + j - 1)
13 endfor
14 endfor
15 svd = To SVD
16
17 utab = Extract left singular vectors
18 selectObject: svd
19 vtab = Extract right singular vectors
20 selectObject: svd
21 dvec = Extract singular values
22 u$= " -0.792608 0.582076 -0.179186 -0.029193 "
23 ... + " -0.451923 -0.370502 0.741918 0.328712 "
24 ... + " -0.322416 -0.509579 -0.100228 -0.791411 "
25 ... + " -0.252161 -0.514048 -0.638283 0.514553 "
26 v$ = "-0.792608 0.582076 -0.179186 -0.029193 "
27 ... + " -0.451923 -0.370502 0.741918 0.328712 "
28 ... + " -0.322416 -0.509579 -0.100228 -0.791411 "
29 ... + " -0.252161 -0.514048 -0.638283 0.514553"
30 d# = {1.5002, 1.6914e-01, 6.7383e-03, 9.6702e-05}
31
32 appendInfoLine: tab$, tab$, "UDV' test equality of U"
33 @string_to_table: u$, n, n
34 utab_octave = selected ("TableOfReal")
35 appendInfoLine: tab$, tab$, "UDV' test equality of V"
36 @string_to_table: v$, n, n
37 vtab_octave = selected ("TableOfReal")
38
39 @check_tors: utab, utab_octave, eps
40 @check_tors: vtab, vtab_octave, eps
41
42 removeObject: utab, utab_octave, vtab, vtab_octave, svd, t, dvec
43
44 appendInfoLine: tab$, "reconstruct 6x2 matrix"
45 @test_reconstruction: 6, 2, eps
46
47 appendInfoLine: tab$, "reconstruct 2x6 matrix"
48 @test_reconstruction: 2, 6, eps
49
50 appendInfoLine: tab$, "reconstruct 30x500 matrix"
51 @test_reconstruction: 30, 500, eps
52
53 appendInfoLine: "test_SVD.praat OK"
54
55 procedure test_reconstruction: .nrows, .ncols, .eps
56 .t = Create TableOfReal: "t", .nrows, .ncols
57 Formula: "randomUniform (-1,1)"
58 .svd = To SVD
59 .tr = To TableOfReal: 1, 0
60 @check_tors: .t, .tr, .eps
61 removeObject: .t, .svd, .tr
62 endproc
63
64 procedure check_tors: .tor1, .tor2, .eps
65 selectObject: .tor1
66 .nrows = Get number of rows
67 .ncols = Get number of columns
68 for .i to .nrows
69 for .j to .ncols
70 .d =abs ((object[.tor1, .i, .j]-object[.tor2, .i, .j])/object[.tor2, .i, .j])
71 assert .d < .eps; ['.i','.j']: '.tor1', '.tor2', '.d'
72 endfor
73 endfor
74 endproc
75
76 procedure string_to_table: .string$, .nrows, .ncols
77 .s = Create Strings as tokens: .string$
78 .numberOfTokens = Get number of strings
79 assert .numberOfTokens == .nrows * .ncols
80 .t = Create TableOfReal: "t", .nrows, .ncols
81 for .i to .nrows
82 for .j to .ncols
83 selectObject: .s
84 .val$ = Get string: (.i-1)*.ncols + .j
85 selectObject: .t
86 Set value: .i, .j, number (.val$)
87 endfor
88 endfor
89 removeObject: .s
90 selectObject: .t
91 endproc
92
93