00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <math.h> 00004 #include <string.h> 00005 #include "vector.h" 00006 #include "iotools.h" 00007 00008 00009 00010 /** 00011 The constructor allocates memory form the heap to the member array a. 00012 00013 @param n is the number of %vector elements 00014 00015 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00016 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00017 */ 00018 vector :: vector (long n) 00019 { 00020 vector::n = n; 00021 a = new double[n]; 00022 if (a == NULL) 00023 print_err("cannot allocate memory for vector v(%ld)", __FILE__, __LINE__, __func__, n); 00024 memset (a,0,n*sizeof(double)); 00025 00026 #ifdef DEBUG_VECTOR 00027 Acv++; 00028 #endif 00029 } 00030 00031 00032 00033 /** 00034 The copy constructor creates copy of the object given by the constant reference parameter. 00035 00036 @param v is reference to the object of the vector which should be copied 00037 00038 created 16.10.2003 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00039 */ 00040 vector :: vector (const vector &v) 00041 { 00042 long i; 00043 if ((v.n != n) || (a == NULL)) 00044 { 00045 n = v.n; 00046 a = new double[v.n]; 00047 if (a == NULL) 00048 print_err("cannot allocate memory for vector v(%ld)", __FILE__, __LINE__, __func__, n); 00049 } 00050 for (i = 0; i < n; i++) 00051 a[i] = v.a[i]; 00052 #ifdef DEBUG_VECTOR 00053 print_warning("copy constructor of vector is called.\n" 00054 "Please check your code and make sure you want use copy constructor.", __FILE__, __LINE__, __func__); 00055 Acv++; 00056 #endif 00057 } 00058 00059 00060 00061 /** 00062 The destructor deallocates the memory occupied by the member array a 00063 00064 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00065 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00066 */ 00067 vector :: ~vector() 00068 { 00069 #ifdef DEBUG_VECTOR 00070 if (a != NULL) 00071 Acv--; 00072 #endif 00073 n = 0; 00074 delete [] a; 00075 a = NULL; 00076 } 00077 00078 00079 00080 /** 00081 The constructor allocates memory form the heap to the member array a. 00082 00083 @param n is the number of ivector elements 00084 00085 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00086 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00087 */ 00088 ivector :: ivector (long n) 00089 { 00090 ivector::n = n; 00091 a = new long[n]; 00092 if (a == NULL) 00093 print_err("cannot allocate memory for ivector v(%ld)", __FILE__, __LINE__, __func__, n); 00094 memset (a,0,n*sizeof(long)); 00095 00096 #ifdef DEBUG_VECTOR 00097 Aciv++; 00098 #endif 00099 } 00100 00101 00102 00103 /** 00104 The copy constructor creates copy of the object given by the constant reference parameter. 00105 00106 @param v is reference to the object of the ivector which should be copied 00107 00108 created 16.10.2003 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00109 */ 00110 ivector :: ivector (const ivector &v) 00111 { 00112 long i; 00113 if ((v.n != n) || (a == NULL)) 00114 { 00115 n = v.n; 00116 a = new long[v.n]; 00117 if (a == NULL) 00118 print_err("cannot allocate memory for ivector v(%ld)", __FILE__, __LINE__, __func__, n); 00119 } 00120 for (i = 0; i < n; i++) 00121 a[i] = v.a[i]; 00122 #ifdef DEBUG_VECTOR 00123 print_warning("copy constructor of ivector is called.\n" 00124 "Please check your code and make sure you want use copy constructor.", __FILE__, __LINE__, __func__); 00125 Aciv++; 00126 #endif 00127 } 00128 00129 /** 00130 The destructor deallocates the memory occupied by the member array a 00131 00132 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00133 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00134 */ 00135 ivector :: ~ivector() 00136 { 00137 #ifdef DEBUG_VECTOR 00138 if (a != NULL) 00139 Aciv--; 00140 #endif 00141 n = 0; 00142 delete [] a; 00143 a = NULL; 00144 } 00145 00146 00147 #ifdef DEBUG_VECTOR 00148 long give_acv() 00149 { 00150 return Acv; 00151 } 00152 #endif 00153 00154 00155 00156 /** 00157 The function allocates memory from heap to the %vector's member array a. 00158 00159 @param n is the number of the vector elements 00160 @param vec is the structure for allocated vector 00161 00162 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00163 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00164 */ 00165 long allocv(long n, vector &vec) 00166 { 00167 vec.n = n; 00168 vec.a = new double[n]; 00169 if (vec.a == NULL) 00170 { 00171 print_err("cannot allocate memory for vector v(%ld)", __FILE__, __LINE__, __func__, n); 00172 return (1); 00173 } 00174 memset (vec.a,0,n*sizeof(double)); 00175 #ifdef DEBUG_VECTOR 00176 Acv++; 00177 #endif 00178 return (0); 00179 } 00180 00181 00182 00183 /** 00184 The function reallocates memory for the vector's member array a. 00185 If the new vector size is greater than previously allocated one, 00186 array a is deleted and new memory is allocted from the heap. 00187 If the new vector size is less or equal, then the memory 00188 allocated for the array a is left and the number of vector components 00189 is changed. Components of the array a are set to zero. 00190 00191 00192 @param n is the new number of the ivector components 00193 @param vec is the structure for reallocated vector 00194 00195 @retval 0 - on success 00196 @retval 1 - if fails allocating memory 00197 00198 created 12.2012 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00199 */ 00200 long reallocv(long n, vector &vec) 00201 { 00202 if (n > vec.n) 00203 { 00204 delete [] vec.a; 00205 #ifdef DEBUG_VECTOR 00206 Acv--; 00207 #endif 00208 vec.a = new double[n]; 00209 if (vec.a == NULL) 00210 { 00211 print_err("cannot reallocate memory for vector v(%ld)", __FILE__, __LINE__, __func__, n); 00212 return (1); 00213 } 00214 #ifdef DEBUG_VECTOR 00215 Acv++; 00216 #endif 00217 } 00218 vec.n = n; 00219 memset (vec.a,0,n*sizeof(*vec.a)); 00220 return (0); 00221 } 00222 00223 00224 00225 /** 00226 The function allocates memory from heap to the ivector's member array a. 00227 00228 @param n is the number of the ivector elements 00229 @param vec is the structure for allocated ivector 00230 00231 @retval 0 - on success 00232 @retval 1 - if fails allocating memory 00233 00234 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00235 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00236 */ 00237 long allocv(long n, ivector &vec) 00238 { 00239 vec.n = n; 00240 vec.a = new long[n]; 00241 if (vec.a == NULL) 00242 { 00243 print_err("cannot allocate memory for ivector v(%ld)", __FILE__, __LINE__, __func__, n); 00244 return (1); 00245 } 00246 memset (vec.a,0,n*sizeof(*vec.a)); 00247 #ifdef DEBUG_VECTOR 00248 Aciv++; 00249 #endif 00250 return (0); 00251 } 00252 00253 00254 00255 /** 00256 The function reallocates memory for the ivector's member array a. 00257 If the new ivector size is greater than previously allocated one, 00258 array a is deleted and new memory is allocted from the heap. 00259 If the new ivector size is less or equal, then the memory 00260 allocated for the array a is left and the number of %ivector components 00261 is changed. Components of the array a are set to zero. 00262 00263 00264 @param n is the new number of the ivector components 00265 @param vec is the structure for reallocated ivector 00266 00267 @retval 0 - on success 00268 @retval 1 - if fails allocating memory 00269 00270 created 12.2012 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00271 */ 00272 long reallocv(long n, ivector &vec) 00273 { 00274 if (n > vec.n) 00275 { 00276 delete [] vec.a; 00277 #ifdef DEBUG_VECTOR 00278 Aciv--; 00279 #endif 00280 vec.a = new long[n]; 00281 if (vec.a == NULL) 00282 { 00283 print_err("cannot reallocate memory for ivector v(%ld)", __FILE__, __LINE__, __func__, n); 00284 return (1); 00285 } 00286 #ifdef DEBUG_VECTOR 00287 Aciv++; 00288 #endif 00289 } 00290 vec.n = n; 00291 memset (vec.a,0,n*sizeof(*vec.a)); 00292 return (0); 00293 } 00294 00295 00296 00297 /** 00298 The function copies vector given by src to dest. 00299 00300 @param src is the structure of source vector to copy 00301 @param dest is the structure of destination vector to which will be copied contents of src 00302 00303 @b Requests : 00304 dest has to be setuped dimensions and allocated memory array for elements 00305 which is enough large to hold all contents of the vector src. 00306 00307 @retval 0 : on succes 00308 @retval 1 : in case incompatibility sizes of the src and dest vectors 00309 00310 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00311 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00312 */ 00313 long copyv(const vector &src, vector &dest) 00314 { 00315 if (src.n != dest.n) 00316 { 00317 print_err("cannot copy vectors - incompatible dimensions of vectors\n" 00318 "src(%ld) X dest(%ld)", __FILE__, __LINE__, __func__, src.n, dest.n); 00319 return (1); 00320 } 00321 memcpy(dest.a, src.a, sizeof(double)*src.n); 00322 return (0); 00323 } 00324 00325 00326 00327 /** 00328 The function copies %vector given by src to dest. 00329 00330 @param src is the structure of source %ivector to copy 00331 @param dest is the structure of destination %ivector to which will be copied contents of src 00332 00333 @b Requests : 00334 dest has to be setuped dimensions and allocated memory array for elements 00335 which is enough large to hold all contents of the %ivector src. 00336 00337 @retval 0 on succes 00338 @retval 1 in case incompatibility sizes of the src and dest vectors 00339 00340 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00341 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00342 */ 00343 long copyv(const ivector &src, ivector &dest) 00344 { 00345 if (src.n != dest.n) 00346 { 00347 print_err("cannot copy ivector - incompatible dimensions of ivectors\n" 00348 "src(%ld) X dest(%ld)", __FILE__, __LINE__, __func__, src.n, dest.n); 00349 return (1); 00350 } 00351 memcpy(dest.a, src.a, sizeof(long)*src.n); 00352 return (0); 00353 } 00354 00355 00356 00357 /** 00358 The function copies long array given by src to dest. 00359 00360 @param src is the double array to copy 00361 @param dest is the double array to which will be copied contents of src 00362 00363 @retval always zero 00364 00365 created 3.11.2001 by JK 00366 */ 00367 long copyv(const long *src,long *dest,long n) 00368 { 00369 memcpy(dest, src, sizeof(long)*n); 00370 return (0); 00371 } 00372 00373 00374 00375 /** 00376 The function copies double array given by src to dest. 00377 00378 @param src is the double array to copy 00379 @param dest is the double array to which will be copied contents of src 00380 00381 @retval always zero 00382 00383 created 3.11.2001 by JK 00384 */ 00385 long copyv (const double *src, double *dest, long n) 00386 { 00387 memcpy(dest, src, sizeof(double)*n); 00388 return (0); 00389 } 00390 00391 00392 00393 /** 00394 The function copies array given by src to vector dest. 00395 00396 @param src is source array to copy 00397 @param dest is the structure of destination vector which will be copied contents of src to 00398 00399 @b Requests : 00400 dest has to be setuped dimensions and allocated memory array for required elements. 00401 Number of copied elements is given by the dest dimension 00402 00403 @retval 0 : on succes 00404 00405 created 4.10.2007 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00406 */ 00407 long copyv(const double *src, vector &dest) 00408 { 00409 memcpy(dest.a, src, sizeof(double)*dest.n); 00410 return (0); 00411 } 00412 00413 00414 00415 /** 00416 The function copies vector given by src to array dest. 00417 00418 @param src is the structure of source vector to copy 00419 @param dest is the destination array which will be copied contents of src to 00420 00421 @b Requests : 00422 dest has to have allocated memory which is enough large to hold all contents 00423 of the vector src. Number of copied elements is given by the src dimension 00424 00425 @retval 0 : on succes 00426 00427 created 4.10.2007 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00428 */ 00429 long copyv(const vector &src, double *dest) 00430 { 00431 memcpy(dest, src.a, sizeof(double)*src.n); 00432 return (0); 00433 } 00434 00435 00436 00437 /** 00438 The function copies vector given by src multiplied by c to dest. 00439 00440 @param src - structure with source %vector to copy 00441 @param dest - structure with destination %vector to which will be copied contents of the src 00442 @param c - scalar multiplier 00443 00444 @b Requests : 00445 dest has to have dimensions and allocated memory array for %vector components 00446 which is large enough to hold all contents of the %vector src. 00447 00448 @retval 0 : on succes 00449 @retval 1 : in case incompatibility sizes of the src and dest vectors 00450 00451 created 08.2011 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00452 */ 00453 long copymultv(const vector &src, vector &dest, const double c) 00454 { 00455 if (src.n != dest.n) 00456 { 00457 print_err("cannot copy vectors - incompatible dimensions of vectors\n" 00458 "src(%ld) X dest(%ld)", __FILE__, __LINE__, __func__, src.n, dest.n); 00459 return (1); 00460 } 00461 long i; 00462 for (i=0; i<src.n; i++) 00463 dest[i] = c*src[i]; 00464 return (0); 00465 } 00466 00467 00468 00469 /** 00470 The function copies ivector given by src multiplied by scalar c to dest. 00471 00472 @param src - structure with the source ivector to copy 00473 @param dest - structure with the destination %ivector to which will be copied the contents of the src 00474 @param c - scalar multiplier 00475 00476 @b Requests : 00477 dest has to have dimensions and allocated memory array for %vector components 00478 which is large enough to hold all contents of the %ivector src. 00479 00480 @retval 0 on succes 00481 @retval 1 in case incompatibility sizes of the src and dest vectors 00482 00483 created 08.2011 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00484 */ 00485 long copymultv(const ivector &src, ivector &dest, const long c) 00486 { 00487 if (src.n != dest.n) 00488 { 00489 print_err("cannot copy ivector - incompatible dimensions of ivectors\n" 00490 "src(%ld) X dest(%ld)", __FILE__, __LINE__, __func__, src.n, dest.n); 00491 return (1); 00492 } 00493 long i; 00494 for (i=0; i<src.n; i++) 00495 dest[i] = c*src[i]; 00496 return (0); 00497 } 00498 00499 00500 00501 /** 00502 The function copies double array given by the src multiplied by scalar c to the dest. 00503 00504 @param src - double array to copy 00505 @param dest - double array to which will be copied contents of src 00506 @param c - scalar multiplier 00507 @param n - length of arrays src and dest 00508 00509 @retval always zero 00510 00511 created 08.2011 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00512 */ 00513 long copymultv(const double *src, double *dest, const double c, long n) 00514 { 00515 long i; 00516 for (i=0; i<n; i++) 00517 dest[i] = c*src[i]; 00518 return (0); 00519 } 00520 00521 00522 00523 /** 00524 The function fills vect's member array a with value c 00525 00526 @param c is value, which will be used for filling memory 00527 @param vec is the structure %vector for the deallocated %vector 00528 00529 @return always zero 00530 00531 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00532 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00533 */ 00534 long fillv(double c, vector &vec) 00535 { 00536 for (long i = 0; i < vec.n; vec(i) = c, i++); 00537 return (0); 00538 } 00539 00540 00541 00542 /** 00543 The function fills vect's member array a with value c 00544 00545 @param c is value, which will be used for filling memory 00546 @param vec is the structure %ivector which should be filled 00547 00548 @retval always zero 00549 00550 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00551 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00552 */ 00553 long fillv(long c, ivector &vec) 00554 { 00555 for (long i = 0; i < vec.n; vec(i) = c, i++); 00556 return (0); 00557 } 00558 00559 00560 00561 /** 00562 The function fills vect's member array a with value c 00563 00564 @param c is value, which will be used for filling memory 00565 @param vec is the structure %ivector which should be filled 00566 00567 @retval always zero 00568 00569 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00570 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00571 */ 00572 void fillv(long c, long *vec,long n) 00573 { 00574 long i; 00575 00576 for (i=0;i<n;i++) 00577 vec[i]=c; 00578 } 00579 00580 00581 00582 /** 00583 The function fills vect's member array a with value c 00584 00585 @param c is value, which will be used for filling memory 00586 @param vec is the structure %ivector which should be filled 00587 00588 @retval always zero 00589 00590 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00591 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00592 */ 00593 void fillv(double c, double *vec,long n) 00594 { 00595 long i; 00596 00597 for (i=0;i<n;i++) 00598 vec[i]=c; 00599 } 00600 00601 00602 00603 /** 00604 The function fills double array a with zeros 00605 00606 @param a is array which should be filled 00607 00608 @retval always zero 00609 */ 00610 long nullv (double *a,long n) 00611 { 00612 memset (a,0,n*sizeof(double)); 00613 return (0); 00614 } 00615 00616 00617 00618 long nullv (ivector &a) 00619 { 00620 memset (a.a,0,a.n*sizeof(long)); 00621 return (0); 00622 } 00623 00624 00625 00626 /** 00627 The function deallocates memory occupied by %vector vec 00628 00629 @param vec is the structure for the deallocated %vector 00630 00631 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00632 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00633 */ 00634 long destrv(vector &vec) 00635 { 00636 #ifdef DEBUG_VECTOR 00637 if (vec.a != NULL) 00638 Acv--; 00639 #endif 00640 vec.n = 0; 00641 delete [] vec.a; 00642 vec.a = NULL; 00643 return (0); 00644 } 00645 00646 00647 00648 /** 00649 The function deallocates memory occupied by %ivector vec 00650 00651 @param vec is the structure for the deallocated %ivector 00652 00653 @retval 0 00654 00655 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00656 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00657 */ 00658 long destrv(ivector &vec) 00659 { 00660 #ifdef DEBUG_VECTOR 00661 if (vec.a != NULL) 00662 Aciv--; 00663 #endif 00664 vec.n = 0; 00665 delete [] vec.a; 00666 vec.a = NULL; 00667 return (0); 00668 } 00669 00670 00671 00672 /** 00673 The function adds vector given by a to %vector given by b, the result is stored in c 00674 00675 @param a is the structure of the first added vector 00676 @param b is the structure of the second added vector 00677 @param c is the structure of the result vector 00678 00679 @b Requests : 00680 a, b and c have to be same dimensions, c has to have allocated memory array for elements 00681 which is enough large to hold contents of the result. 00682 00683 @retval 0 : on succes 00684 @retval 1 : in case incompatibility sizes of a, b and c vectors 00685 00686 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00687 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00688 */ 00689 long addv(const vector &a, const vector &b, vector &c) 00690 { 00691 if ((a.n != b.n) || (b.n != c.n)) 00692 { 00693 print_err("cannot add vectors due to their incompatible dimensions\n" 00694 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 00695 return (1); 00696 } 00697 for (long i = 0; i < a.n; i++) 00698 c.a[i] = a.a[i] + b.a[i]; 00699 00700 return(0); 00701 } 00702 00703 00704 00705 /** 00706 The function adds vector given by a to %vector given by b, the result is stored in c. 00707 00708 @param a is the structure of the first added vector 00709 @param b is the structure of the second added vector 00710 @param c is the structure of the result vector 00711 00712 @b Requests : 00713 a, b and c must be the same dimensions, c must have allocated memory array for elements 00714 which is large enough to hold contents of the result. 00715 00716 @return The function returns the result in the parameter c. 00717 00718 created 08.2011 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00719 */ 00720 void addmultv (const vector &a, const vector &b, double bc, vector &c) 00721 { 00722 if ((a.n != b.n) || (b.n != c.n)) 00723 { 00724 print_err("cannot add vectors due to their incompatible dimensions\n" 00725 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 00726 return; 00727 } 00728 for (long i=0;i<a.n;i++) 00729 c[i] = a[i]+b[i]*bc; 00730 } 00731 00732 00733 00734 /** 00735 The function adds vector given by a to %vector given by b which is multiplied by bc, the result is stored in a. 00736 00737 @param a is the structure of the first added vector 00738 @param b is the structure of the second added vector 00739 00740 @b Requests : 00741 a and b must have the same dimensions, c must have allocated memory array for elements 00742 which is large enough to hold contents of the result. 00743 00744 @return The function returns the result in the parameter a. 00745 00746 created 08.2011 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00747 */ 00748 void addmultv (vector &a, const vector &b, double bc) 00749 { 00750 if (a.n != b.n) 00751 { 00752 print_err("cannot add vectors due to their incompatible dimensions\n" 00753 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 00754 return; 00755 } 00756 for (long i=0;i<a.n;i++) 00757 a[i] += b[i]*bc; 00758 } 00759 00760 00761 00762 /** 00763 The function adds ivector given by a to ivector given by b, the result is stored in c 00764 00765 @param a is the structure of the first added ivector 00766 @param b is the structure of the second added ivector 00767 @param c is the structure of the result ivector 00768 00769 @b Requests : 00770 a, b and c should have same dimensions, c has to have allocated memory array for elements 00771 which is enough large to hold contents of the result. 00772 00773 @retval 0 on succes 00774 @retval 1 in case incompatibility sizes of a, b and c vectors 00775 00776 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00777 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00778 */ 00779 long addv(const ivector &a, const ivector &b, ivector &c) 00780 { 00781 if ((a.n != b.n) || (b.n != c.n)) 00782 { 00783 print_err("cannot add ivectors due to their incompatible dimensions\n" 00784 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 00785 return (1); 00786 } 00787 for (long i = 0; i < a.n; i++) 00788 c.a[i] = a.a[i] + b.a[i]; 00789 00790 return(0); 00791 } 00792 00793 00794 00795 /** 00796 The function adds double array b to double array given by a, the result is stored in a 00797 00798 @param a is result array 00799 @param b is added array 00800 @param n is the number of vector components 00801 00802 @retval always zero 00803 00804 created by JK 00805 */ 00806 void addv (double *a,double *b,long n) 00807 { 00808 for (long i=0;i<n;i++) 00809 a[i]+=b[i]; 00810 } 00811 00812 00813 00814 /** 00815 The function adds two double arrays a and b, the result is stored in c 00816 00817 @param a is added array 00818 @param b is added array 00819 @param c is result array 00820 @param n is the number of vector components 00821 00822 created by JK 00823 */ 00824 void addv (double *a,double *b,double *c,long n) 00825 { 00826 for (long i=0;i<n;i++) 00827 c[i] = a[i]+b[i]; 00828 } 00829 00830 00831 00832 /** 00833 The function adds double array b multiplied by scalar c to double array given by a, the result is stored in a 00834 00835 @param a is result array 00836 @param b is added array 00837 @param bc is scalar 00838 @param n is the number of vector components 00839 00840 @return The result is stored in the array a. 00841 00842 created by JK 00843 */ 00844 void addmultv (double *a,double *b,double bc,long n) 00845 { 00846 for (long i=0;i<n;i++) 00847 a[i]+=b[i]*bc; 00848 } 00849 00850 00851 00852 /** 00853 The function adds double array b multiplied by scalar bc to double array given by a, the result is stored in c 00854 00855 @param a is result array 00856 @param b is added array 00857 @param bc is scalar 00858 @param c is scalar 00859 @param n is the number of vector components 00860 00861 @return The result is stored in the array c. 00862 00863 created by TKo 00864 */ 00865 void addmultv (double *a,double *b,double bc, double* c,long n) 00866 { 00867 for (long i=0;i<n;i++) 00868 c[i] = a[i]+b[i]*bc; 00869 } 00870 00871 00872 00873 /** 00874 The function subtracts vector given by b from %vector given by a, the result is stored in the c 00875 00876 @param a is the structure of the %vector, from which is subtracted 00877 @param b is the structure of subtracted %vector 00878 @param c is the structure of the result %vector 00879 00880 @b Requests : 00881 a, b and c have to be same dimensions, c has to have allocated memory array for elements 00882 which is enough large to hold contents of the result. 00883 00884 @retval 0 on succes 00885 @retval 1 in case incompatibility sizes of a, b and c vectors 00886 00887 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00888 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00889 */ 00890 long subv(const vector &a, const vector &b, vector &c) 00891 { 00892 if ((a.n != b.n) || (b.n != c.n)) 00893 { 00894 print_err("cannot subtract vectors due to their incompatible dimensions\n" 00895 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 00896 return (1); 00897 } 00898 for (long i = 0; i < c.n; i++) 00899 c.a[i] = a.a[i] - b.a[i]; 00900 00901 return(0); 00902 } 00903 00904 00905 00906 /** 00907 The function subtracts vector given by b from %vector given by a, the result is stored in the a 00908 00909 @param a is the structure of the %vector, from which is subtracted 00910 @param b is the structure of subtracted %vector 00911 00912 @b Requests : 00913 a and b have to have same dimensions 00914 00915 @retval 0 on succes 00916 @retval 1 in case incompatibility sizes of a and b vectors 00917 00918 created 08.2011 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00919 */ 00920 long subv(const vector &a, const vector &b) 00921 { 00922 if (a.n != b.n) 00923 { 00924 print_err("cannot subtract vectors due to their incompatible dimensions\n" 00925 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 00926 return (1); 00927 } 00928 for (long i = 0; i < a.n; i++) 00929 a.a[i] -= b.a[i]; 00930 00931 return(0); 00932 } 00933 00934 00935 00936 /** 00937 The function subtracts %ivector given by b from %ivector given by a, the result is stored in the c 00938 00939 @param a is the structure of the %ivector, from which is subtracted 00940 @param b is the structure of subtracted %ivector 00941 @param c is the structure of the result %ivector 00942 00943 @b Requests : 00944 a, b and c should have same dimensions, c has to have allocated memory array for elements 00945 which is enough large to hold contents of the result. 00946 00947 @retval 0 on succes 00948 @retval 1 in case incompatibility sizes of a, b and c ivectors 00949 00950 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00951 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 00952 */ 00953 long subv(const ivector &a, const ivector &b, ivector &c) 00954 { 00955 if ((a.n != b.n) || (b.n != c.n)) 00956 { 00957 print_err("cannot subtract vectors due to their incompatible dimensions\n" 00958 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 00959 return (1); 00960 } 00961 for (long i = 0; i < c.n; i++) 00962 c.a[i] = a.a[i] - b.a[i]; 00963 00964 return(0); 00965 } 00966 00967 00968 00969 /** 00970 The function subtracts double array given by b from double array given by a, the result is stored in the a 00971 00972 @param a is the array, from which is subtracted 00973 @param b is the subtracted array 00974 @param n is number of members of arrays 00975 00976 @retval always zero 00977 00978 created by JK 00979 */ 00980 void subv (double *a,double *b,long n) 00981 { 00982 for (long i=0;i<n;i++) 00983 a[i]-=b[i]; 00984 } 00985 00986 00987 00988 /** 00989 The function subtracts double array given by b from double array by a, the result is stored 00990 in the array c. 00991 00992 @param a - double array of minuend 00993 @param b - double array of subtrahent 00994 @param c - double array of resulting difference 00995 @param n - length of arrays a, b and c 00996 00997 @return The result is returned in the parameter c. 00998 00999 Created by JK 01000 */ 01001 void subv (double *a,double *b,double *c,long n) 01002 { 01003 long i; 01004 01005 for (i=0;i<n;i++){ 01006 c[i] = a[i]-b[i]; 01007 } 01008 } 01009 01010 01011 01012 /** 01013 The function performs vector product, multiplies %vector given by a 01014 from right with %vector given by b, the result is stored in c 01015 01016 @param a is the structure of the %vector, from which is multiplied 01017 @param b is the structure of the multiplicating %vector 01018 @param c is the structure of the result %vector 01019 01020 @b Requests : 01021 a, b and c have to be same dimension equal 3 01022 c has to have allocated memory array for elements 01023 which is enough large to hold contents of the result. 01024 01025 @retval 0 on succes 01026 @retval 1 in case incompatibility sizes of a, b and c vectors 01027 @retval 2 in case a and b vectors have dimension inequal 3 01028 01029 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01030 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01031 */ 01032 long crprd(const vector &a, const vector &b, vector &c) 01033 { 01034 if ((a.n != b.n) || (b.n != c.n)) 01035 { 01036 print_err("cannot perform cross product of vectors due to their incompatible dimensions\n" 01037 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 01038 return (1); 01039 } 01040 if ((a.n != 3) || (b.n != 3)) 01041 { 01042 print_err("cannot perform cross product of vectors due to wrong dimensions (!= 3)\n" 01043 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 01044 return (2); 01045 } 01046 c.a[0] = a.a[1] * b.a[2] - a.a[2] * b.a[1]; 01047 c.a[1] = a.a[2] * b.a[0] - a.a[0] * b.a[2]; 01048 c.a[2] = a.a[0] * b.a[1] - a.a[1] * b.a[0]; 01049 01050 return (0); 01051 } 01052 01053 01054 01055 /** 01056 The function performs vector product, multiplies %ivector given by a 01057 from right with %ivector given by b, the result is stored in c 01058 01059 @param a is the structure of the %ivector, from which is multiplied 01060 @param b is the structure of the multiplicating %ivector 01061 @param c is the structure of the result %ivector 01062 01063 @b Requests : 01064 a, b and c should have same dimension equal 3 01065 c has to have allocated memory array for elements 01066 which is enough large to hold contents of the result. 01067 01068 01069 @retval 0 on succes 01070 @retval 1 in case incompatibility sizes of a, b and c vectors 01071 @retval 2 in case a and b vectors have dimension inequal 3 01072 01073 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01074 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01075 */ 01076 long crprd(const ivector &a, const ivector &b, ivector &c) 01077 { 01078 if ((a.n != b.n) || (b.n != c.n)) 01079 { 01080 print_err("cannot perform cross product of ivectors due to their incompatible dimensions\n" 01081 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 01082 return (1); 01083 } 01084 if ((a.n != 3) || (b.n != 3)) 01085 { 01086 print_err("cannot perform cross product of ivectors due to wrong dimensions (!= 3)\n" 01087 "a(%ld) X b(%ld) X c(%ld)", __FILE__, __LINE__, __func__, a.n, b.n, c.n); 01088 return (2); 01089 } 01090 c.a[0] = a.a[1] * b.a[2] - a.a[2] * b.a[1]; 01091 c.a[1] = a.a[2] * b.a[0] - a.a[0] * b.a[2]; 01092 c.a[2] = a.a[0] * b.a[1] - a.a[1] * b.a[0]; 01093 01094 return (0); 01095 } 01096 01097 01098 01099 /** 01100 The function performs scalar product of the %vector given by a and %vector given by b, 01101 the result is stored in the c. 01102 01103 @param a is the structure of the scalar multiplied %vector 01104 @param b is the structure of the scalar multiplied %vector 01105 @param scalar is the real number type of double by which the result is passed 01106 01107 @b Requests : 01108 a and b have to be same dimension 01109 01110 @retval 0 : on succes 01111 @retval 1 : in case incompatibility sizes of a and b 01112 01113 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01114 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01115 */ 01116 long scprd(const vector &a, const vector &b, double &scalar) 01117 { 01118 if (a.n != b.n) 01119 { 01120 print_err("cannot perform scalar product of vectors due to their incompatible dimensions\n" 01121 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 01122 return (1); 01123 } 01124 scalar = 0.0; 01125 for (long i=0; i < a.n; i++) 01126 scalar += a.a[i] * b.a[i]; 01127 return (0); 01128 } 01129 01130 01131 01132 /** 01133 The function computes the scalar product of the %vector given by a and %vector given by b. 01134 01135 @param a - onedimensional array of the first multiplied %vector 01136 @param b - onedimensional array of the second multiplied %vector 01137 @param n - number of components in arrays a and b. 01138 01139 @b Requests : 01140 a and b must have the same dimension 01141 01142 @return The function returns the scalar product of vectors a and b. 01143 01144 created 12.2011 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01145 */ 01146 double scprd(const double *a, const double *b, long n) 01147 { 01148 double scalar = 0.0; 01149 for (long i=0; i < n; i++) 01150 scalar += a[i] * b[i]; 01151 return (scalar); 01152 } 01153 01154 01155 01156 /** 01157 The function performs scalar product of the %ivector given by a and %ivector given by b, 01158 the result is stored in the c 01159 01160 @param a is the structure of the scalar multiplied %ivector 01161 @param b is the structure of the scalar multiplied %ivector 01162 @param scalar is the integer number type of long by which the result is passed 01163 01164 @b Requests : 01165 a and b should have same dimension 01166 01167 @retval 0 on succes 01168 @retval 1 in case incompatibility sizes of a and b 01169 01170 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01171 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01172 */ 01173 long scprd(const ivector &a, const ivector &b, long &scalar) 01174 { 01175 if (a.n != b.n) 01176 { 01177 print_err("cannot perform scalar product of ivectors due to their incompatible dimensions\n" 01178 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 01179 return (1); 01180 } 01181 scalar = 0; 01182 for (long i=0; i < a.n; i++) 01183 scalar += a.a[i] * b.a[i]; 01184 return (0); 01185 } 01186 01187 01188 01189 /** 01190 The function performs scalar product of the double array given by a and double array given by b, 01191 the result is stored in the c 01192 01193 @param a is the first array 01194 @param b is the second array 01195 @param n is number of members of arrays 01196 01197 created by JK 01198 */ 01199 double ss (double *a,double *b,long n) 01200 { 01201 double s = 0.0; 01202 for (long i=0;i<n;i++) 01203 s+=a[i]*b[i]; 01204 01205 return s; 01206 } 01207 01208 01209 01210 /** 01211 The function multiplies %vector given by u by real constant c, 01212 the result is stored in the double %vector v 01213 01214 @param c is the real number type double 01215 @param u is the structure of the %vector, which is multiplied 01216 @param v is the structure of the result %vector 01217 01218 @b Requests : 01219 u and v have to be same dimensions, u has to have allocated memory array for elements 01220 which is enough large to hold contents of the result. 01221 01222 @retval 0 : on succes 01223 @retval 1 : in case incompatibility sizes of u and v vectors 01224 01225 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01226 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01227 */ 01228 long cmulv(double c, const vector &u, vector &v) 01229 { 01230 if (u.n != v.n) 01231 { 01232 print_err("cannot multiply vector by constant due to their incompatible dimensions\n" 01233 "u(%ld) X v(%ld)", __FILE__, __LINE__, __func__, u.n, v.n); 01234 return (1); 01235 } 01236 for (long i=0; i < u.n; i++) 01237 v.a[i] = c * u.a[i]; 01238 01239 return (0); 01240 } 01241 01242 01243 01244 /** 01245 The function multiplies %ivector given by u by real constant c, 01246 the result is stored in the double %ivector v 01247 01248 @param c is the real number type double 01249 @param u is the structure of the %ivector, which is multiplied 01250 @param v is the structure of the result %vector 01251 01252 @b Requests : 01253 u and v should have same dimensions, u has to have allocated memory array for elements 01254 which is enough large to hold contents of the result. 01255 01256 @retval 0 on succes 01257 @retval 1 in case incompatibility sizes of u and v vectors 01258 01259 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01260 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01261 */ 01262 long cmulv(double c, const ivector &u, vector &v) 01263 { 01264 if (u.n != v.n) 01265 { 01266 print_err("cannot multiply ivector by constant due to their incompatible dimensions\n" 01267 "u(%ld) X v(%ld)", __FILE__, __LINE__, __func__, u.n, v.n); 01268 return (1); 01269 } 01270 for (long i=0; i < u.n; i++) 01271 v.a[i] = c * u.a[i]; 01272 01273 return (0); 01274 } 01275 01276 01277 01278 /** 01279 The function multiplies %ivector given by u by integer constant c, 01280 the result is stored in the %vector v 01281 01282 @param c is the integer number type long 01283 @param u is the structure of the %ivector, which is multiplied 01284 @param v is the structure of the result %ivector 01285 01286 @b Requests : 01287 u and v should have same dimensions, u has to have allocated memory array for elements 01288 which is enough large to hold contents of the result. 01289 01290 @retval 0 on succes 01291 @retval 1 in case incompatibility sizes of u and v %vector 01292 01293 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01294 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01295 */ 01296 long cmulv(long c, const ivector &u, ivector &v) 01297 { 01298 if (u.n != v.n) 01299 { 01300 print_err("cannot multiply ivector by constant due to their incompatible dimensions\n" 01301 "u(%ld) X v(%ld)", __FILE__, __LINE__, __func__, u.n, v.n); 01302 return (1); 01303 } 01304 for (long i=0; i < u.n; i++) 01305 v.a[i] = c * u.a[i]; 01306 01307 return (0); 01308 } 01309 01310 01311 01312 /** 01313 The function multiplies %vector given by a with constant c, 01314 the result is stored in a 01315 01316 @param c is the real number 01317 @param a is the structure of the %vector, which is multiplied 01318 01319 @return always zero 01320 01321 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01322 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01323 */ 01324 long cmulv(double c, vector &a) 01325 { 01326 for (long i=0; i < a.n; i++) 01327 a.a[i] *= c; 01328 01329 return (0); 01330 } 01331 01332 01333 01334 /** 01335 The function multiplies %ivector given by a with integer constant c, 01336 the result is stored in a 01337 01338 @param c is the integer number 01339 @param a is the structure of the %ivector, which is multiplied 01340 01341 @retval 0 always 01342 01343 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01344 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01345 */ 01346 long cmulv(long c, ivector &a) 01347 { 01348 for (long i=0; i < a.n; i++) 01349 a.a[i] *= c; 01350 01351 return (0); 01352 } 01353 01354 01355 01356 /** 01357 The function multiplies double array given by a with constant c, 01358 the result is stored in a 01359 01360 @param c is the real number 01361 @param a is the array, which is multiplied 01362 @param n is number of members of arrays 01363 01364 @return always zero 01365 01366 created by JK 01367 */ 01368 void cmulv(double c,double *a,long n) 01369 { 01370 for (long i=0; i < n; i++) 01371 a[i] *= c; 01372 01373 } 01374 01375 01376 01377 /** 01378 The function multiplies double array given by a with constant c, 01379 the result is stored in u. 01380 01381 @param c is the real number 01382 @param a is the array, which is multiplied 01383 @param u is the array, where the result is stored 01384 @param n is number of members of arrays 01385 01386 @return always zero 01387 01388 created by JK 01389 */ 01390 void cmulv(double c,double *a, double *u, long n) 01391 { 01392 for (long i=0; i < n; i++) 01393 u[i] = c*a[i]; 01394 } 01395 01396 01397 01398 /** 01399 The function computes norm of %vector given by a. 01400 01401 @param a - structure of %vector components 01402 01403 @return The function returns computed norm of the %vector. 01404 01405 created 02.2010 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01406 */ 01407 double normv(const vector &a) 01408 { 01409 double s = 0.0; 01410 for (long i=0; i < a.n; i++) 01411 s += a.a[i] * a.a[i]; 01412 return sqrt(s); 01413 } 01414 01415 01416 01417 /* 01418 The function computes norm of the %vector a. 01419 01420 @param a - array with vector values 01421 @param n - number of %vector components (i.e. size of the array a) 01422 01423 @return The function returns computed norm of the %vector. 01424 01425 created 02.2010 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01426 */ 01427 double normv(const double *a,long n) 01428 { 01429 long i; 01430 double s; 01431 01432 s=0.0; 01433 for (i=0;i<n;i++){ 01434 s+=a[i]*a[i]; 01435 } 01436 return sqrt(s); 01437 } 01438 01439 01440 01441 /** 01442 The function computes norm of the %ivector given by a, 01443 the result is stored in the parameter norm 01444 01445 @param a is the structure of the ivector, which size is requested 01446 01447 @return Returns computed size of %vector norm. 01448 01449 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01450 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01451 */ 01452 double normv(const ivector &a) 01453 { 01454 double norm = 0.0; 01455 for (long i=0; i < a.n; i++) 01456 norm += a.a[i] * a.a[i]; 01457 norm = sqrt(norm); 01458 01459 return(norm); 01460 } 01461 01462 01463 01464 /** 01465 The function computes cosine of the angle between %vector given by a and b 01466 the result is stored in the cos 01467 01468 @param a is the structure of the first %vector 01469 @param b is the structure of the second %vector 01470 @param cos is real number, type double, where result is stored 01471 01472 @b Requests : 01473 vectors a nd b should have the same dimension 01474 01475 @retval 0 on succes 01476 @retval 1 in case 01477 01478 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01479 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01480 */ 01481 long cosav(const vector &a, const vector &b, double &cos) 01482 { 01483 if (a.n != b.n) 01484 { 01485 print_err("cannot determine cos angle of vectors due to their incompatible dimensions\n" 01486 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 01487 return (1); 01488 } 01489 01490 double scalar, norm_a, norm_b; 01491 scprd(a, b, scalar); 01492 norm_a = normv(a); 01493 norm_b = normv(b); 01494 cos = scalar/(norm_a * norm_b); 01495 01496 return (0); 01497 } 01498 01499 01500 01501 /** 01502 The function computes cosine of the angle between %ivector given by a and b 01503 the result is stored in the cos 01504 01505 @param a is the structure of the first %ivector 01506 @param b is the structure of the second %ivector 01507 @param cos is real number, type double, where result is stored 01508 01509 @b Requests : 01510 vectors a nd b should have the same dimension 01511 01512 @retval 0 on succes 01513 @retval 1 in case 01514 01515 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01516 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01517 */ 01518 long cosav(const ivector &a, const ivector &b, double &cos) 01519 { 01520 if (a.n != b.n) 01521 { 01522 print_err("cannot determine cos angle of ivectors due to their incompatible dimensions\n" 01523 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 01524 return (1); 01525 } 01526 01527 long scalar; 01528 double norm_a, norm_b; 01529 scprd(a, b, scalar); 01530 norm_a = normv(a); 01531 norm_b = normv(b); 01532 cos = scalar/(norm_a * norm_b); 01533 01534 return (0); 01535 } 01536 01537 01538 01539 /** 01540 The function changes components of %vector a to their absolute value 01541 01542 @param a is the structure of the first %ivector 01543 01544 created 7.2008 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01545 */ 01546 void absv(vector &a) 01547 { 01548 long i; 01549 for (i=0; i<a.n; i++) 01550 a[i] = fabs(a[i]); 01551 } 01552 01553 01554 01555 /** 01556 The function changes components of %vector a to their absolute value 01557 01558 @param a is the structure of the first %ivector 01559 01560 created 7.2008 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01561 */ 01562 void absv(ivector &a) 01563 { 01564 long i; 01565 for (i=0; i<a.n; i++) 01566 a[i] = labs(a[i]); 01567 } 01568 01569 01570 01571 /** 01572 The function prints out the contents of the %vector u to the out file, 01573 Optionally, the file, precision and field width can be specified file. 01574 01575 @param u is the structure of the %vector which is printed 01576 01577 @b Optionally : 01578 @param out is the structure with opened file for %vector output (default is stdout) 01579 @param prec is desired precision of the %vector elements on the output (default is 3) 01580 @param width is desired width of the %vector elements on the output (default is 11) 01581 01582 @retval 0 : on succes 01583 @retval 1 : in case NULL pointer of the out parameter 01584 01585 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01586 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01587 */ 01588 long printv(const vector &u, FILE *out, int prec, int width) 01589 { 01590 if (out == NULL) 01591 { 01592 print_err("cannot print vector - parameter for output is a NULL pointer", 01593 __FILE__, __LINE__, __func__); 01594 return (1); 01595 } 01596 for (long i = 0; i < u.n; i++) 01597 fprintf(out, "%*.*e\n", width, prec, u.a[i]); 01598 return (0); 01599 } 01600 01601 01602 01603 /** 01604 The function prints out the contents of the %vector u given by array to the out file. 01605 Optionally, file, precision and field width can be specified 01606 01607 @param u - pointer to array of double values which will be printed 01608 @param n - size of array u 01609 01610 @b Optionally : 01611 @param out is the structure with opened file for %vector output (default is stdout) 01612 @param prec is desired precision of the %vector elements on the output (default is 3) 01613 @param width is desired width of the %vector elements on the output (default is 11) 01614 01615 @retval 0 : on succes 01616 @retval 1 : in case NULL pointer of the out parameter 01617 01618 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01619 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01620 */ 01621 long printv(const double *u, long n, FILE *out, int prec, int width) 01622 { 01623 if (out == NULL) 01624 { 01625 print_err("cannot print vector - parameter for output is a NULL pointer", 01626 __FILE__, __LINE__, __func__); 01627 return (1); 01628 } 01629 for (long i = 0; i < n; i++) 01630 fprintf(out, "%*.*e\n", width, prec, u[i]); 01631 return (0); 01632 } 01633 01634 01635 01636 /** 01637 The function prints contents of the %vector u into the file out, 01638 01639 @param out - pointer to the structure with opened output file 01640 @param u - %vector which contents will be printed 01641 01642 @b Requirements : 01643 Vector u have to be allocated memory. 01644 Number of read values is given by the %vector n member. 01645 01646 @retval 0 : on succes 01647 @retval 1 : in case NULL pointer of the in parameter 01648 @retval 2 : in case error reading number from the file 01649 01650 TKr, 11/02/2013 accroding to readv(XFILE *in, vector u) 01651 */ 01652 long printv(FILE *out, vector &u) 01653 { 01654 for (long i = 0; i < u.n; i++) 01655 { 01656 fprintf(out, " %le", u[i]); 01657 } 01658 return (0); 01659 } 01660 01661 01662 01663 /** 01664 The function prints out the contents of the %ivector u to the file out. 01665 Optionally, the file and field width can be specified. 01666 01667 @param u is the structure of the %ivector which is printed 01668 01669 @b Optionally : 01670 @param out is the structure with opened file for %ivector output (default is stdout) 01671 @param width is desired width of the %ivector elements on the output (default is 11) 01672 01673 @retval 0 on succes 01674 @retval 1 in case NULL pointer of the out parameter 01675 01676 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01677 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01678 */ 01679 long printv(const ivector &u, FILE *out, int width) 01680 { 01681 if (out == NULL) 01682 { 01683 print_err("cannot print ivector - parameter for output is a NULL pointer", 01684 __FILE__, __LINE__, __func__); 01685 return (1); 01686 } 01687 for (long i = 0; i < u.n; i++) 01688 fprintf(out, "%*ld\n", width, u.a[i]); 01689 return (0); 01690 } 01691 01692 01693 01694 /** 01695 The function prints out the contents of the %vector u (integer values are assumed) 01696 given by array. Optionally, the file and field width can be specified. 01697 01698 @param u - pointer to array of integer values which will be printed 01699 @param n - size of array u 01700 01701 @b Optionally : 01702 @param out is the structure with opened file for %ivector output (default is stdout) 01703 @param width is desired width of the %ivector elements on the output (default is 11) 01704 01705 @retval 0 on succes 01706 @retval 1 in case NULL pointer of the out parameter 01707 01708 created 29.5.2000 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01709 modified 9.5.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01710 */ 01711 long printv(const long *u, long n, FILE *out, int width) 01712 { 01713 if (out == NULL) 01714 { 01715 print_err("cannot print vector - parameter for output is a NULL pointer", 01716 __FILE__, __LINE__, __func__); 01717 return (1); 01718 } 01719 for (long i = 0; i < n; i++) 01720 fprintf(out, "%*ld\n", width, u[i]); 01721 return (0); 01722 } 01723 01724 01725 01726 /** 01727 The function reads contents of the %vector u from the file in, 01728 01729 @param in pointer to the structure with opened input file 01730 @param u %vector which contents will be read 01731 01732 @b Requirements : 01733 Vector u have to be allocated memory. 01734 Number of read values is given by the %vector n member. 01735 01736 @retval 0 : on succes 01737 @retval 1 : in case NULL pointer of the in parameter 01738 @retval 2 : in case error reading number from the file 01739 01740 created 3.12.2006 by Jaroslav Kruis, jk@cml.fsv.cvut.cz 01741 */ 01742 long readv(XFILE *in, vector &u) 01743 { 01744 if (in == NULL) 01745 { 01746 print_err("cannot read vector - parameter for input file is a NULL pointer", 01747 __FILE__, __LINE__, __func__); 01748 return (1); 01749 } 01750 for (long i = 0; i < u.n; i++) 01751 { 01752 if (xfscanf(in, "%le", &u.a[i]) != 1) 01753 return(2); 01754 } 01755 return (0); 01756 } 01757 01758 01759 /** 01760 The function reads contents of the %vector u from the string in, 01761 01762 @param in pointer to the input string 01763 @param u vector which contents will be read 01764 01765 @b Requirements : 01766 Vector u have to be allocated memory. 01767 Number of read values is given by the vector n member. 01768 01769 @retval 0 : on succes 01770 @retval 1 : in case NULL pointer of the in parameter 01771 @retval 2 : in case error reading number from the file 01772 01773 created 6.9.2001 by Tomas Koudelka, koudelka@cml.fsv.cvut.cz 01774 */ 01775 long readv(char *in, vector &u) 01776 { 01777 if (in == NULL) 01778 { 01779 print_err("cannot read vector - parameter for input string is a NULL pointer", 01780 __FILE__, __LINE__, __func__); 01781 return (1); 01782 } 01783 for (long i = 0; i < u.n; i++) 01784 { 01785 if (sscanf(in, "%le", &u.a[i]) != 1) 01786 return(2); 01787 } 01788 return (0); 01789 } 01790 01791 01792 01793 /** 01794 function extracts ncomp components from %vector b 01795 and put them into %vector a 01796 01797 @param a - %vector containing extracted components 01798 @param b - %vector from where components are extracted 01799 @param fi - first index 01800 @param ncomp - number of components 01801 01802 30.11.2002 01803 */ 01804 void extract (vector &a,vector &b,long fi, long ncomp) 01805 { 01806 long i,j; 01807 01808 j=fi; 01809 for (i=0;i<ncomp;i++){ 01810 a[i]=b[j]; 01811 j++; 01812 } 01813 } 01814 01815 01816 01817 /** 01818 Function extracts positive components from vector a 01819 and puts them into vector b in corresponding position as in the vector a. 01820 If component is negative the zero is stored. 01821 01822 @param a - %vector containing compared components 01823 @param b - %vector where extracted components are stored 01824 01825 3.3.2003 01826 */ 01827 long extractposv (vector &a,vector &b) 01828 { 01829 long i; 01830 if (a.n != b.n) 01831 { 01832 print_err("cannot extract positive components - incompatible dimensions of vectors\n" 01833 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 01834 return (1); 01835 } 01836 for (i = 0; i < a.n; i++) 01837 { 01838 if (a[i] > 0.0) 01839 b[i] = a[i]; 01840 else 01841 b[i] = 0.0; 01842 } 01843 return 0; 01844 } 01845 01846 01847 01848 /** 01849 Function extracts negative components from vector a 01850 and puts them into vector b in corresponding position as in the vector a. 01851 If component is positive the zero is stored. 01852 01853 @param a - %vector containing compared components 01854 @param b - %vector where extracted components are stored 01855 01856 25.2.2004 01857 */ 01858 long extractnegv (vector &a,vector &b) 01859 { 01860 long i; 01861 if (a.n != b.n) 01862 { 01863 print_err("cannot extract negative components - incompatible dimensions of vectors\n" 01864 "a(%ld) X b(%ld)", __FILE__, __LINE__, __func__, a.n, b.n); 01865 return (1); 01866 } 01867 for (i = 0; i < a.n; i++) 01868 { 01869 if (a[i] < 0.0) 01870 b[i] = a[i]; 01871 else 01872 b[i] = 0.0; 01873 } 01874 return 0; 01875 } 01876 01877 01878 01879 /** 01880 The function sorts ivector v by Shell sort algorithm in ascending order. 01881 01882 @param v - vector of sorted numbers 01883 01884 @return The function does not return anything but changes order of v elements. 01885 01886 Created by Tomas Koudelka, 2.7.2013 01887 */ 01888 void shell_sort(ivector &v) 01889 { 01890 long flag = 1, d = v.n, i, temp; 01891 01892 while(flag || (d>1)) // boolean flag (true when not equal to 0) 01893 { 01894 flag = 0; // reset flag to 0 to check for future swaps 01895 d = (d+1)/2; 01896 for (i=0; i<(v.n-d); i++) 01897 { 01898 if (v[i+d] < v[i]) 01899 { 01900 temp = v[i+d]; // swap items at positions i+d and d 01901 v[i+d] = v[i]; 01902 v[i] = temp; 01903 flag = 1; // indicate that a swap has occurred 01904 } 01905 } 01906 } 01907 return; 01908 } 01909 01910 01911 01912 /** 01913 The function sorts array of integer numbers v by Shell sort algorithm in ascending order. 01914 01915 @param v - array of sorted numbers 01916 @param n - length of array v 01917 01918 @return The function does not return anything but changes order of v elements. 01919 01920 Created by Tomas Koudelka, 3.7.2013 01921 */ 01922 void shell_sort(long *v, long n) 01923 { 01924 long flag = 1, d = n, i, temp; 01925 01926 while(flag || (d>1)) // boolean flag (true when not equal to 0) 01927 { 01928 flag = 0; // reset flag to 0 to check for future swaps 01929 d = (d+1)/2; 01930 for (i=0; i<(n-d); i++) 01931 { 01932 if (v[i+d] < v[i]) 01933 { 01934 temp = v[i+d]; // swap items at positions i+d and d 01935 v[i+d] = v[i]; 01936 v[i] = temp; 01937 flag = 1; // indicate that a swap has occurred 01938 } 01939 } 01940 } 01941 return; 01942 } 01943 01944 01945 01946 /** 01947 function reads number of attributes and order of attributes used in stochastic or fuzzy computations 01948 01949 @param in - pointer to input file 01950 01951 JK, 23.8.2005 01952 */ 01953 long atsel::read(XFILE *in) 01954 { 01955 long i; 01956 01957 // number of all attributes 01958 // number of basic attributes 01959 // number of hidden attributes 01960 // 01961 // number of basic attributes should be usually equal to the number of all attributes 01962 // it differs in case of plastic material, where parameters of the model are stored first (nba components) 01963 // and hardening/softening parameters are stored behind them (nia components) 01964 xfscanf(in, "%ld %ld %ld", &num,&nba,&nia); 01965 01966 if (nba+nia != num) 01967 print_err("wrong number of all attributes, basic attributes and hidden attributes", 01968 __FILE__, __LINE__, __func__); 01969 01970 atrib = new long[num]; 01971 for (i = 0; i < num; i++){ 01972 xfscanf(in, "%ld", atrib+i); 01973 atrib[i]--; 01974 } 01975 return 0; 01976 }