Debian Codegraph @

dwtest/test_Procrustes.praat

raw
1 # test_Procrustes.praat
2 # djmw 20040117, 20070820
3
4 appendInfoLine: "test_Procrustes.praat"
5
6 eps =2.3e-15
7 @bg_19_4
8 @test_orthogonal_procrustes_gvl_12_4_1
9 @test_procrustes_random_configurations: 12
10
11 appendInfoLine: "test_Procrustes.praat OK"
12
13 procedure bg_19_4
14 # example Borg & Groenen section 19.4
15 appendInfoLine: tab$, "Example in Borg & Groenen section 19.4"
16 .nrow = 4
17 .ncol = 2
18 .x1# = { 1, 2}
19 .x2# = {-1, 2}
20 .x3# = {-1, -2}
21 .x4# = { 1, -2}
22 .y1# = {0.07, 2.62}
23 .y2# = {0.93, 3.12}
24 .y3# = {1.93, 1.38}
25 .y4# = {1.07, 0.88}
26 .xconf = Create Configuration: "X", .nrow, .ncol, "0"
27 .yconf = Create Configuration: "Y", .nrow, .ncol, "0"
28 for .icol to .ncol
29 selectObject: .xconf
30 Set value: 1, .icol, .x1# [.icol]
31 Set value: 2, .icol, .x2# [.icol]
32 Set value: 3, .icol, .x3# [.icol]
33 Set value: 4, .icol, .x4# [.icol]
34 selectObject: .yconf
35 Set value: 1, .icol, .y1# [.icol]
36 Set value: 2, .icol, .y2# [.icol]
37 Set value: 3, .icol, .y3# [.icol]
38 Set value: 4, .icol, .y4# [.icol]
39 endfor
40 selectObject: .xconf, .yconf
41 .p = To Procrustes: "no"
42 .t_given# = {3.72, -2.46}; 3.73, -2.47
43 .r1_given# = {-0.87, -0.50}
44 .r2_given# = {-0.50, 0.87}
45 .s_given = 2.0
46 .s = Get scale
47 .srounded = number (fixed$ (.s, 1))
48 assert .srounded = .s_given; '.srounded' = '.s_given'
49 for .i to .ncol
50 .t = Get translation element: .i
51 .trounded = number (fixed$ (.t, 2))
52 .tgiven = .t_given#[.i]
53 assert .trounded = .tgiven; '.trounded' = '.tgiven'
54 endfor
55 removeObject: .xconf, .yconf, .p
56 endproc
57
58 procedure test_procrustes_random_configurations: .numconf
59 appendInfoLine: tab$, .numconf, " randomly generated configurations of dimension 2^k x 2"
60 .nc = 2
61 for .k to .numconf
62 .nr = 2^.k
63 appendInfoLine: tab$, tab$, .nr, " rows"
64 .c_x = Create Configuration: "X", .nr, .nc, "randomUniform(-1, 1)"
65 .c_y = Copy: "Y"
66 Invert dimension: 1
67 .alpha = randomUniform (0,90)
68 Rotate: 1, 2, .alpha
69 .t1 = randomUniform (0,2)
70 .t2 = randomUniform (0,2)
71 .scale = randomUniform(0.5,2)
72 Formula... .scale*self + (if col=1 then .t1 else .t2 fi)
73
74 plusObject: .c_x
75 .p_xy = To Procrustes: 0
76 Rename: "X_Y"
77 plusObject: .c_y
78 .c_z = To Configuration
79 Rename: "Z"
80 Formula: "self -object[.c_x, row, col]"
81 .eps = 10 * .nr * .nc * eps
82 for .i to .nr
83 for .j to 2
84 assert object[.c_z, .i, .j] < .eps; Configuration_Z['.i','.j'] < '.eps'
85 endfor
86 endfor
87
88 selectObject: .p_xy
89 .p_xy_i = Invert
90 Rename: "X_Yi"
91 # no need to test the translations, they need not be equal (see BG page 347)
92 .sp = Get scale
93 assert abs(.scale - .sp) < .eps; '.scale' '.sp'
94
95 removeObject: .c_x, .c_y, .c_z, .p_xy, .p_xy_i
96 endfor
97 endproc
98
99 procedure test_orthogonal_procrustes_gvl_12_4_1
100 appendInfoLine: tab$, "Orthognal Procrustes transform (example 12.4.1 Golub & van Loan)"
101 .a = Create Configuration: "a", 4, 2, "0"
102 Set value: 1, 1, 1
103 Set value: 2, 1, 3
104 Set value: 3, 1, 5
105 Set value: 4, 1, 7
106 Set value: 1, 2, 2
107 Set value: 2, 2, 4
108 Set value: 3, 2, 6
109 Set value: 4, 2, 8
110
111 .b = Create Configuration: "b", 4, 2, "0"
112 Set value: 1, 1, 1.2
113 Set value: 2, 1, 2.9
114 Set value: 3, 1, 5.2
115 Set value: 4, 1, 6.8
116 Set value: 1, 2, 2.1
117 Set value: 2, 2, 4.3
118 Set value: 3, 2, 6.1
119 Set value: 4, 2, 8.1
120
121 selectObject: .a, .b
122 .p = To Procrustes: "yes"
123
124 .t2 = Get translation element: 2
125 assert .t2 = 0
126 .s = Get scale
127 assert .s = 1;
128
129 .eps = 5e-5
130 .r11 = Get transformation element: 1, 1
131 .r22 = Get transformation element: 2, 2
132 assert .r11-.r22 < .eps; '.r11'-'.r22' < '.eps' ?
133 assert .r11-0.9999 < .eps; '.r11'-0.9999 < '.eps' ?
134 .r12 = Get transformation element: 1, 2
135 .r21 = Get transformation element: 2, 1
136 assert .r12+0.0126 < .eps; '.r12'+0.0126 < '.eps' ?
137 assert .r21-0.0126 < .eps; '.r21'-0.0126 < '.eps' ?
138
139 removeObject: .a, .b, .p
140 endproc