| 1 | /* TableOfReal_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2023 David Weenink, 2011-2012,2023-2025 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 3 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 20000202 17 typos in F1-3,L1-3 table corrected |
| 21 | djmw 20030707 Changed TableOfReal_drawVectors interface. |
| 22 | djmw 20031030 Added TableOfReal_appendColumns |
| 23 | djmw 20031216 Interface change in TableOfReal_choleskyDecomposition |
| 24 | djmw 20040108 Corrected a memory leak in TableOfReal_drawBoxPlots |
| 25 | djmw 20040211 Modified TableOfReal_copyLabels behaviour: copy NULL-labels too. |
| 26 | djmw 20040511 Removed TableOfReal_extractRowsByRowLabel,TableOfReal_selectColumnsWhereRow |
| 27 | djmw 20040617 Removed column selection bug in TableOfReal_drawRowsAsHistogram |
| 28 | djmw 20041105 Added TableOfReal_createFromVanNieropData_25females |
| 29 | djmw 20041115 TableOfReal_drawScatterPlot: plotting a NULL-label crashed Praat. |
| 30 | djmw 20041213 Added TableOfReal_createFromWeeninkData. |
| 31 | djmw 20050221 TableOfReal_meansByRowLabels, extra reduce parameter. |
| 32 | djmw 20050222 TableOfReal_drawVectors didn't draw rowlabels. |
| 33 | djmw 20050512 TableOfReal TableOfReal_meansByRowLabels crashed if first label in sorted was NULL. |
| 34 | djmw 20051116 TableOfReal_drawScatterPlot draw reverse permitted by choosing xmin > xmax and/or ymin>ymax |
| 35 | djmw 20060301 TableOfReal_meansByRowLabels extra medianize |
| 36 | djmw 20060626 Extra NULL argument for ExecRE. |
| 37 | djmw 20070822 wchar |
| 38 | djmw 20070902 Better error messages (object type and name feedback) |
| 39 | djmw 20070614 updated to version 1.30 of regular expressions. |
| 40 | djmw 20071202 Melder_warning<n> |
| 41 | djmw 20080122 float -> double |
| 42 | djmw 20081119 +TableOfReal_areAllCellsDefined |
| 43 | djmw 20090506 +setInner for _drawScatterPlotMatrix |
| 44 | djmw 20091009 +TableOfReal_drawColumnAsDistribution |
| 45 | djmw 20100222 Corrected a bug in TableOfReal_copyOneRowWithLabel which caused label corruption if |
| 46 | from and to table were equal and rows were equal too. |
| 47 | djmw 20111110 Use autostringvector |
| 48 | djmw 20111123 Always use Melder_wcscmp |
| 49 | */ |
| 50 | |
| 51 | #include "Correlation.h" |
| 52 | #include "Covariance.h" |
| 53 | #include "Graphics_extensions.h" |
| 54 | #include "Matrix_extensions.h" |
| 55 | #include "NUMlapack.h" |
| 56 | #include "NUM2.h" |
| 57 | #include "SVD.h" |
| 58 | #include "Table_extensions.h" |
| 59 | #include "TableOfReal_extensions.h" |
| 60 | #include "TableOfReal_and_Permutation.h" |
| 61 | #include "regularExp.h" |
| 62 | #include "Formula.h" |
| 63 | |
| 64 | #define EMPTY_STRING(s) (! (s) || s [0] == '\0') |
| 65 | |
| 66 | #define Graphics_ARROW 1 |
| 67 | #define Graphics_TWOWAYARROW 2 |
| 68 | #define Graphics_LINE 3 |
| 69 | |
| 70 | static autoTableOfReal TableOfReal_TableOfReal_columnCorrelations (TableOfReal me, TableOfReal thee, bool center, bool normalize); |
| 71 | static autoTableOfReal TableOfReal_TableOfReal_rowCorrelations (TableOfReal me, TableOfReal thee, bool center, bool normalize); |
| 72 | |
| 73 | integer TableOfReal_getColumnIndexAtMaximumInRow (TableOfReal me, integer rowNumber) { |
| 74 | return ( rowNumber > 0 && rowNumber <= my numberOfRows ? |
| 75 | NUMmaxPos (my data.row (rowNumber)) : 0 ); |
| 76 | } |
| 77 | |
| 78 | conststring32 TableOfReal_getColumnLabelAtMaximumInRow (TableOfReal me, integer rowNumber) { |
| 79 | integer columnNumber = TableOfReal_getColumnIndexAtMaximumInRow (me, rowNumber); |
| 80 | return my v_getColStr (columnNumber); |
| 81 | } |
| 82 | |
| 83 | void TableOfReal_copyOneRowWithLabel (TableOfReal me, TableOfReal thee, integer myrow, integer thyrow) { |
| 84 | try { |
| 85 | if (me == thee && myrow == thyrow) |
| 86 | return; |
| 87 | Melder_require (myrow > 0 && myrow <= my numberOfRows && thyrow > 0 && thyrow <= thy numberOfRows && my numberOfColumns == thy numberOfColumns, |
| 88 | U"The dimensions do not fit."); |
| 89 | |
| 90 | thy rowLabels [thyrow] = Melder_dup (my rowLabels [myrow].get()); |
| 91 | thy data.row (thyrow) <<= my data.row (myrow); |
| 92 | } catch (MelderError) { |
| 93 | Melder_throw (me, U": row ", myrow, U" not copied to ", thee); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | bool TableOfReal_hasRowLabels (TableOfReal me) { |
| 98 | if (! my rowLabels) |
| 99 | return false; |
| 100 | for (integer i = 1; i <= my numberOfRows; i ++) |
| 101 | if (EMPTY_STRING (my rowLabels [i])) |
| 102 | return false; |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool TableOfReal_hasColumnLabels (TableOfReal me) { |
| 107 | if (! my columnLabels) |
| 108 | return false; |
| 109 | for (integer i = 1; i <= my numberOfColumns; i ++) |
| 110 | if (EMPTY_STRING (my columnLabels [i])) |
| 111 | return false; |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | autoTableOfReal TableOfReal_createIrisDataset () { |
| 116 | const double iris [150] [4] = { |
| 117 | {5.1, 3.5, 1.4, 0.2}, {4.9, 3.0, 1.4, 0.2}, {4.7, 3.2, 1.3, 0.2}, {4.6, 3.1, 1.5, 0.2}, {5.0, 3.6, 1.4, 0.2}, |
| 118 | {5.4, 3.9, 1.7, 0.4}, {4.6, 3.4, 1.4, 0.3}, {5.0, 3.4, 1.5, 0.2}, {4.4, 2.9, 1.4, 0.2}, {4.9, 3.1, 1.5, 0.1}, |
| 119 | {5.4, 3.7, 1.5, 0.2}, {4.8, 3.4, 1.6, 0.2}, {4.8, 3.0, 1.4, 0.1}, {4.3, 3.0, 1.1, 0.1}, {5.8, 4.0, 1.2, 0.2}, |
| 120 | {5.7, 4.4, 1.5, 0.4}, {5.4, 3.9, 1.3, 0.4}, {5.1, 3.5, 1.4, 0.3}, {5.7, 3.8, 1.7, 0.3}, {5.1, 3.8, 1.5, 0.3}, |
| 121 | {5.4, 3.4, 1.7, 0.2}, {5.1, 3.7, 1.5, 0.4}, {4.6, 3.6, 1.0, 0.2}, {5.1, 3.3, 1.7, 0.5}, {4.8, 3.4, 1.9, 0.2}, |
| 122 | {5.0, 3.0, 1.6, 0.2}, {5.0, 3.4, 1.6, 0.4}, {5.2, 3.5, 1.5, 0.2}, {5.2, 3.4, 1.4, 0.2}, {4.7, 3.2, 1.6, 0.2}, |
| 123 | {4.8, 3.1, 1.6, 0.2}, {5.4, 3.4, 1.5, 0.4}, {5.2, 4.1, 1.5, 0.1}, {5.5, 4.2, 1.4, 0.2}, {4.9, 3.1, 1.5, 0.2}, |
| 124 | {5.0, 3.2, 1.2, 0.2}, {5.5, 3.5, 1.3, 0.2}, {4.9, 3.6, 1.4, 0.1}, {4.4, 3.0, 1.3, 0.2}, {5.1, 3.4, 1.5, 0.2}, |
| 125 | {5.0, 3.5, 1.3, 0.3}, {4.5, 2.3, 1.3, 0.3}, {4.4, 3.2, 1.3, 0.2}, {5.0, 3.5, 1.6, 0.6}, {5.1, 3.8, 1.9, 0.4}, |
| 126 | {4.8, 3.0, 1.4, 0.3}, {5.1, 3.8, 1.6, 0.2}, {4.6, 3.2, 1.4, 0.2}, {5.3, 3.7, 1.5, 0.2}, {5.0, 3.3, 1.4, 0.2}, |
| 127 | {7.0, 3.2, 4.7, 1.4}, {6.4, 3.2, 4.5, 1.5}, {6.9, 3.1, 4.9, 1.5}, {5.5, 2.3, 4.0, 1.3}, {6.5, 2.8, 4.6, 1.5}, |
| 128 | {5.7, 2.8, 4.5, 1.3}, {6.3, 3.3, 4.7, 1.6}, {4.9, 2.4, 3.3, 1.0}, {6.6, 2.9, 4.6, 1.3}, {5.2, 2.7, 3.9, 1.4}, |
| 129 | {5.0, 2.0, 3.5, 1.0}, {5.9, 3.0, 4.2, 1.5}, {6.0, 2.2, 4.0, 1.0}, {6.1, 2.9, 4.7, 1.4}, {5.6, 2.9, 3.6, 1.3}, |
| 130 | {6.7, 3.1, 4.4, 1.4}, {5.6, 3.0, 4.5, 1.5}, {5.8, 2.7, 4.1, 1.0}, {6.2, 2.2, 4.5, 1.5}, {5.6, 2.5, 3.9, 1.1}, |
| 131 | {5.9, 3.2, 4.8, 1.8}, {6.1, 2.8, 4.0, 1.3}, {6.3, 2.5, 4.9, 1.5}, {6.1, 2.8, 4.7, 1.2}, {6.4, 2.9, 4.3, 1.3}, |
| 132 | {6.6, 3.0, 4.4, 1.4}, {6.8, 2.8, 4.8, 1.4}, {6.7, 3.0, 5.0, 1.7}, {6.0, 2.9, 4.5, 1.5}, {5.7, 2.6, 3.5, 1.0}, |
| 133 | {5.5, 2.4, 3.8, 1.1}, {5.5, 2.4, 3.7, 1.0}, {5.8, 2.7, 3.9, 1.2}, {6.0, 2.7, 5.1, 1.6}, {5.4, 3.0, 4.5, 1.5}, |
| 134 | {6.0, 3.4, 4.5, 1.6}, {6.7, 3.1, 4.7, 1.5}, {6.3, 2.3, 4.4, 1.3}, {5.6, 3.0, 4.1, 1.3}, {5.5, 2.5, 4.0, 1.3}, |
| 135 | {5.5, 2.6, 4.4, 1.2}, {6.1, 3.0, 4.6, 1.4}, {5.8, 2.6, 4.0, 1.2}, {5.0, 2.3, 3.3, 1.0}, {5.6, 2.7, 4.2, 1.3}, |
| 136 | {5.7, 3.0, 4.2, 1.2}, {5.7, 2.9, 4.2, 1.3}, {6.2, 2.9, 4.3, 1.3}, {5.1, 2.5, 3.0, 1.1}, {5.7, 2.8, 4.1, 1.3}, |
| 137 | {6.3, 3.3, 6.0, 2.5}, {5.8, 2.7, 5.1, 1.9}, {7.1, 3.0, 5.9, 2.1}, {6.3, 2.9, 5.6, 1.8}, {6.5, 3.0, 5.8, 2.2}, |
| 138 | {7.6, 3.0, 6.6, 2.1}, {4.9, 2.5, 4.5, 1.7}, {7.3, 2.9, 6.3, 1.8}, {6.7, 2.5, 5.8, 1.8}, {7.2, 3.6, 6.1, 2.5}, |
| 139 | {6.5, 3.2, 5.1, 2.0}, {6.4, 2.7, 5.3, 1.9}, {6.8, 3.0, 5.5, 2.1}, {5.7, 2.5, 5.0, 2.0}, {5.8, 2.8, 5.1, 2.4}, |
| 140 | {6.4, 3.2, 5.3, 2.3}, {6.5, 3.0, 5.5, 1.8}, {7.7, 3.8, 6.7, 2.2}, {7.7, 2.6, 6.9, 2.3}, {6.0, 2.2, 5.0, 1.5}, |
| 141 | {6.9, 3.2, 5.7, 2.3}, {5.6, 2.8, 4.9, 2.0}, {7.7, 2.8, 6.7, 2.0}, {6.3, 2.7, 4.9, 1.8}, {6.7, 3.3, 5.7, 2.1}, |
| 142 | {7.2, 3.2, 6.0, 1.8}, {6.2, 2.8, 4.8, 1.8}, {6.1, 3.0, 4.9, 1.8}, {6.4, 2.8, 5.6, 2.1}, {7.2, 3.0, 5.8, 1.6}, |
| 143 | {7.4, 2.8, 6.1, 1.9}, {7.9, 3.8, 6.4, 2.0}, {6.4, 2.8, 5.6, 2.2}, {6.3, 2.8, 5.1, 1.5}, {6.1, 2.6, 5.6, 1.4}, |
| 144 | {7.7, 3.0, 6.1, 2.3}, {6.3, 3.4, 5.6, 2.4}, {6.4, 3.1, 5.5, 1.8}, {6.0, 3.0, 4.8, 1.8}, {6.9, 3.1, 5.4, 2.1}, |
| 145 | {6.7, 3.1, 5.6, 2.4}, {6.9, 3.1, 5.1, 2.3}, {5.8, 2.7, 5.1, 1.9}, {6.8, 3.2, 5.9, 2.3}, {6.7, 3.3, 5.7, 2.5}, |
| 146 | {6.7, 3.0, 5.2, 2.3}, {6.3, 2.5, 5.0, 1.9}, {6.5, 3.0, 5.2, 2.0}, {6.2, 3.4, 5.4, 2.3}, {5.9, 3.0, 5.1, 1.8} |
| 147 | }; |
| 148 | |
| 149 | try { |
| 150 | autoTableOfReal me = TableOfReal_create (150, 4); |
| 151 | |
| 152 | TableOfReal_setColumnLabel (me.get(), 1, U"sl"); |
| 153 | TableOfReal_setColumnLabel (me.get(), 2, U"sw"); |
| 154 | TableOfReal_setColumnLabel (me.get(), 3, U"pl"); |
| 155 | TableOfReal_setColumnLabel (me.get(), 4, U"pw"); |
| 156 | for (integer i = 1; i <= 150; i ++) { |
| 157 | const integer kind = (i - 1) / 50 + 1; |
| 158 | char32 const *label = kind == 1 ? U"1" : kind == 2 ? U"2" : U"3"; |
| 159 | for (integer j = 1; j <= 4; j ++) |
| 160 | my data [i] [j] = iris [i - 1] [j - 1]; |
| 161 | TableOfReal_setRowLabel (me.get(), i, label); |
| 162 | } |
| 163 | Thing_setName (me.get(), U"iris"); |
| 164 | return me; |
| 165 | } catch (MelderError) { |
| 166 | Melder_throw (U"TableOfReal from iris data not created."); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | autoStrings TableOfReal_extractRowLabels (TableOfReal me) { |
| 171 | try { |
| 172 | autoStrings thee = Thing_new (Strings); |
| 173 | |
| 174 | if (my numberOfRows > 0) { |
| 175 | thy strings = autoSTRVEC (my numberOfRows); |
| 176 | thy numberOfStrings = my numberOfRows; |
| 177 | for (integer i = 1; i <= my numberOfRows; i ++) { |
| 178 | const conststring32 label = my rowLabels [i] ? my rowLabels [i].get() : U"?"; |
| 179 | thy strings [i] = Melder_dup (label); |
| 180 | } |
| 181 | } |
| 182 | return thee; |
| 183 | } catch (MelderError) { |
| 184 | Melder_throw (me, U": row labels not extracted."); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | autoStrings TableOfReal_extractColumnLabels (TableOfReal me) { |
| 189 | try { |
| 190 | autoStrings thee = Thing_new (Strings); |
| 191 | |
| 192 | if (my numberOfColumns > 0) { |
| 193 | thy strings = autoSTRVEC (my numberOfColumns); |
| 194 | thy numberOfStrings = my numberOfColumns; |
| 195 | |
| 196 | for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 197 | const conststring32 label = my columnLabels [i] ? my columnLabels [i].get() : U"?"; |
| 198 | thy strings [i] = Melder_dup (label); |
| 199 | } |
| 200 | } |
| 201 | return thee; |
| 202 | } catch (MelderError) { |
| 203 | Melder_throw (me, U": column labels not extracted."); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | autoTableOfReal TableOfReal_transpose (TableOfReal me) { |
| 208 | try { |
| 209 | autoTableOfReal thee = TableOfReal_create (my numberOfColumns, my numberOfRows); |
| 210 | thy data.all() <<= my data.transpose(); |
| 211 | thy columnLabels.all() <<= my rowLabels.all(); |
| 212 | thy rowLabels.all() <<= my columnLabels.all(); |
| 213 | return thee; |
| 214 | } catch (MelderError) { |
| 215 | Melder_throw (me, U": not transposed."); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void TableOfReal_to_PatternList_and_Categories (TableOfReal me, integer fromrow, integer torow, integer fromcol, integer tocol, |
| 220 | autoPatternList *out_patternList, autoCategories *out_categories) |
| 221 | { |
| 222 | try { |
| 223 | integer ncols = my numberOfColumns, nrows = my numberOfRows; |
| 224 | fromrow = fromrow == 0 ? 1 : fromrow; |
| 225 | torow = torow == 0 ? nrows : torow; |
| 226 | Melder_require (fromrow > 0 && fromrow <= torow && torow <= nrows, |
| 227 | U"Invalid row selection."); |
| 228 | fromcol = fromcol == 0 ? 1 : fromcol; |
| 229 | tocol = tocol == 0 ? ncols : tocol; |
| 230 | |
| 231 | Melder_require (fromcol > 0 && fromcol <= tocol && tocol <= ncols, |
| 232 | U"Invalid column selection."); |
| 233 | |
| 234 | nrows = torow - fromrow + 1; |
| 235 | ncols = tocol - fromcol + 1; |
| 236 | autoPatternList ap = PatternList_create (nrows, ncols); |
| 237 | autoCategories ac = Categories_create (); |
| 238 | |
| 239 | integer row = 1; |
| 240 | for (integer i = fromrow; i <= torow; i ++, row ++) { |
| 241 | const char32 *s = my rowLabels [i] ? my rowLabels [i].get() : U"?"; |
| 242 | autoSimpleString item = SimpleString_create (s); |
| 243 | ac -> addItem_move (item.move()); |
| 244 | integer col = 1; |
| 245 | for (integer j = fromcol; j <= tocol; j ++, col ++) |
| 246 | ap -> z [row] [col] = my data [i] [j]; |
| 247 | } |
| 248 | if (out_patternList) |
| 249 | *out_patternList = ap.move(); |
| 250 | if (out_categories) |
| 251 | *out_categories = ac.move(); |
| 252 | } catch (MelderError) { |
| 253 | Melder_throw (U"PatternList and Categories not created from TableOfReal."); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void TableOfReal_drawRowsAsHistogram (TableOfReal me, Graphics g, constINTVECVU const& rowNumbers, integer colb, integer cole, double ymin, |
| 258 | double ymax, double xoffsetFraction, double interbarFraction, double interbarsFraction, constVECVU const& greys, bool garnish) |
| 259 | { |
| 260 | if (colb == 0) // undefined? |
| 261 | colb = 1; // sensible default: all |
| 262 | if (cole == 0) // undefined? |
| 263 | cole = my numberOfColumns; // sensible default: all |
| 264 | |
| 265 | Melder_require (NUMisSorted4 (1_integer, colb, cole, my numberOfColumns), |
| 266 | U"Invalid column numbers"); |
| 267 | |
| 268 | for (integer i = 1; i <= rowNumbers.size; i ++) { |
| 269 | const integer rowNumber = rowNumbers [i]; |
| 270 | Melder_require (rowNumber > 0 && rowNumber <= my numberOfRows, |
| 271 | U"Invalid row (", rowNumber, U")."); |
| 272 | if (ymin >= ymax) { |
| 273 | double min, max; |
| 274 | NUMextrema_u (my data.row (rowNumber).part (colb, cole), & min, & max); |
| 275 | if (isundef (min) || isundef (max)) |
| 276 | return; |
| 277 | if (i > 1) { |
| 278 | if (min < ymin) |
| 279 | ymin = min; |
| 280 | if (max > ymax) |
| 281 | ymax = max; |
| 282 | } else { |
| 283 | ymin = min; |
| 284 | ymax = max; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | Graphics_setWindow (g, 0.0, 1.0, ymin, ymax); |
| 290 | Graphics_setInner (g); |
| 291 | |
| 292 | const integer ncols = cole - colb + 1, nrows = rowNumbers.size; |
| 293 | const double bar_width = 1.0 / (ncols * nrows + 2.0 * xoffsetFraction + (ncols - 1) * interbarsFraction + ncols * (nrows - 1) * interbarFraction); |
| 294 | const double dx = (interbarsFraction + nrows + (nrows - 1) * interbarFraction) * bar_width; |
| 295 | |
| 296 | for (integer i = 1; i <= nrows; i ++) { |
| 297 | const integer rowNumber = rowNumbers [i]; |
| 298 | const double xb = xoffsetFraction * bar_width + (i - 1) * (1.0 + interbarFraction) * bar_width; |
| 299 | |
| 300 | const double grey = greys [1 + (i - 1) % greys.size]; // cycle through the colours |
| 301 | double x1 = xb; |
| 302 | for (integer j = colb; j <= cole; j ++) { |
| 303 | const double x2 = x1 + bar_width; |
| 304 | double y2 = my data [rowNumber] [j]; |
| 305 | if (y2 > ymin) { |
| 306 | if (y2 > ymax) |
| 307 | y2 = ymax; |
| 308 | Graphics_setGrey (g, grey); |
| 309 | Graphics_fillRectangle (g, x1, x2, ymin, y2); |
| 310 | Graphics_setGrey (g, 0.0); // black |
| 311 | Graphics_rectangle (g, x1, x2, ymin, y2); |
| 312 | } |
| 313 | x1 += dx; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | Graphics_unsetInner (g); |
| 318 | |
| 319 | if (garnish) { |
| 320 | double xb = (xoffsetFraction + 0.5 * (nrows + (nrows - 1) * interbarFraction)) * bar_width; |
| 321 | for (integer j = colb; j <= cole; j ++) { |
| 322 | if (my columnLabels [j]) |
| 323 | Graphics_markBottom (g, xb, false, false, false, my columnLabels [j].get()); |
| 324 | xb += dx; |
| 325 | } |
| 326 | Graphics_drawInnerBox (g); |
| 327 | Graphics_marksLeft (g, 2, true, true, false); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void TableOfReal_drawBiplot (TableOfReal me, Graphics g, double xmin, double xmax, double ymin, double ymax, double sv_splitfactor, int labelsize, bool garnish) { |
| 332 | const integer nr = my numberOfRows, nc = my numberOfColumns, nPoints = nr + nc; |
| 333 | const double fontsize = Graphics_inqFontSize (g); |
| 334 | |
| 335 | autoSVD svd = SVD_create (nr, nc); |
| 336 | |
| 337 | svd -> u.all() <<= my data.all(); |
| 338 | centreEachColumn_MAT_inout (svd -> u.get()); |
| 339 | |
| 340 | SVD_compute (svd.get()); |
| 341 | const integer numberOfZeroed = SVD_zeroSmallSingularValues (svd.get(), 0.0); |
| 342 | |
| 343 | const integer nmin = std::min (nr, nc) - numberOfZeroed; |
| 344 | Melder_require (nmin > 1, |
| 345 | U"There should be at least two (independent) columns in the table."); |
| 346 | |
| 347 | autoVEC x = raw_VEC (nPoints); |
| 348 | autoVEC y = raw_VEC (nPoints); |
| 349 | |
| 350 | const double lambda1 = pow (svd -> d [1], sv_splitfactor); |
| 351 | const double lambda2 = pow (svd -> d [2], sv_splitfactor); |
| 352 | x.part (1, nr) <<= svd -> u.column (1) * lambda1; |
| 353 | y.part (1, nr) <<= svd -> u.column (2) * lambda2; |
| 354 | |
| 355 | const double lambda3 = svd -> d [1] / lambda1; |
| 356 | const double lambda4 = svd -> d [2] / lambda2; |
| 357 | x.part (nr + 1, nPoints) <<= svd -> v.column (1) * lambda3; |
| 358 | y.part (nr + 1, nPoints) <<= svd -> v.column (2) * lambda4; |
| 359 | |
| 360 | if (xmax <= xmin) |
| 361 | NUMextrema_u (x.get(), & xmin, & xmax); |
| 362 | if (xmax <= xmin) { |
| 363 | xmax += 1.0; |
| 364 | xmin -= 1.0; |
| 365 | } |
| 366 | if (ymax <= ymin) |
| 367 | NUMextrema_u (y.get(), & ymin, & ymax); |
| 368 | if (ymax <= ymin) { |
| 369 | ymax += 1.0; |
| 370 | ymin -= 1.0; |
| 371 | } |
| 372 | if (isundef (xmin) || isundef (xmax) || isundef (ymin) || isundef (ymax)) |
| 373 | return; |
| 374 | |
| 375 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 376 | Graphics_setInner (g); |
| 377 | if (labelsize > 0) |
| 378 | Graphics_setFontSize (g, labelsize); |
| 379 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 380 | |
| 381 | for (integer i = 1; i <= nPoints; i ++) { |
| 382 | char32 const *label; |
| 383 | if (i <= nr) { |
| 384 | label = my rowLabels [i].get(); |
| 385 | if (! label) |
| 386 | label = U"?__r_"; |
| 387 | } else { |
| 388 | label = my columnLabels [i - nr].get(); |
| 389 | if (! label) |
| 390 | label = U"?__c_"; |
| 391 | } |
| 392 | Graphics_text (g, x [i], y [i], label); |
| 393 | } |
| 394 | |
| 395 | Graphics_unsetInner (g); |
| 396 | |
| 397 | if (garnish) { |
| 398 | Graphics_drawInnerBox (g); |
| 399 | Graphics_marksLeft (g, 2, true, true, false); |
| 400 | Graphics_marksBottom (g, 2, true, true, false); |
| 401 | } |
| 402 | |
| 403 | if (labelsize > 0) |
| 404 | Graphics_setFontSize (g, fontsize); |
| 405 | } |
| 406 | |
| 407 | void TableOfReal_drawBoxPlots (TableOfReal me, Graphics g, integer rowmin, integer rowmax, integer colmin, integer colmax, double ymin, double ymax, bool garnish) { |
| 408 | if (rowmax < rowmin || rowmax < 1) { |
| 409 | rowmin = 1; |
| 410 | rowmax = my numberOfRows; |
| 411 | } |
| 412 | if (rowmin < 1) |
| 413 | rowmin = 1; |
| 414 | if (rowmax > my numberOfRows) |
| 415 | rowmax = my numberOfRows; |
| 416 | |
| 417 | const integer numberOfRows = rowmax - rowmin + 1; |
| 418 | if (colmax < colmin || colmax < 1) { |
| 419 | colmin = 1; |
| 420 | colmax = my numberOfColumns; |
| 421 | } |
| 422 | if (colmin < 1) |
| 423 | colmin = 1; |
| 424 | if (colmax > my numberOfColumns) |
| 425 | colmax = my numberOfColumns; |
| 426 | |
| 427 | if (ymax <= ymin) { |
| 428 | const MelderRealRange yrange = NUMextrema_u (my data.part (rowmin, rowmax, colmin, colmax)); |
| 429 | if (isundef (yrange)) |
| 430 | return; |
| 431 | ymin = yrange.min; |
| 432 | ymax = yrange.max; |
| 433 | } |
| 434 | |
| 435 | Graphics_setWindow (g, colmin - 0.5, colmax + 0.5, ymin, ymax); |
| 436 | Graphics_setInner (g); |
| 437 | |
| 438 | autoVEC data = raw_VEC (numberOfRows); |
| 439 | for (integer j = colmin; j <= colmax; j ++) { |
| 440 | const double x = j, r = 0.05, w = 0.2; |
| 441 | integer ndata = 0; |
| 442 | |
| 443 | for (integer i = 1; i <= numberOfRows; i ++) { |
| 444 | if (isdefined (my data [rowmin + i - 1] [j])) |
| 445 | data [ ++ ndata] = my data [rowmin + i - 1] [j]; |
| 446 | } |
| 447 | Graphics_boxAndWhiskerPlot (g, data.get(), x, r, w, ymin, ymax); |
| 448 | } |
| 449 | Graphics_unsetInner (g); |
| 450 | if (garnish) { |
| 451 | Graphics_drawInnerBox (g); |
| 452 | for (integer j = colmin; j <= colmax; j ++) { |
| 453 | if (my columnLabels && my columnLabels [j] && my columnLabels [j] [0]) |
| 454 | Graphics_markBottom (g, j, false, true, false, my columnLabels [j].get()); |
| 455 | } |
| 456 | Graphics_marksLeft (g, 2, true, true, false); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void TableOfReal_copyLabels (TableOfReal me, TableOfReal thee, int rowOrigin, int columnOrigin) { |
| 461 | if (rowOrigin == 1) { |
| 462 | Melder_require (my numberOfRows == thy numberOfRows, |
| 463 | U"Both tables must have the same number of rows."); |
| 464 | thy rowLabels.all() <<= my rowLabels.all(); |
| 465 | } else if (rowOrigin == -1) { |
| 466 | Melder_require (my numberOfColumns == thy numberOfRows, |
| 467 | U"Both tables must have the same number of columns."); |
| 468 | thy rowLabels.all() <<= my columnLabels.all(); |
| 469 | } |
| 470 | if (columnOrigin == 1) { |
| 471 | Melder_require (my numberOfColumns == thy numberOfColumns, |
| 472 | U"Both tables must have the same number of columns."); |
| 473 | thy columnLabels.all() <<= my columnLabels.all(); |
| 474 | } else if (columnOrigin == -1) { |
| 475 | Melder_require (my numberOfRows == thy numberOfColumns, |
| 476 | U"Both tables must have the same number of rows."); |
| 477 | thy columnLabels.all() <<= my rowLabels.all(); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void TableOfReal_setLabelsFromCollectionItemNames (TableOfReal me, Collection thee, bool setRowLabels, bool setColumnLabels) { |
| 482 | try { |
| 483 | if (setRowLabels) { |
| 484 | Melder_assert (my numberOfRows == thy size); |
| 485 | for (integer i = 1; i <= my numberOfRows; i ++) { |
| 486 | const conststring32 name = Thing_getName (thy at [i]); |
| 487 | TableOfReal_setRowLabel (me, i, name); |
| 488 | } |
| 489 | } |
| 490 | if (setColumnLabels) { |
| 491 | Melder_assert (my numberOfColumns == thy size); |
| 492 | for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 493 | const conststring32 name = Thing_getName (thy at [i]); |
| 494 | TableOfReal_setColumnLabel (me, i, name); |
| 495 | } |
| 496 | } |
| 497 | } catch (MelderError) { |
| 498 | Melder_throw (me, U": labels not changed."); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void TableOfReal_centreColumns (TableOfReal me) { |
| 503 | centreEachColumn_MAT_inout (my data.get()); |
| 504 | } |
| 505 | |
| 506 | void TableOfReal_Categories_setRowLabels (TableOfReal me, Categories thee) { |
| 507 | try { |
| 508 | Melder_require (my numberOfRows == thy size, |
| 509 | U"The number of items in both objects should be equal."); |
| 510 | /* |
| 511 | Create without change. |
| 512 | */ |
| 513 | autoCategories categories_copy = Data_copy (thee); |
| 514 | /* |
| 515 | Change without error. |
| 516 | */ |
| 517 | for (integer i = 1; i <= my numberOfRows; i ++) |
| 518 | my rowLabels [i] = categories_copy->at [i] -> string.move(); |
| 519 | } catch (MelderError) { |
| 520 | Melder_throw (me, U": row labels not set from categories."); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void TableOfReal_centreColumns_byRowLabel (TableOfReal me) { |
| 525 | conststring32 label = my rowLabels [1].get(); |
| 526 | integer index = 1; |
| 527 | for (integer i = 2; i <= my numberOfRows; i ++) { |
| 528 | const conststring32 li = my rowLabels [i].get(); |
| 529 | if (! Melder_equ (li, label)) { |
| 530 | centreEachColumn_MAT_inout (my data.horizontalBand (index, i - 1)); |
| 531 | label = li; |
| 532 | index = i; |
| 533 | } |
| 534 | } |
| 535 | centreEachColumn_MAT_inout (my data.horizontalBand (index, my numberOfRows)); |
| 536 | } |
| 537 | |
| 538 | double TableOfReal_getRowSum (TableOfReal me, integer rowNumber) { |
| 539 | Melder_require (rowNumber > 0 && rowNumber <= my numberOfRows, |
| 540 | U"Row number not in valid range."); |
| 541 | return NUMsum (my data.row (rowNumber)); |
| 542 | } |
| 543 | |
| 544 | double TableOfReal_getColumnSum (TableOfReal me, integer columnNumber) { |
| 545 | Melder_require (columnNumber > 0 && columnNumber <= my numberOfColumns, |
| 546 | U"Column number not in valid range."); |
| 547 | return NUMsum (my data.column (columnNumber)); |
| 548 | } |
| 549 | |
| 550 | double TableOfReal_getRowSumByLabel (TableOfReal me, conststring32 rowLabel) { |
| 551 | const integer rowNumber = TableOfReal_rowLabelToIndex (me, rowLabel); |
| 552 | Melder_require (rowNumber > 0, |
| 553 | U"There is no \"", rowLabel, U"\" row label."); |
| 554 | return TableOfReal_getRowSum (me, rowNumber); |
| 555 | } |
| 556 | |
| 557 | double TableOfReal_getColumnSumByLabel (TableOfReal me, conststring32 columnLabel) { |
| 558 | const integer columnNumber = TableOfReal_columnLabelToIndex (me, columnLabel); |
| 559 | Melder_require (columnNumber > 0, |
| 560 | U"There is no \"", columnLabel, U"\" column label."); |
| 561 | return TableOfReal_getColumnSum (me, columnNumber); |
| 562 | } |
| 563 | |
| 564 | double TableOfReal_getGrandSum (TableOfReal me) { |
| 565 | return NUMsum (my data.all()); |
| 566 | } |
| 567 | |
| 568 | void TableOfReal_centreRows (TableOfReal me) { |
| 569 | centreEachRow_MAT_inout (my data.get()); |
| 570 | } |
| 571 | |
| 572 | void TableOfReal_doubleCentre (TableOfReal me) { |
| 573 | doubleCentre_MAT_inout (my data.get()); |
| 574 | } |
| 575 | |
| 576 | void TableOfReal_normalizeColumns (TableOfReal me, double norm) { |
| 577 | MATnormalizeRows_inplace (my data.transpose(), 2.0, norm); |
| 578 | } |
| 579 | |
| 580 | void TableOfReal_normalizeRows (TableOfReal me, double norm) { |
| 581 | MATnormalizeRows_inplace (my data.get(), 2.0, norm); |
| 582 | } |
| 583 | |
| 584 | void TableOfReal_standardizeColumns (TableOfReal me) { |
| 585 | if (my numberOfRows <= 1) { |
| 586 | my data.all() <<= 0.0; |
| 587 | return; |
| 588 | } |
| 589 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 590 | MelderGaussianStats stats = NUMmeanStdev (my data.column (icol)); |
| 591 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 592 | my data [irow] [icol] = (my data [irow] [icol] - stats.mean) / stats.stdev; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | void TableOfReal_standardizeRows (TableOfReal me) { |
| 597 | if (my numberOfColumns <= 1) { |
| 598 | my data.all() <<= 0.0; |
| 599 | return; |
| 600 | } |
| 601 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 602 | MelderGaussianStats stats = NUMmeanStdev (my data.row (irow)); |
| 603 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 604 | my data [irow] [icol] = (my data [irow] [icol] - stats.mean) / stats.stdev; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void TableOfReal_normalizeTable (TableOfReal me, double norm) { |
| 609 | MATnormalize_inplace (my data.get(), 2.0, norm); |
| 610 | } |
| 611 | |
| 612 | double TableOfReal_getTableNorm (TableOfReal me) { |
| 613 | return NUMnorm (my data.get(), 2.0); |
| 614 | } |
| 615 | |
| 616 | bool TableOfReal_isNonNegative (TableOfReal me) { |
| 617 | return NUMisNonNegative (my data.all()); |
| 618 | } |
| 619 | |
| 620 | void TableOfReal_drawScatterPlotMatrix (TableOfReal me, Graphics g, integer colb, integer cole, double fractionWhite) { |
| 621 | |
| 622 | if (colb == 0 && cole == 0) { |
| 623 | colb = 1; |
| 624 | cole = my numberOfColumns; |
| 625 | } else if (cole < colb || colb < 1 || cole > my numberOfColumns) |
| 626 | return; |
| 627 | |
| 628 | const integer numberOfColumns = cole - colb + 1; |
| 629 | if (numberOfColumns == 1) |
| 630 | return; |
| 631 | autoVEC colmin = raw_VEC (numberOfColumns); |
| 632 | autoVEC colmax = raw_VEC (numberOfColumns); |
| 633 | |
| 634 | for (integer j = 1; j <= numberOfColumns; j ++) { |
| 635 | colmin [j] = NUMmin_u (my data.column (colb + j - 1)); |
| 636 | colmax [j] = NUMmax_u (my data.column (colb + j - 1)); |
| 637 | if (isundef (colmin [j]) || isundef (colmax [j])) |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | for (integer j = 1; j <= numberOfColumns; j ++) { |
| 642 | double extra = fractionWhite * fabs (colmax [j] - colmin [j]); |
| 643 | if (extra == 0.0) |
| 644 | extra = 0.5; |
| 645 | colmin [j] -= extra; |
| 646 | colmax [j] += extra; |
| 647 | } |
| 648 | |
| 649 | Graphics_setWindow (g, 0.0, numberOfColumns, 0.0, numberOfColumns); |
| 650 | Graphics_setInner (g); |
| 651 | Graphics_line (g, 0.0, numberOfColumns, numberOfColumns, numberOfColumns); |
| 652 | Graphics_line (g, 0.0, 0.0, 0.0, numberOfColumns); |
| 653 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 654 | |
| 655 | for (integer icol = 1; icol <= numberOfColumns; icol ++) { |
| 656 | const integer ycol = colb + icol - 1; |
| 657 | Graphics_line (g, 0.0, numberOfColumns - icol, numberOfColumns, numberOfColumns - icol); |
| 658 | Graphics_line (g, icol, numberOfColumns, icol, 0.0); |
| 659 | for (integer irow = 1; irow <= numberOfColumns; irow ++) { |
| 660 | const integer xcol = colb + irow - 1; |
| 661 | if (icol == irow) { |
| 662 | conststring32 mark = my columnLabels [xcol].get(); |
| 663 | char32 label [40]; |
| 664 | if (! mark) { |
| 665 | Melder_sprint (label, 40, U"Column ", xcol); |
| 666 | mark = label; |
| 667 | } |
| 668 | Graphics_text (g, irow - 0.5, numberOfColumns - icol + 0.5, mark); |
| 669 | } else { |
| 670 | for (integer k = 1; k <= my numberOfRows; k ++) { |
| 671 | const double x = irow - 1 + (my data [k] [xcol] - colmin [irow]) / (colmax [irow] - colmin [irow]); |
| 672 | const double y = numberOfColumns - icol + (my data [k] [ycol] - colmin [icol]) / (colmax [icol] - colmin [icol]); |
| 673 | conststring32 mark = EMPTY_STRING (my rowLabels [k]) ? U"+" : my rowLabels [k].get(); |
| 674 | Graphics_text (g, x, y, mark); |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | Graphics_unsetInner (g); |
| 680 | } |
| 681 | |
| 682 | void TableOfReal_drawAsScalableSquares (TableOfReal me, Graphics g, integer rowmin, integer rowmax, integer colmin, integer colmax, kGraphicsMatrixOrigin origin, double cellSizeFactor, kGraphicsMatrixCellDrawingOrder fillOrder, bool garnish) { |
| 683 | try { |
| 684 | NUMfixIndicesInRange (1, my numberOfRows, & rowmin, & rowmax); |
| 685 | NUMfixIndicesInRange (1, my numberOfColumns, & colmin, & colmax); |
| 686 | autoMatrix thee = TableOfReal_to_Matrix (me); |
| 687 | Graphics_setWindow (g, colmin - 0.5, colmax + 0.5, rowmin - 0.5, rowmax + 0.5); |
| 688 | Graphics_setInner (g); |
| 689 | Matrix_drawAsSquares_inside (thee.get(), g, colmin - 0.5, colmax + 0.5, rowmin - 0.5, rowmax + 0.5, origin, cellSizeFactor, fillOrder); |
| 690 | Graphics_unsetInner (g); |
| 691 | if (garnish) { |
| 692 | Graphics_drawInnerBox (g); |
| 693 | Graphics_marksBottomEvery (g, 1.0, 1.0, false, true, false); |
| 694 | Graphics_marksLeftEvery (g, 1.0, 1.0, false, true, false); |
| 695 | } |
| 696 | } catch (MelderError) { |
| 697 | Melder_clearError (); // drawing errors shall be ignored |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | void TableOfReal_drawScatterPlot (TableOfReal me, Graphics g, integer icx, integer icy, integer rowb, integer rowe, double xmin, double xmax, double ymin, double ymax, |
| 702 | integer labelSize, bool useRowLabels, conststring32 label, bool garnish) |
| 703 | { |
| 704 | const double fontSize = Graphics_inqFontSize (g); |
| 705 | |
| 706 | Melder_require (icx >= 1 && icx <= my numberOfColumns, |
| 707 | U"The horizontal column number should be in the range from 1 to ", my numberOfColumns, U"."); |
| 708 | Melder_require (icy >= 1 && icy <= my numberOfColumns, |
| 709 | U"The vertical column number should be in the range from 1 to ", my numberOfColumns, U"."); |
| 710 | if (rowb < 1) |
| 711 | rowb = 1; |
| 712 | if (rowe > my numberOfRows) |
| 713 | rowe = my numberOfRows; |
| 714 | if (rowe <= rowb) { |
| 715 | rowb = 1; |
| 716 | rowe = my numberOfRows; |
| 717 | } |
| 718 | if (xmax == xmin) { |
| 719 | const MelderRealRange xrange = NUMextrema_u (my data.part (rowb, rowe, icx, icx)); |
| 720 | if (isundef (xrange)) |
| 721 | return; |
| 722 | const double tmp = ( xrange.max == xrange.min ? 0.5 : 0.0 ); |
| 723 | xmin = xrange.min - tmp; |
| 724 | xmax = xrange.max + tmp; |
| 725 | } |
| 726 | if (ymax == ymin) { |
| 727 | const MelderRealRange yrange = NUMextrema_u (my data.part (rowb, rowe, icy, icy)); |
| 728 | if (isundef (yrange)) |
| 729 | return; |
| 730 | const double tmp = ( yrange.max == yrange.min ? 0.5 : 0.0 ); |
| 731 | ymin = yrange.min - tmp; |
| 732 | ymax = yrange.max + tmp; |
| 733 | } |
| 734 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 735 | Graphics_setInner (g); |
| 736 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 737 | Graphics_setFontSize (g, labelSize); |
| 738 | |
| 739 | for (integer i = rowb; i <= rowe; i ++) { |
| 740 | const double x = my data [i] [icx], y = my data [i] [icy]; |
| 741 | if (((xmin < xmax && x >= xmin && x <= xmax) || (xmin > xmax && x <= xmin && x >= xmax)) && |
| 742 | ((ymin < ymax && y >= ymin && y <= ymax) || (ymin > ymax && y <= ymin && y >= ymax))) { |
| 743 | conststring32 mark = useRowLabels ? my rowLabels [i].get() : label; |
| 744 | if (mark) |
| 745 | Graphics_text (g, x, y, mark); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | Graphics_setFontSize (g, fontSize); |
| 750 | Graphics_unsetInner (g); |
| 751 | |
| 752 | if (garnish) { |
| 753 | Graphics_drawInnerBox (g); |
| 754 | if (ymin < ymax) { |
| 755 | if (my columnLabels [icx]) |
| 756 | Graphics_textBottom (g, true, my columnLabels [icx].get()); |
| 757 | Graphics_marksBottom (g, 2, true, true, false); |
| 758 | } else { |
| 759 | if (my columnLabels [icx]) |
| 760 | Graphics_textTop (g, true, my columnLabels [icx].get()); |
| 761 | Graphics_marksTop (g, 2, true, true, false); |
| 762 | } |
| 763 | if (xmin < xmax) { |
| 764 | if (my columnLabels [icy]) |
| 765 | Graphics_textLeft (g, true, my columnLabels [icy].get()); |
| 766 | Graphics_marksLeft (g, 2, true, true, false); |
| 767 | } else { |
| 768 | if (my columnLabels [icy]) |
| 769 | Graphics_textRight (g, true, my columnLabels [icy].get()); |
| 770 | Graphics_marksRight (g, 2, true, true, false); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | |
| 776 | #pragma mark - class TableOfRealList |
| 777 | |
| 778 | autoTableOfReal TableOfRealList_sum (TableOfRealList me) { |
| 779 | try { |
| 780 | if (my size <= 0) |
| 781 | return autoTableOfReal(); |
| 782 | |
| 783 | autoTableOfReal thee = Data_copy (my at [1]); |
| 784 | |
| 785 | for (integer i = 2; i <= my size; i ++) { |
| 786 | TableOfReal him = my at [i]; |
| 787 | Melder_require (thy numberOfRows == his numberOfRows && thy numberOfColumns == his numberOfColumns |
| 788 | && NUMequal (thy rowLabels.get(), his rowLabels.get()) |
| 789 | && NUMequal (thy columnLabels.get(), his columnLabels.get()), |
| 790 | U"Dimensions or labels differ for table ", i, U"."); |
| 791 | thy data.get() += his data.get(); |
| 792 | } |
| 793 | return thee; |
| 794 | } catch (MelderError) { |
| 795 | Melder_throw (me, U": sum not created."); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | bool TableOfRealList_haveIdenticalDimensions (TableOfRealList me) { |
| 800 | if (my size < 2) |
| 801 | return true; |
| 802 | const TableOfReal t1 = my at [1]; |
| 803 | for (integer i = 2; i <= my size; i ++) { |
| 804 | const TableOfReal t = my at [i]; |
| 805 | if (t -> numberOfColumns != t1 -> numberOfColumns || t -> numberOfRows != t1 -> numberOfRows) |
| 806 | return false; |
| 807 | } |
| 808 | return true; |
| 809 | } |
| 810 | |
| 811 | double TableOfReal_getColumnQuantile (TableOfReal me, integer columnNumber, double quantile) { |
| 812 | try { |
| 813 | if (columnNumber < 1 || columnNumber > my numberOfColumns) |
| 814 | return undefined; |
| 815 | autoVEC values = column_VEC (my data.get(), columnNumber); |
| 816 | sort_e_VEC_inout (values.get()); |
| 817 | return NUMquantile (values.get(), quantile); |
| 818 | } catch (MelderError) { |
| 819 | return undefined; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | autoTableOfReal TableOfReal_create_sandwell1987 () { |
| 824 | try { |
| 825 | /* |
| 826 | The following numbers are the 21 (approximate) data points in Fig, 2 in Sandwell (1987). |
| 827 | They were measured by djmw from a printed picture blown up by 800%. |
| 828 | The following numbers are in cm measured from the left (x) and the bottom (y) of the figure. |
| 829 | Vertical scale: 8.25 cm in the picture is 12 units, y [1] is at y = 0.0. |
| 830 | Horizontal scale: 17.75 cm is 10 units, x [1] is at x = 0.0. |
| 831 | */ |
| 832 | const double x [22] = { 0.0, 0.9, 2.15, 3.5, 4.75, 5.3, 6.15, 7.15, 7.95, 8.85, 9.95, 10.15, 10.3, 11.5, 12.4, 13.3, 14.2, 15.15, 16.0, 16.85, 17.25, 18.15 }; |
| 833 | const double y [22] = { 0.0, 4.2, 3.5, 4.2, 5.65, 10.1, 8.5, 7.8, 7.1, 6.4, 5.65, 0.6, 5.65, 4.2, 5.65, 7.1, 6.75, 6.35, 4.2, 2.05, 4.95, 4.25 }; |
| 834 | const integer numberOfSamples = 21; |
| 835 | autoTableOfReal thee = TableOfReal_create (numberOfSamples, 2); |
| 836 | for (integer isample = 1; isample <= numberOfSamples; isample ++) { |
| 837 | thy data [isample] [1] = (x [isample] - x [1]) * 10.0 / 17.75; |
| 838 | thy data [isample] [2] = (y [isample] - y [1]) * 12.0 / 8.25; |
| 839 | } |
| 840 | return thee; |
| 841 | } catch (MelderError) { |
| 842 | Melder_throw (U"Sandwell (1987) table not created."); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | static autoTableOfReal TableOfReal_createPolsVanNieropData (int choice, bool include_levels) { |
| 847 | try { |
| 848 | autoTable table = Table_create_polsVanNierop1973 (); |
| 849 | /* |
| 850 | Default: Pols 50 males, first part of the table. |
| 851 | */ |
| 852 | const integer ncols = include_levels ? 6 : 3; |
| 853 | integer ib = 1,nrows = 50 * 12; |
| 854 | if (choice == 2) { /* Van Nierop 25 females */ |
| 855 | ib = nrows + 1; |
| 856 | nrows = 25 * 12; |
| 857 | } |
| 858 | |
| 859 | autoTableOfReal thee = TableOfReal_create (nrows, ncols); |
| 860 | |
| 861 | for (integer i = 1; i <= nrows; i ++) { |
| 862 | const TableRow row = table -> rows.at [ib + i - 1]; |
| 863 | TableOfReal_setRowLabel (thee.get(), i, row -> cells [4]. string.get()); |
| 864 | for (integer j = 1; j <= 3; j ++) { |
| 865 | thy data [i] [j] = Melder_atof (row -> cells [4 + j]. string.get()); |
| 866 | if (include_levels) |
| 867 | thy data [i] [3 + j] = Melder_atof (row -> cells [7 + j]. string.get()); |
| 868 | } |
| 869 | } |
| 870 | for (integer j = 1; j <= 3; j ++) { |
| 871 | conststring32 label = table -> columnHeaders [4 + j]. label.get(); |
| 872 | TableOfReal_setColumnLabel (thee.get(), j, label); |
| 873 | if (include_levels) { |
| 874 | label = table -> columnHeaders [7 + j]. label.get(); |
| 875 | TableOfReal_setColumnLabel (thee.get(), 3 + j, label); |
| 876 | } |
| 877 | } |
| 878 | return thee; |
| 879 | } catch (MelderError) { |
| 880 | Melder_throw (U"TableOfReal from Pols & Van Nierop data not created."); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | autoTableOfReal TableOfReal_create_pols1973 (bool include_levels) { |
| 885 | return TableOfReal_createPolsVanNieropData (1, include_levels); |
| 886 | } |
| 887 | |
| 888 | autoTableOfReal TableOfReal_create_vanNierop1973 (bool include_levels) { |
| 889 | return TableOfReal_createPolsVanNieropData (2, include_levels); |
| 890 | } |
| 891 | |
| 892 | autoTableOfReal TableOfReal_create_weenink1983 (int option) { |
| 893 | try { |
| 894 | const integer nvowels = 12, ncols = 3, nrows = 10 * nvowels; |
| 895 | |
| 896 | autoTable table = Table_create_weenink1983 (); |
| 897 | |
| 898 | integer ib = ( option == 1 ? 1 : option == 2 ? 11 : 21 ); /* m f c*/ |
| 899 | ib = (ib - 1) * nvowels + 1; |
| 900 | |
| 901 | autoTableOfReal thee = TableOfReal_create (nrows, ncols); |
| 902 | for (integer i = 1; i <= nrows; i ++) { |
| 903 | const TableRow row = table -> rows.at [ib + i - 1]; |
| 904 | TableOfReal_setRowLabel (thee.get(), i, row -> cells [5]. string.get()); |
| 905 | for (integer j = 1; j <= 3; j ++) |
| 906 | thy data [i] [j] = Melder_atof (row -> cells [6 + j]. string.get()); /* Skip F0 */ |
| 907 | } |
| 908 | for (integer j = 1; j <= 3; j ++) { |
| 909 | const conststring32 label = table -> columnHeaders [6 + j]. label.get(); |
| 910 | TableOfReal_setColumnLabel (thee.get(), j, label); |
| 911 | } |
| 912 | return thee; |
| 913 | } catch (MelderError) { |
| 914 | Melder_throw (U"TableOfReal from Weenink data not created."); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | autoTableOfReal TableOfReal_randomizeRows (TableOfReal me) { |
| 919 | try { |
| 920 | autoPermutation p = Permutation_create (my numberOfRows, false); |
| 921 | autoTableOfReal thee = TableOfReal_Permutation_permuteRows (me, p.get()); |
| 922 | return thee; |
| 923 | } catch (MelderError) { |
| 924 | Melder_throw (me, U": randomized rows not created"); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | autoTableOfReal TableOfReal_bootstrap (TableOfReal me) { |
| 929 | try { |
| 930 | autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns); |
| 931 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 932 | TableOfReal_setColumnLabel (thee.get(), icol, my columnLabels [icol].get()); |
| 933 | /* |
| 934 | Select randomly from table with replacement. Because of replacement |
| 935 | you do not get back the original data set. A random fraction, |
| 936 | typically 1/e (37%), are replaced by duplicates. |
| 937 | */ |
| 938 | for (integer thyRow = 1; thyRow <= thy numberOfRows; thyRow ++) { |
| 939 | const integer myRow = NUMrandomInteger (1, my numberOfRows); |
| 940 | thy data.row (thyRow) <<= my data.row (myRow); |
| 941 | TableOfReal_setRowLabel (thee.get(), thyRow, my rowLabels [myRow].get()); |
| 942 | } |
| 943 | return thee; |
| 944 | } catch (MelderError) { |
| 945 | Melder_throw (me, U": bootstrapped data not created."); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | void TableOfReal_changeRowLabels (TableOfReal me, |
| 950 | conststring32 search, conststring32 replace, integer maximumNumberOfReplaces, |
| 951 | integer *nmatches, integer *nstringmatches, bool use_regexp) { |
| 952 | try { |
| 953 | autoSTRVEC rowLabels = string32vector_searchAndReplace (my rowLabels.get(), |
| 954 | search, replace, maximumNumberOfReplaces, nmatches, nstringmatches, use_regexp); |
| 955 | my rowLabels = std::move (rowLabels); |
| 956 | } catch (MelderError) { |
| 957 | Melder_throw (me, U": row labels not changed."); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | void TableOfReal_changeColumnLabels (TableOfReal me, |
| 962 | conststring32 search, conststring32 replace, integer maximumNumberOfReplaces, |
| 963 | integer *nmatches, integer *nstringmatches, bool use_regexp) { |
| 964 | try { |
| 965 | autoSTRVEC columnLabels = string32vector_searchAndReplace (my columnLabels.get(), |
| 966 | search, replace, maximumNumberOfReplaces, nmatches, nstringmatches, use_regexp); |
| 967 | my columnLabels = std::move (columnLabels); |
| 968 | } catch (MelderError) { |
| 969 | Melder_throw (me, U": column labels not changed."); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | integer TableOfReal_getNumberOfLabelMatches (TableOfReal me, conststring32 search, bool columnLabels, bool use_regexp) { |
| 974 | integer nmatches = 0, numberOfLabels = my numberOfRows; |
| 975 | char32 **labels = my rowLabels.peek2(); |
| 976 | regexp *compiled_regexp = nullptr; |
| 977 | |
| 978 | if (! search || search [0] == U'\0') |
| 979 | return 0; |
| 980 | if (columnLabels) { |
| 981 | numberOfLabels = my numberOfColumns; |
| 982 | labels = my columnLabels.peek2(); |
| 983 | } |
| 984 | try { |
| 985 | if (use_regexp) |
| 986 | compiled_regexp = CompileRE_throwable (search, 0); |
| 987 | for (integer i = 1; i <= numberOfLabels; i ++) { |
| 988 | if (! labels [i]) |
| 989 | continue; |
| 990 | if (use_regexp) { |
| 991 | if (ExecRE (compiled_regexp, nullptr, labels [i], nullptr, false, U'\0', U'\0', nullptr, nullptr)) |
| 992 | nmatches ++; |
| 993 | } else if (str32equ (labels [i], search)) |
| 994 | nmatches ++; |
| 995 | } |
| 996 | if (use_regexp) |
| 997 | free (compiled_regexp); |
| 998 | } catch (MelderError) { |
| 999 | if (use_regexp) |
| 1000 | free (compiled_regexp); |
| 1001 | Melder_throw (me, U"Can't get number of matches."); |
| 1002 | } |
| 1003 | return nmatches; |
| 1004 | } |
| 1005 | |
| 1006 | void TableOfReal_drawVectors (TableOfReal me, Graphics g, integer colx1, integer coly1, integer colx2, integer coly2, double xmin, double xmax, double ymin, double ymax, int vectype, int labelsize, bool garnish) { |
| 1007 | const integer nx = my numberOfColumns, ny = my numberOfRows; |
| 1008 | const double fontsize = Graphics_inqFontSize (g); |
| 1009 | |
| 1010 | Melder_require (colx1 > 0 && colx1 <= nx && coly1 > 0 && coly1 <= nx, |
| 1011 | U"The index in the \"From\" column(s) should be in range [1, ", nx, U"]."); |
| 1012 | Melder_require (colx2 > 0 && colx2 <= nx && coly2 > 0 && coly2 <= nx, |
| 1013 | U"The index in the \"To\" column(s) should be in range [1, ", nx, U"]."); |
| 1014 | |
| 1015 | if (xmin >= xmax) { |
| 1016 | const MelderRealRange x1 = NUMextrema_u (my data.column (colx1)); |
| 1017 | const MelderRealRange x2 = NUMextrema_u (my data.column (colx2)); |
| 1018 | if (isundef (x1) || isundef (x2)) |
| 1019 | return; |
| 1020 | xmin = std::min (x1.min, x2.min); |
| 1021 | xmax = std::max (x1.max, x2.max); |
| 1022 | } |
| 1023 | if (ymin >= ymax) { |
| 1024 | const MelderRealRange y1 = NUMextrema_u (my data.column (coly1)); |
| 1025 | const MelderRealRange y2 = NUMextrema_u (my data.column (coly2)); |
| 1026 | if (isundef (y1) || isundef (y2)) |
| 1027 | return; |
| 1028 | ymin = std::min (y1.min, y2.min); |
| 1029 | ymax = std::max (y1.max, y2.max); |
| 1030 | } |
| 1031 | if (xmin == xmax) { |
| 1032 | if (ymin == ymax) |
| 1033 | return; |
| 1034 | xmin -= 0.5; |
| 1035 | xmax += 0.5; |
| 1036 | } |
| 1037 | if (ymin == ymax) { |
| 1038 | ymin -= 0.5; |
| 1039 | ymax += 0.5; |
| 1040 | } |
| 1041 | |
| 1042 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 1043 | Graphics_setInner (g); |
| 1044 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 1045 | |
| 1046 | if (labelsize > 0) |
| 1047 | Graphics_setFontSize (g, labelsize); |
| 1048 | |
| 1049 | for (integer i = 1; i <= ny; i ++) { |
| 1050 | const double x1 = my data [i] [colx1]; |
| 1051 | const double y1 = my data [i] [coly1]; |
| 1052 | const double x2 = my data [i] [colx2]; |
| 1053 | const double y2 = my data [i] [coly2]; |
| 1054 | const conststring32 mark = EMPTY_STRING (my rowLabels [i]) ? U"" : my rowLabels [i].get(); |
| 1055 | if (vectype == Graphics_LINE) |
| 1056 | Graphics_line (g, x1, y1, x2, y2); |
| 1057 | else if (vectype == Graphics_TWOWAYARROW) { |
| 1058 | Graphics_arrow (g, x1, y1, x2, y2); |
| 1059 | Graphics_arrow (g, x2, y2, x1, y1); |
| 1060 | } else /*if (vectype == Graphics_ARROW) */ |
| 1061 | Graphics_arrow (g, x1, y1, x2, y2); |
| 1062 | if (labelsize > 0) |
| 1063 | Graphics_text (g, x1, y1, mark); |
| 1064 | } |
| 1065 | if (labelsize > 0) |
| 1066 | Graphics_setFontSize (g, fontsize); |
| 1067 | |
| 1068 | Graphics_unsetInner (g); |
| 1069 | if (garnish) { |
| 1070 | Graphics_drawInnerBox (g); |
| 1071 | Graphics_marksLeft (g, 2, true, true, false); |
| 1072 | Graphics_marksBottom (g, 2, true, true, false); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | void TableOfReal_drawColumnAsDistribution (TableOfReal me, Graphics g, integer column, double minimum, double maximum, integer nBins, double freqMin, double freqMax, bool cumulative, bool garnish) { |
| 1077 | if (column < 1 || column > my numberOfColumns) |
| 1078 | return; |
| 1079 | autoMatrix thee = TableOfReal_to_Matrix (me); |
| 1080 | Matrix_drawDistribution (thee.get(), g, column - 0.5, column + 0.5, 0.0, 0.0, minimum, maximum, nBins, freqMin, freqMax, cumulative, garnish); |
| 1081 | if (garnish && my columnLabels [column]) |
| 1082 | Graphics_textBottom (g, true, my columnLabels [column].get()); |
| 1083 | } |
| 1084 | |
| 1085 | autoTableOfReal TableOfReal_sortRowsByIndex (TableOfReal me, constINTVEC index, bool reverse) { |
| 1086 | try { |
| 1087 | Melder_require (my rowLabels, |
| 1088 | U"No labels to sort"); |
| 1089 | const MelderIntegerRange range = NUMextrema_e (index); |
| 1090 | Melder_require (range.first > 0 && range.first <= my numberOfRows && range.last > 0 && range.last <= my numberOfRows, |
| 1091 | U"One or more indices out of range [1, ", my numberOfRows, U"]."); |
| 1092 | autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns); |
| 1093 | for (integer i = 1; i <= my numberOfRows; i ++) { |
| 1094 | const integer myindex = reverse ? i : index [i]; |
| 1095 | const integer thyindex = reverse ? index [i] : i; |
| 1096 | thy rowLabels [thyindex] = Melder_dup (my rowLabels [myindex].get()); |
| 1097 | thy data.row (thyindex) <<= my data.row (myindex); |
| 1098 | } |
| 1099 | thy columnLabels.all() <<= my columnLabels.all(); |
| 1100 | return thee; |
| 1101 | } catch (MelderError) { |
| 1102 | Melder_throw (me, U": not sorted by row index."); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | autoINTVEC TableOfReal_getSortedIndexFromRowLabels (TableOfReal me) { |
| 1107 | try { |
| 1108 | return newINTVECindex (my rowLabels.get()); |
| 1109 | } catch (MelderError) { |
| 1110 | Melder_throw (me, U": no sorted index created."); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | autoTableOfReal TableOfReal_sortOnlyByRowLabels (TableOfReal me) { |
| 1115 | try { |
| 1116 | autoPermutation index = TableOfReal_to_Permutation_sortRowLabels (me); |
| 1117 | autoTableOfReal thee = TableOfReal_Permutation_permuteRows (me, index.get()); |
| 1118 | return thee; |
| 1119 | } catch (MelderError) { |
| 1120 | Melder_throw (me, U": not sorted by row labels."); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | static void NUMaverageBlock_byColumns_inplace (MAT a, integer rb, integer re, integer cb, integer ce, bool medians) { |
| 1125 | Melder_assert (rb > 0 && rb <= a.nrow); |
| 1126 | Melder_assert (rb <= re && re <= a.nrow); |
| 1127 | Melder_assert (cb > 0 && cb <= a.ncol); |
| 1128 | Melder_assert (cb <= ce && ce <= a.ncol); |
| 1129 | const integer n = re - rb + 1; |
| 1130 | if (n < 2) |
| 1131 | return; |
| 1132 | autoVEC tmp = raw_VEC (n); |
| 1133 | for (integer j = cb; j <= ce; j ++) { |
| 1134 | integer k = 1; |
| 1135 | for (integer i = rb; i <= re; i ++, k ++) |
| 1136 | tmp [k] = a [i] [j]; |
| 1137 | double average; |
| 1138 | if (medians) { |
| 1139 | sort_e_VEC_inout (tmp.get()); |
| 1140 | average = NUMquantile (tmp.get(), 0.5); |
| 1141 | } else { |
| 1142 | average = NUMmean (tmp.get()); |
| 1143 | } |
| 1144 | for (integer i = rb; i <= re; i ++) |
| 1145 | a [i] [j] = average; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | autoTableOfReal TableOfReal_meansByRowLabels (TableOfReal me, bool expand, bool useMedians) { |
| 1150 | try { |
| 1151 | autoTableOfReal thee; |
| 1152 | autoINTVEC index = TableOfReal_getSortedIndexFromRowLabels (me); |
| 1153 | autoTableOfReal sorted = TableOfReal_sortRowsByIndex (me, index.get(), false); |
| 1154 | |
| 1155 | integer indexi = 1, indexr = 0; |
| 1156 | conststring32 label = sorted -> rowLabels [1].get(); |
| 1157 | for (integer i = 2; i <= my numberOfRows; i ++) { |
| 1158 | const conststring32 li = sorted -> rowLabels [i].get(); |
| 1159 | if (Melder_cmp (li, label) != 0) { |
| 1160 | NUMaverageBlock_byColumns_inplace (sorted -> data.get(), indexi, i - 1, 1, my numberOfColumns, useMedians); |
| 1161 | if (! expand) { |
| 1162 | indexr ++; |
| 1163 | TableOfReal_copyOneRowWithLabel (sorted.get(), sorted.get(), indexi, indexr); |
| 1164 | } |
| 1165 | label = li; |
| 1166 | indexi = i; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | NUMaverageBlock_byColumns_inplace (sorted -> data.get(), indexi, my numberOfRows, 1, my numberOfColumns, useMedians); |
| 1171 | |
| 1172 | if (expand) { |
| 1173 | /* |
| 1174 | Now revert the sorting |
| 1175 | */ |
| 1176 | thee = TableOfReal_sortRowsByIndex (sorted.get(), index.get(), true); |
| 1177 | } else { |
| 1178 | indexr ++; |
| 1179 | TableOfReal_copyOneRowWithLabel (sorted.get(), sorted.get(), indexi, indexr); |
| 1180 | thee = TableOfReal_create (indexr, my numberOfColumns); |
| 1181 | for (integer i = 1; i <= indexr; i ++) |
| 1182 | TableOfReal_copyOneRowWithLabel (sorted.get(), thee.get(), i, i); |
| 1183 | thy columnLabels.all() <<= sorted -> columnLabels.all(); |
| 1184 | } |
| 1185 | return thee; |
| 1186 | } catch (MelderError) { |
| 1187 | Melder_throw (me, U": means by row labels not created."); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | autoTableOfReal TableOfReal_rankColumns (TableOfReal me, integer fromColumn, integer toColumn) { |
| 1192 | try { |
| 1193 | fixAndCheckColumnRange (& fromColumn, & toColumn, my data.get(), 1); |
| 1194 | autoTableOfReal thee = Data_copy (me); |
| 1195 | MATrankColumns (thy data.get(), fromColumn, toColumn); |
| 1196 | return thee; |
| 1197 | } catch (MelderError) { |
| 1198 | Melder_throw (me, U": column ranks not created."); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | s [lo] = precursor<number> |
| 1204 | s [lo+1] = precursor<number+1> |
| 1205 | ... |
| 1206 | s [hi] = precursor<number+hi-lo> |
| 1207 | */ |
| 1208 | void TableOfReal_setSequentialColumnLabels (TableOfReal me, integer from, integer to, conststring32 precursor, integer number, integer increment) { |
| 1209 | from = ( from == 0 ? 1 : from ); |
| 1210 | to = ( to == 0 ? my numberOfColumns : to ); |
| 1211 | Melder_require (from > 0 && from <= to && to <= my numberOfColumns, |
| 1212 | U"Wrong column indices."); |
| 1213 | for (integer i = from; i <= to; i ++, number += increment) |
| 1214 | my columnLabels [i] = Melder_dup (Melder_cat (precursor, number)); |
| 1215 | } |
| 1216 | |
| 1217 | void TableOfReal_setSequentialRowLabels (TableOfReal me, integer from, integer to, conststring32 precursor, integer number, integer increment) { |
| 1218 | from = ( from == 0 ? 1 : from ); |
| 1219 | to = ( to == 0 ? my numberOfRows : to ); |
| 1220 | Melder_require (from > 0 && from <= to && to <= my numberOfRows, |
| 1221 | U"Wrong row indices."); |
| 1222 | for (integer i = from; i <= to; i ++, number += increment) |
| 1223 | my rowLabels [i] = Melder_dup (Melder_cat (precursor, number)); |
| 1224 | } |
| 1225 | |
| 1226 | /* For the inheritors */ |
| 1227 | autoTableOfReal TableOfReal_to_TableOfReal (TableOfReal me) { |
| 1228 | try { |
| 1229 | autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns); |
| 1230 | thy data.all() <<= my data.all(); |
| 1231 | TableOfReal_copyLabels (me, thee.get(), 1, 1); |
| 1232 | return thee; |
| 1233 | } catch (MelderError) { |
| 1234 | Melder_throw (me, U": not copied."); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | autoTableOfReal TableOfReal_choleskyDecomposition (TableOfReal me, bool upper, bool inverse) { |
| 1239 | try { |
| 1240 | integer n = my numberOfColumns, lda = my numberOfRows, info; |
| 1241 | |
| 1242 | Melder_require (n == lda, |
| 1243 | U"The table should be a square symmetric table."); |
| 1244 | |
| 1245 | autoTableOfReal thee = Data_copy (me); |
| 1246 | |
| 1247 | if (upper) { |
| 1248 | for (integer i = 2; i <= n; i ++) |
| 1249 | for (integer j = 1; j < i; j ++) |
| 1250 | thy data [i] [j] = 0.0; |
| 1251 | } else { |
| 1252 | for (integer i = 1; i < n; i ++) |
| 1253 | for (integer j = i + 1; j <= n; j ++) |
| 1254 | thy data [i] [j] = 0.0; |
| 1255 | } |
| 1256 | const char *uplo = ( upper ? "L" : "U" ); |
| 1257 | NUMlapack_dpotf2_ (uplo, n, & thy data [1] [1], lda, & info); |
| 1258 | Melder_require (info == 0, |
| 1259 | U"dpotf2 fails"); |
| 1260 | |
| 1261 | if (inverse) { |
| 1262 | NUMlapack_dtrtri_ (uplo, "N", n, & thy data [1] [1], lda, &info); |
| 1263 | Melder_require (info == 0, |
| 1264 | U"dtrtri fails"); |
| 1265 | } |
| 1266 | return thee; |
| 1267 | } catch (MelderError) { |
| 1268 | Melder_throw (me, U": Cholesky decomposition not performed."); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | autoTableOfReal TableOfReal_appendColumns (TableOfReal me, TableOfReal thee) { |
| 1273 | try { |
| 1274 | const integer ncols = my numberOfColumns + thy numberOfColumns; |
| 1275 | Melder_require (my numberOfRows == thy numberOfRows, |
| 1276 | U"The numbers of rows should be equal."); |
| 1277 | /* |
| 1278 | Stricter label checking??? |
| 1279 | append only if |
| 1280 | (my rowLabels [i] == thy rowlabels [i], i=1..my numberOfRows) or |
| 1281 | (my rowLabels [i] == 'empty', i=1..my numberOfRows) or |
| 1282 | (thy rowLabels [i] == 'empty', i=1..my numberOfRows); |
| 1283 | 'empty': nullptr or \w* |
| 1284 | */ |
| 1285 | autoTableOfReal him = TableOfReal_create (my numberOfRows, ncols); |
| 1286 | his rowLabels.all() <<= my rowLabels.all(); |
| 1287 | his columnLabels.part (1, my numberOfColumns) <<= my columnLabels.all(); |
| 1288 | his columnLabels.part (my numberOfColumns + 1, ncols) <<= thy columnLabels.all(); |
| 1289 | his data.verticalBand (1, my numberOfColumns) <<= my data.all(); |
| 1290 | his data.verticalBand (my numberOfColumns + 1, ncols) <<= thy data.all(); |
| 1291 | |
| 1292 | integer labeldiffs = 0; |
| 1293 | for (integer i = 1; i <= my numberOfRows; i ++) |
| 1294 | if (! Melder_equ (my rowLabels [i].get(), thy rowLabels [i].get())) |
| 1295 | labeldiffs ++; |
| 1296 | if (labeldiffs > 0) |
| 1297 | Melder_warning (labeldiffs, U" row labels differed."); |
| 1298 | return him; |
| 1299 | } catch (MelderError) { |
| 1300 | Melder_throw (U"TableOfReal with appended columns not created."); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | autoTableOfReal TableOfRealList_appendColumnsMany (TableOfRealList me) { |
| 1305 | try { |
| 1306 | Melder_require (my size > 0, |
| 1307 | U"No tables selected."); |
| 1308 | const TableOfReal first = my at [1]; |
| 1309 | const integer nrows = first -> numberOfRows; |
| 1310 | integer ncols = first -> numberOfColumns; |
| 1311 | for (integer itab = 2; itab <= my size; itab ++) { |
| 1312 | const TableOfReal next = my at [itab]; |
| 1313 | ncols += next -> numberOfColumns; |
| 1314 | Melder_require (next -> numberOfRows == nrows, |
| 1315 | U"Numbers of rows in item ", itab, U" differs from previous."); |
| 1316 | } |
| 1317 | autoTableOfReal him = TableOfReal_create (nrows, ncols); |
| 1318 | /* Unsafe: new attributes not initialized. */ |
| 1319 | his rowLabels.all() <<= first -> rowLabels.all(); |
| 1320 | |
| 1321 | integer hisColumnIndex = 0; |
| 1322 | for (integer itab = 1; itab <= my size; itab ++) { |
| 1323 | const TableOfReal table = my at [itab]; |
| 1324 | for (integer icol = 1; icol <= table -> numberOfColumns; icol ++) { |
| 1325 | hisColumnIndex ++; |
| 1326 | TableOfReal_setColumnLabel (him.get(), hisColumnIndex, table -> columnLabels [icol].get()); |
| 1327 | his data.column (hisColumnIndex) <<= table -> data.column (icol); |
| 1328 | } |
| 1329 | } |
| 1330 | Melder_assert (hisColumnIndex == his numberOfColumns); |
| 1331 | return him; |
| 1332 | } catch (MelderError) { |
| 1333 | Melder_throw (U"TableOfReal with appended columns not created."); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | double TableOfReal_normalityTest_BHEP (TableOfReal me, double *h, double *out_tnb, double *out_lnmu, double *out_lnvar, bool *out_singularCovariance) { |
| 1338 | try { |
| 1339 | /* |
| 1340 | Henze & Wagner (1997), A new approach to the BHEP tests for multivariate normality, |
| 1341 | Journal of Multivariate Analysis 62, 1-23. |
| 1342 | */ |
| 1343 | autoCovariance thee = TableOfReal_to_Covariance (me); |
| 1344 | autoVEC weights; |
| 1345 | const double probability = Covariance_TableOfReal_normalityTest_BHEP (thee.get(), me, weights.get(), h, out_tnb, out_lnmu, out_lnvar, out_singularCovariance); |
| 1346 | return probability; |
| 1347 | } catch (MelderError) { |
| 1348 | Melder_throw (me, U": cannot determine normality."); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | autoTableOfReal TableOfReal_TableOfReal_crossCorrelations (TableOfReal me, TableOfReal thee, bool by_columns, bool center, bool normalize) { |
| 1353 | return by_columns ? TableOfReal_TableOfReal_columnCorrelations (me, thee, center, normalize) : |
| 1354 | TableOfReal_TableOfReal_rowCorrelations (me, thee, center, normalize); |
| 1355 | } |
| 1356 | |
| 1357 | autoTableOfReal TableOfReal_TableOfReal_rowCorrelations (TableOfReal me, TableOfReal thee, bool centre, bool normalize) { |
| 1358 | try { |
| 1359 | Melder_require (my numberOfColumns == thy numberOfColumns, |
| 1360 | U"Both tables should have the same number of columns."); |
| 1361 | autoTableOfReal him = TableOfReal_create (my numberOfRows, thy numberOfRows); |
| 1362 | autoMAT my_data = copy_MAT (my data.get()); |
| 1363 | autoMAT thy_data = copy_MAT (thy data.get()); |
| 1364 | if (centre) { |
| 1365 | centreEachRow_MAT_inout (my_data.get()); |
| 1366 | centreEachRow_MAT_inout (thy_data.get()); |
| 1367 | } |
| 1368 | if (normalize) { |
| 1369 | MATnormalizeRows_inplace (my_data.get(), 2.0, 1.0); |
| 1370 | MATnormalizeRows_inplace (thy_data.get(), 2.0, 1.0); |
| 1371 | } |
| 1372 | his rowLabels.all() <<= my rowLabels.all(); |
| 1373 | his columnLabels.all() <<= thy rowLabels.all(); |
| 1374 | mul_MAT_out (his data.get(), my_data.get(), thy_data.transpose()); |
| 1375 | return him; |
| 1376 | } catch (MelderError) { |
| 1377 | Melder_throw (U"TableOfReal with row correlations not created."); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | autoTableOfReal TableOfReal_TableOfReal_columnCorrelations (TableOfReal me, TableOfReal thee, bool center, bool normalize) { |
| 1382 | try { |
| 1383 | Melder_require (my numberOfRows == thy numberOfRows, |
| 1384 | U"Both tables should have the same number of rows."); |
| 1385 | autoTableOfReal him = TableOfReal_create (my numberOfColumns, thy numberOfColumns); |
| 1386 | autoMAT my_data = copy_MAT (my data.get()); |
| 1387 | autoMAT thy_data = copy_MAT (thy data.get()); |
| 1388 | if (center) { |
| 1389 | centreEachColumn_MAT_inout (my_data.get()); |
| 1390 | centreEachColumn_MAT_inout (thy_data.get()); |
| 1391 | } |
| 1392 | if (normalize) { |
| 1393 | MATnormalizeColumns_inplace (my_data.get(), 2.0, 1.0); |
| 1394 | MATnormalizeColumns_inplace (thy_data.get(), 2.0, 1.0); |
| 1395 | } |
| 1396 | his rowLabels.all() <<= my columnLabels.all(); |
| 1397 | his columnLabels.all() <<= thy columnLabels.all(); |
| 1398 | mul_MAT_out (his data.get(), my_data.transpose(), thy_data.get()); |
| 1399 | return him; |
| 1400 | } catch (MelderError) { |
| 1401 | Melder_throw (U"TableOfReal with column correlations not created."); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | autoMatrix TableOfReal_to_Matrix_interpolateOnRectangularGrid (TableOfReal me, double xmin, double xmax, double nx, double ymin, double ymax, integer ny, int /* method */) { |
| 1406 | try { |
| 1407 | Melder_require (my numberOfColumns > 2 && my numberOfColumns > 2, |
| 1408 | U"There should be at least three colums and three rows."); |
| 1409 | autoVEC weights = newVECbiharmonic2DSplineInterpolation_getWeights (my data.column (1), my data.column (2), my data.column (3)); |
| 1410 | const double dx = (xmax - xmin) / nx, dy = (ymax - ymin) / ny; |
| 1411 | autoMatrix thee = Matrix_create (xmin, xmax, nx, dx, xmin + 0.5 * dx, |
| 1412 | ymin, ymax, ny, dy, ymin + 0.5 * dy); |
| 1413 | for (integer irow = 1; irow <= ny; irow ++) { |
| 1414 | const double yp = thy y1 + (irow - 1) * dy; |
| 1415 | for (integer icol = 1; icol <= nx; icol ++) { |
| 1416 | const double xp = thy x1 + (icol - 1) * dx; |
| 1417 | thy z [irow] [icol] = NUMbiharmonic2DSplineInterpolation (my data.column (1), my data.column (2), weights.get(), xp, yp); |
| 1418 | } |
| 1419 | } |
| 1420 | return thee; |
| 1421 | } catch (MelderError) { |
| 1422 | Melder_throw (me, U": interpolation not finished."); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | static void TableOfReal_permuteRowElements_inplace (TableOfReal me, integer fromRow, integer toRow) { |
| 1427 | if (fromRow == 0 && toRow == 0) { |
| 1428 | fromRow = 1; |
| 1429 | toRow = my numberOfRows; |
| 1430 | } |
| 1431 | Melder_require (fromRow <= toRow && fromRow >= 1 && toRow <= my numberOfRows, |
| 1432 | U"The row range should be larger than 0 and smaller than ", my numberOfRows + 1); |
| 1433 | autoPermutation permutation = Permutation_create (my numberOfColumns, true); |
| 1434 | autoVEC rowData = raw_VEC (my numberOfColumns); |
| 1435 | for (integer irow = fromRow; irow <= toRow; irow ++) { |
| 1436 | Permutation_permuteRandomly_inplace (permutation.get(), 0, 0); |
| 1437 | rowData.get() <<= my data.row (irow); |
| 1438 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 1439 | my data [irow][icol] = rowData [Permutation_getValueAtIndex (permutation.get(), icol)]; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | static void TableOfReal_permuteColumnElements_inplace (TableOfReal me, integer fromColumn, integer toColumn) { |
| 1444 | if (fromColumn == 0 && toColumn == 0) { |
| 1445 | fromColumn = 1; |
| 1446 | toColumn = my numberOfColumns; |
| 1447 | } |
| 1448 | Melder_require (fromColumn <= toColumn && fromColumn >= 1 && toColumn <= my numberOfColumns, |
| 1449 | U"The column range should be larger than 0 and smaller than ", my numberOfColumns + 1); |
| 1450 | autoPermutation permutation = Permutation_create (my numberOfRows, true); |
| 1451 | autoVEC columnData = raw_VEC (my numberOfRows); |
| 1452 | for (integer icol = fromColumn; icol <= toColumn; icol ++) { |
| 1453 | Permutation_permuteRandomly_inplace (permutation.get(), 0, 0); |
| 1454 | columnData.get() <<= my data.column (icol); |
| 1455 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 1456 | my data [irow] [icol] = columnData [Permutation_getValueAtIndex (permutation.get(), irow)]; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | static void TableOfReal_shuffleCombinedRows (TableOfReal xShuffled, TableOfReal yShuffled, TableOfReal x, TableOfReal y) { |
| 1461 | Melder_require (x -> numberOfColumns == y -> numberOfColumns && |
| 1462 | xShuffled -> numberOfColumns == yShuffled -> numberOfColumns && |
| 1463 | x -> numberOfColumns == yShuffled -> numberOfColumns, |
| 1464 | U"All TableOfReals should have the same number of columns."); |
| 1465 | Melder_require (x -> numberOfRows == xShuffled -> numberOfRows && y -> numberOfRows == yShuffled -> numberOfRows, |
| 1466 | U"The number of rows of the first and the third TableOfReal should be equal and the number of rows of the " |
| 1467 | "second and forth TableOfReal should also be equal."); |
| 1468 | const integer numberOfRowsCombined = x -> numberOfRows + y -> numberOfRows; |
| 1469 | autoPermutation permutation = Permutation_create (numberOfRowsCombined, false); |
| 1470 | for (integer irow = 1; irow <= numberOfRowsCombined; irow ++) { |
| 1471 | const integer rowIndex = Permutation_getValueAtIndex (permutation.get(), irow); |
| 1472 | constVEC fromRow = ( rowIndex > x -> numberOfRows ? y -> data.row (rowIndex - x -> numberOfRows) : |
| 1473 | x -> data.row (rowIndex) ); |
| 1474 | VEC toRow = ( irow > xShuffled -> numberOfRows ? yShuffled -> data.row (irow - xShuffled -> numberOfRows) : |
| 1475 | xShuffled -> data.row (irow) ); |
| 1476 | toRow <<= fromRow; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | static autoVEC vectorizeLowerMinusDiagonal_VEC (constMATVU m) { |
| 1481 | Melder_assert (m.nrow == m.ncol); |
| 1482 | integer index = 0; |
| 1483 | const integer size = m.nrow * (m.nrow - 1) / 2; |
| 1484 | autoVEC vech = raw_VEC (size); |
| 1485 | for (integer irow = 2; irow <= m.nrow; irow ++) |
| 1486 | for (integer icol = 1; icol < irow; icol ++) |
| 1487 | vech [++ index] = m [irow] [icol]; |
| 1488 | Melder_assert (index == size); |
| 1489 | return vech; |
| 1490 | } |
| 1491 | |
| 1492 | static autoVEC vectorizeLowerPlusDiagonal_VEC (constMATVU m) { |
| 1493 | Melder_assert (m.nrow == m.ncol); |
| 1494 | integer index = 0; |
| 1495 | const integer size = m.nrow * (m.nrow + 1) / 2; |
| 1496 | autoVEC vech = raw_VEC (size); |
| 1497 | for (integer irow = 1; irow <= m.nrow; irow ++) |
| 1498 | for (integer icol = 1; icol <= irow; icol ++) |
| 1499 | vech [++ index] = m [irow] [icol]; |
| 1500 | Melder_assert (index == size); |
| 1501 | return vech; |
| 1502 | } |
| 1503 | |
| 1504 | static double TableOfReal_computeTestStatistic_WuEtAl1215 (TableOfReal me, bool useCorrelation) { |
| 1505 | autoCovariance thee = TableOfReal_to_Covariance (me); |
| 1506 | double testStatistic; |
| 1507 | if (useCorrelation) { |
| 1508 | autoCorrelation him = SSCP_to_Correlation (thee.get()); |
| 1509 | autoVEC vech = vectorizeLowerMinusDiagonal_VEC (his data.get()); |
| 1510 | testStatistic = 1.0 - sqrt (my numberOfColumns) / NUMnorm (vech.get(), 2.0); |
| 1511 | } else { |
| 1512 | autoVEC vech = vectorizeLowerPlusDiagonal_VEC (thy data.get()); |
| 1513 | testStatistic = 1.0 - NUMtrace (thy data.get()) / (sqrt (my numberOfColumns) * NUMnorm (vech.get(), 2.0)); |
| 1514 | } |
| 1515 | return testStatistic; |
| 1516 | } |
| 1517 | |
| 1518 | static double TableOfReal_computeTestStatistic_WuEtAl16 (TableOfReal me, bool useCorrelation) { |
| 1519 | autoCovariance thee = TableOfReal_to_Covariance (me); |
| 1520 | autoCorrelation him; |
| 1521 | constMATVU data = thy data.get(); |
| 1522 | if (useCorrelation) { |
| 1523 | him = SSCP_to_Correlation (thee.get()); |
| 1524 | data = his data.get(); |
| 1525 | } |
| 1526 | autoVEC vech = vectorizeLowerMinusDiagonal_VEC (data); |
| 1527 | const double testStatistic = 1.0 - NUMsum (vech.get()) / (NUMnorm (vech.get(), 2.0) * sqrt (vech.size)); |
| 1528 | return testStatistic; |
| 1529 | } |
| 1530 | |
| 1531 | static double TableOfReal_computeTestStatistic_WuEtAl17 (TableOfReal me, TableOfReal thee, bool useCorrelation) { |
| 1532 | autoCovariance mycov = TableOfReal_to_Covariance (me); |
| 1533 | autoCovariance thycov = TableOfReal_to_Covariance (thee); |
| 1534 | autoVEC myvech,thyvech; |
| 1535 | if (useCorrelation) { |
| 1536 | autoCorrelation mycor = SSCP_to_Correlation (mycov.get()); |
| 1537 | myvech = vectorizeLowerMinusDiagonal_VEC (mycor -> data.get()); |
| 1538 | autoCorrelation thycor = SSCP_to_Correlation (thycov.get()); |
| 1539 | thyvech = vectorizeLowerMinusDiagonal_VEC (thycor -> data.get()); |
| 1540 | } else { |
| 1541 | myvech = vectorizeLowerPlusDiagonal_VEC (mycov -> data.get()); |
| 1542 | thyvech = vectorizeLowerPlusDiagonal_VEC (thycov -> data.get()); |
| 1543 | } |
| 1544 | const double testStatistic = 1.0 - NUMinner (myvech.get(), thyvech.get()) / (NUMnorm (myvech.get(), 2.0) * NUMnorm (thyvech.get(), 2.0)); |
| 1545 | return testStatistic; |
| 1546 | } |
| 1547 | |
| 1548 | double TableOfReal_testSphericityOfCovariance (TableOfReal me, integer numberOfPermutations, bool useCorrelation) { |
| 1549 | try { |
| 1550 | autoTableOfReal thee = Data_copy (me); |
| 1551 | double testStatistic0 = TableOfReal_computeTestStatistic_WuEtAl1215 (me, useCorrelation); |
| 1552 | integer countLargerOrEqual = 0; |
| 1553 | for (integer iperm = 1; iperm <= numberOfPermutations; iperm ++) { |
| 1554 | TableOfReal_permuteRowElements_inplace (thee.get(), 0, 0); |
| 1555 | TableOfReal_permuteColumnElements_inplace (thee.get(), 0, 0); |
| 1556 | const double testStatistic = TableOfReal_computeTestStatistic_WuEtAl1215 (thee.get(), useCorrelation); |
| 1557 | if (testStatistic >= testStatistic0) |
| 1558 | countLargerOrEqual ++; |
| 1559 | } |
| 1560 | return (1.0 + countLargerOrEqual) / (1.0 + numberOfPermutations); |
| 1561 | } catch (MelderError) { |
| 1562 | Melder_throw (me, U": could not determine probability for sphericity."); |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | double TableOfReal_testCovarianceEqualsIdentityMatrix (TableOfReal me, integer numberOfPermutations, bool useCorrelation) { |
| 1567 | try { |
| 1568 | autoTableOfReal mycopy = Data_copy (me); |
| 1569 | double testStatistic0 = TableOfReal_computeTestStatistic_WuEtAl1215 (me, useCorrelation); |
| 1570 | integer countLargerOrEqual = 0; |
| 1571 | for (integer iperm = 1; iperm <= numberOfPermutations; iperm ++) { |
| 1572 | TableOfReal_permuteColumnElements_inplace (mycopy.get(), 0, 0); |
| 1573 | const double testStatistic = TableOfReal_computeTestStatistic_WuEtAl1215 (mycopy.get(), useCorrelation); |
| 1574 | if (testStatistic >= testStatistic0) |
| 1575 | countLargerOrEqual ++; |
| 1576 | } |
| 1577 | return (1.0 + countLargerOrEqual) / (1.0 + numberOfPermutations); |
| 1578 | } catch (MelderError) { |
| 1579 | Melder_throw (me, U": cannot determine probability for identity."); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | double TableOfReal_testCovarianceCompoundSymmetry (TableOfReal me, integer numberOfPermutations, bool useCorrelation) { |
| 1584 | try { |
| 1585 | autoTableOfReal mycopy = Data_copy (me); |
| 1586 | double testStatistic0 = TableOfReal_computeTestStatistic_WuEtAl16 (me, useCorrelation); |
| 1587 | integer countLargerOrEqual = 0; |
| 1588 | for (integer iperm = 1; iperm <= numberOfPermutations; iperm ++) { |
| 1589 | TableOfReal_permuteRowElements_inplace (mycopy.get(), 0, 0); |
| 1590 | const double testStatistic = TableOfReal_computeTestStatistic_WuEtAl16 (mycopy.get(), useCorrelation); |
| 1591 | if (testStatistic >= testStatistic0) |
| 1592 | countLargerOrEqual ++; |
| 1593 | } |
| 1594 | return (1.0 + countLargerOrEqual) / (1.0 + numberOfPermutations); |
| 1595 | } catch (MelderError) { |
| 1596 | Melder_throw (me, U": cannot determine compound symmetry."); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | double TableOfReal_testEqualityOfCovariances (TableOfReal me, TableOfReal thee, integer numberOfPermutations, bool useCorrelation) { |
| 1601 | try { |
| 1602 | Melder_require (my numberOfColumns == thy numberOfColumns, |
| 1603 | U"The number of columns of both TableOfReal should be equal."); |
| 1604 | autoTableOfReal myCopy = Data_copy (me); |
| 1605 | autoTableOfReal thyCopy = Data_copy (thee); |
| 1606 | double testStatistic0 = TableOfReal_computeTestStatistic_WuEtAl17 (me, thee, useCorrelation); |
| 1607 | integer countLargerOrEqual = 0; |
| 1608 | for (integer iperm = 1; iperm <= numberOfPermutations; iperm ++) { |
| 1609 | double testStatistic; |
| 1610 | if (iperm % 2 == 1) { |
| 1611 | TableOfReal_shuffleCombinedRows (myCopy.get(), thyCopy.get(), me, thee); |
| 1612 | testStatistic = TableOfReal_computeTestStatistic_WuEtAl17 (myCopy.get(), thyCopy.get(), useCorrelation); |
| 1613 | } else { |
| 1614 | TableOfReal_shuffleCombinedRows (me, thee, myCopy.get(), thyCopy.get()); |
| 1615 | testStatistic = TableOfReal_computeTestStatistic_WuEtAl17 (me, thee, useCorrelation); |
| 1616 | } |
| 1617 | if (testStatistic >= testStatistic0) |
| 1618 | countLargerOrEqual ++; |
| 1619 | } |
| 1620 | return (1.0 + countLargerOrEqual) / (1.0 + numberOfPermutations); |
| 1621 | } catch (MelderError) { |
| 1622 | Melder_throw (me, U" & ", thee, U": could not determine probability for equality."); |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | #undef EMPTY_STRING |
| 1627 | |
| 1628 | /* End of file TableOfReal_extensions.c */ |