MDSVec.cpp 3.1 KB

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