Debian Codegraph @

dwtools/Configuration_AffineTransform.cpp

raw symbols SCIP
1 /* Configuration_AffineTransform.cpp
2 *
3 * Copyright (C) 1993-2021 David Weenink
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 djmw 20020315 GPL header
21 */
22
23 #include "Configuration_AffineTransform.h"
24 #include "Configuration_and_Procrustes.h"
25 #include "Procrustes.h"
26 #include "SVD.h"
27
28 static void do_steps45 (constMATVU const& w, MATVU const& t, constMATVU const& c, double *out_f) {
29 /*
30 Step 4 || 10: If W'T has negative diagonal elements, multiply corresponding columns in T by -1.
31 */
32 for (integer icol = 1; icol <= w.ncol; icol ++) {
33 const double d = NUMinner (w.column (icol), t.column (icol));
34 if (d < 0.0)
35 t.column (icol) *= -1.0;
36 }
37 /*
38 Step 5 & 11: f = tr W'T (Diag (T'CT))^-1/2
39 */
40 *out_f = 0.0;
41 for (integer icol = 1; icol <= w.ncol; icol ++) {
42 const longdouble d = NUMinner (w.column (icol), t.column (icol));
43 double tct = 0.0;
44 for (integer jcol = 1; jcol <= w.ncol; jcol ++)
45 tct += t [jcol] [icol] * NUMinner (c.row (jcol), t.column (icol));
46 if (tct > 0.0)
47 *out_f += d / sqrt (tct);
48 }
49 }
50
51 /*
52 Using: Kiers & Groenen (1996), A monotonically convergent congruence algorithm for orthogonal congruence rotation,
53 Psychometrika (61), 375-389.
54 */
55 static void NUMmaximizeCongruence_inplace (MATVU const& t, constMATVU const& b, constMATVU const& a, integer maximumNumberOfIterations, double tolerance) {
56 Melder_assert (t.nrow == t.ncol);
57 Melder_assert (t.ncol == b.ncol && b.nrow == a.nrow && b.ncol == a.ncol);
58 integer numberOfIterations = 0;
59
60 if (b.ncol == 1) {
61 t [1] [1] = 1.0;
62 return;
63 }
64 const integer ncol = b.ncol;
65 autoMAT u = zero_MAT (ncol, ncol);
66 autoVEC evec = zero_VEC (ncol);
67 autoSVD svd = SVD_create (ncol, ncol);
68
69 // Steps 1 & 2: C = A'A and W = A'B
70
71 autoMAT c = mtm_MAT (a);
72 autoMAT w = mul_MAT (a.transpose(), b);
73 const double checkc = NUMsum (c.all());
74 const double checkw = NUMsum (w.all());
75
76 Melder_require (checkc != 0.0 && checkw != 0.0,
77 U"NUMmaximizeCongruence: the matrix should not be zero.");
78
79 // Scale W by (diag(B'B))^-1/2
80
81 for (integer icol = 1; icol <= ncol; icol ++) {
82 double scale = NUMinner (b.column (icol), b.row (icol));
83 if (scale > 0.0)
84 scale = 1.0 / sqrt (scale);
85 w.column (icol) *= scale;
86 }
87
88 // Step 3: largest eigenvalue of C
89
90 evec [1] = 1.0;
91 const double rho = VECdominantEigenvector_inplace (evec.get(), c.get(), 1.0e-6);
92 double f, f_old;
93
94 do_steps45 (w.get(), t, c.get(), & f);
95 do {
96 for (integer jcol = 1; jcol <= ncol; jcol ++) {
97 // Step 7.a
98
99 longdouble p = 0.0;
100 for (integer k = 1; k <= ncol; k ++)
101 for (integer icol = 1; icol <= ncol; icol ++)
102 p += t [k] [jcol] * c [k] [icol] * t [icol] [jcol];
103
104 // Step 7.b
105
106 const double q = NUMinner (w.column (jcol), t.column (jcol));
107
108 // Step 7.c
109
110 if (q == 0.0) {
111 u.column (jcol) <<= 0.0;
112 } else {
113 const double ww = NUMsum2 (w.column (jcol));
114 for (integer irow = 1; irow <= ncol; irow ++) {
115 const double ct = NUMinner (c.row (irow), t.column (jcol));
116 u [irow] [jcol] = (q * (ct - rho * t [irow] [jcol]) / double (p) - 2.0 * ww * t [irow] [jcol] / q - w [irow] [jcol]) / sqrt (double (p));
117 }
118 }
119 }
120
121 // Step 8
122
123 SVD_update (svd.get(), u.all());
124
125 // Step 9
126 mul_MAT_out (t, svd -> u.all(), svd -> v.transpose());
127 t *= -1.0;
128
129 numberOfIterations++;
130 f_old = f;
131
132 // Steps 10 & 11 equal steps 4 & 5
133
134 do_steps45 (w.all(), t, c.all(), & f);
135
136 } while (fabs (f_old - f) > std::max (tolerance * fabs (f_old), NUMeps) && numberOfIterations < maximumNumberOfIterations);
137 }
138
139 autoAffineTransform Configurations_to_AffineTransform_congruence (Configuration me, Configuration thee, integer maximumNumberOfIterations, double tolerance) {
140 try {
141 /*
142 Use Procrustes transform to obtain starting configuration.
143 (We only need the transformation matrix T.)
144 */
145 autoProcrustes p = Configurations_to_Procrustes (me, thee, false);
146 Melder_assert (p -> dimension == my data.ncol);
147 Melder_assert (p -> dimension == thy data.ncol);
148 NUMmaximizeCongruence_inplace (p -> r.get (), my data.get (), thy data.get (), maximumNumberOfIterations, tolerance);
149 autoAffineTransform at = AffineTransform_create (p -> dimension);
150 at -> r.get () <<= p -> r.get ();
151 return at;
152 } catch (MelderError) {
153 Melder_throw (me, U": no congruence transformation created.");
154 }
155 }
156
157 autoConfiguration Configuration_AffineTransform_to_Configuration (Configuration me, AffineTransform thee) {
158 try {
159 Melder_require (my numberOfColumns == thy dimension,
160 U"The number of columns in the Configuration should equal the dimension of the transform.");
161
162 autoConfiguration him = Data_copy (me);
163
164 // Apply transformation YT
165
166 thy v_transform (his data.get(), my data.get());
167 return him;
168 } catch (MelderError) {
169 Melder_throw (U"Configuration not created.");
170 }
171 }
172
173 /* End of file Configuration_AffineTransform.cpp */