| 1 |
/* Proximity_and_Distance.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2018-2019 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 |
#include "Proximity_and_Distance.h" |
| 20 |
#include "TableOfReal_extensions.h" |
| 21 |
|
| 22 |
autoDistance Dissimilarity_to_Distance (Dissimilarity me, kMDS_AnalysisScale scale) { |
| 23 |
try { |
| 24 |
double additiveConstant = 0.0; |
| 25 |
|
| 26 |
autoDistance thee = Distance_create (my numberOfRows); |
| 27 |
TableOfReal_copyLabels (me, thee.get(), 1, 1); |
| 28 |
if (scale == kMDS_AnalysisScale::ORDINAL) { |
| 29 |
additiveConstant = Dissimilarity_getAdditiveConstant (me); |
| 30 |
Melder_require (isdefined (additiveConstant), |
| 31 |
U"The additive constant could not be determined. Something is wrong with your Dissimilarity."); |
| 32 |
} |
| 33 |
for (integer i = 1; i <= my numberOfRows - 1; i ++) { |
| 34 |
for (integer j = i + 1; j <= my numberOfColumns; j ++) { |
| 35 |
const double d = 0.5 * (my data [i] [j] + my data [j] [i]) + additiveConstant; |
| 36 |
thy data [i] [j] = thy data [j] [i] = d; |
| 37 |
} |
| 38 |
} |
| 39 |
return thee; |
| 40 |
} catch (MelderError) { |
| 41 |
Melder_throw (me, U": no Distance created."); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
autoDissimilarity Distance_to_Dissimilarity (Distance me) { |
| 46 |
try { |
| 47 |
autoDissimilarity thee = Dissimilarity_create (my numberOfRows); |
| 48 |
TableOfReal_copyLabels (me, thee.get(), 1, 1); |
| 49 |
Melder_assert (thy data.ncol == my numberOfColumns); |
| 50 |
thy data.all() <<= my data.all(); |
| 51 |
return thee; |
| 52 |
} catch (MelderError) { |
| 53 |
Melder_throw (U"Dissimilarity not created from Distance."); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
autoDistanceList DissimilarityList_to_DistanceList (DissimilarityList me, kMDS_AnalysisScale scale) { |
| 58 |
try { |
| 59 |
autoDistanceList thee = DistanceList_create (); |
| 60 |
for (integer i = 1; i <= my size; i ++) { |
| 61 |
autoDistance him = Dissimilarity_to_Distance (my at [i], scale); |
| 62 |
conststring32 name = Thing_getName (my at [i]); |
| 63 |
Thing_setName (him.get(), name ? name : U"untitled"); |
| 64 |
thy addItem_move (him.move()); |
| 65 |
} |
| 66 |
return thee; |
| 67 |
} catch (MelderError) { |
| 68 |
Melder_throw (me, U": no DistanceList created."); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/* End of file Proximity_and_Distance.cpp */ |