| 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" |
| 25 | #include "Distance.h" |
| 26 | #include "Proximity_and_Distance.h" |
| 27 | #include "TableOfReal_extensions.h" |
| 28 | |
| 29 | #include "NUM2.h" |
| 30 | |
| 31 | Thing_implement (Proximity, TableOfReal, 0); |
| 32 | |
| 33 | void Proximity_init (Proximity me, integer numberOfPoints) { |
| 34 | TableOfReal_init (me, numberOfPoints, numberOfPoints); |
| 35 | TableOfReal_setSequentialRowLabels (me, 0, 0, nullptr, 1, 1); |
| 36 | TableOfReal_setSequentialColumnLabels (me, 0, 0, nullptr, 1, 1); |
| 37 | } |
| 38 | |
| 39 | Thing_implement (Dissimilarity, Proximity, 0); |
| 40 | |
| 41 | static bool Dissimilarity_hasNoNegativeValues (Dissimilarity me) { |
| 42 | for (integer i = 1; i <= my numberOfRows - 1; i ++) |
| 43 | for (integer j = i + 1; j <= my numberOfRows; j ++) |
| 44 | if (my data [i] [j] < 0.0) |
| 45 | return false; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | autoDissimilarity Dissimilarity_create (integer numberOfPoints) { |
| 50 | try { |
| 51 | autoDissimilarity me = Thing_new (Dissimilarity); |
| 52 | Proximity_init (me.get(), numberOfPoints); |
| 53 | return me; |
| 54 | } catch (MelderError) { |
| 55 | Melder_throw (U"Dissimilarity not created."); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | autoDissimilarity Dissimilarity_createLetterRExample (double noiseStd) { |
| 60 | try { |
| 61 | autoConfiguration r = Configuration_createLetterRExample (1); |
| 62 | autoDistance d = Configuration_to_Distance (r.get()); |
| 63 | autoDissimilarity me = Distance_to_Dissimilarity (d.get()); |
| 64 | Thing_setName (me.get(), U"R"); |
| 65 | |
| 66 | for (integer i = 1; i <= my numberOfRows - 1; i ++) { |
| 67 | for (integer j = i + 1; j <= my numberOfRows; j ++) { |
| 68 | const double dis = my data [i] [j]; |
| 69 | my data [j] [i] = my data [i] [j] = dis * dis + 5.0 + NUMrandomUniform (0.0, noiseStd); |
| 70 | } |
| 71 | } |
| 72 | return me; |
| 73 | } catch (MelderError) { |
| 74 | Melder_throw (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_getAdditiveConstant (Dissimilarity me) { |
| 84 | try { |
| 85 | const integer nPoints = my numberOfRows, nPoints2 = 2 * nPoints; |
| 86 | Melder_require (nPoints > 0, |
| 87 | U"Matrix part should not be empty."); |
| 88 | |
| 89 | Melder_require (Dissimilarity_hasNoNegativeValues (me), |
| 90 | U"Dissimilarities should not be negative."); |
| 91 | |
| 92 | autoMAT wd = zero_MAT (nPoints, nPoints); |
| 93 | autoMAT wdsqrt = zero_MAT (nPoints, nPoints); |
| 94 | /* |
| 95 | The matrices D & D1/2 with distances (squared and linear) |
| 96 | */ |
| 97 | for (integer i = 1; i <= nPoints - 1; i ++) { |
| 98 | for (integer j = i + 1; j <= nPoints; j ++) { |
| 99 | const double proximity = (my data [i] [j] + my data [j] [i]) / 2.0; |
| 100 | wdsqrt [j] [i] = wdsqrt [i] [j] = - proximity / 2.0; // djmw 20180830 |
| 101 | wd [j] [i] = wd [i] [j] = - proximity * proximity / 2.0; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | doubleCentre_MAT_inout (wdsqrt.get()); |
| 106 | doubleCentre_MAT_inout (wd.get()); |
| 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 | autoEigen eigen_wd = Eigen_createFromSquareMAT (wd.get(), kMAT_TYPE::SYMMETRIC, 0, false); |
| 115 | if (eigen_wd -> eigenvalues [nPoints] >= 0.0) |
| 116 | return 0.0; |
| 117 | |
| 118 | /* |
| 119 | Set up the B matrix according to eq. 6 in Calliez. |
| 120 | */ |
| 121 | autoMAT b = zero_MAT (nPoints2, nPoints2); |
| 122 | b.part (1, nPoints, nPoints + 1, nPoints2) <<= 2.0 * wd.get(); |
| 123 | b.part (nPoints + 1, nPoints2, 1, nPoints).diagonal() <<= -1.0; |
| 124 | b.part (nPoints + 1, nPoints2, nPoints + 1, nPoints2) <<= -4.0 * wdsqrt.get(); |
| 125 | /* |
| 126 | Get eigenvalues of B |
| 127 | */ |
| 128 | autoEigen eigen = Eigen_createFromSquareMAT (b.get(), 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 (integer i = 1; i <= nPoints2; i ++) |
| 136 | if (eigen -> eigenvalues_im [i] == 0.0 && eigen -> eigenvalues [i] > largestRealEigenvalue) |
| 137 | largestRealEigenvalue = eigen -> eigenvalues [i]; |
| 138 | return largestRealEigenvalue; |
| 139 | } catch (MelderError) { |
| 140 | Melder_throw (U"Additive constant not calculated."); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | autoSimilarity Similarity_create (integer numberOfPoints) { |
| 145 | try { |
| 146 | autoSimilarity me = Thing_new (Similarity); |
| 147 | Proximity_init (me.get(), numberOfPoints); |
| 148 | return me; |
| 149 | } catch (MelderError) { |
| 150 | Melder_throw (U"Similarity not created."); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* End of file Proximity.cpp */ |