00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <it/math.h>
00027 #include <it/vec.h>
00028 #include <it/io.h>
00029 #include <it/random.h>
00030
00031
00032
00033
00034
00035
00036 static Vec_header_t __vec_null = {
00037 0,
00038 0,
00039 NULL,
00040 sizeof (double),
00041 };
00042 static Vec_header_t __ivec_null = {
00043 0,
00044 0,
00045 NULL,
00046 sizeof (int),
00047 };
00048 static Vec_header_t __bvec_null = {
00049 0,
00050 0,
00051 NULL,
00052 sizeof (byte),
00053 };
00054 static Vec_header_t __cvec_null = {
00055 0,
00056 0,
00057 NULL,
00058 sizeof (cplx),
00059 };
00060
00061
00062
00063
00064 vec const vec_null = (vec) (&__vec_null + 1);
00065 ivec const ivec_null = (ivec) (&__ivec_null + 1);
00066 bvec const bvec_null = (bvec) (&__bvec_null + 1);
00067 cvec const cvec_null = (cvec) (&__cvec_null + 1);
00068
00069
00070
00071
00072
00073 void *__Vec_new_alloc (size_t elem_size, idx_t length, idx_t length_max)
00074 {
00075 Vec_header_t *hdr;
00076 char *ptr, *aligned;
00077 int padding;
00078
00079
00080
00081 ptr =
00082 (char *) malloc (sizeof (Vec_header_t) + length_max * elem_size +
00083 IT_ALLOC_ALIGN);
00084 it_assert (ptr, "No enough memory to allocate the vector");
00085
00086
00087 aligned = ptr + sizeof (Vec_header_t) + IT_ALLOC_ALIGN - 1;
00088 padding = ((int) (long) (aligned)) & (IT_ALLOC_ALIGN - 1);
00089 aligned -= padding;
00090
00091 hdr = (Vec_header_t *) aligned - 1;
00092 hdr->length = length;
00093 hdr->length_max = length_max;
00094 hdr->ptr = ptr;
00095 hdr->element_size = elem_size;
00096 return (aligned);
00097 }
00098
00099
00100 vec vec_new_alloc (idx_t length, idx_t length_max) {
00101 assert (length_max >= length);
00102 return Vec_new_alloc (double, length, length_max);
00103 }
00104
00105
00106 ivec ivec_new_alloc (idx_t length, idx_t length_max) {
00107 assert (length_max >= length);
00108 return Vec_new_alloc (int, length, length_max);
00109 }
00110
00111
00112 bvec bvec_new_alloc (idx_t length, idx_t length_max) {
00113 assert (length_max >= length);
00114 return Vec_new_alloc (byte, length, length_max);
00115 }
00116
00117
00118 cvec cvec_new_alloc (idx_t length, idx_t length_max) {
00119 assert (length_max >= length);
00120 return Vec_new_alloc (cplx, length, length_max);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 void *__Vec_new_realloc (void *V, size_t elem_size, idx_t length,
00139 idx_t length_max)
00140 {
00141 void *new_Vec;
00142 idx_t old_length;
00143
00144 new_Vec = __Vec_new_alloc (elem_size, length, length_max);
00145 assert (new_Vec);
00146 if (V) {
00147 old_length = Vec_length (V);
00148 memcpy (new_Vec, V, old_length * elem_size);
00149 Vec_delete (V);
00150 }
00151 return (new_Vec);
00152 }
00153
00154
00155 Vec __Vec_new (size_t elem_size, idx_t N)
00156 {
00157 return (__Vec_new_alloc (elem_size, N, N));
00158 }
00159
00160
00161 vec vec_new (idx_t length)
00162 {
00163 return Vec_new (double, length);
00164 }
00165
00166
00167 ivec ivec_new (idx_t length)
00168 {
00169 return Vec_new (int, length);
00170 }
00171
00172
00173 bvec bvec_new (idx_t length)
00174 {
00175 return Vec_new (byte, length);
00176 }
00177
00178
00179 cvec cvec_new (idx_t length)
00180 {
00181 return Vec_new (cplx, length);
00182 }
00183
00184
00185 void vec_delete (vec v)
00186 {
00187 Vec_delete (v);
00188 }
00189
00190
00191 void ivec_delete (ivec v)
00192 {
00193 Vec_delete (v);
00194 }
00195
00196
00197 void bvec_delete (bvec v)
00198 {
00199 Vec_delete (v);
00200 }
00201
00202
00203 void cvec_delete (cvec v)
00204 {
00205 Vec_delete (v);
00206 }
00207
00208
00209
00210
00211
00212
00213 idx_t vec_length (vec v)
00214 {
00215 return Vec_length (v);
00216 }
00217
00218
00219 idx_t ivec_length (ivec v)
00220 {
00221 return Vec_length (v);
00222 }
00223
00224
00225 idx_t bvec_length (bvec v)
00226 {
00227 return Vec_length (v);
00228 }
00229
00230
00231 idx_t cvec_length (cvec v)
00232 {
00233 return Vec_length (v);
00234 }
00235
00236
00237 idx_t vec_length_max (vec v)
00238 {
00239 return Vec_length_max (v);
00240 }
00241
00242
00243 idx_t ivec_length_max (ivec v)
00244 {
00245 return Vec_length_max (v);
00246 }
00247
00248
00249 idx_t bvec_length_max (bvec v)
00250 {
00251 return Vec_length_max (v);
00252 }
00253
00254
00255 idx_t cvec_length_max (cvec v)
00256 {
00257 return Vec_length_max (v);
00258 }
00259
00260
00261 vec _vec_set_length_max (vec v, idx_t N)
00262 {
00263 Vec_set_length_max (v, N);
00264 return v;
00265 }
00266
00267
00268 ivec _ivec_set_length_max (ivec v, idx_t N)
00269 {
00270 Vec_set_length_max (v, N);
00271 return v;
00272 }
00273
00274
00275 bvec _bvec_set_length_max (bvec v, idx_t N)
00276 {
00277 Vec_set_length_max (v, N);
00278 return v;
00279 }
00280
00281
00282 cvec _cvec_set_length_max (cvec v, idx_t N)
00283 {
00284 Vec_set_length_max (v, N);
00285 return v;
00286 }
00287
00288
00289 vec _vec_set_length (vec v, idx_t N)
00290 {
00291 Vec_set_length (v, N);
00292 return v;
00293 }
00294
00295
00296 ivec _ivec_set_length (ivec v, idx_t N)
00297 {
00298 Vec_set_length (v, N);
00299 return v;
00300 }
00301
00302
00303 bvec _bvec_set_length (bvec v, idx_t N)
00304 {
00305 Vec_set_length (v, N);
00306 return v;
00307 }
00308
00309
00310 cvec _cvec_set_length (cvec v, idx_t N)
00311 {
00312 Vec_set_length (v, N);
00313 return v;
00314 }
00315
00316
00317
00318
00319
00320
00321 vec vec_init (vec v, double *buf, idx_t N)
00322 {
00323 return (vec) Vec_init (v, buf, N);
00324 }
00325
00326
00327 ivec ivec_init (ivec v, int *buf, idx_t N)
00328 {
00329 return (ivec) Vec_init (v, buf, N);
00330 }
00331
00332
00333 bvec bvec_init (bvec v, byte * buf, idx_t N)
00334 {
00335 return (bvec) Vec_init (v, buf, N);
00336 }
00337
00338
00339 cvec cvec_init (cvec v, cplx * buf, idx_t N)
00340 {
00341 return (cvec) Vec_init (v, buf, N);
00342 }
00343
00344
00345 vec vec_set (vec v, double val)
00346 {
00347 Vec_set (v, val);
00348 return v;
00349 }
00350
00351
00352 ivec ivec_set (ivec v, int val)
00353 {
00354 Vec_set (v, val);
00355 return v;
00356 }
00357
00358
00359 bvec bvec_set (bvec v, byte val)
00360 {
00361 Vec_set (v, val);
00362 return v;
00363 }
00364
00365
00366 cvec cvec_set (cvec v, cplx val)
00367 {
00368 Vec_set (v, val);
00369 return v;
00370 }
00371
00372
00373 vec vec_set_between (vec v, idx_t i1, idx_t i2, double val)
00374 {
00375 Vec_set_between (v, i1, i2, val);
00376 return v;
00377 }
00378
00379
00380 ivec ivec_set_between (ivec v, idx_t i1, idx_t i2, int val)
00381 {
00382 Vec_set_between (v, i1, i2, val);
00383 return v;
00384 }
00385
00386
00387 bvec bvec_set_between (bvec v, idx_t i1, idx_t i2, byte val)
00388 {
00389 Vec_set_between (v, i1, i2, val);
00390 return v;
00391 }
00392
00393
00394 cvec cvec_set_between (cvec v, idx_t i1, idx_t i2, cplx val)
00395 {
00396 Vec_set_between (v, i1, i2, val);
00397 return v;
00398 }
00399
00400
00401 void vec_set_subvector (vec v, vec s, idx_t idx)
00402 {
00403 VEC_END_PARAM (v, idx);
00404 Vec_set_subvector (v, s, idx);
00405 }
00406
00407
00408 void ivec_set_subvector (ivec v, ivec s, idx_t idx)
00409 {
00410 VEC_END_PARAM (v, idx);
00411 Vec_set_subvector (v, s, idx);
00412 }
00413
00414
00415 void bvec_set_subvector (bvec v, bvec s, idx_t idx)
00416 {
00417 VEC_END_PARAM (v, idx);
00418 Vec_set_subvector (v, s, idx);
00419 }
00420
00421
00422 void cvec_set_subvector (cvec v, cvec s, idx_t idx)
00423 {
00424 VEC_END_PARAM (v, idx);
00425 Vec_set_subvector (v, s, idx);
00426 }
00427
00428
00429 vec vec_get_subvector (vec v, idx_t i1, idx_t i2)
00430 {
00431 vec s;
00432 VEC_END_PARAM (v, i1);
00433 VEC_END_PARAM (v, i2);
00434 s = vec_new (i2 - i1 + 1);
00435 vec_init (s, v + i1, i2 - i1 + 1);
00436 return s;
00437 }
00438
00439
00440 ivec ivec_get_subvector (ivec v, idx_t i1, idx_t i2)
00441 {
00442 ivec s;
00443 VEC_END_PARAM (v, i1);
00444 VEC_END_PARAM (v, i2);
00445 s = ivec_new (i2 - i1 + 1);
00446 ivec_init (s, v + i1, i2 - i1 + 1);
00447 return s;
00448 }
00449
00450
00451 bvec bvec_get_subvector (bvec v, idx_t i1, idx_t i2)
00452 {
00453 bvec s;
00454 VEC_END_PARAM (v, i1);
00455 VEC_END_PARAM (v, i2);
00456 s = bvec_new (i2 - i1 + 1);
00457 bvec_init (s, v + i1, i2 - i1 + 1);
00458 return s;
00459 }
00460
00461
00462 cvec cvec_get_subvector (cvec v, idx_t i1, idx_t i2)
00463 {
00464 cvec s;
00465 VEC_END_PARAM (v, i1);
00466 VEC_END_PARAM (v, i2);
00467 s = cvec_new (i2 - i1 + 1);
00468 cvec_init (s, v + i1, i2 - i1 + 1);
00469 return s;
00470 }
00471
00472
00473
00474
00475
00476
00477
00478 Vec __Vec_copy (Vec v1, Vec v2)
00479 {
00480 assert (v1);
00481 assert (v2);
00482 assert (Vec_element_size (v1) == Vec_element_size (v2));
00483 assert (Vec_length (v1) == Vec_length (v2));
00484 memcpy (v1, v2, Vec_element_size (v1) * Vec_length (v1));
00485 return (v1);
00486 }
00487
00488
00489 void vec_copy (vec dest, vec orig)
00490 {
00491 Vec_copy (dest, orig);
00492 }
00493
00494
00495 void ivec_copy (ivec dest, ivec orig)
00496 {
00497 Vec_copy (dest, orig);
00498 }
00499
00500
00501 void bvec_copy (bvec dest, bvec orig)
00502 {
00503 Vec_copy (dest, orig);
00504 }
00505
00506
00507 void cvec_copy (cvec dest, cvec orig)
00508 {
00509 Vec_copy (dest, orig);
00510 }
00511
00512
00513 Vec Vec_clone (Vec v)
00514 {
00515 assert (v);
00516 return (Vec_copy (__Vec_new (Vec_element_size (v), Vec_length (v)), v));
00517 }
00518
00519
00520 vec vec_clone (vec v)
00521 {
00522 assert (v);
00523 return ((vec) Vec_copy (vec_new (vec_length (v)), v));
00524 }
00525
00526
00527 ivec ivec_clone (ivec v)
00528 {
00529 assert (v);
00530 return ((ivec) Vec_copy (ivec_new (ivec_length (v)), v));
00531 }
00532
00533
00534 bvec bvec_clone (bvec v)
00535 {
00536 assert (v);
00537 return ((bvec) Vec_copy (bvec_new (bvec_length (v)), v));
00538 }
00539
00540
00541 cvec cvec_clone (cvec v)
00542 {
00543 assert (v);
00544 return ((cvec) Vec_copy (cvec_new (cvec_length (v)), v));
00545 }
00546
00547
00548 void vec_copy_from_ivec (vec dest, ivec orig)
00549 {
00550 int i;
00551 assert (vec_length(dest) == ivec_length (orig));
00552
00553 for (i = 0 ; i < vec_length (dest) ; i++ )
00554 dest[i] = orig[i];
00555 }
00556
00557
00558 void vec_copy_from_bvec (vec dest, bvec orig)
00559 {
00560 int i;
00561 assert (vec_length(dest) == bvec_length (orig));
00562
00563 for (i = 0 ; i < vec_length (dest) ; i++ )
00564 dest[i] = orig[i];
00565 }
00566
00567
00568 void vec_copy_from_cvec (vec dest, cvec orig)
00569 {
00570 int i;
00571 assert (vec_length(dest) == cvec_length (orig));
00572
00573 for (i = 0 ; i < vec_length (dest) ; i++ )
00574 dest[i] = creal (orig[i]);
00575 }
00576
00577
00578 void ivec_copy_from_vec (ivec dest, vec orig)
00579 {
00580 int i;
00581 assert (ivec_length(dest) == vec_length (orig));
00582
00583 for (i = 0 ; i < ivec_length (dest) ; i++ )
00584 dest[i] = (int) orig[i];
00585 }
00586
00587
00588 void ivec_copy_from_bvec (ivec dest, bvec orig)
00589 {
00590 int i;
00591 assert (ivec_length(dest) == bvec_length (orig));
00592
00593 for (i = 0 ; i < ivec_length (dest) ; i++ )
00594 dest[i] = orig[i];
00595 }
00596
00597
00598 void ivec_copy_from_cvec (ivec dest, cvec orig)
00599 {
00600 int i;
00601 assert (ivec_length(dest) == cvec_length (orig));
00602
00603 for (i = 0 ; i < ivec_length (dest) ; i++ )
00604 dest[i] = (int) creal (orig[i]);
00605 }
00606
00607
00608 void bvec_copy_from_vec (bvec dest, vec orig)
00609 {
00610 int i;
00611 assert (bvec_length(dest) == vec_length (orig));
00612
00613 for (i = 0 ; i < bvec_length (dest) ; i++ )
00614 dest[i] = (byte) orig[i];
00615 }
00616
00617
00618 void bvec_copy_from_ivec (bvec dest, ivec orig)
00619 {
00620 int i;
00621 assert (bvec_length(dest) == ivec_length (orig));
00622
00623 for (i = 0 ; i < bvec_length (dest) ; i++ )
00624 dest[i] = (byte) orig[i];
00625 }
00626
00627
00628 void bvec_copy_from_cvec (bvec dest, cvec orig)
00629 {
00630 int i;
00631 assert (bvec_length(dest) == cvec_length (orig));
00632
00633 for (i = 0 ; i < bvec_length (dest) ; i++ )
00634 dest[i] = (byte) creal (orig[i]);
00635 }
00636
00637
00638
00639 void cvec_copy_from_vec (cvec dest, vec orig)
00640 {
00641 int i;
00642 assert (cvec_length(dest) == vec_length (orig));
00643
00644 for (i = 0 ; i < cvec_length (dest) ; i++ ) {
00645 dest[i].r = orig[i];
00646 dest[i].i = 0;
00647 }
00648 }
00649
00650
00651 void cvec_copy_from_ivec (cvec dest, ivec orig)
00652 {
00653 int i;
00654 assert (cvec_length(dest) == ivec_length (orig));
00655
00656 for (i = 0 ; i < cvec_length (dest) ; i++ ) {
00657 dest[i].r = (int) orig[i];
00658 dest[i].i = 0;
00659 }
00660 }
00661
00662
00663 void cvec_copy_from_bvec (cvec dest, bvec orig)
00664 {
00665 int i;
00666 assert (cvec_length(dest) == bvec_length (orig));
00667
00668 for (i = 0 ; i < cvec_length (dest) ; i++ ) {
00669 dest[i].r = (byte) orig[i];
00670 dest[i].i = 0;
00671 }
00672 }
00673
00674
00675
00676
00677
00678 void vec_copy_mem (double *buf, vec v)
00679 {
00680 idx_t i;
00681 assert (v);
00682 assert (buf);
00683 for (i = 0; i < vec_length (v); i++)
00684 buf[i] = v[i];
00685 }
00686
00687
00688
00689 void ivec_copy_mem (int *buf, ivec v)
00690 {
00691 idx_t i;
00692 assert (v);
00693 assert (buf);
00694 for (i = 0; i < ivec_length (v); i++)
00695 buf[i] = v[i];
00696 }
00697
00698
00699
00700 void bvec_copy_mem (byte * buf, bvec v)
00701 {
00702 idx_t i;
00703 assert (v);
00704 assert (buf);
00705 for (i = 0; i < bvec_length (v); i++)
00706 buf[i] = v[i];
00707 }
00708
00709
00710
00711 void cvec_copy_mem (cplx * buf, cvec v)
00712 {
00713 idx_t i;
00714 assert (v);
00715 assert (buf);
00716 for (i = 0; i < cvec_length (v); i++)
00717 buf[i] = v[i];
00718 }
00719
00720
00721 void bvec_pack (byte * buf, bvec v)
00722 {
00723 idx_t i, j;
00724 idx_t l = bvec_length (v);
00725 byte t;
00726 assert (v);
00727 assert (buf);
00728 for (i = 0; i < l / 8; i++) {
00729 t = 0;
00730 for (j = 0; j < 8; j++)
00731 t |= v[8 * i + j] << (7 - j);
00732 buf[i] = t;
00733 }
00734 t = 0;
00735 for (j = 0; j < l % 8; j++)
00736 t |= v[8 * i + j] << (7 - j);
00737 if (l % 8)
00738 buf[i] = t;
00739 }
00740
00741 void bvec_unpack (bvec v, byte * buf)
00742 {
00743 idx_t i, j;
00744 idx_t l = bvec_length (v);
00745 byte t = 0;
00746 assert (v);
00747 assert (buf);
00748 for (i = 0; i < l / 8; i++) {
00749 t = buf[i];
00750 for (j = 0; j < 8; j++)
00751 v[8 * i + j] = (t >> (7 - j)) & 1;
00752 }
00753 if (l % 8)
00754 t = buf[i];
00755
00756 for (j = 0; j < l % 8; j++)
00757 v[8 * i + j] = (t >> (7 - j)) & 1;
00758 }
00759
00760
00761
00762 vec ivec_to_vec (ivec v)
00763 {
00764 idx_t i;
00765 idx_t l = ivec_length (v);
00766 vec r = vec_new (l);
00767
00768 for (i = 0; i < l; i++)
00769 r[i] = (double) v[i];
00770
00771 return (r);
00772 }
00773
00774
00775 bvec ivec_to_bvec (ivec v)
00776 {
00777 idx_t i;
00778 idx_t l = ivec_length (v);
00779 bvec r = bvec_new (l);
00780
00781 for (i = 0; i < l; i++)
00782 r[i] = (byte) v[i];
00783
00784 return (r);
00785 }
00786
00787 cvec ivec_to_cvec (ivec v)
00788 {
00789 idx_t i;
00790 idx_t l = ivec_length (v);
00791 cvec r = cvec_new (l);
00792
00793 for (i = 0; i < l; i++) {
00794 creal (r[i]) = v[i];
00795 cimag (r[i]) = 0;
00796 }
00797
00798 return (r);
00799 }
00800
00801
00802
00803 ivec bvec_to_ivec (bvec v)
00804 {
00805 idx_t i;
00806 idx_t l = bvec_length (v);
00807 ivec r = ivec_new (l);
00808
00809 for (i = 0; i < l; i++)
00810 r[i] = (int) v[i];
00811
00812 return (r);
00813 }
00814
00815
00816 vec bvec_to_vec (bvec v)
00817 {
00818 idx_t i;
00819 idx_t l = bvec_length (v);
00820 vec r = vec_new (l);
00821
00822 for (i = 0; i < l; i++)
00823 r[i] = (double) v[i];
00824
00825 return (r);
00826 }
00827
00828 cvec bvec_to_cvec (bvec v)
00829 {
00830 idx_t i;
00831 idx_t l = bvec_length (v);
00832 cvec r = cvec_new (l);
00833
00834 for (i = 0; i < l; i++) {
00835 creal (r[i]) = v[i];
00836 cimag (r[i]) = 0;
00837 }
00838
00839 return (r);
00840 }
00841
00842
00843 ivec vec_to_ivec (vec v)
00844 {
00845 idx_t i;
00846 idx_t l = vec_length (v);
00847 ivec r = ivec_new (l);
00848
00849 for (i = 0; i < l; i++)
00850 r[i] = (int) v[i];
00851
00852 return (r);
00853 }
00854
00855
00856 bvec vec_to_bvec (vec v)
00857 {
00858 idx_t i;
00859 idx_t l = vec_length (v);
00860 bvec r = bvec_new (l);
00861
00862 for (i = 0; i < l; i++)
00863 r[i] = (byte) v[i];
00864
00865 return (r);
00866 }
00867
00868 cvec vec_to_cvec (vec v)
00869 {
00870 idx_t i;
00871 idx_t l = vec_length (v);
00872 cvec r = cvec_new (l);
00873
00874 for (i = 0; i < l; i++) {
00875 creal (r[i]) = v[i];
00876 cimag (r[i]) = 0;
00877 }
00878
00879 return (r);
00880 }
00881
00882
00883
00884
00885
00886
00887
00888 vec vec_del (vec v, idx_t pos)
00889 {
00890 Vec_del (v, pos);
00891 return v;
00892 }
00893
00894
00895 ivec ivec_del (ivec v, idx_t pos)
00896 {
00897 Vec_del (v, pos);
00898 return v;
00899 }
00900
00901
00902 bvec bvec_del (bvec v, idx_t pos)
00903 {
00904 Vec_del (v, pos);
00905 return v;
00906 }
00907
00908
00909 cvec cvec_del (cvec v, idx_t pos)
00910 {
00911 Vec_del (v, pos);
00912 return v;
00913 }
00914
00915
00916
00917 vec _vec_ins (vec v, idx_t pos, double elt)
00918 {
00919 Vec_ins (v, pos, elt);
00920 return v;
00921 }
00922
00923
00924 ivec _ivec_ins (ivec v, idx_t pos, int elt)
00925 {
00926 Vec_ins (v, pos, elt);
00927 return v;
00928 }
00929
00930
00931 bvec _bvec_ins (bvec v, idx_t pos, byte elt)
00932 {
00933 Vec_ins (v, pos, elt);
00934 return v;
00935 }
00936
00937
00938 cvec _cvec_ins (cvec v, idx_t pos, cplx elt)
00939 {
00940 Vec_ins (v, pos, elt);
00941 return v;
00942 }
00943
00944
00945 vec _vec_push (vec v, double elt)
00946 {
00947 vec_ins (v, vec_length (v), elt);
00948 return v;
00949 }
00950
00951
00952 ivec _ivec_push (ivec v, int elt)
00953 {
00954 ivec_ins (v, ivec_length (v), elt);
00955 return v;
00956 }
00957
00958
00959 bvec _bvec_push (bvec v, byte elt)
00960 {
00961 bvec_ins (v, bvec_length (v), elt);
00962 return v;
00963 }
00964
00965
00966 cvec _cvec_push (cvec v, cplx elt) {
00967 cvec_ins (v, cvec_length (v), elt);
00968 return v;
00969 }
00970
00971
00972 vec vec_pop (vec v)
00973 {
00974 Vec_pop (v);
00975 return v;
00976 }
00977
00978
00979 ivec ivec_pop (ivec v)
00980 {
00981 Vec_pop (v);
00982 return v;
00983 }
00984
00985
00986 bvec bvec_pop (bvec v)
00987 {
00988 Vec_pop (v);
00989 return v;
00990 }
00991
00992
00993 cvec cvec_pop (cvec v)
00994 {
00995 Vec_pop (v);
00996 return v;
00997 }
00998
00999
01000 double vec_head (vec v)
01001 {
01002 return Vec_head (v);
01003 }
01004
01005
01006 int ivec_head (ivec v)
01007 {
01008 return Vec_head (v);
01009 }
01010
01011
01012 byte bvec_head (bvec v)
01013 {
01014 return Vec_head (v);
01015 }
01016
01017
01018 cplx cvec_head (const cvec v)
01019 {
01020 return Vec_head (v);
01021 }
01022
01023
01024
01025
01026
01027
01028 double *__vec (vec v, idx_t i)
01029 {
01030 assert (v);
01031 VEC_END_PARAM (v, i);
01032 assert (i >= 0 && i < vec_length (v));
01033 return (&v[i]);
01034 }
01035
01036
01037 int *__ivec (ivec v, idx_t i)
01038 {
01039 assert (v);
01040 VEC_END_PARAM (v, i);
01041 assert (i >= 0 && i < ivec_length (v));
01042 return (&v[i]);
01043 }
01044
01045
01046 byte *__bvec (bvec v, idx_t i)
01047 {
01048 assert (v);
01049 VEC_END_PARAM (v, i);
01050 assert (i >= 0 && i < bvec_length (v));
01051 return (&v[i]);
01052 }
01053
01054
01055 cplx *__cvec (cvec v, idx_t i)
01056 {
01057 assert (v);
01058 VEC_END_PARAM (v, i);
01059 assert (i >= 0 && i < cvec_length (v));
01060 return (&v[i]);
01061 }
01062
01063
01064
01065
01066
01067
01068 int vec_eq (vec v1, vec v2)
01069 {
01070 idx_t i;
01071 assert (v1);
01072 assert (v2);
01073
01074 if (vec_length (v1) != vec_length (v2))
01075 return 0;
01076
01077 for (i = 0; i < vec_length (v1); i++)
01078 if (v1[i] != v2[i])
01079 return 0;
01080 return 1;
01081 }
01082
01083
01084
01085 int ivec_eq (ivec v1, ivec v2)
01086 {
01087 idx_t i;
01088 assert (v1);
01089 assert (v2);
01090
01091 if (ivec_length (v1) != ivec_length (v2))
01092 return 0;
01093
01094 for (i = 0; i < ivec_length (v1); i++)
01095 if (v1[i] != v2[i])
01096 return 0;
01097 return 1;
01098 }
01099
01100
01101
01102 int bvec_eq (bvec v1, bvec v2)
01103 {
01104 idx_t i;
01105 assert (v1);
01106 assert (v2);
01107
01108 if (bvec_length (v1) != bvec_length (v2))
01109 return 0;
01110
01111 for (i = 0; i < bvec_length (v1); i++)
01112 if (v1[i] != v2[i])
01113 return 0;
01114 return 1;
01115 }
01116
01117
01118
01119 int cvec_eq (cvec v1, cvec v2)
01120 {
01121 idx_t i;
01122 assert (v1);
01123 assert (v2);
01124
01125 if (cvec_length (v1) != cvec_length (v2))
01126 return 0;
01127
01128 for (i = 0; i < cvec_length (v1); i++)
01129 if (!ceq (v1[i], v2[i]))
01130 return 0;
01131 return 1;
01132 }
01133
01134
01135
01136 int vec_geq (vec v1, vec v2)
01137 {
01138 idx_t i, minl;
01139 assert (v1);
01140 assert (v2);
01141 minl =
01142 (vec_length (v1) > vec_length (v2) ? vec_length (v2) : vec_length (v1));
01143
01144 for (i = 0; i < minl; i++)
01145 if (v1[i] > v2[i])
01146 return 0;
01147
01148 if (vec_length (v1) >= vec_length (v2))
01149 return 1;
01150 else
01151 return 0;
01152 }
01153
01154
01155
01156 int ivec_geq (ivec v1, ivec v2)
01157 {
01158 idx_t i, minl;
01159 assert (v1);
01160 assert (v2);
01161 minl =
01162 (ivec_length (v1) >
01163 ivec_length (v2) ? ivec_length (v2) : ivec_length (v1));
01164
01165 for (i = 0; i < minl; i++)
01166 if (v1[i] > v2[i])
01167 return 0;
01168
01169 if (ivec_length (v1) >= ivec_length (v2))
01170 return 1;
01171 else
01172 return 0;
01173 }
01174
01175
01176
01177 int bvec_geq (bvec v1, bvec v2)
01178 {
01179 idx_t i, minl;
01180 assert (v1);
01181 assert (v2);
01182 minl =
01183 (bvec_length (v1) >
01184 bvec_length (v2) ? bvec_length (v2) : bvec_length (v1));
01185
01186 for (i = 0; i < minl; i++)
01187 if (v1[i] > v2[i])
01188 return 0;
01189
01190 if (bvec_length (v1) >= bvec_length (v2))
01191 return 1;
01192 else
01193 return 0;
01194 }
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205 void vec_incr (vec v, double a)
01206 {
01207 idx_t i;
01208 assert (v);
01209 for (i = 0; i < vec_length (v); i++)
01210 v[i] += a;
01211 }
01212
01213
01214 void vec_decr (vec v, double a)
01215 {
01216 idx_t i;
01217 assert (v);
01218 for (i = 0; i < vec_length (v); i++)
01219 v[i] -= a;
01220 }
01221
01222
01223 void vec_mul_by (vec v, double a)
01224 {
01225 idx_t i;
01226 assert (v);
01227 for (i = 0; i < vec_length (v); i++)
01228 v[i] *= a;
01229 }
01230
01231
01232 void vec_div_by (vec v, double a)
01233 {
01234 idx_t i;
01235 assert (v);
01236 assert (a);
01237 a = 1 / a;
01238 for (i = 0; i < vec_length (v); i++)
01239 v[i] *= a;
01240 }
01241
01242
01243
01244 void ivec_incr (ivec v, int a)
01245 {
01246 idx_t i;
01247 assert (v);
01248 for (i = 0; i < ivec_length (v); i++)
01249 v[i] += a;
01250 }
01251
01252
01253 void ivec_decr (ivec v, int a)
01254 {
01255 idx_t i;
01256 assert (v);
01257 for (i = 0; i < ivec_length (v); i++)
01258 v[i] -= a;
01259 }
01260
01261
01262 void ivec_mul_by (ivec v, int a)
01263 {
01264 idx_t i;
01265 assert (v);
01266 for (i = 0; i < ivec_length (v); i++)
01267 v[i] *= a;
01268 }
01269
01270
01271 void ivec_div_by (ivec v, int a)
01272 {
01273 idx_t i;
01274 assert (v);
01275 assert (a);
01276 for (i = 0; i < ivec_length (v); i++)
01277 v[i] /= a;
01278 }
01279
01280
01281
01282 void cvec_incr_real (cvec v, double a)
01283 {
01284 idx_t i;
01285 assert (v);
01286 for (i = 0; i < cvec_length (v); i++)
01287 creal (v[i]) += a;
01288 }
01289
01290
01291 void cvec_decr_real (cvec v, double a)
01292 {
01293 idx_t i;
01294 assert (v);
01295 for (i = 0; i < cvec_length (v); i++)
01296 creal (v[i]) -= a;
01297 }
01298
01299
01300 void cvec_mul_by_real (cvec v, double a)
01301 {
01302 idx_t i;
01303 assert (v);
01304 for (i = 0; i < cvec_length (v); i++) {
01305 v[i].r *= a;
01306 v[i].i *= a;
01307 }
01308 }
01309
01310
01311 void cvec_div_by_real (cvec v, double a)
01312 {
01313 idx_t i;
01314 assert (v);
01315 assert (a);
01316 a = 1 / a;
01317 for (i = 0; i < cvec_length (v); i++) {
01318 v[i].r *= a;
01319 v[i].i *= a;
01320 }
01321 }
01322
01323
01324 void cvec_conj (cvec v)
01325 {
01326 idx_t i;
01327 assert (v);
01328 for (i = 0; i < cvec_length (v); i++)
01329 cconj (v[i]);
01330 }
01331
01332 void cvec_incr (cvec v, cplx a)
01333 {
01334 idx_t i;
01335 assert (v);
01336 for (i = 0; i < cvec_length (v); i++)
01337 v[i] = cadd (v[i], a);
01338 }
01339
01340
01341 void cvec_decr (cvec v, cplx a)
01342 {
01343 idx_t i;
01344 assert (v);
01345 for (i = 0; i < cvec_length (v); i++)
01346 v[i] = csub (v[i], a);
01347 }
01348
01349
01350 void cvec_mul_by (cvec v, cplx a)
01351 {
01352 idx_t i;
01353 assert (v);
01354 for (i = 0; i < cvec_length (v); i++)
01355 v[i] = cmul (v[i], a);
01356 }
01357
01358
01359 void cvec_div_by (cvec v, cplx a)
01360 {
01361 idx_t i;
01362 assert (v);
01363 assert (!ceq (a, cplx_0));
01364 a = cinv (a);
01365 for (i = 0; i < cvec_length (v); i++)
01366 v[i] = cmul (v[i], a);
01367 }
01368
01369
01370
01371
01372 void vec_add (vec v1, vec v2)
01373 {
01374 idx_t i;
01375 assert (v1);
01376 assert (v2);
01377 assert (vec_length (v1) == vec_length (v2));
01378 for (i = 0; i < vec_length (v1); i++)
01379 v1[i] += v2[i];
01380 }
01381
01382
01383 void vec_sub (vec v1, vec v2)
01384 {
01385 idx_t i;
01386 assert (v1);
01387 assert (v2);
01388 assert (vec_length (v1) == vec_length (v2));
01389 for (i = 0; i < vec_length (v1); i++)
01390 v1[i] -= v2[i];
01391 }
01392
01393
01394 void vec_mul (vec v1, vec v2)
01395 {
01396 idx_t i;
01397 assert (v1);
01398 assert (v2);
01399 assert (vec_length (v1) == vec_length (v2));
01400 for (i = 0; i < vec_length (v1); i++)
01401 v1[i] *= v2[i];
01402 }
01403
01404
01405 void vec_div (vec v1, vec v2)
01406 {
01407 idx_t i;
01408 assert (v1);
01409 assert (v2);
01410 assert (vec_length (v1) == vec_length (v2));
01411 for (i = 0; i < vec_length (v1); i++)
01412 v1[i] /= v2[i];
01413 }
01414
01415
01416 void ivec_add (ivec v1, ivec v2)
01417 {
01418 idx_t i;
01419 assert (v1);
01420 assert (v2);
01421 assert (ivec_length (v1) == ivec_length (v2));
01422 for (i = 0; i < ivec_length (v1); i++)
01423 v1[i] += v2[i];
01424 }
01425
01426
01427 void ivec_sub (ivec v1, ivec v2)
01428 {
01429 idx_t i;
01430 assert (v1);
01431 assert (v2);
01432 assert (ivec_length (v1) == ivec_length (v2));
01433 for (i = 0; i < ivec_length (v1); i++)
01434 v1[i] -= v2[i];
01435 }
01436
01437
01438 void ivec_mul (ivec v1, ivec v2)
01439 {
01440 idx_t i;
01441 assert (v1);
01442 assert (v2);
01443 assert (ivec_length (v1) == ivec_length (v2));
01444 for (i = 0; i < ivec_length (v1); i++)
01445 v1[i] *= v2[i];
01446 }
01447
01448
01449 void ivec_div (ivec v1, ivec v2)
01450 {
01451 idx_t i;
01452 assert (v1);
01453 assert (v2);
01454 assert (ivec_length (v1) == ivec_length (v2));
01455 for (i = 0; i < ivec_length (v1); i++)
01456 v1[i] /= v2[i];
01457 }
01458
01459
01460 void cvec_add (cvec v1, cvec v2)
01461 {
01462 idx_t i;
01463 assert (v1);
01464 assert (v2);
01465 assert (cvec_length (v1) == cvec_length (v2));
01466 for (i = 0; i < cvec_length (v1); i++)
01467 v1[i] = cadd (v1[i], v2[i]);
01468 }
01469
01470
01471 void cvec_sub (cvec v1, cvec v2)
01472 {
01473 idx_t i;
01474 assert (v1);
01475 assert (v2);
01476 assert (cvec_length (v1) == cvec_length (v2));
01477 for (i = 0; i < cvec_length (v1); i++)
01478 v1[i] = csub (v1[i], v2[i]);
01479 }
01480
01481
01482 void cvec_mul (cvec v1, cvec v2)
01483 {
01484 idx_t i;
01485 assert (v1);
01486 assert (v2);
01487 assert (cvec_length (v1) == cvec_length (v2));
01488 for (i = 0; i < cvec_length (v1); i++)
01489 v1[i] = cmul (v1[i], v2[i]);
01490 }
01491
01492
01493 void cvec_div (cvec v1, cvec v2)
01494 {
01495 idx_t i;
01496 assert (v1);
01497 assert (v2);
01498 assert (cvec_length (v1) == cvec_length (v2));
01499 for (i = 0; i < cvec_length (v1); i++)
01500 v1[i] = cdiv (v1[i], v2[i]);
01501 }
01502
01503
01504 void cvec_conj_mul (cvec v1, cvec v2)
01505 {
01506 idx_t i;
01507 assert (v1);
01508 assert (v2);
01509 assert (cvec_length (v1) == cvec_length (v2));
01510 for (i = 0; i < cvec_length (v1); i++)
01511 v1[i] = cmul (v1[i], cconj (v2[i]));
01512 }
01513
01514
01515 vec vec_new_add (vec v1, vec v2)
01516 {
01517 vec r = vec_clone (v1);
01518 vec_add (r, v2);
01519 return r;
01520 }
01521
01522
01523 vec vec_new_sub (vec v1, vec v2)
01524 {
01525 vec r = vec_clone (v1);
01526 vec_sub (r, v2);
01527 return r;
01528 }
01529
01530
01531 vec vec_new_mul (vec v1, vec v2)
01532 {
01533 vec r = vec_clone (v1);
01534 vec_mul (r, v2);
01535 return r;
01536 }
01537
01538
01539 vec vec_new_div (vec v1, vec v2)
01540 {
01541 vec r = vec_clone (v1);
01542 vec_div (r, v2);
01543 return r;
01544 }
01545
01546
01547 ivec ivec_new_add (ivec v1, ivec v2)
01548 {
01549 ivec r = ivec_clone (v1);
01550 ivec_add (r, v2);
01551 return r;
01552 }
01553
01554
01555 ivec ivec_new_sub (ivec v1, ivec v2)
01556 {
01557 ivec r = ivec_clone (v1);
01558 ivec_sub (r, v2);
01559 return r;
01560 }
01561
01562
01563 ivec ivec_new_mul (ivec v1, ivec v2)
01564 {
01565 ivec r = ivec_clone (v1);
01566 ivec_mul (r, v2);
01567 return r;
01568 }
01569
01570
01571 ivec ivec_new_div (ivec v1, ivec v2)
01572 {
01573 ivec r = ivec_clone (v1);
01574 ivec_div (r, v2);
01575 return r;
01576 }
01577
01578
01579
01580 double vec_inner_product (vec v1, vec v2)
01581 {
01582 double p = 0;
01583 idx_t i;
01584 assert (v1);
01585 assert (v2);
01586 assert (vec_length (v1) == vec_length (v2));
01587 for (i = 0; i < vec_length (v1); i++)
01588 p += v1[i] * v2[i];
01589 return p;
01590 }
01591
01592
01593 double vec_inner_product_robust (vec v1, vec v2)
01594 {
01595 idx_t i;
01596 double p;
01597 double c = 0., t, y;
01598 assert (v1);
01599 assert (v2);
01600 assert (vec_length (v1) == vec_length (v2));
01601 p = v1[0]*v2[0];
01602 for ( i= 1; i< vec_length (v1); i++ )
01603 {
01604 y = v1[i]*v2[i]-c;
01605 t = p+y;
01606 c = (t-p) - y;
01607 p = t;
01608 }
01609 return p;
01610 }
01611
01612
01613
01614 int ivec_inner_product (ivec v1, ivec v2)
01615 {
01616 int p = 0;
01617 idx_t i;
01618 assert (v1);
01619 assert (v2);
01620 assert (ivec_length (v1) == ivec_length (v2));
01621 for (i = 0; i < ivec_length (v1); i++)
01622 p += v1[i] * v2[i];
01623 return p;
01624 }
01625
01626
01627
01628 double vecivec_inner_product (vec v1, ivec v2)
01629 {
01630 double p = 0;
01631 idx_t i;
01632 assert (v1);
01633 assert (v2);
01634 assert (vec_length (v1) == ivec_length (v2));
01635 for (i = 0; i < vec_length (v1); i++)
01636 p += v1[i] * v2[i];
01637 return p;
01638 }
01639
01640
01641
01642 int bvecivec_inner_product (bvec v1, ivec v2)
01643 {
01644 int p = 0;
01645 idx_t i;
01646 assert (v1);
01647 assert (v2);
01648 assert (bvec_length (v1) == ivec_length (v2));
01649 for (i = 0; i < bvec_length (v1); i++)
01650 p += v1[i] * v2[i];
01651 return p;
01652 }
01653
01654
01655
01656 void vec_neg (vec v)
01657 {
01658 idx_t i;
01659 assert (v);
01660 for (i = 0; i < vec_length (v); i++)
01661 v[i] = -v[i];
01662 }
01663
01664
01665 void ivec_neg (ivec v)
01666 {
01667 idx_t i;
01668 assert (v);
01669 for (i = 0; i < ivec_length (v); i++)
01670 v[i] = -v[i];
01671 }
01672
01673
01674 void cvec_neg (cvec v)
01675 {
01676 idx_t i;
01677 assert (v);
01678 for (i = 0; i < cvec_length (v); i++)
01679 v[i] = cneg (v[i]);
01680 }
01681
01682
01683 void cvec_abssqr (cvec v)
01684 {
01685 idx_t i;
01686 double re, im;
01687 assert (v);
01688 for (i = 0; i < cvec_length (v); i++) {
01689 re = creal (v[i]);
01690 im = cimag (v[i]);
01691 creal (v[i]) = re * re + im * im;
01692 cimag (v[i]) = 0;
01693 }
01694 }
01695
01696 void vec_sqr (vec v)
01697 {
01698 idx_t i;
01699 assert (v);
01700 for (i = 0; i < vec_length (v); i++)
01701 v[i] *= v[i];
01702 }
01703
01704
01705 void ivec_sqr (ivec v)
01706 {
01707 idx_t i;
01708 assert (v);
01709 for (i = 0; i < ivec_length (v); i++)
01710 v[i] *= v[i];
01711 }
01712
01713
01714 void vec_sqrt (vec v)
01715 {
01716 idx_t i;
01717 assert (v);
01718 for (i = 0; i < Vec_length (v); i++)
01719 v[i] = v[i]<0?v[i]:sqrt (v[i]);
01720 }
01721
01722
01723 void vec_log (vec v)
01724 {
01725 idx_t i;
01726 assert (v);
01727 for (i = 0; i < Vec_length (v); i++)
01728 v[i] = log (v[i]);
01729 }
01730
01731
01732 void vec_log10 (vec v)
01733 {
01734 idx_t i;
01735 assert (v);
01736 for (i = 0; i < Vec_length (v); i++)
01737 v[i] = log10 (v[i]);
01738 }
01739
01740
01741 void vec_exp (vec v)
01742 {
01743 idx_t i;
01744 assert (v);
01745 for (i = 0; i < Vec_length (v); i++)
01746 v[i] = exp (v[i]);
01747 }
01748
01749
01750 void vec_abs (vec v)
01751 {
01752 idx_t i;
01753 assert (v);
01754 for (i = 0; i < vec_length (v); i++)
01755 if (v[i] < 0)
01756 v[i] = -(v[i]);
01757 }
01758
01759
01760 void ivec_abs (ivec v)
01761 {
01762 idx_t i;
01763 assert (v);
01764 for (i = 0; i < ivec_length (v); i++)
01765 if (v[i] < 0)
01766 v[i] = -(v[i]);
01767 }
01768
01769
01770 vec vec_new_abs (vec v)
01771 {
01772 vec r = vec_new (vec_length (v));
01773 vec_abs (r);
01774 return r;
01775 }
01776
01777
01778 ivec ivec_new_abs (ivec v)
01779 {
01780 ivec r = ivec_new (ivec_length (v));
01781 ivec_abs (r);
01782 return r;
01783 }
01784
01785
01786 vec cvec_new_abs (cvec v)
01787 {
01788 idx_t i;
01789 vec va;
01790 assert (v);
01791 va = vec_new (cvec_length (v));
01792 for (i = 0; i < cvec_length (v); i++)
01793 va[i] = cnorm (v[i]);
01794 return va;
01795 }
01796
01797
01798 void vec_pow (vec v, double a)
01799 {
01800 idx_t i;
01801 assert (v);
01802 for (i = 0; i < Vec_length (v); i++)
01803 v[i] = pow (v[i], a);
01804 }
01805
01806
01807 void vec_normalize (vec v, double nr)
01808 {
01809 idx_t i;
01810 double s = 0;
01811 assert (v);
01812
01813
01814 if (nr == 1)
01815 s = vec_sum (v);
01816 else {
01817 for (i = 0; i < vec_length (v); i++)
01818 s += pow (fabs (v[i]), nr);
01819 s = pow (s, 1.0 / nr);
01820 }
01821
01822 if (s == 0)
01823 return;
01824
01825 for (i = 0; i < vec_length (v); i++)
01826 v[i] /= s;
01827 }
01828
01829
01830 vec vec_new_pow (vec v, double a)
01831 {
01832 idx_t i;
01833 assert (v);
01834 vec r = vec_new (vec_length (v));
01835
01836 for (i = 0; i < Vec_length (v); i++)
01837 r[i] = pow (v[i], a);
01838
01839 return r;
01840 }
01841
01842
01843 vec vec_new_normalize (vec v, double nr)
01844 {
01845 idx_t i;
01846 double s = 0;
01847 assert (v);
01848 vec r = vec_new (vec_length (v));
01849
01850
01851 if (nr == 1)
01852 s = vec_sum (v);
01853 else {
01854 for (i = 0; i < vec_length (v); i++)
01855 s += pow (fabs (v[i]), nr);
01856 s = pow (s, 1.0 / nr);
01857 }
01858
01859 if (s == 0)
01860 return vec_new_set (1. / vec_length (v), vec_length (v));
01861
01862 for (i = 0; i < vec_length (v); i++)
01863 r[i] = v[i] / s;
01864
01865 return r;
01866 }
01867
01868
01869 int ivec_min (ivec v)
01870 {
01871 idx_t i;
01872 int m = INT_MAX;
01873 assert (v);
01874 for (i = 0; i < Vec_length (v); i++)
01875 if (v[i] < m)
01876 m = v[i];
01877 return m;
01878 }
01879
01880
01881 int ivec_max (ivec v)
01882 {
01883 idx_t i;
01884 int m = INT_MIN;
01885 assert (v);
01886 for (i = 0; i < Vec_length (v); i++)
01887 if (v[i] > m)
01888 m = v[i];
01889 return m;
01890 }
01891
01892 idx_t ivec_min_index (ivec v)
01893 {
01894 idx_t i, mi = NULL_INDEX;
01895 int m = INT_MAX;
01896 assert (v);
01897 for (i = 0; i < ivec_length (v); i++)
01898 if (v[i] < m) {
01899 m = v[i];
01900 mi = i;
01901 }
01902 return mi;
01903 }
01904
01905
01906 idx_t ivec_max_index (ivec v)
01907 {
01908 idx_t i, mi = NULL_INDEX;
01909 int m = INT_MIN;
01910 assert (v);
01911 for (i = 0; i < ivec_length (v); i++)
01912 if (v[i] > m) {
01913 m = v[i];
01914 mi = i;
01915 }
01916 return mi;
01917 }
01918
01919
01920 double ivec_mean (ivec v)
01921 {
01922 assert (v);
01923 return ivec_sum (v) / (double) ivec_length (v);
01924 }
01925
01926
01927
01928 double vec_sum (vec v)
01929 {
01930 idx_t i;
01931 double s = 0;
01932 assert (v);
01933 for (i = 0; i < Vec_length (v); i++)
01934 s += v[i];
01935 return s;
01936 }
01937
01938
01939 double vec_sum_robust(vec v)
01940 {
01941
01942 idx_t i;
01943 double s;
01944 double c = 0., t, y;
01945 assert (v);
01946 s = v[0];
01947 for ( i= 1; i< Vec_length (v); i++ )
01948 {
01949 y = v[i]-c;
01950 t = s+y;
01951 c = (t-s) - y;
01952 s = t;
01953 }
01954 return s;
01955 }
01956
01957
01958 int ivec_sum (ivec v)
01959 {
01960 idx_t i;
01961 int s = 0;
01962 assert (v);
01963 for (i = 0; i < ivec_length (v); i++) {
01964 if (v[i] < 0 && s < INT_MIN - v[i])
01965 it_warning ("underflow in ivec_sum");
01966 if (v[i] > 0 && s > INT_MAX - v[i])
01967 it_warning ("overflow in ivec_sum");
01968 s += v[i];
01969 }
01970 return s;
01971 }
01972
01973
01974 cplx cvec_sum (cvec v)
01975 {
01976 idx_t i;
01977 cplx s = cplx_0;
01978 assert (v);
01979 for (i = 0; i < cvec_length (v); i++)
01980 s = cadd (s, v[i]);
01981 return s;
01982 }
01983
01984
01985 vec vec_cum_sum (vec v)
01986 {
01987 vec cs = vec_new (vec_length (v));
01988 idx_t i;
01989 assert (v);
01990
01991 if (vec_length (v) > 0)
01992 cs[0] = v[0];
01993
01994 for (i = 1; i < Vec_length (v); i++)
01995 cs[i] = cs[i - 1] + v[i];
01996
01997 return cs;
01998 }
01999
02000
02001 ivec ivec_cum_sum (ivec v)
02002 {
02003 ivec cs = ivec_new (ivec_length (v));
02004 idx_t i;
02005 assert (v);
02006
02007 if (ivec_length (v) > 0)
02008 cs[0] = v[0];
02009
02010 for (i = 1; i < Vec_length (v); i++) {
02011 if (v[i] < 0 && cs[i - 1] < INT_MIN - v[i])
02012 it_warning ("underflow in vec_cum_sum");
02013 if (v[i] > 0 && cs[i - 1] > INT_MAX - v[i])
02014 it_warning ("overflow in vec_cum_sum");
02015 cs[i] = cs[i - 1] + v[i];
02016 }
02017 return cs;
02018 }
02019
02020
02021 cvec cvec_cum_sum (cvec v)
02022 {
02023 cvec cs = cvec_new (cvec_length (v));
02024 idx_t i;
02025 assert (v);
02026
02027 if (cvec_length (v) > 0)
02028 cs[0] = v[0];
02029
02030 for (i = 1; i < cvec_length (v); i++) {
02031 cs[i] = cadd (cs[i - 1], v[i]);
02032 }
02033 return cs;
02034 }
02035
02036
02037 double vec_sum_sqr (vec v)
02038 {
02039 idx_t i;
02040 double s = 0;
02041 assert (v);
02042 for (i = 0; i < Vec_length (v); i++)
02043 s += v[i] * v[i];
02044 return s;
02045 }
02046
02047
02048 double vec_sum_between (vec v, idx_t i1, idx_t i2)
02049 {
02050 idx_t i;
02051 double s = 0;
02052 VEC_END_PARAM (v, i2);
02053 assert (v);
02054 for (i = i1; i <= i2; i++)
02055 s += v[i];
02056 return s;
02057 }
02058
02059
02060 int ivec_sum_between (ivec v, idx_t i1, idx_t i2)
02061 {
02062 idx_t i;
02063 int s = 0;
02064 VEC_END_PARAM (v, i2);
02065 assert (v);
02066 for (i = i1; i <= i2; i++) {
02067 if (v[i] < 0 && s < INT_MIN - v[i])
02068 it_warning ("underflow in ivec_sum");
02069 if (v[i] > 0 && s > INT_MAX - v[i])
02070 it_warning ("overflow in ivec_sum");
02071 s += v[i];
02072 }
02073 return s;
02074 }
02075
02076
02077 cplx cvec_sum_between (cvec v, idx_t i1, idx_t i2)
02078 {
02079 idx_t i;
02080 cplx s = cplx_0;
02081 VEC_END_PARAM (v, i2);
02082 assert (v);
02083 for (i = i1; i <= i2; i++)
02084 s = cadd (s, v[i]);
02085 return s;
02086 }
02087
02088
02089 double vec_min (vec v)
02090 {
02091 idx_t i;
02092 double m = HUGE_VAL;
02093 assert (v);
02094 for (i = 0; i < Vec_length (v); i++)
02095 if (v[i] < m)
02096 m = v[i];
02097 return m;
02098 }
02099
02100
02101 double vec_max (vec v)
02102 {
02103 idx_t i;
02104 double m = -HUGE_VAL;
02105 assert (v);
02106 for (i = 0; i < Vec_length (v); i++)
02107 if (v[i] > m)
02108 m = v[i];
02109 return m;
02110 }
02111
02112
02113 idx_t vec_min_index (vec v)
02114 {
02115 idx_t i, mi = NULL_INDEX;
02116 double m = HUGE_VAL;
02117 assert (v);
02118 for (i = 0; i < vec_length (v); i++)
02119 if (v[i] < m) {
02120 m = v[i];
02121 mi = i;
02122 }
02123 return mi;
02124 }
02125
02126
02127 idx_t vec_max_index (vec v)
02128 {
02129 idx_t i, mi = NULL_INDEX;
02130 double m = -HUGE_VAL;
02131 assert (v);
02132 for (i = 0; i < vec_length (v); i++)
02133 if (v[i] > m) {
02134 m = v[i];
02135 mi = i;
02136 }
02137 return mi;
02138 }
02139
02140
02141
02142 vec vec_k_min_between (vec v, int k, idx_t a, idx_t b)
02143 {
02144 vec r, v1, v2;
02145 idx_t i1 = 0, i2 = 0;
02146 int lmax;
02147
02148 if (a > b)
02149 return vec_new_void();
02150
02151 if (a == b) {
02152 r = vec_new (1);
02153 r[0] = v[a];
02154 return r;
02155 }
02156
02157 v1 = vec_k_min_between (v, k, a, (a + b) / 2);
02158 v2 = vec_k_min_between (v, k, (a + b) / 2 + 1, b);
02159
02160 lmax = vec_length (v1) + vec_length (v2);
02161 if (lmax > k)
02162 lmax = k;
02163
02164 r = vec_new_alloc (0, lmax);
02165
02166
02167 while (i1 + i2 < lmax) {
02168 if (i1 == vec_length (v1))
02169 vec_push (r, v2[i2++]);
02170 else if (i2 == vec_length (v2))
02171 vec_push (r, v1[i1++]);
02172 else if (v1[i1] < v2[i2])
02173 vec_push (r, v1[i1++]);
02174 else
02175 vec_push (r, v2[i2++]);
02176 }
02177
02178 vec_delete (v1);
02179 vec_delete (v2);
02180 return r;
02181 }
02182
02183
02184 vec vec_k_min (vec v, int k)
02185 {
02186 return vec_k_min_between (v, k, 0, vec_length (v) - 1);
02187 }
02188
02189
02190
02191 vec vec_k_max_between (vec v, int k, idx_t a, idx_t b)
02192 {
02193 vec r, v1, v2;
02194 idx_t i1 = 0, i2 = 0;
02195 int lmax;
02196
02197 if (a > b)
02198 return vec_new_void();
02199
02200 if (a == b) {
02201 r = vec_new (1);
02202 r[0] = v[a];
02203 return r;
02204 }
02205
02206 v1 = vec_k_max_between (v, k, a, (a + b) / 2);
02207 v2 = vec_k_max_between (v, k, (a + b) / 2 + 1, b);
02208
02209 lmax = vec_length (v1) + vec_length (v2);
02210 if (lmax > k)
02211 lmax = k;
02212
02213 r = vec_new_alloc (0, lmax);
02214
02215
02216 while (i1 + i2 < lmax) {
02217 if (i1 >= vec_length (v1))
02218 vec_push (r, v2[i2++]);
02219 else if (i2 >= vec_length (v2))
02220 vec_push (r, v1[i1++]);
02221 else if (v1[i1] > v2[i2])
02222 vec_push (r, v1[i1++]);
02223 else
02224 vec_push (r, v2[i2++]);
02225 }
02226 vec_delete (v1);
02227 vec_delete (v2);
02228 return r;
02229 }
02230
02231
02232 vec vec_k_max (vec v, int k)
02233 {
02234 return vec_k_max_between (v, k, 0, vec_length (v) - 1);
02235 }
02236
02237
02238
02239 ivec ivec_k_min_between (ivec v, int k, idx_t a, idx_t b)
02240 {
02241 ivec r, v1, v2;
02242 idx_t i1 = 0, i2 = 0;
02243 int lmax;
02244
02245 if (a > b)
02246 return ivec_new_void();
02247
02248 if (a == b) {
02249 r = ivec_new (1);
02250 r[0] = v[a];
02251 return r;
02252 }
02253
02254 v1 = ivec_k_min_between (v, k, a, (a + b) / 2);
02255 v2 = ivec_k_min_between (v, k, (a + b) / 2 + 1, b);
02256
02257 lmax = ivec_length (v1) + ivec_length (v2);
02258 if (lmax > k)
02259 lmax = k;
02260
02261 r = ivec_new_alloc (0, lmax);
02262
02263
02264 while (i1 + i2 < lmax) {
02265 if (i1 == ivec_length (v1))
02266 ivec_push (r, v2[i2++]);
02267 else if (i2 == ivec_length (v2))
02268 ivec_push (r, v1[i1++]);
02269 else if (v1[i1] < v2[i2])
02270 ivec_push (r, v1[i1++]);
02271 else
02272 ivec_push (r, v2[i2++]);
02273 }
02274
02275 ivec_delete (v1);
02276 ivec_delete (v2);
02277 return r;
02278 }
02279
02280
02281 ivec ivec_k_min (ivec v, int k)
02282 {
02283 return ivec_k_min_between (v, k, 0, ivec_length (v) - 1);
02284 }
02285
02286
02287
02288 ivec ivec_k_max_between (ivec v, int k, idx_t a, idx_t b)
02289 {
02290 ivec r, v1, v2;
02291 idx_t i1 = 0, i2 = 0;
02292 int lmax;
02293
02294 if (a > b)
02295 return ivec_new_void();
02296
02297 if (a == b) {
02298 r = ivec_new (1);
02299 r[0] = v[a];
02300 return r;
02301 }
02302
02303 v1 = ivec_k_max_between (v, k, a, (a + b) / 2);
02304 v2 = ivec_k_max_between (v, k, (a + b) / 2 + 1, b);
02305
02306 lmax = k;
02307 if (lmax > k)
02308 lmax = k;
02309
02310 r = ivec_new_alloc (0, lmax);
02311
02312
02313 while (i1 + i2 < lmax) {
02314 if (i1 >= ivec_length (v1))
02315 ivec_push (r, v2[i2++]);
02316 else if (i2 >= ivec_length (v2))
02317 ivec_push (r, v1[i1++]);
02318 else if (v1[i1] > v2[i2])
02319 ivec_push (r, v1[i1++]);
02320 else
02321 ivec_push (r, v2[i2++]);
02322 }
02323 ivec_delete (v1);
02324 ivec_delete (v2);
02325 return r;
02326 }
02327
02328
02329 ivec ivec_k_max (ivec v, int k)
02330 {
02331 return ivec_k_max_between (v, k, 0, ivec_length (v) - 1);
02332 }
02333
02334
02335
02336 ivec vec_k_min_index_between (vec v, int k, idx_t a, idx_t b)
02337 {
02338 ivec r, v1, v2;
02339 idx_t i1 = 0, i2 = 0;
02340 int lmax;
02341
02342 if (a > b)
02343 return ivec_new_void();
02344
02345 if (a == b) {
02346 r = ivec_new (1);
02347 r[0] = a;
02348 return r;
02349 }
02350
02351 v1 = vec_k_min_index_between (v, k, a, (a + b) / 2);
02352 v2 = vec_k_min_index_between (v, k, (a + b) / 2 + 1, b);
02353
02354 lmax = ivec_length (v1) + ivec_length (v2);
02355 if (lmax > k)
02356 lmax = k;
02357
02358 r = ivec_new_alloc (0, lmax);
02359
02360
02361 while (i1 + i2 < lmax) {
02362 if (i1 == ivec_length (v1))
02363 ivec_push (r, v2[i2++]);
02364 else if (i2 == ivec_length (v2))
02365 ivec_push (r, v1[i1++]);
02366 else if (v[v1[i1]] < v[v2[i2]])
02367 ivec_push (r, v1[i1++]);
02368 else
02369 ivec_push (r, v2[i2++]);
02370 }
02371
02372 ivec_delete (v1);
02373 ivec_delete (v2);
02374 return r;
02375 }
02376
02377
02378 ivec vec_k_min_index (vec v, int k)
02379 {
02380 return vec_k_min_index_between (v, k, 0, vec_length (v) - 1);
02381 }
02382
02383
02384
02385 ivec vec_k_max_index_between (vec v, int k, idx_t a, idx_t b)
02386 {
02387 ivec r, v1, v2;
02388 idx_t i1 = 0, i2 = 0;
02389 int lmax;
02390
02391 if (a > b)
02392 return ivec_new_void();
02393
02394 if (a == b) {
02395 r = ivec_new (1);
02396 r[0] = a;
02397 return r;
02398 }
02399
02400 v1 = vec_k_max_index_between (v, k, a, (a + b) / 2);
02401 v2 = vec_k_max_index_between (v, k, (a + b) / 2 + 1, b);
02402
02403 lmax = ivec_length (v1) + ivec_length (v2);
02404 if (lmax > k)
02405 lmax = k;
02406
02407 r = ivec_new_alloc (0, lmax);
02408
02409
02410 while (i1 + i2 < lmax) {
02411 if (i1 == ivec_length (v1))
02412 ivec_push (r, v2[i2++]);
02413 else if (i2 == ivec_length (v2))
02414 ivec_push (r, v1[i1++]);
02415 else if (v[v1[i1]] > v[v2[i2]])
02416 ivec_push (r, v1[i1++]);
02417 else
02418 ivec_push (r, v2[i2++]);
02419 }
02420
02421 ivec_delete (v1);
02422 ivec_delete (v2);
02423 return r;
02424 }
02425
02426
02427 ivec vec_k_max_index (vec v, int k)
02428 {
02429 return vec_k_max_index_between (v, k, 0, vec_length (v) - 1);
02430 }
02431
02432
02433
02434 ivec ivec_k_min_index_between (ivec v, int k, idx_t a, idx_t b)
02435 {
02436 ivec r, v1, v2;
02437 idx_t i1 = 0, i2 = 0;
02438 int lmax;
02439
02440 if (a > b)
02441 return ivec_new_void();
02442
02443 if (a == b) {
02444 r = ivec_new (1);
02445 r[0] = a;
02446 return r;
02447 }
02448
02449 v1 = ivec_k_min_index_between (v, k, a, (a + b) / 2);
02450 v2 = ivec_k_min_index_between (v, k, (a + b) / 2 + 1, b);
02451
02452 lmax = ivec_length (v1) + ivec_length (v2);
02453 if (lmax > k)
02454 lmax = k;
02455
02456 r = ivec_new_alloc (0, lmax);
02457
02458
02459 while (i1 + i2 < lmax) {
02460 if (i1 == ivec_length (v1))
02461 ivec_push (r, v2[i2++]);
02462 else if (i2 == ivec_length (v2))
02463 ivec_push (r, v1[i1++]);
02464 else if (v[v1[i1]] < v[v2[i2]])
02465 ivec_push (r, v1[i1++]);
02466 else
02467 ivec_push (r, v2[i2++]);
02468 }
02469
02470 ivec_delete (v1);
02471 ivec_delete (v2);
02472 return r;
02473 }
02474
02475
02476 ivec ivec_k_min_index (ivec v, int k)
02477 {
02478 return ivec_k_min_index_between (v, k, 0, ivec_length (v) - 1);
02479 }
02480
02481
02482
02483 ivec ivec_k_max_index_between (ivec v, int k, idx_t a, idx_t b)
02484 {
02485 ivec r, v1, v2;
02486 idx_t i1 = 0, i2 = 0;
02487 int lmax;
02488
02489 if (a > b)
02490 return ivec_new_void();
02491
02492 if (a == b) {
02493 r = ivec_new (1);
02494 r[0] = a;
02495 return r;
02496 }
02497
02498 v1 = ivec_k_max_index_between (v, k, a, (a + b) / 2);
02499 v2 = ivec_k_max_index_between (v, k, (a + b) / 2 + 1, b);
02500
02501 lmax = ivec_length (v1) + ivec_length (v2);
02502 if (lmax > k)
02503 lmax = k;
02504
02505 r = ivec_new_alloc (0, lmax);
02506
02507
02508 while (i1 + i2 < lmax) {
02509 if (i1 == ivec_length (v1))
02510 ivec_push (r, v2[i2++]);
02511 else if (i2 == ivec_length (v2))
02512 ivec_push (r, v1[i1++]);
02513 else if (v[v1[i1]] > v[v2[i2]])
02514 ivec_push (r, v1[i1++]);
02515 else
02516 ivec_push (r, v2[i2++]);
02517 }
02518
02519 ivec_delete (v1);
02520 ivec_delete (v2);
02521 return r;
02522 }
02523
02524
02525 ivec ivec_k_max_index (ivec v, int k)
02526 {
02527 return ivec_k_max_index_between (v, k, 0, ivec_length (v) - 1);
02528 }
02529
02530
02531 double vec_mean (vec v)
02532 {
02533 assert (v);
02534 return vec_sum (v) / Vec_length (v);
02535 }
02536
02537 double vec_mean_robust (vec v)
02538 {
02539 assert (v);
02540 return vec_sum_robust (v) / Vec_length (v);
02541 }
02542
02543
02544 int ivec_median (ivec v)
02545 {
02546 ivec c;
02547 int m;
02548 assert (v);
02549 if (ivec_length (v) == 0) {
02550 it_warning ("Undefined median value for vector of size 0. Return 0.\n");
02551 return 0;
02552 }
02553
02554 if (ivec_length (v) == 1)
02555 return v[0];
02556
02557 c = ivec_clone (v);
02558 ivec_sort (c);
02559
02560 m = (c[(ivec_length (v) - 1) / 2] + c[ivec_length (v) / 2]) / 2;
02561
02562 ivec_delete (c);
02563 return m;
02564 }
02565
02566
02567 double vec_median (vec v)
02568 {
02569 vec c;
02570 double m;
02571 assert (v);
02572 if (vec_length (v) == 0) {
02573 it_warning ("Undefined median value for vector of size 0. Return 0.\n");
02574 return 0;
02575 }
02576
02577 if (vec_length (v) == 1)
02578 return v[0];
02579
02580 c = vec_clone (v);
02581 vec_sort (c);
02582
02583 m = (c[(vec_length (v) - 1) / 2] + c[vec_length (v) / 2]) / 2;
02584
02585 vec_delete (c);
02586 return m;
02587 }
02588
02589
02590 double vec_variance (vec v)
02591 {
02592 idx_t i, l;
02593 double sum = 0;
02594 double var = 0;
02595 assert (v);
02596 l = Vec_length (v);
02597 assert (l > 1);
02598 for (i = 0; i < l; i++) {
02599 sum += v[i];
02600 var += v[i] * v[i];
02601 }
02602
02603 return (var - sum * sum / l) / (l - 1);
02604 }
02605
02606
02607 double vec_variance_robust (vec v)
02608 {
02609 double s= 0., r, c, m= 0.;
02610 idx_t i;
02611 size_t l;
02612
02613 assert (v);
02614 l = Vec_length( v );
02615
02616 r = vec_mean_robust (v);
02617
02618 for ( i= 0; i< l; i++ )
02619 {
02620 c = ( v[i]-r ) - m;
02621 m += c/(double)(i+1);
02622 s+= c*( ( v[i]-r ) - m );
02623 }
02624 return (s/(double)(l-1) );
02625 }
02626
02627 double vec_cov (vec v1, vec v2)
02628 {
02629 double n = vec_length (v1);
02630 double m1 = vec_mean (v1);
02631 double m2 = vec_mean (v2);
02632 double cov;
02633 vec v1_s, v2_s;
02634
02635 vec v1_mean = vec_new_set (m1, n);
02636 vec v2_mean = vec_new_set (m2, n);
02637
02638
02639 v1_s = vec_clone (v1);
02640 v2_s = vec_clone (v2);
02641
02642 vec_sub (v1_s, v1_mean);
02643 vec_sub (v2_s, v2_mean);
02644
02645
02646
02647 vec_mul (v1_s, v2_s);
02648
02649
02650 cov = (1 / n) * vec_sum (v1_s);
02651
02652 vec_delete (v1_s);
02653 vec_delete (v2_s);
02654 vec_delete (v1_mean);
02655 vec_delete (v2_mean);
02656
02657 return cov;
02658
02659 }
02660
02661 double vec_norm (vec v, double n)
02662 {
02663 idx_t i;
02664 double nr = 0;
02665 assert (v);
02666 assert (n > 0);
02667 for (i = 0; i < vec_length (v); i++)
02668 nr += pow (fabs (v[i]), n);
02669
02670 return pow (nr, 1.0 / n);
02671 }
02672
02673
02674
02675 double ivec_norm (ivec v, int n)
02676 {
02677 idx_t i;
02678 double nr = 0;
02679 assert (v);
02680 assert (n > 0);
02681 for (i = 0; i < ivec_length (v); i++)
02682 nr += pow (fabs ((double) v[i]), (double) n);
02683
02684 return pow ((double) nr, 1.0 / (double) n);
02685 }
02686
02687
02688
02689 void vec_apply_function (vec v, it_function_t function, it_args_t args)
02690 {
02691 idx_t i;
02692 idx_t l;
02693 assert (v);
02694 l = vec_length (v);
02695 for (i = 0; i < l; i++)
02696 v[i] = function (v[i], args);
02697 }
02698
02699
02700 vec vec_new_apply_function (vec v, it_function_t function, it_args_t args)
02701 {
02702 vec r;
02703 idx_t i;
02704 idx_t l;
02705 assert (v);
02706 l = vec_length (v);
02707 r = vec_new (l);
02708 for (i = 0; i < l; i++)
02709 r[i] = function (v[i], args);
02710 return (r);
02711 }
02712
02713
02714 ivec ivec_apply_function (ivec v, it_ifunction_t function, it_args_t args)
02715 {
02716 idx_t i;
02717 idx_t l;
02718 assert (v);
02719 l = ivec_length (v);
02720 for (i = 0; i < l; i++)
02721 v[i] = function (v[i], args);
02722 return v;
02723 }
02724
02725
02726 ivec ivec_new_apply_function (ivec v, it_ifunction_t function, it_args_t args)
02727 {
02728 ivec r;
02729 idx_t i;
02730 idx_t l;
02731 assert (v);
02732 l = ivec_length (v);
02733 r = ivec_new (l);
02734 for (i = 0; i < l; i++)
02735 r[i] = function (v[i], args);
02736 return (r);
02737 }
02738
02739
02740
02741 void vec_reverse (vec v)
02742 {
02743 idx_t i, j, m;
02744 double tmp;
02745 assert (v);
02746
02747 m = vec_length (v) / 2;
02748 for (i = 0, j = vec_length (v) - 1; i < m; i++, j--) {
02749 tmp = v[i];
02750 v[i] = v[j];
02751 v[j] = tmp;
02752 }
02753 }
02754
02755
02756 void ivec_reverse (ivec v)
02757 {
02758 idx_t i, j, m;
02759 int tmp;
02760 assert (v);
02761
02762 m = ivec_length (v) / 2;
02763 for (i = 0, j = ivec_length (v) - 1; i < m; i++, j--) {
02764 tmp = v[i];
02765 v[i] = v[j];
02766 v[j] = tmp;
02767 }
02768 }
02769
02770
02771 void bvec_reverse (bvec v)
02772 {
02773 idx_t i, j, m;
02774 byte tmp;
02775 assert (v);
02776
02777 m = bvec_length (v) / 2;
02778 for (i = 0, j = bvec_length (v) - 1; i < m; i++, j--) {
02779 tmp = v[i];
02780 v[i] = v[j];
02781 v[j] = tmp;
02782 }
02783 }
02784
02785
02786 void cvec_reverse (cvec v)
02787 {
02788 idx_t i, j, m;
02789 double tmp_real, tmp_imag;
02790 assert (v);
02791
02792 m = cvec_length (v) / 2;
02793 for (i = 0, j = cvec_length (v) - 1; i < m; i++, j--) {
02794 tmp_real = creal (v[i]);
02795 tmp_imag = cimag (v[i]);
02796 creal (v[i]) = creal (v[j]);
02797 cimag (v[i]) = cimag (v[j]);
02798 creal (v[j]) = tmp_real;
02799 creal (v[j]) = tmp_imag;
02800 }
02801 }
02802
02803
02804
02805 vec vec_new_reverse (vec v)
02806 {
02807 idx_t i;
02808 vec cl;
02809 assert (v);
02810
02811 cl = vec_new (vec_length (v));
02812 if (cl == NULL)
02813 return NULL;
02814
02815 for (i = 0; i < vec_length (v); i++)
02816 cl[i] = v[vec_length (v) - 1 - i];
02817
02818 return cl;
02819 }
02820
02821 ivec ivec_new_reverse (ivec v)
02822 {
02823 idx_t i;
02824 ivec cl;
02825 assert (v);
02826
02827 cl = ivec_new (ivec_length (v));
02828 if (cl == NULL)
02829 return NULL;
02830
02831 for (i = 0; i < ivec_length (v); i++)
02832 cl[i] = v[ivec_length (v) - 1 - i];
02833
02834 return cl;
02835 }
02836
02837 bvec bvec_new_reverse (bvec v)
02838 {
02839 idx_t i;
02840 bvec cl;
02841 assert (v);
02842
02843 cl = bvec_new (bvec_length (v));
02844 if (cl == NULL)
02845 return NULL;
02846
02847 for (i = 0; i < bvec_length (v); i++)
02848 cl[i] = v[bvec_length (v) - 1 - i];
02849
02850 return cl;
02851 }
02852
02853
02854 cvec cvec_new_reverse (cvec v)
02855 {
02856 idx_t i;
02857 cvec cl;
02858 assert (v);
02859
02860 cl = cvec_new (cvec_length (v));
02861 if (cl == NULL)
02862 return NULL;
02863
02864 for (i = 0; i < cvec_length (v); i++) {
02865 creal (cl[i]) = creal (v[cvec_length (v) - 1 - i]);
02866 cimag (cl[i]) = cimag (v[cvec_length (v) - 1 - i]);
02867 }
02868
02869 return cl;
02870 }
02871
02872
02873
02874
02875 idx_t vec_find_first (vec v, double a)
02876 {
02877 idx_t i;
02878 assert (v);
02879 for (i = 0; i < Vec_length (v); i++)
02880 if (v[i] == a)
02881 return i;
02882 return NULL_INDEX;
02883 }
02884
02885
02886 idx_t ivec_find_first (ivec v, int a)
02887 {
02888 idx_t i;
02889 assert (v);
02890 for (i = 0; i < ivec_length (v); i++)
02891 if (v[i] == a)
02892 return i;
02893 return NULL_INDEX;
02894 }
02895
02896
02897 idx_t bvec_find_first (bvec v, byte a)
02898 {
02899 idx_t i;
02900 assert (v);
02901 for (i = 0; i < bvec_length (v); i++)
02902 if (v[i] == a)
02903 return i;
02904 return NULL_INDEX;
02905 }
02906
02907
02908 idx_t cvec_find_first (cvec v, cplx a)
02909 {
02910 idx_t i;
02911 assert (v);
02912 for (i = 0; i < cvec_length (v); i++)
02913 if (ceq (v[i], a))
02914 return i;
02915 return NULL_INDEX;
02916 }
02917
02918
02919
02920 ivec vec_find (vec v, double a)
02921 {
02922 idx_t count = vec_count (v, a);
02923 ivec pos = ivec_new (count);
02924 idx_t i, j;
02925 assert (v);
02926
02927 for (i = 0, j = 0; i < Vec_length (v); i++)
02928 if (v[i] == a)
02929 pos[j++] = i;
02930
02931 return pos;
02932 }
02933
02934
02935 ivec ivec_find (ivec v, int a)
02936 {
02937 idx_t count = ivec_count (v, a);
02938 ivec pos = ivec_new (count);
02939 idx_t i, j;
02940 assert (v);
02941
02942 for (i = 0, j = 0; i < ivec_length (v); i++)
02943 if (v[i] == a)
02944 pos[j++] = i;
02945
02946 return pos;
02947 }
02948
02949
02950 ivec bvec_find (bvec v, byte a)
02951 {
02952 idx_t count = bvec_count (v, a);
02953 ivec pos = ivec_new (count);
02954 idx_t i, j;
02955 assert (v);
02956
02957 for (i = 0, j = 0; i < bvec_length (v); i++)
02958 if (v[i] == a)
02959 pos[j++] = i;
02960
02961 return pos;
02962 }
02963
02964
02965 ivec cvec_find (cvec v, cplx a)
02966 {
02967 idx_t count = cvec_count (v, a);
02968 ivec pos = ivec_new (count);
02969 idx_t i, j;
02970 assert (v);
02971
02972 for (i = 0, j = 0; i < cvec_length (v); i++)
02973 if (ceq (v[i], a))
02974 pos[j++] = i;
02975
02976 return pos;
02977 }
02978
02979
02980
02981
02982
02983 idx_t vec_find_sorted (vec v, double a)
02984 {
02985 idx_t left = 0;
02986 idx_t right = vec_length (v) - 1;
02987 idx_t m;
02988
02989 if (a > v[right] )
02990 return vec_length (v);
02991
02992 while (left < right - 1) {
02993 m = ( left + right ) / 2;
02994
02995 if (a < v[m])
02996 right = m;
02997 else
02998 left = m;
02999 }
03000
03001 if (a <= v[left])
03002 return left;
03003 else
03004 return right;
03005 }
03006
03007
03008 idx_t ivec_find_sorted (ivec v, int a)
03009 {
03010 idx_t left = 0;
03011 idx_t right = ivec_length (v) - 1;
03012 idx_t m;
03013
03014 if (a > v[right] )
03015 return ivec_length (v);
03016
03017 while (left < right - 1) {
03018 m = ( left + right ) / 2;
03019
03020 if (a < v[m])
03021 right = m;
03022 else
03023 left = m;
03024 }
03025
03026 if (a <= v[left])
03027 return left;
03028 else
03029 return right;
03030 }
03031
03032
03033 idx_t bvec_find_sorted (bvec v, byte a)
03034 {
03035 idx_t left = 0;
03036 idx_t right = bvec_length (v) - 1;
03037 idx_t m;
03038
03039 if (a > v[right] )
03040 return bvec_length (v);
03041
03042 while (left < right - 1) {
03043 m = ( left + right ) / 2;
03044
03045 if (a < v[m])
03046 right = m;
03047 else
03048 left = m;
03049 }
03050
03051 if (a <= v[left])
03052 return left;
03053 else
03054 return right;
03055 }
03056
03057
03058
03059
03060 ivec vec_replace (vec v, double a, double b)
03061 {
03062 idx_t count = vec_count (v, a);
03063 ivec pos = ivec_new (count);
03064 idx_t i, j;
03065 assert (v);
03066
03067 for (i = 0, j = 0; i < Vec_length (v); i++)
03068 if (v[i] == a) {
03069 pos[j++] = i;
03070 v[i] = b;
03071 }
03072
03073 return pos;
03074 }
03075
03076
03077 ivec ivec_replace (ivec v, int a, int b)
03078 {
03079 idx_t count = ivec_count (v, a);
03080 ivec pos = ivec_new (count);
03081 idx_t i, j;
03082 assert (v);
03083
03084 for (i = 0, j = 0; i < ivec_length (v); i++)
03085 if (v[i] == a) {
03086 pos[j++] = i;
03087 v[i] = b;
03088 }
03089
03090 return pos;
03091 }
03092
03093
03094 ivec bvec_replace (bvec v, byte a, byte b)
03095 {
03096 idx_t count = bvec_count (v, a);
03097 ivec pos = ivec_new (count);
03098 idx_t i, j;
03099 assert (v);
03100
03101 for (i = 0, j = 0; i < bvec_length (v); i++)
03102 if (v[i] == a) {
03103 pos[j++] = i;
03104 v[i] = b;
03105 }
03106
03107 return pos;
03108 }
03109
03110
03111 ivec cvec_replace (cvec v, cplx a, cplx b)
03112 {
03113 idx_t count = cvec_count (v, a);
03114 ivec pos = ivec_new (count);
03115 idx_t i, j;
03116 assert (v);
03117
03118 for (i = 0, j = 0; i < cvec_length (v); i++)
03119 if (ceq (v[i], a)) {
03120 pos[j++] = i;
03121 v[i] = b;
03122 }
03123
03124 return pos;
03125 }
03126
03127
03128
03129 idx_t vec_count (vec v, double a)
03130 {
03131 idx_t c = 0;
03132 idx_t i;
03133 assert (v);
03134 for (i = 0; i < Vec_length (v); i++)
03135 if (v[i] == a)
03136 c++;
03137 return c;
03138 }
03139
03140
03141 idx_t bvec_count (bvec v, byte a)
03142 {
03143 idx_t c = 0;
03144 idx_t i;
03145 assert (v);
03146 for (i = 0; i < bvec_length (v); i++)
03147 if (v[i] == a)
03148 c++;
03149 return c;
03150 }
03151
03152
03153 idx_t ivec_count (ivec v, int a)
03154 {
03155 idx_t c = 0;
03156 idx_t i;
03157 assert (v);
03158 for (i = 0; i < ivec_length (v); i++)
03159 if (v[i] == a)
03160 c++;
03161 return c;
03162 }
03163
03164
03165 idx_t cvec_count (cvec v, cplx a)
03166 {
03167 idx_t c = 0;
03168 idx_t i;
03169 assert (v);
03170 for (i = 0; i < cvec_length (v); i++)
03171 if (ceq (v[i], a))
03172 c++;
03173 return c;
03174 }
03175
03176
03177
03178
03179
03180
03181 vec vec_new_concat (vec v1, vec v2)
03182 {
03183 idx_t i, j;
03184 vec v;
03185 assert (v1);
03186 assert (v2);
03187 v = vec_new (vec_length (v1) + vec_length (v2));
03188 for (i = 0; i < vec_length (v1); i++)
03189 v[i] = v1[i];
03190
03191 for (j = 0; j < vec_length (v2); j++, i++)
03192 v[i] = v2[j];
03193 return v;
03194 }
03195
03196
03197 ivec ivec_new_concat (ivec v1, ivec v2)
03198 {
03199 idx_t i, j;
03200 ivec v;
03201 assert (v1);
03202 assert (v2);
03203 v = ivec_new (ivec_length (v1) + ivec_length (v2));
03204 for (i = 0; i < ivec_length (v1); i++)
03205 v[i] = v1[i];
03206
03207 for (j = 0; j < ivec_length (v2); j++, i++)
03208 v[i] = v2[j];
03209 return v;
03210 }
03211
03212
03213 bvec bvec_new_concat (bvec v1, bvec v2)
03214 {
03215 idx_t i, j;
03216 bvec v;
03217 assert (v1);
03218 assert (v2);
03219 v = bvec_new (bvec_length (v1) + bvec_length (v2));
03220 for (i = 0; i < bvec_length (v1); i++)
03221 v[i] = v1[i];
03222
03223 for (j = 0; j < bvec_length (v2); j++, i++)
03224 v[i] = v2[j];
03225 return v;
03226 }
03227
03228
03229 cvec cvec_new_concat (cvec v1, cvec v2)
03230 {
03231 idx_t i, j;
03232 cvec v;
03233 assert (v1);
03234 assert (v2);
03235 v = cvec_new (cvec_length (v1) + cvec_length (v2));
03236 for (i = 0; i < cvec_length (v1); i++)
03237 v[i] = v1[i];
03238
03239 for (j = 0; j < cvec_length (v2); j++, i++)
03240 v[i] = v2[j];
03241 return v;
03242 }
03243
03244
03245
03246
03247 vec vec_new_unique (vec v)
03248 {
03249 idx_t i;
03250 vec vsorted, vtmp = vec_new (0);
03251
03252 assert (v);
03253 if (vec_length (v) == 0)
03254 return vtmp;
03255
03256 vsorted = vec_clone (v);
03257 vec_sort (vsorted);
03258
03259 vec_push (vtmp, vsorted[0]);
03260 for (i = 1; i < vec_length (v); i++)
03261 if (vsorted[i] != vsorted[i - 1])
03262 vec_push (vtmp, vsorted[i]);
03263
03264 vec_delete (vsorted);
03265 return vtmp;
03266 }
03267
03268
03269 ivec ivec_new_unique (ivec v)
03270 {
03271 idx_t i;
03272 ivec vsorted, vtmp = ivec_new (0);
03273
03274 assert (v);
03275 if (ivec_length (v) == 0)
03276 return vtmp;
03277
03278 vsorted = ivec_clone (v);
03279 ivec_sort (vsorted);
03280
03281 ivec_push (vtmp, vsorted[0]);
03282 for (i = 1; i < ivec_length (v); i++)
03283 if (vsorted[i] != vsorted[i - 1])
03284 ivec_push (vtmp, vsorted[i]);
03285
03286 ivec_delete (vsorted);
03287 return vtmp;
03288 }
03289
03290
03291 bvec bvec_new_unique (bvec v)
03292 {
03293 idx_t i;
03294 bvec vsorted = bvec_new (0);
03295 ivec vtmp = ivec_new_zeros (1 << (8 * sizeof (byte)));
03296
03297 assert (v);
03298
03299 for (i = 0; i < bvec_length (v); i++)
03300 vtmp[v[i]]++;
03301
03302 for (i = 0; i < ivec_length (vtmp); i++)
03303 if (vtmp[i] > 0)
03304 bvec_push (vsorted, (byte) i);
03305
03306 ivec_delete (vtmp);
03307 return vsorted;
03308 }
03309
03310
03311 vec vec_new_union (vec v1, vec v2)
03312 {
03313 idx_t i1, i2;
03314 vec vu1 = vec_new_unique (v1);
03315 vec vu2 = vec_new_unique (v2);
03316 vec vu = vec_new_alloc (0, vec_length (vu1) + vec_length (vu2));
03317
03318 for (i1 = 0, i2 = 0; i1 < vec_length (vu1) && i2 < vec_length (vu2);)
03319 if (vu1[i1] < vu2[i2])
03320 vec_push (vu, vu1[i1++]);
03321 else if (vu1[i1] > vu2[i2])
03322 vec_push (vu, vu2[i2++]);
03323 else {
03324 vec_push (vu, vu1[i1]);
03325 i1++;
03326 i2++;
03327 };
03328
03329
03330 while (i1 < vec_length (vu1))
03331 vec_push (vu, vu1[i1++]);
03332
03333 while (i2 < vec_length (vu2))
03334 vec_push (vu, vu2[i2++]);
03335
03336 vec_delete (vu1);
03337 vec_delete (vu2);
03338 return vu;
03339 }
03340
03341
03342 ivec ivec_new_union (ivec v1, ivec v2)
03343 {
03344 idx_t i1, i2;
03345 ivec vu1 = ivec_new_unique (v1);
03346 ivec vu2 = ivec_new_unique (v2);
03347 ivec vu = ivec_new_alloc (0, ivec_length (vu1) + ivec_length (vu2));
03348
03349 for (i1 = 0, i2 = 0; i1 < ivec_length (vu1) && i2 < ivec_length (vu2);)
03350 if (vu1[i1] < vu2[i2])
03351 ivec_push (vu, vu1[i1++]);
03352 else if (vu1[i1] > vu2[i2])
03353 ivec_push (vu, vu2[i2++]);
03354 else {
03355 ivec_push (vu, vu1[i1]);
03356 i1++;
03357 i2++;
03358 };
03359
03360
03361 while (i1 < ivec_length (vu1))
03362 ivec_push (vu, vu1[i1++]);
03363
03364 while (i2 < ivec_length (vu2))
03365 ivec_push (vu, vu2[i2++]);
03366
03367 ivec_delete (vu1);
03368 ivec_delete (vu2);
03369 return vu;
03370 }
03371
03372
03373 bvec bvec_new_union (bvec v1, bvec v2)
03374 {
03375 idx_t i;
03376 bvec vsorted = bvec_new (0);
03377 ivec vtmp = ivec_new_zeros (1 << (8 * sizeof (byte)));
03378
03379 assert (v1);
03380 assert (v2);
03381
03382 for (i = 0; i < bvec_length (v1); i++)
03383 vtmp[v1[i]]++;
03384
03385 for (i = 0; i < bvec_length (v2); i++)
03386 vtmp[v2[i]]++;
03387
03388 for (i = 0; i < ivec_length (vtmp); i++)
03389 if (vtmp[i] > 0)
03390 bvec_push (vsorted, (byte) i);
03391
03392 ivec_delete (vtmp);
03393 return vsorted;
03394 }
03395
03396
03397 vec vec_new_intersection (vec v1, vec v2)
03398 {
03399 idx_t i1, i2;
03400 vec vu1 = vec_new_unique (v1);
03401 vec vu2 = vec_new_unique (v2);
03402 vec vu = vec_new_alloc (0, vec_length (vu1) > vec_length (vu2) ?
03403 vec_length (vu2) : vec_length (vu1));
03404
03405 for (i1 = 0, i2 = 0; i1 < vec_length (vu1) && i2 < vec_length (vu2);
03406 i1++, i2++) {
03407 while (vu1[i1] < vu2[i2] && i1 < vec_length (vu1))
03408 i1++;
03409
03410 if (i1 == vec_length (vu1))
03411 break;
03412
03413 while (vu2[i2] < vu1[i1] && i2 < vec_length (vu2))
03414 i2++;
03415
03416 if (i2 == vec_length (vu2))
03417 break;
03418
03419 vec_push (vu, vu1[i1]);
03420 }
03421
03422 vec_delete (vu1);
03423 vec_delete (vu2);
03424 return vu;
03425 }
03426
03427
03428 ivec ivec_new_intersection (ivec v1, ivec v2)
03429 {
03430 idx_t i1, i2;
03431 ivec vu1 = ivec_new_unique (v1);
03432 ivec vu2 = ivec_new_unique (v2);
03433 ivec vu = ivec_new_alloc (0, ivec_length (vu1) > ivec_length (vu2) ?
03434 ivec_length (vu2) : ivec_length (vu1));
03435
03436 for (i1 = 0, i2 = 0; i1 < ivec_length (vu1) && i2 < ivec_length (vu2);
03437 i1++, i2++) {
03438 while (vu1[i1] < vu2[i2] && i1 < ivec_length (vu1))
03439 i1++;
03440
03441 if (i1 == ivec_length (vu1))
03442 break;
03443
03444 while (vu2[i2] < vu1[i1] && i2 < ivec_length (vu2))
03445 i2++;
03446
03447 if (i2 == ivec_length (vu2))
03448 break;
03449
03450 ivec_push (vu, vu1[i1]);
03451 }
03452
03453 ivec_delete (vu1);
03454 ivec_delete (vu2);
03455 return vu;
03456 }
03457
03458
03459 bvec bvec_new_intersection (bvec v1, bvec v2)
03460 {
03461 idx_t i;
03462 bvec vsorted = bvec_new (0);
03463 ivec vtmp = ivec_new_zeros (1 << (8 * sizeof (byte)));
03464
03465 assert (v1);
03466 assert (v2);
03467
03468 for (i = 0; i < bvec_length (v1); i++)
03469 vtmp[v1[i]]++;
03470
03471 for (i = 0; i < bvec_length (v2); i++)
03472 if (vtmp[v2[i]] > 0)
03473 vtmp[v2[i]] = -1;
03474
03475 for (i = 0; i < ivec_length (vtmp); i++)
03476 if (vtmp[i] == -1)
03477 bvec_push (vsorted, (byte) i);
03478
03479 ivec_delete (vtmp);
03480 return vsorted;
03481 }
03482
03483
03484
03485
03486 vec vec_index_by (vec v, ivec idx)
03487 {
03488 vec r = vec_new (ivec_length (idx));
03489 idx_t i;
03490 assert (v);
03491 assert (idx);
03492 for (i = 0; i < ivec_length (idx); i++)
03493 r[i] = v[idx[i]];
03494 return r;
03495 }
03496
03497
03498 ivec ivec_index_by (ivec v, ivec idx)
03499 {
03500 ivec r = ivec_new (ivec_length (idx));
03501 idx_t i;
03502 assert (v);
03503 assert (idx);
03504 for (i = 0; i < ivec_length (idx); i++)
03505 r[i] = v[idx[i]];
03506 return r;
03507 }
03508
03509
03510 bvec bvec_index_by (bvec v, ivec idx)
03511 {
03512 bvec r = bvec_new (ivec_length (idx));
03513 idx_t i;
03514 assert (v);
03515 assert (idx);
03516 for (i = 0; i < ivec_length (idx); i++)
03517 r[i] = v[idx[i]];
03518 return r;
03519 }
03520
03521
03522 cvec cvec_index_by (cvec v, ivec idx)
03523 {
03524 cvec r = cvec_new (ivec_length (idx));
03525 idx_t i;
03526 assert (v);
03527 assert (idx);
03528 for (i = 0; i < ivec_length (idx); i++)
03529 r[i] = v[idx[i]];
03530 return r;
03531 }
03532
03533
03534
03535
03536
03537 void Vec_sort (Vec v, int (*elem_leq) (const void *, const void *))
03538 {
03539 assert (v);
03540 qsort (v, Vec_length (v), Vec_element_size (v), elem_leq);
03541 }
03542
03543 ivec Vec_sort_index (Vec v, int (*elem_leq_idx) (const void *, const void *))
03544 {
03545
03546
03547
03548
03549
03550
03551
03552
03553
03554 int i;
03555 size_t elem_size = Vec_element_size (v);
03556 void **ptr = Vec_new (void *, Vec_length (v));
03557 ivec idx = (ivec) ptr;
03558
03559
03560 for (i = 0; i < Vec_length (v); i++)
03561 ptr[i] = (void *) ((char *) v + i * elem_size);
03562
03563
03564 qsort (ptr, Vec_length (ptr), sizeof (void *), elem_leq_idx);
03565
03566
03567 for (i = 0; i < Vec_length (v); i++)
03568 idx[i] = (int) ((char *) ptr[i] - (char *) v) / elem_size;
03569
03570
03571 if (sizeof (void *) != sizeof (int)) {
03572 Vec_element_size (idx) = sizeof (int);
03573 Vec_length_max (idx) *= sizeof (void *) / sizeof (int);
03574 }
03575 return (idx);
03576 }
03577
03578
03579
03580 static int double_leq (const void *d1, const void *d2)
03581 {
03582 if (*((double *) d1) > *((double *) d2))
03583 return 1;
03584 else if (*((double *) d1) == *((double *) d2))
03585 return 0;
03586 return -1;
03587 }
03588
03589
03590 static int double_leq_index (const void *d1, const void *d2)
03591 {
03592 return (double_leq (*(void **) d1, *(void **) d2));
03593 }
03594
03595 void vec_sort (vec v)
03596 {
03597 Vec_sort (v, double_leq);
03598 }
03599
03600
03601 ivec vec_sort_index (vec v)
03602 {
03603 return (Vec_sort_index (v, double_leq_index));
03604 }
03605
03606
03607
03608 static int int_leq (const void *d1, const void *d2)
03609 {
03610 if (*((int *) d1) > *((int *) d2))
03611 return 1;
03612 else if (*((int *) d1) == *((int *) d2))
03613 return 0;
03614 return -1;
03615 }
03616
03617
03618 static int int_leq_index (const void *d1, const void *d2)
03619 {
03620 return (int_leq (*(void **) d1, *(void **) d2));
03621 }
03622
03623 void ivec_sort (ivec v)
03624 {
03625 Vec_sort (v, int_leq);
03626 }
03627
03628
03629 ivec ivec_sort_index (ivec v)
03630 {
03631 return (Vec_sort_index (v, int_leq_index));
03632 }
03633
03634
03635
03636 static int byte_leq (const void *d1, const void *d2)
03637 {
03638 if (*((byte *) d1) > *((byte *) d2))
03639 return 1;
03640 else if (*((byte *) d1) == *((byte *) d2))
03641 return 0;
03642 return -1;
03643 }
03644
03645
03646 static int byte_leq_index (const void *d1, const void *d2)
03647 {
03648 return (byte_leq (*(void **) d1, *(void **) d2));
03649 }
03650
03651
03652 void bvec_sort (bvec v)
03653 {
03654 Vec_sort (v, byte_leq);
03655 }
03656
03657
03658 ivec bvec_sort_index (bvec v)
03659 {
03660 return (Vec_sort_index (v, byte_leq_index));
03661 }
03662
03663
03664
03665
03666
03667
03668
03669
03670
03671 void vec_void (vec v)
03672 {
03673 Vec_void (v);
03674 }
03675
03676
03677 void ivec_void (ivec v)
03678 {
03679 Vec_void (v);
03680 }
03681
03682
03683 void bvec_void (bvec v)
03684 {
03685 Vec_void (v);
03686 }
03687
03688
03689 void cvec_void (cvec v)
03690 {
03691 Vec_void (v);
03692 }
03693
03694
03695
03696 void vec_zeros (vec v)
03697 {
03698 Vec_set (v, 0);
03699 }
03700
03701
03702 void ivec_zeros (ivec v)
03703 {
03704 Vec_set (v, 0);
03705 }
03706
03707
03708 void bvec_zeros (bvec v)
03709 {
03710 Vec_set (v, 0);
03711 }
03712
03713
03714 void cvec_zeros (cvec v)
03715 {
03716 Vec_set (v, cplx_0);
03717 }
03718
03719
03720 vec vec_new_zeros (idx_t N) {
03721 return (vec_new_set (0., N));
03722 }
03723
03724
03725 ivec ivec_new_zeros (idx_t N) {
03726 return (ivec_new_set (0, N));
03727 }
03728
03729
03730 bvec bvec_new_zeros (idx_t N) {
03731 return (bvec_new_set (0, N));
03732 }
03733
03734
03735 cvec cvec_new_zeros (idx_t N) {
03736 return (cvec_new_set (cplx_0, N));
03737 }
03738
03739
03740
03741 void vec_ones (vec v)
03742 {
03743 vec_set (v, 1);
03744 }
03745
03746
03747 void ivec_ones (ivec v)
03748 {
03749 ivec_set (v, 1);
03750 }
03751
03752
03753 void bvec_ones (bvec v)
03754 {
03755 bvec_set (v, 1);
03756 }
03757
03758
03759 void cvec_ones (cvec v)
03760 {
03761 cvec_set (v, cplx_1);
03762 }
03763
03764
03765 vec vec_new_ones (idx_t N)
03766 {
03767 return (vec_new_set (1., N));
03768 }
03769
03770
03771 ivec ivec_new_ones (idx_t N)
03772 {
03773 return (ivec_new_set (1, N));
03774 }
03775
03776
03777 bvec bvec_new_ones (idx_t N)
03778 {
03779 return (bvec_new_set (1, N));
03780 }
03781
03782
03783 cvec cvec_new_ones (idx_t N)
03784 {
03785 return (cvec_new_set (cplx_1, N));
03786 }
03787
03788
03789 void vec_range (vec v)
03790 {
03791 idx_t i;
03792 assert (v);
03793 for (i = 0; i < vec_length (v); i++)
03794 v[i] = i;
03795 }
03796
03797
03798 void ivec_range (ivec v)
03799 {
03800 idx_t i;
03801 assert (v);
03802 for (i = 0; i < ivec_length (v); i++)
03803 v[i] = i;
03804 }
03805
03806
03807 void bvec_range (bvec v)
03808 {
03809 idx_t i;
03810 assert (v);
03811 for (i = 0; i < bvec_length (v); i++)
03812 v[i] = i;
03813 }
03814
03815
03816 void cvec_range (cvec v)
03817 {
03818 idx_t i;
03819 assert (v);
03820 for (i = 0; i < cvec_length (v); i++) {
03821 v[i].r = i;
03822 v[i].i = 0;
03823 }
03824 }
03825
03826
03827
03828 void vec_1N (vec v)
03829 {
03830 idx_t i;
03831 assert (v);
03832 for (i = 0; i < vec_length (v); i++)
03833 v[i] = i + 1;
03834 }
03835
03836
03837 void ivec_1N (ivec v)
03838 {
03839 idx_t i;
03840 assert (v);
03841 for (i = 0; i < ivec_length (v); i++)
03842 v[i] = i + 1;
03843 }
03844
03845
03846 void bvec_1N (bvec v)
03847 {
03848 idx_t i;
03849 assert (v);
03850 for (i = 0; i < bvec_length (v); i++)
03851 v[i] = i + 1;
03852 }
03853
03854
03855 void cvec_1N (cvec v)
03856 {
03857 idx_t i;
03858 assert (v);
03859 for (i = 0; i < cvec_length (v); i++) {
03860 v[i].r = i + 1;
03861 v[i].i = 0;
03862 }
03863 }
03864
03865
03866
03867 void vec_arithm (vec v, double start, double incr)
03868 {
03869 idx_t i;
03870 double value = start;
03871 assert (v);
03872 for (i = 0; i < vec_length (v); i++, value += incr)
03873 v[i] = value;
03874 }
03875
03876
03877 void ivec_arithm (ivec v, int start, int incr)
03878 {
03879 idx_t i;
03880 int value = start;
03881 assert (v);
03882 for (i = 0; i < ivec_length (v); i++, value += incr)
03883 v[i] = value;
03884 }
03885
03886
03887 void bvec_arithm (bvec v, byte start, byte incr)
03888 {
03889 idx_t i;
03890 byte value = start;
03891 assert (v);
03892 for (i = 0; i < bvec_length (v); i++, value += incr)
03893 v[i] = value;
03894 }
03895
03896
03897 void cvec_arithm (cvec v, cplx start, cplx incr)
03898 {
03899 idx_t i;
03900 cplx value = start;
03901 assert (v);
03902 for (i = 0; i < cvec_length (v); i++, value = cadd (value, incr))
03903 v[i] = value;
03904 }
03905
03906
03907
03908 void vec_geom (vec v, double start, double r)
03909 {
03910 idx_t i;
03911 double value = start;
03912 assert (v);
03913 for (i = 0; i < vec_length (v); i++, value *= r)
03914 v[i] = value;
03915 }
03916
03917
03918 void ivec_geom (ivec v, int start, int r)
03919 {
03920 idx_t i;
03921 int value = start;
03922 assert (v);
03923 for (i = 0; i < ivec_length (v); i++, value *= r)
03924 v[i] = value;
03925 }
03926
03927
03928 void bvec_geom (bvec v, byte start, byte r)
03929 {
03930 idx_t i;
03931 byte value = start;
03932 assert (v);
03933 for (i = 0; i < bvec_length (v); i++, value *= r)
03934 v[i] = value;
03935 }
03936
03937
03938 void cvec_geom (cvec v, cplx start, cplx r)
03939 {
03940 idx_t i;
03941 cplx value = start;
03942 assert (v);
03943 for (i = 0; i < cvec_length (v); i++, value = cmul (value, r))
03944 v[i] = value;
03945 }
03946
03947
03948
03949
03950
03951
03952 vec vec_new_void ()
03953 {
03954 return vec_new (0);
03955 }
03956
03957
03958 ivec ivec_new_void ()
03959 {
03960 return ivec_new (0);
03961 }
03962
03963
03964 bvec bvec_new_void ()
03965 {
03966 return bvec_new (0);
03967 }
03968
03969 cvec cvec_new_void ()
03970 {
03971 return cvec_new (0);
03972 }
03973
03974
03975
03976 vec vec_new_set (double val, idx_t N)
03977 {
03978 vec v = vec_new (N);
03979 vec_set (v, val);
03980 return (v);
03981 }
03982
03983 ivec ivec_new_set (int val, idx_t N)
03984 {
03985 ivec v = ivec_new (N);
03986 ivec_set (v, val);
03987 return (v);
03988 }
03989
03990 bvec bvec_new_set (byte val, idx_t N)
03991 {
03992 bvec v = bvec_new (N);
03993 bvec_set (v, val);
03994 return (v);
03995 }
03996
03997 cvec cvec_new_set (cplx val, idx_t N)
03998 {
03999 cvec v = cvec_new (N);
04000 cvec_set (v, val);
04001 return (v);
04002 }
04003
04004
04005
04006
04007 vec vec_new_1N (idx_t N)
04008 {
04009 idx_t i;
04010 vec v;
04011 v = vec_new (N);
04012 for (i = 0; i < N; i++)
04013 v[i] = (double) (i + 1);
04014 return v;
04015 }
04016
04017
04018 ivec ivec_new_1N (idx_t N)
04019 {
04020 idx_t i;
04021 ivec v;
04022 v = ivec_new (N);
04023 for (i = 0; i < N; i++)
04024 v[i] = i + 1;
04025 return v;
04026 }
04027
04028
04029 bvec bvec_new_1N (idx_t N)
04030 {
04031 idx_t i;
04032 bvec v;
04033 v = bvec_new (N);
04034 for (i = 0; i < N; i++)
04035 v[i] = i + 1;
04036 return v;
04037 }
04038
04039
04040 cvec cvec_new_1N (idx_t N)
04041 {
04042 idx_t i;
04043 cvec v;
04044 v = cvec_new (N);
04045 for (i = 0; i < N; i++) {
04046 creal (v[i]) = i + 1;
04047 cimag (v[i]) = 0;
04048 }
04049 return v;
04050 }
04051
04052 vec vec_new_range (idx_t N)
04053 {
04054 idx_t i;
04055 vec v;
04056 v = vec_new (N);
04057 for (i = 0; i < N; i++)
04058 v[i] = (double) (i);
04059 return v;
04060 }
04061
04062
04063 ivec ivec_new_range (idx_t N)
04064 {
04065 idx_t i;
04066 ivec v;
04067 v = ivec_new (N);
04068 for (i = 0; i < N; i++)
04069 v[i] = i;
04070 return v;
04071 }
04072
04073
04074 bvec bvec_new_range (idx_t N)
04075 {
04076 idx_t i;
04077 bvec v;
04078 v = bvec_new (N);
04079 for (i = 0; i < N; i++)
04080 v[i] = i;
04081 return v;
04082 }
04083
04084 cvec cvec_new_range (idx_t N)
04085 {
04086 idx_t i;
04087 cvec v;
04088 v = cvec_new (N);
04089 for (i = 0; i < N; i++) {
04090 creal (v[i]) = i;
04091 cimag (v[i]) = 0;
04092 }
04093 return v;
04094 }
04095
04096
04097
04098 vec vec_new_arithm (double start, double incr, idx_t N)
04099 {
04100 idx_t i;
04101 double value = start;
04102 vec v;
04103 v = vec_new (N);
04104 for (i = 0; i < N; i++, value += incr)
04105 v[i] = value;
04106 return v;
04107 }
04108
04109
04110 ivec ivec_new_arithm (int start, int incr, idx_t N)
04111 {
04112 idx_t i;
04113 int value = start;
04114 ivec v;
04115 v = ivec_new (N);
04116 for (i = 0; i < N; i++, value += incr)
04117 v[i] = value;
04118 return v;
04119 }
04120
04121
04122 bvec bvec_new_arithm (byte start, byte incr, idx_t N)
04123 {
04124 idx_t i;
04125 int value = start;
04126 bvec v;
04127 v = bvec_new (N);
04128 for (i = 0; i < N; i++, value += incr)
04129 v[i] = value;
04130 return v;
04131 }
04132
04133 cvec cvec_new_arithm (cplx start, cplx incr, idx_t N)
04134 {
04135 idx_t i;
04136 cplx value = start;
04137 cvec v;
04138 v = cvec_new (N);
04139 for (i = 0; i < N; i++) {
04140 v[i] = value;
04141 value = cadd (value, incr);
04142 }
04143 return v;
04144 }
04145
04146
04147
04148 vec vec_new_geom (double start, double r, idx_t N)
04149 {
04150 idx_t i;
04151 double value = start;
04152 vec v;
04153 it_assert (N >= 0, "Sequence should not be of negative length");
04154 v = vec_new (N);
04155 for (i = 0; i < N; i++, value *= r)
04156 v[i] = value;
04157 return v;
04158 }
04159
04160
04161 ivec ivec_new_geom (int start, int r, idx_t N)
04162 {
04163 idx_t i;
04164 int value = start;
04165 ivec v;
04166 it_assert (N >= 0, "Sequence should not be of negative length");
04167 v = ivec_new (N);
04168 for (i = 0; i < N; i++, value *= r)
04169 v[i] = value;
04170 return v;
04171 }
04172
04173
04174 bvec bvec_new_geom (byte start, byte r, idx_t N)
04175 {
04176 idx_t i;
04177 byte value = start;
04178 bvec v;
04179 it_assert (N >= 0, "Sequence should not be of negative length");
04180 v = bvec_new (N);
04181 for (i = 0; i < N; i++, value *= r)
04182 v[i] = value;
04183 return v;
04184 }
04185
04186 cvec cvec_new_geom (cplx start, cplx r, idx_t N)
04187 {
04188 idx_t i;
04189 cplx value = start;
04190 cvec v;
04191 v = cvec_new (N);
04192 for (i = 0; i < N; i++) {
04193 v[i] = value;
04194 value = cmul (value, r);
04195 }
04196 return v;
04197 }
04198
04199
04200 cvec cvec_new_unit_roots (idx_t N)
04201 {
04202 idx_t k;
04203 cvec v;
04204
04205 v = cvec_new (N);
04206
04207
04208 for (k = 0; k < N; k++) {
04209 creal (v[k]) = cos (2 * k * M_PI / N);
04210 cimag (v[k]) = sin (2 * k * M_PI / N);
04211 }
04212
04213 return (v);
04214 }
04215
04216
04217
04218 vec vec_conv (vec v1, vec v2)
04219 {
04220 int i1, i2;
04221 vec v = vec_new_zeros (vec_length (v1) + vec_length (v2) - 1);
04222
04223 for (i1 = 0; i1 < vec_length (v1); i1++)
04224 for (i2 = 0; i2 < vec_length (v2); i2++)
04225 v[i1 + i2] += v1[i1] * v2[i2];
04226
04227 return v;
04228 }
04229
04230
04231
04232 ivec ivec_conv (ivec v1, ivec v2)
04233 {
04234 int i1, i2;
04235 ivec v = ivec_new_zeros (ivec_length (v1) + ivec_length (v2) - 1);
04236
04237 for (i1 = 0; i1 < ivec_length (v1); i1++)
04238 for (i2 = 0; i2 < ivec_length (v2); i2++)
04239 v[i1 + i2] += v1[i1] * v2[i2];
04240
04241 return v;
04242 }
04243
04244
04245
04246 void vec_rand (vec v)
04247 {
04248 int i;
04249 for (i = 0; i < vec_length (v); i++)
04250 v[i] = it_rand ();
04251 }
04252
04253
04254 void vec_randn (vec v)
04255 {
04256 int i;
04257 for (i = 0; i < vec_length (v); i++)
04258 v[i] = it_randn ();
04259 }
04260
04261
04262
04263 vec vec_new_rand (idx_t n)
04264 {
04265 vec v = vec_new (n);
04266 vec_rand (v);
04267 return v;
04268 }
04269
04270
04271 vec vec_new_randn (idx_t n)
04272 {
04273 vec v = vec_new (n);
04274 vec_randn (v);
04275 return v;
04276 }
04277
04278
04279
04280
04281
04282
04283
04284
04285
04286
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296 ivec ivec_new_perm (size_t len)
04297 {
04298
04299 ivec perm;
04300 idx_t i = 0;
04301 idx_t p = 0;
04302 int n = 0;
04303
04304 it_assert (len > 1, "Permutation of a scalar is crazy");
04305
04306 perm = ivec_new_arithm (0, 1, len);
04307
04308 for (i = 0; i < len; i++) {
04309 p = i + (unsigned int) (it_rand () * (len - i));
04310 n = perm[p];
04311 perm[p] = perm[i];
04312 perm[i] = n;
04313 }
04314
04315 return (perm);
04316
04317 }
04318
04319
04320
04321
04322
04323
04324
04325 void vec_to_spvec (vec v, ivec * svi_out, vec * sv_out)
04326 {
04327 int n = vec_length (v);
04328 int nz = 0, i, j;
04329 ivec svi;
04330 vec sv;
04331 for (i = 0; i < n; i++)
04332 if (v[i] != 0)
04333 nz++;
04334 svi = ivec_new (nz);
04335 sv = vec_new (nz);
04336
04337 j = 0;
04338 for (i = 0; i < n; i++)
04339 if (v[i] != 0) {
04340 svi[j] = i;
04341 sv[j] = v[i];
04342 j++;
04343 }
04344 *svi_out = svi;
04345 *sv_out = sv;
04346 }
04347
04348
04349 void ivec_to_spivec (ivec v, ivec * svi_out, ivec * sv_out)
04350 {
04351 int n = ivec_length (v);
04352 int nz = 0, i, j;
04353 ivec svi, sv;
04354 for (i = 0; i < n; i++)
04355 if (v[i] != 0)
04356 nz++;
04357 svi = ivec_new (nz);
04358 sv = ivec_new (nz);
04359 j = 0;
04360 for (i = 0; i < n; i++)
04361 if (v[i] != 0) {
04362 svi[j] = i;
04363 sv[j] = v[i];
04364 j++;
04365 }
04366 *svi_out = svi;
04367 *sv_out = sv;
04368 }
04369
04370
04371
04372 vec spvec_to_vec (ivec svi, vec sv, int n)
04373 {
04374 int nz = ivec_length (svi), i;
04375 vec v;
04376
04377 assert (vec_length (sv) == nz);
04378
04379 if (n < 0) {
04380 int i;
04381 for (i = 0; i < nz; i++)
04382 if (svi[i] + 1 > n)
04383 n = svi[i] + 1;
04384 }
04385
04386 v = vec_new_zeros (n);
04387
04388 for (i = 0; i < nz; i++)
04389 v[svi[i]] = sv[i];
04390
04391 return v;
04392 }
04393
04394
04395 ivec spivec_to_ivec (ivec svi, ivec sv, int n)
04396 {
04397 int nz = ivec_length (svi), i;
04398 ivec v;
04399
04400 assert (ivec_length (sv) == nz);
04401
04402 if (n < 0) {
04403 int i;
04404 for (i = 0; i < nz; i++)
04405 if (svi[i] + 1 > n)
04406 n = svi[i] + 1;
04407 }
04408
04409 v = ivec_new_zeros (n);
04410
04411 for (i = 0; i < nz; i++)
04412 v[svi[i]] = sv[i];
04413
04414 return v;
04415 }
04416
04417
04418
04419 double spvec_inner_product (ivec svi1, vec sv1, ivec svi2, vec sv2)
04420 {
04421 double s = 0;
04422 idx_t i1 = 0, i2 = 0;
04423
04424 while (1) {
04425
04426 if (i1 == ivec_length (svi1))
04427 break;
04428
04429 if (i2 == ivec_length (svi2))
04430 break;
04431
04432 if (svi1[i1] == svi2[i2]) {
04433 s += sv1[i1] * sv2[i2];
04434 i1++;
04435 i2++;
04436 }
04437
04438 else {
04439 if (svi1[i1] < svi2[i2])
04440 i1++;
04441
04442 else
04443 i2++;
04444 }
04445 }
04446
04447 return s;
04448 }
04449
04450
04451
04452 int spivec_inner_product (ivec svi1, ivec sv1, ivec svi2, ivec sv2)
04453 {
04454 int s = 0;
04455 idx_t i1 = 0, i2 = 0;
04456
04457 while (1) {
04458
04459 if (i1 == ivec_length (svi1))
04460 break;
04461
04462 if (i2 == ivec_length (svi2))
04463 break;
04464
04465 if (svi1[i1] == svi2[i2]) {
04466 s += sv1[i1] * sv2[i2];
04467 i1++;
04468 i2++;
04469 }
04470
04471 else {
04472 if (svi1[i1] < svi2[i2])
04473 i1++;
04474
04475 else
04476 i2++;
04477 }
04478 }
04479
04480 return s;
04481 }