Debian Codegraph @

dwtools/Proximity.cpp

raw symbols SCIP
1 /* Proximity.cpp
2 *
3 * Copyright (C) 1993-2022 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 20020813 GPL header
21 djmw 20040309 Removed assertion 'numberOfPoints> 0' in Proximity_init
22 */
23
24 #include "Configuration.h"cxx . . $ `<file>/dwtools/Configuration.h`/
25 #include "Distance.h"cxx . . $ `<file>/dwtools/Distance.h`/
26 #include "Proximity_and_Distance.h"cxx . . $ `<file>/dwtools/Proximity_and_Distance.h`/
27 #include "TableOfReal_extensions.h"cxx . . $ `<file>/dwtools/TableOfReal_extensions.h`/
28
29 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
30
31 Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Proximity, TableOfReal, 0);
32
33 void Proximity_initcxx . . $ Proximity_init(bae185fe52776cec). (Proximitycxx . . $ Proximity# me, integercxx . . $ integer# numberOfPoints) {
34 TableOfReal_initcxx . . $ TableOfReal_init(cfadf70b559ed315). (me, numberOfPoints, numberOfPoints);
35 TableOfReal_setSequentialRowLabelscxx . . $ TableOfReal_setSequentialRowLabels(8dc3139cdc86d712). (me, 0, 0, nullptr, 1, 1);
36 TableOfReal_setSequentialColumnLabelscxx . . $ TableOfReal_setSequentialColumnLabels(8dc3139cdc86d712). (me, 0, 0, nullptr, 1, 1);
37 }
38
39 Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Dissimilarity, Proximity, 0);
40
41 static bool Dissimilarity_hasNoNegativeValuescxx . . $ Dissimilarity_hasNoNegativeValues(5fd514b05914a886). (Dissimilaritycxx . . $ Dissimilarity# me) {
42 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. - 1; i ++)
43 for (integercxx . . $ integer# j = i + 1; j <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; j ++)
44 if (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] < 0.0)
45 return false;
46 return true;
47 }
48
49 autoDissimilaritycxx . . $ autoDissimilarity# Dissimilarity_createcxx . . $ Dissimilarity_create(cdd51d9389725cda). (integercxx . . $ integer# numberOfPoints) {
50 try {
51 autoDissimilaritycxx . . $ autoDissimilarity# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Dissimilarity);
52 Proximity_initcxx . . $ Proximity_init(bae185fe52776cec). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), numberOfPoints);
53 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
54 } catch (MelderErrorcxx . . $ MelderError#) {
55 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Dissimilarity not created.");
56 }
57 }
58
59 autoDissimilaritycxx . . $ autoDissimilarity# Dissimilarity_createLetterRExamplecxx . . $ Dissimilarity_createLetterRExample(3788cea6d5cbc1f2). (double noiseStd) {
60 try {
61 autoConfigurationcxx . . $ autoConfiguration# r = Configuration_createLetterRExamplecxx . . $ Configuration_createLetterRExample(edb1c2a8de44a665). (1);
62 autoDistancecxx . . $ autoDistance# d = Configuration_to_Distancecxx . . $ Configuration_to_Distance(eb9925fa1662ef19). (r.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
63 autoDissimilaritycxx . . $ autoDissimilarity# me = Distance_to_Dissimilaritycxx . . $ Distance_to_Dissimilarity(3ef1c2fefce3b7fd). (d.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
64 Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), U"R");
65
66 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. - 1; i ++) {
67 for (integercxx . . $ integer# j = i + 1; j <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; j ++) {
68 const double dis = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j];
69 mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = dis * dis + 5.0 + NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0.0, noiseStd);
70 }
71 }
72 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
73 } catch (MelderErrorcxx . . $ MelderError#) {
74 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Dissimilarity for letter R example not created.");
75 }
76 }
77
78 /*
79 Get the best estimate for the additive constant:
80 "distance = dissimilarity + constant"
81 F. Cailliez (1983), The analytical solution of the additive constant problem, Psychometrika 48, 305-308.
82 */
83 double Dissimilarity_getAdditiveConstantcxx . . $ Dissimilarity_getAdditiveConstant(5458a667c2451424). (Dissimilaritycxx . . $ Dissimilarity# me) {
84 try {
85 const integercxx . . $ integer# nPoints = mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., nPoints2 = 2 * nPoints;
86 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (nPoints > 0,
87 U"Matrix part should not be empty.");
88
89 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (Dissimilarity_hasNoNegativeValues (me),
90 U"Dissimilarities should not be negative.");
91
92 autoMATcxx . . $ autoMAT# wd = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (nPoints, nPoints);
93 autoMATcxx . . $ autoMAT# wdsqrt = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (nPoints, nPoints);
94 /*
95 The matrices D & D1/2 with distances (squared and linear)
96 */
97 for (integercxx . . $ integer# i = 1; i <= nPoints - 1; i ++) {
98 for (integercxx . . $ integer# j = i + 1; j <= nPoints; j ++) {
99 const double proximity = (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] + mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]) / 2.0;
100 wdsqrt [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = wdsqrt [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = - proximity / 2.0; // djmw 20180830
101 wd [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = wd [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = - proximity * proximity / 2.0;
102 }
103 }
104
105 doubleCentre_MAT_inoutcxx . . $ doubleCentre_MAT_inout(5dd9f6ea610bc98c). (wdsqrtcxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
106 doubleCentre_MAT_inoutcxx . . $ doubleCentre_MAT_inout(5dd9f6ea610bc98c). (wdcxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
107
108 /*
109 if wd is not positive semi-definite then the dissimilarities have no euclidean representation
110 and we need to calculate an additive constant c such that the dissimilarities
111 d'[i][j] = d [i][j] + c have an euclidean representation.
112 A positive semi-definite matrix has all eigenvalues larger than or equal to zero.
113 */
114 autoEigencxx . . $ autoEigen# eigen_wd = Eigen_createFromSquareMATcxx . . $ Eigen_createFromSquareMAT(4719edc02e5d652b). (wdcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), kMAT_TYPEcxx . . $ kMAT_TYPE#::SYMMETRICcxx . . $ kMAT_TYPE#SYMMETRIC., 0, false);
115 if (eigen_wd ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvaluescxx . . $ structEigen#eigenvalues. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).nPoints] >= 0.0)
116 return 0.0;
117
118 /*
119 Set up the B matrix according to eq. 6 in Calliez.
120 */
121 autoMATcxx . . $ autoMAT# b = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (nPoints2, nPoints2);
122 b.partcxx . . $ automatrix#part(5c23e73480cfacfb). (1, nPoints, nPoints + 1, nPoints2) <<=cxx . . $ `operator<<=`(b78e8a4998bd3572). 2.0 *cxx . . $ `operator*`(63c818071e7f3db9). wdcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).();
123 b.partcxx . . $ automatrix#part(5c23e73480cfacfb). (nPoints + 1, nPoints2, 1, nPoints).diagonalcxx . . $ matrixview#diagonal(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(c555d170d72ff256). -1.0;
124 b.partcxx . . $ automatrix#part(5c23e73480cfacfb). (nPoints + 1, nPoints2, nPoints + 1, nPoints2) <<=cxx . . $ `operator<<=`(b78e8a4998bd3572). -4.0 *cxx . . $ `operator*`(63c818071e7f3db9). wdsqrtcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).();
125 /*
126 Get eigenvalues of B
127 */
128 autoEigencxx . . $ autoEigen# eigen = Eigen_createFromSquareMATcxx . . $ Eigen_createFromSquareMAT(4719edc02e5d652b). (bcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), kMAT_TYPEcxx . . $ kMAT_TYPE#::GENERALcxx . . $ kMAT_TYPE#GENERAL., 0, false);
129 /*
130 Get largest real eigenvalue. This value needs to be larger than or equal to zero,
131 therefore we left-clip against zero.
132 Even if all eigenvalues turn out to be negative we set it to zero.
133 */
134 double largestRealEigenvalue = 0.0;
135 for (integercxx . . $ integer# i = 1; i <= nPoints2; i ++)
136 if (eigen ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvalues_imcxx . . $ structEigen#eigenvalues_im. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] == 0.0 && eigen ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvaluescxx . . $ structEigen#eigenvalues. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] > largestRealEigenvalue)
137 largestRealEigenvalue = eigen ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvaluescxx . . $ structEigen#eigenvalues. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i];
138 return largestRealEigenvalue;
139 } catch (MelderErrorcxx . . $ MelderError#) {
140 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Additive constant not calculated.");
141 }
142 }
143
144 autoSimilaritycxx . . $ autoSimilarity# Similarity_createcxx . . $ Similarity_create(32730dd5c8f6d644). (integercxx . . $ integer# numberOfPoints) {
145 try {
146 autoSimilaritycxx . . $ autoSimilarity# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Similarity);
147 Proximity_initcxx . . $ Proximity_init(bae185fe52776cec). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), numberOfPoints);
148 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
149 } catch (MelderErrorcxx . . $ MelderError#) {
150 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Similarity not created.");
151 }
152 }
153
154 /* End of file Proximity.cpp */