| 1 | /* MDSVec.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 "NUM2.h" |
| 20 | #include "MDSVec.h" |
| 21 | |
| 22 | #include "oo_DESTROY.h" |
| 23 | #include "MDSVec_def.h" |
| 24 | #include "oo_COPY.h" |
| 25 | #include "MDSVec_def.h" |
| 26 | #include "oo_EQUAL.h" |
| 27 | #include "MDSVec_def.h" |
| 28 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 | #include "MDSVec_def.h" |
| 30 | #include "oo_WRITE_TEXT.h" |
| 31 | #include "MDSVec_def.h" |
| 32 | #include "oo_WRITE_BINARY.h" |
| 33 | #include "MDSVec_def.h" |
| 34 | #include "oo_READ_TEXT.h" |
| 35 | #include "MDSVec_def.h" |
| 36 | #include "oo_READ_BINARY.h" |
| 37 | #include "MDSVec_def.h" |
| 38 | #include "oo_DESCRIPTION.h" |
| 39 | #include "MDSVec_def.h" |
| 40 | |
| 41 | Thing_implement (MDSVec, Daata, 0); |
| 42 | |
| 43 | void structMDSVec :: v1_info () { |
| 44 | structDaata :: v1_info (); |
| 45 | MelderInfo_writeLine (U"Number of points: ", numberOfPoints); |
| 46 | MelderInfo_writeLine (U"Number of proximities: ", numberOfProximities); |
| 47 | } |
| 48 | |
| 49 | autoMDSVec MDSVec_create (integer numberOfPoints) { |
| 50 | try { |
| 51 | autoMDSVec me = Thing_new (MDSVec); |
| 52 | my numberOfPoints = numberOfPoints; |
| 53 | my numberOfProximities = numberOfPoints * (numberOfPoints - 1) / 2; |
| 54 | my proximity = zero_VEC (my numberOfProximities); |
| 55 | my rowIndex = zero_INTVEC (my numberOfProximities); |
| 56 | my columnIndex = zero_INTVEC (my numberOfProximities); |
| 57 | return me; |
| 58 | } catch (MelderError) { |
| 59 | Melder_throw (U"MDSVec not created."); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | autoMDSVec Dissimilarity_to_MDSVec (Dissimilarity me) { |
| 64 | try { |
| 65 | autoMDSVec thee = MDSVec_create (my numberOfRows); |
| 66 | |
| 67 | integer n = 0; |
| 68 | for (integer irow = 1; irow <= my numberOfRows - 1; irow ++) { |
| 69 | for (integer icol = irow + 1; icol <= my numberOfColumns; icol ++) { |
| 70 | const double f = 0.5 * (my data [irow] [icol] + my data [icol] [irow]); |
| 71 | if (f > 0.0) { |
| 72 | n ++; |
| 73 | thy proximity [n] = f; |
| 74 | thy rowIndex [n] = irow; |
| 75 | thy columnIndex [n] = icol; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | thy numberOfProximities = n; |
| 80 | VECsort3_inplace (thy proximity.get(), thy rowIndex.get(), thy columnIndex.get(), false); |
| 81 | return thee; |
| 82 | } catch (MelderError) { |
| 83 | Melder_throw (me, U": no MDSVec created."); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /*********************** MDSVECS *******************************************/ |
| 88 | |
| 89 | Thing_implement (MDSVecList, Ordered, 0); |
| 90 | |
| 91 | autoMDSVecList DissimilarityList_to_MDSVecList (DissimilarityList me) { |
| 92 | try { |
| 93 | autoMDSVecList thee = MDSVecList_create (); |
| 94 | for (integer i = 1; i <= my size; i ++) { |
| 95 | autoMDSVec him = Dissimilarity_to_MDSVec (my at [i]); |
| 96 | Thing_setName (him.get(), Thing_getName (my at [i])); |
| 97 | thy addItem_move (him.move()); |
| 98 | } |
| 99 | return thee; |
| 100 | } catch (MelderError) { |
| 101 | Melder_throw (me, U": no MDSVecs created."); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* End of file MDSVec.cpp */ |
| 106 |