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 00055 #include <jwsc/config.h> 00056 00057 #include <stdlib.h> 00058 #include <assert.h> 00059 #include <math.h> 00060 #include <inttypes.h> 00061 00062 #include "jwsc/base/error.h" 00063 #include "jwsc/math/complex.h" 00064 #include "jwsc/math/blas.h" 00065 #include "jwsc/vector/vector.h" 00066 #include "jwsc/matrix/matrix.h" 00067 00068 #ifdef JWSC_HAVE_DMALLOC 00069 #include <dmalloc.h> 00070 #endif 00071 00072 00088 void create_matrix_u32 00089 ( 00090 Matrix_u32** m_out, 00091 uint32_t num_rows, 00092 uint32_t num_cols 00093 ) 00094 { 00095 uint32_t row; 00096 uint32_t* elts; 00097 Matrix_u32* m; 00098 00099 assert(num_rows > 0 && num_cols > 0); 00100 00101 if (*m_out == NULL) 00102 { 00103 assert(*m_out = malloc(sizeof(Matrix_u32))); 00104 (*m_out)->num_rows = 0; 00105 (*m_out)->num_cols = 0; 00106 (*m_out)->elts = NULL; 00107 } 00108 m = *m_out; 00109 00110 if (m->num_rows != num_rows || m->num_cols != num_cols) 00111 { 00112 elts = (m->elts != NULL) ? *(m->elts) : NULL; 00113 elts = realloc(elts, num_rows*num_cols*sizeof(uint32_t)); 00114 assert(elts); 00115 00116 m->elts = realloc(m->elts, num_rows*sizeof(uint32_t*)); 00117 assert(m->elts); 00118 00119 for (row = 0; row < num_rows; row++) 00120 { 00121 m->elts[ row ] = &(elts[ row*num_cols ]); 00122 } 00123 m->num_rows = num_rows; 00124 m->num_cols = num_cols; 00125 } 00126 } 00127 00136 void create_matrix_i32 00137 ( 00138 Matrix_i32** m_out, 00139 uint32_t num_rows, 00140 uint32_t num_cols 00141 ) 00142 { 00143 uint32_t row; 00144 int32_t* elts; 00145 Matrix_i32* m; 00146 00147 assert(num_rows > 0 && num_cols > 0); 00148 00149 if (*m_out == NULL) 00150 { 00151 assert(*m_out = malloc(sizeof(Matrix_i32))); 00152 (*m_out)->num_rows = 0; 00153 (*m_out)->num_cols = 0; 00154 (*m_out)->elts = NULL; 00155 } 00156 m = *m_out; 00157 00158 if (m->num_rows != num_rows || m->num_cols != num_cols) 00159 { 00160 elts = (m->elts != NULL) ? *(m->elts) : NULL; 00161 elts = realloc(elts, num_rows*num_cols*sizeof(int32_t)); 00162 assert(elts); 00163 00164 m->elts = realloc(m->elts, num_rows*sizeof(int32_t*)); 00165 assert(m->elts); 00166 00167 for (row = 0; row < num_rows; row++) 00168 { 00169 m->elts[ row ] = &(elts[ row*num_cols ]); 00170 } 00171 m->num_rows = num_rows; 00172 m->num_cols = num_cols; 00173 } 00174 } 00175 00184 void create_matrix_i64 00185 ( 00186 Matrix_i64** m_out, 00187 uint32_t num_rows, 00188 uint32_t num_cols 00189 ) 00190 { 00191 uint32_t row; 00192 int64_t* elts; 00193 Matrix_i64* m; 00194 00195 assert(num_rows > 0 && num_cols > 0); 00196 00197 if (*m_out == NULL) 00198 { 00199 assert(*m_out = malloc(sizeof(Matrix_i64))); 00200 (*m_out)->num_rows = 0; 00201 (*m_out)->num_cols = 0; 00202 (*m_out)->elts = NULL; 00203 } 00204 m = *m_out; 00205 00206 if (m->num_rows != num_rows || m->num_cols != num_cols) 00207 { 00208 elts = (m->elts != NULL) ? *(m->elts) : NULL; 00209 elts = realloc(elts, num_rows*num_cols*sizeof(int64_t)); 00210 assert(elts); 00211 00212 m->elts = realloc(m->elts, num_rows*sizeof(int64_t*)); 00213 assert(m->elts); 00214 00215 for (row = 0; row < num_rows; row++) 00216 { 00217 m->elts[ row ] = &(elts[ row*num_cols ]); 00218 } 00219 m->num_rows = num_rows; 00220 m->num_cols = num_cols; 00221 } 00222 } 00223 00232 void create_matrix_f 00233 ( 00234 Matrix_f** m_out, 00235 uint32_t num_rows, 00236 uint32_t num_cols 00237 ) 00238 { 00239 uint32_t row; 00240 float* elts; 00241 Matrix_f* m; 00242 00243 assert(num_rows > 0 && num_cols > 0); 00244 00245 if (*m_out == NULL) 00246 { 00247 assert(*m_out = malloc(sizeof(Matrix_f))); 00248 (*m_out)->num_rows = 0; 00249 (*m_out)->num_cols = 0; 00250 (*m_out)->elts = NULL; 00251 } 00252 m = *m_out; 00253 00254 if (m->num_rows != num_rows || m->num_cols != num_cols) 00255 { 00256 elts = (m->elts != NULL) ? *(m->elts) : NULL; 00257 elts = realloc(elts, num_rows*num_cols*sizeof(float)); 00258 assert(elts); 00259 00260 m->elts = realloc(m->elts, num_rows*sizeof(float*)); 00261 assert(m->elts); 00262 00263 for (row = 0; row < num_rows; row++) 00264 { 00265 m->elts[ row ] = &(elts[ row*num_cols ]); 00266 } 00267 m->num_rows = num_rows; 00268 m->num_cols = num_cols; 00269 } 00270 } 00271 00280 void create_matrix_d 00281 ( 00282 Matrix_d** m_out, 00283 uint32_t num_rows, 00284 uint32_t num_cols 00285 ) 00286 { 00287 uint32_t row; 00288 double* elts; 00289 Matrix_d* m; 00290 00291 assert(num_rows > 0 && num_cols > 0); 00292 00293 if (*m_out == NULL) 00294 { 00295 assert(*m_out = malloc(sizeof(Matrix_d))); 00296 (*m_out)->num_rows = 0; 00297 (*m_out)->num_cols = 0; 00298 (*m_out)->elts = NULL; 00299 } 00300 m = *m_out; 00301 00302 if (m->num_rows != num_rows || m->num_cols != num_cols) 00303 { 00304 elts = (m->elts != NULL) ? *(m->elts) : NULL; 00305 elts = realloc(elts, num_rows*num_cols*sizeof(double)); 00306 assert(elts); 00307 00308 m->elts = realloc(m->elts, num_rows*sizeof(double*)); 00309 assert(m->elts); 00310 00311 for (row = 0; row < num_rows; row++) 00312 { 00313 m->elts[ row ] = &(elts[ row*num_cols ]); 00314 } 00315 m->num_rows = num_rows; 00316 m->num_cols = num_cols; 00317 } 00318 } 00319 00328 void create_matrix_cf 00329 ( 00330 Matrix_cf** m_out, 00331 uint32_t num_rows, 00332 uint32_t num_cols 00333 ) 00334 { 00335 uint32_t row; 00336 Complex_f* elts; 00337 Matrix_cf* m; 00338 00339 assert(num_rows > 0 && num_cols > 0); 00340 00341 if (*m_out == NULL) 00342 { 00343 assert(*m_out = malloc(sizeof(Matrix_cf))); 00344 (*m_out)->num_rows = 0; 00345 (*m_out)->num_cols = 0; 00346 (*m_out)->elts = NULL; 00347 } 00348 m = *m_out; 00349 00350 if (m->num_rows != num_rows || m->num_cols != num_cols) 00351 { 00352 elts = (m->elts != NULL) ? *(m->elts) : NULL; 00353 elts = realloc(elts, num_rows*num_cols*sizeof(Complex_f)); 00354 assert(elts); 00355 00356 m->elts = realloc(m->elts, num_rows*sizeof(Complex_f*)); 00357 assert(m->elts); 00358 00359 for (row = 0; row < num_rows; row++) 00360 { 00361 m->elts[ row ] = &(elts[ row*num_cols ]); 00362 } 00363 m->num_rows = num_rows; 00364 m->num_cols = num_cols; 00365 } 00366 } 00367 00376 void create_matrix_cd 00377 ( 00378 Matrix_cd** m_out, 00379 uint32_t num_rows, 00380 uint32_t num_cols 00381 ) 00382 { 00383 uint32_t row; 00384 Complex_d* elts; 00385 Matrix_cd* m; 00386 00387 assert(num_rows > 0 && num_cols > 0); 00388 00389 if (*m_out == NULL) 00390 { 00391 assert(*m_out = malloc(sizeof(Matrix_cd))); 00392 (*m_out)->num_rows = 0; 00393 (*m_out)->num_cols = 0; 00394 (*m_out)->elts = NULL; 00395 } 00396 m = *m_out; 00397 00398 if (m->num_rows != num_rows || m->num_cols != num_cols) 00399 { 00400 elts = (m->elts != NULL) ? *(m->elts) : NULL; 00401 elts = realloc(elts, num_rows*num_cols*sizeof(Complex_d)); 00402 assert(elts); 00403 00404 m->elts = realloc(m->elts, num_rows*sizeof(Complex_d*)); 00405 assert(m->elts); 00406 00407 for (row = 0; row < num_rows; row++) 00408 { 00409 m->elts[ row ] = &(elts[ row*num_cols ]); 00410 } 00411 m->num_rows = num_rows; 00412 m->num_cols = num_cols; 00413 } 00414 } 00415 00437 void create_init_matrix_u32 00438 ( 00439 Matrix_u32** m_out, 00440 uint32_t num_rows, 00441 uint32_t num_cols, 00442 uint32_t val 00443 ) 00444 { 00445 uint32_t num_elts; 00446 uint32_t elt; 00447 uint32_t* m_elts; 00448 00449 create_matrix_u32(m_out, num_rows, num_cols); 00450 m_elts = *((*m_out)->elts); 00451 num_elts = num_rows*num_cols; 00452 00453 for (elt = 0; elt < num_elts; elt++) 00454 { 00455 m_elts[ elt ] = val; 00456 } 00457 } 00458 00468 void create_init_matrix_i32 00469 ( 00470 Matrix_i32** m_out, 00471 uint32_t num_rows, 00472 uint32_t num_cols, 00473 int32_t val 00474 ) 00475 { 00476 uint32_t num_elts; 00477 uint32_t elt; 00478 int32_t* m_elts; 00479 00480 create_matrix_i32(m_out, num_rows, num_cols); 00481 m_elts = *((*m_out)->elts); 00482 num_elts = num_rows*num_cols; 00483 00484 for (elt = 0; elt < num_elts; elt++) 00485 { 00486 m_elts[ elt ] = val; 00487 } 00488 } 00489 00499 void create_init_matrix_i64 00500 ( 00501 Matrix_i64** m_out, 00502 uint32_t num_rows, 00503 uint32_t num_cols, 00504 int64_t val 00505 ) 00506 { 00507 uint32_t num_elts; 00508 uint32_t elt; 00509 int64_t* m_elts; 00510 00511 create_matrix_i64(m_out, num_rows, num_cols); 00512 m_elts = *((*m_out)->elts); 00513 num_elts = num_rows*num_cols; 00514 00515 for (elt = 0; elt < num_elts; elt++) 00516 { 00517 m_elts[ elt ] = val; 00518 } 00519 } 00520 00530 void create_init_matrix_f 00531 ( 00532 Matrix_f** m_out, 00533 uint32_t num_rows, 00534 uint32_t num_cols, 00535 float val 00536 ) 00537 { 00538 uint32_t num_elts; 00539 uint32_t elt; 00540 float* m_elts; 00541 00542 create_matrix_f(m_out, num_rows, num_cols); 00543 m_elts = *((*m_out)->elts); 00544 num_elts = num_rows*num_cols; 00545 00546 for (elt = 0; elt < num_elts; elt++) 00547 { 00548 m_elts[ elt ] = val; 00549 } 00550 } 00551 00561 void create_init_matrix_d 00562 ( 00563 Matrix_d** m_out, 00564 uint32_t num_rows, 00565 uint32_t num_cols, 00566 double val 00567 ) 00568 { 00569 uint32_t num_elts; 00570 uint32_t elt; 00571 double* m_elts; 00572 00573 create_matrix_d(m_out, num_rows, num_cols); 00574 m_elts = *((*m_out)->elts); 00575 num_elts = num_rows*num_cols; 00576 00577 for (elt = 0; elt < num_elts; elt++) 00578 { 00579 m_elts[ elt ] = val; 00580 } 00581 } 00582 00592 void create_init_matrix_cf 00593 ( 00594 Matrix_cf** m_out, 00595 uint32_t num_rows, 00596 uint32_t num_cols, 00597 Complex_f val 00598 ) 00599 { 00600 uint32_t num_elts; 00601 uint32_t elt; 00602 Complex_f* m_elts; 00603 00604 create_matrix_cf(m_out, num_rows, num_cols); 00605 m_elts = *((*m_out)->elts); 00606 num_elts = num_rows*num_cols; 00607 00608 for (elt = 0; elt < num_elts; elt++) 00609 { 00610 m_elts[ elt ] = val; 00611 } 00612 } 00613 00623 void create_init_matrix_cd 00624 ( 00625 Matrix_cd** m_out, 00626 uint32_t num_rows, 00627 uint32_t num_cols, 00628 Complex_d val 00629 ) 00630 { 00631 uint32_t num_elts; 00632 uint32_t elt; 00633 Complex_d* m_elts; 00634 00635 create_matrix_cd(m_out, num_rows, num_cols); 00636 m_elts = *((*m_out)->elts); 00637 num_elts = num_rows*num_cols; 00638 00639 for (elt = 0; elt < num_elts; elt++) 00640 { 00641 m_elts[ elt ] = val; 00642 } 00643 } 00644 00663 void create_identity_matrix_u32(Matrix_u32** m_out, uint32_t size) 00664 { 00665 uint32_t elt; 00666 00667 create_zero_matrix_u32(m_out, size, size); 00668 00669 for (elt = 0; elt < size; elt++) 00670 { 00671 (*m_out)->elts[ elt ][ elt ] = 1; 00672 } 00673 } 00674 00680 void create_identity_matrix_i32(Matrix_i32** m_out, uint32_t size) 00681 { 00682 uint32_t elt; 00683 00684 create_zero_matrix_i32(m_out, size, size); 00685 00686 for (elt = 0; elt < size; elt++) 00687 { 00688 (*m_out)->elts[ elt ][ elt ] = 1; 00689 } 00690 } 00691 00697 void create_identity_matrix_i64(Matrix_i64** m_out, uint32_t size) 00698 { 00699 uint32_t elt; 00700 00701 create_zero_matrix_i64(m_out, size, size); 00702 00703 for (elt = 0; elt < size; elt++) 00704 { 00705 (*m_out)->elts[ elt ][ elt ] = 1; 00706 } 00707 } 00708 00714 void create_identity_matrix_f(Matrix_f** m_out, uint32_t size) 00715 { 00716 uint32_t elt; 00717 00718 create_zero_matrix_f(m_out, size, size); 00719 00720 for (elt = 0; elt < size; elt++) 00721 { 00722 (*m_out)->elts[ elt ][ elt ] = 1; 00723 } 00724 } 00725 00731 void create_identity_matrix_d(Matrix_d** m_out, uint32_t size) 00732 { 00733 uint32_t elt; 00734 00735 create_zero_matrix_d(m_out, size, size); 00736 00737 for (elt = 0; elt < size; elt++) 00738 { 00739 (*m_out)->elts[ elt ][ elt ] = 1; 00740 } 00741 } 00742 00764 void create_zero_matrix_u32 00765 ( 00766 Matrix_u32** m_out, 00767 uint32_t num_rows, 00768 uint32_t num_cols 00769 ) 00770 { 00771 create_init_matrix_u32(m_out, num_rows, num_cols, 0); 00772 } 00773 00782 void create_zero_matrix_i32 00783 ( 00784 Matrix_i32** m_out, 00785 uint32_t num_rows, 00786 uint32_t num_cols 00787 ) 00788 { 00789 create_init_matrix_i32(m_out, num_rows, num_cols, 0); 00790 } 00791 00798 void create_zero_matrix_i64 00799 ( 00800 Matrix_i64** m_out, 00801 uint32_t num_rows, 00802 uint32_t num_cols 00803 ) 00804 { 00805 create_init_matrix_i64(m_out, num_rows, num_cols, 0); 00806 } 00807 00816 void create_zero_matrix_f 00817 ( 00818 Matrix_f** m_out, 00819 uint32_t num_rows, 00820 uint32_t num_cols 00821 ) 00822 { 00823 create_init_matrix_f(m_out, num_rows, num_cols, 0); 00824 } 00825 00834 void create_zero_matrix_d 00835 ( 00836 Matrix_d** m_out, 00837 uint32_t num_rows, 00838 uint32_t num_cols 00839 ) 00840 { 00841 create_init_matrix_d(m_out, num_rows, num_cols, 0); 00842 } 00843 00852 void create_zero_matrix_cf 00853 ( 00854 Matrix_cf** m_out, 00855 uint32_t num_rows, 00856 uint32_t num_cols 00857 ) 00858 { 00859 Complex_f z = {0}; 00860 create_init_matrix_cf(m_out, num_rows, num_cols, z); 00861 } 00862 00871 void create_zero_matrix_cd 00872 ( 00873 Matrix_cd** m_out, 00874 uint32_t num_rows, 00875 uint32_t num_cols 00876 ) 00877 { 00878 Complex_d z = {0}; 00879 create_init_matrix_cd(m_out, num_rows, num_cols, z); 00880 } 00881 00896 void create_diagnonal_matrix_u32 00897 ( 00898 Matrix_u32** m_out, 00899 const Vector_u32* v 00900 ) 00901 { 00902 uint32_t n, N; 00903 Matrix_u32* m; 00904 00905 N = v->num_elts; 00906 00907 create_zero_matrix_u32(m_out, N, N); 00908 m = *m_out; 00909 00910 for (n = 0; n < N; n++) 00911 { 00912 m->elts[ n ][ n ] = v->elts[ n ]; 00913 } 00914 } 00915 00917 void create_diagonal_matrix_i32 00918 ( 00919 Matrix_i32** m_out, 00920 const Vector_i32* v 00921 ) 00922 { 00923 uint32_t n, N; 00924 Matrix_i32* m; 00925 00926 N = v->num_elts; 00927 00928 create_zero_matrix_i32(m_out, N, N); 00929 m = *m_out; 00930 00931 for (n = 0; n < N; n++) 00932 { 00933 m->elts[ n ][ n ] = v->elts[ n ]; 00934 } 00935 } 00936 00938 void create_diagonal_matrix_i64 00939 ( 00940 Matrix_i64** m_out, 00941 const Vector_i64* v 00942 ) 00943 { 00944 uint32_t n, N; 00945 Matrix_i64* m; 00946 00947 N = v->num_elts; 00948 00949 create_zero_matrix_i64(m_out, N, N); 00950 m = *m_out; 00951 00952 for (n = 0; n < N; n++) 00953 { 00954 m->elts[ n ][ n ] = v->elts[ n ]; 00955 } 00956 } 00957 00959 void create_diagonal_matrix_f 00960 ( 00961 Matrix_f** m_out, 00962 const Vector_f* v 00963 ) 00964 { 00965 uint32_t n, N; 00966 Matrix_f* m; 00967 00968 N = v->num_elts; 00969 00970 create_zero_matrix_f(m_out, N, N); 00971 m = *m_out; 00972 00973 for (n = 0; n < N; n++) 00974 { 00975 m->elts[ n ][ n ] = v->elts[ n ]; 00976 } 00977 } 00978 00980 void create_diagonal_matrix_d 00981 ( 00982 Matrix_d** m_out, 00983 const Vector_d* v 00984 ) 00985 { 00986 uint32_t n, N; 00987 Matrix_d* m; 00988 00989 N = v->num_elts; 00990 00991 create_zero_matrix_d(m_out, N, N); 00992 m = *m_out; 00993 00994 for (n = 0; n < N; n++) 00995 { 00996 m->elts[ n ][ n ] = v->elts[ n ]; 00997 } 00998 } 00999 01001 void create_diagonal_matrix_cf 01002 ( 01003 Matrix_cf** m_out, 01004 const Vector_cf* v 01005 ) 01006 { 01007 uint32_t n, N; 01008 Matrix_cf* m; 01009 01010 N = v->num_elts; 01011 01012 create_zero_matrix_cf(m_out, N, N); 01013 m = *m_out; 01014 01015 for (n = 0; n < N; n++) 01016 { 01017 m->elts[ n ][ n ] = v->elts[ n ]; 01018 } 01019 } 01020 01022 void create_diagonal_matrix_cd 01023 ( 01024 Matrix_cd** m_out, 01025 const Vector_cd* v 01026 ) 01027 { 01028 uint32_t n, N; 01029 Matrix_cd* m; 01030 01031 N = v->num_elts; 01032 01033 create_zero_matrix_cd(m_out, N, N); 01034 m = *m_out; 01035 01036 for (n = 0; n < N; n++) 01037 { 01038 m->elts[ n ][ n ] = v->elts[ n ]; 01039 } 01040 } 01041 01070 void create_random_matrix_u32 01071 ( 01072 Matrix_u32** m_out, 01073 uint32_t num_rows, 01074 uint32_t num_cols, 01075 uint32_t min, 01076 uint32_t max 01077 ) 01078 { 01079 uint32_t num_elts; 01080 uint32_t elt; 01081 uint32_t* m_elts; 01082 double r; 01083 01084 create_matrix_u32(m_out, num_rows, num_cols); 01085 m_elts = *((*m_out)->elts); 01086 num_elts = num_rows*num_cols; 01087 01088 for (elt = 0; elt < num_elts; elt++) 01089 { 01090 r = (double)rand() / (double)RAND_MAX; 01091 m_elts[ elt ] = min + (uint32_t)floor((max - min)*r + 0.5); 01092 } 01093 } 01094 01110 void create_random_matrix_i32 01111 ( 01112 Matrix_i32** m_out, 01113 uint32_t num_rows, 01114 uint32_t num_cols, 01115 int32_t min, 01116 int32_t max 01117 ) 01118 { 01119 uint32_t num_elts; 01120 uint32_t elt; 01121 int32_t* m_elts; 01122 double r; 01123 01124 create_matrix_i32(m_out, num_rows, num_cols); 01125 m_elts = *((*m_out)->elts); 01126 num_elts = num_rows*num_cols; 01127 01128 for (elt = 0; elt < num_elts; elt++) 01129 { 01130 r = (double)rand() / (double)RAND_MAX; 01131 m_elts[ elt ] = min + (int32_t)floor((max - min)*r + 0.5); 01132 } 01133 } 01134 01150 void create_random_matrix_i64 01151 ( 01152 Matrix_i64** m_out, 01153 uint32_t num_rows, 01154 uint32_t num_cols, 01155 int64_t min, 01156 int64_t max 01157 ) 01158 { 01159 uint32_t num_elts; 01160 uint32_t elt; 01161 int64_t* m_elts; 01162 double r; 01163 01164 create_matrix_i64(m_out, num_rows, num_cols); 01165 m_elts = *((*m_out)->elts); 01166 num_elts = num_rows*num_cols; 01167 01168 for (elt = 0; elt < num_elts; elt++) 01169 { 01170 r = (double)rand() / (double)RAND_MAX; 01171 m_elts[ elt ] = min + (int64_t)floor((max - min)*r + 0.5); 01172 } 01173 } 01174 01190 void create_random_matrix_f 01191 ( 01192 Matrix_f** m_out, 01193 uint32_t num_rows, 01194 uint32_t num_cols, 01195 float min, 01196 float max 01197 ) 01198 { 01199 uint32_t num_elts; 01200 uint32_t elt; 01201 float* m_elts; 01202 float r; 01203 01204 create_matrix_f(m_out, num_rows, num_cols); 01205 m_elts = *((*m_out)->elts); 01206 num_elts = num_rows*num_cols; 01207 01208 for (elt = 0; elt < num_elts; elt++) 01209 { 01210 r = (float)rand() / (float)RAND_MAX; 01211 m_elts[ elt ] = min + (max - min)*r; 01212 } 01213 } 01214 01230 void create_random_matrix_d 01231 ( 01232 Matrix_d** m_out, 01233 uint32_t num_rows, 01234 uint32_t num_cols, 01235 double min, 01236 double max 01237 ) 01238 { 01239 uint32_t num_elts; 01240 uint32_t elt; 01241 double* m_elts; 01242 double r; 01243 01244 create_matrix_d(m_out, num_rows, num_cols); 01245 m_elts = *((*m_out)->elts); 01246 num_elts = num_rows*num_cols; 01247 01248 for (elt = 0; elt < num_elts; elt++) 01249 { 01250 r = (double)rand() / (double)RAND_MAX; 01251 m_elts[ elt ] = min + (max - min)*r; 01252 } 01253 } 01254 01270 void create_random_matrix_cf 01271 ( 01272 Matrix_cf** m_out, 01273 uint32_t num_rows, 01274 uint32_t num_cols, 01275 Complex_f min, 01276 Complex_f max 01277 ) 01278 { 01279 uint32_t num_elts; 01280 uint32_t elt; 01281 Complex_f* m_elts; 01282 float r; 01283 01284 create_matrix_cf(m_out, num_rows, num_cols); 01285 m_elts = *((*m_out)->elts); 01286 num_elts = num_rows*num_cols; 01287 01288 for (elt = 0; elt < num_elts; elt++) 01289 { 01290 r = (float)rand() / (float)RAND_MAX; 01291 m_elts[ elt ].r = min.r + (max.r - min.r)*r; 01292 r = (float)rand() / (float)RAND_MAX; 01293 m_elts[ elt ].i = min.i + (max.i - min.i)*r; 01294 } 01295 } 01296 01312 void create_random_matrix_cd 01313 ( 01314 Matrix_cd** m_out, 01315 uint32_t num_rows, 01316 uint32_t num_cols, 01317 Complex_d min, 01318 Complex_d max 01319 ) 01320 { 01321 uint32_t num_elts; 01322 uint32_t elt; 01323 Complex_d* m_elts; 01324 double r; 01325 01326 create_matrix_cd(m_out, num_rows, num_cols); 01327 m_elts = *((*m_out)->elts); 01328 num_elts = num_rows*num_cols; 01329 01330 for (elt = 0; elt < num_elts; elt++) 01331 { 01332 r = (double)rand() / (double)RAND_MAX; 01333 m_elts[ elt ].r = min.r + (max.r - min.r)*r; 01334 r = (double)rand() / (double)RAND_MAX; 01335 m_elts[ elt ].i = min.i + (max.i - min.i)*r; 01336 } 01337 } 01338 01363 Error* copy_matrix_u32(Matrix_u32** m_out, const Matrix_u32* m_in) 01364 { 01365 uint32_t num_elts; 01366 uint32_t elt; 01367 uint32_t* m_out_elts; 01368 uint32_t* m_in_elts; 01369 01370 if (*m_out == m_in) 01371 { 01372 return JWSC_EARG("Copying a matrix into itself is not supported"); 01373 } 01374 01375 create_matrix_u32(m_out, m_in->num_rows, m_in->num_cols); 01376 num_elts = m_in->num_rows*m_in->num_cols; 01377 01378 m_out_elts = *((*m_out)->elts); 01379 m_in_elts = *(m_in->elts); 01380 01381 for (elt = 0; elt < num_elts; elt++) 01382 { 01383 m_out_elts[ elt ] = m_in_elts[ elt ]; 01384 } 01385 01386 return NULL; 01387 } 01388 01401 Error* copy_matrix_i32(Matrix_i32** m_out, const Matrix_i32* m_in) 01402 { 01403 uint32_t num_elts; 01404 uint32_t elt; 01405 int32_t* m_out_elts; 01406 int32_t* m_in_elts; 01407 01408 if (*m_out == m_in) 01409 { 01410 return JWSC_EARG("Copying a matrix into itself is not supported"); 01411 } 01412 01413 create_matrix_i32(m_out, m_in->num_rows, m_in->num_cols); 01414 num_elts = m_in->num_rows*m_in->num_cols; 01415 01416 m_out_elts = *((*m_out)->elts); 01417 m_in_elts = *(m_in->elts); 01418 01419 for (elt = 0; elt < num_elts; elt++) 01420 { 01421 m_out_elts[ elt ] = m_in_elts[ elt ]; 01422 } 01423 01424 return NULL; 01425 } 01426 01439 Error* copy_matrix_i64(Matrix_i64** m_out, const Matrix_i64* m_in) 01440 { 01441 uint32_t num_elts; 01442 uint32_t elt; 01443 int64_t* m_out_elts; 01444 int64_t* m_in_elts; 01445 01446 if (*m_out == m_in) 01447 { 01448 return JWSC_EARG("Copying a matrix into itself is not supported"); 01449 } 01450 01451 create_matrix_i64(m_out, m_in->num_rows, m_in->num_cols); 01452 num_elts = m_in->num_rows*m_in->num_cols; 01453 01454 m_out_elts = *((*m_out)->elts); 01455 m_in_elts = *(m_in->elts); 01456 01457 for (elt = 0; elt < num_elts; elt++) 01458 { 01459 m_out_elts[ elt ] = m_in_elts[ elt ]; 01460 } 01461 01462 return NULL; 01463 } 01464 01479 Error* copy_matrix_f(Matrix_f** m_out, const Matrix_f* m_in) 01480 { 01481 uint32_t num_elts; 01482 float* m_out_elts; 01483 float* m_in_elts; 01484 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01485 #else 01486 uint32_t elt; 01487 #endif 01488 01489 if (*m_out == m_in) 01490 { 01491 return JWSC_EARG("Copying a matrix into itself is not supported"); 01492 } 01493 01494 create_matrix_f(m_out, m_in->num_rows, m_in->num_cols); 01495 num_elts = m_in->num_rows*m_in->num_cols; 01496 01497 m_out_elts = *((*m_out)->elts); 01498 m_in_elts = *(m_in->elts); 01499 01500 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01501 blas_scopy(num_elts, m_in_elts, 1, m_out_elts, 1); 01502 #else 01503 for (elt = 0; elt < num_elts; elt++) 01504 { 01505 m_out_elts[ elt ] = m_in_elts[ elt ]; 01506 } 01507 #endif 01508 01509 return NULL; 01510 } 01511 01524 Error* copy_matrix_d(Matrix_d** m_out, const Matrix_d* m_in) 01525 { 01526 uint32_t num_elts; 01527 double* m_out_elts; 01528 double* m_in_elts; 01529 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01530 #else 01531 uint32_t elt; 01532 #endif 01533 01534 if (*m_out == m_in) 01535 { 01536 return JWSC_EARG("Copying a matrix into itself is not supported"); 01537 } 01538 01539 create_matrix_d(m_out, m_in->num_rows, m_in->num_cols); 01540 num_elts = m_in->num_rows*m_in->num_cols; 01541 01542 m_out_elts = *((*m_out)->elts); 01543 m_in_elts = *(m_in->elts); 01544 01545 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01546 blas_dcopy(num_elts, m_in_elts, 1, m_out_elts, 1); 01547 #else 01548 for (elt = 0; elt < num_elts; elt++) 01549 { 01550 m_out_elts[ elt ] = m_in_elts[ elt ]; 01551 } 01552 #endif 01553 01554 return NULL; 01555 } 01556 01569 Error* copy_matrix_cf(Matrix_cf** m_out, const Matrix_cf* m_in) 01570 { 01571 uint32_t num_elts; 01572 Complex_f* m_out_elts; 01573 Complex_f* m_in_elts; 01574 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01575 #else 01576 uint32_t elt; 01577 #endif 01578 01579 if (*m_out == m_in) 01580 { 01581 return JWSC_EARG("Copying a matrix into itself is not supported"); 01582 } 01583 01584 create_matrix_cf(m_out, m_in->num_rows, m_in->num_cols); 01585 num_elts = m_in->num_rows*m_in->num_cols; 01586 01587 m_out_elts = *((*m_out)->elts); 01588 m_in_elts = *(m_in->elts); 01589 01590 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01591 blas_ccopy(num_elts, m_in_elts, 1, m_out_elts, 1); 01592 #else 01593 for (elt = 0; elt < num_elts; elt++) 01594 { 01595 m_out_elts[ elt ] = m_in_elts[ elt ]; 01596 } 01597 #endif 01598 01599 return NULL; 01600 } 01601 01614 Error* copy_matrix_cd(Matrix_cd** m_out, const Matrix_cd* m_in) 01615 { 01616 uint32_t num_elts; 01617 Complex_d* m_out_elts; 01618 Complex_d* m_in_elts; 01619 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01620 #else 01621 uint32_t elt; 01622 #endif 01623 01624 if (*m_out == m_in) 01625 { 01626 return JWSC_EARG("Copying a matrix into itself is not supported"); 01627 } 01628 01629 create_matrix_cd(m_out, m_in->num_rows, m_in->num_cols); 01630 num_elts = m_in->num_rows*m_in->num_cols; 01631 01632 m_out_elts = *((*m_out)->elts); 01633 m_in_elts = *(m_in->elts); 01634 01635 #if defined JWSC_HAVE_BLAS || defined JWSC_HAVE_ACCELERATE 01636 blas_zcopy(num_elts, m_in_elts, 1, m_out_elts, 1); 01637 #else 01638 for (elt = 0; elt < num_elts; elt++) 01639 { 01640 m_out_elts[ elt ] = m_in_elts[ elt ]; 01641 } 01642 #endif 01643 01644 return NULL; 01645 } 01646 01680 Error* copy_matrix_block_u32 01681 ( 01682 Matrix_u32** m_out, 01683 const Matrix_u32* m_in, 01684 uint32_t row_offset, 01685 uint32_t col_offset, 01686 uint32_t num_rows, 01687 uint32_t num_cols 01688 ) 01689 { 01690 uint32_t row, col; 01691 Matrix_u32* m; 01692 01693 if (*m_out == m_in) 01694 { 01695 return JWSC_EARG("Copying a matrix into itself is not supported"); 01696 } 01697 01698 if (m_in->num_rows < (row_offset+num_rows) || 01699 m_in->num_cols < (col_offset+num_cols)) 01700 { 01701 free_matrix_u32(*m_out); m_out = NULL; 01702 return JWSC_EARG("Section to copy outside bounds of input matrix"); 01703 } 01704 01705 create_matrix_u32(m_out, num_rows, num_cols); 01706 m = *m_out; 01707 01708 for (row = 0; row < num_rows; row++) 01709 { 01710 for (col = 0; col < num_cols; col++) 01711 { 01712 m->elts[ row ][ col ] 01713 = m_in->elts[ row+row_offset ][ col+col_offset ]; 01714 } 01715 } 01716 01717 return NULL; 01718 } 01719 01741 Error* copy_matrix_block_i32 01742 ( 01743 Matrix_i32** m_out, 01744 const Matrix_i32* m_in, 01745 uint32_t row_offset, 01746 uint32_t col_offset, 01747 uint32_t num_rows, 01748 uint32_t num_cols 01749 ) 01750 { 01751 uint32_t row, col; 01752 Matrix_i32* m; 01753 01754 if (*m_out == m_in) 01755 { 01756 return JWSC_EARG("Copying a matrix into itself is not supported"); 01757 } 01758 01759 if (m_in->num_rows < (row_offset+num_rows) || 01760 m_in->num_cols < (col_offset+num_cols)) 01761 { 01762 free_matrix_i32(*m_out); m_out = NULL; 01763 return JWSC_EARG("Section to copy outside bounds of input matrix"); 01764 } 01765 01766 create_matrix_i32(m_out, num_rows, num_cols); 01767 m = *m_out; 01768 01769 for (row = 0; row < num_rows; row++) 01770 { 01771 for (col = 0; col < num_cols; col++) 01772 { 01773 m->elts[ row ][ col ] 01774 = m_in->elts[ row+row_offset ][ col+col_offset ]; 01775 } 01776 } 01777 01778 return NULL; 01779 } 01780 01802 Error* copy_matrix_block_i64 01803 ( 01804 Matrix_i64** m_out, 01805 const Matrix_i64* m_in, 01806 uint32_t row_offset, 01807 uint32_t col_offset, 01808 uint32_t num_rows, 01809 uint32_t num_cols 01810 ) 01811 { 01812 uint32_t row, col; 01813 Matrix_i64* m; 01814 01815 if (*m_out == m_in) 01816 { 01817 return JWSC_EARG("Copying a matrix into itself is not supported"); 01818 } 01819 01820 if (m_in->num_rows < (row_offset+num_rows) || 01821 m_in->num_cols < (col_offset+num_cols)) 01822 { 01823 free_matrix_i64(*m_out); m_out = NULL; 01824 return JWSC_EARG("Section to copy outside bounds of input matrix"); 01825 } 01826 01827 create_matrix_i64(m_out, num_rows, num_cols); 01828 m = *m_out; 01829 01830 for (row = 0; row < num_rows; row++) 01831 { 01832 for (col = 0; col < num_cols; col++) 01833 { 01834 m->elts[ row ][ col ] 01835 = m_in->elts[ row+row_offset ][ col+col_offset ]; 01836 } 01837 } 01838 01839 return NULL; 01840 } 01841 01863 Error* copy_matrix_block_f 01864 ( 01865 Matrix_f** m_out, 01866 const Matrix_f* m_in, 01867 uint32_t row_offset, 01868 uint32_t col_offset, 01869 uint32_t num_rows, 01870 uint32_t num_cols 01871 ) 01872 { 01873 uint32_t row, col; 01874 Matrix_f* m; 01875 01876 if (*m_out == m_in) 01877 { 01878 return JWSC_EARG("Copying a matrix into itself is not supported"); 01879 } 01880 01881 if (m_in->num_rows < (row_offset+num_rows) || 01882 m_in->num_cols < (col_offset+num_cols)) 01883 { 01884 free_matrix_f(*m_out); m_out = NULL; 01885 return JWSC_EARG("Section to copy outside bounds of input matrix"); 01886 } 01887 01888 create_matrix_f(m_out, num_rows, num_cols); 01889 m = *m_out; 01890 01891 for (row = 0; row < num_rows; row++) 01892 { 01893 for (col = 0; col < num_cols; col++) 01894 { 01895 m->elts[ row ][ col ] 01896 = m_in->elts[ row+row_offset ][ col+col_offset ]; 01897 } 01898 } 01899 01900 return NULL; 01901 } 01902 01924 Error* copy_matrix_block_d 01925 ( 01926 Matrix_d** m_out, 01927 const Matrix_d* m_in, 01928 uint32_t row_offset, 01929 uint32_t col_offset, 01930 uint32_t num_rows, 01931 uint32_t num_cols 01932 ) 01933 { 01934 uint32_t row, col; 01935 Matrix_d* m; 01936 01937 if (*m_out == m_in) 01938 { 01939 return JWSC_EARG("Copying a matrix into itself is not supported"); 01940 } 01941 01942 if (m_in->num_rows < (row_offset+num_rows) || 01943 m_in->num_cols < (col_offset+num_cols)) 01944 { 01945 free_matrix_d(*m_out); m_out = NULL; 01946 return JWSC_EARG("Section to copy outside bounds of input matrix"); 01947 } 01948 01949 create_matrix_d(m_out, num_rows, num_cols); 01950 m = *m_out; 01951 01952 for (row = 0; row < num_rows; row++) 01953 { 01954 for (col = 0; col < num_cols; col++) 01955 { 01956 m->elts[ row ][ col ] 01957 = m_in->elts[ row+row_offset ][ col+col_offset ]; 01958 } 01959 } 01960 01961 return NULL; 01962 } 01963 01985 Error* copy_matrix_block_cf 01986 ( 01987 Matrix_cf** m_out, 01988 const Matrix_cf* m_in, 01989 uint32_t row_offset, 01990 uint32_t col_offset, 01991 uint32_t num_rows, 01992 uint32_t num_cols 01993 ) 01994 { 01995 uint32_t row, col; 01996 Matrix_cf* m; 01997 01998 if (*m_out == m_in) 01999 { 02000 return JWSC_EARG("Copying a matrix into itself is not supported"); 02001 } 02002 02003 if (m_in->num_rows < (row_offset+num_rows) || 02004 m_in->num_cols < (col_offset+num_cols)) 02005 { 02006 free_matrix_cf(*m_out); m_out = NULL; 02007 return JWSC_EARG("Section to copy outside bounds of input matrix"); 02008 } 02009 02010 create_matrix_cf(m_out, num_rows, num_cols); 02011 m = *m_out; 02012 02013 for (row = 0; row < num_rows; row++) 02014 { 02015 for (col = 0; col < num_cols; col++) 02016 { 02017 m->elts[ row ][ col ] 02018 = m_in->elts[ row+row_offset ][ col+col_offset ]; 02019 } 02020 } 02021 02022 return NULL; 02023 } 02024 02046 Error* copy_matrix_block_cd 02047 ( 02048 Matrix_cd** m_out, 02049 const Matrix_cd* m_in, 02050 uint32_t row_offset, 02051 uint32_t col_offset, 02052 uint32_t num_rows, 02053 uint32_t num_cols 02054 ) 02055 { 02056 uint32_t row, col; 02057 Matrix_cd* m; 02058 02059 if (*m_out == m_in) 02060 { 02061 return JWSC_EARG("Copying a matrix into itself is not supported"); 02062 } 02063 02064 if (m_in->num_rows < (row_offset+num_rows) || 02065 m_in->num_cols < (col_offset+num_cols)) 02066 { 02067 free_matrix_cd(*m_out); m_out = NULL; 02068 return JWSC_EARG("Section to copy outside bounds of input matrix"); 02069 } 02070 02071 create_matrix_cd(m_out, num_rows, num_cols); 02072 m = *m_out; 02073 02074 for (row = 0; row < num_rows; row++) 02075 { 02076 for (col = 0; col < num_cols; col++) 02077 { 02078 m->elts[ row ][ col ] 02079 = m_in->elts[ row+row_offset ][ col+col_offset ]; 02080 } 02081 } 02082 02083 return NULL; 02084 } 02085 02117 Error* copy_matrix_block_into_matrix_u32 02118 ( 02119 Matrix_u32* m_1, 02120 uint32_t row_offset_1, 02121 uint32_t col_offset_1, 02122 const Matrix_u32* m_2, 02123 uint32_t row_offset_2, 02124 uint32_t col_offset_2, 02125 uint32_t num_rows, 02126 uint32_t num_cols 02127 ) 02128 { 02129 uint32_t row, col; 02130 02131 if (m_1 == m_2) 02132 { 02133 return JWSC_EARG("Copying a matrix into itself is not supported"); 02134 } 02135 02136 /* Test if the size of the block to copy is in the bounds of the input 02137 * matrix. */ 02138 if (m_2->num_rows < (row_offset_2+num_rows) || 02139 m_2->num_cols < (col_offset_2+num_cols) || 02140 m_1->num_rows < (row_offset_1+num_rows) || 02141 m_1->num_cols < (col_offset_1+num_cols)) 02142 { 02143 return JWSC_EARG("Block to copy outside bounds of input matrix"); 02144 } 02145 02146 for (row = 0; row < num_rows; row++) 02147 { 02148 for (col = 0; col < num_cols; col++) 02149 { 02150 m_1->elts[ row+row_offset_1 ][ col+col_offset_1 ] 02151 = m_2->elts[ row+row_offset_2 ][ col+col_offset_2 ]; 02152 } 02153 } 02154 02155 return NULL; 02156 } 02157 02177 Error* copy_matrix_block_into_matrix_i32 02178 ( 02179 Matrix_i32* m_1, 02180 uint32_t row_offset_1, 02181 uint32_t col_offset_1, 02182 const Matrix_i32* m_2, 02183 uint32_t row_offset_2, 02184 uint32_t col_offset_2, 02185 uint32_t num_rows, 02186 uint32_t num_cols 02187 ) 02188 { 02189 uint32_t row, col; 02190 02191 if (m_1 == m_2) 02192 { 02193 return JWSC_EARG("Copying a matrix into itself is not supported"); 02194 } 02195 02196 /* Test if the size of the block to copy is in the bounds of the input 02197 * matrix. */ 02198 if (m_2->num_rows < (row_offset_2+num_rows) || 02199 m_2->num_cols < (col_offset_2+num_cols) || 02200 m_1->num_rows < (row_offset_1+num_rows) || 02201 m_1->num_cols < (col_offset_1+num_cols)) 02202 { 02203 return JWSC_EARG("Block to copy outside bounds of input matrix"); 02204 } 02205 02206 for (row = 0; row < num_rows; row++) 02207 { 02208 for (col = 0; col < num_cols; col++) 02209 { 02210 m_1->elts[ row+row_offset_1 ][ col+col_offset_1 ] 02211 = m_2->elts[ row+row_offset_2 ][ col+col_offset_2 ]; 02212 } 02213 } 02214 02215 return NULL; 02216 } 02217 02237 Error* copy_matrix_block_into_matrix_i64 02238 ( 02239 Matrix_i64* m_1, 02240 uint32_t row_offset_1, 02241 uint32_t col_offset_1, 02242 const Matrix_i64* m_2, 02243 uint32_t row_offset_2, 02244 uint32_t col_offset_2, 02245 uint32_t num_rows, 02246 uint32_t num_cols 02247 ) 02248 { 02249 uint32_t row, col; 02250 02251 if (m_1 == m_2) 02252 { 02253 return JWSC_EARG("Copying a matrix into itself is not supported"); 02254 } 02255 02256 /* Test if the size of the block to copy is in the bounds of the input 02257 * matrix. */ 02258 if (m_2->num_rows < (row_offset_2+num_rows) || 02259 m_2->num_cols < (col_offset_2+num_cols) || 02260 m_1->num_rows < (row_offset_1+num_rows) || 02261 m_1->num_cols < (col_offset_1+num_cols)) 02262 { 02263 return JWSC_EARG("Block to copy outside bounds of input matrix"); 02264 } 02265 02266 for (row = 0; row < num_rows; row++) 02267 { 02268 for (col = 0; col < num_cols; col++) 02269 { 02270 m_1->elts[ row+row_offset_1 ][ col+col_offset_1 ] 02271 = m_2->elts[ row+row_offset_2 ][ col+col_offset_2 ]; 02272 } 02273 } 02274 02275 return NULL; 02276 } 02277 02297 Error* copy_matrix_block_into_matrix_f 02298 ( 02299 Matrix_f* m_1, 02300 uint32_t row_offset_1, 02301 uint32_t col_offset_1, 02302 const Matrix_f* m_2, 02303 uint32_t row_offset_2, 02304 uint32_t col_offset_2, 02305 uint32_t num_rows, 02306 uint32_t num_cols 02307 ) 02308 { 02309 uint32_t row, col; 02310 02311 if (m_1 == m_2) 02312 { 02313 return JWSC_EARG("Copying a matrix into itself is not supported"); 02314 } 02315 02316 /* Test if the size of the block to copy is in the bounds of the input 02317 * matrix. */ 02318 if (m_2->num_rows < (row_offset_2+num_rows) || 02319 m_2->num_cols < (col_offset_2+num_cols) || 02320 m_1->num_rows < (row_offset_1+num_rows) || 02321 m_1->num_cols < (col_offset_1+num_cols)) 02322 { 02323 return JWSC_EARG("Block to copy outside bounds of input matrix"); 02324 } 02325 02326 for (row = 0; row < num_rows; row++) 02327 { 02328 for (col = 0; col < num_cols; col++) 02329 { 02330 m_1->elts[ row+row_offset_1 ][ col+col_offset_1 ] 02331 = m_2->elts[ row+row_offset_2 ][ col+col_offset_2 ]; 02332 } 02333 } 02334 02335 return NULL; 02336 } 02337 02357 Error* copy_matrix_block_into_matrix_d 02358 ( 02359 Matrix_d* m_1, 02360 uint32_t row_offset_1, 02361 uint32_t col_offset_1, 02362 const Matrix_d* m_2, 02363 uint32_t row_offset_2, 02364 uint32_t col_offset_2, 02365 uint32_t num_rows, 02366 uint32_t num_cols 02367 ) 02368 { 02369 uint32_t row, col; 02370 02371 if (m_1 == m_2) 02372 { 02373 return JWSC_EARG("Copying a matrix into itself is not supported"); 02374 } 02375 02376 /* Test if the size of the block to copy is in the bounds of the input 02377 * matrix. */ 02378 if (m_2->num_rows < (row_offset_2+num_rows) || 02379 m_2->num_cols < (col_offset_2+num_cols) || 02380 m_1->num_rows < (row_offset_1+num_rows) || 02381 m_1->num_cols < (col_offset_1+num_cols)) 02382 { 02383 return JWSC_EARG("Block to copy outside bounds of input matrix"); 02384 } 02385 02386 for (row = 0; row < num_rows; row++) 02387 { 02388 for (col = 0; col < num_cols; col++) 02389 { 02390 m_1->elts[ row+row_offset_1 ][ col+col_offset_1 ] 02391 = m_2->elts[ row+row_offset_2 ][ col+col_offset_2 ]; 02392 } 02393 } 02394 02395 return NULL; 02396 } 02397 02417 Error* copy_matrix_block_into_matrix_cf 02418 ( 02419 Matrix_cf* m_1, 02420 uint32_t row_offset_1, 02421 uint32_t col_offset_1, 02422 const Matrix_cf* m_2, 02423 uint32_t row_offset_2, 02424 uint32_t col_offset_2, 02425 uint32_t num_rows, 02426 uint32_t num_cols 02427 ) 02428 { 02429 uint32_t row, col; 02430 02431 if (m_1 == m_2) 02432 { 02433 return JWSC_EARG("Copying a matrix into itself is not supported"); 02434 } 02435 02436 /* Test if the size of the block to copy is in the bounds of the input 02437 * matrix. */ 02438 if (m_2->num_rows < (row_offset_2+num_rows) || 02439 m_2->num_cols < (col_offset_2+num_cols) || 02440 m_1->num_rows < (row_offset_1+num_rows) || 02441 m_1->num_cols < (col_offset_1+num_cols)) 02442 { 02443 return JWSC_EARG("Block to copy outside bounds of input matrix"); 02444 } 02445 02446 for (row = 0; row < num_rows; row++) 02447 { 02448 for (col = 0; col < num_cols; col++) 02449 { 02450 m_1->elts[ row+row_offset_1 ][ col+col_offset_1 ] 02451 = m_2->elts[ row+row_offset_2 ][ col+col_offset_2 ]; 02452 } 02453 } 02454 02455 return NULL; 02456 } 02457 02479 Error* copy_matrix_block_into_matrix_cd 02480 ( 02481 Matrix_cd* m_1, 02482 uint32_t row_offset_1, 02483 uint32_t col_offset_1, 02484 const Matrix_cd* m_2, 02485 uint32_t row_offset_2, 02486 uint32_t col_offset_2, 02487 uint32_t num_rows, 02488 uint32_t num_cols 02489 ) 02490 { 02491 uint32_t row, col; 02492 02493 if (m_1 == m_2) 02494 { 02495 return JWSC_EARG("Copying a matrix into itself is not supported"); 02496 } 02497 02498 /* Test if the size of the block to copy is in the bounds of the input 02499 * matrix. */ 02500 if (m_2->num_rows < (row_offset_2+num_rows) || 02501 m_2->num_cols < (col_offset_2+num_cols) || 02502 m_1->num_rows < (row_offset_1+num_rows) || 02503 m_1->num_cols < (col_offset_1+num_cols)) 02504 { 02505 return JWSC_EARG("Block to copy outside bounds of input matrix"); 02506 } 02507 02508 for (row = 0; row < num_rows; row++) 02509 { 02510 for (col = 0; col < num_cols; col++) 02511 { 02512 m_1->elts[ row+row_offset_1 ][ col+col_offset_1 ] 02513 = m_2->elts[ row+row_offset_2 ][ col+col_offset_2 ]; 02514 } 02515 } 02516 02517 return NULL; 02518 } 02519 02548 Error* copy_vector_into_matrix_u32 02549 ( 02550 Matrix_u32** m_out, 02551 const Matrix_u32* m_in, 02552 const Vector_u32* v, 02553 Matrix_vector orient, 02554 uint32_t index 02555 ) 02556 { 02557 uint32_t num_rows, num_cols; 02558 uint32_t row, col; 02559 uint32_t num_indices; 02560 02561 Matrix_u32* m; 02562 02563 num_rows = m_in->num_rows; 02564 num_cols = m_in->num_cols; 02565 02566 switch (orient) 02567 { 02568 case MATRIX_ROW_VECTOR: 02569 num_indices = m_in->num_rows; 02570 if (num_cols != v->num_elts) 02571 { 02572 return JWSC_EARG("Incompatible vector and matrix size"); 02573 } 02574 break; 02575 default: 02576 num_indices = m_in->num_cols; 02577 if (num_rows != v->num_elts) 02578 { 02579 return JWSC_EARG("Incompatible vector and matrix size"); 02580 } 02581 } 02582 if (index >= num_indices) 02583 { 02584 return JWSC_EARG("Invalid index to copy into matrix"); 02585 } 02586 02587 if (*m_out != m_in) 02588 { 02589 copy_matrix_u32(m_out, m_in); 02590 } 02591 m = *m_out; 02592 02593 switch (orient) 02594 { 02595 case MATRIX_ROW_VECTOR: 02596 for (col = 0; col < num_cols; col++) 02597 { 02598 m->elts[ index ][ col ] = v->elts[ col ]; 02599 } 02600 break; 02601 default: 02602 for (row = 0; row < num_rows; row++) 02603 { 02604 m->elts[ row ][ index ] = v->elts[ row ]; 02605 } 02606 } 02607 02608 return NULL; 02609 } 02610 02627 Error* copy_vector_into_matrix_i32 02628 ( 02629 Matrix_i32** m_out, 02630 const Matrix_i32* m_in, 02631 const Vector_i32* v, 02632 Matrix_vector orient, 02633 uint32_t index 02634 ) 02635 { 02636 uint32_t num_rows, num_cols; 02637 uint32_t row, col; 02638 uint32_t num_indices; 02639 02640 Matrix_i32* m; 02641 02642 num_rows = m_in->num_rows; 02643 num_cols = m_in->num_cols; 02644 02645 switch (orient) 02646 { 02647 case MATRIX_ROW_VECTOR: 02648 num_indices = m_in->num_rows; 02649 if (num_cols != v->num_elts) 02650 { 02651 return JWSC_EARG("Incompatible vector and matrix size"); 02652 } 02653 break; 02654 default: 02655 num_indices = m_in->num_cols; 02656 if (num_rows != v->num_elts) 02657 { 02658 return JWSC_EARG("Incompatible vector and matrix size"); 02659 } 02660 } 02661 if (index >= num_indices) 02662 { 02663 return JWSC_EARG("Invalid index to copy into matrix"); 02664 } 02665 02666 if (*m_out != m_in) 02667 { 02668 copy_matrix_i32(m_out, m_in); 02669 } 02670 m = *m_out; 02671 02672 switch (orient) 02673 { 02674 case MATRIX_ROW_VECTOR: 02675 for (col = 0; col < num_cols; col++) 02676 { 02677 m->elts[ index ][ col ] = v->elts[ col ]; 02678 } 02679 break; 02680 default: 02681 for (row = 0; row < num_rows; row++) 02682 { 02683 m->elts[ row ][ index ] = v->elts[ row ]; 02684 } 02685 } 02686 02687 return NULL; 02688 } 02689 02706 Error* copy_vector_into_matrix_i64 02707 ( 02708 Matrix_i64** m_out, 02709 const Matrix_i64* m_in, 02710 const Vector_i64* v, 02711 Matrix_vector orient, 02712 uint32_t index 02713 ) 02714 { 02715 uint32_t num_rows, num_cols; 02716 uint32_t row, col; 02717 uint32_t num_indices; 02718 02719 Matrix_i64* m; 02720 02721 num_rows = m_in->num_rows; 02722 num_cols = m_in->num_cols; 02723 02724 switch (orient) 02725 { 02726 case MATRIX_ROW_VECTOR: 02727 num_indices = m_in->num_rows; 02728 if (num_cols != v->num_elts) 02729 { 02730 return JWSC_EARG("Incompatible vector and matrix size"); 02731 } 02732 break; 02733 default: 02734 num_indices = m_in->num_cols; 02735 if (num_rows != v->num_elts) 02736 { 02737 return JWSC_EARG("Incompatible vector and matrix size"); 02738 } 02739 } 02740 if (index >= num_indices) 02741 { 02742 return JWSC_EARG("Invalid index to copy into matrix"); 02743 } 02744 02745 if (*m_out != m_in) 02746 { 02747 copy_matrix_i64(m_out, m_in); 02748 } 02749 m = *m_out; 02750 02751 switch (orient) 02752 { 02753 case MATRIX_ROW_VECTOR: 02754 for (col = 0; col < num_cols; col++) 02755 { 02756 m->elts[ index ][ col ] = v->elts[ col ]; 02757 } 02758 break; 02759 default: 02760 for (row = 0; row < num_rows; row++) 02761 { 02762 m->elts[ row ][ index ] = v->elts[ row ]; 02763 } 02764 } 02765 02766 return NULL; 02767 } 02768 02785 Error* copy_vector_into_matrix_f 02786 ( 02787 Matrix_f** m_out, 02788 const Matrix_f* m_in, 02789 const Vector_f* v, 02790 Matrix_vector orient, 02791 uint32_t index 02792 ) 02793 { 02794 uint32_t num_rows, num_cols; 02795 uint32_t row, col; 02796 uint32_t num_indices; 02797 02798 Matrix_f* m; 02799 02800 num_rows = m_in->num_rows; 02801 num_cols = m_in->num_cols; 02802 02803 switch (orient) 02804 { 02805 case MATRIX_ROW_VECTOR: 02806 num_indices = m_in->num_rows; 02807 if (num_cols != v->num_elts) 02808 { 02809 return JWSC_EARG("Incompatible vector and matrix size"); 02810 } 02811 break; 02812 default: 02813 num_indices = m_in->num_cols; 02814 if (num_rows != v->num_elts) 02815 { 02816 return JWSC_EARG("Incompatible vector and matrix size"); 02817 } 02818 } 02819 if (index >= num_indices) 02820 { 02821 return JWSC_EARG("Invalid index to copy into matrix"); 02822 } 02823 02824 if (*m_out != m_in) 02825 { 02826 copy_matrix_f(m_out, m_in); 02827 } 02828 m = *m_out; 02829 02830 switch (orient) 02831 { 02832 case MATRIX_ROW_VECTOR: 02833 for (col = 0; col < num_cols; col++) 02834 { 02835 m->elts[ index ][ col ] = v->elts[ col ]; 02836 } 02837 break; 02838 default: 02839 for (row = 0; row < num_rows; row++) 02840 { 02841 m->elts[ row ][ index ] = v->elts[ row ]; 02842 } 02843 } 02844 02845 return NULL; 02846 } 02847 02864 Error* copy_vector_into_matrix_d 02865 ( 02866 Matrix_d** m_out, 02867 const Matrix_d* m_in, 02868 const Vector_d* v, 02869 Matrix_vector orient, 02870 uint32_t index 02871 ) 02872 { 02873 uint32_t num_rows, num_cols; 02874 uint32_t row, col; 02875 uint32_t num_indices; 02876 02877 Matrix_d* m; 02878 02879 num_rows = m_in->num_rows; 02880 num_cols = m_in->num_cols; 02881 02882 switch (orient) 02883 { 02884 case MATRIX_ROW_VECTOR: 02885 num_indices = m_in->num_rows; 02886 if (num_cols != v->num_elts) 02887 { 02888 return JWSC_EARG("Incompatible vector and matrix size"); 02889 } 02890 break; 02891 default: 02892 num_indices = m_in->num_cols; 02893 if (num_rows != v->num_elts) 02894 { 02895 return JWSC_EARG("Incompatible vector and matrix size"); 02896 } 02897 } 02898 if (index >= num_indices) 02899 { 02900 return JWSC_EARG("Invalid index to copy into matrix"); 02901 } 02902 02903 if (*m_out != m_in) 02904 { 02905 copy_matrix_d(m_out, m_in); 02906 } 02907 m = *m_out; 02908 02909 switch (orient) 02910 { 02911 case MATRIX_ROW_VECTOR: 02912 for (col = 0; col < num_cols; col++) 02913 { 02914 m->elts[ index ][ col ] = v->elts[ col ]; 02915 } 02916 break; 02917 default: 02918 for (row = 0; row < num_rows; row++) 02919 { 02920 m->elts[ row ][ index ] = v->elts[ row ]; 02921 } 02922 } 02923 02924 return NULL; 02925 } 02926 02943 Error* copy_vector_into_matrix_cf 02944 ( 02945 Matrix_cf** m_out, 02946 const Matrix_cf* m_in, 02947 const Vector_cf* v, 02948 Matrix_vector orient, 02949 uint32_t index 02950 ) 02951 { 02952 uint32_t num_rows, num_cols; 02953 uint32_t row, col; 02954 uint32_t num_indices; 02955 02956 Matrix_cf* m; 02957 02958 num_rows = m_in->num_rows; 02959 num_cols = m_in->num_cols; 02960 02961 switch (orient) 02962 { 02963 case MATRIX_ROW_VECTOR: 02964 num_indices = m_in->num_rows; 02965 if (num_cols != v->num_elts) 02966 { 02967 return JWSC_EARG("Incompatible vector and matrix size"); 02968 } 02969 break; 02970 default: 02971 num_indices = m_in->num_cols; 02972 if (num_rows != v->num_elts) 02973 { 02974 return JWSC_EARG("Incompatible vector and matrix size"); 02975 } 02976 } 02977 if (index >= num_indices) 02978 { 02979 return JWSC_EARG("Invalid index to copy into matrix"); 02980 } 02981 02982 if (*m_out != m_in) 02983 { 02984 copy_matrix_cf(m_out, m_in); 02985 } 02986 m = *m_out; 02987 02988 switch (orient) 02989 { 02990 case MATRIX_ROW_VECTOR: 02991 for (col = 0; col < num_cols; col++) 02992 { 02993 m->elts[ index ][ col ] = v->elts[ col ]; 02994 } 02995 break; 02996 default: 02997 for (row = 0; row < num_rows; row++) 02998 { 02999 m->elts[ row ][ index ] = v->elts[ row ]; 03000 } 03001 } 03002 03003 return NULL; 03004 } 03005 03022 Error* copy_vector_into_matrix_cd 03023 ( 03024 Matrix_cd** m_out, 03025 const Matrix_cd* m_in, 03026 const Vector_cd* v, 03027 Matrix_vector orient, 03028 uint32_t index 03029 ) 03030 { 03031 uint32_t num_rows, num_cols; 03032 uint32_t row, col; 03033 uint32_t num_indices; 03034 03035 Matrix_cd* m; 03036 03037 num_rows = m_in->num_rows; 03038 num_cols = m_in->num_cols; 03039 03040 switch (orient) 03041 { 03042 case MATRIX_ROW_VECTOR: 03043 num_indices = m_in->num_rows; 03044 if (num_cols != v->num_elts) 03045 { 03046 return JWSC_EARG("Incompatible vector and matrix size"); 03047 } 03048 break; 03049 default: 03050 num_indices = m_in->num_cols; 03051 if (num_rows != v->num_elts) 03052 { 03053 return JWSC_EARG("Incompatible vector and matrix size"); 03054 } 03055 } 03056 if (index >= num_indices) 03057 { 03058 return JWSC_EARG("Invalid index to copy into matrix"); 03059 } 03060 03061 if (*m_out != m_in) 03062 { 03063 copy_matrix_cd(m_out, m_in); 03064 } 03065 m = *m_out; 03066 03067 switch (orient) 03068 { 03069 case MATRIX_ROW_VECTOR: 03070 for (col = 0; col < num_cols; col++) 03071 { 03072 m->elts[ index ][ col ] = v->elts[ col ]; 03073 } 03074 break; 03075 default: 03076 for (row = 0; row < num_rows; row++) 03077 { 03078 m->elts[ row ][ index ] = v->elts[ row ]; 03079 } 03080 } 03081 03082 return NULL; 03083 } 03084 03110 Error* copy_vector_from_matrix_u32 03111 ( 03112 Vector_u32** v_out, 03113 const Matrix_u32* m, 03114 Matrix_vector orient, 03115 uint32_t index 03116 ) 03117 { 03118 uint32_t num_rows, num_cols; 03119 uint32_t row, col; 03120 uint32_t num_indices; 03121 03122 Vector_u32* v; 03123 03124 num_rows = m->num_rows; 03125 num_cols = m->num_cols; 03126 03127 switch (orient) 03128 { 03129 case MATRIX_ROW_VECTOR: 03130 num_indices = m->num_rows; 03131 break; 03132 default: 03133 num_indices = m->num_cols; 03134 } 03135 if (index >= num_indices) 03136 { 03137 return JWSC_EARG("Invalid index to copy from matrix"); 03138 } 03139 03140 switch (orient) 03141 { 03142 case MATRIX_ROW_VECTOR: 03143 create_vector_u32(v_out, num_cols); 03144 v = *v_out; 03145 for (col = 0; col < num_cols; col++) 03146 { 03147 v->elts[ col ] = m->elts[ index ][ col ]; 03148 } 03149 break; 03150 default: 03151 create_vector_u32(v_out, num_rows); 03152 v = *v_out; 03153 for (row = 0; row < num_rows; row++) 03154 { 03155 v->elts[ row ] = m->elts[ row ][ index ]; 03156 } 03157 } 03158 03159 return NULL; 03160 } 03161 03175 Error* copy_vector_from_matrix_i32 03176 ( 03177 Vector_i32** v_out, 03178 const Matrix_i32* m, 03179 Matrix_vector orient, 03180 uint32_t index 03181 ) 03182 { 03183 uint32_t num_rows, num_cols; 03184 uint32_t row, col; 03185 uint32_t num_indices; 03186 03187 Vector_i32* v; 03188 03189 num_rows = m->num_rows; 03190 num_cols = m->num_cols; 03191 03192 switch (orient) 03193 { 03194 case MATRIX_ROW_VECTOR: 03195 num_indices = m->num_rows; 03196 break; 03197 default: 03198 num_indices = m->num_cols; 03199 } 03200 if (index >= num_indices) 03201 { 03202 return JWSC_EARG("Invalid index to copy from matrix"); 03203 } 03204 03205 switch (orient) 03206 { 03207 case MATRIX_ROW_VECTOR: 03208 create_vector_i32(v_out, num_cols); 03209 v = *v_out; 03210 for (col = 0; col < num_cols; col++) 03211 { 03212 v->elts[ col ] = m->elts[ index ][ col ]; 03213 } 03214 break; 03215 default: 03216 create_vector_i32(v_out, num_rows); 03217 v = *v_out; 03218 for (row = 0; row < num_rows; row++) 03219 { 03220 v->elts[ row ] = m->elts[ row ][ index ]; 03221 } 03222 } 03223 03224 return NULL; 03225 } 03226 03240 Error* copy_vector_from_matrix_i64 03241 ( 03242 Vector_i64** v_out, 03243 const Matrix_i64* m, 03244 Matrix_vector orient, 03245 uint32_t index 03246 ) 03247 { 03248 uint32_t num_rows, num_cols; 03249 uint32_t row, col; 03250 uint32_t num_indices; 03251 03252 Vector_i64* v; 03253 03254 num_rows = m->num_rows; 03255 num_cols = m->num_cols; 03256 03257 switch (orient) 03258 { 03259 case MATRIX_ROW_VECTOR: 03260 num_indices = m->num_rows; 03261 break; 03262 default: 03263 num_indices = m->num_cols; 03264 } 03265 if (index >= num_indices) 03266 { 03267 return JWSC_EARG("Invalid index to copy from matrix"); 03268 } 03269 03270 switch (orient) 03271 { 03272 case MATRIX_ROW_VECTOR: 03273 create_vector_i64(v_out, num_cols); 03274 v = *v_out; 03275 for (col = 0; col < num_cols; col++) 03276 { 03277 v->elts[ col ] = m->elts[ index ][ col ]; 03278 } 03279 break; 03280 default: 03281 create_vector_i64(v_out, num_rows); 03282 v = *v_out; 03283 for (row = 0; row < num_rows; row++) 03284 { 03285 v->elts[ row ] = m->elts[ row ][ index ]; 03286 } 03287 } 03288 03289 return NULL; 03290 } 03291 03305 Error* copy_vector_from_matrix_f 03306 ( 03307 Vector_f** v_out, 03308 const Matrix_f* m, 03309 Matrix_vector orient, 03310 uint32_t index 03311 ) 03312 { 03313 uint32_t num_rows, num_cols; 03314 uint32_t row, col; 03315 uint32_t num_indices; 03316 03317 Vector_f* v; 03318 03319 num_rows = m->num_rows; 03320 num_cols = m->num_cols; 03321 03322 switch (orient) 03323 { 03324 case MATRIX_ROW_VECTOR: 03325 num_indices = m->num_rows; 03326 break; 03327 default: 03328 num_indices = m->num_cols; 03329 } 03330 if (index >= num_indices) 03331 { 03332 return JWSC_EARG("Invalid index to copy from matrix"); 03333 } 03334 03335 switch (orient) 03336 { 03337 case MATRIX_ROW_VECTOR: 03338 create_vector_f(v_out, num_cols); 03339 v = *v_out; 03340 for (col = 0; col < num_cols; col++) 03341 { 03342 v->elts[ col ] = m->elts[ index ][ col ]; 03343 } 03344 break; 03345 default: 03346 create_vector_f(v_out, num_rows); 03347 v = *v_out; 03348 for (row = 0; row < num_rows; row++) 03349 { 03350 v->elts[ row ] = m->elts[ row ][ index ]; 03351 } 03352 } 03353 03354 return NULL; 03355 } 03356 03370 Error* copy_vector_from_matrix_d 03371 ( 03372 Vector_d** v_out, 03373 const Matrix_d* m, 03374 Matrix_vector orient, 03375 uint32_t index 03376 ) 03377 { 03378 uint32_t num_rows, num_cols; 03379 uint32_t row, col; 03380 uint32_t num_indices; 03381 03382 Vector_d* v; 03383 03384 num_rows = m->num_rows; 03385 num_cols = m->num_cols; 03386 03387 switch (orient) 03388 { 03389 case MATRIX_ROW_VECTOR: 03390 num_indices = m->num_rows; 03391 break; 03392 default: 03393 num_indices = m->num_cols; 03394 } 03395 if (index >= num_indices) 03396 { 03397 return JWSC_EARG("Invalid index to copy from matrix"); 03398 } 03399 03400 switch (orient) 03401 { 03402 case MATRIX_ROW_VECTOR: 03403 create_vector_d(v_out, num_cols); 03404 v = *v_out; 03405 for (col = 0; col < num_cols; col++) 03406 { 03407 v->elts[ col ] = m->elts[ index ][ col ]; 03408 } 03409 break; 03410 default: 03411 create_vector_d(v_out, num_rows); 03412 v = *v_out; 03413 for (row = 0; row < num_rows; row++) 03414 { 03415 v->elts[ row ] = m->elts[ row ][ index ]; 03416 } 03417 } 03418 03419 return NULL; 03420 } 03421 03435 Error* copy_vector_from_matrix_cf 03436 ( 03437 Vector_cf** v_out, 03438 const Matrix_cf* m, 03439 Matrix_vector orient, 03440 uint32_t index 03441 ) 03442 { 03443 uint32_t num_rows, num_cols; 03444 uint32_t row, col; 03445 uint32_t num_indices; 03446 03447 Vector_cf* v; 03448 03449 num_rows = m->num_rows; 03450 num_cols = m->num_cols; 03451 03452 switch (orient) 03453 { 03454 case MATRIX_ROW_VECTOR: 03455 num_indices = m->num_rows; 03456 break; 03457 default: 03458 num_indices = m->num_cols; 03459 } 03460 if (index >= num_indices) 03461 { 03462 return JWSC_EARG("Invalid index to copy from matrix"); 03463 } 03464 03465 switch (orient) 03466 { 03467 case MATRIX_ROW_VECTOR: 03468 create_vector_cf(v_out, num_cols); 03469 v = *v_out; 03470 for (col = 0; col < num_cols; col++) 03471 { 03472 v->elts[ col ] = m->elts[ index ][ col ]; 03473 } 03474 break; 03475 default: 03476 create_vector_cf(v_out, num_rows); 03477 v = *v_out; 03478 for (row = 0; row < num_rows; row++) 03479 { 03480 v->elts[ row ] = m->elts[ row ][ index ]; 03481 } 03482 } 03483 03484 return NULL; 03485 } 03486 03500 Error* copy_vector_from_matrix_cd 03501 ( 03502 Vector_cd** v_out, 03503 const Matrix_cd* m, 03504 Matrix_vector orient, 03505 uint32_t index 03506 ) 03507 { 03508 uint32_t num_rows, num_cols; 03509 uint32_t row, col; 03510 uint32_t num_indices; 03511 03512 Vector_cd* v; 03513 03514 num_rows = m->num_rows; 03515 num_cols = m->num_cols; 03516 03517 switch (orient) 03518 { 03519 case MATRIX_ROW_VECTOR: 03520 num_indices = m->num_rows; 03521 break; 03522 default: 03523 num_indices = m->num_cols; 03524 } 03525 if (index >= num_indices) 03526 { 03527 return JWSC_EARG("Invalid index to copy from matrix"); 03528 } 03529 03530 switch (orient) 03531 { 03532 case MATRIX_ROW_VECTOR: 03533 create_vector_cd(v_out, num_cols); 03534 v = *v_out; 03535 for (col = 0; col < num_cols; col++) 03536 { 03537 v->elts[ col ] = m->elts[ index ][ col ]; 03538 } 03539 break; 03540 default: 03541 create_vector_cd(v_out, num_rows); 03542 v = *v_out; 03543 for (row = 0; row < num_rows; row++) 03544 { 03545 v->elts[ row ] = m->elts[ row ][ index ]; 03546 } 03547 } 03548 03549 return NULL; 03550 } 03551 03565 void free_matrix_u32(Matrix_u32* m) 03566 { 03567 if (m == NULL) return; 03568 03569 free(*(m->elts)); 03570 free(m->elts); 03571 free(m); 03572 } 03573 03575 void free_matrix_i32(Matrix_i32* m) 03576 { 03577 if (m == NULL) return; 03578 03579 free(*(m->elts)); 03580 free(m->elts); 03581 free(m); 03582 } 03583 03585 void free_matrix_i64(Matrix_i64* m) 03586 { 03587 if (m == NULL) return; 03588 03589 free(*(m->elts)); 03590 free(m->elts); 03591 free(m); 03592 } 03593 03595 void free_matrix_f(Matrix_f* m) 03596 { 03597 if (m == NULL) return; 03598 03599 free(*(m->elts)); 03600 free(m->elts); 03601 free(m); 03602 } 03603 03605 void free_matrix_d(Matrix_d* m) 03606 { 03607 if (m == NULL) return; 03608 03609 free(*(m->elts)); 03610 free(m->elts); 03611 free(m); 03612 } 03613 03615 void free_matrix_cf(Matrix_cf* m) 03616 { 03617 if (m == NULL) return; 03618 03619 free(*(m->elts)); 03620 free(m->elts); 03621 free(m); 03622 } 03623 03625 void free_matrix_cd(Matrix_cd* m) 03626 { 03627 if (m == NULL) return; 03628 03629 free(*(m->elts)); 03630 free(m->elts); 03631 free(m); 03632 } 03633