| 1 | /* NUM.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2021,2023,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 "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 20 | |
| 21 | /* |
| 22 | Local functions. |
| 23 | */ |
| 24 | |
| 25 | static longdoublecxx . . $ longdouble# NUMsum_longdoublecxx . . $ NUMsum_longdouble(4b452550a19f78a4). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 26 | /* |
| 27 | This function started to crash on October 27, 2020. |
| 28 | The cause was that if `vec.firstCell == nullptr`, |
| 29 | `& vec [1]` is a "null reference" (see the definition of constvectorview::operator[]). |
| 30 | This causes "undefined behaviour", even if `p` is never read from that address |
| 31 | (which it isn't, because if `vec.firstCell == nullptr`, then `vec.size` must be 0). |
| 32 | What precisely happened was that although `vec.size` was null, |
| 33 | the processor would branch-predict into the `if (_n & 1)` branch of PAIRWISE_SUM, |
| 34 | thereby pre-fetching `p` from the address `& vec [1]`, |
| 35 | which would lead to a Bad Access exception. |
| 36 | |
| 37 | A possible repair is to use a pointer instead of a reference. |
| 38 | */ |
| 39 | if (vec.stridecxx . . $ constvectorview#stride. == 1) { |
| 40 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 41 | longdouble, sum, |
| 42 | integer, vec.size, |
| 43 | //const double *p = & vec [1], // valid C++ only if vec is well-defined |
| 44 | const double *p = & vec. firstCell [1 - 1], // null *pointers* are fine |
| 45 | longdouble (*p), |
| 46 | p += 1 |
| 47 | ) |
| 48 | return sum; |
| 49 | } else { |
| 50 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 51 | longdouble, sum, |
| 52 | integer, vec.size, |
| 53 | //const double *p = & vec [1], // valid C++ only if vec is well-defined |
| 54 | const double *p = & vec. firstCell [1 - 1], // null *pointers* are fine |
| 55 | longdouble (*p), |
| 56 | p += vec.stride |
| 57 | ) |
| 58 | return sum; |
| 59 | } |
| 60 | } |
| 61 | static longdoublecxx . . $ longdouble# NUMsum_longdoublecxx . . $ NUMsum_longdouble(d39cb01001833591). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 62 | if (mat.nrowcxx . . $ constmatrixview#nrow. <= mat.ncolcxx . . $ constmatrixview#ncol.) { |
| 63 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 64 | longdouble, sum, |
| 65 | integer, mat.nrow, |
| 66 | integer irow = 1, |
| 67 | NUMsum_longdouble (mat [irow]), |
| 68 | irow += 1 |
| 69 | ) |
| 70 | return sum; |
| 71 | } else { |
| 72 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 73 | longdouble, sum, |
| 74 | integer, mat.ncol, |
| 75 | integer icol = 1, |
| 76 | NUMsum_longdouble (mat.column (icol)), |
| 77 | icol += 1 |
| 78 | ) |
| 79 | return sum; |
| 80 | } |
| 81 | } |
| 82 | static longdoublecxx . . $ longdouble# NUMsumOfSquaredDifferences_longdoublecxx . . $ NUMsumOfSquaredDifferences_longdouble(a510f21eb67343cb). (constVECVUcxx . . $ constVECVU# const& vec, const double mean) { |
| 83 | if (vec.stridecxx . . $ constvectorview#stride. == 1) { |
| 84 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 85 | longdouble, sum, |
| 86 | integer, vec.size, |
| 87 | const double *p = vec. firstCell, |
| 88 | longdouble (*p - mean) * longdouble (*p - mean), |
| 89 | p += 1 |
| 90 | ) |
| 91 | return sum; |
| 92 | } else { |
| 93 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 94 | longdouble, sum, |
| 95 | integer, vec.size, |
| 96 | const double *p = vec. firstCell, |
| 97 | longdouble (*p - mean) * longdouble (*p - mean), |
| 98 | p += vec.stride |
| 99 | ) |
| 100 | return sum; |
| 101 | } |
| 102 | } |
| 103 | static longdoublecxx . . $ longdouble# NUMsum2_longdoublecxx . . $ NUMsum2_longdouble(4b452550a19f78a4). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 104 | if (vec.stridecxx . . $ constvectorview#stride. == 1) { |
| 105 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 106 | longdouble, sum, |
| 107 | integer, vec.size, |
| 108 | const double *p = vec. firstCell, |
| 109 | longdouble (*p) * longdouble (*p), |
| 110 | p += 1 |
| 111 | ) |
| 112 | return sum; |
| 113 | } else { |
| 114 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 115 | longdouble, sum, |
| 116 | integer, vec.size, |
| 117 | const double *p = vec. firstCell, |
| 118 | longdouble (*p) * longdouble (*p), |
| 119 | p += vec.stride |
| 120 | ) |
| 121 | return sum; |
| 122 | } |
| 123 | } |
| 124 | static longdoublecxx . . $ longdouble# NUMsum2_longdoublecxx . . $ NUMsum2_longdouble(d39cb01001833591). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 125 | if (mat.nrowcxx . . $ constmatrixview#nrow. <= mat.ncolcxx . . $ constmatrixview#ncol.) { |
| 126 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 127 | longdouble, sum, |
| 128 | integer, mat.nrow, |
| 129 | integer irow = 1, |
| 130 | NUMsum2_longdouble (mat [irow]), |
| 131 | irow += 1 |
| 132 | ) |
| 133 | return sum; |
| 134 | } else { |
| 135 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 136 | longdouble, sum, |
| 137 | integer, mat.ncol, |
| 138 | integer icol = 1, |
| 139 | NUMsum2_longdouble (mat.column (icol)), |
| 140 | icol += 1 |
| 141 | ) |
| 142 | return sum; |
| 143 | } |
| 144 | } |
| 145 | static longdoublecxx . . $ longdouble# NUMsumAbs_longdoublecxx . . $ NUMsumAbs_longdouble(4b452550a19f78a4). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 146 | if (vec.stridecxx . . $ constvectorview#stride. == 1) { |
| 147 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 148 | longdouble, sum, |
| 149 | integer, vec.size, |
| 150 | const double *p = vec. firstCell, |
| 151 | longdouble (fabs (*p)), |
| 152 | p += 1 |
| 153 | ) |
| 154 | return sum; |
| 155 | } else { |
| 156 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 157 | longdouble, sum, |
| 158 | integer, vec.size, |
| 159 | const double *p = vec. firstCell, |
| 160 | longdouble (fabs (*p)), |
| 161 | p += vec.stride |
| 162 | ) |
| 163 | return sum; |
| 164 | } |
| 165 | } |
| 166 | static longdoublecxx . . $ longdouble# NUMsumAbs_longdoublecxx . . $ NUMsumAbs_longdouble(d39cb01001833591). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 167 | if (mat.nrowcxx . . $ constmatrixview#nrow. <= mat.ncolcxx . . $ constmatrixview#ncol.) { |
| 168 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 169 | longdouble, sum, |
| 170 | integer, mat.nrow, |
| 171 | integer irow = 1, |
| 172 | NUMsumAbs_longdouble (mat [irow]), |
| 173 | irow += 1 |
| 174 | ) |
| 175 | return sum; |
| 176 | } else { |
| 177 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 178 | longdouble, sum, |
| 179 | integer, mat.ncol, |
| 180 | integer icol = 1, |
| 181 | NUMsumAbs_longdouble (mat.column (icol)), |
| 182 | icol += 1 |
| 183 | ) |
| 184 | return sum; |
| 185 | } |
| 186 | } |
| 187 | static longdoublecxx . . $ longdouble# NUMsumPower_longdoublecxx . . $ NUMsumPower_longdouble(e282ae4a75dec06e). (constVECVUcxx . . $ constVECVU# const& vec, const longdoublecxx . . $ longdouble# power) { |
| 188 | if (vec.stridecxx . . $ constvectorview#stride. == 1) { |
| 189 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 190 | longdouble, sum, |
| 191 | integer, vec.size, |
| 192 | const double *p = vec. firstCell, |
| 193 | ( sizeof (longdouble) == 8 ? pow (longdouble (fabs (*p)), power) : powl (longdouble (fabs (*p)), power) ), |
| 194 | p += 1 |
| 195 | ) |
| 196 | return sum; |
| 197 | } else { |
| 198 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 199 | longdouble, sum, |
| 200 | integer, vec.size, |
| 201 | const double *p = vec. firstCell, |
| 202 | ( sizeof (longdouble) == 8 ? pow (longdouble (fabs (*p)), power) : powl (longdouble (fabs (*p)), power) ), |
| 203 | p += vec.stride |
| 204 | ) |
| 205 | return sum; |
| 206 | } |
| 207 | } |
| 208 | static longdoublecxx . . $ longdouble# NUMsumPower_longdoublecxx . . $ NUMsumPower_longdouble(f936129dc83ad90c). (constMATVUcxx . . $ constMATVU# const& mat, const longdoublecxx . . $ longdouble# power) { |
| 209 | if (mat.nrowcxx . . $ constmatrixview#nrow. <= mat.ncolcxx . . $ constmatrixview#ncol.) { |
| 210 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 211 | longdouble, sum, |
| 212 | integer, mat.nrow, |
| 213 | integer irow = 1, |
| 214 | NUMsumPower_longdouble (mat [irow], power), |
| 215 | irow += 1 |
| 216 | ) |
| 217 | return sum; |
| 218 | } else { |
| 219 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! ( |
| 220 | longdouble, sum, |
| 221 | integer, mat.ncol, |
| 222 | integer icol = 1, |
| 223 | NUMsumPower_longdouble (mat.column (icol), power), |
| 224 | icol += 1 |
| 225 | ) |
| 226 | return sum; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | struct MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# { |
| 231 | longdoublecxx . . $ longdouble# meancxx . . $ MelderMeanSumsq_longdouble#mean., sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq.; |
| 232 | }; |
| 233 | |
| 234 | static MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# NUMmeanSumsqcxx . . $ NUMmeanSumsq(5339da3437877a63). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 235 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# result; |
| 236 | result.meancxx . . $ MelderMeanSumsq_longdouble#mean. = NUMsum_longdoublecxx . . $ NUMsum_longdouble(4b452550a19f78a4). (vec) / vec.sizecxx . . $ constvectorview#size.; |
| 237 | const double mean = double (result.meancxx . . $ MelderMeanSumsq_longdouble#mean.); |
| 238 | if (vec.stridecxx . . $ constvectorview#stride. == 1) { |
| 239 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sumsq, integer, vec.size, |
| 240 | const double *p = vec. firstCell, |
| 241 | longdouble (*p - mean) * longdouble (*p - mean), |
| 242 | p += 1 |
| 243 | ) |
| 244 | result.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. = sumsq; |
| 245 | } else { |
| 246 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sumsq, integer, vec.size, |
| 247 | const double *p = vec. firstCell, |
| 248 | longdouble (*p - mean) * longdouble (*p - mean), |
| 249 | p += vec.stride |
| 250 | ) |
| 251 | result.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. = sumsq; |
| 252 | } |
| 253 | return result; |
| 254 | } |
| 255 | static MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# NUMmeanSumsqcxx . . $ NUMmeanSumsq(fd30fb828cc58ed9). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 256 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# result; |
| 257 | result.meancxx . . $ MelderMeanSumsq_longdouble#mean. = NUMsum_longdoublecxx . . $ NUMsum_longdouble(d39cb01001833591). (mat) / (mat.nrowcxx . . $ constmatrixview#nrow. * mat.ncolcxx . . $ constmatrixview#ncol.); |
| 258 | const double mean = double (result.meancxx . . $ MelderMeanSumsq_longdouble#mean.); |
| 259 | if (mat.nrowcxx . . $ constmatrixview#nrow. <= mat.ncolcxx . . $ constmatrixview#ncol.) { |
| 260 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sumsq, integer, mat.nrow, |
| 261 | integer irow = 1, |
| 262 | NUMsumOfSquaredDifferences_longdouble (mat [irow], mean), |
| 263 | irow += 1 |
| 264 | ) |
| 265 | result.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. = sumsq; |
| 266 | } else { |
| 267 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sumsq, integer, mat.ncol, |
| 268 | integer icol = 1, |
| 269 | NUMsumOfSquaredDifferences_longdouble (mat.column (icol), mean), |
| 270 | icol += 1 |
| 271 | ) |
| 272 | result.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. = sumsq; |
| 273 | } |
| 274 | return result; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | Global functions in alphabetic order. |
| 279 | */ |
| 280 | |
| 281 | double NUMcenterOfGravitycxx . . $ NUMcenterOfGravity(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& x) noexcept { |
| 282 | longdoublecxx . . $ longdouble# weightedSumOfIndexes = 0.0, sumOfWeights = 0.0; |
| 283 | for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ constvectorview#size.; i ++) { |
| 284 | weightedSumOfIndexes += i * x [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 285 | sumOfWeights += x [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 286 | } |
| 287 | return double (weightedSumOfIndexes / sumOfWeights); |
| 288 | } |
| 289 | |
| 290 | double NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (constVECVUcxx . . $ constVECVU# const& x, constVECVUcxx . . $ constVECVU# const& y) noexcept { |
| 291 | if (x.stridecxx . . $ constvectorview#stride. == 1) { |
| 292 | if (y.stridecxx . . $ constvectorview#stride. == 1) { |
| 293 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 294 | const double *px = x. firstCell; |
| 295 | const double *py = y. firstCell, |
| 296 | longdouble (*px) * longdouble (*py), |
| 297 | (px += 1, py += 1) |
| 298 | ) |
| 299 | return double (sum); |
| 300 | } else { |
| 301 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 302 | const double *px = x. firstCell; |
| 303 | const double *py = y. firstCell, |
| 304 | longdouble (*px) * longdouble (*py), |
| 305 | (px += 1, py += y.stride) |
| 306 | ) |
| 307 | return double (sum); |
| 308 | } |
| 309 | } else if (y.stridecxx . . $ constvectorview#stride. == 1) { |
| 310 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 311 | const double *px = x. firstCell; |
| 312 | const double *py = y. firstCell, |
| 313 | longdouble (*px) * longdouble (*py), |
| 314 | (px += x.stride, py += 1) |
| 315 | ) |
| 316 | return double (sum); |
| 317 | } else { |
| 318 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 319 | const double *px = x. firstCell; |
| 320 | const double *py = y. firstCell, |
| 321 | longdouble (*px) * longdouble (*py), |
| 322 | (px += x.stride, py += y.stride) |
| 323 | ) |
| 324 | return double (sum); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static longdoublecxx . . $ longdouble# NUMinnerMinusMean_longdoublecxx . . $ NUMinnerMinusMean_longdouble(cca1ad01927fef7). (constVECVUcxx . . $ constVECVU# const& x, const longdoublecxx . . $ longdouble# meanX, constVECVUcxx . . $ constVECVU# const& y, const longdoublecxx . . $ longdouble# meanY) noexcept { |
| 329 | if (x.stridecxx . . $ constvectorview#stride. == 1) { |
| 330 | if (y.stridecxx . . $ constvectorview#stride. == 1) { |
| 331 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 332 | const double *px = x. firstCell; |
| 333 | const double *py = y. firstCell, |
| 334 | (longdouble (*px) - meanX) * (longdouble (*py) - meanY), |
| 335 | (px += 1, py += 1) |
| 336 | ) |
| 337 | return sum; |
| 338 | } else { |
| 339 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 340 | const double *px = x. firstCell; |
| 341 | const double *py = y. firstCell, |
| 342 | (longdouble (*px) - meanX) * (longdouble (*py) - meanY), |
| 343 | (px += 1, py += y.stride) |
| 344 | ) |
| 345 | return sum; |
| 346 | } |
| 347 | } else if (y.stridecxx . . $ constvectorview#stride. == 1) { |
| 348 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 349 | const double *px = x. firstCell; |
| 350 | const double *py = y. firstCell, |
| 351 | (longdouble (*px) - meanX) * (longdouble (*py) - meanY), |
| 352 | (px += x.stride, py += 1) |
| 353 | ) |
| 354 | return sum; |
| 355 | } else { |
| 356 | PAIRWISE_SUMcxx . . $ `melder/PAIRWISE_SUM.h:406:9`! (longdouble, sum, integer, x.size, |
| 357 | const double *px = x. firstCell; |
| 358 | const double *py = y. firstCell, |
| 359 | (longdouble (*px) - meanX) * (longdouble (*py) - meanY), |
| 360 | (px += x.stride, py += y.stride) |
| 361 | ) |
| 362 | return sum; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | double NUMmeancxx . . $ NUMmean(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 367 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) |
| 368 | return undefinedcxx . . $ undefined.; |
| 369 | const longdoublecxx . . $ longdouble# sum = NUMsum_longdoublecxx . . $ NUMsum_longdouble(4b452550a19f78a4). (vec); |
| 370 | return double (sum / vec.sizecxx . . $ constvectorview#size.); |
| 371 | } |
| 372 | double NUMmeancxx . . $ NUMmean(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 373 | if (NUMisEmptycxx . . $ NUMisEmpty(939f42bb6b0c0c9c). (mat)) |
| 374 | return undefinedcxx . . $ undefined.; |
| 375 | const longdoublecxx . . $ longdouble# sum = NUMsum_longdoublecxx . . $ NUMsum_longdouble(d39cb01001833591). (mat); |
| 376 | return double (sum / (mat.nrowcxx . . $ constmatrixview#nrow. * mat.ncolcxx . . $ constmatrixview#ncol.)); |
| 377 | } |
| 378 | |
| 379 | MelderGaussianStatscxx . . $ MelderGaussianStats# NUMmeanStdevcxx . . $ NUMmeanStdev(97af7fa66bc3d2dd). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 380 | MelderGaussianStatscxx . . $ MelderGaussianStats# result; |
| 381 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# stats = NUMmeanSumsqcxx . . $ NUMmeanSumsq(5339da3437877a63). (vec); |
| 382 | result.meancxx . . $ MelderGaussianStats#mean. = double (stats.meancxx . . $ MelderMeanSumsq_longdouble#mean.); // SHOULD be undefined if vec.size < 1 |
| 383 | const integercxx . . $ integer# df = vec.sizecxx . . $ constvectorview#size. - 1; |
| 384 | result.stdevcxx . . $ MelderGaussianStats#stdev. = ( df <= 0 ? undefinedcxx . . $ undefined. : sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (stats.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq.) / df) ); |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | MelderGaussianStatscxx . . $ MelderGaussianStats# NUMmeanStdevcxx . . $ NUMmeanStdev(89994f7c2020bdc1). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 389 | MelderGaussianStatscxx . . $ MelderGaussianStats# result; |
| 390 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# stats = NUMmeanSumsqcxx . . $ NUMmeanSumsq(fd30fb828cc58ed9). (mat); |
| 391 | result.meancxx . . $ MelderGaussianStats#mean. = double (stats.meancxx . . $ MelderMeanSumsq_longdouble#mean.); // SHOULD be undefined if mat.nrow * mat.ncol < 1 |
| 392 | const integercxx . . $ integer# df = mat.nrowcxx . . $ constmatrixview#nrow. * mat.ncolcxx . . $ constmatrixview#ncol. - 1; |
| 393 | result.stdevcxx . . $ MelderGaussianStats#stdev. = ( df <= 0 ? undefinedcxx . . $ undefined. : sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (stats.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. / df)) ); |
| 394 | return result; |
| 395 | } |
| 396 | |
| 397 | template <bool canError> |
| 398 | double NUMmincxx . . $ NUMmin(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { // propagate NaNs (including -inf and +inf) |
| 399 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) { |
| 400 | if constexpr (canError) |
| 401 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_e: cannot determine the minimum of an empty vector."); |
| 402 | else |
| 403 | return undefinedcxx . . $ undefined.; |
| 404 | } |
| 405 | /* |
| 406 | The following procedure is the simplest, and possibly the best. |
| 407 | |
| 408 | We also tried a "smart" version, which had fewer `isundef` calls, |
| 409 | namely by rewriting the test as `if (! (value >= minimum)`. |
| 410 | This captured cases where `minimum` is NaN, so that the condition |
| 411 | `if (isundef (value))` could be moved into the then-part of `if (! (value >= minimum))`. |
| 412 | This saved a lot of `isundef`s and handled NaN and -inf; |
| 413 | however, it didn't handle +inf. |
| 414 | The version below does handle all infinities, and guess what?: the procedure is |
| 415 | not more than 2 percent slower than without the `isundef` test! |
| 416 | (last checked 2023-02-07 for the Mac) |
| 417 | Hence: DON'T OPTIMIZE. |
| 418 | */ |
| 419 | double minimum = + stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 420 | for (integercxx . . $ integer# i = 1; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 421 | const double value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 422 | if (isundefcxx . . $ isundef(f8197d536af80c96). (value)) { |
| 423 | if constexpr (canError) |
| 424 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_e: cannot determine the minimum of a vector: element ", i, U" is undefined."); |
| 425 | else |
| 426 | return undefinedcxx . . $ undefined.; |
| 427 | } |
| 428 | if (value < minimum) |
| 429 | minimum = value; |
| 430 | } |
| 431 | return minimum; |
| 432 | } |
| 433 | double NUMmin_ecxx . . $ NUMmin_e(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 434 | return NUMmincxx . . $ NUMmin(fa513f1eb826a064). <true> (vec); |
| 435 | } |
| 436 | double NUMmin_ucxx . . $ NUMmin_u(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 437 | return NUMmincxx . . $ NUMmin(fa513f1eb826a064). <false> (vec); |
| 438 | } |
| 439 | |
| 440 | template <bool canError> |
| 441 | double NUMmin_removeUndefinedcxx . . $ NUMmin_removeUndefined(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 442 | double minimum = + stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 443 | for (integercxx . . $ integer# i = 1; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 444 | const double value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 445 | if (value < minimum) // NaN-safe, because false if value is undefined |
| 446 | minimum = value; |
| 447 | } |
| 448 | /* |
| 449 | We have been ignoring undefineds, but this is inappropriate if *all* elements are undefined. |
| 450 | This includes the case of a zero-sized vector. |
| 451 | */ |
| 452 | if (isundefcxx . . $ isundef(f8197d536af80c96). (minimum)) { // including the original infinity |
| 453 | if constexpr (canError) { |
| 454 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) |
| 455 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_e: cannot determine the minimum of an empty vector."); |
| 456 | else |
| 457 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_e: cannot determine the minimum of a vector with only undefined elements."); |
| 458 | } else |
| 459 | return undefinedcxx . . $ undefined.; |
| 460 | } |
| 461 | return minimum; |
| 462 | } |
| 463 | double NUMmin_removeUndefined_ecxx . . $ NUMmin_removeUndefined_e(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 464 | return NUMmin_removeUndefinedcxx . . $ NUMmin_removeUndefined(fa513f1eb826a064). <true> (vec); |
| 465 | } |
| 466 | double NUMmin_removeUndefined_ucxx . . $ NUMmin_removeUndefined_u(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 467 | return NUMmin_removeUndefinedcxx . . $ NUMmin_removeUndefined(fa513f1eb826a064). <false> (vec); |
| 468 | } |
| 469 | |
| 470 | template <bool canError> |
| 471 | integercxx . . $ integer# NUMmincxx . . $ NUMmin(ff5c8185b8e92fd6). (constINTVECVUcxx . . $ constINTVECVU# const& vec) { |
| 472 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) { |
| 473 | if constexpr (canError) |
| 474 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_e: cannot determine the minimum of an empty integer vector."); |
| 475 | else |
| 476 | return INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!; |
| 477 | } |
| 478 | integercxx . . $ integer# minimum = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).1]; |
| 479 | for (integercxx . . $ integer# i = 2; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 480 | const integercxx . . $ integer# value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 481 | if (value < minimum) |
| 482 | minimum = value; |
| 483 | } |
| 484 | return minimum; |
| 485 | } |
| 486 | integercxx . . $ integer# NUMmin_ecxx . . $ NUMmin_e(ff5c8185b8e92fd6). (constINTVECVUcxx . . $ constINTVECVU# const& vec) { |
| 487 | return NUMmincxx . . $ NUMmin(ff5c8185b8e92fd6). <true> (vec); |
| 488 | } |
| 489 | integercxx . . $ integer# NUMmin_ucxx . . $ NUMmin_u(967b12debc2a64dd). (constINTVECVUcxx . . $ constINTVECVU# const& vec) noexcept { |
| 490 | return NUMmincxx . . $ NUMmin(ff5c8185b8e92fd6). <false> (vec); |
| 491 | } |
| 492 | |
| 493 | template <bool canError> |
| 494 | double NUMmincxx . . $ NUMmin(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { // propagate NaNs (including -inf and +inf) |
| 495 | if (NUMisEmptycxx . . $ NUMisEmpty(939f42bb6b0c0c9c). (mat)) { |
| 496 | if constexpr (canError) |
| 497 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_e: cannot determine the minimum of an empty matrix."); |
| 498 | else |
| 499 | return undefinedcxx . . $ undefined.; |
| 500 | } |
| 501 | double minimum = + stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 502 | for (integercxx . . $ integer# irow = 1; irow <= mat.nrowcxx . . $ constmatrixview#nrow.; irow ++) { |
| 503 | for (integercxx . . $ integer# icol = 1; icol <= mat.ncolcxx . . $ constmatrixview#ncol.; icol ++) { |
| 504 | const double value = mat [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).icol]; |
| 505 | if (isundefcxx . . $ isundef(f8197d536af80c96). (value)) { |
| 506 | if (canError) |
| 507 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_e: cannot determine the minimum of a matrix: element [", irow, U", ", icol, U"] is undefined."); |
| 508 | else |
| 509 | return undefinedcxx . . $ undefined.; |
| 510 | } |
| 511 | if (value < minimum) |
| 512 | minimum = value; |
| 513 | } |
| 514 | } |
| 515 | return minimum; |
| 516 | } |
| 517 | double NUMmin_ecxx . . $ NUMmin_e(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 518 | return NUMmincxx . . $ NUMmin(7a3744b654164b28). <true> (mat); |
| 519 | } |
| 520 | double NUMmin_ucxx . . $ NUMmin_u(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 521 | return NUMmincxx . . $ NUMmin(7a3744b654164b28). <false> (mat); |
| 522 | } |
| 523 | |
| 524 | template <bool canError> |
| 525 | double NUMmin_removeUndefinedcxx . . $ NUMmin_removeUndefined(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 526 | double minimum = + stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 527 | for (integercxx . . $ integer# irow = 1; irow <= mat.nrowcxx . . $ constmatrixview#nrow.; irow ++) { |
| 528 | for (integercxx . . $ integer# icol = 1; icol <= mat.ncolcxx . . $ constmatrixview#ncol.; icol ++) { |
| 529 | const double value = mat [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).icol]; |
| 530 | if (value < minimum) // NaN-correct, because false if value is undefined |
| 531 | minimum = value; |
| 532 | } |
| 533 | } |
| 534 | if (isundefcxx . . $ isundef(f8197d536af80c96). (minimum)) { // including the original infinity |
| 535 | if constexpr (canError) { |
| 536 | if (NUMisEmptycxx . . $ NUMisEmpty(939f42bb6b0c0c9c). (mat)) |
| 537 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_removeUndefined_e: cannot determine the minimum of an empty matrix."); |
| 538 | else |
| 539 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"min_removeUndefined_e: cannot determine the minimum of a matrix with only undefined cells."); |
| 540 | } else |
| 541 | return undefinedcxx . . $ undefined.; |
| 542 | } |
| 543 | return minimum; |
| 544 | } |
| 545 | double NUMmin_removeUndefined_ecxx . . $ NUMmin_removeUndefined_e(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 546 | return NUMmin_removeUndefinedcxx . . $ NUMmin_removeUndefined(7a3744b654164b28). <true> (mat); |
| 547 | } |
| 548 | double NUMmin_removeUndefined_ucxx . . $ NUMmin_removeUndefined_u(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 549 | return NUMmin_removeUndefinedcxx . . $ NUMmin_removeUndefined(7a3744b654164b28). <false> (mat); |
| 550 | } |
| 551 | |
| 552 | template <bool canError> |
| 553 | double NUMmaxcxx . . $ NUMmax(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { // propagate NaNs (including -inf and +inf) |
| 554 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) { |
| 555 | if constexpr (canError) |
| 556 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_e: cannot determine the maximum of an empty vector."); |
| 557 | else |
| 558 | return undefinedcxx . . $ undefined.; |
| 559 | } |
| 560 | double maximum = - stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 561 | for (integercxx . . $ integer# i = 1; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 562 | const double value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 563 | if (isundefcxx . . $ isundef(f8197d536af80c96). (value)) { |
| 564 | if constexpr (canError) |
| 565 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_e: cannot determine the maximum of a vector: element ", i, U" is undefined."); |
| 566 | else |
| 567 | return undefinedcxx . . $ undefined.; |
| 568 | } |
| 569 | if (value > maximum) |
| 570 | maximum = value; |
| 571 | } |
| 572 | return maximum; |
| 573 | } |
| 574 | double NUMmax_ecxx . . $ NUMmax_e(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 575 | return NUMmaxcxx . . $ NUMmax(fa513f1eb826a064). <true> (vec); |
| 576 | } |
| 577 | double NUMmax_ucxx . . $ NUMmax_u(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 578 | return NUMmaxcxx . . $ NUMmax(fa513f1eb826a064). <false> (vec); |
| 579 | } |
| 580 | |
| 581 | template <bool canError> |
| 582 | double NUMmax_removeUndefinedcxx . . $ NUMmax_removeUndefined(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 583 | double maximum = - stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 584 | for (integercxx . . $ integer# i = 1; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 585 | const double value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 586 | if (value > maximum) // NaN-safe, because false if value is undefined |
| 587 | maximum = value; |
| 588 | } |
| 589 | if (isundefcxx . . $ isundef(f8197d536af80c96). (maximum)) { // including the original -infinity |
| 590 | if constexpr (canError) { |
| 591 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) |
| 592 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_removeUndefined_e: cannot determine the maximum of an empty vector."); |
| 593 | else |
| 594 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_removeUndefined_e: cannot determine the maximum of a vector with only undefined elements."); |
| 595 | } else |
| 596 | return undefinedcxx . . $ undefined.; |
| 597 | } |
| 598 | return maximum; |
| 599 | } |
| 600 | double NUMmax_removeUndefined_ecxx . . $ NUMmax_removeUndefined_e(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 601 | return NUMmax_removeUndefinedcxx . . $ NUMmax_removeUndefined(fa513f1eb826a064). <true> (vec); |
| 602 | } |
| 603 | double NUMmax_removeUndefined_ucxx . . $ NUMmax_removeUndefined_u(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 604 | return NUMmax_removeUndefinedcxx . . $ NUMmax_removeUndefined(fa513f1eb826a064). <false> (vec); |
| 605 | } |
| 606 | |
| 607 | template <bool canError> |
| 608 | integercxx . . $ integer# NUMmaxcxx . . $ NUMmax(ff5c8185b8e92fd6). (constINTVECVUcxx . . $ constINTVECVU# const& vec) { |
| 609 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) { |
| 610 | if constexpr (canError) |
| 611 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_e: cannot determine the maximum of an empty integer vector."); |
| 612 | else |
| 613 | return INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`!; |
| 614 | } |
| 615 | integercxx . . $ integer# maximum = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).1]; |
| 616 | for (integercxx . . $ integer# i = 2; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 617 | const integercxx . . $ integer# value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 618 | if (value > maximum) |
| 619 | maximum = value; |
| 620 | } |
| 621 | return maximum; |
| 622 | } |
| 623 | integercxx . . $ integer# NUMmax_ecxx . . $ NUMmax_e(ff5c8185b8e92fd6). (constINTVECVUcxx . . $ constINTVECVU# const& vec) { |
| 624 | return NUMmaxcxx . . $ NUMmax(ff5c8185b8e92fd6). <true> (vec); |
| 625 | } |
| 626 | integercxx . . $ integer# NUMmax_ucxx . . $ NUMmax_u(967b12debc2a64dd). (constINTVECVUcxx . . $ constINTVECVU# const& vec) noexcept { |
| 627 | return NUMmaxcxx . . $ NUMmax(ff5c8185b8e92fd6). <false> (vec); |
| 628 | } |
| 629 | |
| 630 | template <bool canError> |
| 631 | double NUMmaxcxx . . $ NUMmax(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { // propagate NaNs (including -inf and +inf) |
| 632 | if (NUMisEmptycxx . . $ NUMisEmpty(939f42bb6b0c0c9c). (mat)) { |
| 633 | if constexpr (canError) |
| 634 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_e: cannot determine the maximum of an empty matrix."); |
| 635 | else |
| 636 | return undefinedcxx . . $ undefined.; |
| 637 | } |
| 638 | double maximum = - stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 639 | for (integercxx . . $ integer# irow = 1; irow <= mat.nrowcxx . . $ constmatrixview#nrow.; irow ++) { |
| 640 | for (integercxx . . $ integer# icol = 1; icol <= mat.ncolcxx . . $ constmatrixview#ncol.; icol ++) { |
| 641 | const double value = mat [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).icol]; |
| 642 | if (isundefcxx . . $ isundef(f8197d536af80c96). (value)) { |
| 643 | if constexpr (canError) |
| 644 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_e: cannot determine the maximum of a matrix: element [", irow, U", ", icol, U"] is undefined."); |
| 645 | else |
| 646 | return undefinedcxx . . $ undefined.; |
| 647 | } |
| 648 | if (value > maximum) |
| 649 | maximum = value; |
| 650 | } |
| 651 | } |
| 652 | return maximum; |
| 653 | } |
| 654 | double NUMmax_ecxx . . $ NUMmax_e(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 655 | return NUMmaxcxx . . $ NUMmax(7a3744b654164b28). <true> (mat); |
| 656 | } |
| 657 | double NUMmax_ucxx . . $ NUMmax_u(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { // propagate NaNs (including -inf and +inf) |
| 658 | return NUMmaxcxx . . $ NUMmax(7a3744b654164b28). <false> (mat); |
| 659 | } |
| 660 | |
| 661 | template <bool canError> |
| 662 | double NUMmax_removeUndefinedcxx . . $ NUMmax_removeUndefined(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 663 | double maximum = - stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 664 | for (integercxx . . $ integer# irow = 1; irow <= mat.nrowcxx . . $ constmatrixview#nrow.; irow ++) { |
| 665 | for (integercxx . . $ integer# icol = 1; icol <= mat.ncolcxx . . $ constmatrixview#ncol.; icol ++) { |
| 666 | const double value = mat [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).icol]; |
| 667 | if (value > maximum) // NaN-safe, because false if value is undefined |
| 668 | maximum = value; |
| 669 | } |
| 670 | } |
| 671 | if (isundefcxx . . $ isundef(f8197d536af80c96). (maximum)) { // including the original -infinity |
| 672 | if constexpr (canError) { |
| 673 | if (NUMisEmptycxx . . $ NUMisEmpty(939f42bb6b0c0c9c). (mat)) |
| 674 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_removeUndefined_e: cannot determine the maximum of an empty matrix."); |
| 675 | else |
| 676 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"max_removeUndefined_e: cannot determine the maximum of a matrix with only undefined cells."); |
| 677 | } else |
| 678 | return undefinedcxx . . $ undefined.; |
| 679 | } |
| 680 | return maximum; |
| 681 | } |
| 682 | double NUMmax_removeUndefined_ecxx . . $ NUMmax_removeUndefined_e(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 683 | return NUMmax_removeUndefinedcxx . . $ NUMmax_removeUndefined(7a3744b654164b28). <true> (mat); |
| 684 | } |
| 685 | double NUMmax_removeUndefined_ucxx . . $ NUMmax_removeUndefined_u(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 686 | return NUMmax_removeUndefinedcxx . . $ NUMmax_removeUndefined(7a3744b654164b28). <false> (mat); |
| 687 | } |
| 688 | |
| 689 | template <bool canError> |
| 690 | MelderRealRangecxx . . $ MelderRealRange# NUMextremacxx . . $ NUMextrema(276b80eee54cd772). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 691 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) { |
| 692 | if constexpr (canError) |
| 693 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"extrema_e: cannot compute the extrema of an empty vector."); |
| 694 | else |
| 695 | return { undefinedcxx . . $ undefined., undefinedcxx . . $ undefined. }; |
| 696 | } |
| 697 | double minimum = + stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 698 | double maximum = - stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 699 | for (integercxx . . $ integer# i = 1; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 700 | const double value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 701 | if (isundefcxx . . $ isundef(f8197d536af80c96). (value)) { |
| 702 | if constexpr (canError) |
| 703 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"extrema_e: cannot compute the extrema of a vector: element ", i, U" is undefined."); |
| 704 | else |
| 705 | return { undefinedcxx . . $ undefined., undefinedcxx . . $ undefined. }; |
| 706 | } |
| 707 | if (value < minimum) |
| 708 | minimum = value; |
| 709 | if (value > maximum) |
| 710 | maximum = value; |
| 711 | } |
| 712 | return { minimum, maximum }; |
| 713 | } |
| 714 | MelderRealRangecxx . . $ MelderRealRange# NUMextrema_ecxx . . $ NUMextrema_e(276b80eee54cd772). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 715 | return NUMextremacxx . . $ NUMextrema(276b80eee54cd772). <true> (vec); |
| 716 | } |
| 717 | MelderRealRangecxx . . $ MelderRealRange# NUMextrema_ucxx . . $ NUMextrema_u(453fd68922d5429d). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 718 | return NUMextremacxx . . $ NUMextrema(276b80eee54cd772). <false> (vec); |
| 719 | } |
| 720 | |
| 721 | template <bool canError> |
| 722 | MelderRealRangecxx . . $ MelderRealRange# NUMextremacxx . . $ NUMextrema(409218941a87bbaf). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 723 | if (NUMisEmptycxx . . $ NUMisEmpty(939f42bb6b0c0c9c). (mat)) { |
| 724 | if constexpr (canError) |
| 725 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"extrema_e: cannot compute the extrema of an empty matrix."); |
| 726 | else |
| 727 | return { undefinedcxx . . $ undefined., undefinedcxx . . $ undefined. }; |
| 728 | } |
| 729 | double minimum = + stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 730 | double maximum = - stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<double>::infinitycxx . . $ std/numeric_limits#infinity(e4aa362e81f92a46).(); |
| 731 | for (integercxx . . $ integer# irow = 1; irow <= mat.nrowcxx . . $ constmatrixview#nrow.; irow ++) { |
| 732 | for (integercxx . . $ integer# icol = 1; icol <= mat.ncolcxx . . $ constmatrixview#ncol.; icol ++) { |
| 733 | const double value = mat [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).icol]; |
| 734 | if (isundefcxx . . $ isundef(f8197d536af80c96). (value)) { |
| 735 | if constexpr (canError) |
| 736 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"extrema_e: cannot the compute extrema of a matrix: element [", irow, U", ", icol, U"] is undefined."); |
| 737 | else |
| 738 | return { undefinedcxx . . $ undefined., undefinedcxx . . $ undefined. }; |
| 739 | } |
| 740 | if (value < minimum) |
| 741 | minimum = value; |
| 742 | if (value > maximum) |
| 743 | maximum = value; |
| 744 | } |
| 745 | } |
| 746 | return { minimum, maximum }; |
| 747 | } |
| 748 | MelderRealRangecxx . . $ MelderRealRange# NUMextrema_ecxx . . $ NUMextrema_e(409218941a87bbaf). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 749 | return NUMextremacxx . . $ NUMextrema(409218941a87bbaf). <true> (mat); |
| 750 | } |
| 751 | MelderRealRangecxx . . $ MelderRealRange# NUMextrema_ucxx . . $ NUMextrema_u(422efbc4a34c6b38). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 752 | return NUMextremacxx . . $ NUMextrema(409218941a87bbaf). <false> (mat); |
| 753 | } |
| 754 | |
| 755 | template <bool canError> |
| 756 | MelderIntegerRangecxx . . $ MelderIntegerRange# NUMextremacxx . . $ NUMextrema(cfa463b8968d2917). (constINTVECVUcxx . . $ constINTVECVU# const& vec) { |
| 757 | if (NUMisEmptycxx . . $ NUMisEmpty(cc1ac9a2a7378777). (vec)) { |
| 758 | if constexpr (canError) |
| 759 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"extrema_e: cannot compute the extrema of an empty integer vector."); |
| 760 | else |
| 761 | return { INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`!, INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`! }; |
| 762 | } |
| 763 | integercxx . . $ integer# minimum = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).1], maximum = minimum; |
| 764 | for (integercxx . . $ integer# i = 2; i <= vec.sizecxx . . $ constvectorview#size.; i ++) { |
| 765 | const integercxx . . $ integer# value = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 766 | if (value < minimum) |
| 767 | minimum = value; |
| 768 | if (value > maximum) |
| 769 | maximum = value; |
| 770 | } |
| 771 | return { minimum, maximum }; |
| 772 | } |
| 773 | MelderIntegerRangecxx . . $ MelderIntegerRange# NUMextrema_ecxx . . $ NUMextrema_e(cfa463b8968d2917). (constINTVECVUcxx . . $ constINTVECVU# const& vec) { |
| 774 | return NUMextremacxx . . $ NUMextrema(cfa463b8968d2917). <true> (vec); |
| 775 | } |
| 776 | MelderIntegerRangecxx . . $ MelderIntegerRange# NUMextrema_ucxx . . $ NUMextrema_u(3682e4cc51b95972). (constINTVECVUcxx . . $ constINTVECVU# const& vec) noexcept { |
| 777 | return NUMextremacxx . . $ NUMextrema(cfa463b8968d2917). <false> (vec); |
| 778 | } |
| 779 | |
| 780 | double NUMminimumLengthcxx . . $ NUMminimumLength(ca1a6d5bd822b1c2). (constSTRVECcxx . . $ constSTRVEC# const& x) noexcept { |
| 781 | if (NUMisEmptycxx . . $ NUMisEmpty(72eec41a35d7e37c). (x)) |
| 782 | return undefinedcxx . . $ undefined.; |
| 783 | double result = NUMlengthcxx . . $ NUMlength(d232a409a91fe966). (x [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).1]); |
| 784 | for (integercxx . . $ integer# i = 2; i <= x.sizecxx . . $ _conststringvector#size.; i ++) { |
| 785 | const double length = NUMlengthcxx . . $ NUMlength(d232a409a91fe966). (x [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).i]); |
| 786 | if (length < result) |
| 787 | result = length; |
| 788 | } |
| 789 | return result; |
| 790 | } |
| 791 | double NUMmaximumLengthcxx . . $ NUMmaximumLength(ca1a6d5bd822b1c2). (constSTRVECcxx . . $ constSTRVEC# const& x) noexcept { |
| 792 | if (NUMisEmptycxx . . $ NUMisEmpty(72eec41a35d7e37c). (x)) |
| 793 | return undefinedcxx . . $ undefined.; |
| 794 | double result = NUMlengthcxx . . $ NUMlength(d232a409a91fe966). (x [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).1]); |
| 795 | for (integercxx . . $ integer# i = 2; i <= x.sizecxx . . $ _conststringvector#size.; i ++) { |
| 796 | const double length = NUMlengthcxx . . $ NUMlength(d232a409a91fe966). (x [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).i]); |
| 797 | if (length > result) |
| 798 | result = length; |
| 799 | } |
| 800 | return result; |
| 801 | } |
| 802 | |
| 803 | double NUMnormcxx . . $ NUMnorm(885a97927582bd48). (constVECVUcxx . . $ constVECVU# const& vec, const double power) noexcept { |
| 804 | if (power < 0.0) |
| 805 | return undefinedcxx . . $ undefined.; |
| 806 | if (power == 2.0) { |
| 807 | const double sum2 = double (NUMsum2_longdoublecxx . . $ NUMsum2_longdouble(4b452550a19f78a4). (vec)); |
| 808 | return sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (sum2); |
| 809 | } else if (power == 1.0) { |
| 810 | const double sumAbs = double (NUMsumAbs_longdoublecxx . . $ NUMsumAbs_longdouble(4b452550a19f78a4). (vec)); |
| 811 | return sumAbs; |
| 812 | } else { |
| 813 | const longdoublecxx . . $ longdouble# sumPower = NUMsumPower_longdoublecxx . . $ NUMsumPower_longdouble(e282ae4a75dec06e). (vec, power); |
| 814 | return double (powl (sumPower, longdoublecxx . . $ longdouble# (1.0) / power)); |
| 815 | } |
| 816 | } |
| 817 | double NUMnormcxx . . $ NUMnorm(e5abd6d02bcd8f8). (constMATVUcxx . . $ constMATVU# const& mat, const double power) noexcept { |
| 818 | if (power < 0.0) |
| 819 | return undefinedcxx . . $ undefined.; |
| 820 | if (power == 2.0) { |
| 821 | const double sum2 = double (NUMsum2_longdoublecxx . . $ NUMsum2_longdouble(d39cb01001833591). (mat)); |
| 822 | return sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (sum2); |
| 823 | } else if (power == 1.0) { |
| 824 | const double sumAbs = double (NUMsumAbs_longdoublecxx . . $ NUMsumAbs_longdouble(d39cb01001833591). (mat)); |
| 825 | return sumAbs; |
| 826 | } else { |
| 827 | const longdoublecxx . . $ longdouble# sumPower = NUMsumPower_longdoublecxx . . $ NUMsumPower_longdouble(f936129dc83ad90c). (mat, power); |
| 828 | return double (powl (sumPower, longdoublecxx . . $ longdouble# (1.0) / power)); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | integercxx . . $ integer# NUMnumberOfTokenscxx . . $ NUMnumberOfTokens(3fae411214b133ad). (const conststring32cxx . . $ conststring32# string) noexcept { |
| 833 | integercxx . . $ integer# numberOfTokens = 0; |
| 834 | const char32cxx . . $ char32# *p = & string [0]; |
| 835 | for (;;) { |
| 836 | Melder_skipHorizontalOrVerticalSpacecxx . . $ Melder_skipHorizontalOrVerticalSpace(1babf4dbe7380927). (& p); |
| 837 | if (*p == U'\0') |
| 838 | break; |
| 839 | numberOfTokens ++; |
| 840 | p ++; // step over first nonspace |
| 841 | p = Melder_findEndOfInkcxx . . $ Melder_findEndOfInk(5553299aa8c4fee9). (p); |
| 842 | } |
| 843 | return numberOfTokens; |
| 844 | } |
| 845 | |
| 846 | double NUMstdevcxx . . $ NUMstdev(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 847 | const integercxx . . $ integer# df = vec.sizecxx . . $ constvectorview#size. - 1; |
| 848 | if (df <= 0) |
| 849 | return undefinedcxx . . $ undefined.; |
| 850 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# stats = NUMmeanSumsqcxx . . $ NUMmeanSumsq(5339da3437877a63). (vec); |
| 851 | const longdoublecxx . . $ longdouble# variance = stats.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. / df; |
| 852 | const double stdev = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (variance)); |
| 853 | return stdev; |
| 854 | } |
| 855 | double NUMstdevcxx . . $ NUMstdev(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 856 | const integercxx . . $ integer# df = mat.nrowcxx . . $ constmatrixview#nrow. * mat.ncolcxx . . $ constmatrixview#ncol. - 1; |
| 857 | if (df <= 0) |
| 858 | return undefinedcxx . . $ undefined.; |
| 859 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# stats = NUMmeanSumsqcxx . . $ NUMmeanSumsq(fd30fb828cc58ed9). (mat); |
| 860 | const longdoublecxx . . $ longdouble# variance = stats.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. / df; |
| 861 | const double stdev = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (variance)); |
| 862 | return stdev; |
| 863 | } |
| 864 | |
| 865 | double NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 866 | const longdoublecxx . . $ longdouble# sum = NUMsum_longdoublecxx . . $ NUMsum_longdouble(4b452550a19f78a4). (vec); |
| 867 | return double (sum); |
| 868 | } |
| 869 | double NUMsumcxx . . $ NUMsum(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 870 | const longdoublecxx . . $ longdouble# sum = NUMsum_longdoublecxx . . $ NUMsum_longdouble(d39cb01001833591). (mat); |
| 871 | return double (sum); |
| 872 | } |
| 873 | |
| 874 | double NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 875 | return double (NUMsum2_longdoublecxx . . $ NUMsum2_longdouble(4b452550a19f78a4). (vec)); |
| 876 | } |
| 877 | double NUMsum2cxx . . $ NUMsum2(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& mat) { |
| 878 | return double (NUMsum2_longdoublecxx . . $ NUMsum2_longdouble(d39cb01001833591). (mat)); |
| 879 | } |
| 880 | |
| 881 | double NUMsumOfSquaredDifferencescxx . . $ NUMsumOfSquaredDifferences(207ff1b89ec3edee). (constVECVUcxx . . $ constVECVU# const& vec, const double mean) { |
| 882 | return double (NUMsumOfSquaredDifferences_longdoublecxx . . $ NUMsumOfSquaredDifferences_longdouble(a510f21eb67343cb). (vec, mean)); |
| 883 | } |
| 884 | |
| 885 | double NUMcorrelationcxx . . $ NUMcorrelation(453baa2d8fbe1ac4). (constVECVUcxx . . $ constVECVU# const& vec1, constVECVUcxx . . $ constVECVU# const& vec2) { |
| 886 | if (vec1.sizecxx . . $ constvectorview#size. != vec2.sizecxx . . $ constvectorview#size.) |
| 887 | return undefinedcxx . . $ undefined.; |
| 888 | if (vec1.sizecxx . . $ constvectorview#size. < 2) |
| 889 | return undefinedcxx . . $ undefined.; |
| 890 | const MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# meanSumsq1 = NUMmeanSumsqcxx . . $ NUMmeanSumsq(5339da3437877a63). (vec1); |
| 891 | const MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# meanSumsq2 = NUMmeanSumsqcxx . . $ NUMmeanSumsq(5339da3437877a63). (vec2); |
| 892 | const longdoublecxx . . $ longdouble# inner = NUMinnerMinusMean_longdoublecxx . . $ NUMinnerMinusMean_longdouble(cca1ad01927fef7). (vec1, meanSumsq1.meancxx . . $ MelderMeanSumsq_longdouble#mean., vec2, meanSumsq2.meancxx . . $ MelderMeanSumsq_longdouble#mean.); |
| 893 | return double (inner) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (meanSumsq1.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. * meanSumsq2.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq.)); |
| 894 | } |
| 895 | |
| 896 | double NUMtotalLengthcxx . . $ NUMtotalLength(eab58d34c81ade32). (constSTRVECcxx . . $ constSTRVEC# const& x) { |
| 897 | double totalLength = 0.0; |
| 898 | for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ _conststringvector#size.; i ++) |
| 899 | totalLength += NUMlengthcxx . . $ NUMlength(d232a409a91fe966). (x [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).i]); |
| 900 | return double (totalLength); |
| 901 | } |
| 902 | |
| 903 | double NUMvariancecxx . . $ NUMvariance(4bb9c3e3a8dd5442). (constVECVUcxx . . $ constVECVU# const& vec) noexcept { |
| 904 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# stats = NUMmeanSumsqcxx . . $ NUMmeanSumsq(5339da3437877a63). (vec); |
| 905 | const longdoublecxx . . $ longdouble# variance = stats.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. / (vec.sizecxx . . $ constvectorview#size. - 1); |
| 906 | return double (variance); |
| 907 | } |
| 908 | double NUMvariancecxx . . $ NUMvariance(98ed2a492958fa8a). (constMATVUcxx . . $ constMATVU# const& mat) noexcept { |
| 909 | MelderMeanSumsq_longdoublecxx . . $ MelderMeanSumsq_longdouble# stats = NUMmeanSumsqcxx . . $ NUMmeanSumsq(fd30fb828cc58ed9). (mat); |
| 910 | const longdoublecxx . . $ longdouble# variance = stats.sumsqcxx . . $ MelderMeanSumsq_longdouble#sumsq. / (mat.nrowcxx . . $ constmatrixview#nrow. * mat.ncolcxx . . $ constmatrixview#ncol. - 1); |
| 911 | return double (variance); |
| 912 | } |
| 913 | |
| 914 | /* End of file NUM.cpp */ |