JWS C Library
C language utility library
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00053 #include <jwsc/config.h> 00054 00055 #include <stdlib.h> 00056 #include <assert.h> 00057 #include <math.h> 00058 #include <inttypes.h> 00059 00060 #include "jwsc/base/error.h" 00061 #include "jwsc/math/complex.h" 00062 #include "jwsc/math/blas.h" 00063 #include "jwsc/vector/vector.h" 00064 00065 #ifdef JWSC_HAVE_DMALLOC 00066 #include <dmalloc.h> 00067 #endif 00068 00069 00086 void create_vector_u32(Vector_u32** v_out, uint32_t num_elts) 00087 { 00088 Vector_u32* v; 00089 00090 assert(num_elts > 0); 00091 00092 if (*v_out == NULL) 00093 { 00094 assert(*v_out = malloc(sizeof(Vector_u32))); 00095 (*v_out)->num_elts = 0; 00096 (*v_out)->elts = NULL; 00097 } 00098 v = *v_out; 00099 00100 if (v->num_elts != num_elts) 00101 { 00102 v->elts = realloc(v->elts, num_elts*sizeof(uint32_t)); 00103 assert(v->elts); 00104 v->num_elts = num_elts; 00105 } 00106 } 00107 00117 void create_vector_i32(Vector_i32** v_out, uint32_t num_elts) 00118 { 00119 Vector_i32* v; 00120 00121 assert(num_elts > 0); 00122 00123 if (*v_out == NULL) 00124 { 00125 assert(*v_out = malloc(sizeof(Vector_i32))); 00126 (*v_out)->num_elts = 0; 00127 (*v_out)->elts = NULL; 00128 } 00129 v = *v_out; 00130 00131 if (v->num_elts != num_elts) 00132 { 00133 v->elts = realloc(v->elts, num_elts*sizeof(int32_t)); 00134 assert(v->elts); 00135 v->num_elts = num_elts; 00136 } 00137 } 00138 00148 void create_vector_i64(Vector_i64** v_out, uint32_t num_elts) 00149 { 00150 Vector_i64* v; 00151 00152 assert(num_elts > 0); 00153 00154 if (*v_out == NULL) 00155 { 00156 assert(*v_out = malloc(sizeof(Vector_i64))); 00157 (*v_out)->num_elts = 0; 00158 (*v_out)->elts = NULL; 00159 } 00160 v = *v_out; 00161 00162 if (v->num_elts != num_elts) 00163 { 00164 v->elts = realloc(v->elts, num_elts*sizeof(int64_t)); 00165 assert(v->elts); 00166 v->num_elts = num_elts; 00167 } 00168 } 00169 00179 void create_vector_f(Vector_f** v_out, uint32_t num_elts) 00180 { 00181 Vector_f* v; 00182 00183 assert(num_elts > 0); 00184 00185 if (*v_out == NULL) 00186 { 00187 assert(*v_out = malloc(sizeof(Vector_f))); 00188 (*v_out)->num_elts = 0; 00189 (*v_out)->elts = NULL; 00190 } 00191 v = *v_out; 00192 00193 if (v->num_elts != num_elts) 00194 { 00195 v->elts = realloc(v->elts, num_elts*sizeof(float)); 00196 assert(v->elts); 00197 v->num_elts = num_elts; 00198 } 00199 } 00200 00210 void create_vector_d(Vector_d** v_out, uint32_t num_elts) 00211 { 00212 Vector_d* v; 00213 00214 assert(num_elts > 0); 00215 00216 if (*v_out == NULL) 00217 { 00218 assert(*v_out = malloc(sizeof(Vector_d))); 00219 (*v_out)->num_elts = 0; 00220 (*v_out)->elts = NULL; 00221 } 00222 v = *v_out; 00223 00224 if (v->num_elts != num_elts) 00225 { 00226 v->elts = realloc(v->elts, num_elts*sizeof(double)); 00227 assert(v->elts); 00228 v->num_elts = num_elts; 00229 } 00230 } 00231 00241 void create_vector_cf(Vector_cf** v_out, uint32_t num_elts) 00242 { 00243 Vector_cf* v; 00244 00245 assert(num_elts > 0); 00246 00247 if (*v_out == NULL) 00248 { 00249 assert(*v_out = malloc(sizeof(Vector_cf))); 00250 (*v_out)->num_elts = 0; 00251 (*v_out)->elts = NULL; 00252 } 00253 v = *v_out; 00254 00255 if (v->num_elts != num_elts) 00256 { 00257 v->elts = realloc(v->elts, num_elts*sizeof(Complex_f)); 00258 assert(v->elts); 00259 v->num_elts = num_elts; 00260 } 00261 } 00262 00272 void create_vector_cd(Vector_cd** v_out, uint32_t num_elts) 00273 { 00274 Vector_cd* v; 00275 00276 assert(num_elts > 0); 00277 00278 if (*v_out == NULL) 00279 { 00280 assert(*v_out = malloc(sizeof(Vector_cd))); 00281 (*v_out)->num_elts = 0; 00282 (*v_out)->elts = NULL; 00283 } 00284 v = *v_out; 00285 00286 if (v->num_elts != num_elts) 00287 { 00288 v->elts = realloc(v->elts, num_elts*sizeof(Complex_d)); 00289 assert(v->elts); 00290 v->num_elts = num_elts; 00291 } 00292 } 00293 00314 void create_init_vector_u32(Vector_u32** v_out, uint32_t num_elts, uint32_t val) 00315 { 00316 uint32_t elt; 00317 Vector_u32* v; 00318 00319 create_vector_u32(v_out, num_elts); 00320 v = *v_out; 00321 00322 for (elt = 0; elt < num_elts; elt++) 00323 { 00324 v->elts[ elt ] = val; 00325 } 00326 } 00327 00336 void create_init_vector_i32(Vector_i32** v_out, uint32_t num_elts, int32_t val) 00337 { 00338 uint32_t elt; 00339 Vector_i32* v; 00340 00341 create_vector_i32(v_out, num_elts); 00342 v = *v_out; 00343 00344 for (elt = 0; elt < num_elts; elt++) 00345 { 00346 v->elts[ elt ] = val; 00347 } 00348 } 00349 00358 void create_init_vector_i64(Vector_i64** v_out, uint32_t num_elts, int64_t val) 00359 { 00360 uint32_t elt; 00361 Vector_i64* v; 00362 00363 create_vector_i64(v_out, num_elts); 00364 v = *v_out; 00365 00366 for (elt = 0; elt < num_elts; elt++) 00367 { 00368 v->elts[ elt ] = val; 00369 } 00370 } 00371 00380 void create_init_vector_f(Vector_f** v_out, uint32_t num_elts, float val) 00381 { 00382 uint32_t elt; 00383 Vector_f* v; 00384 00385 create_vector_f(v_out, num_elts); 00386 v = *v_out; 00387 00388 for (elt = 0; elt < num_elts; elt++) 00389 { 00390 v->elts[ elt ] = val; 00391 } 00392 } 00393 00402 void create_init_vector_d(Vector_d** v_out, uint32_t num_elts, double val) 00403 { 00404 uint32_t elt; 00405 Vector_d* v; 00406 00407 create_vector_d(v_out, num_elts); 00408 v = *v_out; 00409 00410 for (elt = 0; elt < num_elts; elt++) 00411 { 00412 v->elts[ elt ] = val; 00413 } 00414 } 00415 00424 void create_init_vector_cf(Vector_cf** v_out, uint32_t num_elts, Complex_f val) 00425 { 00426 uint32_t elt; 00427 Vector_cf* v; 00428 00429 create_vector_cf(v_out, num_elts); 00430 v = *v_out; 00431 00432 for (elt = 0; elt < num_elts; elt++) 00433 { 00434 v->elts[ elt ] = val; 00435 } 00436 } 00437 00446 void create_init_vector_cd(Vector_cd** v_out, uint32_t num_elts, Complex_d val) 00447 { 00448 uint32_t elt; 00449 Vector_cd* v; 00450 00451 create_vector_cd(v_out, num_elts); 00452 v = *v_out; 00453 00454 for (elt = 0; elt < num_elts; elt++) 00455 { 00456 v->elts[ elt ] = val; 00457 } 00458 } 00459 00480 void create_zero_vector_u32(Vector_u32** v_out, uint32_t num_elts) 00481 { 00482 create_init_vector_u32(v_out, num_elts, 0); 00483 } 00484 00485 00493 void create_zero_vector_i32(Vector_i32** v_out, uint32_t num_elts) 00494 { 00495 create_init_vector_i32(v_out, num_elts, 0); 00496 } 00497 00498 00506 void create_zero_vector_i64(Vector_i64** v_out, uint32_t num_elts) 00507 { 00508 create_init_vector_i64(v_out, num_elts, 0); 00509 } 00510 00511 00519 void create_zero_vector_f(Vector_f** v_out, uint32_t num_elts) 00520 { 00521 create_init_vector_f(v_out, num_elts, 0); 00522 } 00523 00524 00532 void create_zero_vector_d(Vector_d** v_out, uint32_t num_elts) 00533 { 00534 create_init_vector_d(v_out, num_elts, 0); 00535 } 00536 00537 00543 void create_zero_vector_cf(Vector_cf** v_out, uint32_t num_elts) 00544 { 00545 Complex_f z = {0}; 00546 create_init_vector_cf(v_out, num_elts, z); 00547 } 00548 00549 00557 void create_zero_vector_cd(Vector_cd** v_out, uint32_t num_elts) 00558 { 00559 Complex_d z = {0}; 00560 create_init_vector_cd(v_out, num_elts, z); 00561 } 00562 00590 void create_random_vector_u32 00591 ( 00592 Vector_u32** v_out, 00593 uint32_t num_elts, 00594 uint32_t min, 00595 uint32_t max 00596 ) 00597 { 00598 uint32_t elt; 00599 double r; 00600 Vector_u32* v; 00601 00602 create_vector_u32(v_out, num_elts); 00603 v = *v_out; 00604 00605 for (elt = 0; elt < num_elts; elt++) 00606 { 00607 r = (double)rand() / (double)RAND_MAX; 00608 v->elts[ elt ] = min + (uint32_t)floor((max - min)*r + 0.5); 00609 } 00610 } 00611 00626 void create_random_vector_i32 00627 ( 00628 Vector_i32** v_out, 00629 uint32_t num_elts, 00630 int32_t min, 00631 int32_t max 00632 ) 00633 { 00634 uint32_t elt; 00635 double r; 00636 Vector_i32* v; 00637 00638 create_vector_i32(v_out, num_elts); 00639 v = *v_out; 00640 00641 for (elt = 0; elt < num_elts; elt++) 00642 { 00643 r = (double)rand() / (double)RAND_MAX; 00644 v->elts[ elt ] = min + (int32_t)floor((max - min)*r + 0.5); 00645 } 00646 } 00647 00662 void create_random_vector_i64 00663 ( 00664 Vector_i64** v_out, 00665 uint32_t num_elts, 00666 int64_t min, 00667 int64_t max 00668 ) 00669 { 00670 uint32_t elt; 00671 double r; 00672 Vector_i64* v; 00673 00674 create_vector_i64(v_out, num_elts); 00675 v = *v_out; 00676 00677 for (elt = 0; elt < num_elts; elt++) 00678 { 00679 r = (double)rand() / (double)RAND_MAX; 00680 v->elts[ elt ] = min + (int64_t)floor((max - min)*r + 0.5); 00681 } 00682 } 00683 00698 void create_random_vector_f 00699 ( 00700 Vector_f** v_out, 00701 uint32_t num_elts, 00702 float min, 00703 float max 00704 ) 00705 { 00706 uint32_t elt; 00707 float r; 00708 Vector_f* v; 00709 00710 create_vector_f(v_out, num_elts); 00711 v = *v_out; 00712 00713 for (elt = 0; elt < num_elts; elt++) 00714 { 00715 r = (float)rand() / (float)RAND_MAX; 00716 v->elts[ elt ] = min + (max - min)*r; 00717 } 00718 } 00719 00734 void create_random_vector_d 00735 ( 00736 Vector_d** v_out, 00737 uint32_t num_elts, 00738 double min, 00739 double max 00740 ) 00741 { 00742 uint32_t elt; 00743 double r; 00744 Vector_d* v; 00745 00746 create_vector_d(v_out, num_elts); 00747 v = *v_out; 00748 00749 for (elt = 0; elt < num_elts; elt++) 00750 { 00751 r = (double)rand() / (double)RAND_MAX; 00752 v->elts[ elt ] = min + (max - min)*r; 00753 } 00754 } 00755 00770 void create_random_vector_cf 00771 ( 00772 Vector_cf** v_out, 00773 uint32_t num_elts, 00774 Complex_f min, 00775 Complex_f max 00776 ) 00777 { 00778 uint32_t elt; 00779 float r; 00780 Vector_cf* v; 00781 00782 create_vector_cf(v_out, num_elts); 00783 v = *v_out; 00784 00785 for (elt = 0; elt < num_elts; elt++) 00786 { 00787 r = (float)rand() / (float)RAND_MAX; 00788 v->elts[ elt ].r = min.r + (max.r - min.r)*r; 00789 r = (float)rand() / (float)RAND_MAX; 00790 v->elts[ elt ].i = min.i + (max.i - min.i)*r; 00791 } 00792 } 00793 00808 void create_random_vector_cd 00809 ( 00810 Vector_cd** v_out, 00811 uint32_t num_elts, 00812 Complex_d min, 00813 Complex_d max 00814 ) 00815 { 00816 uint32_t elt; 00817 double r; 00818 Vector_cd* v; 00819 00820 create_vector_cd(v_out, num_elts); 00821 v = *v_out; 00822 00823 for (elt = 0; elt < num_elts; elt++) 00824 { 00825 r = (double)rand() / (double)RAND_MAX; 00826 v->elts[ elt ].r = min.r + (max.r - min.r)*r; 00827 r = (double)rand() / (double)RAND_MAX; 00828 v->elts[ elt ].i = min.i + (max.i - min.i)*r; 00829 } 00830 } 00831 00856 Error* copy_vector_u32(Vector_u32** v_out, const Vector_u32* v_in) 00857 { 00858 uint32_t num_elts, elt; 00859 Vector_u32* v; 00860 00861 if (*v_out == v_in) 00862 { 00863 return JWSC_EARG("Copying a vector into itself is not supported"); 00864 } 00865 00866 num_elts = v_in->num_elts; 00867 create_vector_u32(v_out, num_elts); 00868 v = *v_out; 00869 00870 for (elt = 0; elt < num_elts; elt++) 00871 { 00872 v->elts[ elt ] = v_in->elts[ elt ]; 00873 } 00874 00875 return NULL; 00876 } 00877 00890 Error* copy_vector_i32(Vector_i32** v_out, const Vector_i32* v_in) 00891 { 00892 uint32_t num_elts, elt; 00893 Vector_i32* v; 00894 00895 if (*v_out == v_in) 00896 { 00897 return JWSC_EARG("Copying a vector into itself is not supported"); 00898 } 00899 00900 num_elts = v_in->num_elts; 00901 create_vector_i32(v_out, num_elts); 00902 v = *v_out; 00903 00904 for (elt = 0; elt < num_elts; elt++) 00905 { 00906 v->elts[ elt ] = v_in->elts[ elt ]; 00907 } 00908 00909 return NULL; 00910 } 00911 00924 Error* copy_vector_i64(Vector_i64** v_out, const Vector_i64* v_in) 00925 { 00926 uint32_t num_elts, elt; 00927 Vector_i64* v; 00928 00929 if (*v_out == v_in) 00930 { 00931 return JWSC_EARG("Copying a vector into itself is not supported"); 00932 } 00933 00934 num_elts = v_in->num_elts; 00935 create_vector_i64(v_out, num_elts); 00936 v = *v_out; 00937 00938 for (elt = 0; elt < num_elts; elt++) 00939 { 00940 v->elts[ elt ] = v_in->elts[ elt ]; 00941 } 00942 00943 return NULL; 00944 } 00945 00958 Error* copy_vector_f(Vector_f** v_out, const Vector_f* v_in) 00959 { 00960 uint32_t num_elts; 00961 Vector_f* v; 00962 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 00963 #else 00964 uint32_t elt; 00965 #endif 00966 00967 if (*v_out == v_in) 00968 { 00969 return JWSC_EARG("Copying a vector into itself is not supported"); 00970 } 00971 00972 num_elts = v_in->num_elts; 00973 create_vector_f(v_out, num_elts); 00974 v = *v_out; 00975 00976 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 00977 blas_scopy(num_elts, v_in->elts, 1, v->elts, 1); 00978 #else 00979 for (elt = 0; elt < num_elts; elt++) 00980 { 00981 v->elts[ elt ] = v_in->elts[ elt ]; 00982 } 00983 #endif 00984 00985 return NULL; 00986 } 00987 01000 Error* copy_vector_d(Vector_d** v_out, const Vector_d* v_in) 01001 { 01002 uint32_t num_elts; 01003 Vector_d* v; 01004 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01005 #else 01006 uint32_t elt; 01007 #endif 01008 01009 if (*v_out == v_in) 01010 { 01011 return JWSC_EARG("Copying a vector into itself is not supported"); 01012 } 01013 01014 num_elts = v_in->num_elts; 01015 create_vector_d(v_out, num_elts); 01016 v = *v_out; 01017 01018 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01019 blas_dcopy(num_elts, v_in->elts, 1, v->elts, 1); 01020 #else 01021 for (elt = 0; elt < num_elts; elt++) 01022 { 01023 v->elts[ elt ] = v_in->elts[ elt ]; 01024 } 01025 #endif 01026 01027 return NULL; 01028 } 01029 01042 Error* copy_vector_cf(Vector_cf** v_out, const Vector_cf* v_in) 01043 { 01044 uint32_t num_elts; 01045 Vector_cf* v; 01046 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01047 #else 01048 uint32_t elt; 01049 #endif 01050 01051 if (*v_out == v_in) 01052 { 01053 return JWSC_EARG("Copying a vector into itself is not supported"); 01054 } 01055 01056 num_elts = v_in->num_elts; 01057 create_vector_cf(v_out, num_elts); 01058 v = *v_out; 01059 01060 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01061 blas_ccopy(num_elts, v_in->elts, 1, v->elts, 1); 01062 #else 01063 for (elt = 0; elt < num_elts; elt++) 01064 { 01065 v->elts[ elt ] = v_in->elts[ elt ]; 01066 } 01067 #endif 01068 01069 return NULL; 01070 } 01071 01084 Error* copy_vector_cd(Vector_cd** v_out, const Vector_cd* v_in) 01085 { 01086 uint32_t num_elts; 01087 Vector_cd* v; 01088 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01089 #else 01090 uint32_t elt; 01091 #endif 01092 01093 if (*v_out == v_in) 01094 { 01095 return JWSC_EARG("Copying a vector into itself is not supported"); 01096 } 01097 01098 num_elts = v_in->num_elts; 01099 create_vector_cd(v_out, num_elts); 01100 v = *v_out; 01101 01102 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01103 blas_zcopy(num_elts, v_in->elts, 1, v->elts, 1); 01104 #else 01105 for (elt = 0; elt < num_elts; elt++) 01106 { 01107 v->elts[ elt ] = v_in->elts[ elt ]; 01108 } 01109 #endif 01110 01111 return NULL; 01112 } 01113 01143 Error* copy_vector_section_u32 01144 ( 01145 Vector_u32** v_out, 01146 const Vector_u32* v_in, 01147 uint32_t offset, 01148 uint32_t num_elts 01149 ) 01150 { 01151 uint32_t elt; 01152 Vector_u32* v; 01153 01154 if (*v_out == v_in) 01155 { 01156 return JWSC_EARG("Copying a vector into itself is not supported"); 01157 } 01158 01159 if (v_in->num_elts < (offset+num_elts)) 01160 { 01161 free_vector_u32(*v_out); v_out = NULL; 01162 return JWSC_EARG("Section to copy outside bounds of input vector"); 01163 } 01164 01165 create_vector_u32(v_out, num_elts); 01166 v = *v_out; 01167 01168 for (elt = 0; elt < num_elts; elt++) 01169 { 01170 v->elts[ elt ] = v_in->elts[ elt+offset ]; 01171 } 01172 01173 return NULL; 01174 } 01175 01193 Error* copy_vector_section_i32 01194 ( 01195 Vector_i32** v_out, 01196 const Vector_i32* v_in, 01197 uint32_t offset, 01198 uint32_t num_elts 01199 ) 01200 { 01201 uint32_t elt; 01202 Vector_i32* v; 01203 01204 if (*v_out == v_in) 01205 { 01206 return JWSC_EARG("Copying a vector into itself is not supported"); 01207 } 01208 01209 if (v_in->num_elts < (offset+num_elts)) 01210 { 01211 free_vector_i32(*v_out); v_out = NULL; 01212 return JWSC_EARG("Section to copy outside bounds of input vector"); 01213 } 01214 01215 create_vector_i32(v_out, num_elts); 01216 v = *v_out; 01217 01218 for (elt = 0; elt < num_elts; elt++) 01219 { 01220 v->elts[ elt ] = v_in->elts[ elt+offset ]; 01221 } 01222 01223 return NULL; 01224 } 01225 01243 Error* copy_vector_section_i64 01244 ( 01245 Vector_i64** v_out, 01246 const Vector_i64* v_in, 01247 uint32_t offset, 01248 uint32_t num_elts 01249 ) 01250 { 01251 uint32_t elt; 01252 Vector_i64* v; 01253 01254 if (*v_out == v_in) 01255 { 01256 return JWSC_EARG("Copying a vector into itself is not supported"); 01257 } 01258 01259 if (v_in->num_elts < (offset+num_elts)) 01260 { 01261 free_vector_i64(*v_out); v_out = NULL; 01262 return JWSC_EARG("Section to copy outside bounds of input vector"); 01263 } 01264 01265 create_vector_i64(v_out, num_elts); 01266 v = *v_out; 01267 01268 for (elt = 0; elt < num_elts; elt++) 01269 { 01270 v->elts[ elt ] = v_in->elts[ elt+offset ]; 01271 } 01272 01273 return NULL; 01274 } 01275 01293 Error* copy_vector_section_f 01294 ( 01295 Vector_f** v_out, 01296 const Vector_f* v_in, 01297 uint32_t offset, 01298 uint32_t num_elts 01299 ) 01300 { 01301 Vector_f* v; 01302 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01303 #else 01304 uint32_t elt; 01305 #endif 01306 01307 if (*v_out == v_in) 01308 { 01309 return JWSC_EARG("Copying a vector into itself is not supported"); 01310 } 01311 01312 if (v_in->num_elts < (offset+num_elts)) 01313 { 01314 free_vector_f(*v_out); v_out = NULL; 01315 return JWSC_EARG("Section to copy outside bounds of input vector"); 01316 } 01317 01318 create_vector_f(v_out, num_elts); 01319 v = *v_out; 01320 01321 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01322 blas_scopy(num_elts, &(v_in->elts[ offset ]), 1, v->elts, 1); 01323 #else 01324 for (elt = 0; elt < num_elts; elt++) 01325 { 01326 v->elts[ elt ] = v_in->elts[ elt+offset ]; 01327 } 01328 #endif 01329 01330 return NULL; 01331 } 01332 01350 Error* copy_vector_section_d 01351 ( 01352 Vector_d** v_out, 01353 const Vector_d* v_in, 01354 uint32_t offset, 01355 uint32_t num_elts 01356 ) 01357 { 01358 Vector_d* v; 01359 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01360 #else 01361 uint32_t elt; 01362 #endif 01363 01364 if (*v_out == v_in) 01365 { 01366 return JWSC_EARG("Copying a vector into itself is not supported"); 01367 } 01368 01369 if (v_in->num_elts < (offset+num_elts)) 01370 { 01371 free_vector_d(*v_out); v_out = NULL; 01372 return JWSC_EARG("Section to copy outside bounds of input vector"); 01373 } 01374 01375 create_vector_d(v_out, num_elts); 01376 v = *v_out; 01377 01378 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01379 blas_dcopy(num_elts, &(v_in->elts[ offset ]), 1, v->elts, 1); 01380 #else 01381 for (elt = 0; elt < num_elts; elt++) 01382 { 01383 v->elts[ elt ] = v_in->elts[ elt+offset ]; 01384 } 01385 #endif 01386 01387 return NULL; 01388 } 01389 01407 Error* copy_vector_section_cf 01408 ( 01409 Vector_cf** v_out, 01410 const Vector_cf* v_in, 01411 uint32_t offset, 01412 uint32_t num_elts 01413 ) 01414 { 01415 Vector_cf* v; 01416 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01417 #else 01418 uint32_t elt; 01419 #endif 01420 01421 if (*v_out == v_in) 01422 { 01423 return JWSC_EARG("Copying a vector into itself is not supported"); 01424 } 01425 01426 if (v_in->num_elts < (offset+num_elts)) 01427 { 01428 free_vector_cf(*v_out); v_out = NULL; 01429 return JWSC_EARG("Section to copy outside bounds of input vector"); 01430 } 01431 01432 create_vector_cf(v_out, num_elts); 01433 v = *v_out; 01434 01435 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01436 blas_ccopy(num_elts, &(v_in->elts[ offset ]), 1, v->elts, 1); 01437 #else 01438 for (elt = 0; elt < num_elts; elt++) 01439 { 01440 v->elts[ elt ] = v_in->elts[ elt+offset ]; 01441 } 01442 #endif 01443 01444 return NULL; 01445 } 01446 01464 Error* copy_vector_section_cd 01465 ( 01466 Vector_cd** v_out, 01467 const Vector_cd* v_in, 01468 uint32_t offset, 01469 uint32_t num_elts 01470 ) 01471 { 01472 Vector_cd* v; 01473 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01474 #else 01475 uint32_t elt; 01476 #endif 01477 01478 if (*v_out == v_in) 01479 { 01480 return JWSC_EARG("Copying a vector into itself is not supported"); 01481 } 01482 01483 if (v_in->num_elts < (offset+num_elts)) 01484 { 01485 free_vector_cd(*v_out); v_out = NULL; 01486 return JWSC_EARG("Section to copy outside bounds of input vector"); 01487 } 01488 01489 create_vector_cd(v_out, num_elts); 01490 v = *v_out; 01491 01492 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01493 blas_zcopy(num_elts, &(v_in->elts[ offset ]), 1, v->elts, 1); 01494 #else 01495 for (elt = 0; elt < num_elts; elt++) 01496 { 01497 v->elts[ elt ] = v_in->elts[ elt+offset ]; 01498 } 01499 #endif 01500 01501 return NULL; 01502 } 01503 01532 Error* copy_vector_section_into_vector_u32 01533 ( 01534 Vector_u32* v_1, 01535 uint32_t offset_1, 01536 const Vector_u32* v_2, 01537 uint32_t offset_2, 01538 uint32_t num_elts 01539 ) 01540 { 01541 uint32_t elt; 01542 01543 if (v_1 == v_2) 01544 { 01545 return JWSC_EARG("Copying a vector into itself is not supported"); 01546 } 01547 01548 /* Test if the size of the section to copy is in the bounds of the input 01549 * vector. */ 01550 if (v_2->num_elts < (offset_2+num_elts) || 01551 v_1->num_elts < (offset_1+num_elts)) 01552 { 01553 return JWSC_EARG("Section to copy outside bounds of input vector"); 01554 } 01555 01556 for (elt = 0; elt < num_elts; elt++) 01557 { 01558 v_1->elts[ elt+offset_1 ] = v_2->elts[ elt+offset_2 ]; 01559 } 01560 01561 return NULL; 01562 } 01563 01580 Error* copy_vector_section_into_vector_i32 01581 ( 01582 Vector_i32* v_1, 01583 uint32_t offset_1, 01584 const Vector_i32* v_2, 01585 uint32_t offset_2, 01586 uint32_t num_elts 01587 ) 01588 { 01589 uint32_t elt; 01590 01591 if (v_1 == v_2) 01592 { 01593 return JWSC_EARG("Copying a vector into itself is not supported"); 01594 } 01595 01596 /* Test if the size of the section to copy is in the bounds of the input 01597 * vector. */ 01598 if (v_2->num_elts < (offset_2+num_elts) || 01599 v_1->num_elts < (offset_1+num_elts)) 01600 { 01601 return JWSC_EARG("Section to copy outside bounds of input vector"); 01602 } 01603 01604 for (elt = 0; elt < num_elts; elt++) 01605 { 01606 v_1->elts[ elt+offset_1 ] = v_2->elts[ elt+offset_2 ]; 01607 } 01608 01609 return NULL; 01610 } 01611 01628 Error* copy_vector_section_into_vector_i64 01629 ( 01630 Vector_i64* v_1, 01631 uint32_t offset_1, 01632 const Vector_i64* v_2, 01633 uint32_t offset_2, 01634 uint32_t num_elts 01635 ) 01636 { 01637 uint32_t elt; 01638 01639 if (v_1 == v_2) 01640 { 01641 return JWSC_EARG("Copying a vector into itself is not supported"); 01642 } 01643 01644 /* Test if the size of the section to copy is in the bounds of the input 01645 * vector. */ 01646 if (v_2->num_elts < (offset_2+num_elts) || 01647 v_1->num_elts < (offset_1+num_elts)) 01648 { 01649 return JWSC_EARG("Section to copy outside bounds of input vector"); 01650 } 01651 01652 for (elt = 0; elt < num_elts; elt++) 01653 { 01654 v_1->elts[ elt+offset_1 ] = v_2->elts[ elt+offset_2 ]; 01655 } 01656 01657 return NULL; 01658 } 01659 01676 Error* copy_vector_section_into_vector_f 01677 ( 01678 Vector_f* v_1, 01679 uint32_t offset_1, 01680 const Vector_f* v_2, 01681 uint32_t offset_2, 01682 uint32_t num_elts 01683 ) 01684 { 01685 uint32_t elt; 01686 01687 if (v_1 == v_2) 01688 { 01689 return JWSC_EARG("Copying a vector into itself is not supported"); 01690 } 01691 01692 /* Test if the size of the section to copy is in the bounds of the input 01693 * vector. */ 01694 if (v_2->num_elts < (offset_2+num_elts) || 01695 v_1->num_elts < (offset_1+num_elts)) 01696 { 01697 return JWSC_EARG("Section to copy outside bounds of input vector"); 01698 } 01699 01700 for (elt = 0; elt < num_elts; elt++) 01701 { 01702 v_1->elts[ elt+offset_1 ] = v_2->elts[ elt+offset_2 ]; 01703 } 01704 01705 return NULL; 01706 } 01707 01724 Error* copy_vector_section_into_vector_d 01725 ( 01726 Vector_d* v_1, 01727 uint32_t offset_1, 01728 const Vector_d* v_2, 01729 uint32_t offset_2, 01730 uint32_t num_elts 01731 ) 01732 { 01733 uint32_t elt; 01734 01735 if (v_1 == v_2) 01736 { 01737 return JWSC_EARG("Copying a vector into itself is not supported"); 01738 } 01739 01740 /* Test if the size of the section to copy is in the bounds of the input 01741 * vector. */ 01742 if (v_2->num_elts < (offset_2+num_elts) || 01743 v_1->num_elts < (offset_1+num_elts)) 01744 { 01745 return JWSC_EARG("Section to copy outside bounds of input vector"); 01746 } 01747 01748 for (elt = 0; elt < num_elts; elt++) 01749 { 01750 v_1->elts[ elt+offset_1 ] = v_2->elts[ elt+offset_2 ]; 01751 } 01752 01753 return NULL; 01754 } 01755 01772 Error* copy_vector_section_into_vector_cf 01773 ( 01774 Vector_cf* v_1, 01775 uint32_t offset_1, 01776 const Vector_cf* v_2, 01777 uint32_t offset_2, 01778 uint32_t num_elts 01779 ) 01780 { 01781 uint32_t elt; 01782 01783 if (v_1 == v_2) 01784 { 01785 return JWSC_EARG("Copying a vector into itself is not supported"); 01786 } 01787 01788 /* Test if the size of the section to copy is in the bounds of the input 01789 * vector. */ 01790 if (v_2->num_elts < (offset_2+num_elts) || 01791 v_1->num_elts < (offset_1+num_elts)) 01792 { 01793 return JWSC_EARG("Section to copy outside bounds of input vector"); 01794 } 01795 01796 for (elt = 0; elt < num_elts; elt++) 01797 { 01798 v_1->elts[ elt+offset_1 ] = v_2->elts[ elt+offset_2 ]; 01799 } 01800 01801 return NULL; 01802 } 01803 01822 Error* copy_vector_section_into_vector_cd 01823 ( 01824 Vector_cd* v_1, 01825 uint32_t offset_1, 01826 const Vector_cd* v_2, 01827 uint32_t offset_2, 01828 uint32_t num_elts 01829 ) 01830 { 01831 uint32_t elt; 01832 01833 if (v_1 == v_2) 01834 { 01835 return JWSC_EARG("Copying a vector into itself is not supported"); 01836 } 01837 01838 /* Test if the size of the section to copy is in the bounds of the input 01839 * vector. */ 01840 if (v_2->num_elts < (offset_2+num_elts) || 01841 v_1->num_elts < (offset_1+num_elts)) 01842 { 01843 return JWSC_EARG("Section to copy outside bounds of input vector"); 01844 } 01845 01846 for (elt = 0; elt < num_elts; elt++) 01847 { 01848 v_1->elts[ elt+offset_1 ] = v_2->elts[ elt+offset_2 ]; 01849 } 01850 01851 return NULL; 01852 } 01853 01874 void cat_vectors_u32 01875 ( 01876 Vector_u32** v_out, 01877 const Vector_u32* v_1, 01878 const Vector_u32* v_2 01879 ) 01880 { 01881 uint32_t N_1, N_2; 01882 Vector_u32* v; 01883 01884 N_1 = v_1->num_elts; 01885 N_2 = v_2->num_elts; 01886 01887 v = (*v_out == v_1 || *v_out == v_2) ? NULL : *v_out; 01888 create_vector_u32(&v, N_1+N_2); 01889 01890 copy_vector_section_into_vector_u32(v, 0, v_1, 0, N_1); 01891 copy_vector_section_into_vector_u32(v, N_1, v_2, 0, N_2); 01892 01893 if (*v_out == v_1 || *v_out == v_2) 01894 { 01895 copy_vector_u32(v_out, v); 01896 } 01897 } 01898 01907 void cat_vectors_i32 01908 ( 01909 Vector_i32** v_out, 01910 const Vector_i32* v_1, 01911 const Vector_i32* v_2 01912 ) 01913 { 01914 uint32_t N_1, N_2; 01915 Vector_i32* v; 01916 01917 N_1 = v_1->num_elts; 01918 N_2 = v_2->num_elts; 01919 01920 v = (*v_out == v_1 || *v_out == v_2) ? NULL : *v_out; 01921 create_vector_i32(&v, N_1+N_2); 01922 01923 copy_vector_section_into_vector_i32(v, 0, v_1, 0, N_1); 01924 copy_vector_section_into_vector_i32(v, N_1, v_2, 0, N_2); 01925 01926 if (*v_out == v_1 || *v_out == v_2) 01927 { 01928 copy_vector_i32(v_out, v); 01929 } 01930 } 01931 01940 void cat_vectors_i64 01941 ( 01942 Vector_i64** v_out, 01943 const Vector_i64* v_1, 01944 const Vector_i64* v_2 01945 ) 01946 { 01947 uint32_t N_1, N_2; 01948 Vector_i64* v; 01949 01950 N_1 = v_1->num_elts; 01951 N_2 = v_2->num_elts; 01952 01953 v = (*v_out == v_1 || *v_out == v_2) ? NULL : *v_out; 01954 create_vector_i64(&v, N_1+N_2); 01955 01956 copy_vector_section_into_vector_i64(v, 0, v_1, 0, N_1); 01957 copy_vector_section_into_vector_i64(v, N_1, v_2, 0, N_2); 01958 01959 if (*v_out == v_1 || *v_out == v_2) 01960 { 01961 copy_vector_i64(v_out, v); 01962 } 01963 } 01964 01973 void cat_vectors_f 01974 ( 01975 Vector_f** v_out, 01976 const Vector_f* v_1, 01977 const Vector_f* v_2 01978 ) 01979 { 01980 uint32_t N_1, N_2; 01981 Vector_f* v; 01982 01983 N_1 = v_1->num_elts; 01984 N_2 = v_2->num_elts; 01985 01986 v = (*v_out == v_1 || *v_out == v_2) ? NULL : *v_out; 01987 create_vector_f(&v, N_1+N_2); 01988 01989 copy_vector_section_into_vector_f(v, 0, v_1, 0, N_1); 01990 copy_vector_section_into_vector_f(v, N_1, v_2, 0, N_2); 01991 01992 if (*v_out == v_1 || *v_out == v_2) 01993 { 01994 copy_vector_f(v_out, v); 01995 } 01996 } 01997 02006 void cat_vectors_d 02007 ( 02008 Vector_d** v_out, 02009 const Vector_d* v_1, 02010 const Vector_d* v_2 02011 ) 02012 { 02013 uint32_t N_1, N_2; 02014 Vector_d* v; 02015 02016 N_1 = v_1->num_elts; 02017 N_2 = v_2->num_elts; 02018 02019 v = (*v_out == v_1 || *v_out == v_2) ? NULL : *v_out; 02020 create_vector_d(&v, N_1+N_2); 02021 02022 copy_vector_section_into_vector_d(v, 0, v_1, 0, N_1); 02023 copy_vector_section_into_vector_d(v, N_1, v_2, 0, N_2); 02024 02025 if (*v_out == v_1 || *v_out == v_2) 02026 { 02027 copy_vector_d(v_out, v); 02028 } 02029 } 02030 02039 void cat_vectors_cf 02040 ( 02041 Vector_cf** v_out, 02042 const Vector_cf* v_1, 02043 const Vector_cf* v_2 02044 ) 02045 { 02046 uint32_t N_1, N_2; 02047 Vector_cf* v; 02048 02049 N_1 = v_1->num_elts; 02050 N_2 = v_2->num_elts; 02051 02052 v = (*v_out == v_1 || *v_out == v_2) ? NULL : *v_out; 02053 create_vector_cf(&v, N_1+N_2); 02054 02055 copy_vector_section_into_vector_cf(v, 0, v_1, 0, N_1); 02056 copy_vector_section_into_vector_cf(v, N_1, v_2, 0, N_2); 02057 02058 if (*v_out == v_1 || *v_out == v_2) 02059 { 02060 copy_vector_cf(v_out, v); 02061 } 02062 } 02063 02072 void cat_vectors_cd 02073 ( 02074 Vector_cd** v_out, 02075 const Vector_cd* v_1, 02076 const Vector_cd* v_2 02077 ) 02078 { 02079 uint32_t N_1, N_2; 02080 Vector_cd* v; 02081 02082 N_1 = v_1->num_elts; 02083 N_2 = v_2->num_elts; 02084 02085 v = (*v_out == v_1 || *v_out == v_2) ? NULL : *v_out; 02086 create_vector_cd(&v, N_1+N_2); 02087 02088 copy_vector_section_into_vector_cd(v, 0, v_1, 0, N_1); 02089 copy_vector_section_into_vector_cd(v, N_1, v_2, 0, N_2); 02090 02091 if (*v_out == v_1 || *v_out == v_2) 02092 { 02093 copy_vector_cd(v_out, v); 02094 } 02095 } 02096 02112 void free_vector_u32(Vector_u32* v) 02113 { 02114 if (v == NULL) return; 02115 02116 free(v->elts); 02117 free(v); 02118 } 02119 02123 void free_vector_i32(Vector_i32* v) 02124 { 02125 if (v == NULL) return; 02126 02127 free(v->elts); 02128 free(v); 02129 } 02130 02134 void free_vector_i64(Vector_i64* v) 02135 { 02136 if (v == NULL) return; 02137 02138 free(v->elts); 02139 free(v); 02140 } 02141 02145 void free_vector_f(Vector_f* v) 02146 { 02147 if (v == NULL) return; 02148 02149 free(v->elts); 02150 free(v); 02151 } 02152 02156 void free_vector_d(Vector_d* v) 02157 { 02158 if (v == NULL) return; 02159 02160 free(v->elts); 02161 free(v); 02162 } 02163 02167 void free_vector_cf(Vector_cf* v) 02168 { 02169 if (v == NULL) return; 02170 02171 free(v->elts); 02172 free(v); 02173 } 02174 02178 void free_vector_cd(Vector_cd* v) 02179 { 02180 if (v == NULL) return; 02181 02182 free(v->elts); 02183 free(v); 02184 } 02185