| 1 | #pragma once |
| 2 | /* NUM.h |
| 3 | * |
| 4 | * Copyright (C) 1992-2024 Paul Boersma |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | * See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | The following functions are called in inline functions, |
| 22 | so they have to be declared first. |
| 23 | */ |
| 24 | |
| 25 | template <typename T> |
| 26 | bool NUMisEmpty (vector<T> const& x) noexcept { |
| 27 | return x.size == 0; // note: testing on x.at is incorrect, because the capacity may be large when the size is 0 |
| 28 | } |
| 29 | template <typename T> |
| 30 | bool NUMisEmpty (vectorview<T> const& x) noexcept { |
| 31 | return x.size == 0; // note: testing on x.at is incorrect, because the capacity may be large when the size is 0 |
| 32 | } |
| 33 | template <typename T> |
| 34 | bool NUMisEmpty (constvector<T> const& x) noexcept { |
| 35 | return x.size == 0; // note: testing on x.at is incorrect, because the capacity may be large when the size is 0 |
| 36 | } |
| 37 | template <typename T> |
| 38 | bool NUMisEmpty (constvectorview<T> const& x) noexcept { |
| 39 | return x.size == 0; // note: testing on x.at is incorrect, because the capacity may be large when the size is 0 |
| 40 | } |
| 41 | |
| 42 | template <typename T> |
| 43 | bool NUMisEmpty (matrix<T> const& x) noexcept { |
| 44 | const integer numberOfCells = x.nrow * x.ncol; |
| 45 | return numberOfCells == 0; // note: a matrix with 0 rows and 6 columns is a valid empty matrix, to which e.g. a row can be added |
| 46 | } |
| 47 | template <typename T> |
| 48 | bool NUMisEmpty (constmatrix<T> const& x) noexcept { |
| 49 | const integer numberOfCells = x.nrow * x.ncol; |
| 50 | return numberOfCells == 0; // note: a matrix with 0 rows and 6 columns is a valid empty matrix, to which e.g. a row can be added |
| 51 | } |
| 52 | template <typename T> |
| 53 | bool NUMisEmpty (matrixview<T> const& x) noexcept { |
| 54 | const integer numberOfCells = x.nrow * x.ncol; |
| 55 | return numberOfCells == 0; // note: a matrix with 0 rows and 6 columns is a valid empty matrix, to which e.g. a row can be added |
| 56 | } |
| 57 | template <typename T> |
| 58 | bool NUMisEmpty (constmatrixview<T> const& x) noexcept { |
| 59 | const integer numberOfCells = x.nrow * x.ncol; |
| 60 | return numberOfCells == 0; // note: a matrix with 0 rows and 6 columns is a valid empty matrix, to which e.g. a row can be added |
| 61 | } |
| 62 | |
| 63 | inline bool NUMisEmpty (constSTRVEC const& x) noexcept { |
| 64 | return x.size == 0; |
| 65 | } |
| 66 | |
| 67 | extern MelderRealRange NUMextrema_e (constVECVU const& vec); |
| 68 | extern MelderRealRange NUMextrema_u (constVECVU const& vec) noexcept; |
| 69 | extern MelderRealRange NUMextrema_e (constMATVU const& mat); |
| 70 | extern MelderRealRange NUMextrema_u (constMATVU const& mat) noexcept; |
| 71 | extern MelderIntegerRange NUMextrema_e (constINTVECVU const& vec); |
| 72 | extern MelderIntegerRange NUMextrema_u (constINTVECVU const& vec) noexcept; |
| 73 | |
| 74 | /* |
| 75 | From here on, the functions appear in alphabetical order. |
| 76 | */ |
| 77 | |
| 78 | extern double NUMcenterOfGravity (constVECVU const& x) noexcept; |
| 79 | |
| 80 | inline integer NUMcountDefined (constVECVU const& vec) noexcept { |
| 81 | integer result = 0; |
| 82 | for (integer i = 1; i <= vec.size; i ++) |
| 83 | if (isdefined (vec [i])) |
| 84 | result += 1; |
| 85 | return result; |
| 86 | } |
| 87 | inline bool NUMdefined (constVECVU const& vec) noexcept { |
| 88 | for (integer i = 1; i <= vec.size; i ++) |
| 89 | if (isundef (vec [i])) |
| 90 | return false; |
| 91 | return true; |
| 92 | } |
| 93 | inline bool NUMdefined (constMATVU const& mat) noexcept { |
| 94 | for (integer irow = 1; irow <= mat.nrow; irow ++) |
| 95 | for (integer icol = 1; icol <= mat.ncol; icol ++) |
| 96 | if (isundef (mat [irow] [icol])) |
| 97 | return false; |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | inline bool NUMequal (double x, double y) { |
| 102 | /* |
| 103 | Any defined value is unequal to --undefined--, |
| 104 | but any undefined value (inf or NaN) *is* equal to --undefined--. |
| 105 | This is different from how "==" works in C, hence the following complication. |
| 106 | */ |
| 107 | return x == y || (isundef (x) && isundef (y)); |
| 108 | } |
| 109 | inline bool NUMequal (integer x, integer y) { |
| 110 | return x == y; |
| 111 | } |
| 112 | inline bool NUMequal (bool x, bool y) { |
| 113 | return x == y; |
| 114 | } |
| 115 | inline bool NUMequal (byte x, byte y) { |
| 116 | return x == y; |
| 117 | } |
| 118 | inline bool NUMequal (dcomplex x, dcomplex y) { |
| 119 | return x.real() == y.real() && x.imag() == y.imag(); // |
| 120 | } |
| 121 | template <typename T> |
| 122 | bool NUMequal (constvector<T> const& x, constvector<T> const& y) noexcept { |
| 123 | const integer n = x.size; |
| 124 | if (y.size != n) |
| 125 | return false; |
| 126 | for (integer i = 1; i <= n; i ++) |
| 127 | if (! NUMequal (x [i], y [i])) |
| 128 | return false; |
| 129 | return true; |
| 130 | } |
| 131 | template <typename T> |
| 132 | bool NUMequal (vector<T> const& x, constvector<T> const& y) noexcept { |
| 133 | return NUMequal (constvector<T> (x), y); |
| 134 | } |
| 135 | template <typename T> |
| 136 | bool NUMequal (constvector<T> const& x, vector<T> const& y) noexcept { |
| 137 | return NUMequal (x, constvector<T> (y)); |
| 138 | } |
| 139 | template <typename T> |
| 140 | bool NUMequal (vector<T> const& x, vector<T> const& y) noexcept { |
| 141 | return NUMequal (constvector<T> (x), constvector<T> (y)); |
| 142 | } |
| 143 | |
| 144 | template <typename T> |
| 145 | bool NUMequal (constmatrix<T> const& x, constmatrix<T> const& y) noexcept { |
| 146 | const integer nrow = x.nrow, ncol = x.ncol; |
| 147 | if (y.nrow != nrow || y.ncol != ncol) |
| 148 | return false; |
| 149 | for (integer irow = 1; irow <= nrow; irow ++) |
| 150 | for (integer icol = 1; icol <= ncol; icol ++) |
| 151 | if (! NUMequal (x [irow] [icol], y [irow] [icol])) |
| 152 | return false; |
| 153 | return true; |
| 154 | } |
| 155 | template <typename T> |
| 156 | bool NUMequal (matrix<T> const& x, constmatrix<T> const& y) noexcept { |
| 157 | return NUMequal (constmatrix<T> (x), y); |
| 158 | } |
| 159 | template <typename T> |
| 160 | bool NUMequal (constmatrix<T> const& x, matrix<T> const& y) noexcept { |
| 161 | return NUMequal (x, constmatrix<T> (y)); |
| 162 | } |
| 163 | template <typename T> |
| 164 | bool NUMequal (matrix<T> const& x, matrix<T> const& y) noexcept { |
| 165 | return NUMequal (constmatrix<T> (x), constmatrix<T> (y)); |
| 166 | } |
| 167 | |
| 168 | inline bool NUMequal (constSTRVEC x, constSTRVEC y) noexcept { |
| 169 | const integer n = x.size; |
| 170 | if (y.size != n) |
| 171 | return false; |
| 172 | for (integer i = 1; i <= n; i ++) |
| 173 | if (! Melder_equ (x [i], y [i])) |
| 174 | return false; |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | inline double NUMextremum_e (constVECVU const& vec) { |
| 179 | if (NUMisEmpty (vec)) |
| 180 | Melder_throw (U"Cannot determine the extremum of an empty vector."); |
| 181 | double extremum = 0.0; |
| 182 | for (integer i = 1; i <= vec.size; i ++) { |
| 183 | const double value = fabs (vec [i]); |
| 184 | if (isundef (value)) |
| 185 | Melder_throw (U"Cannot determine the extremum of a vector: element ", i, U" is undefined."); |
| 186 | if (value > extremum) |
| 187 | extremum = value; |
| 188 | } |
| 189 | return extremum; |
| 190 | } |
| 191 | inline double NUMextremum_u (constVECVU const& vec) noexcept { |
| 192 | if (NUMisEmpty (vec)) |
| 193 | return undefined; |
| 194 | double extremum = 0.0; |
| 195 | for (integer i = 1; i <= vec.size; i ++) { |
| 196 | const double value = fabs (vec [i]); |
| 197 | if (isundef (value)) |
| 198 | return undefined; |
| 199 | if (value > extremum) |
| 200 | extremum = value; |
| 201 | } |
| 202 | return extremum; |
| 203 | } |
| 204 | inline double NUMextremum_e (constMATVU const& mat) { |
| 205 | const MelderRealRange range = NUMextrema_e (mat); |
| 206 | return std::max (fabs (range.min), fabs (range.max)); |
| 207 | } |
| 208 | inline double NUMextremum_u (constMATVU const& mat) { |
| 209 | const MelderRealRange range = NUMextrema_u (mat); |
| 210 | if (isundef (range)) |
| 211 | return undefined; |
| 212 | return std::max (fabs (range.min), fabs (range.max)); |
| 213 | } |
| 214 | |
| 215 | inline integer NUMfindFirst (constSTRVEC const& strvec, conststring32 str) { |
| 216 | for (integer i = 1; i <= strvec.size; i ++) |
| 217 | if (Melder_equ (strvec [i], str)) |
| 218 | return i; |
| 219 | return 0; |
| 220 | } |
| 221 | inline integer NUMfindFirst_caseInsensitive (constSTRVEC const& strvec, conststring32 str) { |
| 222 | for (integer i = 1; i <= strvec.size; i ++) |
| 223 | if (Melder_equ_caseInsensitive (strvec [i], str)) |
| 224 | return i; |
| 225 | return 0; |
| 226 | } |
| 227 | inline integer NUMfindLast (constSTRVEC const& strvec, conststring32 str) { |
| 228 | for (integer i = strvec.size; i >= 1; i --) |
| 229 | if (Melder_equ (strvec [i], str)) |
| 230 | return i; |
| 231 | return 0; |
| 232 | } |
| 233 | inline integer NUMfindLast_caseInsensitive (constSTRVEC const& strvec, conststring32 str) { |
| 234 | for (integer i = strvec.size; i >= 1; i --) |
| 235 | if (Melder_equ (strvec [i], str)) |
| 236 | return i; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | inline integer NUMfindInSorted (constSTRVEC const& strvec, conststring32 string) { |
| 241 | if (strvec.size == 0) |
| 242 | return 0; |
| 243 | |
| 244 | const int atEnd = str32cmp (string, strvec [strvec.size]); |
| 245 | if (atEnd > 0) |
| 246 | return 0; |
| 247 | if (atEnd == 0) |
| 248 | return strvec.size; |
| 249 | |
| 250 | const int atStart = str32cmp (string, strvec [1]); |
| 251 | if (atStart < 0) |
| 252 | return 0; |
| 253 | if (atStart == 0) |
| 254 | return 1; |
| 255 | |
| 256 | integer left = 1, right = strvec.size; |
| 257 | while (left < right - 1) { |
| 258 | const integer mid = (left + right) / 2; |
| 259 | const int here = str32cmp (string, strvec [mid]); |
| 260 | if (here == 0) |
| 261 | return mid; |
| 262 | if (here > 0) |
| 263 | left = mid; |
| 264 | else |
| 265 | right = mid; |
| 266 | } |
| 267 | Melder_assert (right == left + 1); |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | extern double NUMinner (constVECVU const& x, constVECVU const& y) noexcept; |
| 272 | |
| 273 | inline bool NUMisSquare (constMATVU const& x) noexcept { |
| 274 | return x.nrow == x.ncol; |
| 275 | } |
| 276 | |
| 277 | inline bool NUMisSymmetric (constMATVU const& x) noexcept { |
| 278 | if (x.nrow != x.ncol) |
| 279 | return false; |
| 280 | const integer n = x.nrow; |
| 281 | for (integer irow = 1; irow <= n; irow ++) |
| 282 | for (integer icol = irow + 1; icol < n; icol ++) |
| 283 | if (x [irow] [icol] != x [icol] [irow]) |
| 284 | return false; |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | inline double NUMlength (conststring32 str) noexcept { |
| 289 | return double (Melder_length (str)); |
| 290 | } |
| 291 | |
| 292 | inline double NUMlog2 (double x) noexcept { |
| 293 | return log (x) * NUMlog2e; |
| 294 | } |
| 295 | |
| 296 | extern double NUMmin_e (constVECVU const& vec); // throw if empty or undefined element |
| 297 | extern double NUMmin_u (constVECVU const& vec) noexcept; // undef if empty or undefined element |
| 298 | extern double NUMmin_removeUndefined_e (constVECVU const& vec); // throw if no defined elements |
| 299 | extern double NUMmin_removeUndefined_u (constVECVU const& vec) noexcept; // undef if no defined elements |
| 300 | extern integer NUMmin_e (constINTVECVU const& vec); // throw if empty |
| 301 | extern integer NUMmin_u (constINTVECVU const& vec) noexcept; // intmax if empty |
| 302 | extern double NUMmin_e (constMATVU const& mat); // throw if empty or undefined element |
| 303 | extern double NUMmin_u (constMATVU const& mat) noexcept; // undef if empty or undefined element |
| 304 | extern double NUMmin_removeUndefined_e (constMATVU const& mat); // throw if no defined elements |
| 305 | extern double NUMmin_removeUndefined_u (constMATVU const& mat) noexcept; // undef if no defined elements |
| 306 | |
| 307 | extern double NUMmax_e (constVECVU const& vec); // throw if empty or undefined element |
| 308 | extern double NUMmax_u (constVECVU const& vec) noexcept; // undef if empty or undefined element |
| 309 | extern double NUMmax_removeUndefined_e (constVECVU const& vec); // throw if no defined elements |
| 310 | extern double NUMmax_removeUndefined_u (constVECVU const& vec) noexcept; // undef if no defined elements |
| 311 | extern integer NUMmax_e (constINTVECVU const& vec); // throw if empty |
| 312 | extern integer NUMmax_u (constINTVECVU const& vec) noexcept; // intmin if empty |
| 313 | extern double NUMmax_e (constMATVU const& mat); // throw if empty or undefined element |
| 314 | extern double NUMmax_u (constMATVU const& mat) noexcept; // undef if empty or undefined element |
| 315 | extern double NUMmax_removeUndefined_e (constMATVU const& mat); // throw if no defined elements |
| 316 | extern double NUMmax_removeUndefined_u (constMATVU const& mat) noexcept; // undef if no defined elements |
| 317 | |
| 318 | extern double NUMminimumLength (constSTRVEC const& x) noexcept; |
| 319 | extern double NUMmaximumLength (constSTRVEC const& x) noexcept; |
| 320 | |
| 321 | extern double NUMmean (constVECVU const& vec) noexcept; |
| 322 | extern double NUMmean (constMATVU const& mat) noexcept; |
| 323 | |
| 324 | extern MelderGaussianStats NUMmeanStdev (constVECVU const& vec) noexcept; |
| 325 | extern MelderGaussianStats NUMmeanStdev (constMATVU const& mat) noexcept; |
| 326 | |
| 327 | double NUMnorm (constVECVU const& x, double power) noexcept; |
| 328 | double NUMnorm (constMATVU const& x, double power) noexcept; |
| 329 | |
| 330 | integer NUMnumberOfTokens (conststring32 str) noexcept; |
| 331 | |
| 332 | /* |
| 333 | Return zero for non-positive base. |
| 334 | */ |
| 335 | inline double NUMpow (double base, double exponent) { |
| 336 | return base <= 0.0 ? 0.0 : pow (base, exponent); |
| 337 | } |
| 338 | |
| 339 | inline double NUMsqrt_u (const double x) { |
| 340 | #if defined (_WIN32) |
| 341 | if (! (x >= 0.0)) // NaN-safe |
| 342 | return undefined; |
| 343 | #endif |
| 344 | return sqrt (x); |
| 345 | } |
| 346 | |
| 347 | inline double NUMsqrt_0 (const double x) { |
| 348 | if (! (x >= 0.0)) // NaN-safe |
| 349 | return 0.0; |
| 350 | return sqrt (x); |
| 351 | } |
| 352 | |
| 353 | inline double NUMsqrt_e (const double x) { |
| 354 | Melder_require (x >= 0.0, |
| 355 | U"You cannot take the square root of a negative number (", x, U")."); |
| 356 | return sqrt (x); |
| 357 | } |
| 358 | |
| 359 | inline double NUMsqrt_a (const double x) { |
| 360 | Melder_assert (x >= 0.0); |
| 361 | return sqrt (x); |
| 362 | } |
| 363 | |
| 364 | extern double NUMstdev (constVECVU const& vec) noexcept; |
| 365 | extern double NUMstdev (constMATVU const& mat) noexcept; |
| 366 | |
| 367 | extern double NUMsum (constVECVU const& vec) noexcept; |
| 368 | extern double NUMsum (constMATVU const& mat) noexcept; |
| 369 | |
| 370 | extern double NUMsum2 (constVECVU const& vec); |
| 371 | extern double NUMsum2 (constMATVU const& mat); |
| 372 | |
| 373 | extern double NUMsumOfSquaredDifferences (constVECVU const& vec, double mean); |
| 374 | extern double NUMcorrelation (constVECVU const& vec1, constVECVU const& vec2); |
| 375 | |
| 376 | extern double NUMtotalLength (constSTRVEC const& x); |
| 377 | |
| 378 | extern double NUMvariance (constVECVU const& vec) noexcept; |
| 379 | extern double NUMvariance (constMATVU const& mat) noexcept; |
| 380 | |
| 381 | /* End of file NUM.h */ |