| 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"cxx . . $ `<file>/dwtools/Configuration_AffineTransform.h`/ |
| 24 | #include "Configuration_and_Procrustes.h"cxx . . $ `<file>/dwtools/Configuration_and_Procrustes.h`/ |
| 25 | #include "Procrustes.h"cxx . . $ `<file>/dwtools/Procrustes.h`/ |
| 26 | #include "SVD.h"cxx . . $ `<file>/dwsys/SVD.h`/ |
| 27 | |
| 28 | static void do_steps45cxx . . $ do_steps45(9c8d8d7398983475). (constMATVUcxx . . $ constMATVU# const& w, MATVUcxx . . $ MATVU# const& t, constMATVUcxx . . $ 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 (integercxx . . $ integer# icol = 1; icol <= w.ncolcxx . . $ constmatrixview#ncol.; icol ++) { |
| 33 | const double d = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (w.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (icol), tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (icol)); |
| 34 | if (d < 0.0) |
| 35 | t.columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (icol) *=cxx . . $ `operator*=`(c555d170d72ff256). -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 (integercxx . . $ integer# icol = 1; icol <= w.ncolcxx . . $ constmatrixview#ncol.; icol ++) { |
| 42 | const longdoublecxx . . $ longdouble# d = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (w.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (icol), tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (icol)); |
| 43 | double tct = 0.0; |
| 44 | for (integercxx . . $ integer# jcol = 1; jcol <= w.ncolcxx . . $ constmatrixview#ncol.; jcol ++) |
| 45 | tct += t [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).jcol] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).icol] * NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (c.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (jcol), tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (icol)); |
| 46 | if (tct > 0.0) |
| 47 | *out_f += d / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (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_inplacecxx . . $ NUMmaximizeCongruence_inplace(1c8f930df67f67a9). (MATVUcxx . . $ MATVU# const& t, constMATVUcxx . . $ constMATVU# const& b, constMATVUcxx . . $ constMATVU# const& a, integercxx . . $ integer# maximumNumberOfIterations, double tolerance) { |
| 56 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (t.nrow == t.ncol); |
| 57 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (t.ncol == b.ncol && b.nrow == a.nrow && b.ncol == a.ncol); |
| 58 | integercxx . . $ integer# numberOfIterations = 0; |
| 59 | |
| 60 | if (b.ncolcxx . . $ constmatrixview#ncol. == 1) { |
| 61 | t [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).1] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 62 | return; |
| 63 | } |
| 64 | const integercxx . . $ integer# ncol = b.ncolcxx . . $ constmatrixview#ncol.; |
| 65 | autoMATcxx . . $ autoMAT# u = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (ncol, ncol); |
| 66 | autoVECcxx . . $ autoVEC# evec = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (ncol); |
| 67 | autoSVDcxx . . $ autoSVD# svd = SVD_createcxx . . $ SVD_create(1ec6f5698ce1ada6). (ncol, ncol); |
| 68 | |
| 69 | // Steps 1 & 2: C = A'A and W = A'B |
| 70 | |
| 71 | autoMATcxx . . $ autoMAT# c = mtm_MATcxx . . $ mtm_MAT(a392b09ce3fc3972). (a); |
| 72 | autoMATcxx . . $ autoMAT# w = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (a.transposecxx . . $ constmatrixview#transpose(f17ed7654a0ebced).(), b); |
| 73 | const double checkc = NUMsumcxx . . $ NUMsum(98ed2a492958fa8a). (ccxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).()); |
| 74 | const double checkw = NUMsumcxx . . $ NUMsum(98ed2a492958fa8a). (wcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).()); |
| 75 | |
| 76 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (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 (integercxx . . $ integer# icol = 1; icol <= ncol; icol ++) { |
| 82 | double scale = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (b.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (icol), b.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (icol)); |
| 83 | if (scale > 0.0) |
| 84 | scale = 1.0 / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (scale); |
| 85 | w.columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icol) *=cxx . . $ `operator*=`(c555d170d72ff256). scale; |
| 86 | } |
| 87 | |
| 88 | // Step 3: largest eigenvalue of C |
| 89 | |
| 90 | evec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 91 | const double rho = VECdominantEigenvector_inplacecxx . . $ VECdominantEigenvector_inplace(35a6868fae9f156b). (evec.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), ccxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 1.0e-6); |
| 92 | double f, f_old; |
| 93 | |
| 94 | do_steps45cxx . . $ do_steps45(9c8d8d7398983475). (wcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), t, ccxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & f); |
| 95 | do { |
| 96 | for (integercxx . . $ integer# jcol = 1; jcol <= ncol; jcol ++) { |
| 97 | // Step 7.a |
| 98 | |
| 99 | longdoublecxx . . $ longdouble# p = 0.0; |
| 100 | for (integercxx . . $ integer# k = 1; k <= ncol; k ++) |
| 101 | for (integercxx . . $ integer# icol = 1; icol <= ncol; icol ++) |
| 102 | p += t [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).k] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).jcol] * c [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).k] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] * t [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).icol] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).jcol]; |
| 103 | |
| 104 | // Step 7.b |
| 105 | |
| 106 | const double q = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (wcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (jcol), tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (jcol)); |
| 107 | |
| 108 | // Step 7.c |
| 109 | |
| 110 | if (q == 0.0) { |
| 111 | u.columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (jcol) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 112 | } else { |
| 113 | const double ww = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (wcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (jcol)); |
| 114 | for (integercxx . . $ integer# irow = 1; irow <= ncol; irow ++) { |
| 115 | const double ct = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (ccxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow), tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (jcol)); |
| 116 | u [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).jcol] = (q * (ct - rho * t [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).jcol]) / double (p) - 2.0 * ww * t [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).jcol] / q - w [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).jcol]) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (p)); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Step 8 |
| 122 | |
| 123 | SVD_updatecxx . . $ SVD_update(595abe6963532d23). (svd.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), ucxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).()); |
| 124 | |
| 125 | // Step 9 |
| 126 | mul_MAT_outcxx . . $ mul_MAT_out(9e368e0da0f1874a). (t, svdcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). ucxx . . $ structSVD#u..allcxx . . $ automatrix#all(2114117ba8ed4224).(), svdcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). vcxx . . $ structSVD#v..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).()); |
| 127 | t *=cxx . . $ `operator*=`(50b469ae42a42f64). -1.0; |
| 128 | |
| 129 | numberOfIterations++; |
| 130 | f_old = f; |
| 131 | |
| 132 | // Steps 10 & 11 equal steps 4 & 5 |
| 133 | |
| 134 | do_steps45cxx . . $ do_steps45(9c8d8d7398983475). (wcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).(), t, ccxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).(), & f); |
| 135 | |
| 136 | } while (fabscxx . . $ fabs(fee4f1d22bc8e617). (f_old - f) > stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (tolerance * fabscxx . . $ fabs(fee4f1d22bc8e617). (f_old), NUMepscxx . . $ `dwsys/NUM2.h:36:9`!) && numberOfIterations < maximumNumberOfIterations); |
| 137 | } |
| 138 | |
| 139 | autoAffineTransformcxx . . $ autoAffineTransform# Configurations_to_AffineTransform_congruencecxx . . $ Configurations_to_AffineTransform_congruence(edc59d37ab87d18b). (Configurationcxx . . $ Configuration# me, Configurationcxx . . $ Configuration# thee, integercxx . . $ 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 | autoProcrustescxx . . $ autoProcrustes# p = Configurations_to_Procrustescxx . . $ Configurations_to_Procrustes(c2202c350b6058d9). (me, thee, false); |
| 146 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (p -> dimension == mycxx . . $ `melder/melder.h:44:9`! data.ncol); |
| 147 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (p -> dimension == thycxx . . $ `melder/melder.h:45:9`! data.ncol); |
| 148 | NUMmaximizeCongruence_inplacecxx . . $ NUMmaximizeCongruence_inplace(1c8f930df67f67a9). (pcxx . . $ matrixview#matrixview(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rcxx . . $ structAffineTransform#r..getcxx . . $ automatrix#get(b2b81450b5bdda66). (), mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66). (), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66). (), maximumNumberOfIterations, tolerance); |
| 149 | autoAffineTransformcxx . . $ autoAffineTransform# at = AffineTransform_createcxx . . $ AffineTransform_create(e6d63a0a16ea9ae0). (p ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structAffineTransform#dimension.); |
| 150 | atcxx . . $ matrixview#matrixview(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rcxx . . $ structAffineTransform#r..getcxx . . $ automatrix#get(b2b81450b5bdda66). () <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). pcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rcxx . . $ structAffineTransform#r..getcxx . . $ automatrix#get(b2b81450b5bdda66). (); |
| 151 | return atcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 152 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 153 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no congruence transformation created."); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | autoConfigurationcxx . . $ autoConfiguration# Configuration_AffineTransform_to_Configurationcxx . . $ Configuration_AffineTransform_to_Configuration(b84d8ab835655fdd). (Configurationcxx . . $ Configuration# me, AffineTransformcxx . . $ AffineTransform# thee) { |
| 158 | try { |
| 159 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfColumns == thycxx . . $ `melder/melder.h:45:9`! dimension, |
| 160 | U"The number of columns in the Configuration should equal the dimension of the transform."); |
| 161 | |
| 162 | autoConfigurationcxx . . $ autoConfiguration# him = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 163 | |
| 164 | // Apply transformation YT |
| 165 | |
| 166 | thycxx . . $ `melder/melder.h:45:9`! v_transformcxx . . $ structAffineTransform#v_transform(a7d93ba40fe22fe0). (hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 167 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 168 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 169 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Configuration not created."); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* End of file Configuration_AffineTransform.cpp */ |