| 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" |
| 20 |
|
| 21 |
/* |
| 22 |
Local functions. |
| 23 |
*/ |
| 24 |
|
| 25 |
static longdouble NUMsum_longdouble (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.stride == 1) { |
| 40 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsum_longdouble (constMATVU const& mat) { |
| 62 |
if (mat.nrow <= mat.ncol) { |
| 63 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsumOfSquaredDifferences_longdouble (constVECVU const& vec, const double mean) { |
| 83 |
if (vec.stride == 1) { |
| 84 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsum2_longdouble (constVECVU const& vec) { |
| 104 |
if (vec.stride == 1) { |
| 105 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsum2_longdouble (constMATVU const& mat) { |
| 125 |
if (mat.nrow <= mat.ncol) { |
| 126 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsumAbs_longdouble (constVECVU const& vec) { |
| 146 |
if (vec.stride == 1) { |
| 147 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsumAbs_longdouble (constMATVU const& mat) { |
| 167 |
if (mat.nrow <= mat.ncol) { |
| 168 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsumPower_longdouble (constVECVU const& vec, const longdouble power) { |
| 188 |
if (vec.stride == 1) { |
| 189 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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 longdouble NUMsumPower_longdouble (constMATVU const& mat, const longdouble power) { |
| 209 |
if (mat.nrow <= mat.ncol) { |
| 210 |
PAIRWISE_SUM ( |
| 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_SUM ( |
| 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_longdouble { |
| 231 |
longdouble mean, sumsq; |
| 232 |
}; |
| 233 |
|
| 234 |
static MelderMeanSumsq_longdouble NUMmeanSumsq (constVECVU const& vec) noexcept { |
| 235 |
MelderMeanSumsq_longdouble result; |
| 236 |
result.mean = NUMsum_longdouble (vec) / vec.size; |
| 237 |
const double mean = double (result.mean); |
| 238 |
if (vec.stride == 1) { |
| 239 |
PAIRWISE_SUM (longdouble, sumsq, integer, vec.size, |
| 240 |
const double *p = vec. firstCell, |
| 241 |
longdouble (*p - mean) * longdouble (*p - mean), |
| 242 |
p += 1 |
| 243 |
) |
| 244 |
result.sumsq = sumsq; |
| 245 |
} else { |
| 246 |
PAIRWISE_SUM (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.sumsq = sumsq; |
| 252 |
} |
| 253 |
return result; |
| 254 |
} |
| 255 |
static MelderMeanSumsq_longdouble NUMmeanSumsq (constMATVU const& mat) noexcept { |
| 256 |
MelderMeanSumsq_longdouble result; |
| 257 |
result.mean = NUMsum_longdouble (mat) / (mat.nrow * mat.ncol); |
| 258 |
const double mean = double (result.mean); |
| 259 |
if (mat.nrow <= mat.ncol) { |
| 260 |
PAIRWISE_SUM (longdouble, sumsq, integer, mat.nrow, |
| 261 |
integer irow = 1, |
| 262 |
NUMsumOfSquaredDifferences_longdouble (mat [irow], mean), |
| 263 |
irow += 1 |
| 264 |
) |
| 265 |
result.sumsq = sumsq; |
| 266 |
} else { |
| 267 |
PAIRWISE_SUM (longdouble, sumsq, integer, mat.ncol, |
| 268 |
integer icol = 1, |
| 269 |
NUMsumOfSquaredDifferences_longdouble (mat.column (icol), mean), |
| 270 |
icol += 1 |
| 271 |
) |
| 272 |
result.sumsq = sumsq; |
| 273 |
} |
| 274 |
return result; |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
Global functions in alphabetic order. |
| 279 |
*/ |
| 280 |
|
| 281 |
double NUMcenterOfGravity (constVECVU const& x) noexcept { |
| 282 |
longdouble weightedSumOfIndexes = 0.0, sumOfWeights = 0.0; |
| 283 |
for (integer i = 1; i <= x.size; i ++) { |
| 284 |
weightedSumOfIndexes += i * x [i]; |
| 285 |
sumOfWeights += x [i]; |
| 286 |
} |
| 287 |
return double (weightedSumOfIndexes / sumOfWeights); |
| 288 |
} |
| 289 |
|
| 290 |
double NUMinner (constVECVU const& x, constVECVU const& y) noexcept { |
| 291 |
if (x.stride == 1) { |
| 292 |
if (y.stride == 1) { |
| 293 |
PAIRWISE_SUM (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_SUM (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.stride == 1) { |
| 310 |
PAIRWISE_SUM (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_SUM (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 longdouble NUMinnerMinusMean_longdouble (constVECVU const& x, const longdouble meanX, constVECVU const& y, const longdouble meanY) noexcept { |
| 329 |
if (x.stride == 1) { |
| 330 |
if (y.stride == 1) { |
| 331 |
PAIRWISE_SUM (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_SUM (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.stride == 1) { |
| 348 |
PAIRWISE_SUM (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_SUM (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 NUMmean (constVECVU const& vec) noexcept { |
| 367 |
if (NUMisEmpty (vec)) |
| 368 |
return undefined; |
| 369 |
const longdouble sum = NUMsum_longdouble (vec); |
| 370 |
return double (sum / vec.size); |
| 371 |
} |
| 372 |
double NUMmean (constMATVU const& mat) noexcept { |
| 373 |
if (NUMisEmpty (mat)) |
| 374 |
return undefined; |
| 375 |
const longdouble sum = NUMsum_longdouble (mat); |
| 376 |
return double (sum / (mat.nrow * mat.ncol)); |
| 377 |
} |
| 378 |
|
| 379 |
MelderGaussianStats NUMmeanStdev (constVECVU const& vec) noexcept { |
| 380 |
MelderGaussianStats result; |
| 381 |
MelderMeanSumsq_longdouble stats = NUMmeanSumsq (vec); |
| 382 |
result.mean = double (stats.mean); // SHOULD be undefined if vec.size < 1 |
| 383 |
const integer df = vec.size - 1; |
| 384 |
result.stdev = ( df <= 0 ? undefined : sqrt (double (stats.sumsq) / df) ); |
| 385 |
return result; |
| 386 |
} |
| 387 |
|
| 388 |
MelderGaussianStats NUMmeanStdev (constMATVU const& mat) noexcept { |
| 389 |
MelderGaussianStats result; |
| 390 |
MelderMeanSumsq_longdouble stats = NUMmeanSumsq (mat); |
| 391 |
result.mean = double (stats.mean); // SHOULD be undefined if mat.nrow * mat.ncol < 1 |
| 392 |
const integer df = mat.nrow * mat.ncol - 1; |
| 393 |
result.stdev = ( df <= 0 ? undefined : sqrt (double (stats.sumsq / df)) ); |
| 394 |
return result; |
| 395 |
} |
| 396 |
|
| 397 |
template <bool canError> |
| 398 |
double NUMmin (constVECVU const& vec) { // propagate NaNs (including -inf and +inf) |
| 399 |
if (NUMisEmpty (vec)) { |
| 400 |
if constexpr (canError) |
| 401 |
Melder_throw (U"min_e: cannot determine the minimum of an empty vector."); |
| 402 |
else |
| 403 |
return 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 = + std::numeric_limits<double>::infinity(); |
| 420 |
for (integer i = 1; i <= vec.size; i ++) { |
| 421 |
const double value = vec [i]; |
| 422 |
if (isundef (value)) { |
| 423 |
if constexpr (canError) |
| 424 |
Melder_throw (U"min_e: cannot determine the minimum of a vector: element ", i, U" is undefined."); |
| 425 |
else |
| 426 |
return undefined; |
| 427 |
} |
| 428 |
if (value < minimum) |
| 429 |
minimum = value; |
| 430 |
} |
| 431 |
return minimum; |
| 432 |
} |
| 433 |
double NUMmin_e (constVECVU const& vec) { |
| 434 |
return NUMmin <true> (vec); |
| 435 |
} |
| 436 |
double NUMmin_u (constVECVU const& vec) noexcept { |
| 437 |
return NUMmin <false> (vec); |
| 438 |
} |
| 439 |
|
| 440 |
template <bool canError> |
| 441 |
double NUMmin_removeUndefined (constVECVU const& vec) { |
| 442 |
double minimum = + std::numeric_limits<double>::infinity(); |
| 443 |
for (integer i = 1; i <= vec.size; i ++) { |
| 444 |
const double value = vec [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 (isundef (minimum)) { // including the original infinity |
| 453 |
if constexpr (canError) { |
| 454 |
if (NUMisEmpty (vec)) |
| 455 |
Melder_throw (U"min_e: cannot determine the minimum of an empty vector."); |
| 456 |
else |
| 457 |
Melder_throw (U"min_e: cannot determine the minimum of a vector with only undefined elements."); |
| 458 |
} else |
| 459 |
return undefined; |
| 460 |
} |
| 461 |
return minimum; |
| 462 |
} |
| 463 |
double NUMmin_removeUndefined_e (constVECVU const& vec) { |
| 464 |
return NUMmin_removeUndefined <true> (vec); |
| 465 |
} |
| 466 |
double NUMmin_removeUndefined_u (constVECVU const& vec) noexcept { |
| 467 |
return NUMmin_removeUndefined <false> (vec); |
| 468 |
} |
| 469 |
|
| 470 |
template <bool canError> |
| 471 |
integer NUMmin (constINTVECVU const& vec) { |
| 472 |
if (NUMisEmpty (vec)) { |
| 473 |
if constexpr (canError) |
| 474 |
Melder_throw (U"min_e: cannot determine the minimum of an empty integer vector."); |
| 475 |
else |
| 476 |
return INTEGER_MAX; |
| 477 |
} |
| 478 |
integer minimum = vec [1]; |
| 479 |
for (integer i = 2; i <= vec.size; i ++) { |
| 480 |
const integer value = vec [i]; |
| 481 |
if (value < minimum) |
| 482 |
minimum = value; |
| 483 |
} |
| 484 |
return minimum; |
| 485 |
} |
| 486 |
integer NUMmin_e (constINTVECVU const& vec) { |
| 487 |
return NUMmin <true> (vec); |
| 488 |
} |
| 489 |
integer NUMmin_u (constINTVECVU const& vec) noexcept { |
| 490 |
return NUMmin <false> (vec); |
| 491 |
} |
| 492 |
|
| 493 |
template <bool canError> |
| 494 |
double NUMmin (constMATVU const& mat) { // propagate NaNs (including -inf and +inf) |
| 495 |
if (NUMisEmpty (mat)) { |
| 496 |
if constexpr (canError) |
| 497 |
Melder_throw (U"min_e: cannot determine the minimum of an empty matrix."); |
| 498 |
else |
| 499 |
return undefined; |
| 500 |
} |
| 501 |
double minimum = + std::numeric_limits<double>::infinity(); |
| 502 |
for (integer irow = 1; irow <= mat.nrow; irow ++) { |
| 503 |
for (integer icol = 1; icol <= mat.ncol; icol ++) { |
| 504 |
const double value = mat [irow] [icol]; |
| 505 |
if (isundef (value)) { |
| 506 |
if (canError) |
| 507 |
Melder_throw (U"min_e: cannot determine the minimum of a matrix: element [", irow, U", ", icol, U"] is undefined."); |
| 508 |
else |
| 509 |
return undefined; |
| 510 |
} |
| 511 |
if (value < minimum) |
| 512 |
minimum = value; |
| 513 |
} |
| 514 |
} |
| 515 |
return minimum; |
| 516 |
} |
| 517 |
double NUMmin_e (constMATVU const& mat) { |
| 518 |
return NUMmin <true> (mat); |
| 519 |
} |
| 520 |
double NUMmin_u (constMATVU const& mat) noexcept { |
| 521 |
return NUMmin <false> (mat); |
| 522 |
} |
| 523 |
|
| 524 |
template <bool canError> |
| 525 |
double NUMmin_removeUndefined (constMATVU const& mat) { |
| 526 |
double minimum = + std::numeric_limits<double>::infinity(); |
| 527 |
for (integer irow = 1; irow <= mat.nrow; irow ++) { |
| 528 |
for (integer icol = 1; icol <= mat.ncol; icol ++) { |
| 529 |
const double value = mat [irow] [icol]; |
| 530 |
if (value < minimum) // NaN-correct, because false if value is undefined |
| 531 |
minimum = value; |
| 532 |
} |
| 533 |
} |
| 534 |
if (isundef (minimum)) { // including the original infinity |
| 535 |
if constexpr (canError) { |
| 536 |
if (NUMisEmpty (mat)) |
| 537 |
Melder_throw (U"min_removeUndefined_e: cannot determine the minimum of an empty matrix."); |
| 538 |
else |
| 539 |
Melder_throw (U"min_removeUndefined_e: cannot determine the minimum of a matrix with only undefined cells."); |
| 540 |
} else |
| 541 |
return undefined; |
| 542 |
} |
| 543 |
return minimum; |
| 544 |
} |
| 545 |
double NUMmin_removeUndefined_e (constMATVU const& mat) { |
| 546 |
return NUMmin_removeUndefined <true> (mat); |
| 547 |
} |
| 548 |
double NUMmin_removeUndefined_u (constMATVU const& mat) noexcept { |
| 549 |
return NUMmin_removeUndefined <false> (mat); |
| 550 |
} |
| 551 |
|
| 552 |
template <bool canError> |
| 553 |
double NUMmax (constVECVU const& vec) { // propagate NaNs (including -inf and +inf) |
| 554 |
if (NUMisEmpty (vec)) { |
| 555 |
if constexpr (canError) |
| 556 |
Melder_throw (U"max_e: cannot determine the maximum of an empty vector."); |
| 557 |
else |
| 558 |
return undefined; |
| 559 |
} |
| 560 |
double maximum = - std::numeric_limits<double>::infinity(); |
| 561 |
for (integer i = 1; i <= vec.size; i ++) { |
| 562 |
const double value = vec [i]; |
| 563 |
if (isundef (value)) { |
| 564 |
if constexpr (canError) |
| 565 |
Melder_throw (U"max_e: cannot determine the maximum of a vector: element ", i, U" is undefined."); |
| 566 |
else |
| 567 |
return undefined; |
| 568 |
} |
| 569 |
if (value > maximum) |
| 570 |
maximum = value; |
| 571 |
} |
| 572 |
return maximum; |
| 573 |
} |
| 574 |
double NUMmax_e (constVECVU const& vec) { |
| 575 |
return NUMmax <true> (vec); |
| 576 |
} |
| 577 |
double NUMmax_u (constVECVU const& vec) noexcept { |
| 578 |
return NUMmax <false> (vec); |
| 579 |
} |
| 580 |
|
| 581 |
template <bool canError> |
| 582 |
double NUMmax_removeUndefined (constVECVU const& vec) { |
| 583 |
double maximum = - std::numeric_limits<double>::infinity(); |
| 584 |
for (integer i = 1; i <= vec.size; i ++) { |
| 585 |
const double value = vec [i]; |
| 586 |
if (value > maximum) // NaN-safe, because false if value is undefined |
| 587 |
maximum = value; |
| 588 |
} |
| 589 |
if (isundef (maximum)) { // including the original -infinity |
| 590 |
if constexpr (canError) { |
| 591 |
if (NUMisEmpty (vec)) |
| 592 |
Melder_throw (U"max_removeUndefined_e: cannot determine the maximum of an empty vector."); |
| 593 |
else |
| 594 |
Melder_throw (U"max_removeUndefined_e: cannot determine the maximum of a vector with only undefined elements."); |
| 595 |
} else |
| 596 |
return undefined; |
| 597 |
} |
| 598 |
return maximum; |
| 599 |
} |
| 600 |
double NUMmax_removeUndefined_e (constVECVU const& vec) { |
| 601 |
return NUMmax_removeUndefined <true> (vec); |
| 602 |
} |
| 603 |
double NUMmax_removeUndefined_u (constVECVU const& vec) noexcept { |
| 604 |
return NUMmax_removeUndefined <false> (vec); |
| 605 |
} |
| 606 |
|
| 607 |
template <bool canError> |
| 608 |
integer NUMmax (constINTVECVU const& vec) { |
| 609 |
if (NUMisEmpty (vec)) { |
| 610 |
if constexpr (canError) |
| 611 |
Melder_throw (U"max_e: cannot determine the maximum of an empty integer vector."); |
| 612 |
else |
| 613 |
return INTEGER_MIN; |
| 614 |
} |
| 615 |
integer maximum = vec [1]; |
| 616 |
for (integer i = 2; i <= vec.size; i ++) { |
| 617 |
const integer value = vec [i]; |
| 618 |
if (value > maximum) |
| 619 |
maximum = value; |
| 620 |
} |
| 621 |
return maximum; |
| 622 |
} |
| 623 |
integer NUMmax_e (constINTVECVU const& vec) { |
| 624 |
return NUMmax <true> (vec); |
| 625 |
} |
| 626 |
integer NUMmax_u (constINTVECVU const& vec) noexcept { |
| 627 |
return NUMmax <false> (vec); |
| 628 |
} |
| 629 |
|
| 630 |
template <bool canError> |
| 631 |
double NUMmax (constMATVU const& mat) { // propagate NaNs (including -inf and +inf) |
| 632 |
if (NUMisEmpty (mat)) { |
| 633 |
if constexpr (canError) |
| 634 |
Melder_throw (U"max_e: cannot determine the maximum of an empty matrix."); |
| 635 |
else |
| 636 |
return undefined; |
| 637 |
} |
| 638 |
double maximum = - std::numeric_limits<double>::infinity(); |
| 639 |
for (integer irow = 1; irow <= mat.nrow; irow ++) { |
| 640 |
for (integer icol = 1; icol <= mat.ncol; icol ++) { |
| 641 |
const double value = mat [irow] [icol]; |
| 642 |
if (isundef (value)) { |
| 643 |
if constexpr (canError) |
| 644 |
Melder_throw (U"max_e: cannot determine the maximum of a matrix: element [", irow, U", ", icol, U"] is undefined."); |
| 645 |
else |
| 646 |
return undefined; |
| 647 |
} |
| 648 |
if (value > maximum) |
| 649 |
maximum = value; |
| 650 |
} |
| 651 |
} |
| 652 |
return maximum; |
| 653 |
} |
| 654 |
double NUMmax_e (constMATVU const& mat) { |
| 655 |
return NUMmax <true> (mat); |
| 656 |
} |
| 657 |
double NUMmax_u (constMATVU const& mat) noexcept { // propagate NaNs (including -inf and +inf) |
| 658 |
return NUMmax <false> (mat); |
| 659 |
} |
| 660 |
|
| 661 |
template <bool canError> |
| 662 |
double NUMmax_removeUndefined (constMATVU const& mat) { |
| 663 |
double maximum = - std::numeric_limits<double>::infinity(); |
| 664 |
for (integer irow = 1; irow <= mat.nrow; irow ++) { |
| 665 |
for (integer icol = 1; icol <= mat.ncol; icol ++) { |
| 666 |
const double value = mat [irow] [icol]; |
| 667 |
if (value > maximum) // NaN-safe, because false if value is undefined |
| 668 |
maximum = value; |
| 669 |
} |
| 670 |
} |
| 671 |
if (isundef (maximum)) { // including the original -infinity |
| 672 |
if constexpr (canError) { |
| 673 |
if (NUMisEmpty (mat)) |
| 674 |
Melder_throw (U"max_removeUndefined_e: cannot determine the maximum of an empty matrix."); |
| 675 |
else |
| 676 |
Melder_throw (U"max_removeUndefined_e: cannot determine the maximum of a matrix with only undefined cells."); |
| 677 |
} else |
| 678 |
return undefined; |
| 679 |
} |
| 680 |
return maximum; |
| 681 |
} |
| 682 |
double NUMmax_removeUndefined_e (constMATVU const& mat) { |
| 683 |
return NUMmax_removeUndefined <true> (mat); |
| 684 |
} |
| 685 |
double NUMmax_removeUndefined_u (constMATVU const& mat) noexcept { |
| 686 |
return NUMmax_removeUndefined <false> (mat); |
| 687 |
} |
| 688 |
|
| 689 |
template <bool canError> |
| 690 |
MelderRealRange NUMextrema (constVECVU const& vec) { |
| 691 |
if (NUMisEmpty (vec)) { |
| 692 |
if constexpr (canError) |
| 693 |
Melder_throw (U"extrema_e: cannot compute the extrema of an empty vector."); |
| 694 |
else |
| 695 |
return { undefined, undefined }; |
| 696 |
} |
| 697 |
double minimum = + std::numeric_limits<double>::infinity(); |
| 698 |
double maximum = - std::numeric_limits<double>::infinity(); |
| 699 |
for (integer i = 1; i <= vec.size; i ++) { |
| 700 |
const double value = vec [i]; |
| 701 |
if (isundef (value)) { |
| 702 |
if constexpr (canError) |
| 703 |
Melder_throw (U"extrema_e: cannot compute the extrema of a vector: element ", i, U" is undefined."); |
| 704 |
else |
| 705 |
return { undefined, undefined }; |
| 706 |
} |
| 707 |
if (value < minimum) |
| 708 |
minimum = value; |
| 709 |
if (value > maximum) |
| 710 |
maximum = value; |
| 711 |
} |
| 712 |
return { minimum, maximum }; |
| 713 |
} |
| 714 |
MelderRealRange NUMextrema_e (constVECVU const& vec) { |
| 715 |
return NUMextrema <true> (vec); |
| 716 |
} |
| 717 |
MelderRealRange NUMextrema_u (constVECVU const& vec) noexcept { |
| 718 |
return NUMextrema <false> (vec); |
| 719 |
} |
| 720 |
|
| 721 |
template <bool canError> |
| 722 |
MelderRealRange NUMextrema (constMATVU const& mat) { |
| 723 |
if (NUMisEmpty (mat)) { |
| 724 |
if constexpr (canError) |
| 725 |
Melder_throw (U"extrema_e: cannot compute the extrema of an empty matrix."); |
| 726 |
else |
| 727 |
return { undefined, undefined }; |
| 728 |
} |
| 729 |
double minimum = + std::numeric_limits<double>::infinity(); |
| 730 |
double maximum = - std::numeric_limits<double>::infinity(); |
| 731 |
for (integer irow = 1; irow <= mat.nrow; irow ++) { |
| 732 |
for (integer icol = 1; icol <= mat.ncol; icol ++) { |
| 733 |
const double value = mat [irow] [icol]; |
| 734 |
if (isundef (value)) { |
| 735 |
if constexpr (canError) |
| 736 |
Melder_throw (U"extrema_e: cannot the compute extrema of a matrix: element [", irow, U", ", icol, U"] is undefined."); |
| 737 |
else |
| 738 |
return { undefined, undefined }; |
| 739 |
} |
| 740 |
if (value < minimum) |
| 741 |
minimum = value; |
| 742 |
if (value > maximum) |
| 743 |
maximum = value; |
| 744 |
} |
| 745 |
} |
| 746 |
return { minimum, maximum }; |
| 747 |
} |
| 748 |
MelderRealRange NUMextrema_e (constMATVU const& mat) { |
| 749 |
return NUMextrema <true> (mat); |
| 750 |
} |
| 751 |
MelderRealRange NUMextrema_u (constMATVU const& mat) noexcept { |
| 752 |
return NUMextrema <false> (mat); |
| 753 |
} |
| 754 |
|
| 755 |
template <bool canError> |
| 756 |
MelderIntegerRange NUMextrema (constINTVECVU const& vec) { |
| 757 |
if (NUMisEmpty (vec)) { |
| 758 |
if constexpr (canError) |
| 759 |
Melder_throw (U"extrema_e: cannot compute the extrema of an empty integer vector."); |
| 760 |
else |
| 761 |
return { INTEGER_MIN, INTEGER_MAX }; |
| 762 |
} |
| 763 |
integer minimum = vec [1], maximum = minimum; |
| 764 |
for (integer i = 2; i <= vec.size; i ++) { |
| 765 |
const integer value = vec [i]; |
| 766 |
if (value < minimum) |
| 767 |
minimum = value; |
| 768 |
if (value > maximum) |
| 769 |
maximum = value; |
| 770 |
} |
| 771 |
return { minimum, maximum }; |
| 772 |
} |
| 773 |
MelderIntegerRange NUMextrema_e (constINTVECVU const& vec) { |
| 774 |
return NUMextrema <true> (vec); |
| 775 |
} |
| 776 |
MelderIntegerRange NUMextrema_u (constINTVECVU const& vec) noexcept { |
| 777 |
return NUMextrema <false> (vec); |
| 778 |
} |
| 779 |
|
| 780 |
double NUMminimumLength (constSTRVEC const& x) noexcept { |
| 781 |
if (NUMisEmpty (x)) |
| 782 |
return undefined; |
| 783 |
double result = NUMlength (x [1]); |
| 784 |
for (integer i = 2; i <= x.size; i ++) { |
| 785 |
const double length = NUMlength (x [i]); |
| 786 |
if (length < result) |
| 787 |
result = length; |
| 788 |
} |
| 789 |
return result; |
| 790 |
} |
| 791 |
double NUMmaximumLength (constSTRVEC const& x) noexcept { |
| 792 |
if (NUMisEmpty (x)) |
| 793 |
return undefined; |
| 794 |
double result = NUMlength (x [1]); |
| 795 |
for (integer i = 2; i <= x.size; i ++) { |
| 796 |
const double length = NUMlength (x [i]); |
| 797 |
if (length > result) |
| 798 |
result = length; |
| 799 |
} |
| 800 |
return result; |
| 801 |
} |
| 802 |
|
| 803 |
double NUMnorm (constVECVU const& vec, const double power) noexcept { |
| 804 |
if (power < 0.0) |
| 805 |
return undefined; |
| 806 |
if (power == 2.0) { |
| 807 |
const double sum2 = double (NUMsum2_longdouble (vec)); |
| 808 |
return sqrt (sum2); |
| 809 |
} else if (power == 1.0) { |
| 810 |
const double sumAbs = double (NUMsumAbs_longdouble (vec)); |
| 811 |
return sumAbs; |
| 812 |
} else { |
| 813 |
const longdouble sumPower = NUMsumPower_longdouble (vec, power); |
| 814 |
return double (powl (sumPower, longdouble (1.0) / power)); |
| 815 |
} |
| 816 |
} |
| 817 |
double NUMnorm (constMATVU const& mat, const double power) noexcept { |
| 818 |
if (power < 0.0) |
| 819 |
return undefined; |
| 820 |
if (power == 2.0) { |
| 821 |
const double sum2 = double (NUMsum2_longdouble (mat)); |
| 822 |
return sqrt (sum2); |
| 823 |
} else if (power == 1.0) { |
| 824 |
const double sumAbs = double (NUMsumAbs_longdouble (mat)); |
| 825 |
return sumAbs; |
| 826 |
} else { |
| 827 |
const longdouble sumPower = NUMsumPower_longdouble (mat, power); |
| 828 |
return double (powl (sumPower, longdouble (1.0) / power)); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
integer NUMnumberOfTokens (const conststring32 string) noexcept { |
| 833 |
integer numberOfTokens = 0; |
| 834 |
const char32 *p = & string [0]; |
| 835 |
for (;;) { |
| 836 |
Melder_skipHorizontalOrVerticalSpace (& p); |
| 837 |
if (*p == U'\0') |
| 838 |
break; |
| 839 |
numberOfTokens ++; |
| 840 |
p ++; // step over first nonspace |
| 841 |
p = Melder_findEndOfInk (p); |
| 842 |
} |
| 843 |
return numberOfTokens; |
| 844 |
} |
| 845 |
|
| 846 |
double NUMstdev (constVECVU const& vec) noexcept { |
| 847 |
const integer df = vec.size - 1; |
| 848 |
if (df <= 0) |
| 849 |
return undefined; |
| 850 |
MelderMeanSumsq_longdouble stats = NUMmeanSumsq (vec); |
| 851 |
const longdouble variance = stats.sumsq / df; |
| 852 |
const double stdev = sqrt (double (variance)); |
| 853 |
return stdev; |
| 854 |
} |
| 855 |
double NUMstdev (constMATVU const& mat) noexcept { |
| 856 |
const integer df = mat.nrow * mat.ncol - 1; |
| 857 |
if (df <= 0) |
| 858 |
return undefined; |
| 859 |
MelderMeanSumsq_longdouble stats = NUMmeanSumsq (mat); |
| 860 |
const longdouble variance = stats.sumsq / df; |
| 861 |
const double stdev = sqrt (double (variance)); |
| 862 |
return stdev; |
| 863 |
} |
| 864 |
|
| 865 |
double NUMsum (constVECVU const& vec) noexcept { |
| 866 |
const longdouble sum = NUMsum_longdouble (vec); |
| 867 |
return double (sum); |
| 868 |
} |
| 869 |
double NUMsum (constMATVU const& mat) noexcept { |
| 870 |
const longdouble sum = NUMsum_longdouble (mat); |
| 871 |
return double (sum); |
| 872 |
} |
| 873 |
|
| 874 |
double NUMsum2 (constVECVU const& vec) { |
| 875 |
return double (NUMsum2_longdouble (vec)); |
| 876 |
} |
| 877 |
double NUMsum2 (constMATVU const& mat) { |
| 878 |
return double (NUMsum2_longdouble (mat)); |
| 879 |
} |
| 880 |
|
| 881 |
double NUMsumOfSquaredDifferences (constVECVU const& vec, const double mean) { |
| 882 |
return double (NUMsumOfSquaredDifferences_longdouble (vec, mean)); |
| 883 |
} |
| 884 |
|
| 885 |
double NUMcorrelation (constVECVU const& vec1, constVECVU const& vec2) { |
| 886 |
if (vec1.size != vec2.size) |
| 887 |
return undefined; |
| 888 |
if (vec1.size < 2) |
| 889 |
return undefined; |
| 890 |
const MelderMeanSumsq_longdouble meanSumsq1 = NUMmeanSumsq (vec1); |
| 891 |
const MelderMeanSumsq_longdouble meanSumsq2 = NUMmeanSumsq (vec2); |
| 892 |
const longdouble inner = NUMinnerMinusMean_longdouble (vec1, meanSumsq1.mean, vec2, meanSumsq2.mean); |
| 893 |
return double (inner) / sqrt (double (meanSumsq1.sumsq * meanSumsq2.sumsq)); |
| 894 |
} |
| 895 |
|
| 896 |
double NUMtotalLength (constSTRVEC const& x) { |
| 897 |
double totalLength = 0.0; |
| 898 |
for (integer i = 1; i <= x.size; i ++) |
| 899 |
totalLength += NUMlength (x [i]); |
| 900 |
return double (totalLength); |
| 901 |
} |
| 902 |
|
| 903 |
double NUMvariance (constVECVU const& vec) noexcept { |
| 904 |
MelderMeanSumsq_longdouble stats = NUMmeanSumsq (vec); |
| 905 |
const longdouble variance = stats.sumsq / (vec.size - 1); |
| 906 |
return double (variance); |
| 907 |
} |
| 908 |
double NUMvariance (constMATVU const& mat) noexcept { |
| 909 |
MelderMeanSumsq_longdouble stats = NUMmeanSumsq (mat); |
| 910 |
const longdouble variance = stats.sumsq / (mat.nrow * mat.ncol - 1); |
| 911 |
return double (variance); |
| 912 |
} |
| 913 |
|
| 914 |
/* End of file NUM.cpp */ |