| 1 | /* Distributions.cpp |
| 2 | * |
| 3 | * Copyright (C) 1997-2012,2014-2018,2022,2024 Paul Boersma |
| 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. |
| 13 | * See the GNU 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 "Distributions.h"cxx . . $ `<file>/stat/Distributions.h`/ |
| 20 | |
| 21 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Distributions, TableOfReal, 0); |
| 22 | |
| 23 | void structDistributionscxx . . $ structDistributions# :: v1_infocxx . . $ structDistributions#v1_info(49f6e7a06ebc5aa8). () { |
| 24 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 25 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of distributions: ", numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 26 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of values: ", numberOfRowscxx . . $ structTableOfReal#numberOfRows.); |
| 27 | } |
| 28 | |
| 29 | autoDistributionscxx . . $ autoDistributions# Distributions_createcxx . . $ Distributions_create(6e2acaa0535ba708). (integercxx . . $ integer# numberOfRows, integercxx . . $ integer# numberOfColumns) { |
| 30 | try { |
| 31 | autoDistributionscxx . . $ autoDistributions# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Distributions); |
| 32 | TableOfReal_initcxx . . $ TableOfReal_init(cfadf70b559ed315). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), numberOfRows, numberOfColumns); |
| 33 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 34 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 35 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Distributions not created."); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void Distributions_checkSpecifiedColumnNumberWithinRangecxx . . $ Distributions_checkSpecifiedColumnNumberWithinRange(1941a5510fdd3f26). (Distributionscxx . . $ Distributions# me, integercxx . . $ integer# columnNumber) { |
| 40 | if (columnNumber < 1) |
| 41 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": the specified column number is ", columnNumber, U", but should be at least 1."); |
| 42 | if (columnNumber > mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.) |
| 43 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": the specified column number is ", columnNumber, U", but should be at most my number of columns (", mycxx . . $ `melder/melder.h:44:9`! numberOfColumns, U")."); |
| 44 | } |
| 45 | |
| 46 | void Distributions_peekcxx . . $ Distributions_peek(70b1239ea03afa). (Distributionscxx . . $ Distributions# me, integercxx . . $ integer# column, conststring32cxx . . $ conststring32# *out_string, integercxx . . $ integer# *out_number) { |
| 47 | Distributions_checkSpecifiedColumnNumberWithinRangecxx . . $ Distributions_checkSpecifiedColumnNumberWithinRange(1941a5510fdd3f26). (me, column); |
| 48 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. < 1) |
| 49 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": I have no candidates."); |
| 50 | longdoublecxx . . $ longdouble# total = 0.0; |
| 51 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 52 | total += (longdoublecxx . . $ longdouble#) mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).column]; |
| 53 | } |
| 54 | if (total <= 0.0) |
| 55 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": the total weight of column ", column, U" is not positive."); |
| 56 | integercxx . . $ integer# irow; |
| 57 | do { |
| 58 | double rand = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0, (double) total); |
| 59 | longdoublecxx . . $ longdouble# sum = 0.0; |
| 60 | for (irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 61 | sum += mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).column]; |
| 62 | if (rand <= sum) break; |
| 63 | } |
| 64 | } while (irow > mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.); // guard against rounding errors |
| 65 | if (! mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow]) |
| 66 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no string in row ", irow, U"."); |
| 67 | if (out_string) |
| 68 | *out_string = mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 69 | if (out_number) |
| 70 | *out_number = irow; |
| 71 | } |
| 72 | |
| 73 | double Distributions_getProbabilitycxx . . $ Distributions_getProbability(dd8c957dcbd1db3e). (Distributionscxx . . $ Distributions# me, conststring32cxx . . $ conststring32# string, integercxx . . $ integer# column) { |
| 74 | integercxx . . $ integer# row, rowOfString = 0; |
| 75 | longdoublecxx . . $ longdouble# total = 0.0; |
| 76 | if (column < 1 || column > mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.) return undefinedcxx . . $ undefined.; |
| 77 | for (row = 1; row <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; row ++) { |
| 78 | total += mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).row] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).column]; |
| 79 | if (mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).row] && str32equcxx . . $ `melder/melder_str32.h:122:9`! (mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).row].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), string)) |
| 80 | rowOfString = row; |
| 81 | } |
| 82 | if (total <= 0.0) return undefinedcxx . . $ undefined.; |
| 83 | if (rowOfString == 0) return 0.0; |
| 84 | return mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).rowOfString] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).column] / (double) total; |
| 85 | } |
| 86 | |
| 87 | double Distributionses_getMeanAbsoluteDifferencecxx . . $ Distributionses_getMeanAbsoluteDifference(fa02446ffd69dcfb). (Distributionscxx . . $ Distributions# me, Distributionscxx . . $ Distributions# thee, integercxx . . $ integer# column) { |
| 88 | if (column < 1 || column > mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. || column > thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. || |
| 89 | mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. != thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.) return undefinedcxx . . $ undefined.; |
| 90 | longdoublecxx . . $ longdouble# total = 0.0; |
| 91 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 92 | total += (longdoublecxx . . $ longdouble#) fabscxx . . $ fabs(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).column] - thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).column]); |
| 93 | } |
| 94 | return (double) total / mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 95 | } |
| 96 | |
| 97 | static void unicizecxx . . $ unicize(e78560f82c3c1b80). (Distributionscxx . . $ Distributions# me) { |
| 98 | /* Must have been sorted beforehand. */ |
| 99 | integercxx . . $ integer# nrow = 0, ifrom = 1; |
| 100 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 101 | if (irow == mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. || !! mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow] != !! mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow + 1] || |
| 102 | mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow] && ! str32equcxx . . $ `melder/melder_str32.h:122:9`! (mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow + 1].getcxx . . $ _autostring#get(5a2a78a048fb49a8).())) |
| 103 | { |
| 104 | /* |
| 105 | Detected a change. |
| 106 | */ |
| 107 | nrow ++; |
| 108 | integercxx . . $ integer# ito = irow; |
| 109 | /* |
| 110 | Move row 'ifrom' to 'nrow'. May be the same row. |
| 111 | */ |
| 112 | if (ifrom != nrow) { |
| 113 | mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).nrow] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).ifrom].movecxx . . $ _autostring#move(bd43c759570f178f).(); |
| 114 | for (integercxx . . $ integer# icol = 1; icol <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) |
| 115 | mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).nrow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifrom] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol]; |
| 116 | } |
| 117 | /* |
| 118 | Purge rows from 'ifrom'+1 to 'ito'. |
| 119 | */ |
| 120 | for (integercxx . . $ integer# j = ifrom + 1; j <= ito; j ++) { |
| 121 | mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).j]. resetcxx . . $ _autostring#reset(49f6e7a06ebc5aa8).(); |
| 122 | for (integercxx . . $ integer# icol = 1; icol <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) |
| 123 | mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).nrow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] += mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol]; |
| 124 | } |
| 125 | ifrom = ito + 1; |
| 126 | } |
| 127 | } |
| 128 | mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. = nrow; |
| 129 | } |
| 130 | |
| 131 | autoDistributionscxx . . $ autoDistributions# Distributions_addTwocxx . . $ Distributions_addTwo(47767f4f99e5fef0). (Distributionscxx . . $ Distributions# me, Distributionscxx . . $ Distributions# thee) { |
| 132 | try { |
| 133 | autoDistributionscxx . . $ autoDistributions# him = TablesOfReal_appendcxx . . $ TablesOfReal_append(8a870f738eddd928). (me, thee).static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf). <structDistributionscxx . . $ structDistributions#>(); |
| 134 | TableOfReal_sortByLabelcxx . . $ TableOfReal_sortByLabel(cfadf70b559ed315). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0, 0); |
| 135 | unicizecxx . . $ unicize(e78560f82c3c1b80). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 136 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 137 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 138 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": not added."); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | autoDistributionscxx . . $ autoDistributions# Distributions_addManycxx . . $ Distributions_addMany(7ff5de91a1c53937). (OrderedOfcxx . . $ OrderedOf#<structDistributionscxx . . $ structDistributions#>* me) { |
| 143 | try { |
| 144 | autoDistributionscxx . . $ autoDistributions# thee = TablesOfReal_appendManycxx . . $ TablesOfReal_appendMany(fa20816355827365). ((OrderedOfcxx . . $ OrderedOf#<structTableOfRealcxx . . $ structTableOfReal#>*) me).static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf). <structDistributionscxx . . $ structDistributions#>(); // FIXME cast |
| 145 | TableOfReal_sortByLabelcxx . . $ TableOfReal_sortByLabel(cfadf70b559ed315). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0, 0); |
| 146 | unicizecxx . . $ unicize(e78560f82c3c1b80). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 147 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 148 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 149 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Distributions objects not added."); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* End of file Distributions.cpp */ |