| 1 | #pragma once |
| 2 | /* VEC.h |
| 3 | * |
| 4 | * Copyright (C) 2017-2021,2023-2026 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 3 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 | #define GENERATE_FIVE_TENSOR_FUNCTIONS \ |
| 21 | GENERATE_ONE_TENSOR_FUNCTION (operator<<=, =) \ |
| 22 | GENERATE_ONE_TENSOR_FUNCTION (operator+=, +=) \ |
| 23 | GENERATE_ONE_TENSOR_FUNCTION (operator-=, -=) \ |
| 24 | GENERATE_ONE_TENSOR_FUNCTION (operator*=, *=) \ |
| 25 | GENERATE_ONE_TENSOR_FUNCTION (operator/=, /=) |
| 26 | |
| 27 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 28 | inline void operator (VECVU const& target, double number) noexcept { \ |
| 29 | for (integer i = 1; i <= target.size; i ++) \ |
| 30 | target [i] op number; \ |
| 31 | } |
| 32 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 33 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 34 | |
| 35 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 36 | inline void operator (VECVU const& target, constVECVU const& x) { \ |
| 37 | Melder_assert (target.size == x.size); \ |
| 38 | for (integer i = 1; i <= target.size; i ++) \ |
| 39 | target [i] op x [i]; \ |
| 40 | } |
| 41 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 42 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 43 | |
| 44 | struct TypeVECadd_VEC_NUM { constVECVU const& x; double number; }; |
| 45 | inline TypeVECadd_VEC_NUM operator+ (constVECVU const& x, double number) { return { x, number }; } |
| 46 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 47 | inline void operator (VECVU const& target, TypeVECadd_VEC_NUM const& expr) { \ |
| 48 | Melder_assert (expr.x.size == target.size); \ |
| 49 | for (integer i = 1; i <= expr.x.size; i ++) \ |
| 50 | target [i] op expr.x [i] + expr.number; \ |
| 51 | } |
| 52 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 53 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 54 | inline autoVEC add_VEC (constVECVU const& x, double number) { |
| 55 | autoVEC result = raw_VEC (x.size); |
| 56 | result.all() <<= x + number; |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | struct TypeVECsubtract_VEC_NUM { constVECVU const& x; double number; }; |
| 61 | inline TypeVECsubtract_VEC_NUM operator- (constVECVU const& x, double number) { return { x, number }; } |
| 62 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 63 | inline void operator (VECVU const& target, TypeVECsubtract_VEC_NUM const& expr) { \ |
| 64 | Melder_assert (expr.x.size == target.size); \ |
| 65 | for (integer i = 1; i <= expr.x.size; i ++) \ |
| 66 | target [i] op expr.x [i] - expr.number; \ |
| 67 | } |
| 68 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 69 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 70 | inline autoVEC subtract_VEC (constVECVU const& x, double number) { |
| 71 | autoVEC result = raw_VEC (x.size); |
| 72 | result.all() <<= x - number; |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | struct TypeVECmultiply_VEC_NUM { constVECVU const& x; double number; }; |
| 77 | inline TypeVECmultiply_VEC_NUM operator* (constVECVU const& x, double number) { return { x, number }; } |
| 78 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 79 | inline void operator (VECVU const& target, TypeVECmultiply_VEC_NUM const& expr) { \ |
| 80 | Melder_assert (expr.x.size == target.size); \ |
| 81 | for (integer i = 1; i <= expr.x.size; i ++) \ |
| 82 | target [i] op expr.x [i] * expr.number; \ |
| 83 | } |
| 84 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 85 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 86 | inline autoVEC multiply_VEC (constVECVU const& x, double factor) { |
| 87 | autoVEC result = raw_VEC (x.size); |
| 88 | result.all() <<= x * factor; |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | struct TypeVECadd_NUM_VEC { double number; constVECVU const& x; }; |
| 93 | inline TypeVECadd_NUM_VEC operator+ (double number, constVECVU const& x) { return { number, x }; } |
| 94 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 95 | inline void operator (VECVU const& target, TypeVECadd_NUM_VEC const& expr) { \ |
| 96 | Melder_assert (expr.x.size == target.size); \ |
| 97 | for (integer i = 1; i <= expr.x.size; i ++) \ |
| 98 | target [i] op expr.number + expr.x [i]; \ |
| 99 | } |
| 100 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 101 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 102 | inline autoVEC add_VEC (double number, constVECVU const& x) { |
| 103 | autoVEC result = raw_VEC (x.size); |
| 104 | result.all() <<= number + x; |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | struct TypeVECsubtract_NUM_VEC { double number; constVECVU const& x; }; |
| 109 | inline TypeVECsubtract_NUM_VEC operator- (double number, constVECVU const& x) { return { number, x }; } |
| 110 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 111 | inline void operator (VECVU const& target, TypeVECsubtract_NUM_VEC const& expr) { \ |
| 112 | Melder_assert (expr.x.size == target.size); \ |
| 113 | for (integer i = 1; i <= expr.x.size; i ++) \ |
| 114 | target [i] op expr.number - expr.x [i]; \ |
| 115 | } |
| 116 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 117 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 118 | inline autoVEC subtract_VEC (double number, constVECVU const& x) { |
| 119 | autoVEC result = raw_VEC (x.size); |
| 120 | result.all() <<= number - x; |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | struct TypeVECmultiply_NUM_VEC { double number; constVECVU const& x; }; |
| 125 | inline TypeVECmultiply_NUM_VEC operator* (double number, constVECVU const& x) { return { number, x }; } |
| 126 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 127 | inline void operator (VECVU const& target, TypeVECmultiply_NUM_VEC const& expr) { \ |
| 128 | Melder_assert (expr.x.size == target.size); \ |
| 129 | for (integer i = 1; i <= expr.x.size; i ++) \ |
| 130 | target [i] op expr.number * expr.x [i]; \ |
| 131 | } |
| 132 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 133 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 134 | inline autoVEC multiply_VEC (double number, constVECVU const& x) { |
| 135 | autoVEC result = raw_VEC (x.size); |
| 136 | result.all() <<= number * x; |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | struct TypeVECadd_VEC_VEC { constVECVU const& x; constVECVU const& y; }; |
| 141 | inline TypeVECadd_VEC_VEC operator+ (constVECVU const& x, constVECVU const& y) { return { x, y }; } |
| 142 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 143 | inline void operator (VECVU const& target, TypeVECadd_VEC_VEC expr) { \ |
| 144 | integer n = target.size; \ |
| 145 | Melder_assert (expr.x.size == n); \ |
| 146 | Melder_assert (expr.y.size == n); \ |
| 147 | for (integer i = 1; i <= n; i ++) \ |
| 148 | target [i] op expr.x [i] + expr.y [i]; \ |
| 149 | } |
| 150 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 151 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 152 | inline autoVEC add_VEC (constVECVU const& x, constVECVU const& y) { |
| 153 | autoVEC result = raw_VEC (x.size); |
| 154 | result.all() <<= x + y; |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | struct TypeVECsubtract_VEC_VEC { constVECVU const& x; constVECVU const& y; }; |
| 159 | inline TypeVECsubtract_VEC_VEC operator- (constVECVU const& x, constVECVU const& y) { return { x, y }; } |
| 160 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 161 | inline void operator (VECVU const& target, TypeVECsubtract_VEC_VEC expr) { \ |
| 162 | integer n = target.size; \ |
| 163 | Melder_assert (expr.x.size == n); \ |
| 164 | Melder_assert (expr.y.size == n); \ |
| 165 | for (integer i = 1; i <= n; i ++) \ |
| 166 | target [i] op expr.x [i] - expr.y [i]; \ |
| 167 | } |
| 168 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 169 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 170 | inline autoVEC subtract_VEC (constVECVU const& x, constVECVU const& y) { |
| 171 | autoVEC result = raw_VEC (x.size); |
| 172 | result.all() <<= x - y; |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | struct TypeVECmultiply_VEC_VEC { constVECVU const& x; constVECVU const& y; }; |
| 177 | inline TypeVECmultiply_VEC_VEC operator* (constVECVU const& x, constVECVU const& y) { return { x, y }; } |
| 178 | #define GENERATE_ONE_TENSOR_FUNCTION(operator, op) \ |
| 179 | inline void operator (VECVU const& target, TypeVECmultiply_VEC_VEC expr) { \ |
| 180 | const integer n = target.size; \ |
| 181 | Melder_assert (expr.x.size == n); \ |
| 182 | Melder_assert (expr.y.size == n); \ |
| 183 | for (integer i = 1; i <= n; i ++) \ |
| 184 | target [i] op expr.x [i] * expr.y [i]; \ |
| 185 | } |
| 186 | GENERATE_FIVE_TENSOR_FUNCTIONS |
| 187 | #undef GENERATE_ONE_TENSOR_FUNCTION |
| 188 | inline autoVEC multiply_VEC (constVECVU const& x, constVECVU const& y) { |
| 189 | autoVEC result = raw_VEC (x.size); |
| 190 | result.all() <<= x * y; |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | extern void _add_macfast_VEC_out (VECVU const& target, constVECVU const& x, constVECVU const& y) noexcept; |
| 195 | inline void add_VEC_out (VECVU const& target, constVECVU const& x, constVECVU const& y) { |
| 196 | const integer n = target.size; |
| 197 | Melder_assert (x.size == n); |
| 198 | Melder_assert (y.size == n); |
| 199 | #if defined (macintoshXXX) |
| 200 | if (n >= 64) |
| 201 | return _add_macfast_VEC_out (target, x, y); |
| 202 | #endif |
| 203 | for (integer i = 1; i <= n; i ++) |
| 204 | target [i] = x [i] + y [i]; |
| 205 | } |
| 206 | |
| 207 | inline void abs_VEC_out (VECVU const& target, constVECVU const& v) { |
| 208 | Melder_assert (target.size == v.size); |
| 209 | for (integer i = 1; i <= target.size; i ++) |
| 210 | target [i] = fabs (v [i]); |
| 211 | } |
| 212 | inline void abs_VEC_inout (VECVU const& vec) noexcept { |
| 213 | for (integer i = 1; i <= vec.size; i ++) |
| 214 | vec [i] = fabs (vec [i]); |
| 215 | } |
| 216 | inline autoVEC abs_VEC (constVECVU const& vec) { |
| 217 | autoVEC result = raw_VEC (vec.size); |
| 218 | abs_VEC_out (result.all(), vec); |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | inline void centre_VEC_inout (VECVU const& x, double *out_mean = nullptr) noexcept { |
| 223 | const double xmean = NUMmean (x); |
| 224 | for (integer i = 1; i <= x.size; i ++) |
| 225 | x [i] -= xmean; |
| 226 | if (out_mean) |
| 227 | *out_mean = xmean; |
| 228 | } |
| 229 | |
| 230 | inline autoVEC column_VEC (constMATVU const& source, integer columnNumber) { |
| 231 | autoVEC target = raw_VEC (source.nrow); |
| 232 | target.all() <<= source.column (columnNumber); |
| 233 | return target; |
| 234 | } |
| 235 | |
| 236 | inline void columnMeans_VEC_out (VECVU const& target, constMATVU const& x) { |
| 237 | Melder_assert (target.size == x.ncol); |
| 238 | for (integer icol = 1; icol <= x.ncol; icol ++) |
| 239 | target [icol] = NUMmean (x.column (icol)); |
| 240 | } |
| 241 | inline autoVEC columnMeans_VEC (constMATVU const& x) { |
| 242 | autoVEC result = raw_VEC (x.ncol); |
| 243 | columnMeans_VEC_out (result.get(), x); |
| 244 | return result; |
| 245 | } |
| 246 | |
| 247 | extern void mul_VEC_out (VECVU const& target, constVECVU const& vec, constMATVU const& mat); |
| 248 | extern void mul_VEC_out (VECVU const& target, constMATVU const& mat, constVECVU const& vec); |
| 249 | extern autoVEC mul_VEC (constVECVU const& vec, constMATVU const& mat); |
| 250 | extern autoVEC mul_VEC (constMATVU const& mat, constVECVU const& vec); |
| 251 | |
| 252 | inline void neg_VEC_out (VECVU const& target, constVECVU const& v) { |
| 253 | Melder_assert (target.size == v.size); |
| 254 | for (integer i = 1; i <= target.size; i ++) |
| 255 | target [i] = - v [i]; |
| 256 | } |
| 257 | inline void neg_VEC_inout (VECVU const& vec) noexcept { |
| 258 | for (integer i = 1; i <= vec.size; i ++) |
| 259 | vec [i] = - vec [i]; |
| 260 | } |
| 261 | inline autoVEC neg_VEC (constVECVU const& vec) { |
| 262 | autoVEC result = raw_VEC (vec.size); |
| 263 | neg_VEC_out (result.all(), vec); |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | extern void power_VEC_out (VECVU const& target, constVECVU const& vec, double power); |
| 268 | extern void power_VEC_inout (VECVU const& vec, double power); |
| 269 | inline autoVEC power_VEC (constVECVU const& vec, double power) { |
| 270 | autoVEC result = raw_VEC (vec.size); |
| 271 | power_VEC_out (result.all(), vec, power); |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | inline void randomGauss_VEC_out (VECVU const& target, double mu, double sigma) { |
| 276 | for (integer i = 1; i <= target.size; i ++) |
| 277 | target [i] = NUMrandomGauss (mu, sigma); |
| 278 | } |
| 279 | inline autoVEC randomGauss_VEC (integer size, double mu, double sigma) { |
| 280 | autoVEC result = raw_VEC (size); |
| 281 | randomGauss_VEC_out (result.all(), mu, sigma); |
| 282 | return result; |
| 283 | } |
| 284 | inline autoVEC randomGauss_VEC (constVECVU const& model, double mu, double sigma) { |
| 285 | autoVEC result = raw_VEC (model.size); |
| 286 | randomGauss_VEC_out (result.all(), mu, sigma); |
| 287 | return result; |
| 288 | } |
| 289 | |
| 290 | inline void randomUniform_VEC_out (VECVU const& target, double lowest, double highest) { |
| 291 | for (integer i = 1; i <= target.size; i ++) |
| 292 | target [i] = NUMrandomUniform (lowest, highest); |
| 293 | } |
| 294 | inline autoVEC randomUniform_VEC (integer size, double lowest, double highest) { |
| 295 | autoVEC result = raw_VEC (size); |
| 296 | randomUniform_VEC_out (result.all(), lowest, highest); |
| 297 | return result; |
| 298 | } |
| 299 | inline autoVEC randomUniform_VEC (constVECVU const& model, double lowest, double highest) { |
| 300 | autoVEC result = raw_VEC (model.size); |
| 301 | randomUniform_VEC_out (result.all(), lowest, highest); |
| 302 | return result; |
| 303 | } |
| 304 | |
| 305 | inline void rowInners_VEC_out (VECVU const& target, constMATVU const& x, constMATVU const& y) { |
| 306 | Melder_assert (y.nrow == x.nrow); |
| 307 | Melder_assert (y.ncol == x.ncol); |
| 308 | Melder_assert (target.size == x.nrow); |
| 309 | for (integer irow = 1; irow <= target.size; irow ++) |
| 310 | target [irow] = NUMinner (x.row (irow), y.row (irow)); |
| 311 | } |
| 312 | inline autoVEC rowInners_VEC (constMATVU const& x, constMATVU const& y) { |
| 313 | autoVEC result = raw_VEC (x.nrow); |
| 314 | rowInners_VEC_out (result.all(), x, y); |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | inline void sin_VEC_inout (VECVU const& vec) noexcept { |
| 319 | for (integer i = 1; i <= vec.size; i ++) |
| 320 | vec [i] = sin (vec [i]); |
| 321 | } |
| 322 | |
| 323 | extern void shuffle_VEC_inout (VECVU const& x); |
| 324 | extern void shuffle_INTVEC_inout (INTVECVU const& x); |
| 325 | |
| 326 | inline autoVEC shuffle_VEC (constVECVU const& x) { |
| 327 | autoVEC result = copy_VEC (x); |
| 328 | shuffle_VEC_inout (result.get()); |
| 329 | return result; |
| 330 | } |
| 331 | |
| 332 | inline autoINTVEC shuffle_INTVEC (constINTVECVU const& x) { |
| 333 | autoINTVEC result = copy_INTVEC (x); |
| 334 | shuffle_INTVEC_inout (result.get()); |
| 335 | return result; |
| 336 | } |
| 337 | |
| 338 | extern void sort_e_VEC_inout (VEC const& x); // cannot be a VECVU |
| 339 | extern void sort_VEC_inout (VEC const& x); // cannot be a VECVU |
| 340 | extern void sort_INTVEC_inout (INTVEC const& x) noexcept; // cannot be an INTVECVU |
| 341 | |
| 342 | |
| 343 | inline autoVEC sort_e_VEC (constVECVU const& x) { |
| 344 | autoVEC result = copy_VEC (x); |
| 345 | sort_e_VEC_inout (result.get()); |
| 346 | return result; |
| 347 | } |
| 348 | |
| 349 | inline autoINTVEC sort_INTVEC (constINTVECVU const& x) { |
| 350 | autoINTVEC result = copy_INTVEC (x); |
| 351 | sort_INTVEC_inout (result.get()); |
| 352 | return result; |
| 353 | } |
| 354 | |
| 355 | autoVEC sort_removeUndefined_VEC (constVECVU const& vec); |
| 356 | |
| 357 | inline autoINTVEC sortedSet_INTVEC (constINTVECVU const& x) { |
| 358 | if (x.size <= 0) |
| 359 | return autoINTVEC (); |
| 360 | autoINTVEC result = sort_INTVEC (x); |
| 361 | integer newSize = 1; |
| 362 | for (integer i = 2; i <= x.size; i ++) |
| 363 | if (result [i] != result [i - 1]) |
| 364 | result [++ newSize] = result [i]; |
| 365 | result. resize (newSize); |
| 366 | return result; |
| 367 | } |
| 368 | |
| 369 | inline autoVEC rowSums_VEC (constMATVU const& x) { |
| 370 | autoVEC result = raw_VEC (x.nrow); |
| 371 | for (integer irow = 1; irow <= x.nrow; irow ++) |
| 372 | result [irow] = NUMsum (x.row (irow)); |
| 373 | return result; |
| 374 | } |
| 375 | |
| 376 | inline autoVEC columnSums_VEC (constMATVU const& x) { |
| 377 | autoVEC result = raw_VEC (x.ncol); |
| 378 | for (integer icol = 1; icol <= x.ncol; icol ++) |
| 379 | result [icol] = NUMsum (x.column (icol)); |
| 380 | return result; |
| 381 | } |
| 382 | |
| 383 | extern autoVEC to_VEC (double to); |
| 384 | extern autoVEC from_to_VEC (double from, double to); |
| 385 | extern autoVEC from_to_by_VEC (double from, double to, double by); |
| 386 | extern autoVEC from_to_count_VEC (double from, double to, integer count); |
| 387 | extern autoVEC between_by_VEC (double from, double to, double by); |
| 388 | extern autoVEC between_count_VEC (double from, double to, integer count); |
| 389 | |
| 390 | autoVEC splitByWhitespace_VEC (conststring32 string); |
| 391 | |
| 392 | autoINTVEC splitByWhitespaceWithRanges_INTVEC (conststring32 ranges); |
| 393 | |
| 394 | autoVEC cast_VEC (constINTVEC const& intvec); |
| 395 | autoINTVEC ifloor_INTVEC (constVEC const& vec); |
| 396 | autoINTVEC iround_INTVEC (constVEC const& vec); |
| 397 | autoINTVEC iceiling_INTVEC (constVEC const& vec); |
| 398 | |
| 399 | extern void to_INTVEC_out (INTVECVU const& x) noexcept; |
| 400 | extern autoINTVEC to_INTVEC (integer to); |
| 401 | extern autoINTVEC from_to_INTVEC (integer from, integer to); |
| 402 | extern autoINTVEC from_to_by_INTVEC (integer from, integer to, integer by); |
| 403 | extern autoINTVEC from_to_count_INTVEC (integer from, integer to, integer count); |
| 404 | |
| 405 | extern autoVEC blake3_VEC (constMATVU const& x); |
| 406 | |
| 407 | /* End of file VEC.h */ |