Debian Codegraph @

melder/VEC.cpp

raw symbols SCIP
1 /* VEC.cpp
2 *
3 * Copyright (C) 2017-2021,2023,2025,2026 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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 #include "../dwsys/NUM2.h" /* for NUMsort2 */
21 #include "../external/blake3/blake3.h"
22
23 #ifdef macintosh
24 #include "macport_on.h"
25 #include <Accelerate/Accelerate.h>
26 #include "macport_off.h"
27 #endif
28
29 #if defined (macintosh)
30 void _add_macfast_VEC_out (VECVU const& target, constVECVU const& x, constVECVU const& y) noexcept {
31 integer n = target.size;
32 vDSP_vaddD (& x [1], x.stride, & y [1], y.stride, & target [1], target.stride, integer_to_uinteger_a (n));
33 /*
34 Speed if always vDSP_vaddD:
35 //9.3,1.26,0.21, 0.10,0.42,0.70, 1.17,1.97,5.32
36 Speed if always explicit loop:
37 0.61,1.99,4.32, 4.39,2.61,1.51, 0.85,0.50,0.41
38 Combined:
39 6.1,1.19,0.23, 0.13,0.41,0.70, 1.19,2.02,2.45
40 */
41 }
42 #endif
43
44 autoVEC from_to_VEC (double from, double to) {
45 const integer numberOfElements = Melder_ifloor (to - from + 1.0);
46 if (numberOfElements < 1)
47 return autoVEC ();
48 autoVEC result = raw_VEC (numberOfElements);
49 for (integer i = 1; i <= numberOfElements; i ++)
50 result [i] = from + (double) (i - 1);
51 return result;
52 }
53 autoINTVEC from_to_INTVEC (integer from, integer to) {
54 const integer numberOfElements = to - from + 1;
55 if (numberOfElements < 1)
56 return autoINTVEC ();
57 autoINTVEC result = raw_INTVEC (numberOfElements);
58 for (integer i = 1; i <= numberOfElements; i ++)
59 result [i] = from + (i - 1);
60 return result;
61 }
62
63 autoVEC from_to_by_VEC (double from, double to, double by) {
64 Melder_require (by != 0.0,
65 U"from_to_by#: cannot have a step (“by”) of zero.");
66 /*
67 The following algorithm works for both positive and negative `by`.
68 */
69 const integer numberOfElements = Melder_ifloor ((to - from) / by + 1.0);
70 if (numberOfElements < 1)
71 return autoVEC ();
72 autoVEC result = raw_VEC (numberOfElements);
73 for (integer i = 1; i <= numberOfElements; i ++)
74 result [i] = from + (double) (i - 1) * by;
75 return result;
76 }
77 autoINTVEC from_to_by_INTVEC (integer from, integer to, integer by) {
78 Melder_require (by != 0,
79 U"from_to_by#: cannot have a step (“by”) of zero.");
80 /*
81 The following algorithm works for both positive and negative `by`.
82 */
83 const integer numberOfElements = (to - from) / by + 1;
84 if (numberOfElements < 1)
85 return autoINTVEC ();
86 autoINTVEC result = raw_INTVEC (numberOfElements);
87 for (integer i = 1; i <= numberOfElements; i ++)
88 result [i] = from + (i - 1) * by;
89 return result;
90 }
91 /*@praat
92 assert from_to_count# (0, 10, 5) = { 0, 2.5, 5, 7.5, 10 }
93 @*/
94 autoVEC from_to_count_VEC (double from, double to, integer count) {
95 Melder_require (count >= 2,
96 U"from_to_count#: cannot have fewer than two elements.");
97 autoVEC result = raw_VEC (count);
98 const double by = (to - from) / (count - 1);
99 for (integer i = 1; i < count; i ++)
100 result [i] = from + (double) (i - 1) * by;
101 result [count] = to;
102 return result;
103 }
104 autoINTVEC from_to_count_INTVEC (integer from, integer to, integer count) {
105 Melder_require (count >= 2,
106 U"from_to_count#: cannot have fewer than two elements.");
107 autoINTVEC result = raw_INTVEC (count);
108 const integer by = (to - from) / (count - 1);
109 for (integer i = 1; i < count; i ++)
110 result [i] = from + (i - 1) * by;
111 result [count] = to;
112 return result;
113 }
114
115 autoVEC between_by_VEC (double from, double to, double by) {
116 Melder_require (by != 0.0,
117 U"between_by#: cannot have a step (“by”) of zero.");
118 /*
119 The following algorithm works for both positive and negative `by`.
120 */
121 const integer numberOfElements = Melder_ifloor ((to - from) / by + 1.0);
122 if (numberOfElements < 1)
123 return autoVEC ();
124 const double spaceNeeded = (numberOfElements - 1) * by;
125 const double spaceOnEdges = (to - from) - spaceNeeded;
126 const double first = from + 0.5 * spaceOnEdges;
127 autoVEC result = raw_VEC (numberOfElements);
128 for (integer i = 1; i <= numberOfElements; i ++)
129 result [i] = first + (double) (i - 1) * by;
130 return result;
131 }
132
133 /*@praat
134 assert between_count# (0, 10, 5) = { 1, 3, 5, 7, 9 }
135 @*/
136 autoVEC between_count_VEC (double from, double to, integer count) {
137 Melder_require (count >= 0,
138 U"between_count#: cannot have fewer than zero elements.");
139 if (count < 1)
140 return autoVEC ();
141 const double by = (to - from) / count;
142 autoVEC result = raw_VEC (count);
143 for (integer i = 1; i <= count; i ++)
144 result [i] = from + (double) (i - 0.5) * by;
145 return result;
146 }
147
148 void mul_VEC_out (VECVU const& target, constVECVU const& vec, constMATVU const& mat) {
149 Melder_assert (mat.nrow == vec.size);
150 Melder_assert (target.size == mat.ncol);
151 if ((true)) {
152 for (integer icol = 1; icol <= mat.ncol; icol ++) {
153 target [icol] = 0.0;
154 for (integer irow = 1; irow <= mat.nrow; irow ++)
155 target [icol] += vec [irow] * mat [irow] [icol];
156 }
157 } else if ((false)) {
158 for (integer icol = 1; icol <= mat.ncol; icol ++) {
159 PAIRWISE_SUM (longdouble, sum, integer, vec.size,
160 const double *px = & vec [1];
161 const double *py = & mat [1] [icol],
162 (longdouble) *px * (longdouble) *py,
163 (px += vec.stride, py += mat.rowStride) // this goes way beyond the confines of mat
164 )
165 target [icol] = double (sum);
166 }
167 }
168 }
169
170 autoVEC mul_VEC (constVECVU const& vec, constMATVU const& mat) {
171 autoVEC result = raw_VEC (mat.ncol);
172 mul_VEC_out (result.get(), vec, mat);
173 return result;
174 }
175
176 void mul_VEC_out (VECVU const& target, constMATVU const& mat, constVECVU const& vec) {
177 Melder_assert (vec.size == mat.ncol);
178 Melder_assert (target.size == mat.nrow);
179 for (integer i = 1; i <= mat.nrow; i ++) {
180 if ((false)) {
181 target [i] = 0.0;
182 for (integer j = 1; j <= vec.size; j ++)
183 target [i] += mat [i] [j] * vec [j];
184 } else {
185 target [i] = NUMinner (mat.row (i), vec);
186 }
187 }
188 }
189
190 autoVEC mul_VEC (constMATVU const& mat, constVECVU const& vec) {
191 autoVEC result = raw_VEC (mat.nrow);
192 mul_VEC_out (result.all(), mat, vec);
193 return result;
194 }
195
196 void power_VEC_out (VECVU const& target, constVECVU const& vec, double power) {
197 if (power == 2.0) {
198 for (integer i = 1; i <= target.size; i ++)
199 target [i] = vec [i] * vec [i];
200 } else if (power < 0.0) {
201 if (power == -1.0) {
202 for (integer i = 1; i <= target.size; i ++) {
203 Melder_require (vec [i] != 0.0,
204 U"Cannot raise zero to a negative power.");
205 target [i] = 1.0 / vec [i];
206 }
207 } else if (power == -2.0) {
208 for (integer i = 1; i <= target.size; i ++) {
209 Melder_require (vec [i] != 0.0,
210 U"Cannot raise zero to a negative power.");
211 target [i] = 1.0 / (vec [i] * vec [i]);
212 }
213 } else {
214 for (integer i = 1; i <= target.size; i ++) {
215 Melder_require (vec [i] != 0.0,
216 U"Cannot raise zero to a negative power.");
217 target [i] = pow (vec [i], power);
218 }
219 }
220 } else {
221 for (integer i = 1; i <= target.size; i ++)
222 target [i] = pow (vec [i], power);
223 }
224 }
225 void power_VEC_inout (VECVU const& vec, double power) {
226 power_VEC_out (vec, vec, power);
227 }
228
229 autoVEC splitByWhitespace_VEC (conststring32 string) {
230 if (! string)
231 return autoVEC(); // accept null pointer input
232 const integer n = NUMnumberOfTokens (string);
233 if (n == 0)
234 return autoVEC();
235 autoVEC result (n, MelderArray::kInitializationType::ZERO);
236
237 integer itoken = 0;
238 const char32 *p = & string [0];
239 for (;;) {
240 Melder_skipHorizontalOrVerticalSpace (& p);
241 if (*p == U'\0')
242 break;
243 const char32 *beginOfInk = p;
244 p ++; // step over first nonspace
245 p = Melder_findEndOfInk (p);
246 result [++ itoken] = Melder_atof (beginOfInk);
247 }
248 return result;
249 }
250
251 /*
252 Acceptable ranges e.g. "1 4 2 3:7 4:3 3:5:2" -->
253 1, 4, 2, 3, 4, 5, 6, 7, 4, 3, 3, 4, 5, 4, 3, 2
254 Overlap is allowed. Ranges can go up and down.
255 */
256 autoINTVEC splitByWhitespaceWithRanges_INTVEC (conststring32 ranges) {
257 autoINTVEC elements = raw_INTVEC (0);
258 integer previousElement = 0;
259 const char32 *p = & ranges [0];
260 for (;;) {
261 while (Melder_isHorizontalSpace (*p))
262 p ++;
263 if (*p == U'\0')
264 break;
265 if (Melder_isAsciiDecimalNumber (*p) || *p == '-' && Melder_isAsciiDecimalNumber (p [1])) {
266 const integer currentElement = Melder_atoi (p);
267 * elements.append() = currentElement;
268 previousElement = currentElement;
269 do {
270 p ++;
271 } while (Melder_isAsciiDecimalNumber (*p));
272 } else if (*p == U':') {
273 Melder_require (previousElement != 0,
274 U"The range should not start with a colon.");
275 do {
276 p ++;
277 } while (Melder_isHorizontalSpace (*p));
278 Melder_require (*p != U'\0',
279 U"The range should not end with a colon.");
280 Melder_require (Melder_isAsciiDecimalNumber (*p) || *p == '-' && Melder_isAsciiDecimalNumber (p [1]),
281 U"End of range should be a whole number.");
282 const integer currentElement = Melder_atoi (p);
283 if (currentElement > previousElement)
284 for (integer ielement = previousElement + 1; ielement <= currentElement; ielement ++)
285 * elements.append() = ielement;
286 else
287 for (integer ielement = previousElement - 1; ielement >= currentElement; ielement --)
288 * elements.append() = ielement;
289 previousElement = currentElement;
290 do {
291 p ++;
292 } while (Melder_isAsciiDecimalNumber (*p));
293 } else {
294 Melder_throw (U"Start of range should be a whole number.");
295 }
296 }
297 return elements;
298 }
299
300 autoVEC cast_VEC (constINTVEC const& intvec) {
301 autoVEC result = raw_VEC (intvec.size);
302 for (integer i = 1; i <= intvec.size; i ++)
303 result [i] = intvec [i];
304 return result;
305 }
306
307 autoINTVEC ifloor_INTVEC (constVEC const& vec) {
308 autoINTVEC result = raw_INTVEC (vec.size);
309 for (integer i = 1; i <= vec.size; i ++)
310 result [i] = Melder_ifloor (vec [i]);
311 return result;
312 }
313
314 autoINTVEC iround_INTVEC (constVEC const& vec) {
315 autoINTVEC result = raw_INTVEC (vec.size);
316 for (integer i = 1; i <= vec.size; i ++)
317 result [i] = Melder_iround (vec [i]);
318 return result;
319 }
320
321 autoINTVEC iceiling_INTVEC (constVEC const& vec) {
322 autoINTVEC result = raw_INTVEC (vec.size);
323 for (integer i = 1; i <= vec.size; i ++)
324 result [i] = Melder_iceiling (vec [i]);
325 return result;
326 }
327
328 void to_INTVEC_out (INTVECVU const& x) noexcept {
329 for (integer i = 1; i <= x.size; i ++)
330 x [i] = i;
331 }
332
333 autoVEC to_VEC (double to) {
334 const integer numberOfElements = Melder_ifloor (to);
335 autoVEC result = raw_VEC (numberOfElements);
336 for (integer i = 1; i <= numberOfElements; i ++)
337 result [i] = (double) i;
338 return result;
339 }
340 autoINTVEC to_INTVEC (integer to) {
341 const integer numberOfElements = to;
342 autoINTVEC result = raw_INTVEC (numberOfElements);
343 for (integer i = 1; i <= numberOfElements; i ++)
344 result [i] = i;
345 return result;
346 }
347
348 extern autoVEC blake3_VEC (constMATVU const& mat) {
349 blake3_hasher hasher;
350 blake3_hasher_init (& hasher);
351 for (integer irow = 1; irow <= mat.nrow; irow ++)
352 for (integer icol = 1; icol <= mat.ncol; icol ++) {
353 const double value_float = mat [irow] [icol];
354 const uint64 value_int = * (uint64 *) & value_float;
355 uint8 bytes [8];
356 bytes [0] = (uint8) (value_int >> 0); // little-endian
357 bytes [1] = (uint8) (value_int >> 8); // little-endian
358 bytes [2] = (uint8) (value_int >> 16); // little-endian
359 bytes [3] = (uint8) (value_int >> 24); // little-endian
360 bytes [4] = (uint8) (value_int >> 32); // little-endian
361 bytes [5] = (uint8) (value_int >> 40); // little-endian
362 bytes [6] = (uint8) (value_int >> 48); // little-endian
363 bytes [7] = (uint8) (value_int >> 56); // little-endian
364 blake3_hasher_update (& hasher, bytes, 8);
365 }
366 uint8 output [32];
367 blake3_hasher_finalize (& hasher, output, 32); // 256 bits
368 autoVEC result = raw_VEC (8);
369 result [1] = ((uint32) output [0] << 0) | ((uint32) output [1] << 8)
370 | ((uint32) output [2] << 16) | ((uint32) output [3] << 24);
371 result [2] = ((uint32) output [4] << 0) | ((uint32) output [5] << 8)
372 | ((uint32) output [6] << 16) | ((uint32) output [7] << 24);
373 result [3] = ((uint32) output [8] << 0) | ((uint32) output [9] << 8)
374 | ((uint32) output [10] << 16) | ((uint32) output [11] << 24);
375 result [4] = ((uint32) output [12] << 0) | ((uint32) output [13] << 8)
376 | ((uint32) output [14] << 16) | ((uint32) output [15] << 24);
377 result [5] = ((uint32) output [16] << 0) | ((uint32) output [17] << 8)
378 | ((uint32) output [18] << 16) | ((uint32) output [19] << 24);
379 result [6] = ((uint32) output [20] << 0) | ((uint32) output [21] << 8)
380 | ((uint32) output [22] << 16) | ((uint32) output [23] << 24);
381 result [7] = ((uint32) output [24] << 0) | ((uint32) output [25] << 8)
382 | ((uint32) output [26] << 16) | ((uint32) output [27] << 24);
383 result [8] = ((uint32) output [28] << 0) | ((uint32) output [29] << 8)
384 | ((uint32) output [30] << 16) | ((uint32) output [31] << 24);
385 return result;
386 }
387
388 /* End of file VEC.cpp */