Debian Codegraph @

easel/esl_vectorops.c

raw symbols SCIP
1 /* Operations on vectors of floats or doubles.
2 *
3 * Can operate on vectors of doubles, floats, or integers - appropriate
4 * routine is prefixed with D, F, or I. For example, esl_vec_DSet() is
5 * the Set routine for a vector of doubles; esl_vec_ISet() is for integers.
6 *
7 * Contents:
8 * 1. The vectorops API.
9 * 2. Unit tests.
10 * 3. Test driver.
11 * 4. Examples.
12 */
13 #include <esl_config.h>
14
15 #include <math.h>
16 #include <float.h>
17 #include <stdint.h>
18
19 #include "easel.h"
20 #include "esl_random.h"
21 #include "esl_rand64.h"
22
23 #include "esl_vectorops.h"
24
25 /* Function: esl_vec_{DFIL}Set()
26 * Synopsis: Set all items in vector to scalar value.
27 *
28 * Purpose: Sets all <n> items in <vec> to <value>.
29 */
30 void
31 esl_vec_DSet(double *vec, int64_t n, double value)
32 {
33 int64_t i;
34 for (i = 0; i < n; i++) vec[i] = value;
35 }
36 void
37 esl_vec_FSet(float *vec, int64_t n, float value)
38 {
39 int64_t i;
40 for (i = 0; i < n; i++) vec[i] = value;
41 }
42 void
43 esl_vec_ISet(int *vec, int64_t n, int value)
44 {
45 int64_t i;
46 for (i = 0; i < n; i++) vec[i] = value;
47 }
48 void
49 esl_vec_LSet(int64_t *vec, int64_t n, int64_t value)
50 {
51 int64_t i;
52 for (i = 0; i < n; i++) vec[i] = value;
53 }
54
55
56 /* Function: esl_vec_{DFIL}Scale()
57 * Synopsis: Multiply all items in vector by scalar value.
58 *
59 * Purpose: Multiplies all <n> items in <vec> by <scale>.
60 */
61 void
62 esl_vec_DScale(double *vec, int64_t n, double scale)
63 {
64 int64_t i;
65 for (i = 0; i < n; i++) vec[i] *= scale;
66 }
67 void
68 esl_vec_FScale(float *vec, int64_t n, float scale)
69 {
70 int64_t i;
71 for (i = 0; i < n; i++) vec[i] *= scale;
72 }
73 void
74 esl_vec_IScale(int *vec, int64_t n, int scale)
75 {
76 int64_t i;
77 for (i = 0; i < n; i++) vec[i] *= scale;
78 }
79 void
80 esl_vec_LScale(int64_t *vec, int64_t n, int64_t scale)
81 {
82 int64_t i;
83 for (i = 0; i < n; i++) vec[i] *= scale;
84 }
85
86
87 /* Function: esl_vec_{DFIL}Increment()
88 * Synopsis: Add a scalar to all items in a vector.
89 * Incept: SRE, Mon Mar 21 11:56:57 2005 [St. Louis]
90 *
91 * Purpose: Adds scalar <x> to all items in the <n>-vector <v>.
92 */
93 void
94 esl_vec_DIncrement(double *v, int64_t n, double x)
95 {
96 int64_t i;
97 for (i = 0; i < n; i++) v[i] += x;
98 }
99 void
100 esl_vec_FIncrement(float *v, int64_t n, float x)
101 {
102 int64_t i;
103 for (i = 0; i < n; i++) v[i] += x;
104 }
105 void
106 esl_vec_IIncrement(int *v, int64_t n, int x)
107 {
108 int64_t i;
109 for (i = 0; i < n; i++) v[i] += x;
110 }
111 void
112 esl_vec_LIncrement(int64_t *v, int64_t n, int64_t x)
113 {
114 int64_t i;
115 for (i = 0; i < n; i++) v[i] += x;
116 }
117
118
119 /* Function: esl_vec_{DFIL}Add()
120 * Synopsis: Vector addition of two vectors.
121 *
122 * Purpose: Vector addition. Adds <vec2> to <vec1>, leaving
123 * result in <vec1>. (<vec2> is unchanged.).
124 * Both vectors are of size <n>.
125 */
126 void
127 esl_vec_DAdd(double *vec1, const double *vec2, int64_t n)
128 {
129 int64_t i;
130 for (i = 0; i < n; i++) vec1[i] += vec2[i];
131 }
132 void
133 esl_vec_FAdd(float *vec1, const float *vec2, int64_t n)
134 {
135 int64_t i;
136 for (i = 0; i < n; i++) vec1[i] += vec2[i];
137 }
138 void
139 esl_vec_IAdd(int *vec1, const int *vec2, int64_t n)
140 {
141 int64_t i;
142 for (i = 0; i < n; i++) vec1[i] += vec2[i];
143 }
144 void
145 esl_vec_LAdd(int64_t *vec1, const int64_t *vec2, int64_t n)
146 {
147 int64_t i;
148 for (i = 0; i < n; i++) vec1[i] += vec2[i];
149 }
150
151
152 /* Function: esl_vec_{DFIL}AddScaled()
153 * Synopsis: Scale <vec2> and add it to <vec1>.
154 *
155 * Purpose: Scales <vec2> by scalar <a>, and adds that
156 * to <vec1>. Both vectors are of size <n>.
157 */
158 void
159 esl_vec_DAddScaled(double *vec1, const double *vec2, double a, int64_t n)
160 {
161 int64_t i;
162 for (i = 0; i < n; i++) vec1[i] += vec2[i] * a;
163 }
164 void
165 esl_vec_FAddScaled(float *vec1, const float *vec2, float a, int64_t n)
166 {
167 int64_t i;
168 for (i = 0; i < n; i++) vec1[i] += vec2[i] * a;
169 }
170 void
171 esl_vec_IAddScaled(int *vec1, const int *vec2, int a, int64_t n)
172 {
173 int64_t i;
174 for (i = 0; i < n; i++) vec1[i] += vec2[i] * a;
175 }
176 void
177 esl_vec_LAddScaled(int64_t *vec1, const int64_t *vec2, int64_t a, int64_t n)
178 {
179 int64_t i;
180 for (i = 0; i < n; i++) vec1[i] += vec2[i] * a;
181 }
182
183
184
185 /* Function: esl_vec_{DFIL}Sum()
186 * Synopsis: Returns $\sum_i x_i$.
187 *
188 * Purpose: Returns the scalar sum of the <n> items in <vec>.
189 *
190 * Floating point summations use Kahan compensated
191 * summation, in order to minimize roundoff error
192 * accumulation. Additionally, they are most
193 * accurate if vec[] is sorted in increasing order, from
194 * small to large, so you may consider sorting <vec> before
195 * summing it.
196 */
197 double
198 esl_vec_DSum(const double *vec, int64_t n)
199 {
200 double sum = 0.;
201 double y,t,c;
202 int64_t i;
203
204 c = 0.0;
205 for (i = 0; i < n; i++) {
206 y = vec[i] - c; t = sum + y; c = (t-sum)-y; sum = t;
207 }
208 return sum;
209 }
210 float
211 esl_vec_FSum(const float *vec, int64_t n)
212 {
213 float sum = 0.;
214 float y,t,c;
215 int64_t i;
216
217 c = 0.0;
218 for (i = 0; i < n; i++) {
219 y = vec[i] - c; t = sum + y; c = (t-sum)-y; sum = t;
220 }
221 return sum;
222 }
223 int
224 esl_vec_ISum(const int *vec, int64_t n)
225 {
226 int sum = 0;
227 int64_t i;
228 for (i = 0; i < n; i++) sum += vec[i];
229 return sum;
230 }
231 int64_t
232 esl_vec_LSum(const int64_t *vec, int64_t n)
233 {
234 int64_t sum = 0;
235 int64_t i;
236 for (i = 0; i < n; i++) sum += vec[i];
237 return sum;
238 }
239
240
241 /* Function: esl_vec_{DFIL}Dot()
242 * Synopsis: Return the dot product of two vectors.
243 *
244 * Purpose: Returns the scalar dot product <vec1> $\cdot$ <vec2>.
245 * Both vectors are of size <n>.
246 */
247 double
248 esl_vec_DDot(const double *vec1, const double *vec2, int64_t n)
249 {
250 double result = 0.;
251 int64_t i;
252 for (i = 0; i < n; i++) result += vec1[i] * vec2[i];
253 return result;
254 }
255 float
256 esl_vec_FDot(const float *vec1, const float *vec2, int64_t n)
257 {
258 float result = 0.;
259 int64_t i;
260 for (i = 0; i < n; i++) result += vec1[i] * vec2[i];
261 return result;
262 }
263 int
264 esl_vec_IDot(const int *vec1, const int *vec2, int64_t n)
265 {
266 int result = 0;
267 int64_t i;
268 for (i = 0; i < n; i++) result += vec1[i] * vec2[i];
269 return result;
270 }
271 int64_t
272 esl_vec_LDot(const int64_t *vec1, const int64_t *vec2, int64_t n)
273 {
274 int64_t result = 0;
275 int64_t i;
276 for (i = 0; i < n; i++) result += vec1[i] * vec2[i];
277 return result;
278 }
279
280
281
282 /* Function: esl_vec_{DFIL}Max()
283 * Synopsis: Return value of the maximum element in a vector.
284 *
285 * Purpose: Returns the maximum value of the <n> values
286 * in <vec>.
287 */
288 double
289 esl_vec_DMax(const double *vec, int64_t n)
290 {
291 double best;
292 int64_t i;
293 best = vec[0];
294 for (i = 1; i < n; i++)
295 if (vec[i] > best) best = vec[i];
296 return best;
297 }
298 float
299 esl_vec_FMax(const float *vec, int64_t n)
300 {
301 float best;
302 int64_t i;
303 best = vec[0];
304 for (i = 1; i < n; i++)
305 if (vec[i] > best) best = vec[i];
306 return best;
307 }
308 int
309 esl_vec_IMax(const int *vec, int64_t n)
310 {
311 int best;
312 int64_t i;
313 best = vec[0];
314 for (i = 1; i < n; i++)
315 if (vec[i] > best) best = vec[i];
316 return best;
317 }
318 int64_t
319 esl_vec_LMax(const int64_t *vec, int64_t n)
320 {
321 int64_t best;
322 int64_t i;
323 best = vec[0];
324 for (i = 1; i < n; i++)
325 if (vec[i] > best) best = vec[i];
326 return best;
327 }
328
329
330
331 /* Function: esl_vec_{DFIL}Min()
332 * Synopsis: Return value of the minimum element in a vector.
333 *
334 * Purpose: Returns the minimum value of the <n> values
335 * in <vec>.
336 */
337 double
338 esl_vec_DMin(const double *vec, int64_t n)
339 {
340 double best;
341 int64_t i;
342 best = vec[0];
343 for (i = 1; i < n; i++)
344 if (vec[i] < best) best = vec[i];
345 return best;
346 }
347 float
348 esl_vec_FMin(const float *vec, int64_t n)
349 {
350 float best;
351 int64_t i;
352 best = vec[0];
353 for (i = 1; i < n; i++)
354 if (vec[i] < best) best = vec[i];
355 return best;
356 }
357 int
358 esl_vec_IMin(const int *vec, int64_t n)
359 {
360 int best;
361 int64_t i;
362 best = vec[0];
363 for (i = 1; i < n; i++)
364 if (vec[i] < best) best = vec[i];
365 return best;
366 }
367 int64_t
368 esl_vec_LMin(const int64_t *vec, int64_t n)
369 {
370 int64_t best;
371 int64_t i;
372 best = vec[0];
373 for (i = 1; i < n; i++)
374 if (vec[i] < best) best = vec[i];
375 return best;
376 }
377
378
379 /* Function: esl_vec_{DFIL}ArgMax()
380 * Synopsis: Return index of maximum element in a vector.
381 *
382 * Purpose: Returns the index of the maximum value in the <n> values
383 * in <vec>. In case of ties, return the smallest index.
384 *
385 * <n> can be 0 and <vec> can be <NULL>, in which case the
386 * function returns 0.
387 *
388 * Note: Do not change the behavior that the smallest index is
389 * returned in case of ties. Some functions rely on this
390 * behavior: optimal accuracy tracebacks in HMMER for example.
391 */
392 int64_t
393 esl_vec_DArgMax(const double *vec, int64_t n)
394 {
395 int64_t best = 0;
396 int64_t i;
397
398 for (i = 1; i < n; i++)
399 if (vec[i] > vec[best]) best = i;
400 return best;
401 }
402 int64_t
403 esl_vec_FArgMax(const float *vec, int64_t n)
404 {
405 int64_t best = 0;
406 int64_t i;
407
408 for (i = 1; i < n; i++)
409 if (vec[i] > vec[best]) best = i;
410 return best;
411 }
412 int64_t
413 esl_vec_IArgMax(const int *vec, int64_t n)
414 {
415 int64_t best = 0;
416 int64_t i;
417
418 for (i = 1; i < n; i++)
419 if (vec[i] > vec[best]) best = i;
420 return best;
421 }
422 int64_t
423 esl_vec_LArgMax(const int64_t *vec, int64_t n)
424 {
425 int64_t best = 0;
426 int64_t i;
427
428 for (i = 1; i < n; i++)
429 if (vec[i] > vec[best]) best = i;
430 return best;
431 }
432
433 /* Function: esl_vec_{DFIL}ArgMin()
434 * Synopsis: Return index of minimum element in a vector.
435 *
436 * Purpose: Returns the index of the minimum value in the <n> values
437 * in <vec>.
438 */
439 int64_t
440 esl_vec_DArgMin(const double *vec, int64_t n)
441 {
442 int64_t best = 0;
443 int64_t i;
444
445 for (i = 1; i < n; i++)
446 if (vec[i] < vec[best]) best = i;
447 return best;
448 }
449 int64_t
450 esl_vec_FArgMin(const float *vec, int64_t n)
451 {
452 int64_t best = 0;
453 int64_t i;
454
455 for (i = 1; i < n; i++)
456 if (vec[i] < vec[best]) best = i;
457 return best;
458 }
459 int64_t
460 esl_vec_IArgMin(const int *vec, int64_t n)
461 {
462 int64_t best = 0;
463 int64_t i;
464
465 for (i = 1; i < n; i++)
466 if (vec[i] < vec[best]) best = i;
467 return best;
468 }
469 int64_t
470 esl_vec_LArgMin(const int64_t *vec, int64_t n)
471 {
472 int64_t i;
473 int64_t best = 0;
474
475 for (i = 1; i < n; i++)
476 if (vec[i] < vec[best]) best = i;
477 return best;
478 }
479
480
481
482 /* Function: esl_vec_{DFILWB}Copy()
483 * Synopsis: Set <dest> vector to same values as <src>.
484 *
485 * Purpose: Copies <src> to <dest>. <src> is
486 * unchanged. Both vectors are of size <n>.
487 */
488 void
489 esl_vec_DCopy(const double *src, int64_t n, double *dest)
490 {
491 int64_t i;
492 for (i = 0; i < n; i++) dest[i] = src[i];
493 }
494 void
495 esl_vec_FCopy(const float *src, int64_t n, float *dest)
496 {
497 int64_t i;
498 for (i = 0; i < n; i++) dest[i] = src[i];
499 }
500 void
501 esl_vec_ICopy(const int *src, int64_t n, int *dest)
502 {
503 int64_t i;
504 for (i = 0; i < n; i++) dest[i] = src[i];
505 }
506 void
507 esl_vec_LCopy(const int64_t *src, int64_t n, int64_t *dest)
508 {
509 int64_t i;
510 for (i = 0; i < n; i++) dest[i] = src[i];
511 }
512 void
513 esl_vec_WCopy(const int16_t *src, int64_t n, int16_t *dest)
514 {
515 int64_t i;
516 for (i = 0; i < n; i++) dest[i] = src[i];
517 }
518 void
519 esl_vec_BCopy(const int8_t *src, int64_t n, int8_t *dest)
520 {
521 int64_t i;
522 for (i = 0; i < n; i++) dest[i] = src[i];
523 }
524
525
526
527
528 /* Function: esl_vec_{DFIL}Swap()
529 * Synopsis: Swap two vectors.
530 *
531 * Purpose: Swaps <vec2> and <vec1>, both of size <n>.
532 *
533 * But you're better off swapping the pointers to the vectors,
534 * if that's feasible.
535 */
536 void
537 esl_vec_DSwap(double *vec1, double *vec2, int64_t n)
538 {
539 int64_t i;
540 double tmp;
541
542 for (i = 0; i < n; i++)
543 { tmp = vec1[i]; vec1[i] = vec2[i]; vec2[i] = tmp; }
544 }
545 void
546 esl_vec_FSwap(float *vec1, float *vec2, int64_t n)
547 {
548 int64_t i;
549 float tmp;
550
551 for (i = 0; i < n; i++)
552 { tmp = vec1[i]; vec1[i] = vec2[i]; vec2[i] = tmp; }
553 }
554 void
555 esl_vec_ISwap(int *vec1, int *vec2, int64_t n)
556 {
557 int64_t i;
558 int tmp;
559
560 for (i = 0; i < n; i++)
561 { tmp = vec1[i]; vec1[i] = vec2[i]; vec2[i] = tmp; }
562 }
563 void
564 esl_vec_LSwap(int64_t *vec1, int64_t *vec2, int64_t n)
565 {
566 int64_t i;
567 int64_t tmp;
568
569 for (i = 0; i < n; i++)
570 { tmp = vec1[i]; vec1[i] = vec2[i]; vec2[i] = tmp; }
571 }
572
573
574 /* Function: esl_vec_{DFILC}Reverse()
575 * Synopsis: Reverse a vector (possibly in place).
576 *
577 * Purpose: Put the <n> values from vector <vec> in reversed order in
578 * <rev>. Caller provides storage in <rev> for at least <n>
579 * values.
580 *
581 * <vec> and <rev> can be the same, in which case <vec> is
582 * reversed in place.
583 *
584 * <esl_vec_CReverse()> needs to be used carefully if
585 * <vec> is a NUL-terminated string, rather than an array.
586 * If you reverse a string <s> in place (i.e.
587 * <esl_vec_CReverse(s, s, n)>), the trailing NUL will
588 * still be there, and you're fine. If you reverse string
589 * <s> into new storage <s2>, you'll need to NUL-terminate
590 * <s2> yourself.
591 */
592 void
593 esl_vec_DReverse(const double *vec, double *rev, int64_t n)
594 {
595 int64_t i;
596 double x;
597
598 for (i = 0; i < n/2; i++)
599 {
600 x = vec[n-i-1];
601 rev[n-i-1] = vec[i];
602 rev[i] = x;
603 }
604 if (n%2) rev[i] = vec[i];
605 }
606 void
607 esl_vec_FReverse(const float *vec, float *rev, int64_t n)
608 {
609 int64_t i;
610 float x;
611
612 for (i = 0; i < n/2; i++)
613 {
614 x = vec[n-i-1];
615 rev[n-i-1] = vec[i];
616 rev[i] = x;
617 }
618 if (n%2) rev[i] = vec[i];
619 }
620 void
621 esl_vec_IReverse(const int *vec, int *rev, int64_t n)
622 {
623 int64_t i;
624 int x;
625
626 for (i = 0; i < n/2; i++)
627 {
628 x = vec[n-i-1];
629 rev[n-i-1] = vec[i];
630 rev[i] = x;
631 }
632 if (n%2) rev[i] = vec[i];
633 }
634 void
635 esl_vec_LReverse(const int64_t *vec, int64_t *rev, int64_t n)
636 {
637 int64_t i;
638 int64_t x;
639
640 for (i = 0; i < n/2; i++)
641 {
642 x = vec[n-i-1];
643 rev[n-i-1] = vec[i];
644 rev[i] = x;
645 }
646 if (n%2) rev[i] = vec[i];
647 }
648 void
649 esl_vec_CReverse(const char *vec, char *rev, int64_t n)
650 {
651 int64_t i;
652 char x;
653
654 for (i = 0; i < n/2; i++)
655 {
656 x = vec[n-i-1];
657 rev[n-i-1] = vec[i];
658 rev[i] = x;
659 }
660 if (n%2) rev[i] = vec[i];
661 }
662
663
664
665
666
667
668 /* some static functions to pass to qsort() that the
669 * upcoming Sort() functions will call
670 */
671 static int
672 qsort_DIncreasing(const void *xp1, const void *xp2)
673 {
674 double x1 = * (double *) xp1;
675 double x2 = * (double *) xp2;
676 if (x1 < x2) return -1;
677 if (x1 > x2) return 1;
678 return 0;
679 }
680 static int
681 qsort_FIncreasing(const void *xp1, const void *xp2)
682 {
683 float x1 = * (float *) xp1;
684 float x2 = * (float *) xp2;
685 if (x1 < x2) return -1;
686 if (x1 > x2) return 1;
687 return 0;
688 }
689 static int
690 qsort_IIncreasing(const void *xp1, const void *xp2)
691 {
692 int x1 = * (int *) xp1;
693 int x2 = * (int *) xp2;
694 if (x1 < x2) return -1;
695 if (x1 > x2) return 1;
696 return 0;
697 }
698 static int
699 qsort_LIncreasing(const void *xp1, const void *xp2)
700 {
701 int64_t x1 = * (int64_t *) xp1;
702 int64_t x2 = * (int64_t *) xp2;
703 if (x1 < x2) return -1;
704 if (x1 > x2) return 1;
705 return 0;
706 }
707 static int
708 qsort_DDecreasing(const void *xp1, const void *xp2)
709 {
710 double x1 = * (double *) xp1;
711 double x2 = * (double *) xp2;
712 if (x1 > x2) return -1;
713 if (x1 < x2) return 1;
714 return 0;
715 }
716 static int
717 qsort_FDecreasing(const void *xp1, const void *xp2)
718 {
719 float x1 = * (float *) xp1;
720 float x2 = * (float *) xp2;
721 if (x1 > x2) return -1;
722 if (x1 < x2) return 1;
723 return 0;
724 }
725 static int
726 qsort_IDecreasing(const void *xp1, const void *xp2)
727 {
728 int x1 = * (int *) xp1;
729 int x2 = * (int *) xp2;
730 if (x1 > x2) return -1;
731 if (x1 < x2) return 1;
732 return 0;
733 }
734 static int
735 qsort_LDecreasing(const void *xp1, const void *xp2)
736 {
737 int64_t x1 = * (int64_t *) xp1;
738 int64_t x2 = * (int64_t *) xp2;
739 if (x1 > x2) return -1;
740 if (x1 < x2) return 1;
741 return 0;
742 }
743
744
745 /* Function: esl_vec_{DFIL}SortIncreasing()
746 * Synopsis: Sort vector from smallest to largest.
747 * Incept: SRE, Wed Aug 17 10:44:31 2005 [St. Louis]
748 *
749 * Purpose: Sorts <vec> in place, from smallest to largest value.
750 * (That is, <vec[0]> is the minimum and <vec[n-1]> is
751 * the maximum.)
752 */
753 void
754 esl_vec_DSortIncreasing(double *vec, int64_t n)
755 {
756 qsort((void *) vec, n, sizeof(double), qsort_DIncreasing);
757 }
758 void
759 esl_vec_FSortIncreasing(float *vec, int64_t n)
760 {
761 qsort((void *) vec, n, sizeof(float), qsort_FIncreasing);
762 }
763 void
764 esl_vec_ISortIncreasing(int *vec, int64_t n)
765 {
766 qsort((void *) vec, n, sizeof(int), qsort_IIncreasing);
767 }
768 void
769 esl_vec_LSortIncreasing(int64_t *vec, int64_t n)
770 {
771 qsort((void *) vec, n, sizeof(int64_t), qsort_LIncreasing);
772 }
773
774
775
776 /* Function: esl_vec_{DFIL}SortDecreasing()
777 * Synopsis: Sort vector from largest to smallest.
778 * Incept: SRE, Wed Aug 17 10:44:31 2005 [St. Louis]
779 *
780 * Purpose: Sorts <vec> in place, from largest to smallest value.
781 * (That is, <vec[0]> is the maximum and <vec[n-1]> is
782 * the minimum.)
783 */
784 void
785 esl_vec_DSortDecreasing(double *vec, int64_t n)
786 {
787 qsort((void *) vec, n, sizeof(double), qsort_DDecreasing);
788 }
789 void
790 esl_vec_FSortDecreasing(float *vec, int64_t n)
791 {
792 qsort((void *) vec, n, sizeof(float), qsort_FDecreasing);
793 }
794 void
795 esl_vec_ISortDecreasing(int *vec, int64_t n)
796 {
797 qsort((void *) vec, n, sizeof(int), qsort_IDecreasing);
798 }
799 void
800 esl_vec_LSortDecreasing(int64_t *vec, int64_t n)
801 {
802 qsort((void *) vec, n, sizeof(int64_t), qsort_LDecreasing);
803 }
804
805
806 /* Function: esl_vec_{DFIL}Shuffle()
807 * Synopsis: Shuffle a vector, in place.
808 *
809 * Purpose: Shuffle a vector <v> of <n> items, using the
810 * random number generator <r>.
811 *
812 * Although <n> is an int64, <n> cannot be larger than
813 * INT32_MAX (2^{31}-1), basically because the
814 * ESL_RANDOMNESS that we use for shuffling is a 32-bit
815 * random number generator. To circumvent this limitation
816 * and shuffle large vectors with >INT32_MAX elements, use
817 * the 64-bit ESL_RAND64 RNG and the
818 * esl_vec_{DFIL}Shuffle64() functions further below.
819 */
820 int
821 esl_vec_DShuffle(ESL_RANDOMNESS *r, double *v, int64_t n)
822 {
823 ESL_DASSERT1(( n <= INT32_MAX ));
824 double swap;
825 int64_t pos;
826 for ( ; n > 1; n--)
827 {
828 pos = esl_rnd_Roll(r, n);
829 swap = v[pos];
830 v[pos] = v[n-1];
831 v[n-1] = swap;
832 }
833 return eslOK;
834 }
835 int
836 esl_vec_FShuffle(ESL_RANDOMNESS *r, float *v, int64_t n)
837 {
838 ESL_DASSERT1(( n <= INT32_MAX ));
839 float swap;
840 int64_t pos;
841 for ( ; n > 1; n--)
842 {
843 pos = esl_rnd_Roll(r, n);
844 swap = v[pos];
845 v[pos] = v[n-1];
846 v[n-1] = swap;
847 }
848 return eslOK;
849 }
850 int
851 esl_vec_IShuffle(ESL_RANDOMNESS *r, int *v, int64_t n)
852 {
853 ESL_DASSERT1(( n <= INT32_MAX ));
854 int swap;
855 int64_t pos;
856 for ( ; n > 1; n--)
857 {
858 pos = esl_rnd_Roll(r, n);
859 swap = v[pos];
860 v[pos] = v[n-1];
861 v[n-1] = swap;
862 }
863 return eslOK;
864 }
865 int
866 esl_vec_LShuffle(ESL_RANDOMNESS *r, int64_t *v, int64_t n)
867 {
868 ESL_DASSERT1(( n <= INT32_MAX ));
869 int64_t swap;
870 int64_t pos;
871 for ( ; n > 1; n--)
872 {
873 pos = esl_rnd_Roll(r, n);
874 swap = v[pos];
875 v[pos] = v[n-1];
876 v[n-1] = swap;
877 }
878 return eslOK;
879 }
880
881
882 /* Function: esl_vec_{DFIL}Shuffle64()
883 * Synopsis: Shuffle a large vector, in place.
884 *
885 * Purpose: Shuffle a vector <v> of <n> items, using the
886 * 64-bit random number generator <rng>.
887 *
888 * These are intended for the unusual case where <v>
889 * contains a very large number of elements, > INT32_MAX of
890 * them (>=2^31; ~2.15B).
891 */
892 int
893 esl_vec_DShuffle64(ESL_RAND64 *rng, double *v, int64_t n)
894 {
895 double swap;
896 int64_t pos;
897 for ( ; n > 1; n--)
898 {
899 pos = esl_rand64_Roll(rng, n);
900 swap = v[pos];
901 v[pos] = v[n-1];
902 v[n-1] = swap;
903 }
904 return eslOK;
905 }
906 int
907 esl_vec_FShuffle64(ESL_RAND64 *rng, float *v, int64_t n)
908 {
909 float swap;
910 int64_t pos;
911 for ( ; n > 1; n--)
912 {
913 pos = esl_rand64_Roll(rng, n);
914 swap = v[pos];
915 v[pos] = v[n-1];
916 v[n-1] = swap;
917 }
918 return eslOK;
919 }
920 int
921 esl_vec_IShuffle64(ESL_RAND64 *rng, int *v, int64_t n)
922 {
923 int swap;
924 int64_t pos;
925 for ( ; n > 1; n--)
926 {
927 pos = esl_rand64_Roll(rng, n);
928 swap = v[pos];
929 v[pos] = v[n-1];
930 v[n-1] = swap;
931 }
932 return eslOK;
933 }
934 int
935 esl_vec_LShuffle64(ESL_RAND64 *rng, int64_t *v, int64_t n)
936 {
937 int64_t swap;
938 int64_t pos;
939 for ( ; n > 1; n--)
940 {
941 pos = esl_rand64_Roll(rng, n);
942 swap = v[pos];
943 v[pos] = v[n-1];
944 v[n-1] = swap;
945 }
946 return eslOK;
947 }
948
949
950
951 /* Function: esl_vec_{DFIL}Compare()
952 * Synopsis: Return <eslOK> if two vectors are equal.
953 * Incept: SRE, Mon Nov 6 10:20:28 2006 [Janelia]
954 *
955 * Purpose: Compare <vec1> to <vec2> for equality, by
956 * comparing each cognate element pair. Both vectors
957 * are of size <n>. Equality of elements is
958 * defined by being $\leq$ fractional tolerance <tol>
959 * for floating point comparisons, and strict equality
960 * for integer comparisons. Return <eslOK>
961 * if the vectors are equal, and <eslFAIL> if not.
962 *
963 * If <n=0>, the test always succeeds. In this case, either
964 * <vec1> and <vec2> (or both) may be <NULL>. This
965 * accommodates an occasional convention of leaving empty
966 * vectors <NULL>.
967 */
968 int
969 esl_vec_DCompare(const double *vec1, const double *vec2, int64_t n, double tol)
970 {
971 int64_t i;
972 for (i = 0; i < n; i++) if (esl_DCompare_old(vec1[i], vec2[i], tol) == eslFAIL) return eslFAIL;
973 return eslOK;
974 }
975 int
976 esl_vec_FCompare(const float *vec1, const float *vec2, int64_t n, float tol)
977 {
978 int64_t i;
979 for (i = 0; i < n; i++) if (esl_FCompare_old(vec1[i], vec2[i], tol) == eslFAIL) return eslFAIL;
980 return eslOK;
981 }
982 int
983 esl_vec_ICompare(const int *vec1, const int *vec2, int64_t n)
984 {
985 int64_t i;
986 for (i = 0; i < n; i++) if (vec1[i] != vec2[i]) return eslFAIL;
987 return eslOK;
988 }
989 int
990 esl_vec_LCompare(const int64_t *vec1, const int64_t *vec2, int64_t n)
991 {
992 int64_t i;
993 for (i = 0; i < n; i++) if (vec1[i] != vec2[i]) return eslFAIL;
994 return eslOK;
995 }
996
997
998
999
1000 /* Function: esl_vec_{DFIL}Dump()
1001 * Synopsis: Output vector to a stream as text.
1002 * Incept: ER, Thu Jul 21 12:54:56 CDT 2005 [St. Louis]
1003 *
1004 * Purpose: Given a vector, dump it to stream <ofp>.
1005 *
1006 * If <label> string is non-NULL, this is a vector of
1007 * single-character labels to put on the vector elements.
1008 * (For example, these might be a sequence alphabet).
1009 * Numbers 1..n is used if <label> is NULL.
1010 *
1011 * Args: ofp - output file pointer; stdout, for example.
1012 * v - vector to dump.
1013 * label - optional: NULL, or character labels
1014 *
1015 * Returns: <eslOK> on success.
1016 */
1017 int
1018 esl_vec_DDump(FILE *ofp, const double *v, int64_t n, const char *label)
1019 {
1020 int64_t a;
1021
1022 fprintf(ofp, " ");
1023 if (label != NULL)
1024 for (a = 0; a < n; a++) fprintf(ofp, " %c ", label[a]);
1025 else
1026 for (a = 0; a < n; a++) fprintf(ofp, "%10" PRId64 " ", a+1);
1027 fprintf(ofp, "\n");
1028
1029 fprintf(ofp, " ");
1030 for (a = 0; a < n; a++) fprintf(ofp, "%10.6f ", v[a]);
1031 fprintf(ofp, "\n");
1032
1033 return eslOK;
1034 }
1035 int
1036 esl_vec_FDump(FILE *ofp, const float *v, int64_t n, const char *label)
1037 {
1038 int64_t a;
1039
1040 fprintf(ofp, " ");
1041 if (label != NULL)
1042 for (a = 0; a < n; a++) fprintf(ofp, " %c ", label[a]);
1043 else
1044 for (a = 0; a < n; a++) fprintf(ofp, "%10" PRId64 " ", a+1);
1045 fprintf(ofp, "\n");
1046
1047 fprintf(ofp, " ");
1048 for (a = 0; a < n; a++) fprintf(ofp, "%10.6f ", v[a]);
1049 fprintf(ofp, "\n");
1050
1051 return eslOK;
1052 }
1053 int
1054 esl_vec_IDump(FILE *ofp, const int *v, int64_t n, const char *label)
1055 {
1056 int64_t a;
1057
1058 fprintf(ofp, " ");
1059 if (label != NULL)
1060 for (a = 0; a < n; a++) fprintf(ofp, " %c ", label[a]);
1061 else
1062 for (a = 0; a < n; a++) fprintf(ofp, "%8" PRId64 " ", a+1);
1063 fprintf(ofp, "\n");
1064
1065 fprintf(ofp, " ");
1066 for (a = 0; a < n; a++) fprintf(ofp, "%8d ", v[a]);
1067 fprintf(ofp, "\n");
1068
1069 return eslOK;
1070 }
1071 int
1072 esl_vec_LDump(FILE *ofp, const int64_t *v, int64_t n, const char *label)
1073 {
1074 int64_t a;
1075
1076 fprintf(ofp, " ");
1077 if (label != NULL)
1078 for (a = 0; a < n; a++) fprintf(ofp, " %c ", label[a]);
1079 else
1080 for (a = 0; a < n; a++) fprintf(ofp, "%8" PRId64 " ", a+1);
1081 fprintf(ofp, "\n");
1082
1083 fprintf(ofp, " ");
1084 for (a = 0; a < n; a++) fprintf(ofp, "%8" PRId64 " ", v[a]);
1085 fprintf(ofp, "\n");
1086
1087 return eslOK;
1088 }
1089
1090
1091
1092
1093 /* Function: esl_vec_D2F(), esl_vec_F2D(), esl_vec_I2F(), esl_vec_I2D()
1094 * Synopsis: Convert between single-precision and double-precision vectors.
1095 * Incept: SRE, Thu Mar 30 09:04:17 2006 [St. Louis]
1096 *
1097 * Purpose: Copy a double vector <src> to a float vector <dst>. Caller
1098 * provides space in the float vector that is at
1099 * least <n>.
1100 *
1101 * Similarly, <esl_vec_F2D()> converts float to double;
1102 * <esl_vec_I2D()> converts integer to double;
1103 * <esl_vec_I2F()> converts integer to float.
1104 */
1105 void
1106 esl_vec_D2F(double *src, int64_t n, float *dst)
1107 {
1108 int64_t i;
1109 for (i = 0; i < n; i++) dst[i] = src[i];
1110 }
1111 void
1112 esl_vec_F2D(float *src, int64_t n, double *dst)
1113 {
1114 int64_t i;
1115 for (i = 0; i < n; i++) dst[i] = src[i];
1116 }
1117 void
1118 esl_vec_I2F(int *src, int64_t n, float *dst)
1119 {
1120 int64_t i;
1121 for (i = 0; i < n; i++) dst[i] = src[i];
1122 }
1123 void
1124 esl_vec_I2D(int *src, int64_t n, double *dst)
1125 {
1126 int64_t i;
1127 for (i = 0; i < n; i++) dst[i] = src[i];
1128 }
1129
1130
1131
1132
1133 /* Function: esl_vec_{DF}Norm()
1134 * Synopsis: Normalize probability vector.
1135 *
1136 * Purpose: Normalizes a probability vector <vec>,
1137 * such that $\sum_{i=1}{n} \mathrm{vec}_i = 1.0$.
1138 *
1139 * If sum is zero, set all elements to $\frac{1}{n}$.
1140 */
1141 void
1142 esl_vec_DNorm(double *vec, int64_t n)
1143 {
1144 double sum;
1145 int64_t i;
1146
1147 sum = esl_vec_DSum(vec, n);
1148 if (sum != 0.0) for (i = 0; i < n; i++) vec[i] /= sum;
1149 else for (i = 0; i < n; i++) vec[i] = 1. / (double) n;
1150 }
1151 void
1152 esl_vec_FNorm(float *vec, int64_t n)
1153 {
1154 float sum;
1155 int64_t i;
1156
1157 sum = esl_vec_FSum(vec, n);
1158 if (sum != 0.0) for (i = 0; i < n; i++) vec[i] /= sum;
1159 else for (i = 0; i < n; i++) vec[i] = 1. / (float) n;
1160 }
1161
1162
1163 /* Function: esl_vec_{DF}LogNorm(), esl_vec_{DF}Log2Norm()
1164 * Synopsis: Normalize a log (or log_2) p-vector, make it a p-vector.
1165 * Incept: SRE, Thu Apr 7 17:45:39 2005 [St. Louis]
1166 *
1167 * Purpose: Given an unnormalized log (or log_2) probability vector <vec>
1168 * of length <n>, normalize it and make it a
1169 * probability vector.
1170 *
1171 * Returns: (void); <vec> is changed in place.
1172 */
1173 void
1174 esl_vec_DLogNorm(double *vec, int64_t n)
1175 {
1176 double denom;
1177
1178 denom = esl_vec_DLogSum(vec, n);
1179 esl_vec_DIncrement(vec, n, -1.*denom);
1180 esl_vec_DExp (vec, n);
1181 esl_vec_DNorm(vec, n); // unnecessary in theory, useful in practice.
1182 }
1183 void
1184 esl_vec_FLogNorm(float *vec, int64_t n)
1185 {
1186 float denom;
1187
1188 denom = esl_vec_FLogSum(vec, n);
1189 esl_vec_FIncrement(vec, n, -1.*denom);
1190 esl_vec_FExp (vec, n);
1191 esl_vec_FNorm(vec, n);
1192 }
1193 void
1194 esl_vec_DLog2Norm(double *vec, int64_t n)
1195 {
1196 double denom;
1197
1198 denom = esl_vec_DLog2Sum(vec, n);
1199 esl_vec_DIncrement(vec, n, -1.*denom);
1200 esl_vec_DExp2 (vec, n);
1201 esl_vec_DNorm (vec, n);
1202 }
1203 void
1204 esl_vec_FLog2Norm(float *vec, int64_t n)
1205 {
1206 float denom;
1207
1208 denom = esl_vec_FLog2Sum(vec, n);
1209 esl_vec_FIncrement(vec, n, -1.*denom);
1210 esl_vec_FExp2 (vec, n);
1211 esl_vec_FNorm (vec, n);
1212 }
1213
1214
1215
1216
1217
1218
1219 /* Function: esl_vec_{DF}Log(), esl_vec_{DF}Log2()
1220 * Synopsis: Convert probability vector elements to log (or log_2) probabilities.
1221 *
1222 * Purpose: Converts a probability vector <vec> to a log (or log_2)
1223 * probability vector: takes the log of each of the <n>
1224 * values in the vector.
1225 *
1226 * If a value is $\leq 0$, set it to $-\infty$.
1227 */
1228 void
1229 esl_vec_DLog(double *vec, int64_t n)
1230 {
1231 int64_t i;
1232 for (i = 0; i < n; i++)
1233 vec[i] = (vec[i] > 0. ? log(vec[i]) : -eslINFINITY);
1234 }
1235 void
1236 esl_vec_FLog(float *vec, int64_t n)
1237 {
1238 int64_t i;
1239 for (i = 0; i < n; i++)
1240 vec[i] = (vec[i] > 0. ? logf(vec[i]) : -eslINFINITY);
1241 }
1242 void
1243 esl_vec_DLog2(double *vec, int64_t n)
1244 {
1245 int64_t i;
1246 for (i = 0; i < n; i++)
1247 vec[i] = (vec[i] > 0. ? log2(vec[i]) : -eslINFINITY);
1248 }
1249 void
1250 esl_vec_FLog2(float *vec, int64_t n)
1251 {
1252 int64_t i;
1253 for (i = 0; i < n; i++)
1254 vec[i] = (vec[i] > 0. ? log2f(vec[i]) : -eslINFINITY);
1255 }
1256
1257 /* Function: esl_vec_{DF}Exp(), esl_vec_{DF}Exp2()
1258 * Synopsis: Converts log (or log_2) probability vector elements to probabilities.
1259 *
1260 * Purpose: Converts a log (or log_2) probability vector <vec> back to a
1261 * probability vector: exponentiates each of the <n>
1262 * values in the vector.
1263 *
1264 * This routine only calls <exp()> on the elements of
1265 * vector, which are presumed to be log probabilities;
1266 * whether the resulting vector is a properly normalized
1267 * probability vector is the caller's problem.
1268 *
1269 */
1270 void
1271 esl_vec_DExp(double *vec, int64_t n)
1272 {
1273 int64_t i;
1274 for (i = 0; i < n; i++) vec[i] = exp(vec[i]);
1275 }
1276 void
1277 esl_vec_FExp(float *vec, int64_t n)
1278 {
1279 int64_t i;
1280 for (i = 0; i < n; i++) vec[i] = expf(vec[i]);
1281 }
1282 void
1283 esl_vec_DExp2(double *vec, int64_t n)
1284 {
1285 int64_t i;
1286 for (i = 0; i < n; i++) vec[i] = exp2(vec[i]);
1287 }
1288 void
1289 esl_vec_FExp2(float *vec, int64_t n)
1290 {
1291 int64_t i;
1292 for (i = 0; i < n; i++) vec[i] = exp2f(vec[i]);
1293 }
1294
1295 /* Function: esl_vec_{DF}LogSum(), esl_vec_{DF}Log2Sum()
1296 * Synopsis: Given log p-vector (or log_2), return log (or log_2) of sum of probabilities.
1297 *
1298 * Purpose: <vec> is a log probability vector; return the log of the scalar sum
1299 * of the probabilities in <vec>. That is, the <n> elements in <vec>
1300 * are log probabilities, but the summation is done in probability
1301 * space, by exponentiating each of the <n> values in the vector,
1302 * summing, and returning the log of the sum.
1303 *
1304 * That is: return $\log \sum_i e^{v_i}$, but done in a numerically
1305 * stable way.
1306 *
1307 * If you need high accuracy, you should sort <vec> from
1308 * smallest to largest before calling for the logsum. We
1309 * don't do that for you, because we don't want to change
1310 * the order of the input <vec>, and nor do we spend an
1311 * allocation.
1312 *
1313 * The <Log2> versions do the same, but where the values
1314 * are base log_2 (bits).
1315 *
1316 */
1317 double
1318 esl_vec_DLogSum(const double *vec, int64_t n)
1319 {
1320 double max, sum;
1321 int64_t i;
1322
1323 max = esl_vec_DMax(vec, n);
1324 if (max == eslINFINITY) return eslINFINITY; /* avoid inf-inf below! */
1325 sum = 0.0;
1326 for (i = 0; i < n; i++)
1327 if (vec[i] > max - 500.) // DBL_EPSILON ~ 2.2e-16; DBL_MIN ~ 2.2e-308; log() = -36, -708
1328 sum += exp(vec[i] - max);
1329 sum = log(sum) + max;
1330 return sum;
1331 }
1332 float
1333 esl_vec_FLogSum(const float *vec, int64_t n)
1334 {
1335 float max, sum;
1336 int64_t i;
1337
1338 max = esl_vec_FMax(vec, n);
1339 if (max == eslINFINITY) return eslINFINITY;
1340 sum = 0.0;
1341 for (i = 0; i < n; i++)
1342 if (vec[i] > max - 50.) // FLT_EPSILON ~ 1.19e-7; FLT_MIN ~ 1.17e-38; log() ~ -16, -87
1343 sum += expf(vec[i] - max);
1344 sum = logf(sum) + max;
1345 return sum;
1346 }
1347 double
1348 esl_vec_DLog2Sum(const double *vec, int64_t n)
1349 {
1350 double max, sum;
1351 int64_t i;
1352
1353 max = esl_vec_DMax(vec, n);
1354 if (max == eslINFINITY) return eslINFINITY; /* avoid inf-inf below! */
1355 sum = 0.0;
1356 for (i = 0; i < n; i++)
1357 if (vec[i] > max - 500.) // DBL_EPSILON ~ 2.2e-16; DBL_MIN ~ 2.2e-308; log2() = -52, -1022
1358 sum += exp2(vec[i] - max);
1359 sum = log2(sum) + max;
1360 return sum;
1361 }
1362 float
1363 esl_vec_FLog2Sum(const float *vec, int64_t n)
1364 {
1365 float max, sum;
1366 int64_t i;
1367
1368 max = esl_vec_FMax(vec, n);
1369 if (max == eslINFINITY) return eslINFINITY;
1370 sum = 0.0;
1371 for (i = 0; i < n; i++)
1372 if (vec[i] > max - 50.) // FLT_EPSILON ~ 1.19e-7; FLT_MIN ~ 1.17e-38; log() ~ -23, -126
1373 sum += exp2f(vec[i] - max);
1374 sum = log2f(sum) + max;
1375 return sum;
1376 }
1377
1378 /* Function: esl_vec_{DF}Entropy()
1379 * Synopsis: Return Shannon entropy of p-vector, in bits.
1380 *
1381 * Purpose: Returns the Shannon entropy of a probability vector <p>,
1382 * in bits ($\log_2$), defined as \citep{CoverThomas}:
1383 *
1384 * \[
1385 * H = - \sum_x p_i \log_2 p_i
1386 * \]
1387 */
1388 double
1389 esl_vec_DEntropy(const double *p, int64_t n)
1390 {
1391 double H = 0.;
1392 int64_t i;
1393
1394 for (i = 0; i < n; i++)
1395 if (p[i] > 0.) H -= p[i] * log2(p[i]);
1396 return H;
1397 }
1398 float
1399 esl_vec_FEntropy(const float *p, int64_t n)
1400 {
1401 float H = 0.;
1402 int64_t i;
1403
1404 for (i = 0; i < n; i++)
1405 if (p[i] > 0.) H -= p[i] * log2f(p[i]);
1406 return H;
1407 }
1408
1409 /* Function: esl_vec_{DF}RelEntropy()
1410 * Synopsis: Return relative entropy $D(p \parallel q)$ in bits.
1411 * Incept: SRE, Fri May 11 09:03:07 2007 [Janelia]
1412 *
1413 * Purpose: Returns Shannon relative entropy of probability
1414 * vectors <p> and <q> in bits, also known as the
1415 * Kullback-Leibler divergence \citep[p.18]{CoverThomas}:
1416 *
1417 * \[
1418 * D(p \parallel q) = \sum_i p_i \log_2 \frac{p_i}{q_i}.
1419 * \]
1420 *
1421 * If for any $i$ $q_i = 0$ and $p_i > 0$, the relative
1422 * entropy is $\infty$.
1423 */
1424 double
1425 esl_vec_DRelEntropy(const double *p, const double *q, int64_t n)
1426 {
1427 int64_t i;
1428 double kl;
1429
1430 kl = 0.;
1431 for(i = 0; i < n; i++)
1432 if (p[i] > 0.) {
1433 if (q[i] == 0.) return eslINFINITY;
1434 else kl += p[i] * log2(p[i]/q[i]);
1435 }
1436 return kl;
1437 }
1438 float
1439 esl_vec_FRelEntropy(const float *p, const float *q, int64_t n)
1440 {
1441 int64_t i;
1442 float kl;
1443
1444 kl = 0.;
1445 for(i = 0; i < n; i++)
1446 if (p[i] > 0.) {
1447 if (q[i] == 0.) return eslINFINITY;
1448 else kl += p[i] * log2(p[i]/q[i]);
1449 }
1450 return kl;
1451 }
1452
1453
1454
1455 /* Function: esl_vec_{DF}CDF()
1456 * Synopsis: Calculate cumulative distribution for a discrete prob vector
1457 * Incept: SRE, Wed Jan 12 09:09:42 2011 [Janelia]
1458 *
1459 * Purpose: Given a probability vector <p> of length <n>,
1460 * calculates its cumulate distribution function
1461 * and puts in in caller-allocated space <cdf>.
1462 * Caller must have allocated <cdf> for at least
1463 * <n> elements.
1464 *
1465 * By definition, <cdf[0] == p[0]>, and <cdf[n-1]> ought to
1466 * be 1.0; however, numerical roundoff error must be tolerated
1467 * in the sum. If caller isn't sure about <p>'s provenance,
1468 * it may want to check that <cdf[n-1]> is tolerably close
1469 * to 1.0 (see <esl_DCompare_old()>).
1470 *
1471 * It is ok for <cdf> to be the same space as <p>
1472 * (<esl_vec_DCDF(p, n, p)> is fine); that is, <p> can be
1473 * overwritten by <cdf>.
1474 *
1475 * Args: p - input probability vector p[0..n-1]
1476 * n - number of elements in p
1477 * cdf - RETURN: cumulative distribution for p, in caller-allocated space
1478 *
1479 * Returns: (void).
1480 */
1481 void
1482 esl_vec_DCDF(const double *p, int64_t n, double *cdf)
1483 {
1484 int64_t i;
1485
1486 cdf[0] = p[0];
1487 for (i = 1; i < n; i++)
1488 cdf[i] = p[i] + cdf[i-1];
1489 }
1490 void
1491 esl_vec_FCDF(const float *p, int64_t n, float *cdf)
1492 {
1493 int64_t i;
1494
1495 cdf[0] = p[0];
1496 for (i = 1; i < n; i++)
1497 cdf[i] = p[i] + cdf[i-1];
1498 }
1499
1500
1501
1502 /* Function: esl_vec_{DF}Validate()
1503 * Synopsis: Verifies that vector is p-vector.
1504 * Incept: ER, Tue Dec 5 09:38:54 EST 2006 [janelia]
1505 *
1506 * Purpose: Validate a probability vector <vec> of length <n>.
1507 * Each element has to be between 0 and 1, and
1508 * the sum of all elements has to be suitably close
1509 * to 1 (<fabs(1-sum) <= tol>).
1510 *
1511 * Args: v - p vector to validate.
1512 * n - dimensionality of v
1513 * tol - absolute difference; convergence criterion applied to sum of v
1514 * errbuf - NULL, or a failure message buffer allocated
1515 * for at least <eslERRBUFSIZE> chars.
1516 *
1517 * Returns: <eslOK> on success, or <eslFAIL> on validation failure.
1518 * Upon failure, if caller provided a non-<NULL> <errbuf>,
1519 * an informative message is left there.
1520 */
1521 int
1522 esl_vec_DValidate(const double *vec, int64_t n, double tol, char *errbuf)
1523 {
1524 double sum = 0.;
1525 int64_t i;
1526
1527 if (errbuf) *errbuf = 0;
1528 if (n == 0) return eslOK;
1529
1530 for (i = 0; i < n; i++) {
1531 if (! isfinite(vec[i]) || vec[i] < 0.0 || vec[i] > 1.0)
1532 ESL_FAIL(eslFAIL, errbuf, "value %d (%g) is not a probability between 0..1", i, vec[i]);
1533 sum += vec[i];
1534 }
1535
1536 if (fabs(sum - 1.0) > tol) ESL_FAIL(eslFAIL, errbuf, "vector does not sum to 1.0");
1537 return eslOK;
1538 }
1539 int
1540 esl_vec_FValidate(const float *vec, int64_t n, float tol, char *errbuf)
1541 {
1542 float sum = 0.;
1543 int64_t x;
1544 int status;
1545
1546 if (errbuf) *errbuf = 0;
1547 if (n == 0) return eslOK;
1548
1549 for (x = 0; x < n; x++) {
1550 if (vec[x] < 0.0 || vec[x] > 1.0)
1551 ESL_XFAIL(eslFAIL, errbuf, "value %d is not a probability between 0..1", x);
1552 sum += vec[x];
1553 }
1554
1555 if (fabs(sum - 1.0) > tol)
1556 ESL_XFAIL(eslFAIL, errbuf, "vector does not sum to 1.0");
1557 return eslOK;
1558
1559 ERROR:
1560 return status;
1561 }
1562
1563 /* Function: esl_vec_{DF}LogValidate(), esl_vec_{DF}Log2Validate()
1564 * Synopsis: Verify that vector is a log (or log_2) p-vector.
1565 * Incept: ER, Tue Dec 5 09:46:51 EST 2006 [janelia]
1566 *
1567 * Purpose: Validate a log probability vector <vec> of length <n>.
1568 * The exp of each element has to be between 0 and 1, and
1569 * the sum of all elements has to be 1.
1570 *
1571 * Args: vec - log p vector to validate.
1572 * n - dimensionality of v
1573 * tol - convergence criterion applied to sum of exp v
1574 * errbuf - NULL, or a failure message buffer allocated
1575 * for at least p7_ERRBUFSIZE chars.
1576 *
1577 * Returns: <eslOK> on success, or <eslFAIL> on failure; upon failure,
1578 * if caller provided a non-<NULL> <errbuf>, an informative
1579 * message is left there.
1580 *
1581 * Throws: <eslEMEM> on allocation failure.
1582 */
1583 int
1584 esl_vec_DLogValidate(const double *vec, int64_t n, double tol, char *errbuf)
1585 {
1586 int status;
1587 double *expvec = NULL;
1588
1589 if (errbuf) *errbuf = 0;
1590 if (n == 0) return eslOK;
1591
1592 ESL_ALLOC(expvec, sizeof(double)*n);
1593 esl_vec_DCopy(vec, n, expvec);
1594 esl_vec_DExp(expvec, n);
1595 if ((status = esl_vec_DValidate(expvec, n, tol, errbuf)) != eslOK) goto ERROR;
1596 free(expvec);
1597 return eslOK;
1598
1599 ERROR:
1600 if (expvec != NULL) free(expvec);
1601 return status;
1602 }
1603 int
1604 esl_vec_FLogValidate(const float *vec, int64_t n, float tol, char *errbuf)
1605 {
1606 int status;
1607 float *expvec = NULL;
1608
1609 if (errbuf) *errbuf = 0;
1610 if (n == 0) return eslOK;
1611
1612 ESL_ALLOC(expvec, sizeof(float)*n);
1613 esl_vec_FCopy(vec, n, expvec);
1614 esl_vec_FExp(expvec, n);
1615 if ((status = esl_vec_FValidate(expvec, n, tol, errbuf)) != eslOK) goto ERROR;
1616 free(expvec);
1617 return eslOK;
1618
1619 ERROR:
1620 if (expvec != NULL) free(expvec);
1621 return eslOK;
1622 }
1623 int
1624 esl_vec_DLog2Validate(const double *vec, int64_t n, double tol, char *errbuf)
1625 {
1626 int status;
1627 double *expvec = NULL;
1628
1629 if (errbuf) *errbuf = 0;
1630 if (n == 0) return eslOK;
1631
1632 ESL_ALLOC(expvec, sizeof(double)*n);
1633 esl_vec_DCopy(vec, n, expvec);
1634 esl_vec_DExp2(expvec, n);
1635 if ((status = esl_vec_DValidate(expvec, n, tol, errbuf)) != eslOK) goto ERROR;
1636 free(expvec);
1637 return eslOK;
1638
1639 ERROR:
1640 if (expvec != NULL) free(expvec);
1641 return status;
1642 }
1643 int
1644 esl_vec_FLog2Validate(const float *vec, int64_t n, float tol, char *errbuf)
1645 {
1646 int status;
1647 float *expvec = NULL;
1648
1649 if (errbuf) *errbuf = 0;
1650 if (n == 0) return eslOK;
1651
1652 ESL_ALLOC(expvec, sizeof(float)*n);
1653 esl_vec_FCopy(vec, n, expvec);
1654 esl_vec_FExp2(expvec, n);
1655 if ((status = esl_vec_FValidate(expvec, n, tol, errbuf)) != eslOK) goto ERROR;
1656 free(expvec);
1657 return eslOK;
1658
1659 ERROR:
1660 if (expvec != NULL) free(expvec);
1661 return eslOK;
1662 }
1663
1664
1665
1666
1667 /*****************************************************************
1668 * 2. Unit tests
1669 *****************************************************************/
1670 #ifdef eslVECTOROPS_TESTDRIVE
1671
1672 #include "esl_random.h"
1673
1674 /* utest_ivectors
1675 * Tests the integer (I) versions for stupid mistakes.
1676 *
1677 * The utest succeeds for any <rng> seed.
1678 */
1679 static void
1680 utest_ivectors(ESL_RANDOMNESS *rng)
1681 {
1682 char msg[] = "esl_vectorops ivectors test failed";
1683 int n = 20;
1684 int *v1 = malloc(sizeof(int) * n);
1685 int *v2 = malloc(sizeof(int) * n);
1686 int i;
1687
1688 for (i = 0; i < n; i++) esl_vec_ISet(v1+i, n-i, i); // violent way to set vector to 0..n-1
1689 for (i = 0; i < n; i++) v2[i] = i; // ... and more obvious way
1690 if ( esl_vec_ICompare(v1, v2, n) != eslOK) esl_fatal(msg);
1691
1692 esl_vec_IAdd(v1, v2, n); if (v1[1] != 2) esl_fatal(msg); // v1 is now 0..(2n-2)
1693 esl_vec_IScale(v2, n, 2); if (v2[1] != 2) esl_fatal(msg); // ... now v2 is too
1694 esl_vec_IIncrement(v1, n, 1); if (v1[1] != 3) esl_fatal(msg); // v1 now 1..(2n-1)
1695 esl_vec_IAddScaled(v1, v1, -1, n); if (v1[1] != 0) esl_fatal(msg); // v1 is now all 0's
1696 esl_vec_ISwap(v1, v2, n); if (v2[1] != 0) esl_fatal(msg); // v2 is now all 0's, and v1 is 0..(2n-2)
1697 esl_vec_ICopy(v1, n, v2); if (v2[1] != 2) esl_fatal(msg); // both v1 and v2 are now 0..(2n-2) again
1698
1699 esl_vec_IShuffle(rng, v1, n); // shuffle v1, leaving v2.
1700
1701 if (esl_vec_IDot(v1, v1, n) != esl_vec_IDot(v2, v2, n)) esl_fatal(msg);
1702 if (esl_vec_ISum(v1, n) != (n * (n-1))) esl_fatal(msg);
1703 if (esl_vec_IMax(v1, n) != 2*(n-1)) esl_fatal(msg);
1704 if (esl_vec_IMin(v1, n) != 0) esl_fatal(msg);
1705 if (v1[ esl_vec_IArgMax(v1, n) ] != 2*(n-1)) esl_fatal(msg);
1706 if (v1[ esl_vec_IArgMin(v1, n) ] != 0) esl_fatal(msg);
1707
1708 esl_vec_ISortDecreasing(v1, n); // now 2(n-1)..0
1709 esl_vec_ISortIncreasing(v2, n);
1710 esl_vec_IReverse(v2, v2, n); // ... ditto for v2 by another way
1711 if ( esl_vec_ICompare(v1, v2, n) != eslOK) esl_fatal(msg);
1712 if ( v1[0] != 2*(n-1)) esl_fatal(msg);
1713
1714 free(v1);
1715 free(v2);
1716 }
1717
1718 /* utest_lvectors
1719 * Tests the int64_t (L) versions for stupid mistakes.
1720 * Same as utest_ivectors() but with int64_t type.
1721 */
1722 static void
1723 utest_lvectors(ESL_RANDOMNESS *rng)
1724 {
1725 char msg[] = "esl_vectorops lvectors test failed";
1726 int n = 20;
1727 int64_t *v1 = malloc(sizeof(int64_t) * n);
1728 int64_t *v2 = malloc(sizeof(int64_t) * n);
1729 int i;
1730
1731 for (i = 0; i < n; i++) esl_vec_LSet(v1+i, n-i, (int64_t) i);
1732 for (i = 0; i < n; i++) v2[i] = (int64_t) i;
1733 if ( esl_vec_LCompare(v1, v2, n) != eslOK) esl_fatal(msg);
1734
1735 esl_vec_LAdd(v1, v2, n); if (v1[1] != 2) esl_fatal(msg);
1736 esl_vec_LScale(v2, n, 2); if (v2[1] != 2) esl_fatal(msg);
1737 esl_vec_LIncrement(v1, n, 1); if (v1[1] != 3) esl_fatal(msg);
1738 esl_vec_LAddScaled(v1, v1, -1, n); if (v1[1] != 0) esl_fatal(msg);
1739 esl_vec_LSwap(v1, v2, n); if (v2[1] != 0) esl_fatal(msg);
1740 esl_vec_LCopy(v1, n, v2); if (v2[1] != 2) esl_fatal(msg);
1741
1742 esl_vec_LShuffle(rng, v1, n);
1743
1744 if (esl_vec_LDot(v1, v1, n) != esl_vec_LDot(v2, v2, n)) esl_fatal(msg);
1745 if (esl_vec_LSum(v1, n) != (n * (n-1))) esl_fatal(msg);
1746 if (esl_vec_LMax(v1, n) != 2*(n-1)) esl_fatal(msg);
1747 if (esl_vec_LMin(v1, n) != 0) esl_fatal(msg);
1748 if (v1[ esl_vec_LArgMax(v1, n) ] != 2*(n-1)) esl_fatal(msg);
1749 if (v1[ esl_vec_LArgMin(v1, n) ] != 0) esl_fatal(msg);
1750
1751 esl_vec_LSortDecreasing(v1, n);
1752 esl_vec_LSortIncreasing(v2, n);
1753 esl_vec_LReverse(v2, v2, n);
1754 if ( esl_vec_LCompare(v1, v2, n) != eslOK) esl_fatal(msg);
1755 if ( v1[0] != 2*(n-1)) esl_fatal(msg);
1756
1757 free(v1);
1758 free(v2);
1759 }
1760
1761 /* utest_fvectors
1762 * Tests the float (F) versions for stupid mistakes.
1763 * Same as utest_ivectors() but with float type.
1764 * All values are whole numbers, so no roundoff error issues.
1765 */
1766 static void
1767 utest_fvectors(ESL_RANDOMNESS *rng)
1768 {
1769 char msg[] = "esl_vectorops fvectors test failed";
1770 int n = 20;
1771 float *v1 = malloc(sizeof(float) * n);
1772 float *v2 = malloc(sizeof(float) * n);
1773 int i;
1774
1775 for (i = 0; i < n; i++) esl_vec_FSet(v1+i, n-i, (float) i);
1776 for (i = 0; i < n; i++) v2[i] = (float) i;
1777 if ( esl_vec_FCompare(v1, v2, n, 0.) != eslOK) esl_fatal(msg);
1778
1779 esl_vec_FAdd(v1, v2, n); if (v1[1] != 2.) esl_fatal(msg);
1780 esl_vec_FScale(v2, n, 2.); if (v2[1] != 2.) esl_fatal(msg);
1781 esl_vec_FIncrement(v1, n, 1.); if (v1[1] != 3.) esl_fatal(msg);
1782 esl_vec_FAddScaled(v1, v1, -1., n); if (v1[1] != 0.) esl_fatal(msg);
1783 esl_vec_FSwap(v1, v2, n); if (v2[1] != 0.) esl_fatal(msg);
1784 esl_vec_FCopy(v1, n, v2); if (v2[1] != 2.) esl_fatal(msg);
1785
1786 esl_vec_FShuffle(rng, v1, n);
1787
1788 if (esl_vec_FDot(v1, v1, n) != esl_vec_FDot(v2, v2, n)) esl_fatal(msg);
1789 if (esl_vec_FSum(v1, n) != (n * (n-1))) esl_fatal(msg);
1790 if (esl_vec_FMax(v1, n) != 2*(n-1)) esl_fatal(msg);
1791 if (esl_vec_FMin(v1, n) != 0) esl_fatal(msg);
1792 if (v1[esl_vec_FArgMax(v1, n) ] != 2*(n-1)) esl_fatal(msg);
1793 if (v1[esl_vec_FArgMin(v1, n) ] != 0) esl_fatal(msg);
1794
1795 esl_vec_FSortDecreasing(v1, n);
1796 esl_vec_FSortIncreasing(v2, n);
1797 esl_vec_FReverse(v2, v2, n);
1798 if ( esl_vec_FCompare(v1, v2, n, 0.) != eslOK) esl_fatal(msg);
1799 if ( v1[0] != 2*(n-1)) esl_fatal(msg);
1800
1801 free(v1);
1802 free(v2);
1803 }
1804
1805
1806 /* utest_dvectors
1807 * Tests the double (D) versions for stupid mistakes.
1808 * Same as utest_ivectors() but with double type.
1809 * All values are whole numbers, so no roundoff error issues.
1810 */
1811 static void
1812 utest_dvectors(ESL_RANDOMNESS *rng)
1813 {
1814 char msg[] = "esl_vectorops dvectors test failed";
1815 int n = 20;
1816 double *v1 = malloc(sizeof(double) * n);
1817 double *v2 = malloc(sizeof(double) * n);
1818 int i;
1819
1820 for (i = 0; i < n; i++) esl_vec_DSet(v1+i, n-i, (float) i);
1821 for (i = 0; i < n; i++) v2[i] = (float) i;
1822 if ( esl_vec_DCompare(v1, v2, n, 0.) != eslOK) esl_fatal(msg);
1823
1824 esl_vec_DAdd(v1, v2, n); if (v1[1] != 2.) esl_fatal(msg);
1825 esl_vec_DScale(v2, n, 2.); if (v2[1] != 2.) esl_fatal(msg);
1826 esl_vec_DIncrement(v1, n, 1.); if (v1[1] != 3.) esl_fatal(msg);
1827 esl_vec_DAddScaled(v1, v1, -1., n); if (v1[1] != 0.) esl_fatal(msg);
1828 esl_vec_DSwap(v1, v2, n); if (v2[1] != 0.) esl_fatal(msg);
1829 esl_vec_DCopy(v1, n, v2); if (v2[1] != 2.) esl_fatal(msg);
1830
1831 esl_vec_DShuffle(rng, v1, n);
1832
1833 if (esl_vec_DDot(v1, v1, n) != esl_vec_DDot(v2, v2, n)) esl_fatal(msg);
1834 if (esl_vec_DSum(v1, n) != (n * (n-1))) esl_fatal(msg);
1835 if (esl_vec_DMax(v1, n) != 2*(n-1)) esl_fatal(msg);
1836 if (esl_vec_DMin(v1, n) != 0) esl_fatal(msg);
1837 if (v1[esl_vec_DArgMax(v1, n) ] != 2*(n-1)) esl_fatal(msg);
1838 if (v1[esl_vec_DArgMin(v1, n) ] != 0) esl_fatal(msg);
1839
1840 esl_vec_DSortDecreasing(v1, n);
1841 esl_vec_DSortIncreasing(v2, n);
1842 esl_vec_DReverse(v2, v2, n);
1843 if ( esl_vec_DCompare(v1, v2, n, 0.) != eslOK) esl_fatal(msg);
1844 if ( v1[0] != 2*(n-1)) esl_fatal(msg);
1845
1846 free(v1);
1847 free(v2);
1848 }
1849
1850
1851 static void
1852 utest_pvectors(void)
1853 {
1854 char msg[] = "pvector unit test failed";
1855 double p1[4] = { 0.25, 0.25, 0.25, 0.25 };
1856 double p2[4];
1857 double p3[4];
1858 float p1f[4];
1859 float p2f[4] = { 0.0, 0.5, 0.5, 0.0 };
1860 float p3f[4];
1861 int n = 4;
1862 double result;
1863
1864 esl_vec_D2F(p1, n, p1f);
1865 esl_vec_F2D(p2f, n, p2);
1866
1867 if (esl_vec_DValidate(p1, n, 1e-12, NULL) != eslOK) esl_fatal(msg);
1868 if (esl_vec_FValidate(p1f, n, 1e-7, NULL) != eslOK) esl_fatal(msg);
1869
1870 result = esl_vec_DEntropy(p1, n); if (esl_DCompare_old(2.0, result, 1e-9) != eslOK) esl_fatal(msg);
1871 result = esl_vec_FEntropy(p1f, n); if (esl_DCompare_old(2.0, result, 1e-9) != eslOK) esl_fatal(msg);
1872 result = esl_vec_DEntropy(p2, n); if (esl_DCompare_old(1.0, result, 1e-9) != eslOK) esl_fatal(msg);
1873 result = esl_vec_FEntropy(p2f, n); if (esl_DCompare_old(1.0, result, 1e-9) != eslOK) esl_fatal(msg);
1874
1875 result = esl_vec_DRelEntropy(p2, p1, n); if (esl_DCompare_old(1.0, result, 1e-9) != eslOK) esl_fatal(msg);
1876 result = esl_vec_FRelEntropy(p2f, p1f, n); if (esl_DCompare_old(1.0, result, 1e-9) != eslOK) esl_fatal(msg);
1877
1878 result = esl_vec_DRelEntropy(p1, p2, n); if (result != eslINFINITY) esl_fatal(msg);
1879 result = esl_vec_FRelEntropy(p1f, p2f, n); if (result != eslINFINITY) esl_fatal(msg);
1880
1881 esl_vec_DLog(p2, n);
1882 if (esl_vec_DLogValidate(p2, n, 1e-12, NULL) != eslOK) esl_fatal(msg);
1883 esl_vec_DExp(p2, n);
1884 if (p2[0] != 0.) esl_fatal(msg);
1885
1886 esl_vec_FLog(p2f, n);
1887 if (esl_vec_FLogValidate(p2f, n, 1e-7, NULL) != eslOK) esl_fatal(msg);
1888 esl_vec_FExp(p2f, n);
1889 if (p2f[0] != 0.) esl_fatal(msg);
1890
1891 esl_vec_DCopy(p2, n, p3);
1892 esl_vec_DScale(p3, n, 10.);
1893 esl_vec_DNorm(p3, n);
1894 if (esl_vec_DCompare(p2, p3, n, 1e-12) != eslOK) esl_fatal(msg);
1895
1896 esl_vec_DLog(p3, n);
1897 result = esl_vec_DLogSum(p3, n); if (esl_DCompare_old(0.0, result, 1e-12) != eslOK) esl_fatal(msg);
1898 esl_vec_DIncrement(p3, n, 2.0);
1899 esl_vec_DLogNorm(p3, n);
1900 if (esl_vec_DCompare(p2, p3, n, 1e-12) != eslOK) esl_fatal(msg);
1901
1902 esl_vec_FCopy(p2f, n, p3f);
1903 esl_vec_FScale(p3f, n, 10.);
1904 esl_vec_FNorm(p3f, n);
1905 if (esl_vec_FCompare(p2f, p3f, n, 1e-7) != eslOK) esl_fatal(msg);
1906
1907 esl_vec_FLog(p3f, n);
1908 result = esl_vec_FLogSum(p3f, n); if (esl_DCompare_old(0.0, result, 1e-7) != eslOK) esl_fatal(msg);
1909 esl_vec_FIncrement(p3f, n, 2.0);
1910 esl_vec_FLogNorm(p3f, n);
1911 if (esl_vec_FCompare(p2f, p3f, n, 1e-7) != eslOK) esl_fatal(msg);
1912
1913 return;
1914 }
1915
1916
1917 /* utest_stable_sums()
1918 *
1919 * This is more of a note than a unit test, about numerically stable
1920 * sums and means.
1921 *
1922 * esl_vec_{FD}Sum() routines use Kahan compensated summation to do
1923 * numerically stable sums of large numbers of terms. Naively summing
1924 * up a large number of n terms, and calculating a mean by dividing
1925 * the sum by n, is numerically unstable. 1 + 0.5 \epsilon = 1, so
1926 * for floats with FLT_EPSILON ~ 1.19e-7, at A ~ 17M, A+1=A, and your
1927 * sum stops accumulating.
1928 *
1929 * Specifically for calculating means, the Welford running mean
1930 * algorithm is a simple and apparently widely used alternative -
1931 * written here so I don't forget it. But the Welford algorithm also
1932 * breaks on large numbers of terms though; it appears to work on this
1933 * test case only because the terms are iid random. It stops updating
1934 * the mean when i is large enough that (x_i - mean) / i becomes
1935 * negligible. The mean of the first m << n numbers is already
1936 * accurate in this iid case.
1937 *
1938 * Here we take the mean of n uniform random numbers of mean 0.5, so
1939 * we expect an overall mean of 0.5. A naive sum will fail once
1940 * there's on the order of n ~ 34M numbers in the sum. Welford
1941 * algorithm stops updating its mean around the same point, but it
1942 * escapes notice in this case.
1943 */
1944 static void
1945 utest_stable_sums(ESL_RANDOMNESS *rng)
1946 {
1947 char msg[] = "esl_vectorops:: stable_sums unit test failed";
1948 float *x = NULL;
1949 int n = 40000000;
1950 float naive_mean = 0.;
1951 float welford_mean = 0.;
1952 float kahan_mean = 0.;
1953 float easel_mean = 0.;
1954 float y,t,c;
1955 int i;
1956 int status;
1957
1958 ESL_ALLOC(x, sizeof(float) * (n+1)); // n+1 because we'll use 1..n and not touch 0
1959 for (i = 0; i <= n; i++) x[i] = esl_random(rng); // [0,1)
1960
1961 c = 0.;
1962 for (i = 1; i <= n; i++)
1963 {
1964 naive_mean += x[i]; // This is not what you want to do.
1965 welford_mean += (x[i] - welford_mean) / i; // This is the Welford running mean algorithm. https://nullbuffer.com/articles/welford_algorithm.html
1966
1967 y = x[i] - c; // These four lines are the Kahan compensated summation algorithm.
1968 t = kahan_mean + y;
1969 c = (t - kahan_mean) - y;
1970 kahan_mean = t;
1971 }
1972 naive_mean /= n;
1973 kahan_mean /= n;
1974 easel_mean = esl_vec_FSum(x, n) / n;
1975
1976 // printf("%f %f %f %f\n", naive_mean, welford_mean, kahan_mean, easel_mean);
1977
1978 // By checking against actual kahan_mean instead of expected 0.5,
1979 // we're robust against rare outliers in random samples, and
1980 // we don't need to protect against them with a fixed RNG seed;
1981 // any seed will work.
1982 //
1983 // A compiler that's both smart and risktaking (e.g. icc in its
1984 // default mode of -fp-model=fast) can rearrange the above
1985 // calculation and end up getting the right answer for the
1986 // naive_mean. But if it does fail (as we expect), both the welford
1987 // mean and the easel mean will work.
1988 if (esl_FCompare(kahan_mean, naive_mean, 1e-3, 1e-3) == eslFAIL) // naive sum will typically fail, except with fast-math risk-taking optimizers
1989 {
1990 if (esl_FCompare(kahan_mean, welford_mean, 1e-3, 1e-3) != eslOK) esl_fatal(msg); // welford and kahan will both work
1991 if (esl_FCompare(kahan_mean, easel_mean, 1e-3, 1e-3) != eslOK) esl_fatal(msg); // easel uses kahan, check against welford
1992 }
1993 free(x);
1994 return;
1995
1996 ERROR:
1997 esl_fatal(msg);
1998 }
1999 #endif /*eslVECTOROPS_TESTDRIVE*/
2000
2001
2002 /*****************************************************************
2003 * 3. Test driver
2004 *****************************************************************/
2005 #ifdef eslVECTOROPS_TESTDRIVE
2006
2007 #include "easel.h"
2008 #include "esl_getopts.h"
2009 #include "esl_vectorops.h"
2010
2011 static ESL_OPTIONS options[] = {
2012 /* name type default env range toggles reqs incomp help docgroup*/
2013 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
2014 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
2015 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2016 };
2017 static char usage[] = "[-options]";
2018 static char banner[] = "test driver for vectorops module";
2019
2020 int
2021 main(int argc, char **argv)
2022 {
2023 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
2024 ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));
2025
2026 fprintf(stderr, "## %s\n", argv[0]);
2027 fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng));
2028
2029 utest_ivectors(rng);
2030 utest_lvectors(rng);
2031 utest_fvectors(rng);
2032 utest_dvectors(rng);
2033 utest_pvectors();
2034 utest_stable_sums(rng);
2035
2036 fprintf(stderr, "# status = ok\n");
2037
2038 esl_randomness_Destroy(rng);
2039 esl_getopts_Destroy(go);
2040 return eslOK;
2041 }
2042 #endif /*eslVECTOROPS_TESTDRIVE*/
2043
2044 /*****************************************************************
2045 * 4. Examples
2046 *****************************************************************/
2047
2048 #ifdef eslVECTOROPS_EXAMPLE
2049 /*::cexcerpt::vectorops_example::begin::*/
2050 /* gcc -g -Wall -o example -I. -DeslVECTOROPS_EXAMPLE esl_vectorops.c easel.c -lm */
2051 #include "easel.h"
2052 #include "esl_vectorops.h"
2053
2054 int main(void)
2055 {
2056 double *p;
2057 char labels[] = "ACGT";
2058 int n = 4;
2059
2060 p = malloc(sizeof(double) * n);
2061 esl_vec_DSet(p, n, 1.0);
2062 esl_vec_DNorm(p, n);
2063 esl_vec_DDump(stdout, p, n, labels);
2064 free(p);
2065 return 0;
2066 }
2067 /*::cexcerpt::vectorops_example::end::*/
2068 #endif /*eslVECTOROPS_EXAMPLE*/
2069
2070