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 <algorithm>
00022 #include <climits>
00023 #include <cmath>
00024 #include <cstdlib>
00025
00026 #include <fstream>
00027 #include <iostream>
00028 #include <iterator>
00029 #include <istream>
00030 #include <ostream>
00031 #include <sstream>
00032 #include <stdexcept>
00033 #include <string>
00034 #include <vector>
00035
00036 namespace OpenNN
00037 {
00038
00040
00041 template<class Type> class Matrix;
00042
00045
00046 template <class Type>
00047 class Vector : public std::vector<Type>
00048 {
00049
00050 public:
00051
00052
00053
00055
00056 explicit Vector(void) : std::vector<Type>()
00057 {
00058 }
00059
00060
00063
00064 explicit Vector(const unsigned int& new_size) : std::vector<Type>(new_size)
00065 {
00066 }
00067
00068
00073
00074 explicit Vector(const unsigned int& new_size, const Type& value) : std::vector<Type>(new_size, value)
00075 {
00076 }
00077
00078
00081
00082 explicit Vector(const std::string& filename) : std::vector<Type>()
00083 {
00084 load(filename);
00085 }
00086
00087
00089
00090 explicit Vector(const Type& first, const int& step, const Type& last) : std::vector<Type>()
00091 {
00092 if(first > last && step > 0 )
00093 {
00094 }
00095 else if(first < last && step < 0)
00096 {
00097 }
00098 else
00099 {
00100 const unsigned int new_size = 1 + (unsigned int)((last - first)/step + 0.5);
00101
00102 this->resize(new_size);
00103
00104 for(unsigned int i = 0; i < new_size; i++)
00105 {
00106 (*this)[i] = first + i*step;
00107 }
00108 }
00109 }
00110
00111
00113
00114 template<class InputIterator>
00115 Vector(InputIterator first, InputIterator last) : std::vector<Type>(first, last)
00116 {
00117 }
00118
00119
00122
00123 Vector(const Vector<Type>& other_vector) : std::vector<Type>(other_vector)
00124 {
00125 }
00126
00127
00128
00129
00131
00132 ~Vector(void)
00133 {
00134 }
00135
00136
00137
00138
00142
00143 inline bool operator == (const Type& value) const
00144 {
00145 const unsigned int this_size = this->size();
00146
00147 for(unsigned int i = 0; i < this_size; i++)
00148 {
00149 if((*this)[i] != value)
00150 {
00151 return(false);
00152 }
00153 }
00154
00155 return(true);
00156 }
00157
00158
00159
00160
00164
00165 inline bool operator != (const Type& value) const
00166 {
00167 const unsigned int this_size = this->size();
00168
00169 for(unsigned int i = 0; i < this_size; i++)
00170 {
00171 if((*this)[i] != value)
00172 {
00173 return(true);
00174 }
00175 }
00176
00177 return(false);
00178 }
00179
00180
00181
00182
00186
00187 inline bool operator > (const Type& value) const
00188 {
00189 const unsigned int this_size = this->size();
00190
00191 for(unsigned int i = 0; i < this_size; i++)
00192 {
00193 if((*this)[i] <= value)
00194 {
00195 return(false);
00196 }
00197 }
00198
00199 return(true);
00200 }
00201
00202
00203
00204
00208
00209 inline bool operator < (const Type& value) const
00210 {
00211 const unsigned int this_size = this->size();
00212
00213 for(unsigned int i = 0; i < this_size; i++)
00214 {
00215 if((*this)[i] >= value)
00216 {
00217 return(false);
00218 }
00219 }
00220
00221 return(true);
00222 }
00223
00224
00225
00226
00230
00231 inline bool operator >= (const Type& value) const
00232 {
00233 const unsigned int this_size = this->size();
00234
00235 for(unsigned int i = 0; i < this_size; i++)
00236 {
00237 if((*this)[i] < value)
00238 {
00239 return(false);
00240 }
00241 }
00242
00243 return(true);
00244 }
00245
00246
00247
00248
00252
00253 inline bool operator <= (const Type& value) const
00254 {
00255 const unsigned int this_size = this->size();
00256
00257 for(unsigned int i = 0; i < this_size; i++)
00258 {
00259 if((*this)[i] > value)
00260 {
00261 return(false);
00262 }
00263 }
00264
00265 return(true);
00266 }
00267
00268
00269
00270
00271
00272
00274
00275 void set(void)
00276 {
00277 this->resize(0);
00278 }
00279
00280
00281
00282
00285
00286 void set(const unsigned int& new_size)
00287 {
00288 this->resize(new_size);
00289 }
00290
00291
00292
00293
00297
00298 void set(const unsigned int& new_size, const Type& new_value)
00299 {
00300 this->resize(new_size);
00301
00302 initialize(new_value);
00303 }
00304
00305
00306
00307
00311
00312 void set(const std::string& filename)
00313 {
00314 load(filename);
00315 }
00316
00317
00318
00319
00325
00326 void set(const Type& first, const int& step, const Type& last)
00327 {
00328 if(first > last && step > 0 )
00329 {
00330 this->resize(0);
00331 }
00332 else if(first < last && step < 0)
00333 {
00334 this->resize(0);
00335 }
00336 else
00337 {
00338 const unsigned int new_size = 1 + (unsigned int)((last - first)/step + 0.5);
00339
00340 this->resize(new_size);
00341
00342 for(unsigned int i = 0; i < new_size; i++)
00343 {
00344 (*this)[i] = first + i*step;
00345 }
00346 }
00347 }
00348
00349
00350
00351
00357
00358 void set(const Type& first, const double& step, const Type& last)
00359 {
00360 if(first > last && step > 0 )
00361 {
00362 this->resize(0);
00363 }
00364 else if(first < last && step < 0)
00365 {
00366 this->resize(0);
00367 }
00368 else
00369 {
00370 const unsigned int new_size = 1 + (unsigned int)((last - first)/step + 0.5);
00371
00372 this->resize(new_size);
00373
00374 for(unsigned int i = 0; i < new_size; i++)
00375 {
00376 (*this)[i] = first + i*step;
00377 }
00378 }
00379 }
00380
00381
00382
00383
00386
00387 void set(const Vector& other_vector)
00388 {
00389 *this = other_vector;
00390 }
00391
00392
00393
00394
00397
00398 void initialize(const Type& value)
00399 {
00400 const unsigned int this_size = this->size();
00401
00402 for(unsigned int i = 0; i < this_size; i++)
00403 {
00404 (*this)[i] = value;
00405 }
00406 }
00407
00408
00409
00410
00412
00413 void initialize_sequential(void)
00414 {
00415 const unsigned int this_size = this->size();
00416
00417 for(unsigned int i = 0; i < this_size; i++)
00418 {
00419 (*this)[i] = i;
00420 }
00421 }
00422
00423
00424
00425
00427
00428 void initialize_uniform(void)
00429 {
00430 const unsigned int this_size = this->size();
00431
00432 for(unsigned int i = 0; i < this_size; i++)
00433 {
00434 (*this)[i] = calculate_random_uniform(-1.0, 1.0);
00435 }
00436 }
00437
00438
00439
00440
00445
00446 void initialize_uniform(const double& minimum, double maximum)
00447 {
00448
00449
00450 #ifdef _DEBUG
00451
00452 if(minimum > maximum)
00453 {
00454 std::ostringstream buffer;
00455
00456 buffer << "OpenNN Exception: Vector Template.\n"
00457 << "void initialize_uniform(const double&, const double&) method.\n"
00458 << "Minimum value must be less or equal than maximum value.\n";
00459
00460 throw std::logic_error(buffer.str());
00461 }
00462
00463 #endif
00464
00465 const unsigned int this_size = this->size();
00466
00467 for(unsigned int i = 0; i < this_size; i++)
00468 {
00469 (*this)[i] = calculate_random_uniform(minimum, maximum);
00470 }
00471 }
00472
00473
00474
00475
00480
00481 void initialize_uniform(const Vector<double>& minimum, const Vector<double>& maximum)
00482 {
00483 const unsigned int this_size = this->size();
00484
00485
00486
00487 #ifdef _DEBUG
00488
00489 const unsigned int minimum_size = minimum.size();
00490 const unsigned int maximum_size = maximum.size();
00491
00492 if(minimum_size != this_size || maximum_size != this_size)
00493 {
00494 std::ostringstream buffer;
00495
00496 buffer << "OpenNN Exception: Vector Template.\n"
00497 << "void initialize_uniform(const Vector<double>&, const Vector<double>&) method.\n"
00498 << "Minimum and maximum sizes must be equal to vector size.\n";
00499
00500 throw std::logic_error(buffer.str());
00501 }
00502
00503 if(minimum > maximum)
00504 {
00505 std::ostringstream buffer;
00506
00507 buffer << "OpenNN Exception: Vector Template.\n"
00508 << "void initialize_uniform(const Vector<double>&, const Vector<double>&) method.\n"
00509 << "Minimum values must be less or equal than their corresponding maximum values.\n";
00510
00511 throw std::logic_error(buffer.str());
00512 }
00513
00514 #endif
00515
00516 for(unsigned int i = 0; i < this_size; i++)
00517 {
00518 (*this)[i] = calculate_random_uniform(minimum[i], maximum[i]);
00519 }
00520 }
00521
00522
00523
00524
00527
00528 void initialize_normal(void)
00529 {
00530 const unsigned int this_size = this->size();
00531
00532 for(unsigned int i = 0; i < this_size; i++)
00533 {
00534 (*this)[i] = calculate_random_normal(0.0, 1.0);
00535 }
00536 }
00537
00538
00539
00540
00545
00546 void initialize_normal(const double& mean, double standard_deviation)
00547 {
00548
00549
00550 #ifdef _DEBUG
00551
00552 if(standard_deviation < 0.0)
00553 {
00554 std::ostringstream buffer;
00555
00556 buffer << "OpenNN Exception: Vector Template.\n"
00557 << "void initialize_normal(const double&, const double&) method.\n"
00558 << "Standard deviation must be equal or greater than zero.\n";
00559
00560 throw std::logic_error(buffer.str());
00561 }
00562
00563 #endif
00564
00565 const unsigned int this_size = this->size();
00566
00567 for(unsigned int i = 0; i < this_size; i++)
00568 {
00569 (*this)[i] = calculate_random_normal(mean, standard_deviation);
00570 }
00571 }
00572
00573
00574
00575
00580
00581 void initialize_normal(const Vector<double>& mean, const Vector<double>& standard_deviation)
00582 {
00583 const unsigned int this_size = this->size();
00584
00585
00586
00587 #ifdef _DEBUG
00588
00589 const unsigned int mean_size = mean.size();
00590 const unsigned int standard_deviation_size = standard_deviation.size();
00591
00592 if(mean_size != this_size || standard_deviation_size != this_size)
00593 {
00594 std::ostringstream buffer;
00595
00596 buffer << "OpenNN Exception: Vector Template.\n"
00597 << "void initialize_normal(const Vector<double>&, const Vector<double>&) method.\n"
00598 << "Mean and standard deviation sizes must be equal to vector size.\n";
00599
00600 throw std::logic_error(buffer.str());
00601 }
00602
00603 if(standard_deviation < 0.0)
00604 {
00605 std::ostringstream buffer;
00606
00607 buffer << "OpenNN Exception: Vector Template.\n"
00608 << "void initialize_normal(const Vector<double>&, const Vector<double>&) method.\n"
00609 << "Standard deviations must be equal or greater than zero.\n";
00610
00611 throw std::logic_error(buffer.str());
00612 }
00613
00614 #endif
00615
00616 for(unsigned int i = 0; i < this_size; i++)
00617 {
00618 (*this)[i] = calculate_random_normal(mean[i], standard_deviation[i]);
00619 }
00620 }
00621
00622
00623
00624
00626
00627 Type calculate_minimum(void) const
00628 {
00629 const unsigned int this_size = this->size();
00630
00631 Type minimum = (*this)[0];
00632
00633 for(unsigned int i = 1; i < this_size; i++)
00634 {
00635 if((*this)[i] < minimum)
00636 {
00637 minimum = (*this)[i];
00638 }
00639 }
00640
00641 return(minimum);
00642 }
00643
00644
00645
00646
00648
00649 Type calculate_maximum(void) const
00650 {
00651 const unsigned int this_size = this->size();
00652
00653 Type maximum = (*this)[0];
00654
00655 for(unsigned int i = 1; i < this_size; i++)
00656 {
00657 if((*this)[i] > maximum)
00658 {
00659 maximum = (*this)[i];
00660 }
00661 }
00662
00663 return(maximum);
00664 }
00665
00666
00667
00668
00670
00671 Vector<Type> calculate_minimum_maximum(void) const
00672 {
00673 unsigned int this_size = this->size();
00674
00675 Type minimum = (*this)[0];
00676 Type maximum = (*this)[0];
00677
00678 for(unsigned int i = 1; i < this_size; i++)
00679 {
00680 if((*this)[i] < minimum)
00681 {
00682 minimum = (*this)[i];
00683 }
00684
00685 if((*this)[i] > maximum)
00686 {
00687 maximum = (*this)[i];
00688 }
00689 }
00690
00691 Vector<Type> minimum_maximum(2);
00692 minimum_maximum[0] = minimum;
00693 minimum_maximum[1] = maximum;
00694
00695 return(minimum_maximum);
00696 }
00697
00698
00699
00700
00706
00707 Vector< Vector<Type> > calculate_histogram(const unsigned int& bins_number) const
00708 {
00709
00710
00711 #ifdef _DEBUG
00712
00713 if(bins_number < 1)
00714 {
00715 std::ostringstream buffer;
00716
00717 buffer << "OpenNN Exception: Vector Template.\n"
00718 << "Vector< Vector<Type> > calculate_histogram(const unsigned int&) const method.\n"
00719 << "Number of beans must be equal or greater than one.\n";
00720
00721 throw std::logic_error(buffer.str());
00722 }
00723
00724 #endif
00725
00726 Vector<Type> bins_minimum(bins_number);
00727 Vector<Type> bins_maximum(bins_number);
00728
00729 Vector<Type> bins_center(bins_number);
00730 Vector<Type> bins_frequency(bins_number, 0);
00731
00732 const Vector<Type> minimum_maximum = calculate_minimum_maximum();
00733
00734 const Type minimum = minimum_maximum[0];
00735 const Type maximum = minimum_maximum[1];
00736
00737 const double bin_length = (maximum-minimum)/((double)bins_number);
00738
00739 bins_minimum[0] = minimum;
00740 bins_maximum[0] = minimum + bin_length;
00741 bins_center[0] = (bins_maximum[0] + bins_minimum[0])/2.0;
00742
00743
00744
00745 for(unsigned int i = 1; i < bins_number; i++)
00746 {
00747 bins_minimum[i] = bins_minimum[i-1] + bin_length;
00748 bins_maximum[i] = bins_maximum[i-1] + bin_length;
00749
00750 bins_center[i] = (bins_maximum[i] + bins_minimum[i])/2.0;
00751 }
00752
00753
00754
00755 const unsigned int this_size = this->size();
00756
00757 for(unsigned int i = 0; i < this_size; i++)
00758 {
00759 if((*this)[i] < bins_center[0])
00760 {
00761 bins_frequency[0]++;
00762 }
00763
00764 for(unsigned int j = 1; j < bins_number-1; j++)
00765 {
00766 if((*this)[i] >= bins_minimum[j] && (*this)[i] < bins_maximum[j])
00767 {
00768 bins_frequency[j]++;
00769 }
00770 }
00771
00772 if((*this)[i] >= bins_center[bins_number-1])
00773 {
00774 bins_frequency[bins_number-1]++;
00775 }
00776 }
00777
00778 Vector< Vector<Type> > histogram(2);
00779
00780 histogram[0] = bins_frequency;
00781 histogram[1] = bins_center;
00782
00783 return(histogram);
00784 }
00785
00786
00787
00788
00794
00795 Vector< Vector<Type> > calculate_histogram(void) const
00796 {
00797 return(calculate_histogram(10));
00798 }
00799
00800
00801
00802
00804
00805 unsigned int calculate_minimal_index(void) const
00806 {
00807 const unsigned int this_size = this->size();
00808
00809 Type minimum = (*this)[0];
00810 unsigned int minimal_index = 0;
00811
00812 for(unsigned int i = 1; i < this_size; i++)
00813 {
00814 if((*this)[i] < minimum)
00815 {
00816 minimum = (*this)[i];
00817 minimal_index = i;
00818 }
00819 }
00820
00821 return(minimal_index);
00822 }
00823
00824
00825
00826
00828
00829 unsigned int calculate_maximal_index(void) const
00830 {
00831 const unsigned int this_size = this->size();
00832
00833 Type maximum = (*this)[0];
00834 unsigned int maximal_index = 0;
00835
00836 for(unsigned int i = 1; i < this_size; i++)
00837 {
00838 if((*this)[i] > maximum)
00839 {
00840 maximum = (*this)[i];
00841 maximal_index = i;
00842 }
00843 }
00844
00845 return(maximal_index);
00846 }
00847
00848
00849
00850
00852
00853 Vector<unsigned int> calculate_minimal_maximal_index(void) const
00854 {
00855 unsigned int this_size = this->size();
00856
00857 Type minimum = (*this)[0];
00858 Type maximum = (*this)[0];
00859
00860 unsigned int minimal_index = 0;
00861 unsigned int maximal_index = 0;
00862
00863 for(unsigned int i = 1; i < this_size; i++)
00864 {
00865 if((*this)[i] < minimum)
00866 {
00867 minimum = (*this)[i];
00868 minimal_index = i;
00869 }
00870 if((*this)[i] > maximum)
00871 {
00872 maximum = (*this)[i];
00873 maximal_index = i;
00874 }
00875 }
00876
00877 Vector<unsigned int> minimal_maximal_index(2);
00878 minimal_maximal_index[0] = minimal_index;
00879 minimal_maximal_index[1] = maximal_index;
00880
00881 return(minimal_maximal_index);
00882 }
00883
00884
00885
00886
00889
00890 Vector<Type> calculate_pow(const Type& exponent) const
00891 {
00892 const unsigned int this_size = this->size();
00893
00894 Vector<Type> power(this_size);
00895
00896 for(unsigned int i = 0; i < this_size; i++)
00897 {
00898 power[i] = pow((*this)[i], exponent);
00899 }
00900
00901 return(power);
00902 }
00903
00904
00905
00906
00909
00910 Vector<Type> calculate_competitive(void) const
00911 {
00912 const unsigned int this_size = this->size();
00913
00914 Vector<Type> competitive(this_size, 0);
00915
00916 const unsigned int maximal_index = calculate_maximal_index();
00917
00918 competitive[maximal_index] = 1;
00919
00920 return(competitive);
00921 }
00922
00923
00924
00925
00928
00929 Vector<Type> calculate_softmax(void) const
00930 {
00931 const unsigned int this_size = this->size();
00932
00933 Vector<Type> softmax(this_size);
00934
00935 Type sum = 0;
00936
00937 for(unsigned int i = 0; i < this_size; i++)
00938 {
00939 sum += exp((*this)[i]);
00940 }
00941
00942 for(unsigned int i = 0; i < this_size; i++)
00943 {
00944 softmax[i] = exp((*this)[i])/sum;
00945 }
00946
00947 return(softmax);
00948 }
00949
00950
00951
00952
00955
00956 Matrix<Type> calculate_softmax_Jacobian(void) const
00957 {
00958 const unsigned int this_size = this->size();
00959
00960 Matrix<Type> softmax_Jacobian(this_size, this_size);
00961
00962 return(softmax_Jacobian);
00963 }
00964
00965
00966
00967
00968
00971
00972 Vector<bool> calculate_binary(void) const
00973 {
00974 const unsigned int this_size = this->size();
00975
00976 Vector<bool> binary(this_size);
00977
00978 for(unsigned int i = 0; i < this_size; i++)
00979 {
00980 if((*this)[i] < 0.5)
00981 {
00982 binary[i] = false;
00983 }
00984 else
00985 {
00986 binary[i] = true;
00987 }
00988 }
00989
00990 return(binary);
00991 }
00992
00993
00994
00995
00999
01000 unsigned int calculate_cumulative_index(const Type& value) const
01001 {
01002 const unsigned int this_size = this->size();
01003
01004
01005
01006 #ifdef _DEBUG
01007
01008 if(this_size == 0)
01009 {
01010 std::ostringstream buffer;
01011
01012 buffer << "OpenNN Exception: Vector Template.\n"
01013 << "unsigned int calculate_cumulative_index(const Type&) const.\n"
01014 << "Size must be greater than zero.\n";
01015
01016 throw std::logic_error(buffer.str());
01017 }
01018
01019 Type cumulative_value = (*this)[size()-1];
01020
01021 if(value > cumulative_value)
01022 {
01023 std::ostringstream buffer;
01024
01025 buffer << "OpenNN Exception: Vector Template.\n"
01026 << "unsigned int calculate_cumulative_index(const Type&) const.\n"
01027 << "Value (" << value << ") must be less than cumulative value (" << cumulative_value << ").\n";
01028
01029 throw std::logic_error(buffer.str());
01030 }
01031
01032 for(unsigned int i = 1; i < this_size; i++)
01033 {
01034 if((*this)[i] < (*this)[i-1])
01035 {
01036 std::ostringstream buffer;
01037
01038 buffer << "OpenNN Exception: Vector Template.\n"
01039 << "int calculate_cumulative_index(const Type&) const.\n"
01040 << "Vector elements must be crescent.\n";
01041
01042 throw std::logic_error(buffer.str());
01043 }
01044 }
01045
01046 #endif
01047
01048 if(value <= (*this)[0])
01049 {
01050 return(0);
01051 }
01052
01053 for(unsigned int i = 1; i < this_size; i++)
01054 {
01055 if(value > (*this)[i-1] && value <= (*this)[i])
01056 {
01057 return(i);
01058 }
01059 }
01060
01061 return(this_size-1);
01062 }
01063
01064
01065
01066
01068
01069 unsigned int calculate_closest_index(const Type& value) const
01070 {
01071 const Vector<Type> difference = (*this - value).calculate_absolute_value();
01072
01073 const unsigned int closest_index = difference.calculate_minimal_index();
01074
01075 return(closest_index);
01076 }
01077
01078
01079
01080
01082
01083 Type calculate_sum(void) const
01084 {
01085 const unsigned int this_size = this->size();
01086
01087 Type sum = 0;
01088
01089 for(unsigned int i = 0; i < this_size; i++)
01090 {
01091 sum += (*this)[i];
01092 }
01093
01094 return(sum);
01095 }
01096
01097
01098
01099
01101
01102 Type calculate_product(void) const
01103 {
01104 const unsigned int this_size = this->size();
01105
01106 Type product = 1;
01107
01108 for(unsigned int i = 0; i < this_size; i++)
01109 {
01110 product *= (*this)[i];
01111 }
01112
01113 return(product);
01114 }
01115
01116
01117
01118
01120
01121 double calculate_mean(void) const
01122 {
01123 const unsigned int this_size = this->size();
01124
01125
01126
01127 #ifdef _DEBUG
01128
01129 if(this_size == 0)
01130 {
01131 std::ostringstream buffer;
01132
01133 buffer << "OpenNN Exception: Vector Template.\n"
01134 << "double calculate_mean(void) const method.\n"
01135 << "Size must be greater than zero.\n";
01136
01137 throw std::logic_error(buffer.str());
01138 }
01139
01140 #endif
01141
01142 const Type sum = calculate_sum();
01143
01144 const double mean = sum/(double)this_size;
01145
01146 return(mean);
01147 }
01148
01149
01150
01151
01153
01154 double calculate_standard_deviation(void) const
01155 {
01156 const unsigned int this_size = this->size();
01157
01158
01159
01160 #ifdef _DEBUG
01161
01162 if(this_size == 0)
01163 {
01164 std::ostringstream buffer;
01165
01166 buffer << "OpenNN Exception: Vector Template.\n"
01167 << "double calculate_standard_deviation(void) const method.\n"
01168 << "Size must be greater than zero.\n";
01169
01170 throw std::logic_error(buffer.str());
01171 }
01172
01173 #endif
01174
01175 const double mean = calculate_mean();
01176
01177 double sum = 0.0;
01178
01179 for(unsigned int i = 0; i < this_size; i++)
01180 {
01181 sum += ((*this)[i] - mean)*((*this)[i] - mean);
01182 }
01183
01184 const double standard_deviation = sqrt(sum/(double)this_size);
01185
01186 return(standard_deviation);
01187 }
01188
01189
01190
01191
01193
01194 Vector<double> calculate_mean_standard_deviation(void) const
01195 {
01196 const unsigned int this_size = this->size();
01197
01198
01199
01200 #ifdef _DEBUG
01201
01202 if(this_size == 0)
01203 {
01204 std::ostringstream buffer;
01205
01206 buffer << "OpenNN Exception: Vector Template.\n"
01207 << "double calculate_mean_standard_deviation(void).\n"
01208 << "Size must be greater than zero.\n";
01209
01210 throw std::logic_error(buffer.str());
01211 }
01212
01213 #endif
01214
01215 const double mean = calculate_mean();
01216
01217 double sum = 0.0;
01218
01219 for(unsigned int i = 0; i < this_size; i++)
01220 {
01221 sum += ((*this)[i] - mean)*((*this)[i] - mean);
01222 }
01223
01224 const double standard_deviation = sqrt(sum/(double)this_size);
01225
01226 Vector<double> mean_standard_deviation(2);
01227 mean_standard_deviation[0] = mean;
01228 mean_standard_deviation[1] = standard_deviation;
01229
01230 return(mean_standard_deviation);
01231 }
01232
01233
01234
01235
01237
01238 Vector<double> calculate_statistics(void) const
01239 {
01240 unsigned int this_size = this->size();
01241
01242
01243
01244 #ifdef _DEBUG
01245
01246 if(this_size == 0)
01247 {
01248 std::ostringstream buffer;
01249
01250 buffer << "OpenNN Exception: Vector Template.\n"
01251 << "double calculate_statistics(void).\n"
01252 << "Size must be greater than zero.\n";
01253
01254 throw std::logic_error(buffer.str());
01255 }
01256
01257 #endif
01258
01259 return(calculate_minimum_maximum().get_assembly(calculate_mean_standard_deviation()));
01260 }
01261
01262
01263
01264
01266
01267 double calculate_norm(void) const
01268 {
01269 const unsigned int this_size = this->size();
01270
01271
01272
01273 double norm = 0.0;
01274
01275 for(unsigned int i = 0; i < this_size; i++)
01276 {
01277 norm += (*this)[i]*(*this)[i];
01278 }
01279
01280 norm = sqrt(norm);
01281
01282 return(norm);
01283 }
01284
01285
01286
01287
01290
01291 double calculate_distance(const Vector<double>& other_vector) const
01292 {
01293 const unsigned int this_size = this->size();
01294
01295
01296
01297 #ifdef _DEBUG
01298
01299 unsigned int other_size = other_vector.size();
01300
01301 if(other_size != this_size)
01302 {
01303 std::ostringstream buffer;
01304
01305 buffer << "OpenNN Exception: Vector Template.\n"
01306 << "double calculate_distance(const Vector<double>&) const method.\n"
01307 << "Size must be equal to this size.\n";
01308
01309 throw std::logic_error(buffer.str());
01310 }
01311
01312 #endif
01313
01314 double sum_squared_error = 0.0;
01315
01316 for(unsigned int i = 0; i < this_size; i++)
01317 {
01318 sum_squared_error += ((*this)[i] - other_vector[i])*((*this)[i] - other_vector[i]);
01319 }
01320
01321 return(sqrt(sum_squared_error));
01322 }
01323
01324
01325
01326
01329
01330 double calculate_sum_squared_error(const Vector<double>& other_vector) const
01331 {
01332 const unsigned int this_size = this->size();
01333
01334
01335
01336 #ifdef _DEBUG
01337
01338 unsigned int other_size = other_vector.size();
01339
01340 if(other_size != this_size)
01341 {
01342 std::ostringstream buffer;
01343
01344 buffer << "OpenNN Exception: Vector Template.\n"
01345 << "double calculate_sum_squared_error(const Vector<double>&) const method.\n"
01346 << "Size must be equal to this size.\n";
01347
01348 throw std::logic_error(buffer.str());
01349 }
01350
01351 #endif
01352
01353 double sum_squared_error = 0.0;
01354
01355 for(unsigned int i = 0; i < this_size; i++)
01356 {
01357 sum_squared_error += ((*this)[i] - other_vector[i])*((*this)[i] - other_vector[i]);
01358 }
01359
01360 return(sum_squared_error);
01361 }
01362
01363
01364
01365
01369
01370 double calculate_Minkowski_error(const Vector<double>& other_vector, double Minkowski_parameter) const
01371 {
01372 const unsigned int this_size = this->size();
01373
01374
01375
01376 #ifdef _DEBUG
01377
01378 std::ostringstream buffer;
01379
01380 if(this_size == 0)
01381 {
01382 buffer << "OpenNN Exception: Vector Template.\n"
01383 << "double calculate_Minkowski_error(const Vector<double>&) const method.\n"
01384 << "Size must be greater than zero.\n";
01385
01386 throw std::logic_error(buffer.str());
01387 }
01388
01389 const unsigned int other_size = other_vector.size();
01390
01391 if(other_size != this_size)
01392 {
01393 buffer << "OpenNN Exception: Vector Template.\n"
01394 << "double calculate_Minkowski_error(const Vector<double>&) const method.\n"
01395 << "Other size must be equal to this size.\n";
01396
01397 throw std::logic_error(buffer.str());
01398 }
01399
01400
01401
01402 if(Minkowski_parameter < 1.0 || Minkowski_parameter > 2.0)
01403 {
01404 buffer << "OpenNN Exception: Vector Template.\n"
01405 << "double calculate_Minkowski_error(const Vector<double>&) const method.\n"
01406 << "The Minkowski parameter must be comprised between 1 and 2\n";
01407
01408 throw std::logic_error(buffer.str().c_str());
01409 }
01410
01411 #endif
01412
01413 double Minkowski_error = 0.0;
01414
01415 for(unsigned int i = 0; i < this_size; i++)
01416 {
01417 Minkowski_error += pow((*this)[i] - other_vector[i], Minkowski_parameter);
01418 }
01419
01420 return(Minkowski_error);
01421 }
01422
01423
01424
01425
01427
01428 Vector<Type> calculate_absolute_value(void) const
01429 {
01430 const unsigned int this_size = this->size();
01431
01432 Vector<Type> absolute_value(this_size);
01433
01434 for(unsigned int i = 0; i < this_size; i++)
01435 {
01436 if((*this)[i] > 0)
01437 {
01438 absolute_value[i] = (*this)[i];
01439 }
01440 else
01441 {
01442 absolute_value[i] = -(*this)[i];
01443 }
01444 }
01445
01446 return(absolute_value);
01447 }
01448
01449
01450
01451
01453
01454 void apply_absolute_value(void)
01455 {
01456 const unsigned int this_size = this->size();
01457
01458 for(unsigned int i = 0; i < this_size; i++)
01459 {
01460 if((*this)[i] < 0)
01461 {
01462 (*this)[i] = -(*this)[i];
01463 }
01464 }
01465 }
01466
01467
01468
01469
01472
01473 Vector<Type> calculate_lower_bounded(const Type& lower_bound) const
01474 {
01475 const unsigned int this_size = this->size();
01476
01477 Vector<Type> bounded_vector(this_size);
01478
01479 for(unsigned int i = 0; i < this_size; i++)
01480 {
01481 if((*this)[i] < lower_bound)
01482 {
01483 bounded_vector[i] = lower_bound;
01484 }
01485 else
01486 {
01487 bounded_vector[i] = (*this)[i];
01488 }
01489 }
01490
01491 return(bounded_vector);
01492 }
01493
01494
01495
01496
01499
01500 Vector<Type> calculate_lower_bounded(const Vector<Type>& lower_bound) const
01501 {
01502 const unsigned int this_size = this->size();
01503
01504
01505
01506 #ifdef _DEBUG
01507
01508 const unsigned int lower_bound_size = lower_bound.size();
01509
01510 if(lower_bound_size != this_size)
01511 {
01512 std::ostringstream buffer;
01513
01514 buffer << "OpenNN Exception: Vector Template.\n"
01515 << "Vector<Type> calculate_lower_bounded(const Vector<Type>&) const method.\n"
01516 << "Lower bound size must be equal to vector size.\n";
01517
01518 throw std::logic_error(buffer.str());
01519 }
01520
01521 #endif
01522
01523 Vector<Type> bounded_vector(this_size);
01524
01525
01526
01527 for(unsigned int i = 0; i < this_size; i++)
01528 {
01529 if((*this)[i] < lower_bound[i])
01530 {
01531 bounded_vector[i] = lower_bound[i];
01532 }
01533 else
01534 {
01535 bounded_vector[i] = (*this)[i];
01536 }
01537 }
01538
01539 return(bounded_vector);
01540 }
01541
01542
01543
01544
01547
01548 Vector<Type> calculate_upper_bounded(const Type& upper_bound) const
01549 {
01550 const unsigned int this_size = this->size();
01551
01552 Vector<Type> bounded_vector(this_size);
01553
01554 for(unsigned int i = 0; i < this_size; i++)
01555 {
01556 if((*this)[i] > upper_bound)
01557 {
01558 bounded_vector[i] = upper_bound;
01559 }
01560 else
01561 {
01562 bounded_vector[i] = (*this)[i];
01563 }
01564 }
01565
01566 return(bounded_vector);
01567 }
01568
01569
01570
01571
01574
01575 Vector<Type> calculate_upper_bounded(const Vector<Type>& upper_bound) const
01576 {
01577 const unsigned int this_size = this->size();
01578
01579
01580
01581 #ifdef _DEBUG
01582
01583 unsigned int upper_bound_size = upper_bound.size();
01584
01585 if(upper_bound_size != this_size)
01586 {
01587 std::ostringstream buffer;
01588
01589 buffer << "OpenNN Exception: Vector Template.\n"
01590 << "Vector<Type> calculate_upper_bounded(const Vector<Type>&) const method.\n"
01591 << "Upper bound size must be equal to vector size.\n";
01592
01593 throw std::logic_error(buffer.str());
01594 }
01595
01596 #endif
01597
01598 Vector<Type> bounded_vector(this_size);
01599
01600
01601
01602 for(unsigned int i = 0; i < this_size; i++)
01603 {
01604 if((*this)[i] > upper_bound[i])
01605 {
01606 bounded_vector[i] = upper_bound[i];
01607 }
01608 else
01609 {
01610 bounded_vector[i] = (*this)[i];
01611 }
01612 }
01613
01614 return(bounded_vector);
01615 }
01616
01617
01618
01619
01624
01625 Vector<Type> calculate_lower_upper_bounded(const Type& lower_bound, const Type& upper_bound) const
01626 {
01627 const unsigned int this_size = this->size();
01628
01629 Vector<Type> bounded_vector(this_size);
01630
01631 for(unsigned int i = 0; i < this_size; i++)
01632 {
01633 if((*this)[i] < lower_bound)
01634 {
01635 bounded_vector[i] = lower_bound;
01636 }
01637 else if((*this)[i] > upper_bound)
01638 {
01639 bounded_vector[i] = upper_bound;
01640 }
01641 else
01642 {
01643 bounded_vector[i] = (*this)[i];
01644 }
01645 }
01646
01647 return(bounded_vector);
01648 }
01649
01650
01651
01652
01657
01658 Vector<Type> calculate_lower_upper_bounded(const Vector<Type>& lower_bound, const Vector<Type>& upper_bound) const
01659 {
01660 const unsigned int this_size = this->size();
01661
01662
01663
01664 #ifdef _DEBUG
01665
01666 unsigned int lower_bound_size = lower_bound.size();
01667 unsigned int upper_bound_size = upper_bound.size();
01668
01669 if(lower_bound_size != this_size || upper_bound_size != this_size)
01670 {
01671 std::ostringstream buffer;
01672
01673 buffer << "OpenNN Exception: Vector Template.\n"
01674 << "Vector<Type> calculate_lower_upper_bounded(const Vector<Type>&, const Vector<Type>&) const method.\n"
01675 << "Lower and upper bound sizes must be equal to vector size.\n";
01676
01677 throw std::logic_error(buffer.str());
01678 }
01679
01680 #endif
01681
01682 Vector<Type> bounded_vector(this_size);
01683
01684
01685
01686 for(unsigned int i = 0; i < this_size; i++)
01687 {
01688 if((*this)[i] < lower_bound[i])
01689 {
01690 bounded_vector[i] = lower_bound[i];
01691 }
01692 else if((*this)[i] > upper_bound[i])
01693 {
01694 bounded_vector[i] = upper_bound[i];
01695 }
01696 else
01697 {
01698 bounded_vector[i] = (*this)[i];
01699 }
01700 }
01701
01702 return(bounded_vector);
01703 }
01704
01705
01706
01707
01710
01711 void apply_lower_bound(const Type& lower_bound)
01712 {
01713 const unsigned int this_size = this->size();
01714
01715 for(unsigned int i = 0; i < this_size; i++)
01716 {
01717 if((*this)[i] < lower_bound)
01718 {
01719 (*this)[i] = lower_bound;
01720 }
01721 }
01722 }
01723
01724
01725
01726
01729
01730 void apply_lower_bound(const Vector<Type>& lower_bound)
01731 {
01732 const unsigned int this_size = this->size();
01733
01734 for(unsigned int i = 0; i < this_size; i++)
01735 {
01736 if((*this)[i] < lower_bound[i])
01737 {
01738 (*this)[i] = lower_bound[i];
01739 }
01740 }
01741 }
01742
01743
01744
01745
01748
01749 void apply_upper_bound(const Type& upper_bound)
01750 {
01751 const unsigned int this_size = this->size();
01752
01753 for(unsigned int i = 0; i < this_size; i++)
01754 {
01755 if((*this)[i] > upper_bound)
01756 {
01757 (*this)[i] = upper_bound;
01758 }
01759 }
01760 }
01761
01762
01763
01764
01767
01768 void apply_upper_bound(const Vector<Type>& upper_bound)
01769 {
01770 const unsigned int this_size = this->size();
01771
01772 for(unsigned int i = 0; i < this_size; i++)
01773 {
01774 if((*this)[i] > upper_bound[i])
01775 {
01776 (*this)[i] = upper_bound[i];
01777 }
01778 }
01779 }
01780
01781
01782
01783
01788
01789 void apply_lower_upper_bounds(const Type& lower_bound, const Type& upper_bound)
01790 {
01791 const unsigned int this_size = this->size();
01792
01793 for(unsigned int i = 0; i < this_size; i++)
01794 {
01795 if((*this)[i] < lower_bound)
01796 {
01797 (*this)[i] = lower_bound;
01798 }
01799 else if((*this)[i] > upper_bound)
01800 {
01801 (*this)[i] = upper_bound;
01802 }
01803 }
01804 }
01805
01806
01807
01808
01813
01814 void apply_lower_upper_bounds(const Vector<Type>& lower_bound, const Vector<Type>& upper_bound)
01815 {
01816 const unsigned int this_size = this->size();
01817
01818 for(unsigned int i = 0; i < this_size; i++)
01819 {
01820 if((*this)[i] < lower_bound[i])
01821 {
01822 (*this)[i] = lower_bound[i];
01823 }
01824 else if((*this)[i] > upper_bound[i])
01825 {
01826 (*this)[i] = upper_bound[i];
01827 }
01828 }
01829 }
01830
01831
01832
01833
01836
01837 inline Vector<Type> operator + (const Type& scalar) const
01838 {
01839 const unsigned int this_size = this->size();
01840
01841 Vector<Type> sum(this_size);
01842
01843 for(unsigned int i = 0; i < this_size; i++)
01844 {
01845 sum[i] = (*this)[i] + scalar;
01846 }
01847
01848 return(sum);
01849 }
01850
01851
01852
01853
01856
01857 inline Vector<Type> operator + (const Vector<Type>& other_vector) const
01858 {
01859 const unsigned int this_size = this->size();
01860
01861
01862
01863 #ifdef _DEBUG
01864
01865 unsigned int other_size = other_vector.size();
01866
01867 if(other_size != this_size)
01868 {
01869 std::ostringstream buffer;
01870
01871 buffer << "OpenNN Exception: Vector Template.\n"
01872 << "Vector<Type> operator + (const Vector<Type>) const.\n"
01873 << "Size of vectors is " << this_size << " and " << other_size << " and they must be the same.\n";
01874
01875 throw std::logic_error(buffer.str());
01876 }
01877
01878 #endif
01879
01880 Vector<Type> sum(this_size);
01881
01882 for(unsigned int i = 0; i < this_size; i++)
01883 {
01884 sum[i] = (*this)[i] + other_vector[i];
01885 }
01886
01887 return(sum);
01888 }
01889
01890
01891
01892
01895
01896 inline Vector<Type> operator - (const Type& scalar) const
01897 {
01898 const unsigned int this_size = this->size();
01899
01900 Vector<Type> difference(this_size);
01901
01902 for(unsigned int i = 0; i < this_size; i++)
01903 {
01904 difference[i] = (*this)[i] - scalar;
01905 }
01906
01907 return(difference);
01908 }
01909
01910
01911
01912
01915
01916 inline Vector<Type> operator - (const Vector<Type>& other_vector) const
01917 {
01918 const unsigned int this_size = this->size();
01919
01920
01921
01922 #ifdef _DEBUG
01923
01924 const unsigned int other_size = other_vector.size();
01925
01926 if(other_size != this_size)
01927 {
01928 std::ostringstream buffer;
01929
01930 buffer << "OpenNN Exception: Vector Template.\n"
01931 << "Vector<Type> operator - (const Vector<Type>&) const.\n"
01932 << "Size of vectors is " << this_size << " and " << other_size << " and they must be the same.\n";
01933
01934 throw std::logic_error(buffer.str());
01935 }
01936
01937 #endif
01938
01939 Vector<Type> difference(this_size);
01940
01941 for(unsigned int i = 0; i < this_size; i++)
01942 {
01943 difference[i] = (*this)[i] - other_vector[i];
01944 }
01945
01946 return(difference);
01947 }
01948
01949
01950
01951
01954
01955 inline Vector<Type> operator * (const Type& scalar) const
01956 {
01957 const unsigned int this_size = this->size();
01958
01959 Vector<Type> product(this_size);
01960
01961 for(unsigned int i = 0; i < this_size; i++)
01962 {
01963 product[i] = (*this)[i]*scalar;
01964 }
01965
01966 return(product);
01967 }
01968
01969
01970
01971
01974
01975 inline Vector<Type> operator * (const Vector<Type>& other_vector) const
01976 {
01977 const unsigned int this_size = this->size();
01978
01979
01980
01981 #ifdef _DEBUG
01982
01983 const unsigned int other_size = other_vector.size();
01984
01985 if(other_size != this_size)
01986 {
01987 std::ostringstream buffer;
01988
01989 buffer << "OpenNN Exception: Vector Template.\n"
01990 << "Vector<Type> operator * (const Vector<Type>&) const.\n"
01991 << "Size of other vector (" << other_size << ") must be equal to size of this vector (" << this_size << ").\n";
01992
01993 throw std::logic_error(buffer.str());
01994 }
01995
01996 #endif
01997
01998 Vector<Type> product(this_size);
01999
02000 for(unsigned int i = 0; i < this_size; i++)
02001 {
02002 product[i] = (*this)[i]*other_vector[i];
02003 }
02004
02005 return(product);
02006 }
02007
02008
02009
02010
02013
02014 inline Matrix<Type> operator * (const Matrix<Type>& matrix) const
02015 {
02016 const unsigned int rows_number = matrix.get_rows_number();
02017 const unsigned int columns_number = matrix.get_columns_number();
02018
02019
02020
02021 #ifdef _DEBUG
02022
02023 const unsigned int this_size = this->size();
02024
02025 if(rows_number != this_size)
02026 {
02027 std::ostringstream buffer;
02028
02029 buffer << "OpenNN Exception: Vector Template.\n"
02030 << "Vector<Type> operator * (const Matrix<Type>&) const.\n"
02031 << "Number of matrix rows (" << rows_number << ") must be equal to vector size (" << this_size << ").\n";
02032
02033 throw std::logic_error(buffer.str());
02034 }
02035
02036 #endif
02037
02038 Matrix<Type> product(rows_number, columns_number);
02039
02040 for(unsigned int i = 0; i < rows_number; i++)
02041 {
02042 for(unsigned int j = 0; j < columns_number; j++)
02043 {
02044 product[i][j] = (*this)[i]*matrix[i][j];
02045 }
02046 }
02047
02048 return(product);
02049 }
02050
02051
02052
02053
02057
02058 Vector<Type> dot(const Matrix<Type>& matrix) const
02059 {
02060 const unsigned int rows_number = matrix.get_rows_number();
02061
02062
02063
02064 #ifdef _DEBUG
02065
02066 const unsigned int this_size = this->size();
02067
02068 if(rows_number != this_size)
02069 {
02070 std::ostringstream buffer;
02071
02072 buffer << "OpenNN Exception: Vector Template.\n"
02073 << "Vector<Type> dot(const Matrix<Type>&) const method.\n"
02074 << "Matrix number of rows must be equal to vector size.\n";
02075
02076 throw std::logic_error(buffer.str());
02077 }
02078
02079 #endif
02080
02081 const unsigned int columns_number = matrix.get_columns_number();
02082
02083 Vector<Type> product(columns_number);
02084
02085 for(unsigned int j = 0; j < columns_number; j++)
02086 {
02087 product[j] = 0;
02088
02089 for(unsigned int i = 0; i < rows_number; i++)
02090 {
02091 product[j] += (*this)[i]*matrix[i][j];
02092 }
02093 }
02094
02095 return(product);
02096 }
02097
02098
02099
02100
02103
02104 Type dot(const Vector<Type>& other_vector) const
02105 {
02106 const unsigned int this_size = this->size();
02107
02108
02109
02110 #ifdef _DEBUG
02111
02112 const unsigned int other_size = other_vector.size();
02113
02114 if(other_size != this_size)
02115 {
02116 std::ostringstream buffer;
02117
02118 buffer << "OpenNN Exception: Vector Template.\n"
02119 << "Type dot(const Vector<Type>&) const method.\n"
02120 << "Both vector sizes must be the same.\n";
02121
02122 throw std::logic_error(buffer.str());
02123 }
02124
02125 #endif
02126
02127 Type dot_product = 0;
02128
02129 for(unsigned int i = 0; i < this_size; i++)
02130 {
02131 dot_product += (*this)[i]*other_vector[i];
02132 }
02133
02134 return(dot_product);
02135 }
02136
02137
02138
02139
02142
02143 Matrix<Type> direct(const Vector<Type>& other_vector) const
02144 {
02145 const unsigned int this_size = this->size();
02146
02147
02148
02149 #ifdef _DEBUG
02150
02151 const unsigned int other_size = other_vector.size();
02152
02153 if(other_size != this_size)
02154 {
02155 std::ostringstream buffer;
02156
02157 buffer << "OpenNN Exception: Vector Template.\n"
02158 << "Matrix<Type> direct(const Vector<Type>&) const method.\n"
02159 << "Both vector sizes must be the same.\n";
02160
02161 throw std::logic_error(buffer.str());
02162 }
02163
02164 #endif
02165
02166 Matrix<Type> direct(this_size, this_size);
02167
02168
02169
02170 for(unsigned int i = 0; i < this_size; i++)
02171 {
02172 for(unsigned int j = i; j < this_size; j++)
02173 {
02174 direct[i][j] = (*this)[i]*other_vector[j];
02175 }
02176 }
02177
02178
02179
02180 for(unsigned int i = 0; i < this_size; i++)
02181 {
02182 for(unsigned int j = 0; j < i; j++)
02183 {
02184 direct[i][j] = direct[j][i];
02185 }
02186 }
02187
02188 return(direct);
02189 }
02190
02191
02192
02193
02196
02197 Vector<Type> operator / (const Type& scalar) const
02198 {
02199 const unsigned int this_size = this->size();
02200
02201 Vector<Type> cocient(this_size);
02202
02203 for(unsigned int i = 0; i < this_size; i++)
02204 {
02205 cocient[i] = (*this)[i]/scalar;
02206 }
02207
02208 return(cocient);
02209 }
02210
02211
02212
02213
02216
02217 Vector<Type> operator / (const Vector<Type>& other_vector) const
02218 {
02219 const unsigned int this_size = this->size();
02220
02221
02222
02223 #ifdef _DEBUG
02224
02225 const unsigned int other_size = other_vector.size();
02226
02227 if(other_size != this_size)
02228 {
02229 std::ostringstream buffer;
02230
02231 buffer << "OpenNN Exception: Vector Template.\n"
02232 << "Vector<Type> operator / (const Vector<Type>&) const.\n"
02233 << "Both vector sizes must be the same.\n";
02234
02235 throw std::logic_error(buffer.str());
02236 }
02237
02238 #endif
02239
02240 Vector<Type> cocient(this_size);
02241
02242 for(unsigned int i = 0; i < this_size; i++)
02243 {
02244 cocient[i] = (*this)[i]/other_vector[i];
02245 }
02246
02247 return(cocient);
02248 }
02249
02250
02251
02252
02255
02256 void operator += (const Type& value)
02257 {
02258 const unsigned int this_size = this->size();
02259
02260 for(unsigned int i = 0; i < this_size; i++)
02261 {
02262 (*this)[i] += value;
02263 }
02264 }
02265
02266
02267
02268
02271
02272 void operator += (const Vector<Type>& other_vector)
02273 {
02274 const unsigned int this_size = this->size();
02275
02276
02277
02278 #ifdef _DEBUG
02279
02280 const unsigned int other_size = other_vector.size();
02281
02282 if(other_size != this_size)
02283 {
02284 std::ostringstream buffer;
02285
02286 buffer << "OpenNN Exception: Vector Template.\n"
02287 << "void operator += (const Vector<Type>&).\n"
02288 << "Both vector sizes must be the same.\n";
02289
02290 throw std::logic_error(buffer.str());
02291 }
02292
02293 #endif
02294
02295 for(unsigned int i = 0; i < this_size; i++)
02296 {
02297 (*this)[i] += other_vector[i];
02298 }
02299 }
02300
02301
02302
02303
02306
02307 void operator -= (const Type& value)
02308 {
02309 const unsigned int this_size = this->size();
02310
02311 for(unsigned int i = 0; i < this_size; i++)
02312 {
02313 (*this)[i] -= value;
02314 }
02315 }
02316
02317
02318
02319
02322
02323 void operator -= (const Vector<Type>& other_vector)
02324 {
02325 const unsigned int this_size = this->size();
02326
02327
02328
02329 #ifdef _DEBUG
02330
02331 const unsigned int other_size = other_vector.size();
02332
02333 if(other_size != this_size)
02334 {
02335 std::ostringstream buffer;
02336
02337 buffer << "OpenNN Exception: Vector Template.\n"
02338 << "void operator -= (const Vector<Type>&).\n"
02339 << "Both vector sizes must be the same.\n";
02340
02341 throw std::logic_error(buffer.str());
02342 }
02343
02344 #endif
02345
02346 for(unsigned int i = 0; i < this_size; i++)
02347 {
02348 (*this)[i] -= other_vector[i];
02349 }
02350 }
02351
02352
02353
02354
02357
02358 void operator *= (const Type& value)
02359 {
02360 const unsigned int this_size = this->size();
02361
02362 for(unsigned int i = 0; i < this_size; i++)
02363 {
02364 (*this)[i] *= value;
02365 }
02366 }
02367
02368
02369
02370
02373
02374 void operator *= (const Vector<Type>& other_vector)
02375 {
02376 const unsigned int this_size = this->size();
02377
02378
02379
02380 #ifdef _DEBUG
02381
02382 unsigned int other_size = other_vector.size();
02383
02384 if(other_size != this_size)
02385 {
02386 std::ostringstream buffer;
02387
02388 buffer << "OpenNN Exception: Vector Template.\n"
02389 << "void operator *= (const Vector<Type>&).\n"
02390 << "Both vector sizes must be the same.\n";
02391
02392 throw std::logic_error(buffer.str());
02393 }
02394
02395 #endif
02396
02397 for(unsigned int i = 0; i < this_size; i++)
02398 {
02399 (*this)[i] *= other_vector[i];
02400 }
02401 }
02402
02403
02404
02405
02408
02409 void operator /= (const Type& value)
02410 {
02411 const unsigned int this_size = this->size();
02412
02413 for(unsigned int i = 0; i < this_size; i++)
02414 {
02415 (*this)[i] /= value;
02416 }
02417 }
02418
02419
02420
02421
02424
02425 void operator /= (const Vector<Type>& other_vector)
02426 {
02427 const unsigned int this_size = this->size();
02428
02429
02430
02431 #ifdef _DEBUG
02432
02433 unsigned int other_size = other_vector.size();
02434
02435 if(other_size != this_size)
02436 {
02437 std::ostringstream buffer;
02438
02439 buffer << "OpenNN Exception: Vector Template.\n"
02440 << "void operator /= (const Vector<Type>&).\n"
02441 << "Both vector sizes must be the same.\n";
02442
02443 throw std::logic_error(buffer.str());
02444 }
02445
02446 #endif
02447
02448 for(unsigned int i = 0; i < this_size; i++)
02449 {
02450 (*this)[i] /= other_vector[i];
02451 }
02452 }
02453
02454
02455
02456
02462
02463 void scale_mean_standard_deviation(const Vector<Type>& mean, const Vector<Type>& standard_deviation)
02464 {
02465 const unsigned int this_size = this->size();
02466
02467 #ifdef _DEBUG
02468
02469 const unsigned int mean_size = mean.size();
02470
02471 if(mean_size != this_size)
02472 {
02473 std::ostringstream buffer;
02474
02475 buffer << "OpenNN Exception: Vector template."
02476 << "void scale_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) method.\n"
02477 << "Size of mean vector must be equal to size.\n";
02478
02479 throw std::logic_error(buffer.str());
02480 }
02481
02482 unsigned int standard_deviation_size = standard_deviation.size();
02483
02484 if(standard_deviation_size != this_size)
02485 {
02486 std::ostringstream buffer;
02487
02488 buffer << "OpenNN Exception: Vector template."
02489 << "void scale_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) method.\n"
02490 << "Size of standard deviation vector must be equal to size.\n";
02491
02492 throw std::logic_error(buffer.str());
02493 }
02494
02495 #endif
02496
02497
02498
02499 for(unsigned int i = 0; i < this_size; i++)
02500 {
02501 if(standard_deviation[i] < 1e-99)
02502 {
02503 std::cout << "OpenNN Warning: Vector class.\n"
02504 << "void scale_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) method.\n"
02505 << "Standard deviation of variable " << i << " is zero.\n"
02506 << "Those elements won't be scaled.\n";
02507
02508
02509 }
02510 else
02511 {
02512 (*this)[i] = ((*this)[i] - mean[i])/standard_deviation[i];
02513 }
02514 }
02515 }
02516
02517
02518
02519
02520
02525
02526 Vector<Type> calculate_scaled_minimum_maximum(const Vector<Type>& minimum, const Vector<Type>& maximum) const
02527 {
02528 const unsigned int this_size = this->size();
02529
02530 #ifdef _DEBUG
02531
02532 const unsigned int minimum_size = minimum.size();
02533
02534 if(minimum_size != this_size)
02535 {
02536 std::ostringstream buffer;
02537
02538 buffer << "OpenNN Exception: Vector template."
02539 << "Vector<Type> calculate_scaled_minimum_maximum(const Vector<Type>&, const Vector<Type>&) const method.\n"
02540 << "Size of minimum vector must be equal to size.\n";
02541
02542 throw std::logic_error(buffer.str());
02543 }
02544
02545 const unsigned int maximum_size = maximum.size();
02546
02547 if(maximum_size != this_size)
02548 {
02549 std::ostringstream buffer;
02550
02551 buffer << "OpenNN Exception: Vector template."
02552 << "Vector<Type> calculate_scaled_minimum_maximum(const Vector<Type>&, const Vector<Type>&) const method.\n"
02553 << "Size of maximum vector must be equal to size.\n";
02554
02555 throw std::logic_error(buffer.str());
02556 }
02557
02558 #endif
02559
02560 Vector<Type> scaled_minimum_maximum(this_size);
02561
02562
02563
02564 for(unsigned int i = 0; i < this_size; i++)
02565 {
02566 if(maximum[i] - minimum[i] < 1e-99)
02567 {
02568 std::cout << "OpenNN Warning: Vector class.\n"
02569 << "Vector<Type> calculate_scaled_minimum_maximum(const Vector<Type>&, const Vector<Type>&) const method.\n"
02570 << "Minimum and maximum values of variable " << i << " are equal.\n"
02571 << "Those elements won't be scaled.\n";
02572
02573 scaled_minimum_maximum[i] = (*this)[i];
02574 }
02575 else
02576 {
02577 scaled_minimum_maximum[i] = 2.0*((*this)[i] - minimum[i])/(maximum[i]-minimum[i])-1.0;
02578 }
02579 }
02580
02581 return(scaled_minimum_maximum);
02582 }
02583
02584
02585
02586
02591
02592 Vector<Type> calculate_scaled_mean_standard_deviation(const Vector<Type>& mean, const Vector<Type>& standard_deviation) const
02593 {
02594 const unsigned int this_size = this->size();
02595
02596 #ifdef _DEBUG
02597
02598 std::ostringstream buffer;
02599
02600 const unsigned int mean_size = mean.size();
02601
02602 if(mean_size != this_size)
02603 {
02604 buffer << "OpenNN Exception: Vector template."
02605 << "Vector<Type> calculate_scaled_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) const method.\n"
02606 << "Size of mean vector must be equal to size.\n";
02607
02608 throw std::logic_error(buffer.str());
02609 }
02610
02611 const unsigned int standard_deviation_size = standard_deviation.size();
02612
02613 if(standard_deviation_size != this_size)
02614 {
02615 buffer << "OpenNN Exception: Vector template.\n"
02616 << "Vector<Type> calculate_scaled_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) const method.\n"
02617 << "Size of standard deviation vector must be equal to size.\n";
02618
02619 throw std::logic_error(buffer.str());
02620 }
02621
02622 #endif
02623
02624 Vector<Type> scaled_mean_standard_deviation(this_size);
02625
02626 for(unsigned int i = 0; i < this_size; i++)
02627 {
02628 if(standard_deviation[i] < 1e-99)
02629 {
02630 std::cout << "OpenNN Warning: Vector template.\n"
02631 << "Vector<Type> calculate_scaled_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) const method.\n"
02632 << "Standard deviation of variable " << i << " is zero.\n"
02633 << "Those elements won't be scaled.\n";
02634
02635 scaled_mean_standard_deviation = (*this)[i];
02636 }
02637 else
02638 {
02639 scaled_mean_standard_deviation[i] = (*this)[i]*standard_deviation[i] + mean[i];
02640 }
02641 }
02642
02643 return(scaled_mean_standard_deviation);
02644 }
02645
02646
02647
02648
02653
02654 Vector<Type> calculate_unscaled_minimum_maximum(const Vector<Type>& minimum, const Vector<Type>& maximum) const
02655 {
02656 const unsigned int this_size = this->size();
02657
02658 #ifdef _DEBUG
02659
02660 const unsigned int minimum_size = minimum.size();
02661
02662 if(minimum_size != this_size)
02663 {
02664 std::ostringstream buffer;
02665
02666 buffer << "OpenNN Exception: Vector template."
02667 << "Vector<Type> calculate_unscaled_minimum_maximum(const Vector<Type>&, const Vector<Type>&) const method.\n"
02668 << "Size of minimum vector must be equal to size.\n";
02669
02670 throw std::logic_error(buffer.str());
02671 }
02672
02673 const unsigned int maximum_size = maximum.size();
02674
02675 if(maximum_size != this_size)
02676 {
02677 std::ostringstream buffer;
02678
02679 buffer << "OpenNN Exception: Vector template."
02680 << "Vector<Type> calculate_unscaled_minimum_maximum(const Vector<Type>&, const Vector<Type>&) const method.\n"
02681 << "Size of maximum vector must be equal to size.\n";
02682
02683 throw std::logic_error(buffer.str());
02684 }
02685
02686 #endif
02687
02688 Vector<Type> unscaled_minimum_maximum(this_size);
02689
02690 for(unsigned int i = 0; i < this_size; i++)
02691 {
02692 if(maximum[i] - minimum[i] < 1e-99)
02693 {
02694 std::cout << "OpenNN Warning: Vector template.\n"
02695 << "Vector<Type> calculate_unscaled_minimum_maximum(const Vector<Type>&, const Vector<Type>&) const method.\n"
02696 << "Minimum and maximum values of variable " << i << " are equal.\n"
02697 << "Those elements won't be unscaled.\n";
02698
02699 unscaled_minimum_maximum[i] = (*this)[i];
02700 }
02701 else
02702 {
02703 unscaled_minimum_maximum[i] = 0.5*((*this)[i] + 1.0)*(maximum[i]-minimum[i]) + minimum[i];
02704 }
02705 }
02706
02707 return(unscaled_minimum_maximum);
02708 }
02709
02710
02711
02712
02717
02718 Vector<Type> calculate_unscaled_mean_standard_deviation(const Vector<Type>& mean, const Vector<Type>& standard_deviation) const
02719 {
02720 const unsigned int this_size = this->size();
02721
02722 #ifdef _DEBUG
02723
02724 const unsigned int mean_size = mean.size();
02725
02726 if(mean_size != this_size)
02727 {
02728 std::ostringstream buffer;
02729
02730 buffer << "OpenNN Exception: Vector template."
02731 << "Vector<Type> calculate_unscaled_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) const method.\n"
02732 << "Size of mean vector must be equal to size.\n";
02733
02734 throw std::logic_error(buffer.str());
02735 }
02736
02737 const unsigned int standard_deviation_size = standard_deviation.size();
02738
02739 if(standard_deviation_size != this_size)
02740 {
02741 std::ostringstream buffer;
02742
02743 buffer << "OpenNN Exception: Vector template.\n"
02744 << "Vector<Type> calculate_unscaled_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) const method.\n"
02745 << "Size of standard deviation vector must be equal to size.\n";
02746
02747 throw std::logic_error(buffer.str());
02748 }
02749
02750 #endif
02751
02752 Vector<Type> unscaled_mean_standard_deviation(this_size);
02753
02754 for(unsigned int i = 0; i < this_size; i++)
02755 {
02756 if(standard_deviation[i] < 1e-99)
02757 {
02758 std::cout << "OpenNN Warning: Vector template.\n"
02759 << "Vector<Type> calculate_unscaled_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) const method.\n"
02760 << "Standard deviation of variable " << i << " is zero.\n"
02761 << "Those elements won't be scaled.\n";
02762
02763 unscaled_mean_standard_deviation[i] = (*this)[i];
02764 }
02765 else
02766 {
02767 unscaled_mean_standard_deviation[i] = (*this)[i]*standard_deviation[i] + mean[i];
02768 }
02769 }
02770
02771 return(unscaled_mean_standard_deviation);
02772 }
02773
02774
02775
02776
02782
02783 void scale_minimum_maximum(const Vector<Type>& minimum, const Vector<Type>& maximum)
02784 {
02785 const unsigned int this_size = this->size();
02786
02787 #ifdef _DEBUG
02788
02789 const unsigned int minimum_size = minimum.size();
02790
02791 if(minimum_size != this_size)
02792 {
02793 std::ostringstream buffer;
02794
02795 buffer << "OpenNN Exception: Vector template."
02796 << "void scale_minimum_maximum(const Vector<Type>&, const Vector<Type>&) method.\n"
02797 << "Size of minimum vector must be equal to size.\n";
02798
02799 throw std::logic_error(buffer.str());
02800 }
02801
02802 const unsigned int maximum_size = maximum.size();
02803
02804 if(maximum_size != this_size)
02805 {
02806 std::ostringstream buffer;
02807
02808 buffer << "OpenNN Exception: Vector template."
02809 << "void scale_minimum_maximum(const Vector<Type>&, const Vector<Type>&) method.\n"
02810 << "Size of maximum vector must be equal to size.\n";
02811
02812 throw std::logic_error(buffer.str());
02813 }
02814
02815 #endif
02816
02817
02818
02819 for(unsigned int i = 0; i < this_size; i++)
02820 {
02821 if(maximum[i] - minimum[i] < 1e-99)
02822 {
02823 std::cout << "OpenNN Warning: Vector class.\n"
02824 << "void scale_minimum_maximum(const Vector<Type>&, const Vector<Type>&) method.\n"
02825 << "Minimum and maximum values of variable " << i << " are equal.\n"
02826 << "Those elements won't be scaled.\n";
02827
02828
02829 }
02830 else
02831 {
02832 (*this)[i] = 2.0*((*this)[i] - minimum[i])/(maximum[i]-minimum[i])-1.0;
02833 }
02834 }
02835 }
02836
02837
02838
02839
02845
02846 void unscale_minimum_maximum(const Vector<Type>& minimum, const Vector<Type>& maximum)
02847 {
02848 const unsigned int this_size = this->size();
02849
02850 #ifdef _DEBUG
02851
02852 const unsigned int minimum_size = minimum.size();
02853
02854 if(minimum_size != this_size)
02855 {
02856 std::ostringstream buffer;
02857
02858 buffer << "OpenNN Exception: Vector template."
02859 << "void unscale_minimum_maximum(const Vector<Type>&, const Vector<Type>&) method.\n"
02860 << "Size of minimum vector must be equal to size.\n";
02861
02862 throw std::logic_error(buffer.str());
02863 }
02864
02865 const unsigned int maximum_size = maximum.size();
02866
02867 if(maximum_size != this_size)
02868 {
02869 std::ostringstream buffer;
02870
02871 buffer << "OpenNN Exception: Vector template."
02872 << "void unscale_minimum_maximum(const Vector<Type>&, const Vector<Type>&) method.\n"
02873 << "Size of maximum vector must be equal to size.\n";
02874
02875 throw std::logic_error(buffer.str());
02876 }
02877
02878 #endif
02879
02880 for(unsigned int i = 0; i < this_size; i++)
02881 {
02882 if(maximum[i] - minimum[i] < 1e-99)
02883 {
02884 std::cout << "OpenNN Warning: Vector template.\n"
02885 << "void unscale_minimum_maximum(const Vector<Type>&, const Vector<Type>&) method.\n"
02886 << "Minimum and maximum values of variable " << i << " are equal.\n"
02887 << "Those elements won't be unscaled.\n";
02888
02889
02890 }
02891 else
02892 {
02893 (*this)[i] = 0.5*((*this)[i] + 1.0)*(maximum[i]-minimum[i]) + minimum[i];
02894 }
02895 }
02896 }
02897
02898
02899
02900
02906
02907 void unscale_mean_standard_deviation(const Vector<Type>& mean, const Vector<Type>& standard_deviation)
02908 {
02909 const unsigned int this_size = this->size();
02910
02911 #ifdef _DEBUG
02912
02913 const unsigned int mean_size = mean.size();
02914
02915 if(mean_size != this_size)
02916 {
02917 std::ostringstream buffer;
02918
02919 buffer << "OpenNN Exception: Vector template."
02920 << "void unscale_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) method.\n"
02921 << "Size of mean vector must be equal to size.\n";
02922
02923 throw std::logic_error(buffer.str());
02924 }
02925
02926 const unsigned int standard_deviation_size = standard_deviation.size();
02927
02928 if(standard_deviation_size != this_size)
02929 {
02930 std::ostringstream buffer;
02931
02932 buffer << "OpenNN Exception: Vector template.\n"
02933 << "void unscale_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) method.\n"
02934 << "Size of standard deviation vector must be equal to size.\n";
02935
02936 throw std::logic_error(buffer.str());
02937 }
02938
02939 #endif
02940
02941 for(unsigned int i = 0; i < this_size; i++)
02942 {
02943 if(standard_deviation[i] < 1e-99)
02944 {
02945 std::cout << "OpenNN Warning: Vector template.\n"
02946 << "void unscale_mean_standard_deviation(const Vector<Type>&, const Vector<Type>&) method.\n"
02947 << "Standard deviation of variable " << i << " is zero.\n"
02948 << "Those elements won't be scaled.\n";
02949
02950
02951 }
02952 else
02953 {
02954 (*this)[i] = (*this)[i]*standard_deviation[i] + mean[i];
02955 }
02956 }
02957 }
02958
02959
02960
02961
02964
02965 Matrix<Type> arrange_diagonal_matrix(void) const
02966 {
02967 const unsigned int this_size = this->size();
02968
02969 Matrix<Type> matrix(this_size, this_size, 0.0);
02970
02971 for(unsigned int i = 0; i < this_size; i++)
02972 {
02973 matrix[i][i] = (*this)[i];
02974 }
02975
02976 return(matrix);
02977 }
02978
02979
02980
02981
02985
02986 void load(const std::string& filename)
02987 {
02988 std::ifstream file(filename.c_str());
02989
02990 std::stringstream buffer;
02991
02992 std::string line;
02993
02994 while(file.good())
02995 {
02996 getline(file, line);
02997
02998 buffer << line;
02999 }
03000
03001 std::istream_iterator<std::string> it(buffer);
03002 std::istream_iterator<std::string> end;
03003
03004 const std::vector<std::string> results(it, end);
03005
03006 const unsigned int new_size = results.size();
03007
03008 this->resize(new_size);
03009
03010 file.clear();
03011 file.seekg(0, std::ios::beg);
03012
03013
03014
03015 for(unsigned int i = 0; i < new_size; i++)
03016 {
03017 file >> (*this)[i];
03018 }
03019
03020 file.close();
03021 }
03022
03023
03024
03025
03030
03031 void save(const std::string& filename) const
03032 {
03033 std::ofstream file(filename.c_str());
03034
03035 if(!file.is_open())
03036 {
03037 std::ostringstream buffer;
03038
03039 buffer << "OpenNN Exception: Vector template.\n"
03040 << "void save(const std::string&) const method.\n"
03041 << "Cannot open vector data file.\n";
03042
03043 throw std::logic_error(buffer.str());
03044 }
03045
03046
03047
03048 const unsigned int this_size = this->size();
03049
03050 for(unsigned int i = 0; i < this_size; i++)
03051 {
03052 file << (*this)[i] << " ";
03053 }
03054
03055 file << std::endl;
03056
03057
03058
03059 file.close();
03060 }
03061
03062
03063
03064
03068
03069 void tuck_in(const unsigned int& position, const Vector<Type>& other_vector)
03070 {
03071 const unsigned int other_size = other_vector.size();
03072
03073
03074
03075 #ifdef _DEBUG
03076
03077 const unsigned int this_size = this->size();
03078
03079 if(position + other_size > this_size)
03080 {
03081 std::ostringstream buffer;
03082
03083 buffer << "OpenNN Exception: Vector Template.\n"
03084 << "void tuck_in(const unsigned int&, const Vector<Type>&) const method.\n"
03085 << "Cannot tuck in vector.\n";
03086
03087 throw std::logic_error(buffer.str());
03088 }
03089
03090 #endif
03091
03092 for(unsigned int i = 0; i < other_size; i++)
03093 {
03094 (*this)[position + i] = other_vector[i];
03095 }
03096 }
03097
03098
03099
03100
03104
03105 Vector<Type> take_out(const unsigned int& position, const unsigned int& other_size) const
03106 {
03107
03108
03109 #ifdef _DEBUG
03110
03111 const unsigned int this_size = this->size();
03112
03113 if(position + other_size > this_size)
03114 {
03115 std::ostringstream buffer;
03116
03117 buffer << "OpenNN Exception: Vector Template.\n"
03118 << "Vector<Type> take_out(const unsigned int&, const unsigned int&) method.\n"
03119 << "Cannot take out vector.\n";
03120
03121 throw std::logic_error(buffer.str());
03122 }
03123
03124 #endif
03125
03126 Vector<Type> other_vector(other_size);
03127
03128 for(unsigned int i = 0; i < other_size; i++)
03129 {
03130 other_vector[i] = (*this)[position + i];
03131 }
03132
03133 return(other_vector);
03134 }
03135
03136
03137
03138
03141
03142 Vector<Type> get_assembly(const Vector<Type>& other_vector) const
03143 {
03144 const unsigned int this_size = this->size();
03145 const unsigned int other_size = other_vector.size();
03146
03147 if(this_size == 0 && other_size == 0)
03148 {
03149 Vector<Type> assembly;
03150
03151 return(assembly);
03152 }
03153 else if(this_size == 0)
03154 {
03155 return(other_vector);
03156 }
03157 else if(other_size == 0)
03158 {
03159 return(*this);
03160 }
03161 else
03162 {
03163 Vector<Type> assembly(this_size + other_size);
03164
03165 for(unsigned int i = 0; i < this_size; i++)
03166 {
03167 assembly[i] = (*this)[i];
03168 }
03169
03170 for(unsigned int i = 0; i < other_size; i++)
03171 {
03172 assembly[this_size+i] = other_vector[i];
03173 }
03174
03175 return(assembly);
03176 }
03177 }
03178
03179
03180
03181
03184
03185 void parse(const std::string& str)
03186 {
03187 if(!str.empty())
03188 {
03189 std::istringstream buffer(str);
03190
03191 std::istream_iterator<std::string> first(buffer);
03192 std::istream_iterator<std::string> last;
03193
03194 Vector<std::string> str_vector(first, last);
03195
03196 const unsigned int new_size = str_vector.size();
03197
03198 if(new_size > 0)
03199 {
03200 this->resize(new_size);
03201
03202 buffer.clear();
03203 buffer.seekg(0, std::ios::beg);
03204
03205 for(unsigned int i = 0; i < new_size; i++)
03206 {
03207 buffer >> (*this)[i];
03208 }
03209 }
03210 }
03211 }
03212
03213
03214
03215
03217
03218 std::string to_string(void) const
03219 {
03220 std::ostringstream buffer;
03221
03222 buffer << *this;
03223
03224 return(buffer.str());
03225 }
03226
03227
03228
03229
03231
03232 Vector<std::string> get_string_vector(void) const
03233 {
03234 const unsigned int this_size = this->size();
03235
03236 Vector<std::string> string_vector(this_size);
03237
03238 std::ostringstream buffer;
03239
03240 for(unsigned int i = 0; i < this_size; i++)
03241 {
03242 buffer.str("");
03243 buffer << (*this)[i];
03244
03245 string_vector[i] = buffer.str();
03246 }
03247
03248 return(string_vector);
03249 }
03250
03251
03252
03253
03258
03259
03260 Matrix<Type> to_matrix(const unsigned int& rows_number, const unsigned int& columns_number) const
03261 {
03262
03263
03264 #ifdef _DEBUG
03265
03266 const unsigned int this_size = this->size();
03267
03268 if(rows_number*columns_number != this_size)
03269 {
03270 std::ostringstream buffer;
03271
03272 buffer << "OpenNN Exception: Vector Template.\n"
03273 << "Matrix<Type> to_matrix(const unsigned int&, const unsigned int&) method.\n"
03274 << "The number of rows times the number of colums of the matrix must be equal to the size of the vector.\n";
03275
03276 throw std::logic_error(buffer.str());
03277 }
03278
03279 #endif
03280
03281 Matrix<Type> matrix(rows_number, columns_number);
03282
03283 unsigned int index = 0;
03284
03285 for(unsigned int i = 0; i < rows_number; i++)
03286 {
03287 for(unsigned int j = 0; j < rows_number; j++)
03288 {
03289 matrix[i][j] = (*this)[index];
03290 index++;
03291 }
03292 }
03293
03294 return(matrix);
03295 }
03296
03297
03298 private:
03299
03300
03301
03305
03306 double calculate_random_uniform(const double& minimum, double maximum) const
03307 {
03308 const double random = (double)rand()/(RAND_MAX+1.0);
03309
03310 const double random_uniform = minimum + (maximum-minimum)*random;
03311
03312 return(random_uniform);
03313 }
03314
03315
03316
03317
03321
03322 double calculate_random_normal(const double& mean, double standard_deviation) const
03323 {
03324 const double pi = 4.0*atan(1.0);
03325
03326 double random_uniform_1;
03327
03328 do
03329 {
03330 random_uniform_1 = (double)rand()/(RAND_MAX+1.0);
03331
03332 }while(random_uniform_1 == 0.0);
03333
03334 const double random_uniform_2 = (double)rand()/(RAND_MAX+1.0);
03335
03336
03337
03338 const double random_normal = mean + sqrt(-2.0*log(random_uniform_1))*sin(2.0*pi*random_uniform_2)*standard_deviation;
03339
03340 return(random_normal);
03341 }
03342
03343 };
03344
03345
03346
03347
03351
03352 template<typename Type>
03353 std::istream& operator >> (std::istream& is, Vector<Type>& v)
03354 {
03355 const unsigned int size = v.size();
03356
03357 for(unsigned int i = 0; i < size; i++)
03358 {
03359 is >> v[i];
03360 }
03361
03362 return(is);
03363 }
03364
03365
03366
03367
03371
03372 template<typename Type>
03373 inline std::ostream& operator << (std::ostream& os, const Vector<Type>& v)
03374 {
03375 const unsigned int size = v.size();
03376
03377 if(size > 0)
03378 {
03379 for(unsigned int i = 0; i < size-1; i++)
03380 {
03381 os << v[i] << " ";
03382 }
03383
03384 os << v[size-1];
03385 }
03386
03387 return(os);
03388 }
03389
03390
03391 template<typename Type>
03392 inline std::ostream& operator << (std::ostream& os, const Vector< Vector<Type> >& v)
03393 {
03394 for(unsigned int i = 0; i < v.size(); i++)
03395 {
03396 os << "Subvector " << i << "\n"
03397 << v[i] << std::endl;
03398 }
03399
03400 return(os);
03401 }
03402
03403
03404 template<typename Type>
03405 inline std::ostream& operator << (std::ostream& os, const Vector< Matrix<Type> >& v)
03406 {
03407 for(unsigned int i = 0; i < v.size(); i++)
03408 {
03409 os << "Submatrix" << i << "\n"
03410 << v[i];
03411 }
03412
03413 return(os);
03414 }
03415
03416 }
03417
03418 #endif
03419
03420
03421
03422
03423
03424
03425
03426
03427
03428
03429
03430
03431
03432
03433
03434
03435
03436