00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef __VECTOR_H__
00017 #define __VECTOR_H__
00018
00019
00020
00021 #include <iostream>
00022 #include <fstream>
00023 #include <sstream>
00024 #include <string>
00025 #include <cstdlib>
00026 #include <climits>
00027 #include <cmath>
00028
00029 namespace Flood
00030 {
00031
00033
00034 template <class Type> class Matrix;
00035
00038
00039 template <class Type>
00040 class Vector
00041 {
00042
00043 public:
00044
00045
00046
00047
00048
00049 explicit Vector(void);
00050
00051
00052
00053 explicit Vector(int);
00054
00055
00056
00057 explicit Vector(int, const Type&);
00058
00059
00060
00061 explicit Vector(const char*);
00062
00063
00064
00065 Vector(const Vector&);
00066
00067
00068
00069 virtual ~Vector(void);
00070
00071
00072
00073 Vector& operator = (const Vector&);
00074
00075
00076
00077 inline Type& operator [] (int);
00078 inline const Type& operator [] (int) const;
00079
00080
00081
00082 Vector<Type> operator + (const Type&) const;
00083 Vector<Type> operator + (const Vector<Type>&) const;
00084
00085 Vector<Type> operator - (const Type&) const;
00086 Vector<Type> operator - (const Vector<Type>&) const;
00087
00088 Vector<Type> operator * (const Type&) const;
00089 Vector<Type> operator * (const Vector<Type>&) const;
00090
00091 Vector<Type> operator / (const Type&) const;
00092 Vector<Type> operator / (const Vector<Type>&) const;
00093
00094
00095
00096 void operator += (const Type&) const;
00097 void operator += (const Vector<Type>&) const;
00098
00099 void operator -= (const Type&) const;
00100 void operator -= (const Vector<Type>&) const;
00101
00102 void operator *= (const Type&) const;
00103 void operator *= (const Vector<Type>&) const;
00104
00105 void operator /= (const Type&) const;
00106 void operator /= (const Vector<Type>&) const;
00107
00108
00109
00110 bool operator == (const Vector<Type>&) const;
00111 bool operator == (const Type&) const;
00112
00113 bool operator != (const Vector<Type>&) const;
00114 bool operator != (const Type&) const;
00115
00116 bool operator > (const Vector<Type>&) const;
00117 bool operator > (const Type&) const;
00118
00119 bool operator < (const Vector<Type>&) const;
00120 bool operator < (const Type&) const;
00121
00122 bool operator >= (const Vector<Type>&) const;
00123 bool operator >= (const Type&) const;
00124
00125 bool operator <= (const Vector<Type>&) const;
00126 bool operator <= (const Type&) const;
00127
00128
00129
00130
00131 Type* begin() const;
00132 Type* end() const;
00133
00134
00135
00136 inline int get_size(void) const;
00137
00138 bool get_display(void) const;
00139
00140
00141
00142 void set(void);
00143 void set(int);
00144 void set(int, const Type&);
00145 void set(const Vector&);
00146 void set(const char*);
00147
00148 void set_size(int);
00149 void set_size(int, const Type&);
00150 void set_display(bool);
00151
00152
00153
00154 void resize(int);
00155
00156 bool is_zero(void) const;
00157
00158 void insert(int, const Vector<Type>&) const;
00159 Vector<Type> extract(int, int) const;
00160
00161 Vector<Type> assemble(const Vector<Type>&) const;
00162
00163
00164
00165 void initialize(const Type&) const;
00166
00167 void initialize_sequential(void) const;
00168
00169 void initialize_uniform(void) const;
00170 void initialize_uniform(double, double) const;
00171 void initialize_uniform(const Vector<double>&, const Vector<double>&) const;
00172
00173 void initialize_normal(void) const;
00174 void initialize_normal(double, double) const;
00175 void initialize_normal(const Vector<double>&, const Vector<double>&) const;
00176
00177
00178
00179 Type dot(const Vector<Type>&) const;
00180 Vector<Type> dot(const Matrix<Type>&) const;
00181 Matrix<Type> outer(const Vector<Type>&) const;
00182
00183 Type calculate_sum(void) const;
00184 Type calculate_product(void) const;
00185
00186 double calculate_mean(void) const;
00187 double calculate_standard_deviation(void) const;
00188
00189 Vector<double> calculate_mean_standard_deviation(void) const;
00190
00191 Type calculate_minimum(void) const;
00192 Type calculate_maximum(void) const;
00193
00194 Vector<Type> calculate_minimum_maximum(void) const;
00195
00196 int calculate_minimal_index(void) const;
00197 int calculate_maximal_index(void) const;
00198
00199 Vector<int> calculate_minimal_maximal_index(void) const;
00200
00201 double calculate_norm(void) const;
00202
00203 Vector<Type> calculate_lower_bounded(const Type&) const;
00204 Vector<Type> calculate_lower_bounded(const Vector<Type>&) const;
00205
00206 Vector<Type> calculate_upper_bounded(const Type&) const;
00207 Vector<Type> calculate_upper_bounded(const Vector<Type>&) const;
00208
00209 Vector<Type> calculate_lower_upper_bounded(const Type&, const Type&) const;
00210 Vector<Type> calculate_lower_upper_bounded(const Vector<Type>&, const Vector<Type>&) const;
00211
00212 void apply_lower_bound(const Type&);
00213 void apply_lower_bound(const Vector<Type>&);
00214
00215 void apply_upper_bound(const Type&);
00216 void apply_upper_bound(const Vector<Type>&);
00217
00218 void apply_lower_upper_bounds(const Type&, const Type&);
00219 void apply_lower_upper_bounds(const Vector<Type>&, const Vector<Type>&);
00220
00221
00222
00223 std::string to_XML(bool);
00224
00225 void save(const char*);
00226 void save_data(const char*);
00227
00228 void load(const char*);
00229 void load_data(const char*);
00230
00231 private:
00232
00234
00235 int size;
00236
00237 bool display;
00238
00240
00241 Type* data;
00242
00243
00244
00245 double calculate_random_uniform(double, double) const;
00246 double calculate_random_normal(double, double) const;
00247 };
00248
00249
00250
00251
00253
00254 template <class Type>
00255 Vector<Type>::Vector(void)
00256 {
00257 size = 0;
00258 display = true;
00259 data = NULL;
00260 }
00261
00262
00265
00266 template <class Type>
00267 Vector<Type>::Vector(int new_size)
00268 {
00269
00270
00271 #ifdef _DEBUG
00272
00273 if(new_size < 0)
00274 {
00275 std::cout << "Flood Error: Vector Template." << std::endl
00276 << "Vector(int) constructor." << std::endl
00277 << "Size must be equal or greater than zero." << std::endl;
00278
00279 exit(1);
00280 }
00281
00282 #endif
00283
00284 size = new_size;
00285 display = true;
00286 data = new Type[new_size];
00287 }
00288
00289
00294
00295 template <class Type>
00296 Vector<Type> ::Vector(int new_size, const Type& value)
00297 {
00298
00299
00300 #ifdef _DEBUG
00301
00302 if(new_size < 0)
00303 {
00304 std::cerr << "Flood Error: Vector Template." << std::endl
00305 << "Vector(int, const Type&) constructor." << std::endl
00306 << "Size must be equal or greater than zero." << std::endl;
00307
00308 exit(1);
00309 }
00310
00311 #endif
00312
00313 size = new_size;
00314 display = true;
00315 data = new Type[new_size];
00316
00317 for(int i = 0; i < size; i++)
00318 {
00319 data[i] = value;
00320 }
00321 }
00322
00323
00326
00327 template <class Type>
00328 Vector<Type>::Vector(const char* filename)
00329 {
00330 size = 0;
00331 display = true;
00332 data = NULL;
00333
00334 load(filename);
00335 }
00336
00337
00340
00341 template <class Type>
00342 Vector<Type>::Vector(const Vector<Type>& other_vector)
00343 {
00344 size = other_vector.size;
00345 display = true;
00346 data = new Type[size];
00347
00348
00349
00350 for(int i = 0; i < size; i++)
00351 {
00352 data[i] = other_vector[i];
00353 }
00354 }
00355
00356
00357
00358
00360
00361 template <class Type>
00362 Vector<Type>::~Vector(void)
00363 {
00364 if(data != NULL)
00365 {
00366 delete[](data);
00367 }
00368 }
00369
00370
00371
00372
00375
00376 template <class Type>
00377 Vector<Type>& Vector<Type>::operator = (const Vector<Type>& other_vector)
00378 {
00379 if(this != &other_vector)
00380 {
00381 if(size != other_vector.get_size())
00382 {
00383 if(data != NULL)
00384 {
00385
00386
00387
00388
00389
00390
00391
00392
00393 delete [] (data);
00394 }
00395
00396 size = other_vector.size;
00397
00398 data = new Type[size];
00399 }
00400
00401 for(int i = 0; i < size; i++)
00402 {
00403 data[i] = other_vector[i];
00404 }
00405
00406 display = other_vector.get_display();
00407 }
00408
00409 return(*this);
00410 }
00411
00412
00413
00414
00417
00418 template <class Type>
00419 inline Type& Vector<Type>::operator [] (int i)
00420 {
00421
00422
00423 #ifdef _DEBUG
00424
00425 if(size == 0)
00426 {
00427 std::cerr << "Flood Error: Vector Template. " << std::endl
00428 << "Reference operator []." << std::endl
00429 << "Size of vector is zero." << std::endl;
00430
00431 exit(1);
00432 }
00433 else if(i < 0)
00434 {
00435 std::cerr << "Flood Error: Vector Template. " << std::endl
00436 << "Reference operator []." << std::endl
00437 << "Index must be equal or greater than zero." << std::endl;
00438
00439 exit(1);
00440 }
00441 else if(i >= size)
00442 {
00443 std::cerr << "Flood Error: Vector Template. " << std::endl
00444 << "Reference operator []." << std::endl
00445 << "Index is " << i << " and it must be less than " << size << "." << std::endl;
00446
00447 exit(1);
00448 }
00449
00450 #endif
00451
00452
00453
00454 return(data[i]);
00455 }
00456
00457
00460
00461 template <class Type>
00462 inline const Type& Vector<Type>::operator [] (int i) const
00463 {
00464
00465
00466 #ifdef _DEBUG
00467
00468 if(size == 0)
00469 {
00470 std::cerr << "Flood Error: Vector Template. " << std::endl
00471 << "Reference operator []." << std::endl
00472 << "Size of vector is zero." << std::endl;
00473
00474 exit(1);
00475 }
00476 else if(i < 0)
00477 {
00478 std::cerr << "Flood Error: Vector Template. " << std::endl
00479 << "Reference operator []." << std::endl
00480 << "Index must be equal or greater than zero." << std::endl;
00481
00482 exit(1);
00483 }
00484 else if(i >= size)
00485 {
00486 std::cerr << "Flood Error: Vector Template. " << std::endl
00487 << "Reference operator []." << std::endl
00488 << "Index is " << i << " and it must be less than " << size << "." << std::endl;
00489
00490 exit(1);
00491 }
00492
00493 #endif
00494
00495 return(data[i]);
00496 }
00497
00498
00499
00500
00504
00505 template <class Type>
00506 bool Vector<Type>::operator == (const Vector<Type>& other_vector) const
00507 {
00508
00509
00510 #ifdef _DEBUG
00511
00512 int other_size = other_vector.get_size();
00513
00514 if(other_size != size)
00515 {
00516 std::cerr << "Flood Error: Vector Template." << std::endl
00517 << "bool operator == (const Vector<Type>&) const." << std::endl
00518 << "Size of vectors are " << size << " and " << other_size << " and they must be the same."
00519 << std::endl;
00520
00521 exit(1);
00522 }
00523
00524 #endif
00525
00526 for(int i = 0; i < size; i++)
00527 {
00528 if(data[i] != other_vector[i])
00529 {
00530 return(false);
00531 }
00532 }
00533
00534 return(true);
00535 }
00536
00537
00538
00539
00543
00544 template <class Type>
00545 bool Vector<Type>::operator == (const Type& value) const
00546 {
00547 for(int i = 0; i < size; i++)
00548 {
00549 if(data[i] != value)
00550 {
00551 return(false);
00552 }
00553 }
00554
00555 return(true);
00556 }
00557
00558
00559
00560
00564
00565 template <class Type>
00566 bool Vector<Type>::operator != (const Vector<Type>& other_vector) const
00567 {
00568
00569
00570 #ifdef _DEBUG
00571
00572 int other_size = other_vector.get_size();
00573
00574 if(other_size != size)
00575 {
00576 std::cerr << "Flood Error: Vector Template." << std::endl
00577 << "bool operator != (const Vector<Type>&) const." << std::endl
00578 << "Size of vectors are " << size << " and " << other_size << " and they must be the same."
00579 << std::endl;
00580
00581 exit(1);
00582 }
00583
00584 #endif
00585
00586 for(int i = 0; i < size; i++)
00587 {
00588 if(data[i] != other_vector[i])
00589 {
00590 return(true);
00591 }
00592 }
00593
00594 return(false);
00595 }
00596
00597
00598
00599
00603
00604 template <class Type>
00605 bool Vector<Type>::operator != (const Type& value) const
00606 {
00607 for(int i = 0; i < size; i++)
00608 {
00609 if(data[i] != value)
00610 {
00611 return(true);
00612 }
00613 }
00614
00615 return(false);
00616 }
00617
00618
00619
00620
00625
00626 template <class Type>
00627 bool Vector<Type>::operator > (const Vector<Type>& other_vector) const
00628 {
00629
00630
00631 #ifdef _DEBUG
00632
00633 int other_size = other_vector.get_size();
00634
00635 if(other_size != size)
00636 {
00637 std::cerr << "Flood Error: Vector Template." << std::endl
00638 << "bool operator > (const Vector<Type>&) const." << std::endl
00639 << "Size of vectors are " << size << " and " << other_size << " and they must be the same."
00640 << std::endl;
00641
00642 exit(1);
00643 }
00644
00645 #endif
00646
00647 for(int i = 0; i < size; i++)
00648 {
00649 if(data[i] <= other_vector[i])
00650 {
00651 return(false);
00652 }
00653 }
00654
00655 return(true);
00656 }
00657
00658
00659
00660
00664
00665 template <class Type>
00666 bool Vector<Type>::operator > (const Type& value) const
00667 {
00668 for(int i = 0; i < size; i++)
00669 {
00670 if(data[i] <= value)
00671 {
00672 return(false);
00673 }
00674 }
00675
00676 return(true);
00677 }
00678
00679
00680
00681
00686
00687 template <class Type>
00688 bool Vector<Type>::operator < (const Vector<Type>& other_vector) const
00689 {
00690
00691
00692 #ifdef _DEBUG
00693
00694 int other_size = other_vector.get_size();
00695
00696 if(other_size != size)
00697 {
00698 std::cerr << "Flood Error: Vector Template." << std::endl
00699 << "bool operator < (const Vector<Type>&) const." << std::endl
00700 << "Size of vectors are " << size << " and " << other_size << " and they must be the same."
00701 << std::endl;
00702
00703 exit(1);
00704 }
00705
00706 #endif
00707
00708 for(int i = 0; i < size; i++)
00709 {
00710 if(data[i] >= other_vector[i])
00711 {
00712 return(false);
00713 }
00714 }
00715
00716 return(true);
00717 }
00718
00719
00720
00721
00725
00726 template <class Type>
00727 bool Vector<Type>::operator < (const Type& value) const
00728 {
00729 for(int i = 0; i < size; i++)
00730 {
00731 if(data[i] >= value)
00732 {
00733 return(false);
00734 }
00735 }
00736
00737 return(true);
00738 }
00739
00740
00741
00742
00747
00748 template <class Type>
00749 bool Vector<Type>::operator >= (const Vector<Type>& other_vector) const
00750 {
00751
00752
00753 #ifdef _DEBUG
00754
00755 int other_size = other_vector.get_size();
00756
00757 if(other_size != size)
00758 {
00759 std::cerr << "Flood Error: Vector Template." << std::endl
00760 << "bool operator >= (const Vector<Type>&) const." << std::endl
00761 << "Size of vectors are " << size << " and " << other_size << " and they must be the same." << std::endl;
00762
00763 exit(1);
00764 }
00765
00766 #endif
00767
00768 for(int i = 0; i < size; i++)
00769 {
00770 if(data[i] < other_vector[i])
00771 {
00772 return(false);
00773 }
00774 }
00775
00776 return(true);
00777 }
00778
00779
00780
00781
00785
00786 template <class Type>
00787 bool Vector<Type>::operator >= (const Type& value) const
00788 {
00789 for(int i = 0; i < size; i++)
00790 {
00791 if(data[i] < value)
00792 {
00793 return(false);
00794 }
00795 }
00796
00797 return(true);
00798 }
00799
00800
00801
00802
00807
00808 template <class Type>
00809 bool Vector<Type>::operator <= (const Vector<Type>& other_vector) const
00810 {
00811
00812
00813 #ifdef _DEBUG
00814
00815 int other_size = other_vector.get_size();
00816
00817 if(other_size != size)
00818 {
00819 std::cerr << "Flood Error: Vector Template." << std::endl
00820 << "bool operator <= (const Vector<Type>&) const." << std::endl
00821 << "Size of vectors are " << size << " and " << other_size << " and they must be the same."
00822 << std::endl;
00823
00824 exit(1);
00825 }
00826
00827 #endif
00828
00829 for(int i = 0; i < size; i++)
00830 {
00831 if(data[i] > other_vector[i])
00832 {
00833 return(false);
00834 }
00835 }
00836
00837 return(true);
00838 }
00839
00840
00841
00842
00846
00847 template <class Type>
00848 bool Vector<Type>::operator <= (const Type& value) const
00849 {
00850 for(int i = 0; i < size; i++)
00851 {
00852 if(data[i] > value)
00853 {
00854 return(false);
00855 }
00856 }
00857
00858 return(true);
00859 }
00860
00861
00862
00863
00864
00865
00867
00868 template <class Type>
00869 inline int Vector<Type>::get_size(void) const
00870 {
00871 return(size);
00872 }
00873
00874
00875
00876
00879
00880 template <class Type>
00881 inline bool Vector<Type>::get_display(void) const
00882 {
00883 return(display);
00884 }
00885
00886
00887
00888
00890
00891 template <class Type>
00892 bool Vector<Type>::is_zero(void) const
00893 {
00894 if(size == 0)
00895 {
00896 return(true);
00897 }
00898 else
00899 {
00900 return(false);
00901 }
00902 }
00903
00904
00905
00906
00909
00910 template <class Type>
00911 void Vector<Type>::set(void)
00912 {
00913 if(data != NULL)
00914 {
00915 delete[](data);
00916 }
00917
00918 size = 0;
00919 display = true;
00920 data = NULL;
00921 }
00922
00923
00924
00925
00929
00930 template <class Type>
00931 void Vector<Type>::set(int new_size)
00932 {
00933
00934
00935 #ifdef _DEBUG
00936
00937 if(new_size < 0)
00938 {
00939 std::cerr << "Flood Error: Vector Template." << std::endl
00940 << "void set(int) method." << std::endl
00941 << "Size must be equal or greater than zero." << std::endl;
00942
00943 exit(1);
00944 }
00945
00946 #endif
00947
00948 if(new_size != size)
00949 {
00950 if(data != NULL)
00951 {
00952 delete[](data);
00953 }
00954
00955 size = new_size;
00956 display = true;
00957 data = new Type[size];
00958 }
00959 }
00960
00961
00962
00963
00968
00969 template <class Type>
00970 void Vector<Type>::set(int new_size, const Type& new_value)
00971 {
00972
00973
00974 #ifdef _DEBUG
00975
00976 if(new_size < 0)
00977 {
00978 std::cerr << "Flood Error: Vector Template." << std::endl
00979 << "void set(int) method." << std::endl
00980 << "Size must be equal or greater than zero." << std::endl;
00981
00982 exit(1);
00983 }
00984
00985 #endif
00986
00987 set(new_size);
00988 display = true;
00989 initialize(new_value);
00990 }
00991
00992
00993
00994
00997
00998 template <class Type>
00999 void Vector<Type>::set(const Vector& other_vector)
01000 {
01001 if(this != &other_vector)
01002 {
01003 if(size != other_vector.size)
01004 {
01005 if(data != NULL)
01006 {
01007
01008
01009
01010
01011
01012
01013
01014
01015 delete [] (data);
01016 }
01017
01018 size = other_vector.get_size();
01019 display = other_vector.get_display();
01020 data = new Type[size];
01021 }
01022
01023 for(int i = 0; i < size; i++)
01024 {
01025 data[i] = other_vector[i];
01026 }
01027 }
01028 }
01029
01030
01031
01032
01036
01037 template <class Type>
01038 void Vector<Type>::set(const char* filename)
01039 {
01040 load(filename);
01041 }
01042
01043
01044
01045
01050
01051 template <class Type>
01052 void Vector<Type>::set_display(bool new_display)
01053 {
01054 display = new_display;
01055 }
01056
01057
01058
01059
01063
01064 template <class Type>
01065 void Vector<Type>::set_size(int new_size)
01066 {
01067
01068
01069 #ifdef _DEBUG
01070
01071 if(new_size < 0)
01072 {
01073 std::cerr << "Flood Error: Vector Template." << std::endl
01074 << "void set_size(int) method." << std::endl
01075 << "Size must be equal or greater than zero." << std::endl;
01076
01077 exit(1);
01078 }
01079
01080 #endif
01081
01082 if(size != new_size)
01083 {
01084 if(data != 0)
01085 {
01086 delete[](data);
01087 }
01088
01089 data = new Type[new_size];
01090 size = new_size;
01091 }
01092 }
01093
01094
01095
01096
01101
01102 template <class Type>
01103 void Vector<Type>::set_size(int new_size, const Type& new_value)
01104 {
01105 set_size(new_size);
01106 initialize(new_value);
01107 }
01108
01109
01110
01111
01115
01116 template <class Type>
01117 void Vector<Type>::resize(int new_size)
01118 {
01119
01120
01121 #ifdef _DEBUG
01122
01123 if(new_size < 0)
01124 {
01125 std::cerr << "Flood Error: Vector Template." << std::endl
01126 << "void resize(int) method." << std::endl
01127 << "Size must be equal or greater than zero." << std::endl;
01128
01129 exit(1);
01130 }
01131
01132 #endif
01133
01134
01135
01136 if(new_size > size)
01137 {
01138 Vector<Type> copy(*this);
01139
01140 set(new_size);
01141
01142 insert(0, copy);
01143 }
01144 else if(new_size < size)
01145 {
01146 Vector<Type> copy = extract(0, new_size);
01147
01148 set(copy);
01149 }
01150 }
01151
01152
01153
01154
01157
01158 template <class Type>
01159 void Vector<Type>::initialize(const Type& value) const
01160 {
01161 for(int i = 0; i < size; i++)
01162 {
01163 data[i] = value;
01164 }
01165 }
01166
01167
01168
01169
01171
01172 template <class Type>
01173 void Vector<Type>::initialize_sequential(void) const
01174 {
01175 for(int i = 0; i < size; i++)
01176 {
01177 data[i] = i;
01178 }
01179 }
01180
01181
01182
01183
01185
01186 template <class Type>
01187 void Vector<Type>::initialize_uniform(void) const
01188 {
01189 for(int i = 0; i < size; i++)
01190 {
01191 data[i] = calculate_random_uniform(-1.0, 1.0);
01192 }
01193 }
01194
01195
01196
01197
01202
01203 template <class Type>
01204 void Vector<Type>::initialize_uniform(double minimum, double maximum) const
01205 {
01206
01207
01208 #ifdef _DEBUG
01209
01210 if(minimum > maximum)
01211 {
01212 std::cerr << "Flood Error: Vector Template." << std::endl
01213 << "void initialize_uniform(double, double) const method." << std::endl
01214 << "Minimum value must be less or equal than maximum value." << std::endl;
01215
01216 exit(1);
01217 }
01218
01219 #endif
01220
01221 for(int i = 0; i < size; i++)
01222 {
01223 data[i] = calculate_random_uniform(minimum, maximum);
01224 }
01225 }
01226
01227
01228
01229
01234
01235 template <class Type>
01236 void Vector<Type>::initialize_uniform(const Vector<double>& minimum, const Vector<double>& maximum) const
01237 {
01238
01239
01240 #ifdef _DEBUG
01241
01242 int minimum_size = minimum.get_size();
01243 int maximum_size = maximum.get_size();
01244
01245 if(minimum_size != size || maximum_size != size)
01246 {
01247 std::cerr << "Flood Error: Vector Template." << std::endl
01248 << "void initialize_uniform(const Vector<double>&, const Vector<double>&) const method."
01249 << std::endl
01250 << "Minimum and maximum sizes must be equal to vector size." << std::endl;
01251
01252 exit(1);
01253 }
01254
01255 if(minimum > maximum)
01256 {
01257 std::cerr << "Flood Error: Vector Template." << std::endl
01258 << "void initialize_uniform(const Vector<double>&, const Vector<double>&) const method."
01259 << std::endl
01260 << "Minimum values must be less or equal than their corresponding maximum values." << std::endl;
01261
01262 exit(1);
01263 }
01264
01265 #endif
01266
01267 for(int i = 0; i < size; i++)
01268 {
01269 data[i] = calculate_random_uniform(minimum[i], maximum[i]);
01270 }
01271 }
01272
01273
01274
01275
01278
01279 template <class Type>
01280 void Vector<Type>::initialize_normal(void) const
01281 {
01282 for(int i = 0; i < size; i++)
01283 {
01284 data[i] = calculate_random_normal(0.0, 1.0);
01285 }
01286 }
01287
01288
01289
01290
01295
01296 template <class Type>
01297 void Vector<Type>::initialize_normal(double mean, double standard_deviation) const
01298 {
01299
01300
01301 #ifdef _DEBUG
01302
01303 if(standard_deviation < 0.0)
01304 {
01305 std::cerr << "Flood Error: Vector Template." << std::endl
01306 << "void initialize_normal(double, double) const method." << std::endl
01307 << "Standard deviation must be equal or greater than zero." << std::endl;
01308
01309 exit(1);
01310 }
01311
01312 #endif
01313
01314 for(int i = 0; i < size; i++)
01315 {
01316 data[i] = calculate_random_normal(mean, standard_deviation);
01317 }
01318 }
01319
01320
01321
01322
01327
01328 template <class Type>
01329 void Vector<Type>::initialize_normal(const Vector<double>& mean, const Vector<double>& standard_deviation) const
01330 {
01331
01332
01333 #ifdef _DEBUG
01334
01335 int mean_size = mean.get_size();
01336 int standard_deviation_size = standard_deviation.get_size();
01337
01338 if(mean_size != size || standard_deviation_size != size)
01339 {
01340 std::cerr << "Flood Error: Vector Template." << std::endl
01341 << "void initialize_normal(const Vector<double>&, const Vector<double>&) const method."
01342 << std::endl
01343 << "Mean and standard deviation sizes must be equal to vector size." << std::endl;
01344
01345 exit(1);
01346 }
01347
01348 if(standard_deviation < 0.0)
01349 {
01350 std::cerr << "Flood Error: Vector Template." << std::endl
01351 << "void initialize_normal(const Vector<double>&, const Vector<double>&) const method."
01352 << std::endl
01353 << "Standard deviations must be equal or greater than zero." << std::endl;
01354 exit(1);
01355 }
01356
01357 #endif
01358
01359 for(int i = 0; i < size; i++)
01360 {
01361 data[i] = calculate_random_normal(mean[i], standard_deviation[i]);
01362 }
01363 }
01364
01365
01366
01367
01369
01370 template <class Type>
01371 Type Vector<Type>::calculate_minimum(void) const
01372 {
01373 Type minimum = data[0];
01374
01375 for(int i = 1; i < size; i++)
01376 {
01377 if(data[i] < minimum)
01378 {
01379 minimum = data[i];
01380 }
01381 }
01382
01383 return(minimum);
01384 }
01385
01386
01387
01388
01390
01391 template <class Type>
01392 Type Vector<Type>::calculate_maximum(void) const
01393 {
01394 Type maximum = data[0];
01395
01396 for(int i = 1; i < size; i++)
01397 {
01398 if(data[i] > maximum)
01399 {
01400 maximum = data[i];
01401 }
01402 }
01403
01404 return(maximum);
01405 }
01406
01407
01408
01409
01411
01412 template <class Type>
01413 Vector<Type> Vector<Type>::calculate_minimum_maximum(void) const
01414 {
01415 Type minimum = data[0];
01416 Type maximum = data[0];
01417
01418 for(int i = 1; i < size; i++)
01419 {
01420 if(data[i] < minimum)
01421 {
01422 minimum = data[i];
01423 }
01424
01425 if(data[i] > maximum)
01426 {
01427 maximum = data[i];
01428 }
01429 }
01430
01431 Vector<Type> minimum_maximum(2);
01432 minimum_maximum[0] = minimum;
01433 minimum_maximum[1] = maximum;
01434
01435 return(minimum_maximum);
01436 }
01437
01438
01439
01440
01442
01443 template <class Type>
01444 int Vector<Type>::calculate_minimal_index(void) const
01445 {
01446 Type minimum = data[0];
01447 int minimal_index = 0;
01448
01449 for(int i = 1; i < size; i++)
01450 {
01451 if(data[i] < minimum)
01452 {
01453 minimum = data[i];
01454 minimal_index = i;
01455 }
01456 }
01457
01458 return(minimal_index);
01459 }
01460
01461
01462
01463
01465
01466 template <class Type>
01467 int Vector<Type>::calculate_maximal_index(void) const
01468 {
01469 Type maximum = data[0];
01470 int maximal_index = 0;
01471
01472 for(int i = 1; i < size; i++)
01473 {
01474 if(data[i] > maximum)
01475 {
01476 maximum = data[i];
01477 maximal_index = i;
01478 }
01479 }
01480
01481 return(maximal_index);
01482 }
01483
01484
01485
01486
01488
01489 template <class Type>
01490 Vector<int> Vector<Type>::calculate_minimal_maximal_index(void) const
01491 {
01492 Type minimum = data[0];
01493 Type maximum = data[0];
01494
01495 int minimal_index = 0;
01496 int maximal_index = 0;
01497
01498 for(int i = 1; i < size; i++)
01499 {
01500 if(data[i] < minimum)
01501 {
01502 minimum = data[i];
01503 minimal_index = i;
01504 }
01505 if(data[i] > maximum)
01506 {
01507 maximum = data[i];
01508 maximal_index = i;
01509 }
01510 }
01511
01512 Vector<int> minimal_maximal_index(2);
01513 minimal_maximal_index[0] = minimal_index;
01514 minimal_maximal_index[1] = maximal_index;
01515
01516 return(minimal_maximal_index);
01517 }
01518
01519
01520
01521
01523
01524 template <class Type>
01525 Type Vector<Type>::calculate_sum(void) const
01526 {
01527 Type sum = 0;
01528
01529 for(int i = 0; i < size; i++)
01530 {
01531 sum += data[i];
01532 }
01533
01534 return(sum);
01535 }
01536
01537
01538
01539
01541
01542 template <class Type>
01543 Type Vector<Type>::calculate_product(void) const
01544 {
01545 Type product = 1;
01546
01547 for(int i = 0; i < size; i++)
01548 {
01549 product *= data[i];
01550 }
01551
01552 return(product);
01553 }
01554
01555
01556
01557
01559
01560 template <class Type>
01561 double Vector<Type>::calculate_mean(void) const
01562 {
01563
01564
01565 #ifdef _DEBUG
01566
01567 if(size == 0)
01568 {
01569 std::cerr << "Flood Error: Vector Template." << std::endl
01570 << "double calculate_mean(void)." << std::endl
01571 << "Size must be greater than zero." << std::endl;
01572
01573 exit(1);
01574 }
01575
01576 #endif
01577
01578 Type sum = calculate_sum();
01579
01580 double mean = sum/(double)size;
01581
01582 return(mean);
01583 }
01584
01585
01586
01587
01589
01590 template <class Type>
01591 double Vector<Type>::calculate_standard_deviation(void) const
01592 {
01593
01594
01595 #ifdef _DEBUG
01596
01597 if(size == 0)
01598 {
01599 std::cerr << "Flood Error: Vector Template." << std::endl
01600 << "double calculate_standard_deviation(void)." << std::endl
01601 << "Size must be greater than zero." << std::endl;
01602
01603 exit(1);
01604 }
01605
01606 #endif
01607
01608 double mean = calculate_mean();
01609
01610 double sum = 0.0;
01611
01612 for(int i = 0; i < size; i++)
01613 {
01614 sum += (data[i] - mean)*(data[i] - mean);
01615 }
01616
01617 double standard_deviation = sqrt(sum/(double)size);
01618
01619 return(standard_deviation);
01620 }
01621
01622
01623
01624
01626
01627 template <class Type>
01628 Vector<double> Vector<Type>::calculate_mean_standard_deviation(void) const
01629 {
01630
01631
01632 #ifdef _DEBUG
01633
01634 if(size == 0)
01635 {
01636 std::cerr << "Flood Error: Vector Template." << std::endl
01637 << "double calculate_mean_standard_deviation(void)." << std::endl
01638 << "Size must be greater than zero." << std::endl;
01639
01640 exit(1);
01641 }
01642
01643 #endif
01644
01645 double mean = calculate_mean();
01646
01647 double sum = 0.0;
01648
01649 for(int i = 0; i < size; i++)
01650 {
01651 sum += (data[i] - mean)*(data[i] - mean);
01652 }
01653
01654 double standard_deviation = sqrt(sum/(double)size);
01655
01656 Vector<double> mean_standard_deviation(2);
01657 mean_standard_deviation[0] = mean;
01658 mean_standard_deviation[1] = standard_deviation;
01659
01660 return(mean_standard_deviation);
01661 }
01662
01663
01664
01665
01667
01668 template <class Type>
01669 double Vector<Type>::calculate_norm(void) const
01670 {
01671
01672
01673 double norm = 0.0;
01674
01675 for(int i = 0; i < size; i++)
01676 {
01677 norm += data[i]*data[i];
01678 }
01679
01680 norm = sqrt(norm);
01681
01682 return(norm);
01683 }
01684
01685
01686
01687
01690
01691 template <class Type>
01692 Vector<Type> Vector<Type>::calculate_lower_bounded(const Type& lower_bound) const
01693 {
01694 Vector<Type> bounded_vector(size);
01695
01696 for(int i = 0; i < size; i++)
01697 {
01698 if(data[i] < lower_bound)
01699 {
01700 data[i] = lower_bound;
01701 }
01702 }
01703
01704 return(bounded_vector);
01705 }
01706
01707
01708
01709
01712
01713 template <class Type>
01714 Vector<Type> Vector<Type>::calculate_lower_bounded(const Vector<Type>& lower_bound) const
01715 {
01716
01717
01718 #ifdef _DEBUG
01719
01720 int lower_bound_size = lower_bound.get_size();
01721
01722 if(lower_bound_size != size)
01723 {
01724 std::cerr << "Flood Error: Vector Template." << std::endl
01725 << "Vector<Type> calculate_lower_bounded(const Vector<Type>&) const method." << std::endl
01726 << "Lower bound size must be equal to vector size." << std::endl;
01727
01728 exit(1);
01729 }
01730
01731 #endif
01732
01733 Vector<Type> bounded_vector(size);
01734
01735
01736
01737 for(int i = 0; i < size; i++)
01738 {
01739 if(data[i] < lower_bound[i])
01740 {
01741 bounded_vector[i] = lower_bound[i];
01742 }
01743 else
01744 {
01745 bounded_vector[i] = data[i];
01746 }
01747 }
01748
01749 return(bounded_vector);
01750 }
01751
01752
01753
01754
01757
01758 template <class Type>
01759 Vector<Type> Vector<Type>::calculate_upper_bounded(const Type& upper_bound) const
01760 {
01761 Vector<Type> bounded_vector(size);
01762
01763 for(int i = 0; i < size; i++)
01764 {
01765 if(data[i] > upper_bound)
01766 {
01767 bounded_vector[i] = upper_bound;
01768 }
01769 else
01770 {
01771 bounded_vector[i] = data[i];
01772 }
01773 }
01774
01775 return(bounded_vector);
01776 }
01777
01778
01779
01780
01783
01784 template <class Type>
01785 Vector<Type> Vector<Type>::calculate_upper_bounded(const Vector<Type>& upper_bound) const
01786 {
01787
01788
01789 #ifdef _DEBUG
01790
01791 int upper_bound_size = upper_bound.get_size();
01792
01793 if(upper_bound_size != size)
01794 {
01795 std::cerr << "Flood Error: Vector Template." << std::endl
01796 << "Vector<Type> calculate_upper_bounded(const Vector<Type>&) const method." << std::endl
01797 << "Upper bound size must be equal to vector size." << std::endl;
01798
01799 exit(1);
01800 }
01801
01802 #endif
01803
01804 Vector<Type> bounded_vector(size);
01805
01806
01807
01808 for(int i = 0; i < size; i++)
01809 {
01810 if(data[i] > upper_bound[i])
01811 {
01812 bounded_vector[i] = upper_bound[i];
01813 }
01814 else
01815 {
01816 bounded_vector[i] = data[i];
01817 }
01818 }
01819
01820 return(bounded_vector);
01821 }
01822
01823
01824
01825
01830
01831 template <class Type>
01832 Vector<Type> Vector<Type>::calculate_lower_upper_bounded(const Type& lower_bound, const Type& upper_bound) const
01833 {
01834 Vector<Type> bounded_vector(size);
01835
01836 for(int i = 0; i < size; i++)
01837 {
01838 if(data[i] < lower_bound)
01839 {
01840 bounded_vector[i] = lower_bound;
01841 }
01842 else if(data[i] > upper_bound)
01843 {
01844 bounded_vector[i] = upper_bound;
01845 }
01846 else
01847 {
01848 bounded_vector[i] = data[i];
01849 }
01850 }
01851
01852 return(bounded_vector);
01853 }
01854
01855
01856
01857
01862
01863 template <class Type>
01864 Vector<Type> Vector<Type>::calculate_lower_upper_bounded(const Vector<Type>& lower_bound, const Vector<Type>& upper_bound)
01865 const
01866 {
01867
01868
01869 #ifdef _DEBUG
01870
01871 int lower_bound_size = lower_bound.get_size();
01872 int upper_bound_size = upper_bound.get_size();
01873
01874 if(lower_bound_size != size || upper_bound_size != size)
01875 {
01876 std::cerr << "Flood Error: Vector Template." << std::endl
01877 << "Vector<Type> calculate_lower_upper_bounded(const Vector<Type>&, const Vector<Type>&) const method." << std::endl
01878 << "Lower and upper bound sizes must be equal to vector size." << std::endl;
01879
01880 exit(1);
01881 }
01882
01883 #endif
01884
01885 Vector<Type> bounded_vector(size);
01886
01887
01888
01889 for(int i = 0; i < size; i++)
01890 {
01891 if(data[i] < lower_bound[i])
01892 {
01893 bounded_vector[i] = lower_bound[i];
01894 }
01895 else if(data[i] > upper_bound[i])
01896 {
01897 bounded_vector[i] = upper_bound[i];
01898 }
01899 else
01900 {
01901 bounded_vector[i] = data[i];
01902 }
01903 }
01904
01905 return(bounded_vector);
01906 }
01907
01908
01909
01910
01911 template <class Type>
01912 void Vector<Type>::apply_lower_bound(const Type& lower_bound)
01913 {
01914 for(int i = 0; i < size; i++)
01915 {
01916 if(data[i] < lower_bound)
01917 {
01918 data[i] = lower_bound;
01919 }
01920 }
01921 }
01922
01923
01924
01925
01926 template <class Type>
01927 void Vector<Type>::apply_lower_bound(const Vector<Type>& lower_bound)
01928 {
01929 for(int i = 0; i < size; i++)
01930 {
01931 if(data[i] < lower_bound[i])
01932 {
01933 data[i] = lower_bound[i];
01934 }
01935 }
01936 }
01937
01938
01939
01940
01941 template <class Type>
01942 void Vector<Type>::apply_upper_bound(const Type& upper_bound)
01943 {
01944 for(int i = 0; i < size; i++)
01945 {
01946 if(data[i] > upper_bound)
01947 {
01948 data[i] = upper_bound;
01949 }
01950 }
01951 }
01952
01953
01954
01955
01956 template <class Type>
01957 void Vector<Type>::apply_upper_bound(const Vector<Type>& upper_bound)
01958 {
01959 for(int i = 0; i < size; i++)
01960 {
01961 if(data[i] > upper_bound[i])
01962 {
01963 data[i] = upper_bound[i];
01964 }
01965 }
01966 }
01967
01968
01969
01970
01971 template <class Type>
01972 void Vector<Type>::apply_lower_upper_bounds(const Type& lower_bound, const Type& upper_bound)
01973 {
01974 for(int i = 0; i < size; i++)
01975 {
01976 if(data[i] < lower_bound)
01977 {
01978 data[i] = lower_bound;
01979 }
01980 else if(data[i] > upper_bound)
01981 {
01982 data[i] = upper_bound;
01983 }
01984 }
01985 }
01986
01987
01988
01989
01990 template <class Type>
01991 void Vector<Type>::apply_lower_upper_bounds(const Vector<Type>& lower_bound, const Vector<Type>& upper_bound)
01992 {
01993 for(int i = 0; i < size; i++)
01994 {
01995 if(data[i] < lower_bound[i])
01996 {
01997 data[i] = lower_bound[i];
01998 }
01999 else if(data[i] > upper_bound[i])
02000 {
02001 data[i] = upper_bound[i];
02002 }
02003 }
02004 }
02005
02006
02007
02008
02011
02012 template <class Type>
02013 Vector<Type> Vector<Type>::operator + (const Type& scalar) const
02014 {
02015 Vector<Type> sum(size);
02016
02017 for(int i = 0; i < size; i++)
02018 {
02019 sum[i] = data[i] + scalar;
02020 }
02021
02022 return(sum);
02023 }
02024
02025
02026
02027
02030
02031 template <class Type>
02032 Vector<Type> Vector<Type>::operator + (const Vector<Type>& other_vector) const
02033 {
02034
02035
02036 #ifdef _DEBUG
02037
02038 int other_size = other_vector.get_size();
02039
02040 if(other_size != size)
02041 {
02042 std::cerr << "Flood Error: Vector Template. " << std::endl
02043 << "Vector<Type> operator + (const Vector<Type>) const." << std::endl
02044 << "Size of vectors is " << size << " and " << other_size << " and they must be the same."
02045 << std::endl;
02046
02047 exit(1);
02048 }
02049
02050 #endif
02051
02052 Vector<Type> sum(size);
02053
02054 for(int i = 0; i < size; i++)
02055 {
02056 sum[i] = data[i] + other_vector[i];
02057 }
02058
02059 return(sum);
02060 }
02061
02062
02063
02064
02067
02068 template <class Type>
02069 Vector<Type> Vector<Type>::operator - (const Type& scalar) const
02070 {
02071 Vector<Type> difference(size);
02072
02073 for(int i = 0; i < size; i++)
02074 {
02075 difference[i] = data[i] - scalar;
02076 }
02077
02078 return(difference);
02079 }
02080
02081
02082
02083
02086
02087 template <class Type>
02088 Vector<Type> Vector<Type>::operator - (const Vector<Type>& other_vector) const
02089 {
02090
02091
02092 #ifdef _DEBUG
02093
02094 int other_size = other_vector.get_size();
02095
02096 if(other_size != size)
02097 {
02098 std::cerr << "Flood Error: Vector Template." << std::endl
02099 << "Vector<Type> operator - (const Vector<Type>&) const." << std::endl
02100 << "Size of vectors is " << size << " and " << other_size << " and they must be the same."
02101 << std::endl;
02102
02103 exit(1);
02104 }
02105
02106 #endif
02107
02108 Vector<Type> difference(size);
02109
02110 for(int i = 0; i < size; i++)
02111 {
02112 difference[i] = data[i] - other_vector[i];
02113 }
02114
02115 return(difference);
02116 }
02117
02118
02119
02120
02123
02124 template <class Type>
02125 Vector<Type> Vector<Type>::operator * (const Type& scalar) const
02126 {
02127 Vector<Type> product(size);
02128
02129 for(int i = 0; i < size; i++)
02130 {
02131 product[i] = data[i]*scalar;
02132 }
02133
02134 return(product);
02135 }
02136
02137
02138
02139
02142
02143 template <class Type>
02144 Vector<Type> Vector<Type>::operator * (const Vector<Type>& other_vector) const
02145 {
02146
02147
02148 #ifdef _DEBUG
02149
02150 int other_size = other_vector.get_size();
02151
02152 if(other_size != size)
02153 {
02154 std::cerr << "Flood Error: Vector Template." << std::endl
02155 << "Vector<Type> operator * (const Vector<Type>&) const." << std::endl
02156 << "Size of other vector (" << other_size << ") must be equal to size of this vector (" << size << ")."
02157 << std::endl;
02158
02159 exit(1);
02160 }
02161
02162 #endif
02163
02164 Vector<Type> product(size);
02165
02166 for(int i = 0; i < size; i++)
02167 {
02168 product[i] = data[i]*other_vector[i];
02169 }
02170
02171 return(product);
02172 }
02173
02174
02175
02176
02180
02181 template <class Type>
02182 Vector<Type> Vector<Type>::dot(const Matrix<Type>& matrix) const
02183 {
02184 int rows_number = matrix.get_rows_number();
02185
02186
02187
02188 #ifdef _DEBUG
02189
02190 if(rows_number != size)
02191 {
02192 std::cerr << "Flood Error: Vector Template." << std::endl
02193 << "Vector<Type> dot(const Matrix<Type>&) const method." << std::endl
02194 << "Matrix number of rows must be equal to vector size." << std::endl;
02195
02196 exit(1);
02197 }
02198
02199 #endif
02200
02201 int columns_number = matrix.get_columns_number();
02202
02203 Vector<Type> product(columns_number);
02204
02205 for(int j = 0; j < columns_number; j++)
02206 {
02207 product[j] = 0;
02208
02209 for(int i = 0; i < rows_number; i++)
02210 {
02211 product[j] += data[i]*matrix[i][j];
02212 }
02213 }
02214
02215 return(product);
02216 }
02217
02218
02219
02220
02223
02224 template <class Type>
02225 Type Vector<Type>::dot(const Vector<Type>& other_vector) const
02226 {
02227
02228
02229 #ifdef _DEBUG
02230
02231 int other_size = other_vector.get_size();
02232
02233 if(other_size != size)
02234 {
02235 std::cerr << "Flood Error: Vector Template." << std::endl
02236 << "Type dot(const Vector<Type>&) const method." << std::endl
02237 << "Both vector sizes must be the same." << std::endl;
02238
02239 exit(1);
02240 }
02241
02242 #endif
02243
02244 Type dot_product = 0;
02245
02246 for(int i = 0; i < size; i++)
02247 {
02248 dot_product += data[i]*other_vector[i];
02249 }
02250
02251 return(dot_product);
02252 }
02253
02254
02255
02256
02259
02260 template <class Type>
02261 Matrix<Type> Vector<Type>::outer(const Vector<Type>& other_vector) const
02262 {
02263 int other_size = other_vector.get_size();
02264
02265
02266
02267 #ifdef _DEBUG
02268
02269 if(other_size != size)
02270 {
02271 std::cerr << "Flood Error: Vector Template." << std::endl
02272 << "Matrix<Type> outer(const Vector<Type>&) const method." << std::endl
02273 << "Both vector sizes must be the same." << std::endl;
02274
02275 exit(1);
02276 }
02277
02278 #endif
02279
02280 int rows_number = size;
02281 int columns_number = other_size;
02282
02283 Matrix<Type> outer(rows_number, columns_number);
02284
02285 for(int i = 0; i < rows_number; i++)
02286 {
02287 for(int j = 0; j < columns_number; j++)
02288 {
02289 outer[i][j] = data[i]*other_vector[j];
02290 }
02291 }
02292
02293 return(outer);
02294 }
02295
02296
02297
02298
02301
02302 template <class Type>
02303 Vector<Type> Vector<Type>::operator / (const Type& scalar) const
02304 {
02305 Vector<Type> cocient(size);
02306
02307 for(int i = 0; i < size; i++)
02308 {
02309 cocient[i] = data[i]/scalar;
02310 }
02311
02312 return(cocient);
02313 }
02314
02315
02316
02317
02320
02321 template <class Type>
02322 Vector<Type> Vector<Type>::operator / (const Vector<Type>& other_vector) const
02323 {
02324
02325
02326 #ifdef _DEBUG
02327
02328 int other_size = other_vector.get_size();
02329
02330 if(other_size != size)
02331 {
02332 std::cerr << "Flood Error: Vector Template." << std::endl
02333 << "Vector<Type> operator / (const Vector<Type>&) const." << std::endl
02334 << "Both vector sizes must be the same." << std::endl;
02335
02336 exit(1);
02337 }
02338
02339 #endif
02340
02341 Vector<Type> cocient(size);
02342
02343 for(int i = 0; i < size; i++)
02344 {
02345 cocient[i] = data[i]/other_vector[i];
02346 }
02347
02348 return(cocient);
02349 }
02350
02351
02352
02353
02356
02357 template <class Type>
02358 void Vector<Type>::operator += (const Type& value) const
02359 {
02360 for(int i = 0; i < size; i++)
02361 {
02362 data[i] += value;
02363 }
02364 }
02365
02366
02367
02368
02371
02372 template <class Type>
02373 void Vector<Type>::operator += (const Vector<Type>& other_vector) const
02374 {
02375
02376
02377 #ifdef _DEBUG
02378
02379 int other_size = other_vector.get_size();
02380
02381 if(other_size != size)
02382 {
02383 std::cerr << "Flood Error: Vector Template." << std::endl
02384 << "void operator += (const Vector<Type>&) const." << std::endl
02385 << "Both vector sizes must be the same." << std::endl;
02386
02387 exit(1);
02388 }
02389
02390 #endif
02391
02392 for(int i = 0; i < size; i++)
02393 {
02394 data[i] += other_vector[i];
02395 }
02396 }
02397
02398
02399
02400
02403
02404 template <class Type>
02405 void Vector<Type>::operator -= (const Type& value) const
02406 {
02407 for(int i = 0; i < size; i++)
02408 {
02409 data[i] -= value;
02410 }
02411 }
02412
02413
02414
02415
02418
02419 template <class Type>
02420 void Vector<Type>::operator -= (const Vector<Type>& other_vector) const
02421 {
02422
02423
02424 #ifdef _DEBUG
02425
02426 int other_size = other_vector.get_size();
02427
02428 if(other_size != size)
02429 {
02430 std::cerr << "Flood Error: Vector Template." << std::endl
02431 << "void operator -= (const Vector<Type>&) const." << std::endl
02432 << "Both vector sizes must be the same." << std::endl;
02433
02434 exit(1);
02435 }
02436
02437 #endif
02438
02439 for(int i = 0; i < size; i++)
02440 {
02441 data[i] -= other_vector[i];
02442 }
02443 }
02444
02445
02446
02447
02450
02451 template <class Type>
02452 void Vector<Type>::operator *= (const Type& value) const
02453 {
02454 for(int i = 0; i < size; i++)
02455 {
02456 data[i] *= value;
02457 }
02458 }
02459
02460
02461
02462
02465
02466 template <class Type>
02467 void Vector<Type>::operator *= (const Vector<Type>& other_vector) const
02468 {
02469
02470
02471 #ifdef _DEBUG
02472
02473 int other_size = other_vector.get_size();
02474
02475 if(other_size != size)
02476 {
02477 std::cerr << "Flood Error: Vector Template." << std::endl
02478 << "void operator *= (const Vector<Type>&) const." << std::endl
02479 << "Both vector sizes must be the same." << std::endl;
02480
02481 exit(1);
02482 }
02483
02484 #endif
02485
02486 for(int i = 0; i < size; i++)
02487 {
02488 data[i] *= other_vector[i];
02489 }
02490 }
02491
02492
02493
02494
02497
02498 template <class Type>
02499 void Vector<Type>::operator /= (const Type& value) const
02500 {
02501 for(int i = 0; i < size; i++)
02502 {
02503 data[i] /= value;
02504 }
02505 }
02506
02507
02508
02509
02512
02513 template <class Type>
02514 void Vector<Type>::operator /= (const Vector<Type>& other_vector) const
02515 {
02516
02517
02518 #ifdef _DEBUG
02519
02520 int other_size = other_vector.get_size();
02521
02522 if(other_size != size)
02523 {
02524 std::cerr << "Flood Error: Vector Template." << std::endl
02525 << "void operator /= (const Vector<Type>&) const." << std::endl
02526 << "Both vector sizes must be the same." << std::endl;
02527
02528 exit(1);
02529 }
02530
02531 #endif
02532
02533 for(int i = 0; i < size; i++)
02534 {
02535 data[i] /= other_vector[i];
02536 }
02537 }
02538
02539
02540
02541
02543
02544 template <class Type>
02545 Type* Vector<Type>::begin(void) const
02546 {
02547 return(data);
02548 }
02549
02550
02551
02552
02554
02555 template <class Type>
02556 Type* Vector<Type>::end(void) const
02557 {
02558 return(data + size);
02559 }
02560
02561
02562
02563
02567
02568 template<typename Type>
02569 std::istream& operator >> (std::istream& is, Vector<Type>& v)
02570 {
02571 int size = v.get_size();
02572
02573 for(int i = 0; i < size; i++)
02574 {
02575 is >> v[i];
02576 }
02577
02578 return(is);
02579 }
02580
02581
02582
02583
02587
02588 template<typename Type>
02589 std::ostream& operator << (std::ostream& os, Vector<Type>& v)
02590 {
02591 int size = v.get_size();
02592
02593 for(int i = 0; i < size; i++)
02594 {
02595 os << v[i] << " ";
02596 }
02597
02598 return(os);
02599 }
02600
02601
02602
02603
02607
02608 template <class Type>
02609 std::string Vector<Type>::to_XML(bool show_declaration)
02610 {
02611 std::stringstream buffer;
02612
02613
02614
02615 if(show_declaration)
02616 {
02617 buffer << "<Flood version='3.0' class='Vector'>" << std::endl;
02618 }
02619
02620
02621
02622 buffer << "<Size>" << std::endl
02623 << size << std::endl
02624 << "</Size>" << std::endl;
02625
02626
02627
02628 buffer << "<Display>" << std::endl
02629 << display << std::endl
02630 << "</Display>" << std::endl;
02631
02632
02633
02634 buffer << "<Data>" << std::endl;
02635
02636 for(int i = 0; i < size; i++)
02637 {
02638 buffer << data[i] << " ";
02639 }
02640
02641 buffer << std::endl;
02642
02643 buffer << "</Data>" << std::endl;
02644
02645 return(buffer.str());
02646 }
02647
02648
02649
02650
02654
02655 template <class Type>
02656 void Vector<Type>::load(const char* filename)
02657 {
02658
02659
02660 std::ifstream file(filename);
02661
02662 if(!file.is_open())
02663 {
02664 std::cerr << "Flood Error: Vector template." << std::endl
02665 << "void load(const char*) method." << std::endl
02666 << "Cannot open vector XML-type file." << std::endl;
02667
02668 exit(1);
02669 }
02670
02671 std::string line;
02672 std::string word;
02673
02674
02675
02676 getline(file, line);
02677
02678 if(line != "<Flood version='3.0' class='Vector'>")
02679 {
02680
02681
02682
02683
02684
02685 }
02686
02687
02688
02689 file >> word;
02690
02691 if(word != "<Size>")
02692 {
02693 std::cerr << "Flood Error: Vector template." << std::endl
02694 << "void load(const char*) method." << std::endl
02695 << "Unknown size begin tag: " << line << std::endl;
02696
02697 exit(1);
02698 }
02699
02700 int new_size;
02701
02702 file >> new_size;
02703
02704 set_size(new_size);
02705
02706 file >> word;
02707
02708 if(word != "</Size>")
02709 {
02710 std::cerr << "Flood Error: Vector template." << std::endl
02711 << "void load(const char*) method." << std::endl
02712 << "Unknown size end tag: " << line << std::endl;
02713
02714 exit(1);
02715 }
02716
02717 while(!file.eof())
02718 {
02719 file >> word;
02720
02721 if(word == "<Display>")
02722 {
02723 bool new_display;
02724
02725 file >> new_display;
02726
02727 file >> word;
02728
02729 if(word != "</Display>")
02730 {
02731 std::cerr << "Flood Error: Vector template." << std::endl
02732 << "void load(const char*) method." << std::endl
02733 << "Unknown display end tag: " << word << std::endl;
02734
02735 exit(1);
02736 }
02737 }
02738 else if(word == "<Data>")
02739 {
02740 for(int i = 0; i < size; i++)
02741 {
02742 file >> data[i];
02743 }
02744
02745 file >> word;
02746
02747 if(word != "</Data>")
02748 {
02749 std::cerr << "Flood Error: Vector template." << std::endl
02750 << "void load(const char*) method." << std::endl
02751 << "Unknown data end tag: " << word << std::endl;
02752
02753 exit(1);
02754 }
02755 }
02756 else
02757 {
02758
02759
02760
02761
02762
02763 }
02764 }
02765
02766
02767
02768 file.close();
02769 }
02770
02771
02772
02773
02778
02779 template <class Type>
02780 void Vector<Type>::load_data(const char* filename)
02781 {
02782
02783
02784 std::ifstream file(filename);
02785
02786 if(!file.is_open())
02787 {
02788 std::cerr << "Flood Error: Vector template." << std::endl
02789 << "void load_data(const char*) method." << std::endl
02790 << "Cannot open vector data file." << std::endl;
02791
02792 exit(1);
02793 }
02794
02795
02796
02797 for(int i = 0; i < size; i++)
02798 {
02799 file >> data[i];
02800 }
02801
02802
02803
02804 file.close();
02805 }
02806
02807
02808
02809
02814
02815 template <class Type>
02816 void Vector<Type>::save(const char* filename)
02817 {
02818 std::fstream file;
02819
02820
02821
02822 file.open(filename, std::ios::out);
02823
02824 if(!file.is_open())
02825 {
02826 std::cerr << "Flood Error: Vector template." << std::endl
02827 << "void save(const char*) method." << std::endl
02828 << "Cannot open vector file." << std::endl;
02829
02830 exit(1);
02831 }
02832
02833
02834
02835 file << to_XML(true) << std::endl;
02836
02837
02838
02839 file.close();
02840 }
02841
02842
02843
02844
02849
02850 template <class Type>
02851 void Vector<Type>::save_data(const char* filename)
02852 {
02853 std::fstream file;
02854
02855
02856
02857 file.open(filename, std::ios::out);
02858
02859 if(!file.is_open())
02860 {
02861 std::cerr << "Flood Error: Vector template." << std::endl
02862 << "void save_data(const char*) method." << std::endl
02863 << "Cannot open vector data file." << std::endl;
02864
02865 exit(1);
02866 }
02867
02868
02869
02870 for(int i = 0; i < size; i++)
02871 {
02872 file << data[i] << " ";
02873
02874 }
02875
02876 file << std::endl;
02877
02878
02879
02880 file.close();
02881 }
02882
02883
02884
02885
02889
02890 template <class Type>
02891 void Vector<Type>::insert(int position, const Vector<Type>& other_vector) const
02892 {
02893 int other_size = other_vector.get_size();
02894
02895
02896
02897 #ifdef _DEBUG
02898
02899 if(position + other_size > size)
02900 {
02901 std::cerr << "Flood Error: Vector Template." << std::endl
02902 << "void insert(int, const Vector<Type>&) const method." << std::endl
02903 << "Cannot insert vector." << std::endl;
02904
02905 exit(1);
02906 }
02907
02908 #endif
02909
02910 for(int i = 0; i < other_size; i++)
02911 {
02912 data[position + i] = other_vector[i];
02913 }
02914 }
02915
02916
02917
02918
02922
02923 template <class Type>
02924 Vector<Type> Vector<Type>::extract(int position, int other_size) const
02925 {
02926
02927
02928 #ifdef _DEBUG
02929
02930 if(position + other_size > size)
02931 {
02932 std::cerr << "Flood Error: Vector Template." << std::endl
02933 << "Vector<Type> extract(int, int) method." << std::endl
02934 << "Cannot extract vector." << std::endl;
02935
02936 exit(1);
02937 }
02938
02939 #endif
02940
02941 Vector<Type> other_vector(other_size);
02942
02943 for(int i = 0; i < other_size; i++)
02944 {
02945 other_vector[i] = data[position + i];
02946 }
02947
02948 return(other_vector);
02949 }
02950
02951
02952
02953
02956
02957 template <class Type>
02958 Vector<Type> Vector<Type>::assemble(const Vector<Type>& other_vector) const
02959 {
02960 int other_size = other_vector.get_size();
02961
02962 if(size == 0 && other_size == 0)
02963 {
02964 Vector<Type> assembly;
02965
02966 return(assembly);
02967 }
02968 else if(size == 0)
02969 {
02970 return(other_vector);
02971 }
02972 else if(other_size == 0)
02973 {
02974 return(*this);
02975 }
02976 else
02977 {
02978 int other_size = other_vector.get_size();
02979
02980 Vector<Type> assembly(size + other_size);
02981
02982 for(int i = 0; i < size; i++)
02983 {
02984 assembly[i] = data[i];
02985 }
02986
02987 for(int i = 0; i < other_size; i++)
02988 {
02989 assembly[size+i] = other_vector[i];
02990 }
02991
02992 return(assembly);
02993 }
02994 }
02995
02996
02997
02998
03002
03003 template <class Type>
03004 double Vector<Type>::calculate_random_uniform(double minimum, double maximum) const
03005 {
03006 double random = (double)rand()/(RAND_MAX+1.0);
03007
03008 double random_uniform = minimum + (maximum-minimum)*random;
03009
03010 return(random_uniform);
03011 }
03012
03013
03014
03015
03019
03020 template <class Type>
03021 double Vector<Type>::calculate_random_normal(double mean, double standard_deviation) const
03022 {
03023 double random_normal = 0.0;
03024
03025 const double pi = 4.0*atan(1.0);
03026
03027 double random_uniform_1;
03028 double random_uniform_2;
03029
03030 do
03031 {
03032 random_uniform_1 = (double)rand()/(RAND_MAX+1.0);
03033
03034 }while(random_uniform_1 == 0.0);
03035
03036 random_uniform_2 = (double)rand()/(RAND_MAX+1.0);
03037
03038
03039
03040 random_normal = mean + sqrt(-2.0*log(random_uniform_1))*sin(2.0*pi*random_uniform_2)*standard_deviation;
03041
03042 return(random_normal);
03043 }
03044
03045 }
03046
03047 #endif
03048
03049
03050
03051
03052
03053
03054
03055
03056
03057
03058
03059
03060
03061
03062
03063
03064
03065