OpenNN  2.2
Open Neural Networks Library
vector.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* V E C T O R C O N T A I N E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __VECTOR_H__
15 #define __VECTOR_H__
16 
17 // System includes
18 
19 #include <algorithm>
20 #include <cassert>
21 #include <cmath>
22 #include <cstdlib>
23 #include <functional>
24 #include <fstream>
25 #include <iomanip>
26 #include <iostream>
27 #include <iterator>
28 #include <istream>
29 #include <map>
30 #include <numeric>
31 #include <ostream>
32 #include <sstream>
33 #include <stdexcept>
34 #include <string>
35 #include <vector>
36 #include<limits>
37 #include<climits>
38 
39 // Eigen includes
40 
41 #include "../eigen/Eigen"
42 
43 
44 namespace OpenNN
45 {
46 
47 // Forward declarations
48 
49 template<class T> class Matrix;
50 
51 template<class T>
52 T calculate_random_uniform(const T& = -1, const T& = 1);
53 
54 template<class T>
55 T calculate_random_normal(const T& = 0.0, const T& = 1.0);
56 
57 template<class T> struct Histogram;
58 template<class T> struct Statistics;
59 template<class T> struct LinearRegressionParameters;
60 template<class T> struct LogisticRegressionParameters;
61 
64 
65 template<typename T>
66 class Vector : public std::vector<T>
67 {
68 public:
69 
70  // CONSTRUCTORS
71 
72  // Default constructor.
73 
74  explicit Vector(void);
75 
76  // General constructor.
77 
78  explicit Vector(const size_t&);
79 
80  // Constant reference initialization constructor.
81 
82  explicit Vector(const size_t&, const T&);
83 
84  // File constructor.
85 
86  explicit Vector(const std::string&);
87 
88  // Sequential constructor.
89 
90  explicit Vector(const T&, const double&, const T&);
91 
92  // Input iterators constructor
93 
94  template<class InputIterator>
95  explicit Vector(InputIterator, InputIterator);
96 
97  // Copy constructor.
98 
99  Vector(const Vector<T>&);
100 
101  // DESTRUCTOR
102 
103  virtual ~Vector(void);
104 
105  // OPERATORS
106 
107  bool operator == (const T&) const;
108 
109  bool operator != (const T&) const;
110 
111  bool operator > (const T&) const;
112 
113  bool operator < (const T&) const;
114 
115  bool operator >= (const T&) const;
116 
117  bool operator <= (const T&) const;
118 
119  // METHODS
120 
121  // Get methods
122 
123  // Set methods
124 
125  void set(void);
126 
127  void set(const size_t&);
128 
129  void set(const size_t&, const T&);
130 
131  void set(const std::string&);
132 
133  void set(const T&, const double&, const T&);
134 
135  void set(const Vector&);
136 
137  // Initialization methods
138 
139  void initialize(const T&);
140 
141  void initialize_sequential(void);
142 
143  void randomize_uniform(const double& = -1.0, const double& = 1.0);
144  void randomize_uniform(const Vector<double>&, const Vector<double>&);
145 
146  void randomize_normal(const double& = 0.0, const double& = 1.0);
147  void randomize_normal(const Vector<double>&, const Vector<double>&);
148 
149  // Checking methods
150 
151  bool contains(const T&) const;
152 
153  bool contains(const Vector<T>&) const;
154 
155  bool is_in(const T&, const T&) const;
156 
157  bool is_constant(const double& = 0.0) const;
158 
159  bool is_crescent(void) const;
160 
161  bool is_decrescent(void) const;
162 
163  // Other methods
164 
165  size_t count_occurrences(const T&) const;
166 
168 
169  size_t count_greater_than(const T&) const;
170 
171  size_t count_less_than(const T&) const;
172 
174 
176 
177  // Statistics methods
178 
179  T calculate_minimum(void) const;
180 
181  T calculate_maximum(void) const;
182 
184 
186 
188 
190 
191  // Histogram methods
192 
193  Histogram<T> calculate_histogram(const size_t& = 10) const;
194 
195  Histogram<T> calculate_histogram_missing_values(const Vector<size_t>&, const size_t& = 10) const;
196 
197 
198  size_t calculate_minimal_index(void) const;
199 
200  size_t calculate_maximal_index(void) const;
201 
202  Vector<size_t> calculate_minimal_indices(const size_t&) const;
203 
204  Vector<size_t> calculate_maximal_indices(const size_t&) const;
205 
207 
208  Vector<T> calculate_pow(const T&) const;
209 
210  Vector<T> calculate_competitive(void) const;
211 
212  Vector<T> calculate_softmax(void) const;
213 
215 
216  Vector<bool> calculate_binary(void) const;
217 
218  Vector<T> calculate_cumulative(void) const;
219 
220  size_t calculate_cumulative_index(const T&) const;
221 
222  size_t calculate_closest_index(const T&) const;
223 
224  T calculate_sum(void) const;
225 
226 
228 
229  T calculate_product(void) const;
230 
231  double calculate_mean(void) const;
232 
233  double calculate_standard_deviation(void) const;
234 
236 
237  double calculate_mean_missing_values(const Vector<size_t>&) const;
238 
240 
242 
244 
245  // Norm methods
246 
247  double calculate_norm(void) const;
248 
250 
251  Matrix<T> calculate_norm_Hessian(void) const;
252 
253  double calculate_p_norm(const double&) const;
254 
255  Vector<double> calculate_p_norm_gradient(const double&) const;
256 
257  Vector<T> calculate_normalized(void) const;
258 
259  double calculate_distance(const Vector<double>&) const;
260 
261  double calculate_sum_squared_error(const Vector<double>&) const;
262 
263  double calculate_Minkowski_error(const Vector<double>&, const double&) const;
264 
265  // Correlation methods
266 
267  T calculate_linear_correlation(const Vector<T>&) const;
268 
270 
272 
274 
275  void apply_absolute_value(void);
276 
277  // Bounding methods
278 
279  Vector<T> calculate_lower_bounded(const T&) const;
280 
282 
283  Vector<T> calculate_upper_bounded(const T&) const;
284 
286 
287  Vector<T> calculate_lower_upper_bounded(const T&, const T&) const;
288 
290 
291  void apply_lower_bound(const T&);
292 
293  void apply_lower_bound(const Vector<T>&);
294 
295  void apply_upper_bound(const T&);
296 
297  void apply_upper_bound(const Vector<T>&);
298 
299  void apply_lower_upper_bounds(const T&, const T&);
300 
301  void apply_lower_upper_bounds(const Vector<T>&, const Vector<T>&);
302 
303  // Rank methods
304 
306 
308 
309  // Mathematical operators
310 
311  inline Vector<T> operator + (const T&) const;
312 
313  inline Vector<T> operator + (const Vector<T>&) const;
314 
315  inline Vector<T> operator - (const T&) const;
316 
317  inline Vector<T> operator - (const Vector<T>&) const;
318 
319  inline Vector<T> operator * (const T&) const;
320 
321  inline Vector<T> operator * (const Vector<T>&) const;
322 
323  inline Matrix<T> operator * (const Matrix<T>&) const;
324 
325  double dot(const Vector<double>&) const;
326 
327  Vector<double> dot(const Matrix<T>&) const;
328 
329  Matrix<T> direct(const Vector<T>&) const;
330 
331  Vector<T> operator / (const T&) const;
332 
333  Vector<T> operator / (const Vector<T>&) const;
334 
335  void operator += (const T&);
336 
337  void operator += (const Vector<T>&);
338 
339  void operator -= (const T&);
340 
341  void operator -= (const Vector<T>&);
342 
343  void operator *= (const T&);
344 
345  void operator *= (const Vector<T>&);
346 
347  void operator /= (const T&);
348 
349  void operator /= (const Vector<T>&);
350 
351  // Filtering methods
352 
353  void filter_positive(void);
354  void filter_negative(void);
355 
356  // Scaling methods
357 
358  void scale_minimum_maximum(const T&, const T&);
359 
361 
363 
364  void scale_mean_standard_deviation(const T&, const T&);
365 
367 
369 
370  void scale_minimum_maximum(const Vector<T>&, const Vector<T>&);
371 
373 
375 
377 
378  // Unscaling methods
379 
381 
383 
384  void unscale_minimum_maximum(const Vector<T>&, const Vector<T>&);
385 
387 
388  // Arranging methods
389 
391 
393 
394  Vector<T> arrange_subvector_first(const size_t&) const;
395 
396  Vector<T> arrange_subvector_last(const size_t&) const;
397 
398  // File operations
399 
400  void load(const std::string&);
401 
402  void save(const std::string&) const;
403 
404  void tuck_in(const size_t&, const Vector<T>&);
405 
406  Vector<T> take_out(const size_t&, const size_t&) const;
407 
408  Vector<T> insert_element(const size_t&, const T&) const;
409  Vector<T> remove_element(const size_t&) const;
410 
411  Vector<T> remove_value(const T&) const;
412 
413  Vector<T> assemble(const Vector<T>&) const;
414 
415  std::vector<T> to_std_vector(void) const;
416 
417  Matrix<T> to_row_matrix(void) const;
418 
419  Matrix<T> to_column_matrix(void) const;
420 
421  void parse(const std::string&);
422 
423  std::string to_text() const;
424 
425  std::string to_string(const std::string& = " ") const;
426 
427  Vector<std::string> write_string_vector(const size_t& = 3) const;
428 
429  Matrix<T> to_matrix(const size_t&, const size_t&) const;
430 
431 };
432 
433 
434 // CONSTRUCTORS
435 
437 
438 template <class T>
439 Vector<T>::Vector(void) : std::vector<T>()
440 {
441 }
442 
443 
446 
447 template <class T>
448 Vector<T>::Vector(const size_t& new_size) : std::vector<T>(new_size)
449 {
450 }
451 
452 
457 
458 template <class T>
459 Vector<T>::Vector(const size_t& new_size, const T& value) : std::vector<T>(new_size, value)
460 {
461 }
462 
463 
466 
467 template <class T>
468 Vector<T>::Vector(const std::string& file_name) : std::vector<T>()
469 {
470  load(file_name);
471 }
472 
473 
475 
476 template <class T>
477 Vector<T>::Vector(const T& first, const double& step, const T& last) : std::vector<T>()
478 {
479  set(first, step, last);
480 }
481 
482 
484 
485 template<class T>
486 template<class InputIterator>
487 Vector<T>::Vector(InputIterator first, InputIterator last) : std::vector<T>(first, last)
488 {
489 }
490 
491 
494 
495 template <class T>
496 Vector<T>::Vector(const Vector<T>& other_vector) : std::vector<T>(other_vector)
497 {
498 }
499 
500 
501 // DESTRUCTOR
502 
504 template <class T>
506 {
507 }
508 
509 
510 // bool == (const T&) const
511 
515 
516 template <class T>
517 bool Vector<T>::operator == (const T& value) const
518 {
519  const size_t this_size = this->size();
520 
521  for(size_t i = 0; i < this_size; i++)
522  {
523  if((*this)[i] != value)
524  {
525  return(false);
526  }
527  }
528 
529  return(true);
530 }
531 
532 
533 // bool operator != (const T&) const
534 
538 
539 template <class T>
540 bool Vector<T>::operator != (const T& value) const
541 {
542  const size_t this_size = this->size();
543 
544  for(size_t i = 0; i < this_size; i++)
545  {
546  if((*this)[i] != value)
547  {
548  return(true);
549  }
550  }
551 
552  return(false);
553 }
554 
555 
556 // bool operator > (const T&) const
557 
561 
562 template <class T>
563 bool Vector<T>::operator > (const T& value) const
564 {
565  const size_t this_size = this->size();
566 
567  for(size_t i = 0; i < this_size; i++)
568  {
569  if((*this)[i] <= value)
570  {
571  return(false);
572  }
573  }
574 
575  return(true);
576 }
577 
578 
579 // bool operator < (const T&) const
580 
584 
585 template <class T>
586 bool Vector<T>::operator < (const T& value) const
587 {
588  const size_t this_size = this->size();
589 
590  for(size_t i = 0; i < this_size; i++)
591  {
592  if((*this)[i] >= value)
593  {
594  return(false);
595  }
596  }
597 
598  return(true);
599 }
600 
601 
602 // bool operator >= (const T&) const
603 
607 
608 template <class T>
609 bool Vector<T>::operator >= (const T& value) const
610 {
611  const size_t this_size = this->size();
612 
613  for(size_t i = 0; i < this_size; i++)
614  {
615  if((*this)[i] < value)
616  {
617  return(false);
618  }
619  }
620 
621  return(true);
622 }
623 
624 
625 // bool operator <= (const T&) const
626 
630 
631 template <class T>
632 bool Vector<T>::operator <= (const T& value) const
633 {
634  const size_t this_size = this->size();
635 
636 
637  for(size_t i = 0; i < this_size; i++)
638  {
639  if((*this)[i] > value)
640  {
641  return(false);
642  }
643  }
644 
645  return(true);
646 }
647 
648 
649 // METHODS
650 
651 // void set(void) method
652 
654 
655 template <class T>
656 void Vector<T>::set(void)
657 {
658  this->resize(0);
659 }
660 
661 
662 // void set(const size_t&) method
663 
666 
667 template <class T>
668 void Vector<T>::set(const size_t& new_size)
669 {
670  this->resize(new_size);
671 }
672 
673 
674 // void set(const size_t&, const T&) method
675 
679 
680 template <class T>
681 void Vector<T>::set(const size_t& new_size, const T& new_value)
682 {
683  this->resize(new_size);
684 
685  initialize(new_value);
686 }
687 
688 
689 // void set(const std::string&) method
690 
694 
695 template <class T>
696 void Vector<T>::set(const std::string& file_name)
697 {
698  load(file_name);
699 }
700 
701 
702 // void set(const T&, const double&, const T&) method
703 
709 
710 template <class T>
711 void Vector<T>::set(const T& first, const double& step, const T& last)
712 {
713  if(first > last && step > 0 )
714  {
715  this->resize(0);
716  }
717  else if(first < last && step < 0)
718  {
719  this->resize(0);
720  }
721  else
722  {
723  const size_t new_size = 1 + (size_t)((last - first)/step + 0.5);
724 
725  this->resize(new_size);
726 
727  for(size_t i = 0; i < new_size; i++)
728  {
729  (*this)[i] = first + (T)(i*step);
730  }
731  }
732 }
733 
734 
735 // void set(const Vector&) method
736 
739 
740 template <class T>
741 void Vector<T>::set(const Vector& other_vector)
742 {
743  *this = other_vector;
744 }
745 
746 
747 // void initialize(const T&) method
748 
751 
752 template <class T>
753 void Vector<T>::initialize(const T& value)
754 {
755  std::fill((*this).begin(), (*this).end(), value);
756 }
757 
758 
759 // void initialize_sequential(void) method
760 
762 
763 template <class T>
765 {
766  for(size_t i = 0; i < this->size(); i++)
767  {
768  (*this)[i] = (T)i;
769  }
770 }
771 
772 
773 // void randomize_uniform(const double&, const double&) method
774 
779 
780 template <class T>
781 void Vector<T>::randomize_uniform(const double& minimum, const double& maximum)
782 {
783  // Control sentence (if debug)
784 
785  #ifndef NDEBUG
786 
787  if(minimum > maximum)
788  {
789  std::ostringstream buffer;
790 
791  buffer << "OpenNN Exception: Vector Template.\n"
792  << "void randomize_uniform(const double&, const double&) method.\n"
793  << "Minimum value must be less or equal than maximum value.\n";
794 
795  throw std::logic_error(buffer.str());
796  }
797 
798  #endif
799 
800  const size_t this_size = this->size();
801 
802  for(size_t i = 0; i < this_size; i++)
803  {
804  (*this)[i] = (T)calculate_random_uniform(minimum, maximum);
805  }
806 }
807 
808 
809 // void randomize_uniform(const Vector<double>&, const Vector<double>&) method
810 
815 
816 template <class T>
817 void Vector<T>::randomize_uniform(const Vector<double>& minimums, const Vector<double>& maximums)
818 {
819  const size_t this_size = this->size();
820 
821  // Control sentence (if debug)
822 
823  #ifndef NDEBUG
824 
825  const size_t minimums_size = minimums.size();
826  const size_t maximums_size = maximums.size();
827 
828  if(minimums_size != this_size || maximums_size != this_size)
829  {
830  std::ostringstream buffer;
831 
832  buffer << "OpenNN Exception: Vector Template.\n"
833  << "void randomize_uniform(const Vector<double>&, const Vector<double>&) method.\n"
834  << "Minimum and maximum sizes must be equal to vector size.\n";
835 
836  throw std::logic_error(buffer.str());
837  }
838 
839  if(minimums > maximums)
840  {
841  std::ostringstream buffer;
842 
843  buffer << "OpenNN Exception: Vector Template.\n"
844  << "void randomize_uniform(const Vector<double>&, const Vector<double>&) method.\n"
845  << "Minimum values must be less or equal than their corresponding maximum values.\n";
846 
847  throw std::logic_error(buffer.str());
848  }
849 
850  #endif
851 
852  for(size_t i = 0; i < this_size; i++)
853  {
854  (*this)[i] = calculate_random_uniform(minimums[i], maximums[i]);
855  }
856 }
857 
858 
859 // void randomize_normal(const double&, const double&) method
860 
865 
866 template <class T>
867 void Vector<T>::randomize_normal(const double& mean, const double& standard_deviation)
868 {
869  // Control sentence (if debug)
870 
871  #ifndef NDEBUG
872 
873  if(standard_deviation < 0.0)
874  {
875  std::ostringstream buffer;
876 
877  buffer << "OpenNN Exception: Vector Template.\n"
878  << "void randomize_normal(const double&, const double&) method.\n"
879  << "Standard deviation must be equal or greater than zero.\n";
880 
881  throw std::logic_error(buffer.str());
882  }
883 
884  #endif
885 
886  const size_t this_size = this->size();
887 
888  for(size_t i = 0; i < this_size; i++)
889  {
890  (*this)[i] = calculate_random_normal(mean, standard_deviation);
891  }
892 }
893 
894 
895 // void randomize_normal(const Vector<double>, const Vector<double>) method
896 
901 
902 template <class T>
903 void Vector<T>::randomize_normal(const Vector<double>& mean, const Vector<double>& standard_deviation)
904 {
905  const size_t this_size = this->size();
906 
907  // Control sentence (if debug)
908 
909  #ifndef NDEBUG
910 
911  const size_t mean_size = mean.size();
912  const size_t standard_deviation_size = standard_deviation.size();
913 
914  if(mean_size != this_size || standard_deviation_size != this_size)
915  {
916  std::ostringstream buffer;
917 
918  buffer << "OpenNN Exception: Vector Template.\n"
919  << "void randomize_normal(const Vector<double>&, const Vector<double>&) method.\n"
920  << "Mean and standard deviation sizes must be equal to vector size.\n";
921 
922  throw std::logic_error(buffer.str());
923  }
924 
925  if(standard_deviation < 0.0)
926  {
927  std::ostringstream buffer;
928 
929  buffer << "OpenNN Exception: Vector Template.\n"
930  << "void randomize_normal(const Vector<double>&, const Vector<double>&) method.\n"
931  << "Standard deviations must be equal or greater than zero.\n";
932 
933  throw std::logic_error(buffer.str());
934  }
935 
936  #endif
937 
938  for(size_t i = 0; i < this_size; i++)
939  {
940  (*this)[i] = calculate_random_normal(mean[i], standard_deviation[i]);
941  }
942 }
943 
944 
945 // bool contains(const T&) const method
946 
948 
949 template <class T>
950 bool Vector<T>::contains(const T& value) const
951 {
952  const size_t this_size = this->size();
953 
954  for(size_t i = 0; i < this_size; i++)
955  {
956  if((*this)[i] == value)
957  {
958  return(true);
959  }
960  }
961 
962  return(false);
963 }
964 
965 
966 // bool contains(const Vector<T>&) const method
967 
969 
970 template <class T>
971 bool Vector<T>::contains(const Vector<T>& values) const
972 {
973  if(values.empty())
974  {
975  return(false);
976  }
977 
978  const size_t this_size = this->size();
979 
980  const size_t values_size = values.size();
981 
982  for(size_t i = 0; i < this_size; i++)
983  {
984  for(size_t j = 0; j < values_size; j++)
985  {
986  if((*this)[i] == values[j])
987  {
988  return(true);
989  }
990  }
991  }
992 
993  return(false);
994 }
995 
996 
997 // bool is_in(const T&, const T&) const method
998 
1003 
1004 template <class T>
1005 bool Vector<T>::is_in(const T& minimum, const T& maximum) const
1006 {
1007  const size_t this_size = this->size();
1008 
1009  for(size_t i = 0; i < this_size; i++)
1010  {
1011  if((*this)[i] < minimum || (*this)[i] > maximum)
1012  {
1013  return(false);
1014  }
1015  }
1016 
1017  return(true);
1018 }
1019 
1020 
1021 // bool is_constant(const double&) const method
1022 
1026 
1027 template <class T>
1028 bool Vector<T>::is_constant(const double& tolerance) const
1029 {
1030  const size_t this_size = this->size();
1031 
1032  if(this_size == 0)
1033  {
1034  return(false);
1035  }
1036 
1037  const T minimum = calculate_minimum();
1038  const T maximum = calculate_maximum();
1039 
1040  if(fabs(maximum-minimum) <= tolerance)
1041  {
1042  return(true);
1043  }
1044  else
1045  {
1046  return(false);
1047  }
1048 }
1049 
1050 
1051 // bool is_crescent(void) const method
1052 
1054 
1055 template <class T>
1056 bool Vector<T>::is_crescent(void) const
1057 {
1058  for(size_t i = 0; i < this->size()-1; i++)
1059  {
1060  if((*this)[i] > (*this)[i+1])
1061  {
1062  return(false);
1063  }
1064  }
1065 
1066  return(true);
1067 }
1068 
1069 
1070 // bool is_decrescent(void) const method
1071 
1073 
1074 template <class T>
1076 {
1077  for(size_t i = 0; i < this->size()-1; i++)
1078  {
1079  if((*this)[i] < (*this)[i+1])
1080  {
1081  return(false);
1082  }
1083  }
1084 
1085  return(true);
1086 }
1087 
1088 
1089 // size_t count_occurrences(const T&) const method
1090 
1092 
1093 template <class T>
1094 size_t Vector<T>::count_occurrences(const T& value) const
1095 {
1096  const size_t this_size = this->size();
1097 
1098  size_t count = 0;
1099 
1100  for(size_t i = 0; i < this_size; i++)
1101  {
1102  if((*this)[i] == value)
1103  {
1104  count++;
1105  }
1106  }
1107 
1108  return(count);
1109 }
1110 
1111 
1112 // Vector<size_t> calculate_occurrence_indices(const T&) const method
1113 
1116 
1117 template <class T>
1119 {
1120  const size_t this_size = this->size();
1121 
1122  const size_t occurrences_number = count_occurrences(value);
1123 
1124  Vector<size_t> occurrence_indices(occurrences_number);
1125 
1126  size_t index = 0;
1127 
1128  for(size_t i = 0; i < this_size; i++)
1129  {
1130  if((*this)[i] == value)
1131  {
1132  occurrence_indices[index] = i;
1133  index++;
1134  }
1135  }
1136 
1137  return(occurrence_indices);
1138 }
1139 
1140 
1141 // size_t count_greater_than(const T&) const method
1142 
1145 
1146 template <class T>
1147 size_t Vector<T>::count_greater_than(const T& value) const
1148 {
1149  const size_t this_size = this->size();
1150 
1151  size_t count = 0;
1152 
1153  for(size_t i = 0; i < this_size; i++)
1154  {
1155  if((*this)[i] > value)
1156  {
1157  count++;
1158  }
1159  }
1160 
1161  return(count);
1162 }
1163 
1164 
1165 // size_t count_less_than(const T&) const method
1166 
1169 
1170 template <class T>
1171 size_t Vector<T>::count_less_than(const T& value) const
1172 {
1173  const size_t this_size = this->size();
1174 
1175  size_t count = 0;
1176 
1177  for(size_t i = 0; i < this_size; i++)
1178  {
1179  if((*this)[i] < value)
1180  {
1181  count++;
1182  }
1183  }
1184 
1185  return(count);
1186 }
1187 
1188 
1189 // Vector<size_t> calculate_less_than_indices(const T&) const method
1190 
1193 
1194 template <class T>
1196 {
1197  const size_t this_size = this->size();
1198 
1199  Vector<size_t> less_than_indices;
1200 
1201  for(size_t i = 0; i < this_size; i++)
1202  {
1203  if((*this)[i] < value)
1204  {
1205  less_than_indices.push_back(i);
1206  }
1207  }
1208 
1209  return(less_than_indices);
1210 }
1211 
1212 
1213 // Vector<size_t> calculate_greater_than_indices(const T&) const method
1214 
1217 
1218 template <class T>
1220 {
1221  const size_t this_size = this->size();
1222 
1223  Vector<size_t> greater_than_indices;
1224 
1225  for(size_t i = 0; i < this_size; i++)
1226  {
1227  if((*this)[i] > value)
1228  {
1229  greater_than_indices.push_back(i);
1230  }
1231  }
1232 
1233  return(greater_than_indices);
1234 }
1235 
1236 
1237 // T calculate_minimum(void) const method
1238 
1240 
1241 template <class T>
1243 {
1244  const size_t this_size = this->size();
1245 
1246  T minimum = std::numeric_limits<T>::max();
1247 
1248  for(size_t i = 0; i < this_size; i++)
1249  {
1250  if((*this)[i] < minimum)
1251  {
1252  minimum = (*this)[i];
1253  }
1254  }
1255 
1256  return(minimum);
1257 }
1258 
1259 
1260 // T calculate_maximum(void) const method
1261 
1263 
1264 template <class T>
1266 {
1267  const size_t this_size = this->size();
1268 
1269  T maximum = std::numeric_limits<T>::max();
1270 
1271  if(std::numeric_limits<T>::is_signed)
1272  {
1273  maximum *= -1;
1274  }
1275  else
1276  {
1277  maximum = 0;
1278  }
1279 
1280  for(size_t i = 0; i < this_size; i++)
1281  {
1282  if((*this)[i] > maximum)
1283  {
1284  maximum = (*this)[i];
1285  }
1286  }
1287 
1288  return(maximum);
1289 }
1290 
1291 
1292 // Vector<T> calculate_minimum_maximum(void) const method
1293 
1295 
1296 template <class T>
1298 {
1299  size_t this_size = this->size();
1300 
1301  T minimum = std::numeric_limits<T>::max();
1302 
1303  T maximum;
1304 
1305  if(std::numeric_limits<T>::is_signed)
1306  {
1307  maximum = -std::numeric_limits<T>::max();
1308  }
1309  else
1310  {
1311  maximum = 0;
1312  }
1313 
1314  for(size_t i = 0; i < this_size; i++)
1315  {
1316  if((*this)[i] < minimum)
1317  {
1318  minimum = (*this)[i];
1319  }
1320 
1321  if((*this)[i] > maximum)
1322  {
1323  maximum = (*this)[i];
1324  }
1325  }
1326 
1327  Vector<T> minimum_maximum(2);
1328  minimum_maximum[0] = minimum;
1329  minimum_maximum[1] = maximum;
1330 
1331  return(minimum_maximum);
1332 }
1333 
1334 
1335 // T calculate_minimum_missing_values(const Vector<size_t>&) const method
1336 
1338 
1339 template <class T>
1341 {
1342  const size_t this_size = this->size();
1343 
1344  T minimum = std::numeric_limits<T>::max();
1345 
1346  for(size_t i = 0; i < this_size; i++)
1347  {
1348  if((*this)[i] < minimum && !missing_indices.contains(i))
1349  {
1350  minimum = (*this)[i];
1351  }
1352  }
1353 
1354  return(minimum);
1355 }
1356 
1357 
1358 // T calculate_maximum_missing_values(const Vector<size_t>&) const method
1359 
1361 
1362 template <class T>
1364 {
1365  const size_t this_size = this->size();
1366 
1367  T maximum;
1368 
1369  if(std::numeric_limits<T>::is_signed)
1370  {
1371  maximum = -std::numeric_limits<T>::max();
1372  }
1373  else
1374  {
1375  maximum = 0;
1376  }
1377 
1378  for(size_t i = 0; i < this_size; i++)
1379  {
1380  if((*this)[i] > maximum && !missing_indices.contains(i))
1381  {
1382  maximum = (*this)[i];
1383  }
1384  }
1385 
1386  return(maximum);
1387 }
1388 
1389 
1390 // Vector<T> calculate_minimum_maximum_missing_values(const Vector<size_t>&) const method
1391 
1393 
1394 template <class T>
1396 {
1397  size_t this_size = this->size();
1398 
1399  T minimum = std::numeric_limits<T>::max();
1400 
1401  T maximum;
1402 
1403  if(std::numeric_limits<T>::is_signed)
1404  {
1405  maximum = -std::numeric_limits<T>::max();
1406  }
1407  else
1408  {
1409  maximum = 0;
1410  }
1411 
1412  for(size_t i = 0; i < this_size; i++)
1413  {
1414  if(!missing_indices.contains(i))
1415  {
1416  if((*this)[i] < minimum)
1417  {
1418  minimum = (*this)[i];
1419  }
1420 
1421  if((*this)[i] > maximum)
1422  {
1423  maximum = (*this)[i];
1424  }
1425  }
1426  }
1427 
1428  Vector<T> minimum_maximum(2);
1429  minimum_maximum[0] = minimum;
1430  minimum_maximum[1] = maximum;
1431 
1432  return(minimum_maximum);
1433 }
1434 
1435 
1436 // Histogram<T> calculate_histogram(const size_t&) const method
1437 
1443 
1444 template <class T>
1445 Histogram<T> Vector<T>::calculate_histogram(const size_t& bins_number) const
1446 {
1447  // Control sentence (if debug)
1448 
1449  #ifndef NDEBUG
1450 
1451  if(bins_number < 1)
1452  {
1453  std::ostringstream buffer;
1454 
1455  buffer << "OpenNN Exception: Vector Template.\n"
1456  << "Histogram<T> calculate_histogram(const size_t&) const method.\n"
1457  << "Number of bins is less than one.\n";
1458 
1459  throw std::logic_error(buffer.str());
1460  }
1461 
1462  #endif
1463 
1464  Vector<T> minimums(bins_number);
1465  Vector<T> maximums(bins_number);
1466 
1467  Vector<T> centers(bins_number);
1468  Vector<size_t> frequencies(bins_number, 0);
1469 
1470  const Vector<T> minimum_maximum = calculate_minimum_maximum();
1471 
1472  const T minimum = minimum_maximum[0];
1473  const T maximum = minimum_maximum[1];
1474 
1475  const double length = (maximum-minimum)/(double)bins_number;
1476 
1477  minimums[0] = minimum;
1478  maximums[0] = minimum + length;
1479  centers[0] = (maximums[0] + minimums[0])/2.0;
1480 
1481  // Calculate bins center
1482 
1483  for(size_t i = 1; i < bins_number; i++)
1484  {
1485  minimums[i] = minimums[i-1] + length;
1486  maximums[i] = maximums[i-1] + length;
1487 
1488  centers[i] = (maximums[i] + minimums[i])/2.0;
1489  }
1490 
1491  // Calculate bins frequency
1492 
1493  const size_t this_size = this->size();
1494 
1495  for(size_t i = 0; i < this_size; i++)
1496  {
1497  if((*this)[i] < centers[0])
1498  {
1499  frequencies[0]++;
1500  }
1501 
1502  for(size_t j = 1; j < bins_number-1; j++)
1503  {
1504  if((*this)[i] >= minimums[j] && (*this)[i] < maximums[j])
1505  {
1506  frequencies[j]++;
1507  }
1508  }
1509 
1510  if((*this)[i] >= centers[bins_number-1])
1511  {
1512  frequencies[bins_number-1]++;
1513  }
1514  }
1515 
1516  Histogram<T> histogram(bins_number);
1517  histogram.centers = centers;
1518  histogram.frequencies = frequencies;
1519 
1520  return(histogram);
1521 }
1522 
1523 
1524 // Histogram<T> calculate_histogram_missing_values(const size_t&) const method
1525 
1531 
1532 template <class T>
1533 Histogram<T> Vector<T>::calculate_histogram_missing_values(const Vector<size_t>& missing_indices, const size_t& bins_number) const
1534 {
1535  // Control sentence (if debug)
1536 
1537  #ifndef NDEBUG
1538 
1539  if(bins_number < 1)
1540  {
1541  std::ostringstream buffer;
1542 
1543  buffer << "OpenNN Exception: Vector Template.\n"
1544  << "Histogram<T> calculate_histogram_missing_values(const Vector<size_t>&, const size_t&) const method.\n"
1545  << "Number of bins is less than one.\n";
1546 
1547  throw std::logic_error(buffer.str());
1548  }
1549 
1550  #endif
1551 
1552  Vector<T> minimums(bins_number);
1553  Vector<T> maximums(bins_number);
1554 
1555  Vector<T> centers(bins_number);
1556  Vector<size_t> frequencies(bins_number, 0);
1557 
1558  const Vector<T> minimum_maximum = calculate_minimum_maximum_missing_values(missing_indices);
1559 
1560  const T minimum = minimum_maximum[0];
1561  const T maximum = minimum_maximum[1];
1562 
1563  const double length = (maximum-minimum)/(double)bins_number;
1564 
1565  minimums[0] = minimum;
1566  maximums[0] = minimum + length;
1567  centers[0] = (maximums[0] + minimums[0])/2.0;
1568 
1569  // Calculate bins center
1570 
1571  for(size_t i = 1; i < bins_number; i++)
1572  {
1573  minimums[i] = minimums[i-1] + length;
1574  maximums[i] = maximums[i-1] + length;
1575 
1576  centers[i] = (maximums[i] + minimums[i])/2.0;
1577  }
1578 
1579  // Calculate bins frequency
1580 
1581  const size_t this_size = this->size();
1582 
1583  for(size_t i = 0; i < this_size; i++)
1584  {
1585  if(!missing_indices.contains(i))
1586  {
1587  if((*this)[i] < centers[0])
1588  {
1589  frequencies[0]++;
1590  }
1591 
1592  for(size_t j = 1; j < bins_number-1; j++)
1593  {
1594  if((*this)[i] >= minimums[j] && (*this)[i] < maximums[j])
1595  {
1596  frequencies[j]++;
1597  }
1598  }
1599 
1600  if((*this)[i] >= centers[bins_number-1])
1601  {
1602  frequencies[bins_number-1]++;
1603  }
1604  }
1605  }
1606 
1607  Histogram<T> histogram(bins_number);
1608  histogram.centers = centers;
1609  histogram.frequencies = frequencies;
1610 
1611  return(histogram);
1612 }
1613 
1614 
1615 // size_t calculate_minimal_index(void) const method
1616 
1618 
1619 template <class T>
1621 {
1622  const size_t this_size = this->size();
1623 
1624  T minimum = (*this)[0];
1625  size_t minimal_index = 0;
1626 
1627  for(size_t i = 1; i < this_size; i++)
1628  {
1629  if((*this)[i] < minimum)
1630  {
1631  minimum = (*this)[i];
1632  minimal_index = i;
1633  }
1634  }
1635 
1636  return(minimal_index);
1637 }
1638 
1639 
1640 // size_t calculate_maximal_index(void) const method
1641 
1643 
1644 template <class T>
1646 {
1647  const size_t this_size = this->size();
1648 
1649  T maximum = (*this)[0];
1650  size_t maximal_index = 0;
1651 
1652  for(size_t i = 1; i < this_size; i++)
1653  {
1654  if((*this)[i] > maximum)
1655  {
1656  maximum = (*this)[i];
1657  maximal_index = i;
1658  }
1659  }
1660 
1661  return(maximal_index);
1662 }
1663 
1664 
1665 // Vector<size_t> calculate_minimal_indices(const size_t&) const method
1666 
1669 
1670 template <class T>
1672 {
1673  const size_t this_size = this->size();
1674 
1675  const Vector<size_t> rank = calculate_less_rank();
1676 
1677  Vector<size_t> minimal_indices(number);
1678 
1679  for(size_t i = 0; i < this_size; i++)
1680  {
1681  for(size_t j = 0; j < number; j++)
1682  {
1683  if(rank[i] == j)
1684  {
1685  minimal_indices[j] = i;
1686  }
1687  }
1688  }
1689 
1690  return(minimal_indices);
1691 }
1692 
1693 
1694 // Vector<size_t> calculate_maximal_indices(const size_t&) const method
1695 
1698 
1699 template <class T>
1701 {
1702  const size_t this_size = this->size();
1703 
1704  const Vector<size_t> rank = calculate_greater_rank();
1705 
1706  Vector<size_t> maximal_indices(number);
1707 
1708  for(size_t i = 0; i < this_size; i++)
1709  {
1710  for(size_t j = 0; j < number; j++)
1711  {
1712  if(rank[i] == j)
1713  {
1714  maximal_indices[j] = i;
1715  }
1716  }
1717  }
1718 
1719  return(maximal_indices);
1720 }
1721 
1722 
1723 // Vector<size_t> calculate_minimal_maximal_index(void) const method
1724 
1726 
1727 template <class T>
1729 {
1730  const size_t this_size = this->size();
1731 
1732  T minimum = (*this)[0];
1733  T maximum = (*this)[0];
1734 
1735  size_t minimal_index = 0;
1736  size_t maximal_index = 0;
1737 
1738  for(size_t i = 1; i < this_size; i++)
1739  {
1740  if((*this)[i] < minimum)
1741  {
1742  minimum = (*this)[i];
1743  minimal_index = i;
1744  }
1745  if((*this)[i] > maximum)
1746  {
1747  maximum = (*this)[i];
1748  maximal_index = i;
1749  }
1750  }
1751 
1752  Vector<size_t> minimal_maximal_index(2);
1753  minimal_maximal_index[0] = minimal_index;
1754  minimal_maximal_index[1] = maximal_index;
1755 
1756  return(minimal_maximal_index);
1757 }
1758 
1759 
1760 // Vector<double> calculate_pow(const T&) const method
1761 
1764 
1765 template <class T>
1766 Vector<T> Vector<T>::calculate_pow(const T& exponent) const
1767 {
1768  const size_t this_size = this->size();
1769 
1770  Vector<T> power(this_size);
1771 
1772  for(size_t i = 0; i < this_size; i++)
1773  {
1774  power[i] = pow((*this)[i], exponent);
1775  }
1776 
1777  return(power);
1778 }
1779 
1780 
1781 // Vector<double> calculate_competitive(void) const method
1782 
1785 
1786 template <class T>
1788 {
1789  const size_t this_size = this->size();
1790 
1791  Vector<T> competitive(this_size, 0);
1792 
1793  const size_t maximal_index = calculate_maximal_index();
1794 
1795  competitive[maximal_index] = 1;
1796 
1797  return(competitive);
1798 }
1799 
1800 
1801 // Vector<T> calculate_softmax(void) const method
1802 
1805 
1806 template <class T>
1808 {
1809  const size_t this_size = this->size();
1810 
1811  Vector<T> softmax(this_size);
1812 
1813  T sum = 0;
1814 
1815  for(size_t i = 0; i < this_size; i++)
1816  {
1817  sum += exp((*this)[i]);
1818  }
1819 
1820  for(size_t i = 0; i < this_size; i++)
1821  {
1822  softmax[i] = exp((*this)[i])/sum;
1823  }
1824 
1825  return(softmax);
1826 }
1827 
1828 
1829 // Matrix<T> calculate_softmax_Jacobian(void) const method
1830 
1832 
1833 template <class T>
1835 {
1836  const size_t this_size = this->size();
1837 
1838  Matrix<T> softmax_Jacobian(this_size, this_size);
1839 
1840  for(size_t i = 0; i < this_size; i++)
1841  {
1842  for(size_t j = 0; j < this_size; j++)
1843  {
1844  if(i == j)
1845  {
1846  softmax_Jacobian(i,i) = (*this)[i]*(1.0 - (*this)[i]);
1847  }
1848  else
1849  {
1850  softmax_Jacobian(i,i) = (*this)[i]*(*this)[j];
1851  }
1852  }
1853  }
1854 
1855  return(softmax_Jacobian);
1856 }
1857 
1858 
1859 // Vector<bool> calculate_binary(void) const method
1860 
1863 
1864 template <class T>
1866 {
1867  const size_t this_size = this->size();
1868 
1869  Vector<bool> binary(this_size);
1870 
1871  for(size_t i = 0; i < this_size; i++)
1872  {
1873  if((*this)[i] < 0.5)
1874  {
1875  binary[i] = false;
1876  }
1877  else
1878  {
1879  binary[i] = true;
1880  }
1881  }
1882 
1883  return(binary);
1884 }
1885 
1886 
1887 // Vector<T> calculate_cumulative(void) const method
1888 
1891 
1892 template <class T>
1894 {
1895  const size_t this_size = this->size();
1896 
1897  Vector<T> cumulative(this_size);
1898 
1899  if(this_size > 0)
1900  {
1901  cumulative[0] = (*this)[0];
1902 
1903  for(size_t i = 1; i < this_size; i++)
1904  {
1905  cumulative[i] = cumulative[i-1] + (*this)[i];
1906  }
1907  }
1908 
1909  return(cumulative);
1910 }
1911 
1912 
1913 // size_t calculate_cumulative_index(const T&) const method
1914 
1918 
1919 template <class T>
1920 size_t Vector<T>::calculate_cumulative_index(const T& value) const
1921 {
1922  const size_t this_size = this->size();
1923 
1924  // Control sentence (if debug)
1925 
1926  #ifndef NDEBUG
1927 
1928  if(this_size == 0)
1929  {
1930  std::ostringstream buffer;
1931 
1932  buffer << "OpenNN Exception: Vector Template.\n"
1933  << "size_t calculate_cumulative_index(const T&) const.\n"
1934  << "Size must be greater than zero.\n";
1935 
1936  throw std::logic_error(buffer.str());
1937  }
1938 
1939  T cumulative_value = (*this)[this_size-1];
1940 
1941  if(value > cumulative_value)
1942  {
1943  std::ostringstream buffer;
1944 
1945  buffer << "OpenNN Exception: Vector Template.\n"
1946  << "size_t calculate_cumulative_index(const T&) const.\n"
1947  << "Value (" << value << ") must be less than cumulative value (" << cumulative_value << ").\n";
1948 
1949  throw std::logic_error(buffer.str());
1950  }
1951 
1952  for(size_t i = 1; i < this_size; i++)
1953  {
1954  if((*this)[i] < (*this)[i-1])
1955  {
1956  std::ostringstream buffer;
1957 
1958  buffer << "OpenNN Exception: Vector Template.\n"
1959  << "int calculate_cumulative_index(const T&) const.\n"
1960  << "Vector elements must be crescent.\n";
1961 
1962  throw std::logic_error(buffer.str());
1963  }
1964  }
1965 
1966  #endif
1967 
1968  if(value <= (*this)[0])
1969  {
1970  return(0);
1971  }
1972 
1973  for(size_t i = 1; i < this_size; i++)
1974  {
1975  if(value > (*this)[i-1] && value <= (*this)[i])
1976  {
1977  return(i);
1978  }
1979  }
1980 
1981  return(this_size-1);
1982 }
1983 
1984 
1985 // size_t calculate_closest_index(const T&) const method
1986 
1988 
1989 template <class T>
1990 size_t Vector<T>::calculate_closest_index(const T& value) const
1991 {
1992  const Vector<T> difference = (*this - value).calculate_absolute_value();
1993 
1994  const size_t closest_index = difference.calculate_minimal_index();
1995 
1996  return(closest_index);
1997 }
1998 
1999 
2000 // T calculate_sum(void) const method
2001 
2003 
2004 template <class T>
2006 {
2007  const size_t this_size = this->size();
2008 
2009  T sum = 0;
2010 
2011  for(size_t i = 0; i < this_size; i++)
2012  {
2013  sum += (*this)[i];
2014  }
2015 
2016  return(sum);
2017 }
2018 
2019 
2020 // T calculate_sum_missing_values(const Vector<size_t>&) const method
2021 
2023 
2024 template <class T>
2026 {
2027  const size_t this_size = this->size();
2028 
2029  T sum = 0;
2030 
2031  for(size_t i = 0; i < this_size; i++)
2032  {
2033  if(!missing_indices.contains(i))
2034  {
2035  sum += (*this)[i];
2036  }
2037  }
2038 
2039  return(sum);
2040 }
2041 
2042 
2043 // T calculate_product(void) const method
2044 
2046 
2047 template <class T>
2049 {
2050  const size_t this_size = this->size();
2051 
2052  T product = 1;
2053 
2054  for(size_t i = 0; i < this_size; i++)
2055  {
2056  product *= (*this)[i];
2057  }
2058 
2059  return(product);
2060 }
2061 
2062 
2063 // double calculate_mean(void) const method
2064 
2066 
2067 template <class T>
2068 double Vector<T>::calculate_mean(void) const
2069 {
2070  const size_t this_size = this->size();
2071 
2072  // Control sentence (if debug)
2073 
2074  #ifndef NDEBUG
2075 
2076  if(this_size == 0)
2077  {
2078  std::ostringstream buffer;
2079 
2080  buffer << "OpenNN Exception: Vector Template.\n"
2081  << "double calculate_mean(void) const method.\n"
2082  << "Size must be greater than zero.\n";
2083 
2084  throw std::logic_error(buffer.str());
2085  }
2086 
2087  #endif
2088 
2089  const T sum = calculate_sum();
2090 
2091  const double mean = sum/(double)this_size;
2092 
2093  return(mean);
2094 }
2095 
2096 
2097 // double calculate_standard_deviation(void) method
2098 
2100 
2101 template <class T>
2103 {
2104  const size_t this_size = this->size();
2105 
2106  // Control sentence (if debug)
2107 
2108  #ifndef NDEBUG
2109 
2110  if(this_size == 0)
2111  {
2112  std::ostringstream buffer;
2113 
2114  buffer << "OpenNN Exception: Vector Template.\n"
2115  << "double calculate_standard_deviation(void) const method.\n"
2116  << "Size must be greater than zero.\n";
2117 
2118  throw std::logic_error(buffer.str());
2119  }
2120 
2121  #endif
2122 
2123  if(this_size == 1)
2124  {
2125  return(0.0);
2126  }
2127 
2128  double sum = 0.0;
2129  double squared_sum = 0.0;
2130 
2131  for(size_t i = 0; i < this_size; i++)
2132  {
2133  sum += (*this)[i];
2134  squared_sum += (*this)[i]*(*this)[i];
2135  }
2136 
2137  const double numerator = squared_sum - (sum*sum)/this_size;
2138  const double denominator = this_size - 1.0;
2139 
2140  return(sqrt(numerator/denominator));
2141 }
2142 
2143 
2144 // Vector<double> calculate_mean_standard_deviation(void) const method
2145 
2147 
2148 template <class T>
2150 {
2151  // Control sentence (if debug)
2152 
2153  #ifndef NDEBUG
2154 
2155  const size_t this_size = this->size();
2156 
2157  if(this_size == 0)
2158  {
2159  std::ostringstream buffer;
2160 
2161  buffer << "OpenNN Exception: Vector Template.\n"
2162  << "double calculate_mean_standard_deviation(void).\n"
2163  << "Size must be greater than zero.\n";
2164 
2165  throw std::logic_error(buffer.str());
2166  }
2167 
2168  #endif
2169 
2170  const double mean = calculate_mean();
2171  const double standard_deviation = calculate_standard_deviation();
2172 
2173  Vector<double> mean_standard_deviation(2);
2174  mean_standard_deviation[0] = mean;
2175  mean_standard_deviation[1] = standard_deviation;
2176 
2177  return(mean_standard_deviation);
2178 }
2179 
2180 
2181 // double calculate_mean_missing_values(const Vector<size_t>&) const method
2182 
2184 
2185 template <class T>
2186 double Vector<T>::calculate_mean_missing_values(const Vector<size_t>& missing_indices) const
2187 {
2188  const size_t this_size = this->size();
2189 
2190  // Control sentence (if debug)
2191 
2192  #ifndef NDEBUG
2193 
2194  if(this_size == 0)
2195  {
2196  std::ostringstream buffer;
2197 
2198  buffer << "OpenNN Exception: Vector Template.\n"
2199  << "double calculate_mean_missing_values(const Vector<size_t>&) const method.\n"
2200  << "Size must be greater than zero.\n";
2201 
2202  throw std::logic_error(buffer.str());
2203  }
2204 
2205  #endif
2206 
2207  T sum = 0;
2208 
2209  size_t count = 0;
2210 
2211  for(size_t i = 0; i < this_size; i++)
2212  {
2213  if(!missing_indices.contains(i))
2214  {
2215  sum += (*this)[i];
2216  count++;
2217  }
2218  }
2219 
2220  const double mean = sum/(double)count;
2221 
2222  return(mean);
2223 }
2224 
2225 
2226 // double calculate_standard_deviation_missing_values(const Vector<size_t>&) method
2227 
2229 
2230 template <class T>
2232 {
2233  const size_t this_size = this->size();
2234 
2235  // Control sentence (if debug)
2236 
2237  #ifndef NDEBUG
2238 
2239  if(this_size == 0)
2240  {
2241  std::ostringstream buffer;
2242 
2243  buffer << "OpenNN Exception: Vector Template.\n"
2244  << "double calculate_standard_deviation_missing_values(const Vector<size_t>&) const method.\n"
2245  << "Size must be greater than zero.\n";
2246 
2247  throw std::logic_error(buffer.str());
2248  }
2249 
2250  #endif
2251 
2252  double sum = 0.0;
2253  double squared_sum = 0.0;
2254 
2255  size_t count = 0;
2256 
2257  for(size_t i = 0; i < this_size; i++)
2258  {
2259  if(!missing_indices.contains(i))
2260  {
2261  sum += (*this)[i];
2262  squared_sum += (*this)[i]*(*this)[i];
2263 
2264  count++;
2265  }
2266  }
2267 
2268  if(count <= 1)
2269  {
2270  return(0.0);
2271  }
2272 
2273  const double numerator = squared_sum - (sum*sum)/count;
2274  const double denominator = this_size - 1.0;
2275 
2276  return(sqrt(numerator/denominator));
2277 }
2278 
2279 
2280 // Statistics<T> calculate_statistics(void) const method
2281 
2283 
2284 template <class T>
2286 {
2287  // Control sentence (if debug)
2288 
2289  #ifndef NDEBUG
2290 
2291  const size_t this_size = this->size();
2292 
2293  if(this_size == 0)
2294  {
2295  std::ostringstream buffer;
2296 
2297  buffer << "OpenNN Exception: Vector Template.\n"
2298  << "double calculate_statistics(void).\n"
2299  << "Size must be greater than zero.\n";
2300 
2301  throw std::logic_error(buffer.str());
2302  }
2303 
2304  #endif
2305 
2306  Statistics<T> statistics;
2307 
2308  statistics.minimum = calculate_minimum();
2309  statistics.maximum = calculate_maximum();
2310  statistics.mean = calculate_mean();
2311  statistics.standard_deviation = calculate_standard_deviation();
2312 
2313  return(statistics);
2314 }
2315 
2316 
2317 // Statistics<T> calculate_statistics_missing_values(const Vector<size_t>&) const method
2318 
2320 
2321 template <class T>
2323 {
2324  // Control sentence (if debug)
2325 
2326  #ifndef NDEBUG
2327 
2328  const size_t this_size = this->size();
2329 
2330  if(this_size == 0)
2331  {
2332  std::ostringstream buffer;
2333 
2334  buffer << "OpenNN Exception: Vector Template.\n"
2335  << "double calculate_statistics_missing_values(const Vector<size_t>&).\n"
2336  << "Size must be greater than zero.\n";
2337 
2338  throw std::logic_error(buffer.str());
2339  }
2340 
2341  #endif
2342 
2343  Statistics<T> statistics;
2344  statistics.minimum = calculate_minimum_missing_values(missing_indices);
2345  statistics.maximum = calculate_maximum_missing_values(missing_indices);
2346  statistics.mean = calculate_mean_missing_values(missing_indices);
2347  statistics.standard_deviation = calculate_standard_deviation_missing_values(missing_indices);
2348 
2349  return(statistics);
2350 }
2351 
2352 
2353 // double calculate_norm(void) const method
2354 
2356 
2357 template <class T>
2358 double Vector<T>::calculate_norm(void) const
2359 {
2360  const size_t this_size = this->size();
2361 
2362  // Control sentence (if debug)
2363 
2364  double norm = 0.0;
2365 
2366  for(size_t i = 0; i < this_size; i++)
2367  {
2368  norm += (*this)[i]*(*this)[i];
2369  }
2370 
2371  norm = sqrt(norm);
2372 
2373  return(norm);
2374 }
2375 
2376 
2377 // Vector<T> calculate_norm_gradient(void) const method
2378 
2380 
2381 template <class T>
2383 {
2384  const size_t this_size = this->size();
2385 
2386  // Control sentence (if debug)
2387 
2388  Vector<T> gradient(this_size);
2389 
2390  const double norm = calculate_norm();
2391 
2392  if(norm == 0.0)
2393  {
2394  gradient.initialize(0.0);
2395  }
2396  else
2397  {
2398  gradient = (*this)/norm;
2399  }
2400 
2401  return(gradient);
2402 }
2403 
2404 
2405 // Matrix<T> calculate_norm_Hessian(void) const method
2406 
2408 
2409 template <class T>
2411 {
2412  const size_t this_size = this->size();
2413 
2414  // Control sentence (if debug)
2415 
2416  Matrix<T> Hessian(this_size, this_size);
2417 
2418  const double norm = calculate_norm();
2419 
2420  if(norm == 0.0)
2421  {
2422  Hessian.initialize(0.0);
2423  }
2424  else
2425  {
2426  // Hessian = (*this).direct(*this)/pow(norm, 3);
2427  Hessian = (*this).direct(*this)/(norm*norm*norm);
2428  }
2429 
2430  return(Hessian);
2431 }
2432 
2433 
2434 // double calculate_p_norm(const double&) const method
2435 
2437 
2438 template <class T>
2439 double Vector<T>::calculate_p_norm(const double& p) const
2440 {
2441  // Control sentence (if debug)
2442 
2443  #ifndef NDEBUG
2444 
2445  std::ostringstream buffer;
2446 
2447  if(p <= 0)
2448  {
2449  buffer << "OpenNN Exception: Vector Template.\n"
2450  << "double calculate_p_norm(const double&) const method.\n"
2451  << "p value must be greater than zero.\n";
2452 
2453  throw std::logic_error(buffer.str());
2454  }
2455 
2456  #endif
2457 
2458  const size_t this_size = this->size();
2459 
2460  // Control sentence (if debug)
2461 
2462  double norm = 0.0;
2463 
2464  for(size_t i = 0; i < this_size; i++)
2465  {
2466  norm += pow(fabs((*this)[i]), p);
2467  }
2468 
2469  norm = pow(norm, 1.0/p);
2470 
2471  return(norm);
2472 }
2473 
2474 
2475 // Vector<double> calculate_p_norm_gradient(const double&) const method
2476 
2478 
2479 template <class T>
2481 {
2482  // Control sentence (if debug)
2483 
2484  #ifndef NDEBUG
2485 
2486  std::ostringstream buffer;
2487 
2488  if(p <= 0)
2489  {
2490  buffer << "OpenNN Exception: Vector Template.\n"
2491  << "Vector<double> calculate_p_norm_gradient(const double&) const method.\n"
2492  << "p value must be greater than zero.\n";
2493 
2494  throw std::logic_error(buffer.str());
2495  }
2496 
2497  #endif
2498 
2499  const size_t this_size = this->size();
2500 
2501  // Control sentence (if debug)
2502 
2503  Vector<double> gradient(this_size);
2504 
2505  const double p_norm = calculate_p_norm(p);
2506 
2507  if(p_norm == 0.0)
2508  {
2509  gradient.initialize(0.0);
2510  }
2511  else
2512  {
2513  for(size_t i = 0; i < this_size; i++)
2514  {
2515  gradient[i] = (*this)[i]*pow(fabs((*this)[i]),p-2.0)/pow(p_norm, p-1.0);
2516  }
2517 
2518 // gradient = (*this)*(*this).calculate_pow(p-2.0)/pow(p_norm, p-1.0);
2519  }
2520 
2521  return(gradient);
2522 }
2523 
2524 
2525 // double calculate_normalized(void) const method
2526 
2528 
2529 template <class T>
2531 {
2532  const size_t this_size = (*this).size();
2533 
2534  Vector<T> normalized(this_size);
2535 
2536  const double norm = calculate_norm();
2537 
2538  if(norm == 0.0)
2539  {
2540  normalized.initialize(0.0);
2541  }
2542  else
2543  {
2544  normalized = (*this)/norm;
2545  }
2546 
2547  return(normalized);
2548 }
2549 
2550 
2551 // double calculate_distance(const Vector<double>&) const method
2552 
2555 
2556 template <class T>
2557 double Vector<T>::calculate_distance(const Vector<double>& other_vector) const
2558 {
2559  return( sqrt(calculate_sum_squared_error(other_vector)) );
2560 }
2561 
2562 
2563 // double calculate_sum_squared_error(const Vector<double>&) const method
2564 
2567 
2568 template <class T>
2570 {
2571  const size_t this_size = this->size();
2572 
2573  // Control sentence (if debug)
2574 
2575  #ifndef NDEBUG
2576 
2577  const size_t other_size = other_vector.size();
2578 
2579  if(other_size != this_size)
2580  {
2581  std::ostringstream buffer;
2582 
2583  buffer << "OpenNN Exception: Vector Template.\n"
2584  << "double calculate_sum_squared_error(const Vector<double>&) const method.\n"
2585  << "Size must be equal to this size.\n";
2586 
2587  throw std::logic_error(buffer.str());
2588  }
2589 
2590  #endif
2591 
2592  double sum_squared_error = 0.0;
2593  double error;
2594 
2595  for(size_t i = 0; i < this_size; i++)
2596  {
2597  error = (*this)[i] - other_vector[i];
2598 
2599  sum_squared_error += error*error;
2600  }
2601 
2602  return(sum_squared_error);
2603 }
2604 
2605 
2606 // double calculate_Minkowski_error(const Vector<double>&) const method
2607 
2611 
2612 template <class T>
2613 double Vector<T>::calculate_Minkowski_error(const Vector<double>& other_vector, const double& Minkowski_parameter) const
2614 {
2615  const size_t this_size = this->size();
2616 
2617  // Control sentence (if debug)
2618 
2619  #ifndef NDEBUG
2620 
2621  std::ostringstream buffer;
2622 
2623  if(this_size == 0)
2624  {
2625  buffer << "OpenNN Exception: Vector Template.\n"
2626  << "double calculate_Minkowski_error(const Vector<double>&) const method.\n"
2627  << "Size must be greater than zero.\n";
2628 
2629  throw std::logic_error(buffer.str());
2630  }
2631 
2632  const size_t other_size = other_vector.size();
2633 
2634  if(other_size != this_size)
2635  {
2636  buffer << "OpenNN Exception: Vector Template.\n"
2637  << "double calculate_Minkowski_error(const Vector<double>&) const method.\n"
2638  << "Other size must be equal to this size.\n";
2639 
2640  throw std::logic_error(buffer.str());
2641  }
2642 
2643  // Control sentence
2644 
2645  if(Minkowski_parameter < 1.0 || Minkowski_parameter > 2.0)
2646  {
2647  buffer << "OpenNN Exception: Vector Template.\n"
2648  << "double calculate_Minkowski_error(const Vector<double>&) const method.\n"
2649  << "The Minkowski parameter must be comprised between 1 and 2\n";
2650 
2651  throw std::logic_error(buffer.str());
2652  }
2653 
2654  #endif
2655 
2656  double Minkowski_error = 0.0;
2657 
2658  for(size_t i = 0; i < this_size; i++)
2659  {
2660  Minkowski_error += pow(fabs((*this)[i] - other_vector[i]), Minkowski_parameter);
2661  }
2662 
2663  Minkowski_error = pow(Minkowski_error, 1.0/Minkowski_parameter);
2664 
2665  return(Minkowski_error);
2666 }
2667 
2668 
2669 // T calculate_linear_correlation(const Vector<T>&) const method
2670 
2673 
2674 template <class T>
2676 {
2677  const size_t n = this->size();
2678 
2679  // Control sentence (if debug)
2680 
2681  #ifndef NDEBUG
2682 
2683  const size_t other_size = other.size();
2684 
2685  std::ostringstream buffer;
2686 
2687  if(other_size != n)
2688  {
2689  buffer << "OpenNN Exception: Vector Template.\n"
2690  << "T calculate_linear_correlation(const Vector<T>&) const method.\n"
2691  << "Other size must be equal to this size.\n";
2692 
2693  throw std::logic_error(buffer.str());
2694  }
2695 
2696  #endif
2697 
2698  T s_x = 0;
2699  T s_y = 0;
2700 
2701  T s_xx = 0;
2702  T s_yy = 0;
2703 
2704  T s_xy = 0;
2705 
2706  for(size_t i = 0; i < n; i++)
2707  {
2708  s_x += other[i];
2709  s_y += (*this)[i];
2710 
2711  s_xx += other[i]*other[i];
2712  s_yy += (*this)[i]*(*this)[i];
2713 
2714  s_xy += other[i]*(*this)[i];
2715  }
2716 
2717  T linear_correlation;
2718 
2719  if(s_x == 0 && s_y == 0 && s_xx == 0 && s_yy == 0 && s_xy == 0)
2720  {
2721  linear_correlation = 1;
2722  }
2723  else
2724  {
2725  const double numerator = (n*s_xy - s_x*s_y);
2726  const double denominator = sqrt((n*s_xx - s_x*s_x)*(n*s_yy - s_y*s_y));
2727 
2728  if(denominator < 1.0e-50)
2729  {
2730  linear_correlation = 0;
2731  }
2732  else
2733  {
2734  linear_correlation = numerator/denominator;
2735  }
2736  }
2737 
2738  return(linear_correlation);
2739 }
2740 
2741 
2742 // T calculate_linear_correlation_missing_values(const Vector<T>&, const Vector<size_t>&) const method
2743 
2747 
2748 template <class T>
2750 {
2751  const size_t n = this->size();
2752 
2753  // Control sentence (if debug)
2754 
2755  #ifndef NDEBUG
2756 
2757  const size_t other_size = other.size();
2758 
2759  std::ostringstream buffer;
2760 
2761  if(other_size != n)
2762  {
2763  buffer << "OpenNN Exception: Vector Template.\n"
2764  << "T calculate_linear_correlation(const Vector<T>&) const method.\n"
2765  << "Other size must be equal to this size.\n";
2766 
2767  throw std::logic_error(buffer.str());
2768  }
2769 
2770  #endif
2771 
2772  size_t count = 0;
2773 
2774  T s_x = 0;
2775  T s_y = 0;
2776 
2777  T s_xx = 0;
2778  T s_yy = 0;
2779 
2780  T s_xy = 0;
2781 
2782  for(size_t i = 0; i < n; i++)
2783  {
2784  if(!missing_indices.contains(i))
2785  {
2786  s_x += other[i];
2787  s_y += (*this)[i];
2788 
2789  s_xx += other[i]*other[i];
2790  s_yy += (*this)[i]*(*this)[i];
2791 
2792  s_xy += other[i]*(*this)[i];
2793 
2794  count++;
2795  }
2796  }
2797 
2798  T linear_correlation;
2799 
2800  if(s_x == 0 && s_y == 0 && s_xx == 0 && s_yy == 0 && s_xy == 0)
2801  {
2802  linear_correlation = 1;
2803  }
2804  else
2805  {
2806  const double numerator = (count*s_xy - s_x*s_y);
2807  const double denominator = sqrt((count*s_xx - s_x*s_x)*(count*s_yy - s_y*s_y));
2808 
2809  if(denominator < 1.0e-50)
2810  {
2811  linear_correlation = 0;
2812  }
2813  else
2814  {
2815  linear_correlation = numerator/denominator;
2816  }
2817  }
2818 
2819  return(linear_correlation);
2820 }
2821 
2822 
2823 // LinearRegressionParameters<T> calculate_linear_regression_parameters(const Vector<T>&) const method
2824 
2828 
2829 template <class T>
2831 {
2832  const size_t n = this->size();
2833 
2834  // Control sentence (if debug)
2835 
2836  #ifndef NDEBUG
2837 
2838  const size_t other_size = other.size();
2839 
2840  std::ostringstream buffer;
2841 
2842  if(other_size != n)
2843  {
2844  buffer << "OpenNN Exception: Vector Template.\n"
2845  << "LinearRegressionParameters<T> calculate_linear_regression_parameters(const Vector<T>&) const method.\n"
2846  << "Other size must be equal to this size.\n";
2847 
2848  throw std::logic_error(buffer.str());
2849  }
2850 
2851  #endif
2852 
2853  T s_x = 0;
2854  T s_y = 0;
2855 
2856  T s_xx = 0;
2857  T s_yy = 0;
2858 
2859  T s_xy = 0;
2860 
2861  for(size_t i = 0; i < n; i++)
2862  {
2863  s_x += other[i];
2864  s_y += (*this)[i];
2865 
2866  s_xx += other[i]*other[i];
2867  s_yy += (*this)[i]*(*this)[i];
2868 
2869  s_xy += other[i]*(*this)[i];
2870  }
2871 
2872  LinearRegressionParameters<T> linear_regression_parameters;
2873 
2874  if(s_x == 0 && s_y == 0 && s_xx == 0 && s_yy == 0 && s_xy == 0)
2875  {
2876  linear_regression_parameters.intercept = 0;
2877 
2878  linear_regression_parameters.slope = 0;
2879 
2880  linear_regression_parameters.correlation = 1;
2881  }
2882  else
2883  {
2884  linear_regression_parameters.intercept = (s_y*s_xx - s_x*s_xy)
2885  /(n*s_xx - s_x*s_x);
2886 
2887  linear_regression_parameters.slope = (n*s_xy - s_x*s_y)
2888  /(n*s_xx - s_x*s_x);
2889 
2890  linear_regression_parameters.correlation = (n*s_xy - s_x*s_y)
2891  /sqrt((n*s_xx - s_x*s_x)*(n*s_yy - s_y*s_y));
2892  }
2893 
2894  return(linear_regression_parameters);
2895 }
2896 
2897 
2898 // void calculate_absolute_value(void) const method
2899 
2901 
2902 template <class T>
2904 {
2905  const size_t this_size = this->size();
2906 
2907  Vector<T> absolute_value(this_size);
2908 
2909  for(size_t i = 0; i < this_size; i++)
2910  {
2911  if((*this)[i] > 0)
2912  {
2913  absolute_value[i] = (*this)[i];
2914  }
2915  else
2916  {
2917  absolute_value[i] = -(*this)[i];
2918  }
2919  }
2920 
2921  return(absolute_value);
2922 }
2923 
2924 
2925 // void apply_absolute_value(void) method
2926 
2928 
2929 template <class T>
2931 {
2932  const size_t this_size = this->size();
2933 
2934  for(size_t i = 0; i < this_size; i++)
2935  {
2936  if((*this)[i] < 0)
2937  {
2938  (*this)[i] = -(*this)[i];
2939  }
2940  }
2941 }
2942 
2943 
2944 // Vector<T> calculate_lower_bounded(const T&) const method
2945 
2948 
2949 template <class T>
2951 {
2952  const size_t this_size = this->size();
2953 
2954  Vector<T> bounded_vector(this_size);
2955 
2956  for(size_t i = 0; i < this_size; i++)
2957  {
2958  if((*this)[i] < lower_bound)
2959  {
2960  bounded_vector[i] = lower_bound;
2961  }
2962  else
2963  {
2964  bounded_vector[i] = (*this)[i];
2965  }
2966  }
2967 
2968  return(bounded_vector);
2969 }
2970 
2971 
2972 // Vector<T> calculate_lower_bounded(const Vector<T>&) const method
2973 
2976 
2977 template <class T>
2979 {
2980  const size_t this_size = this->size();
2981 
2982  // Control sentence (if debug)
2983 
2984  #ifndef NDEBUG
2985 
2986  const size_t lower_bound_size = lower_bound.size();
2987 
2988  if(lower_bound_size != this_size)
2989  {
2990  std::ostringstream buffer;
2991 
2992  buffer << "OpenNN Exception: Vector Template.\n"
2993  << "Vector<T> calculate_lower_bounded(const Vector<T>&) const method.\n"
2994  << "Lower bound size must be equal to vector size.\n";
2995 
2996  throw std::logic_error(buffer.str());
2997  }
2998 
2999  #endif
3000 
3001  Vector<T> bounded_vector(this_size);
3002 
3003  // Apply lower bound
3004 
3005  for(size_t i = 0; i < this_size; i++)
3006  {
3007  if((*this)[i] < lower_bound[i])
3008  {
3009  bounded_vector[i] = lower_bound[i];
3010  }
3011  else
3012  {
3013  bounded_vector[i] = (*this)[i];
3014  }
3015  }
3016 
3017  return(bounded_vector);
3018 }
3019 
3020 
3021 // Vector<T> calculate_upper_bounded(const T&) const method
3022 
3025 
3026 template <class T>
3028 {
3029  const size_t this_size = this->size();
3030 
3031  Vector<T> bounded_vector(this_size);
3032 
3033  for(size_t i = 0; i < this_size; i++)
3034  {
3035  if((*this)[i] > upper_bound)
3036  {
3037  bounded_vector[i] = upper_bound;
3038  }
3039  else
3040  {
3041  bounded_vector[i] = (*this)[i];
3042  }
3043  }
3044 
3045  return(bounded_vector);
3046 }
3047 
3048 
3049 // Vector<T> calculate_upper_bounded(const Vector<T>&) const method
3050 
3053 
3054 template <class T>
3056 {
3057  const size_t this_size = this->size();
3058 
3059  // Control sentence (if debug)
3060 
3061  #ifndef NDEBUG
3062 
3063  const size_t upper_bound_size = upper_bound.size();
3064 
3065  if(upper_bound_size != this_size)
3066  {
3067  std::ostringstream buffer;
3068 
3069  buffer << "OpenNN Exception: Vector Template.\n"
3070  << "Vector<T> calculate_upper_bounded(const Vector<T>&) const method.\n"
3071  << "Upper bound size must be equal to vector size.\n";
3072 
3073  throw std::logic_error(buffer.str());
3074  }
3075 
3076  #endif
3077 
3078  Vector<T> bounded_vector(this_size);
3079 
3080  // Apply upper bound
3081 
3082  for(size_t i = 0; i < this_size; i++)
3083  {
3084  if((*this)[i] > upper_bound[i])
3085  {
3086  bounded_vector[i] = upper_bound[i];
3087  }
3088  else
3089  {
3090  bounded_vector[i] = (*this)[i];
3091  }
3092  }
3093 
3094  return(bounded_vector);
3095 }
3096 
3097 
3098 // Vector<T> calculate_lower_upper_bounded(const T&, const T&) const method
3099 
3104 
3105 template <class T>
3106 Vector<T> Vector<T>::calculate_lower_upper_bounded(const T& lower_bound, const T& upper_bound) const
3107 {
3108  const size_t this_size = this->size();
3109 
3110  Vector<T> bounded_vector(this_size);
3111 
3112  for(size_t i = 0; i < this_size; i++)
3113  {
3114  if((*this)[i] < lower_bound)
3115  {
3116  bounded_vector[i] = lower_bound;
3117  }
3118  else if((*this)[i] > upper_bound)
3119  {
3120  bounded_vector[i] = upper_bound;
3121  }
3122  else
3123  {
3124  bounded_vector[i] = (*this)[i];
3125  }
3126  }
3127 
3128  return(bounded_vector);
3129 }
3130 
3131 
3132 // Vector<T> calculate_lower_upper_bounded(const Vector<T>&, const Vector<T>&) const method
3133 
3138 
3139 template <class T>
3140 Vector<T> Vector<T>::calculate_lower_upper_bounded(const Vector<T>& lower_bound, const Vector<T>& upper_bound) const
3141 {
3142  const size_t this_size = this->size();
3143 
3144  // Control sentence (if debug)
3145 
3146  #ifndef NDEBUG
3147 
3148  const size_t lower_bound_size = lower_bound.size();
3149  const size_t upper_bound_size = upper_bound.size();
3150 
3151  if(lower_bound_size != this_size || upper_bound_size != this_size)
3152  {
3153  std::ostringstream buffer;
3154 
3155  buffer << "OpenNN Exception: Vector Template.\n"
3156  << "Vector<T> calculate_lower_upper_bounded(const Vector<T>&, const Vector<T>&) const method.\n"
3157  << "Lower and upper bound sizes must be equal to vector size.\n";
3158 
3159  throw std::logic_error(buffer.str());
3160  }
3161 
3162  #endif
3163 
3164  Vector<T> bounded_vector(this_size);
3165 
3166  // Apply lower and upper bounds
3167 
3168  for(size_t i = 0; i < this_size; i++)
3169  {
3170  if((*this)[i] < lower_bound[i])
3171  {
3172  bounded_vector[i] = lower_bound[i];
3173  }
3174  else if((*this)[i] > upper_bound[i])
3175  {
3176  bounded_vector[i] = upper_bound[i];
3177  }
3178  else
3179  {
3180  bounded_vector[i] = (*this)[i];
3181  }
3182  }
3183 
3184  return(bounded_vector);
3185 }
3186 
3187 
3188 // void apply_lower_bound(const T&) method
3189 
3192 
3193 template <class T>
3194 void Vector<T>::apply_lower_bound(const T& lower_bound)
3195 {
3196  const size_t this_size = this->size();
3197 
3198  for(size_t i = 0; i < this_size; i++)
3199  {
3200  if((*this)[i] < lower_bound)
3201  {
3202  (*this)[i] = lower_bound;
3203  }
3204  }
3205 }
3206 
3207 
3208 // void apply_lower_bound(const Vector<T>&) method
3209 
3212 
3213 template <class T>
3214 void Vector<T>::apply_lower_bound(const Vector<T>& lower_bound)
3215 {
3216  const size_t this_size = this->size();
3217 
3218  for(size_t i = 0; i < this_size; i++)
3219  {
3220  if((*this)[i] < lower_bound[i])
3221  {
3222  (*this)[i] = lower_bound[i];
3223  }
3224  }
3225 }
3226 
3227 
3228 // void apply_upper_bound(const T&) method
3229 
3232 
3233 template <class T>
3234 void Vector<T>::apply_upper_bound(const T& upper_bound)
3235 {
3236  const size_t this_size = this->size();
3237 
3238  for(size_t i = 0; i < this_size; i++)
3239  {
3240  if((*this)[i] > upper_bound)
3241  {
3242  (*this)[i] = upper_bound;
3243  }
3244  }
3245 }
3246 
3247 
3248 // void apply_upper_bound(const Vector<T>&) method
3249 
3252 
3253 template <class T>
3254 void Vector<T>::apply_upper_bound(const Vector<T>& upper_bound)
3255 {
3256  const size_t this_size = this->size();
3257 
3258  for(size_t i = 0; i < this_size; i++)
3259  {
3260  if((*this)[i] > upper_bound[i])
3261  {
3262  (*this)[i] = upper_bound[i];
3263  }
3264  }
3265 }
3266 
3267 
3268 // void apply_lower_upper_bounds(const T&, const T&) method
3269 
3274 
3275 template <class T>
3276 void Vector<T>::apply_lower_upper_bounds(const T& lower_bound, const T& upper_bound)
3277 {
3278  const size_t this_size = this->size();
3279 
3280  for(size_t i = 0; i < this_size; i++)
3281  {
3282  if((*this)[i] < lower_bound)
3283  {
3284  (*this)[i] = lower_bound;
3285  }
3286  else if((*this)[i] > upper_bound)
3287  {
3288  (*this)[i] = upper_bound;
3289  }
3290  }
3291 }
3292 
3293 
3294 // void apply_lower_upper_bounds(const Vector<T>&, const Vector<T>&) method
3295 
3300 
3301 template <class T>
3302 void Vector<T>::apply_lower_upper_bounds(const Vector<T>& lower_bound, const Vector<T>& upper_bound)
3303 {
3304  const size_t this_size = this->size();
3305 
3306  for(size_t i = 0; i < this_size; i++)
3307  {
3308  if((*this)[i] < lower_bound[i])
3309  {
3310  (*this)[i] = lower_bound[i];
3311  }
3312  else if((*this)[i] > upper_bound[i])
3313  {
3314  (*this)[i] = upper_bound[i];
3315  }
3316  }
3317 }
3318 
3319 
3320 // Vector<size_t> calculate_less_rank(void) const method
3321 
3325 
3326 
3327 template <class T>
3329 {
3330  const size_t this_size = this->size();
3331 
3332  Vector<size_t> rank(this_size);
3333 
3334  Vector<T> sorted_vector(*this);
3335 
3336  std::sort(sorted_vector.begin(), sorted_vector.end(), std::less<double>());
3337 
3338  for(size_t i = 0; i < this_size; i++)
3339  {
3340  for(size_t j = 0; j < this_size; j++)
3341  {
3342  if((*this)[i] == sorted_vector[j])
3343  {
3344  rank[i] = j;
3345  }
3346  }
3347  }
3348 
3349  return(rank);
3350 }
3351 
3352 
3353 // Vector<size_t> calculate_greater_rank(void) const method
3354 
3358 
3359 template <class T>
3361 {
3362  const size_t this_size = this->size();
3363 
3364  Vector<size_t> rank(this_size);
3365 
3366  Vector<T> sorted_vector(*this);
3367 
3368  std::sort(sorted_vector.begin(), sorted_vector.end(), std::greater<double>());
3369 
3370  for(size_t i = 0; i < this_size; i++)
3371  {
3372  for(size_t j = 0; j < this_size; j++)
3373  {
3374  if((*this)[i] == sorted_vector[j])
3375  {
3376  rank[i] = j;
3377  }
3378  }
3379  }
3380 
3381  return(rank);
3382 }
3383 
3384 
3385 // Vector<T> operator + (const T&) const method
3386 
3389 
3390 template <class T>
3391 inline Vector<T> Vector<T>::operator + (const T& scalar) const
3392 {
3393  const size_t this_size = this->size();
3394 
3395  Vector<T> sum(this_size);
3396 
3397  std::transform(this->begin(), this->end(),
3398  sum.begin(),
3399  std::bind2nd(std::plus<T>(), scalar) );
3400 
3401  return(sum);
3402 }
3403 
3404 
3405 // Vector<T> operator + (const Vector<T>&) const method
3406 
3409 
3410 template <class T>
3411 inline Vector<T> Vector<T>::operator + (const Vector<T>& other_vector) const
3412 {
3413  const size_t this_size = this->size();
3414 
3415  // Control sentence (if debug)
3416 
3417  #ifndef NDEBUG
3418 
3419  const size_t other_size = other_vector.size();
3420 
3421  if(other_size != this_size)
3422  {
3423  std::ostringstream buffer;
3424 
3425  buffer << "OpenNN Exception: Vector Template.\n"
3426  << "Vector<T> operator + (const Vector<T>) const.\n"
3427  << "Size of vectors is " << this_size << " and " << other_size << " and they must be the same.\n";
3428 
3429  throw std::logic_error(buffer.str());
3430  }
3431 
3432  #endif
3433 
3434  Vector<T> sum(this_size);
3435 
3436  std::transform(this->begin(), this->end(), other_vector.begin(), sum.begin(), std::plus<T>());
3437 
3438  return(sum);
3439 }
3440 
3441 
3442 //Vector<T> operator - (const T&) const method
3443 
3446 
3447 template <class T>
3448 inline Vector<T> Vector<T>::operator - (const T& scalar) const
3449 {
3450  const size_t this_size = this->size();
3451 
3452  Vector<T> difference(this_size);
3453 
3454  std::transform(this->begin(), this->end(), difference.begin(), std::bind2nd(std::minus<T>(), scalar));
3455 
3456  return(difference);
3457 }
3458 
3459 
3460 // Vector<T> operator - (const Vector<T>&) const method
3461 
3464 
3465 template <class T>
3466 inline Vector<T> Vector<T>::operator - (const Vector<T>& other_vector) const
3467 {
3468  const size_t this_size = this->size();
3469 
3470  // Control sentence (if debug)
3471 
3472  #ifndef NDEBUG
3473 
3474  const size_t other_size = other_vector.size();
3475 
3476  if(other_size != this_size)
3477  {
3478  std::ostringstream buffer;
3479 
3480  buffer << "OpenNN Exception: Vector Template.\n"
3481  << "Vector<T> operator - (const Vector<T>&) const.\n"
3482  << "Size of vectors is " << this_size << " and " << other_size << " and they must be the same.\n";
3483 
3484  throw std::logic_error(buffer.str());
3485  }
3486 
3487  #endif
3488 
3489  Vector<T> difference(this_size);
3490 
3491  std::transform( this->begin(), this->end(), other_vector.begin(), difference.begin(), std::minus<T>());
3492 
3493  return(difference);
3494 }
3495 
3496 
3497 // Vector<T> operator * (const T&) const method
3498 
3501 
3502 template <class T>
3503 Vector<T> Vector<T>::operator * (const T& scalar) const
3504 {
3505  const size_t this_size = this->size();
3506 
3507  Vector<T> product(this_size);
3508 
3509  std::transform( this->begin(), this->end(), product.begin(), std::bind2nd(std::multiplies<T>(), scalar));
3510 
3511  return(product);
3512 }
3513 
3514 
3515 // Type operator * (const Vector<T>&) const method
3516 
3519 
3520 template <class T>
3521 inline Vector<T> Vector<T>::operator * (const Vector<T>& other_vector) const
3522 {
3523  const size_t this_size = this->size();
3524 
3525  // Control sentence (if debug)
3526 
3527  #ifndef NDEBUG
3528 
3529  const size_t other_size = other_vector.size();
3530 
3531  if(other_size != this_size)
3532  {
3533  std::ostringstream buffer;
3534 
3535  buffer << "OpenNN Exception: Vector Template.\n"
3536  << "Vector<T> operator * (const Vector<T>&) const.\n"
3537  << "Size of other vector (" << other_size << ") must be equal to size of this vector (" << this_size << ").\n";
3538 
3539  throw std::logic_error(buffer.str());
3540  }
3541 
3542  #endif
3543 
3544  Vector<T> product(this_size);
3545 
3546  std::transform(this->begin(), this->end(), other_vector.begin(), product.begin(), std::multiplies<T>());
3547 
3548  return(product);
3549 }
3550 
3551 
3552 // Matrix<T> operator * (const Matrix<T>&) const method
3553 
3556 
3557 template <class T>
3559 {
3560  const size_t rows_number = matrix.get_rows_number();
3561  const size_t columns_number = matrix.get_columns_number();
3562 
3563  // Control sentence (if debug)
3564 
3565  #ifndef NDEBUG
3566 
3567  const size_t this_size = this->size();
3568 
3569  if(rows_number != this_size)
3570  {
3571  std::ostringstream buffer;
3572 
3573  buffer << "OpenNN Exception: Vector Template.\n"
3574  << "Vector<T> operator * (const Matrix<T>&) const.\n"
3575  << "Number of matrix rows (" << rows_number << ") must be equal to vector size (" << this_size << ").\n";
3576 
3577  throw std::logic_error(buffer.str());
3578  }
3579 
3580  #endif
3581 
3582  Matrix<T> product(rows_number, columns_number);
3583 
3584  for(size_t i = 0; i < rows_number; i++)
3585  {
3586  for(size_t j = 0; j < columns_number; j++)
3587  {
3588  product(i,j) = (*this)[i]*matrix(i,j);
3589  }
3590  }
3591 
3592  return(product);
3593 }
3594 
3595 
3596 // Vector<T> dot(const Matrix<T>&) const method
3597 
3601 
3602 template <class T>
3604 {
3605  const size_t rows_number = matrix.get_rows_number();
3606  const size_t columns_number = matrix.get_columns_number();
3607  const size_t this_size = this->size();
3608 
3609  // Control sentence (if debug)
3610 
3611  #ifndef NDEBUG
3612 
3613  if(rows_number != this_size)
3614  {
3615  std::ostringstream buffer;
3616 
3617  buffer << "OpenNN Exception: Vector Template.\n"
3618  << "Vector<T> dot(const Matrix<T>&) const method.\n"
3619  << "Matrix number of rows must be equal to vector size.\n";
3620 
3621  throw std::logic_error(buffer.str());
3622  }
3623 
3624  #endif
3625 
3626  Vector<double> product(columns_number);
3627 
3628 // for(size_t j = 0; j < columns_number; j++)
3629 // {
3630 // product[j] = 0;
3631 
3632 // for(size_t i = 0; i < rows_number; i++)
3633 // {
3634 // product[j] += (*this)[i]*matrix(i,j);
3635 // }
3636 // }
3637 
3638  const Eigen::Map<Eigen::VectorXd> vector_eigen((double*)this->data(), this_size);
3639  const Eigen::Map<Eigen::MatrixXd> matrix_eigen((double*)matrix.data(), rows_number, columns_number);
3640  Eigen::Map<Eigen::VectorXd> product_eigen(product.data(), columns_number);
3641 
3642  product_eigen = vector_eigen.transpose()*matrix_eigen;
3643 
3644  return(product);
3645 }
3646 
3647 
3648 // Vector<T> dot(const Vector<T>&) const method
3649 
3652 
3653 template <class T>
3654 double Vector<T>::dot(const Vector<double>& other_vector) const
3655 {
3656  const size_t this_size = this->size();
3657 
3658  // Control sentence (if debug)
3659 
3660  #ifndef NDEBUG
3661 
3662  const size_t other_size = other_vector.size();
3663 
3664  if(other_size != this_size)
3665  {
3666  std::ostringstream buffer;
3667 
3668  buffer << "OpenNN Exception: Vector Template.\n"
3669  << "Type dot(const Vector<T>&) const method.\n"
3670  << "Both vector sizes must be the same.\n";
3671 
3672  throw std::logic_error(buffer.str());
3673  }
3674 
3675  #endif
3676 
3677  // double dot_product = 0.0;
3678 
3679  // for(size_t i = 0; i < this_size; i++)
3680  // {
3681  // dot_product += (*this)[i]*other_vector[i];
3682  // }
3683 
3684  const Eigen::Map<Eigen::VectorXd> this_vector_eigen((double*)this->data(), this_size);
3685  const Eigen::Map<Eigen::VectorXd> other_vector_eigen((double*)other_vector.data(), this_size);
3686 
3687  return(this_vector_eigen.dot(other_vector_eigen));
3688 }
3689 
3690 
3691 // Matrix<T> direct(const Vector<T>&) const method
3692 
3695 
3696 template <class T>
3697 Matrix<T> Vector<T>::direct(const Vector<T>& other_vector) const
3698 {
3699  const size_t this_size = this->size();
3700 
3701  // Control sentence (if debug)
3702 
3703  #ifndef NDEBUG
3704 
3705  const size_t other_size = other_vector.size();
3706 
3707  if(other_size != this_size)
3708  {
3709  std::ostringstream buffer;
3710 
3711  buffer << "OpenNN Exception: Vector Template.\n"
3712  << "Matrix<T> direct(const Vector<T>&) const method.\n"
3713  << "Both vector sizes must be the same.\n";
3714 
3715  throw std::logic_error(buffer.str());
3716  }
3717 
3718  #endif
3719 
3720  Matrix<T> direct(this_size, this_size);
3721 
3722  for(size_t i = 0; i < this_size; i++)
3723  {
3724  for(size_t j = 0; j < this_size; j++)
3725  {
3726  direct(i,j) = (*this)[i]*other_vector[j];
3727  }
3728  }
3729 
3730  return(direct);
3731 }
3732 
3733 
3734 //Vector<T> operator / (const T&) const method
3735 
3738 
3739 template <class T>
3740 Vector<T> Vector<T>::operator / (const T& scalar) const
3741 {
3742  const size_t this_size = this->size();
3743 
3744  Vector<T> cocient(this_size);
3745 
3746  std::transform(this->begin(), this->end(), cocient.begin(), std::bind2nd(std::divides<T>(), scalar));
3747 
3748  return(cocient);
3749 }
3750 
3751 
3752 // Vector<T> operator / (const Vector<T>&) const method
3753 
3756 
3757 template <class T>
3758 Vector<T> Vector<T>::operator / (const Vector<T>& other_vector) const
3759 {
3760  const size_t this_size = this->size();
3761 
3762  // Control sentence (if debug)
3763 
3764  #ifndef NDEBUG
3765 
3766  const size_t other_size = other_vector.size();
3767 
3768  if(other_size != this_size)
3769  {
3770  std::ostringstream buffer;
3771 
3772  buffer << "OpenNN Exception: Vector Template.\n"
3773  << "Vector<T> operator / (const Vector<T>&) const.\n"
3774  << "Both vector sizes must be the same.\n";
3775 
3776  throw std::logic_error(buffer.str());
3777  }
3778 
3779  #endif
3780 
3781  Vector<T> cocient(this_size);
3782 
3783  std::transform(this->begin(), this->end(), other_vector.begin(), cocient.begin(), std::divides<T>());
3784 
3785  return(cocient);
3786 }
3787 
3788 
3789 // void operator += (const T&)
3790 
3793 
3794 template <class T>
3795 void Vector<T>::operator += (const T& value)
3796 {
3797  const size_t this_size = this->size();
3798 
3799  for(size_t i = 0; i < this_size; i++)
3800  {
3801  (*this)[i] = (*this)[i] + value;
3802  }
3803 }
3804 
3805 
3806 // void operator += (const Vector<T>&)
3807 
3810 
3811 template <class T>
3812 void Vector<T>::operator += (const Vector<T>& other_vector)
3813 {
3814  const size_t this_size = this->size();
3815 
3816  // Control sentence (if debug)
3817 
3818  #ifndef NDEBUG
3819 
3820  const size_t other_size = other_vector.size();
3821 
3822  if(other_size != this_size)
3823  {
3824  std::ostringstream buffer;
3825 
3826  buffer << "OpenNN Exception: Vector Template.\n"
3827  << "void operator += (const Vector<T>&).\n"
3828  << "Both vector sizes must be the same.\n";
3829 
3830  throw std::logic_error(buffer.str());
3831  }
3832 
3833  #endif
3834 
3835  for(size_t i = 0; i < this_size; i++)
3836  {
3837  (*this)[i] = (*this)[i] + other_vector[i];
3838  }
3839 }
3840 
3841 
3842 // void operator -= (const T&)
3843 
3846 
3847 template <class T>
3848 void Vector<T>::operator -= (const T& value)
3849 {
3850  const size_t this_size = this->size();
3851 
3852  for(size_t i = 0; i < this_size; i++)
3853  {
3854  (*this)[i] = (*this)[i] - value;
3855  }
3856 }
3857 
3858 
3859 // void operator -= (const Vector<T>&)
3860 
3863 
3864 template <class T>
3865 void Vector<T>::operator -= (const Vector<T>& other_vector)
3866 {
3867  const size_t this_size = this->size();
3868 
3869  // Control sentence (if debug)
3870 
3871  #ifndef NDEBUG
3872 
3873  const size_t other_size = other_vector.size();
3874 
3875  if(other_size != this_size)
3876  {
3877  std::ostringstream buffer;
3878 
3879  buffer << "OpenNN Exception: Vector Template.\n"
3880  << "void operator -= (const Vector<T>&).\n"
3881  << "Both vector sizes must be the same.\n";
3882 
3883  throw std::logic_error(buffer.str());
3884  }
3885 
3886  #endif
3887 
3888  for(size_t i = 0; i < this_size; i++)
3889  {
3890  (*this)[i] = (*this)[i] - other_vector[i];
3891  }
3892 }
3893 
3894 
3895 // void operator *= (const T&)
3896 
3899 
3900 template <class T>
3901 void Vector<T>::operator *= (const T& value)
3902 {
3903  const size_t this_size = this->size();
3904 
3905  for(size_t i = 0; i < this_size; i++)
3906  {
3907  (*this)[i] = (*this)[i]*value;
3908  }
3909 }
3910 
3911 
3912 // void operator *= (const Vector<T>&)
3913 
3916 
3917 template <class T>
3918 void Vector<T>::operator *= (const Vector<T>& other_vector)
3919 {
3920  const size_t this_size = this->size();
3921 
3922  // Control sentence (if debug)
3923 
3924  #ifndef NDEBUG
3925 
3926  const size_t other_size = other_vector.size();
3927 
3928  if(other_size != this_size)
3929  {
3930  std::ostringstream buffer;
3931 
3932  buffer << "OpenNN Exception: Vector Template.\n"
3933  << "void operator *= (const Vector<T>&).\n"
3934  << "Both vector sizes must be the same.\n";
3935 
3936  throw std::logic_error(buffer.str());
3937  }
3938 
3939  #endif
3940 
3941  for(size_t i = 0; i < this_size; i++)
3942  {
3943  (*this)[i] = (*this)[i]*other_vector[i];
3944  }
3945 }
3946 
3947 
3948 // void operator /= (const T&)
3949 
3952 
3953 template <class T>
3954 void Vector<T>::operator /= (const T& value)
3955 {
3956  const size_t this_size = this->size();
3957 
3958  for(size_t i = 0; i < this_size; i++)
3959  {
3960  (*this)[i] = (*this)[i]/value;
3961  }
3962 }
3963 
3964 
3965 // void operator /= (const Vector<T>&)
3966 
3969 
3970 template <class T>
3971 void Vector<T>::operator /= (const Vector<T>& other_vector)
3972 {
3973  const size_t this_size = this->size();
3974 
3975  // Control sentence (if debug)
3976 
3977  #ifndef NDEBUG
3978 
3979  const size_t other_size = other_vector.size();
3980 
3981  if(other_size != this_size)
3982  {
3983  std::ostringstream buffer;
3984 
3985  buffer << "OpenNN Exception: Vector Template.\n"
3986  << "void operator /= (const Vector<T>&).\n"
3987  << "Both vector sizes must be the same.\n";
3988 
3989  throw std::logic_error(buffer.str());
3990  }
3991 
3992  #endif
3993 
3994  for(size_t i = 0; i < this_size; i++)
3995  {
3996  (*this)[i] = (*this)[i]/other_vector[i];
3997  }
3998 }
3999 
4000 
4001 // void filter_positive(void) method
4002 
4004 
4005 template <class T>
4007 {
4008  for(size_t i = 0; i < this->size(); i++)
4009  {
4010  if((*this)[i] < 0)
4011  {
4012  (*this)[i] = 0;
4013  }
4014  }
4015 }
4016 
4017 
4018 // void filter_negative(void) method
4019 
4021 
4022 template <class T>
4024 {
4025  for(size_t i = 0; i < this->size(); i++)
4026  {
4027  if((*this)[i] > 0)
4028  {
4029  (*this)[i] = 0;
4030  }
4031  }
4032 }
4033 
4034 // void scale_minimum_maximum(const T&, const T&) method
4035 
4039 
4040 template <class T>
4041 void Vector<T>::scale_minimum_maximum(const T& minimum, const T& maximum)
4042 {
4043  if(maximum - minimum < 1.0e-99)
4044  {
4045  return;
4046  }
4047 
4048  const size_t this_size = this->size();
4049 
4050  for(size_t i = 0; i < this_size; i++)
4051  {
4052  (*this)[i] = 2.0*((*this)[i]-minimum)/(maximum-minimum)-1.0;
4053  }
4054 }
4055 
4056 
4057 // void scale_minimum_maximum(const Statistics<T>&) method
4058 
4061 
4062 template <class T>
4064 {
4065  scale_minimum_maximum(statistics.minimum, statistics.maximum);
4066 }
4067 
4068 
4069 // Statistics<T> scale_minimum_maximum(void) method
4070 
4074 
4075 template <class T>
4077 {
4078  const Statistics<T> statistics = calculate_statistics();
4079 
4080  scale_minimum_maximum(statistics);
4081 
4082  return(statistics);
4083 }
4084 
4085 
4086 // void scale_mean_standard_deviation(const T&, const T&) method
4087 
4091 
4092 template <class T>
4093 void Vector<T>::scale_mean_standard_deviation(const T& mean, const T& standard_deviation)
4094 {
4095  if(standard_deviation < 1.0e-99)
4096  {
4097  return;
4098  }
4099 
4100  const size_t this_size = this->size();
4101 
4102  for(size_t i = 0; i < this_size; i++)
4103  {
4104  (*this)[i] = ((*this)[i]-mean)/standard_deviation;
4105  }
4106 }
4107 
4108 
4109 // void scale_mean_standard_deviation(const Statistics<T>&) method
4110 
4114 
4115 template <class T>
4117 {
4118  scale_mean_standard_deviation(statistics.mean, statistics.standard_deviation);
4119 }
4120 
4121 
4122 // Statistics<T> scale_mean_standard_deviation(void) method
4123 
4127 
4128 template <class T>
4130 {
4131  const Statistics<T> statistics = calculate_statistics();
4132 
4133  scale_mean_standard_deviation(statistics);
4134 
4135  return(statistics);
4136 }
4137 
4138 
4139 // void scale_minimum_maximum(const Vector<T>&, const Vector<T>&) method
4140 
4146 
4147 template <class T>
4148 void Vector<T>::scale_minimum_maximum(const Vector<T>& minimum, const Vector<T>& maximum)
4149 {
4150  const size_t this_size = this->size();
4151 
4152  #ifndef NDEBUG
4153 
4154  const size_t minimum_size = minimum.size();
4155 
4156  if(minimum_size != this_size)
4157  {
4158  std::ostringstream buffer;
4159 
4160  buffer << "OpenNN Exception: Vector template."
4161  << "void scale_minimum_maximum(const Vector<T>&, const Vector<T>&) method.\n"
4162  << "Size of minimum vector must be equal to size.\n";
4163 
4164  throw std::logic_error(buffer.str());
4165  }
4166 
4167  const size_t maximum_size = maximum.size();
4168 
4169  if(maximum_size != this_size)
4170  {
4171  std::ostringstream buffer;
4172 
4173  buffer << "OpenNN Exception: Vector template."
4174  << "void scale_minimum_maximum(const Vector<T>&, const Vector<T>&) method.\n"
4175  << "Size of maximum vector must be equal to size.\n";
4176 
4177  throw std::logic_error(buffer.str());
4178  }
4179 
4180  #endif
4181 
4182  // Rescale data
4183 
4184  for(size_t i = 0; i < this_size; i++)
4185  {
4186  if(maximum[i] - minimum[i] < 1e-99)
4187  {
4188  std::cout << "OpenNN Warning: Vector class.\n"
4189  << "void scale_minimum_maximum(const Vector<T>&, const Vector<T>&) method.\n"
4190  << "Minimum and maximum values of variable " << i << " are equal.\n"
4191  << "Those elements won't be scaled.\n";
4192 
4193  // Do nothing
4194  }
4195  else
4196  {
4197  (*this)[i] = 2.0*((*this)[i] - minimum[i])/(maximum[i]-minimum[i])-1.0;
4198  }
4199  }
4200 }
4201 
4202 
4203 // void scale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method
4204 
4210 
4211 template <class T>
4212 void Vector<T>::scale_mean_standard_deviation(const Vector<T>& mean, const Vector<T>& standard_deviation)
4213 {
4214  const size_t this_size = this->size();
4215 
4216  #ifndef NDEBUG
4217 
4218  const size_t mean_size = mean.size();
4219 
4220  if(mean_size != this_size)
4221  {
4222  std::ostringstream buffer;
4223 
4224  buffer << "OpenNN Exception: Vector template."
4225  << "void scale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method.\n"
4226  << "Size of mean vector must be equal to size.\n";
4227 
4228  throw std::logic_error(buffer.str());
4229  }
4230 
4231  const size_t standard_deviation_size = standard_deviation.size();
4232 
4233  if(standard_deviation_size != this_size)
4234  {
4235  std::ostringstream buffer;
4236 
4237  buffer << "OpenNN Exception: Vector template."
4238  << "void scale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method.\n"
4239  << "Size of standard deviation vector must be equal to size.\n";
4240 
4241  throw std::logic_error(buffer.str());
4242  }
4243 
4244  #endif
4245 
4246  // Rescale data
4247 
4248  for(size_t i = 0; i < this_size; i++)
4249  {
4250  if(standard_deviation[i] < 1e-99)
4251  {
4252  std::cout << "OpenNN Warning: Vector class.\n"
4253  << "void scale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method.\n"
4254  << "Standard deviation of variable " << i << " is zero.\n"
4255  << "Those elements won't be scaled.\n";
4256 
4257  // Do nothing
4258  }
4259  else
4260  {
4261  (*this)[i] = ((*this)[i] - mean[i])/standard_deviation[i];
4262  }
4263  }
4264 }
4265 
4266 
4267 // Vector<T> calculate_scaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method
4268 
4273 
4274 template <class T>
4276 {
4277  const size_t this_size = this->size();
4278 
4279  #ifndef NDEBUG
4280 
4281  const size_t minimum_size = minimum.size();
4282 
4283  if(minimum_size != this_size)
4284  {
4285  std::ostringstream buffer;
4286 
4287  buffer << "OpenNN Exception: Vector template."
4288  << "Vector<T> calculate_scaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method.\n"
4289  << "Size of minimum vector must be equal to size.\n";
4290 
4291  throw std::logic_error(buffer.str());
4292  }
4293 
4294  const size_t maximum_size = maximum.size();
4295 
4296  if(maximum_size != this_size)
4297  {
4298  std::ostringstream buffer;
4299 
4300  buffer << "OpenNN Exception: Vector template."
4301  << "Vector<T> calculate_scaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method.\n"
4302  << "Size of maximum vector must be equal to size.\n";
4303 
4304  throw std::logic_error(buffer.str());
4305  }
4306 
4307  #endif
4308 
4309  Vector<T> scaled_minimum_maximum(this_size);
4310 
4311  // Rescale data
4312 
4313  for(size_t i = 0; i < this_size; i++)
4314  {
4315  if(maximum[i] - minimum[i] < 1e-99)
4316  {
4317  std::cout << "OpenNN Warning: Vector class.\n"
4318  << "Vector<T> calculate_scaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method.\n"
4319  << "Minimum and maximum values of variable " << i << " are equal.\n"
4320  << "Those elements won't be scaled.\n";
4321 
4322  scaled_minimum_maximum[i] = (*this)[i];
4323  }
4324  else
4325  {
4326  scaled_minimum_maximum[i] = 2.0*((*this)[i] - minimum[i])/(maximum[i]-minimum[i])-1.0;
4327  }
4328  }
4329 
4330  return(scaled_minimum_maximum);
4331 }
4332 
4333 
4334 // Vector<T> calculate_scaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method
4335 
4340 
4341 template <class T>
4343 {
4344  const size_t this_size = this->size();
4345 
4346  #ifndef NDEBUG
4347 
4348  std::ostringstream buffer;
4349 
4350  const size_t mean_size = mean.size();
4351 
4352  if(mean_size != this_size)
4353  {
4354  buffer << "OpenNN Exception: Vector template."
4355  << "Vector<T> calculate_scaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method.\n"
4356  << "Size of mean vector must be equal to size.\n";
4357 
4358  throw std::logic_error(buffer.str());
4359  }
4360 
4361  const size_t standard_deviation_size = standard_deviation.size();
4362 
4363  if(standard_deviation_size != this_size)
4364  {
4365  buffer << "OpenNN Exception: Vector template.\n"
4366  << "Vector<T> calculate_scaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method.\n"
4367  << "Size of standard deviation vector must be equal to size.\n";
4368 
4369  throw std::logic_error(buffer.str());
4370  }
4371 
4372  #endif
4373 
4374  Vector<T> scaled_mean_standard_deviation(this_size);
4375 
4376  for(size_t i = 0; i < this_size; i++)
4377  {
4378  if(standard_deviation[i] < 1e-99)
4379  {
4380  std::cout << "OpenNN Warning: Vector template.\n"
4381  << "Vector<T> calculate_scaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method.\n"
4382  << "Standard deviation of variable " << i << " is zero.\n"
4383  << "Those elements won't be scaled.\n";
4384 
4385  scaled_mean_standard_deviation = (*this)[i];
4386  }
4387  else
4388  {
4389  scaled_mean_standard_deviation[i] = (*this)[i]*standard_deviation[i] + mean[i];
4390  }
4391  }
4392 
4393  return(scaled_mean_standard_deviation);
4394 }
4395 
4396 
4397 // Vector<T> calculate_unscaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method
4398 
4403 
4404 template <class T>
4406 {
4407  const size_t this_size = this->size();
4408 
4409  #ifndef NDEBUG
4410 
4411  const size_t minimum_size = minimum.size();
4412 
4413  if(minimum_size != this_size)
4414  {
4415  std::ostringstream buffer;
4416 
4417  buffer << "OpenNN Exception: Vector template."
4418  << "Vector<T> calculate_unscaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method.\n"
4419  << "Size of minimum vector must be equal to size.\n";
4420 
4421  throw std::logic_error(buffer.str());
4422  }
4423 
4424  const size_t maximum_size = maximum.size();
4425 
4426  if(maximum_size != this_size)
4427  {
4428  std::ostringstream buffer;
4429 
4430  buffer << "OpenNN Exception: Vector template."
4431  << "Vector<T> calculate_unscaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method.\n"
4432  << "Size of maximum vector must be equal to size.\n";
4433 
4434  throw std::logic_error(buffer.str());
4435  }
4436 
4437  #endif
4438 
4439  Vector<T> unscaled_minimum_maximum(this_size);
4440 
4441  for(size_t i = 0; i < this_size; i++)
4442  {
4443  if(maximum[i] - minimum[i] < 1e-99)
4444  {
4445  std::cout << "OpenNN Warning: Vector template.\n"
4446  << "Vector<T> calculate_unscaled_minimum_maximum(const Vector<T>&, const Vector<T>&) const method.\n"
4447  << "Minimum and maximum values of variable " << i << " are equal.\n"
4448  << "Those elements won't be unscaled.\n";
4449 
4450  unscaled_minimum_maximum[i] = (*this)[i];
4451  }
4452  else
4453  {
4454  unscaled_minimum_maximum[i] = 0.5*((*this)[i] + 1.0)*(maximum[i]-minimum[i]) + minimum[i];
4455  }
4456  }
4457 
4458  return(unscaled_minimum_maximum);
4459 }
4460 
4461 
4462 // Vector<T> calculate_unscaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method
4463 
4468 
4469 template <class T>
4471 {
4472  const size_t this_size = this->size();
4473 
4474  #ifndef NDEBUG
4475 
4476  const size_t mean_size = mean.size();
4477 
4478  if(mean_size != this_size)
4479  {
4480  std::ostringstream buffer;
4481 
4482  buffer << "OpenNN Exception: Vector template."
4483  << "Vector<T> calculate_unscaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method.\n"
4484  << "Size of mean vector must be equal to size.\n";
4485 
4486  throw std::logic_error(buffer.str());
4487  }
4488 
4489  const size_t standard_deviation_size = standard_deviation.size();
4490 
4491  if(standard_deviation_size != this_size)
4492  {
4493  std::ostringstream buffer;
4494 
4495  buffer << "OpenNN Exception: Vector template.\n"
4496  << "Vector<T> calculate_unscaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method.\n"
4497  << "Size of standard deviation vector must be equal to size.\n";
4498 
4499  throw std::logic_error(buffer.str());
4500  }
4501 
4502  #endif
4503 
4504  Vector<T> unscaled_mean_standard_deviation(this_size);
4505 
4506  for(size_t i = 0; i < this_size; i++)
4507  {
4508  if(standard_deviation[i] < 1e-99)
4509  {
4510  std::cout << "OpenNN Warning: Vector template.\n"
4511  << "Vector<T> calculate_unscaled_mean_standard_deviation(const Vector<T>&, const Vector<T>&) const method.\n"
4512  << "Standard deviation of variable " << i << " is zero.\n"
4513  << "Those elements won't be scaled.\n";
4514 
4515  unscaled_mean_standard_deviation[i] = (*this)[i];
4516  }
4517  else
4518  {
4519  unscaled_mean_standard_deviation[i] = (*this)[i]*standard_deviation[i] + mean[i];
4520  }
4521  }
4522 
4523  return(unscaled_mean_standard_deviation);
4524 }
4525 
4526 
4527 // void unscale_minimum_maximum(const Vector<T>&, const Vector<T>&) method
4528 
4534 
4535 template <class T>
4536 void Vector<T>::unscale_minimum_maximum(const Vector<T>& minimum, const Vector<T>& maximum)
4537 {
4538  const size_t this_size = this->size();
4539 
4540  #ifndef NDEBUG
4541 
4542  const size_t minimum_size = minimum.size();
4543 
4544  if(minimum_size != this_size)
4545  {
4546  std::ostringstream buffer;
4547 
4548  buffer << "OpenNN Exception: Vector template."
4549  << "void unscale_minimum_maximum(const Vector<T>&, const Vector<T>&) method.\n"
4550  << "Size of minimum vector must be equal to size.\n";
4551 
4552  throw std::logic_error(buffer.str());
4553  }
4554 
4555  const size_t maximum_size = maximum.size();
4556 
4557  if(maximum_size != this_size)
4558  {
4559  std::ostringstream buffer;
4560 
4561  buffer << "OpenNN Exception: Vector template."
4562  << "void unscale_minimum_maximum(const Vector<T>&, const Vector<T>&) method.\n"
4563  << "Size of maximum vector must be equal to size.\n";
4564 
4565  throw std::logic_error(buffer.str());
4566  }
4567 
4568  #endif
4569 
4570  for(size_t i = 0; i < this_size; i++)
4571  {
4572  if(maximum[i] - minimum[i] < 1e-99)
4573  {
4574  std::cout << "OpenNN Warning: Vector template.\n"
4575  << "void unscale_minimum_maximum(const Vector<T>&, const Vector<T>&) method.\n"
4576  << "Minimum and maximum values of variable " << i << " are equal.\n"
4577  << "Those elements won't be unscaled.\n";
4578 
4579  // Do nothing
4580  }
4581  else
4582  {
4583  (*this)[i] = 0.5*((*this)[i] + 1.0)*(maximum[i]-minimum[i]) + minimum[i];
4584  }
4585  }
4586 }
4587 
4588 
4589 // void unscale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method
4590 
4596 
4597 template <class T>
4598 void Vector<T>::unscale_mean_standard_deviation(const Vector<T>& mean, const Vector<T>& standard_deviation)
4599 {
4600  const size_t this_size = this->size();
4601 
4602  #ifndef NDEBUG
4603 
4604  const size_t mean_size = mean.size();
4605 
4606  if(mean_size != this_size)
4607  {
4608  std::ostringstream buffer;
4609 
4610  buffer << "OpenNN Exception: Vector template."
4611  << "void unscale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method.\n"
4612  << "Size of mean vector must be equal to size.\n";
4613 
4614  throw std::logic_error(buffer.str());
4615  }
4616 
4617  const size_t standard_deviation_size = standard_deviation.size();
4618 
4619  if(standard_deviation_size != this_size)
4620  {
4621  std::ostringstream buffer;
4622 
4623  buffer << "OpenNN Exception: Vector template.\n"
4624  << "void unscale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method.\n"
4625  << "Size of standard deviation vector must be equal to size.\n";
4626 
4627  throw std::logic_error(buffer.str());
4628  }
4629 
4630  #endif
4631 
4632  for(size_t i = 0; i < this_size; i++)
4633  {
4634  if(standard_deviation[i] < 1e-99)
4635  {
4636  std::cout << "OpenNN Warning: Vector template.\n"
4637  << "void unscale_mean_standard_deviation(const Vector<T>&, const Vector<T>&) method.\n"
4638  << "Standard deviation of variable " << i << " is zero.\n"
4639  << "Those elements won't be scaled.\n";
4640 
4641  // Do nothing
4642  }
4643  else
4644  {
4645  (*this)[i] = (*this)[i]*standard_deviation[i] + mean[i];
4646  }
4647  }
4648 }
4649 
4650 
4651 // Matrix<T> arrange_diagonal_matrix(void) const method
4652 
4656 
4657 template <class T>
4659 {
4660  const size_t this_size = this->size();
4661 
4662  Matrix<T> matrix = new Matrix<T>(this_size, this_size, 0.0);
4663 
4664  for(size_t i = 0; i < this_size; i++)
4665  {
4666  matrix(i,i) = (*this)[i];
4667  }
4668 
4669  return(matrix);
4670 }
4671 
4672 
4673 // Vector<T> arrange_subvector(const Vector<size_t>&) const
4674 
4677 
4678 template <class T>
4680 {
4681  const size_t new_size = indices.size();
4682 
4683  // Control sentence (if debug)
4684 
4685  #ifndef NDEBUG
4686 
4687  const size_t this_size = this->size();
4688 
4689  for(size_t i = 0; i < new_size; i++)
4690  {
4691  if(indices[i] > this_size)
4692  {
4693  std::ostringstream buffer;
4694 
4695  buffer << "OpenNN Exception: Vector Template.\n"
4696  << "Vector<T> arrange_subvector(const Vector<T>&) const method.\n"
4697  << "Index is equal or greater than this size.\n";
4698 
4699  throw std::logic_error(buffer.str());
4700  }
4701  }
4702 
4703  #endif
4704 
4705  Vector<T> subvector(new_size);
4706 
4707  for(size_t i = 0; i < new_size; i++)
4708  {
4709  subvector[i] = (*this)[indices[i]];
4710  }
4711 
4712  return(subvector);
4713 }
4714 
4715 
4716 // Vector<T> arrange_subvector_first(const size_t&) const method
4717 
4720 
4721 template <class T>
4722 Vector<T> Vector<T>::arrange_subvector_first(const size_t& elements_number) const
4723 {
4724  // Control sentence (if debug)
4725 
4726  #ifndef NDEBUG
4727 
4728  const size_t this_size = this->size();
4729 
4730  if(elements_number > this_size)
4731  {
4732  std::ostringstream buffer;
4733 
4734  buffer << "OpenNN Exception: Vector Template.\n"
4735  << "Vector<T> arrange_subvector_first(const size_t&) const method.\n"
4736  << "Number of elements must be equal or greater than this size.\n";
4737 
4738  throw std::logic_error(buffer.str());
4739  }
4740 
4741  #endif
4742 
4743  Vector<T> subvector(elements_number);
4744 
4745  for(size_t i = 0; i < elements_number; i++)
4746  {
4747  subvector[i] = (*this)[i];
4748  }
4749 
4750  return(subvector);
4751 }
4752 
4753 
4754 // Vector<T> arrange_subvector_last(const size_t&) const method
4755 
4758 
4759 template <class T>
4760 Vector<T> Vector<T>::arrange_subvector_last(const size_t& elements_number) const
4761 {
4762  const size_t this_size = this->size();
4763 
4764  // Control sentence (if debug)
4765 
4766  #ifndef NDEBUG
4767 
4768  if(elements_number > this_size)
4769  {
4770  std::ostringstream buffer;
4771 
4772  buffer << "OpenNN Exception: Vector Template.\n"
4773  << "Vector<T> arrange_subvector_last(const size_t&) const method.\n"
4774  << "Number of elements must be equal or greater than this size.\n";
4775 
4776  throw std::logic_error(buffer.str());
4777  }
4778 
4779  #endif
4780 
4781  Vector<T> subvector(elements_number);
4782 
4783  for(size_t i = 0; i < elements_number; i++)
4784  {
4785  subvector[i] = (*this)[i+this_size-elements_number];
4786  }
4787 
4788  return(subvector);
4789 }
4790 
4791 
4792 // void load(const std::string&) method
4793 
4797 
4798 template <class T>
4799 void Vector<T>::load(const std::string& file_name)
4800 {
4801  std::ifstream file(file_name.c_str());
4802 
4803  std::stringstream buffer;
4804 
4805  std::string line;
4806 
4807  while(file.good())
4808  {
4809  getline(file, line);
4810 
4811  buffer << line;
4812  }
4813 
4814  std::istream_iterator<std::string> it(buffer);
4815  std::istream_iterator<std::string> end;
4816 
4817  const std::vector<std::string> results(it, end);
4818 
4819  const size_t new_size = (size_t)results.size();
4820 
4821  this->resize(new_size);
4822 
4823  file.clear();
4824  file.seekg(0, std::ios::beg);
4825 
4826  // Read data
4827 
4828  for(size_t i = 0; i < new_size; i++)
4829  {
4830  file >> (*this)[i];
4831  }
4832 
4833  file.close();
4834 }
4835 
4836 
4837 // void save(const std::string&) const method
4838 
4843 
4844 template <class T>
4845 void Vector<T>::save(const std::string& file_name) const
4846 {
4847  std::ofstream file(file_name.c_str());
4848 
4849  if(!file.is_open())
4850  {
4851  std::ostringstream buffer;
4852 
4853  buffer << "OpenNN Exception: Vector template.\n"
4854  << "void save(const std::string&) const method.\n"
4855  << "Cannot open vector data file.\n";
4856 
4857  throw std::logic_error(buffer.str());
4858  }
4859 
4860  // Write file
4861 
4862  const size_t this_size = this->size();
4863 
4864  if(this_size > 0)
4865  {
4866  file << (*this)[0];
4867 
4868  const char space = ' ';
4869 
4870  for(size_t i = 1; i < this_size; i++)
4871  {
4872  file << space << (*this)[i];
4873  }
4874 
4875  file << std::endl;
4876  }
4877 
4878  // Close file
4879 
4880  file.close();
4881 }
4882 
4883 
4884 // void tuck_in(const size_t&, const Vector<T>&) const method
4885 
4889 
4890 template <class T>
4891 void Vector<T>::tuck_in(const size_t& position, const Vector<T>& other_vector)
4892 {
4893  const size_t other_size = other_vector.size();
4894 
4895  // Control sentence (if debug)
4896 
4897  #ifndef NDEBUG
4898 
4899  const size_t this_size = this->size();
4900 
4901  if(position + other_size > this_size)
4902  {
4903  std::ostringstream buffer;
4904 
4905  buffer << "OpenNN Exception: Vector Template.\n"
4906  << "void tuck_in(const size_t&, const Vector<T>&) const method.\n"
4907  << "Cannot tuck in vector.\n";
4908 
4909  throw std::logic_error(buffer.str());
4910  }
4911 
4912  #endif
4913 
4914  for(size_t i = 0; i < other_size; i++)
4915  {
4916  (*this)[position + i] = other_vector[i];
4917  }
4918 }
4919 
4920 
4921 // Vector<T> take_out(const size_t&, const size_t&) method
4922 
4926 
4927 template <class T>
4928 Vector<T> Vector<T>::take_out(const size_t& position, const size_t& other_size) const
4929 {
4930  // Control sentence (if debug)
4931 
4932  #ifndef NDEBUG
4933 
4934  const size_t this_size = this->size();
4935 
4936  if(position + other_size > this_size)
4937  {
4938  std::ostringstream buffer;
4939 
4940  buffer << "OpenNN Exception: Vector Template.\n"
4941  << "Vector<T> take_out(const size_t&, const size_t&) method.\n"
4942  << "Cannot take out vector.\n";
4943 
4944  throw std::logic_error(buffer.str());
4945  }
4946 
4947  #endif
4948 
4949  const Vector<T> other_vector((*this).begin() + position, (*this).begin() + position + other_size);
4950 
4951 // for(size_t i = 0; i < other_size; i++)
4952 // {
4953 // other_vector[i] = (*this)[position + i];
4954 // }
4955 
4956  return(other_vector);
4957 }
4958 
4959 
4960 // void insert_element(const size_t& index, const T& value) method
4961 
4965 
4966 template <class T>
4967 Vector<T> Vector<T>::insert_element(const size_t& index, const T& value) const
4968 {
4969  const size_t this_size = this->size();
4970 
4971  // Control sentence (if debug)
4972 
4973  #ifndef NDEBUG
4974 
4975  if(index > this_size)
4976  {
4977  std::ostringstream buffer;
4978 
4979  buffer << "OpenNN Exception: Vector Template.\n"
4980  << "void insert_element(const size_t& index, const T& value) method.\n"
4981  << "Index is greater than vector size.\n";
4982 
4983  throw std::logic_error(buffer.str());
4984  }
4985 
4986  #endif
4987 
4988  Vector<T> other_vector(this_size+1);
4989 
4990  for(size_t i = 0; i <= this_size; i++)
4991  {
4992  if(i < index)
4993  {
4994  other_vector[i] = (*this)[i];
4995  }
4996  if(i == index)
4997  {
4998  other_vector[i] = value;
4999  }
5000  else if(i > index)
5001  {
5002  other_vector[i] = (*this)[i-1];
5003  }
5004  }
5005 
5006  return(other_vector);
5007 }
5008 
5009 
5010 // Vector<T> remove_element(const size_t) const
5011 
5015 
5016 template <class T>
5017 Vector<T> Vector<T>::remove_element(const size_t& index) const
5018 {
5019  const size_t this_size = this->size();
5020 
5021  // Control sentence (if debug)
5022 
5023  #ifndef NDEBUG
5024 
5025  if(index >= this_size)
5026  {
5027  std::ostringstream buffer;
5028 
5029  buffer << "OpenNN Exception: Vector Template.\n"
5030  << "Vector<T> remove_element(const size_t&) const method.\n"
5031  << "Index is equal or greater than vector size.\n";
5032 
5033  throw std::logic_error(buffer.str());
5034  }
5035 
5036  #endif
5037 
5038  Vector<T> other_vector(this_size-1);
5039 
5040  for(size_t i = 0; i < this_size; i++)
5041  {
5042  if(i < index)
5043  {
5044  other_vector[i] = (*this)[i];
5045  }
5046  else if(i > index)
5047  {
5048  other_vector[i-1] = (*this)[i];
5049  }
5050  }
5051 
5052  return(other_vector);
5053 }
5054 
5055 
5056 // Vector<T> remove_value(const T&) const
5057 
5061 
5062 template <class T>
5063 Vector<T> Vector<T>::remove_value(const T& value) const
5064 {
5065  const size_t this_size = this->size();
5066 
5067  size_t value_count = 0;
5068 
5069  for(size_t i = 0; i < this_size; i++)
5070  {
5071  if((*this)[i] == value)
5072  {
5073  value_count++;
5074  }
5075  }
5076 
5077  if(value_count == 0)
5078  {
5079  return(*this);
5080  }
5081  else
5082  {
5083  const size_t other_size = this_size - value_count;
5084 
5085  Vector<T> other_vector(other_size);
5086 
5087  size_t other_index = 0;
5088 
5089  for(size_t i = 0; i < this_size; i++)
5090  {
5091  if((*this)[i] != value)
5092  {
5093  other_vector[other_index] = (*this)[i];
5094 
5095  other_index++;
5096  }
5097  }
5098 
5099  return(other_vector);
5100  }
5101 }
5102 
5103 
5104 // Vector<T> assemble(const Vector<T>&) const method
5105 
5108 
5109 template <class T>
5110 Vector<T> Vector<T>::assemble(const Vector<T>& other_vector) const
5111 {
5112  const size_t this_size = this->size();
5113  const size_t other_size = other_vector.size();
5114 
5115  if(this_size == 0 && other_size == 0)
5116  {
5117  Vector<T> assembly;
5118 
5119  return(assembly);
5120  }
5121  else if(this_size == 0)
5122  {
5123  return(other_vector);
5124  }
5125  else if(other_size == 0)
5126  {
5127  return(*this);
5128  }
5129  else
5130  {
5131  Vector<T> assembly(this_size + other_size);
5132 
5133  for(size_t i = 0; i < this_size; i++)
5134  {
5135  assembly[i] = (*this)[i];
5136  }
5137 
5138  for(size_t i = 0; i < other_size; i++)
5139  {
5140  assembly[this_size+i] = other_vector[i];
5141  }
5142 
5143  return(assembly);
5144  }
5145 }
5146 
5147 
5148 // std::vector<T> to_std_vector(void) const method
5149 
5151 
5152 template <class T>
5153 std::vector<T> Vector<T>::to_std_vector(void) const
5154 {
5155  const size_t this_size = this->size();
5156 
5157  std::vector<T> std_vector(this_size);
5158 
5159  for(size_t i = 0; i < this_size; i++)
5160  {
5161  std_vector[i] = (*this)[i];
5162  }
5163 
5164 
5165  return(std_vector);
5166 }
5167 
5168 
5169 // Matrix<T> to_row_matrix(void) const method
5170 
5173 
5174 template <class T>
5176 {
5177  const size_t this_size = this->size();
5178 
5179  Matrix<T> matrix(1, this_size);
5180 
5181  for(size_t i = 0; i < this_size; i++)
5182  {
5183  matrix(0,i) = (*this)[i];
5184  }
5185 
5186  return(matrix);
5187 }
5188 
5189 
5190 // Matrix<T> to_column_matrix(void) const method
5191 
5194 
5195 template <class T>
5197 {
5198  const size_t this_size = this->size();
5199 
5200  Matrix<T> matrix(this_size, 1);
5201 
5202  for(size_t i = 0; i < this_size; i++)
5203  {
5204  matrix(i,0) = (*this)[i];
5205  }
5206 
5207  return(matrix);
5208 }
5209 
5210 
5211 // void parse(const std::string&) method
5212 
5215 
5216 template <class T>
5217 void Vector<T>::parse(const std::string& str)
5218 {
5219  if(str.empty())
5220  {
5221  set();
5222  }
5223  else
5224  {
5225  std::istringstream buffer(str);
5226 
5227  std::istream_iterator<std::string> first(buffer);
5228  std::istream_iterator<std::string> last;
5229 
5230  Vector<std::string> str_vector(first, last);
5231 
5232  const size_t new_size = str_vector.size();
5233 
5234  if(new_size > 0)
5235  {
5236  this->resize(new_size);
5237 
5238  buffer.clear();
5239  buffer.seekg(0, std::ios::beg);
5240 
5241  for(size_t i = 0; i < new_size; i++)
5242  {
5243  buffer >> (*this)[i];
5244  }
5245  }
5246  }
5247 }
5248 
5249 
5250 // std::string to_string(const std::string&)
5251 
5253 
5254 template <class T>
5255 std::string Vector<T>::to_string(const std::string& separator) const
5256 {
5257  std::ostringstream buffer;
5258 
5259  const size_t this_size = this->size();
5260 
5261  if(this_size > 0)
5262  {
5263  buffer << (*this)[0];
5264 
5265  for(size_t i = 1; i < this_size; i++)
5266  {
5267  buffer << separator << (*this)[i];
5268  }
5269  }
5270 
5271  return(buffer.str());
5272 }
5273 
5274 
5275 // std::string to_text()
5276 
5278 
5279 template <class T>
5280 std::string Vector<T>::to_text() const
5281 {
5282  std::ostringstream buffer;
5283 
5284  const size_t this_size = this->size();
5285 
5286  if(this_size > 0)
5287  {
5288  buffer << (*this)[0];
5289 
5290  for(size_t i = 1; i < this_size-1; i++)
5291  {
5292  buffer << ", " << (*this)[i];
5293  }
5294 
5295  if(this_size > 1)
5296  {
5297  buffer << " and " << (*this)[this_size-1];
5298  }
5299  }
5300 
5301  return(buffer.str());
5302 }
5303 
5304 
5305 // Vector<std::string> write_string_vector(const size_t& precision) const
5306 
5308 
5309 template <class T>
5311 {
5312  const size_t this_size = this->size();
5313 
5314  Vector<std::string> string_vector(this_size);
5315 
5316  std::ostringstream buffer;
5317 
5318  for(size_t i = 0; i < this_size; i++)
5319  {
5320  buffer.str("");
5321  buffer << std::setprecision(precision) << (*this)[i];
5322 
5323  string_vector[i] = buffer.str();
5324  }
5325 
5326  return(string_vector);
5327 }
5328 
5329 
5330 // Matrix<T> to_matrix(const size_t&, const size_t&) method
5331 
5336 
5337 template <class T>
5338 Matrix<T> Vector<T>::to_matrix(const size_t& rows_number, const size_t& columns_number) const
5339 {
5340  // Control sentence (if debug)
5341 
5342  #ifndef NDEBUG
5343 
5344  const size_t this_size = this->size();
5345 
5346  if(rows_number*columns_number != this_size)
5347  {
5348  std::ostringstream buffer;
5349 
5350  buffer << "OpenNN Exception: Vector Template.\n"
5351  << "Matrix<T> to_matrix(const size_t&, const size_t&) method.\n"
5352  << "The number of rows (" << rows_number << ") times the number of colums (" << columns_number <<") must be equal to the size of the vector (" << this_size << ").\n";
5353 
5354  throw std::logic_error(buffer.str());
5355  }
5356 
5357  #endif
5358 
5359  Matrix<T> matrix(rows_number, columns_number);
5360 
5361  size_t index = 0;
5362 
5363  for(size_t i = 0; i < rows_number; i++)
5364  {
5365  for(size_t j = 0; j < columns_number; j++)
5366  {
5367  matrix(i,j) = (*this)[index];
5368  index++;
5369  }
5370  }
5371 
5372  return(matrix);
5373 }
5374 
5375 
5376 // Vector input operator
5377 
5381 
5382 template<class T>
5383 std::istream& operator >> (std::istream& is, Vector<T>& v)
5384 {
5385  const size_t size = v.size();
5386 
5387  for(size_t i = 0; i < size; i++)
5388  {
5389  is >> v[i];
5390  }
5391 
5392  return(is);
5393 }
5394 
5395 
5396 // Vector output operator
5397 
5401 
5402 template<class T>
5403 std::ostream& operator << (std::ostream& os, const Vector<T>& v)
5404 {
5405  const size_t this_size = v.size();
5406 
5407  if(this_size > 0)
5408  {
5409  os << v[0];
5410 
5411  const char space = ' ';
5412 
5413  for(size_t i = 1; i < this_size; i++)
5414  {
5415  os << space << v[i];
5416  }
5417  }
5418 
5419  return(os);
5420 }
5421 
5422 
5423 // Vector of vectors output operator
5424 
5428 
5429 template<class T>
5430 std::ostream& operator << (std::ostream& os, const Vector< Vector<T> >& v)
5431 {
5432  for(size_t i = 0; i < v.size(); i++)
5433  {
5434  os << "subvector_" << i << "\n"
5435  << v[i] << std::endl;
5436  }
5437 
5438  return(os);
5439 }
5440 
5441 
5442 // Vector of matrices output operator
5443 
5447 
5448 template<class T>
5449 std::ostream& operator << (std::ostream& os, const Vector< Matrix<T> >& v)
5450 {
5451  for(size_t i = 0; i < v.size(); i++)
5452  {
5453  os << "submatrix_" << i << "\n"
5454  << v[i];
5455  }
5456 
5457  return(os);
5458 }
5459 
5460 
5461 // double calculate_random_uniform(const double&, const double&) method
5462 
5466 
5467 template<class T>
5468 T calculate_random_uniform(const T& minimum, const T& maximum)
5469 {
5470  const T random = (T)rand()/(RAND_MAX+1.0);
5471 
5472  const T random_uniform = minimum + (maximum-minimum)*random;
5473 
5474  return(random_uniform);
5475 }
5476 
5477 
5478 // double calculate_random_normal(const double&, const double&) method
5479 
5483 
5484 template<class T>
5485 T calculate_random_normal(const T& mean, const T& standard_deviation)
5486 {
5487  const double pi = 4.0*atan(1.0);
5488 
5489  T random_uniform_1;
5490 
5491  do
5492  {
5493  random_uniform_1 = (T)rand()/(RAND_MAX+1.0);
5494 
5495  }while(random_uniform_1 == 0.0);
5496 
5497  const T random_uniform_2 = (T)rand()/(RAND_MAX+1.0);
5498 
5499  // Box-Muller transformation
5500 
5501  const T random_normal = mean + sqrt(-2.0*log(random_uniform_1))*sin(2.0*pi*random_uniform_2)*standard_deviation;
5502 
5503  return(random_normal);
5504 }
5505 
5508 
5509 template <class T>
5510 struct Statistics
5511 {
5512  // Default constructor.
5513 
5514  Statistics(void);
5515 
5516  // Values constructor.
5517 
5518  Statistics(const T&, const T&, const T&, const T&);
5519 
5521 
5522  virtual ~Statistics(void);
5523 
5524  // METHODS
5525 
5526  void set_minimum(const double&);
5527 
5528  void set_maximum(const double&);
5529 
5530  void set_mean(const double&);
5531 
5532  void set_standard_deviation(const double&);
5533 
5534  Vector<T> to_vector(void) const;
5535 
5536  void initialize_random(void);
5537 
5540 
5541  void save(const std::string& file_name) const;
5542 
5543 
5545 
5547 
5549 
5551 
5553 
5554  T mean;
5555 
5557 
5559 };
5560 
5561 
5562 
5563 template <class T>
5565 {
5566  minimum = (T)-1.0;
5567  maximum = (T)1.0;
5568  mean = (T)0.0;
5569  standard_deviation = (T)1.0;
5570 }
5571 
5573 
5574 template <class T>
5575 Statistics<T>::Statistics(const T& new_minimum, const T& new_maximum, const T& new_mean, const T& new_standard_deviation)
5576 {
5577  minimum = new_minimum;
5578  maximum = new_maximum;
5579  mean = new_mean;
5580  standard_deviation = new_standard_deviation;
5581 }
5582 
5584 
5585 template <class T>
5587 {
5588 }
5589 
5592 
5593 template <class T>
5594 void Statistics<T>::set_minimum(const double& new_minimum)
5595 {
5596  minimum = new_minimum;
5597 }
5598 
5601 
5602 template <class T>
5603 void Statistics<T>::set_maximum(const double& new_maximum)
5604 {
5605  maximum = new_maximum;
5606 }
5607 
5610 
5611 template <class T>
5612 void Statistics<T>::set_mean(const double& new_mean)
5613 {
5614  mean = new_mean;
5615 }
5616 
5619 
5620 template <class T>
5621 void Statistics<T>::set_standard_deviation(const double& new_standard_deviation)
5622 {
5623  standard_deviation = new_standard_deviation;
5624 }
5625 
5629 
5630 template <class T>
5632 {
5633  Vector<T> statistics_vector(4);
5634  statistics_vector[0] = minimum;
5635  statistics_vector[1] = maximum;
5636  statistics_vector[2] = mean;
5637  statistics_vector[3] = standard_deviation;
5638 
5639  return(statistics_vector);
5640 }
5641 
5642 
5646 
5647 
5648 template <class T>
5650 {
5651  minimum = calculate_random_uniform(-1.0, 0.0);
5652  maximum = calculate_random_uniform(0.0, 1.0);
5653  mean = calculate_random_uniform(-1.0, 1.0);
5654  standard_deviation = calculate_random_uniform(0.0, 2.0);
5655 }
5656 
5659 
5660 template <class T>
5662 {
5663  if(-1.000001 < minimum && minimum < -0.999999
5664  && 0.999999 < maximum && maximum < 1.000001)
5665  {
5666  return(true);
5667  }
5668  else
5669  {
5670  return(false);
5671  }
5672 }
5673 
5676 
5677 template <class T>
5679 {
5680  if(-0.000001 < mean && mean < 0.000001
5681  && 0.999999 < standard_deviation && standard_deviation < 1.000001)
5682  {
5683  return(true);
5684  }
5685  else
5686  {
5687  return(false);
5688  }
5689 }
5690 
5691 
5695 
5696 template <class T>
5697 void Statistics<T>::save(const std::string& file_name) const
5698 {
5699  std::ofstream file(file_name.c_str());
5700 
5701  if(!file.is_open())
5702  {
5703  std::ostringstream buffer;
5704 
5705  buffer << "OpenNN Exception: Statistics template.\n"
5706  << "void save(const std::string&) const method.\n"
5707  << "Cannot open statistics data file.\n";
5708 
5709  throw std::logic_error(buffer.str());
5710  }
5711 
5712  // Write file
5713 
5714  file << "Minimum: " << minimum << std::endl
5715  << "Maximum: " << maximum << std::endl
5716  << "Mean: " << mean << std::endl
5717  << "Standard deviation: " << standard_deviation << std::endl;
5718 
5719  // Close file
5720 
5721  file.close();
5722 }
5723 
5724 
5725 // Statistics output operator
5726 
5730 
5731 template<class T>
5732 std::ostream& operator << (std::ostream& os, const Statistics<T>& statistics)
5733 {
5734  os << "Statistics structure\n"
5735  << " Minimum: " << statistics.minimum << std::endl
5736  << " Maximum: " << statistics.maximum << std::endl
5737  << " Mean: " << statistics.mean << std::endl
5738  << " Standard deviation: " << statistics.standard_deviation << std::endl;
5739 
5740  return(os);
5741 }
5742 
5743 
5747 
5748 template <class T>
5749 struct Histogram
5750 {
5752 
5753  explicit Histogram(void);
5754 
5756 
5757  virtual ~Histogram(void);
5758 
5760 
5761  Histogram(const size_t&);
5762 
5764 
5765  Histogram(const Vector<T>&, const Vector<size_t>&);
5766 
5767  size_t get_bins_number(void) const;
5768 
5769  size_t count_empty_bins(void) const;
5770 
5771  size_t calculate_minimum_frequency(void) const;
5772 
5773  size_t calculate_maximum_frequency(void) const;
5774 
5775  Vector<T> calculate_minimal_centers(void) const;
5776 
5777  Vector<T> calculate_maximal_centers(void) const;
5778 
5780 
5782 
5784 
5786 };
5787 
5788 
5789 template <class T>
5791 {
5792 }
5793 
5794 
5796 
5797 template <class T>
5799 {
5800 }
5801 
5802 
5805 
5806 template <class T>
5807 Histogram<T>::Histogram(const size_t& bins_number)
5808 {
5809  centers.resize(bins_number);
5810  frequencies.resize(bins_number);
5811 }
5812 
5813 
5817 
5818 template <class T>
5819 Histogram<T>::Histogram(const Vector<T>& new_centers, const Vector<size_t>& new_frequencies)
5820 {
5821  centers = new_centers;
5822  frequencies = new_frequencies;
5823 }
5824 
5825 
5827 
5828 template <class T>
5830 {
5831  return(centers.size());
5832 }
5833 
5834 
5836 
5837 template <class T>
5839 {
5840  return(frequencies.count_occurrences(0));
5841 }
5842 
5843 
5845 
5846 template <class T>
5848 {
5849  return(frequencies.calculate_minimum());
5850 }
5851 
5852 
5854 
5855 template <class T>
5857 {
5858  return(frequencies.calculate_maximum());
5859 }
5860 
5861 
5863 
5864 template <class T>
5866 {
5867  const size_t minimum_frequency = calculate_minimum_frequency();
5868 
5869  const Vector<size_t> minimal_indices = frequencies.calculate_occurrence_indices(minimum_frequency);
5870 
5871  return(centers.arrange_subvector(minimal_indices));
5872 }
5873 
5874 
5876 
5877 template <class T>
5879 {
5880  const size_t maximum_frequency = calculate_maximum_frequency();
5881 
5882  const Vector<size_t> maximal_indices = frequencies.calculate_occurrence_indices(maximum_frequency);
5883 
5884  return(centers.arrange_subvector(maximal_indices));
5885 }
5886 
5887 
5888 // Histogram output operator
5889 
5893 
5894 template<class T>
5895 std::ostream& operator << (std::ostream& os, const Histogram<T>& histogram)
5896 {
5897  os << "Histogram structure\n"
5898  << "Centers: " << histogram.centers << std::endl
5899  << "Frequencies: " << histogram.frequencies << std::endl;
5900 
5901  return(os);
5902 }
5903 
5904 
5908 
5909 template <class T>
5910 struct LinearRegressionParameters
5911 {
5913 
5914  double intercept;
5915 
5917 
5918  double slope;
5919 
5921 
5922  double correlation;
5923 };
5924 
5925 
5926 template<class T>
5927 std::ostream& operator << (std::ostream& os, const LinearRegressionParameters<T>& linear_regression_parameters)
5928 {
5929  os << "Linear regression parameters:\n"
5930  << "Intercept: " << linear_regression_parameters.intercept << "\n"
5931  << "Slope: " << linear_regression_parameters.slope << "\n"
5932  << "Correlation: " << linear_regression_parameters.correlation << std::endl;
5933 
5934  return(os);
5935 }
5936 
5937 
5941 
5942 template <class T>
5943 struct LogisticRegressionParameters
5944 {
5946 
5947  double position;
5948 
5950 
5951  double slope;
5952 
5954 
5955  double correlation;
5956 };
5957 
5958 
5959 template<class T>
5960 std::ostream& operator << (std::ostream& os, const LogisticRegressionParameters<T>& logistic_regression_parameters)
5961 {
5962  os << "Logistic regression parameters:\n"
5963  << "Position: " << logistic_regression_parameters.position << "\n"
5964  << "Slope: " << logistic_regression_parameters.slope << "\n"
5965  << "Correlation: " << logistic_regression_parameters.correlation << std::endl;
5966 
5967  return(os);
5968 }
5969 
5970 }// end namespace OpenNN
5971 
5972 #endif
5973 
5974 
5975 // OpenNN: Open Neural Networks Library.
5976 // Copyright (c) 2005-2015 Roberto Lopez.
5977 //
5978 // This library is free software; you can redistribute it and/or
5979 // modify it under the terms of the GNU Lesser General Public
5980 // License as published by the Free Software Foundation; either
5981 // version 2.1 of the License, or any later version.
5982 //
5983 // This library is distributed in the hope that it will be useful,
5984 // but WITHOUT ANY WARRANTY; without even the implied warranty of
5985 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5986 // Lesser General Public License for more details.
5987 
5988 // You should have received a copy of the GNU Lesser General Public
5989 // License along with this library; if not, write to the Free Software
5990 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
double calculate_Minkowski_error(const Vector< double > &, const double &) const
Definition: vector.h:2613
Vector< bool > calculate_binary(void) const
Definition: vector.h:1865
void parse(const std::string &)
Definition: vector.h:5217
size_t count_less_than(const T &) const
Definition: vector.h:1171
Statistics< T > scale_minimum_maximum(void)
Definition: vector.h:4076
bool has_minimum_minus_one_maximum_one(void)
Definition: vector.h:5661
virtual ~Statistics(void)
Destructor.
Definition: vector.h:5586
void randomize_uniform(const double &=-1.0, const double &=1.0)
Definition: vector.h:781
bool is_in(const T &, const T &) const
Definition: vector.h:1005
void set_minimum(const double &)
Definition: vector.h:5594
size_t calculate_maximum_frequency(void) const
Returns the number of variates in the most populated bin.
Definition: vector.h:5856
Matrix< T > direct(const Matrix< T > &) const
Definition: matrix.h:5872
Vector< T > calculate_minimum_maximum(void) const
Returns a vector containing the smallest and the largest elements in the vector.
Definition: vector.h:1297
Vector< T > calculate_lower_bounded(const T &) const
Definition: vector.h:2950
Vector< std::string > write_string_vector(const size_t &=3) const
This method retuns a vector of strings with size equal to the size of this vector and elements equal ...
Definition: vector.h:5310
bool is_crescent(void) const
Returns true if all the elements in the vector have values which increase with the index...
Definition: vector.h:1056
void initialize(const T &)
Definition: vector.h:753
Vector< T > take_out(const size_t &, const size_t &) const
Definition: vector.h:4928
Vector< T > arrange_subvector_first(const size_t &) const
Definition: vector.h:4722
Vector< T > operator-(const T &) const
Definition: vector.h:3448
void operator+=(const T &)
Definition: vector.h:3795
bool is_constant(const double &=0.0) const
Definition: vector.h:1028
T calculate_linear_correlation_missing_values(const Vector< T > &, const Vector< size_t > &) const
Definition: vector.h:2749
void operator*=(const T &)
Definition: vector.h:3901
double correlation
Correlation coefficient (R-value) of the linear regression.
Definition: vector.h:5922
void unscale_minimum_maximum(const Vector< T > &, const Vector< T > &)
Definition: vector.h:4536
Statistics< T > calculate_statistics_missing_values(const Vector< size_t > &) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
Definition: vector.h:2322
void operator-=(const T &)
Definition: vector.h:3848
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
Vector< size_t > calculate_maximal_indices(const size_t &) const
Definition: vector.h:1700
bool operator<(const T &) const
Definition: vector.h:586
Vector< T > calculate_absolute_value(void) const
Returns a vector with the absolute values of the current vector.
Definition: vector.h:2903
void set_mean(const double &)
Definition: vector.h:5612
Vector< T > calculate_softmax(void) const
Definition: vector.h:1807
Vector< T > arrange_subvector(const Vector< size_t > &) const
Definition: vector.h:4679
Vector< T > remove_value(const T &) const
Definition: vector.h:5063
T calculate_maximum_missing_values(const Vector< size_t > &) const
Returns the largest element in the vector.
Definition: vector.h:1363
Matrix< T > calculate_norm_Hessian(void) const
Returns the Hessian of the vector norm.
Definition: vector.h:2410
T mean
Mean value of a set, function, etc.
Definition: vector.h:5554
Vector< T > centers
Positions of the bins in the histogram.
Definition: vector.h:5781
bool has_mean_zero_standard_deviation_one(void)
Definition: vector.h:5678
Vector< T > calculate_scaled_minimum_maximum(const Vector< T > &, const Vector< T > &) const
Definition: vector.h:4275
void unscale_mean_standard_deviation(const Vector< T > &, const Vector< T > &)
Definition: vector.h:4598
bool contains(const T &) const
Returns true if the vector contains a certain value, and false otherwise.
Definition: vector.h:950
Vector(void)
Default constructor. It creates a vector of size zero.
Definition: vector.h:439
LinearRegressionParameters< T > calculate_linear_regression_parameters(const Vector< T > &) const
Definition: vector.h:2830
Histogram< T > calculate_histogram(const size_t &=10) const
Definition: vector.h:1445
Vector< T > calculate_upper_bounded(const T &) const
Definition: vector.h:3027
T maximum
Biggest value of a set, function, etc.
Definition: vector.h:5550
void filter_positive(void)
Sets all the negative elements in the vector to zero.
Definition: vector.h:4006
size_t count_greater_than(const T &) const
Definition: vector.h:1147
T calculate_minimum(void) const
Returns the smallest element in the vector.
Definition: vector.h:1242
Vector< T > calculate_cumulative(void) const
Definition: vector.h:1893
Vector< T > assemble(const Vector< T > &) const
Definition: vector.h:5110
void apply_lower_bound(const T &)
Definition: vector.h:3194
Vector< T > calculate_lower_upper_bounded(const T &, const T &) const
Definition: vector.h:3106
void operator/=(const T &)
Definition: vector.h:3954
Matrix< T > arrange_diagonal_matrix(void) const
Definition: vector.h:4658
Vector< T > calculate_maximal_centers(void) const
Returns a vector with the centers of the most populated bins.
Definition: vector.h:5878
Matrix< T > direct(const Vector< T > &) const
Definition: vector.h:3697
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
Definition: matrix.h:1090
bool operator>=(const T &) const
Definition: vector.h:609
size_t calculate_cumulative_index(const T &) const
Definition: vector.h:1920
void filter_negative(void)
Sets all the positive elements in the vector to zero.
Definition: vector.h:4023
double calculate_norm(void) const
Returns the vector norm.
Definition: vector.h:2358
size_t calculate_maximal_index(void) const
Returns the index of the largest element in the vector.
Definition: vector.h:1645
double intercept
Y-intercept of the linear regression.
Definition: vector.h:5914
size_t get_bins_number(void) const
Returns the number of bins in the histogram.
Definition: vector.h:5829
void initialize_sequential(void)
Initializes all the elements of the vector in a sequential order.
Definition: vector.h:764
Vector< size_t > calculate_greater_rank(void) const
Definition: vector.h:3360
double calculate_mean_missing_values(const Vector< size_t > &) const
Returns the mean of the elements in the vector.
Definition: vector.h:2186
virtual ~Vector(void)
Destructor.
Definition: vector.h:505
double calculate_standard_deviation(void) const
Returns the standard deviation of the elements in the vector.
Definition: vector.h:2102
double calculate_sum_squared_error(const Vector< double > &) const
Definition: vector.h:2569
Vector< T > operator*(const T &) const
Definition: vector.h:3503
double slope
Slope at the midpoint of the logistic regression.
Definition: vector.h:5951
std::string to_text() const
Returns a string representation of this vector which can be inserted in a text.
Definition: vector.h:5280
Vector< size_t > calculate_greater_than_indices(const T &) const
Definition: vector.h:1219
void initialize(const T &)
Definition: matrix.h:2510
Vector< size_t > calculate_less_than_indices(const T &) const
Definition: vector.h:1195
size_t calculate_minimum_frequency(void) const
Returns the number of variates in the less populated bin.
Definition: vector.h:5847
Vector< T > operator/(const T &) const
Definition: vector.h:3740
Vector< double > calculate_mean_standard_deviation(void) const
Returns the mean and the standard deviation of the elements in the vector.
Definition: vector.h:2149
bool operator>(const T &) const
Definition: vector.h:563
Vector< T > calculate_normalized(void) const
Returns this vector divided by its norm.
Definition: vector.h:2530
Vector< T > operator+(const T &) const
Definition: vector.h:3391
Matrix< T > to_row_matrix(void) const
Definition: vector.h:5175
void tuck_in(const size_t &, const Vector< T > &)
Definition: vector.h:4891
Vector< T > remove_element(const size_t &) const
Definition: vector.h:5017
Vector< T > calculate_unscaled_minimum_maximum(const Vector< T > &, const Vector< T > &) const
Definition: vector.h:4405
size_t calculate_minimal_index(void) const
Returns the index of the smallest element in the vector.
Definition: vector.h:1620
void save(const std::string &) const
Definition: vector.h:4845
Vector< T > calculate_competitive(void) const
Definition: vector.h:1787
Matrix< T > to_matrix(const size_t &, const size_t &) const
Definition: vector.h:5338
void randomize_normal(const double &=0.0, const double &=1.0)
Definition: vector.h:867
double dot(const Vector< double > &) const
Definition: vector.h:3654
double slope
Slope of the linear regression.
Definition: vector.h:5918
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
Definition: matrix.h:1079
void save(const std::string &file_name) const
Definition: vector.h:5697
T calculate_maximum(void) const
Returns the largest element in the vector.
Definition: vector.h:1265
Matrix< T > calculate_softmax_Jacobian(void) const
Returns the softmax Jacobian of this vector.
Definition: vector.h:1834
T calculate_linear_correlation(const Vector< T > &) const
Definition: vector.h:2675
void apply_absolute_value(void)
Sets the elements of the vector to their absolute values.
Definition: vector.h:2930
double calculate_distance(const Vector< double > &) const
Definition: vector.h:2557
double position
Position of the midpoint of the logistic regression.
Definition: vector.h:5947
T calculate_minimum_missing_values(const Vector< size_t > &) const
Returns the smallest element in the vector.
Definition: vector.h:1340
bool operator<=(const T &) const
Definition: vector.h:632
Histogram< T > calculate_histogram_missing_values(const Vector< size_t > &, const size_t &=10) const
Definition: vector.h:1533
void load(const std::string &)
Definition: vector.h:4799
bool operator!=(const T &) const
Definition: vector.h:540
Vector< size_t > frequencies
Population of the bins in the histogram.
Definition: vector.h:5785
T minimum
Smallest value of a set, function, etc.
Definition: vector.h:5546
void apply_upper_bound(const T &)
Definition: vector.h:3234
double calculate_p_norm(const double &) const
Returns the vector p-norm.
Definition: vector.h:2439
Statistics< T > scale_mean_standard_deviation(void)
Definition: vector.h:4129
Vector< size_t > calculate_less_rank(void) const
Definition: vector.h:3328
Statistics< T > calculate_statistics(void) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
Definition: vector.h:2285
T calculate_sum_missing_values(const Vector< size_t > &) const
Returns the sum of the elements in the vector.
Definition: vector.h:2025
Vector< T > insert_element(const size_t &, const T &) const
Definition: vector.h:4967
Vector< size_t > calculate_minimal_indices(const size_t &) const
Definition: vector.h:1671
Vector< double > calculate_p_norm_gradient(const double &) const
Returns the gradient of the vector norm.
Definition: vector.h:2480
Vector< T > calculate_norm_gradient(void) const
Returns the gradient of the vector norm.
Definition: vector.h:2382
Vector< T > calculate_pow(const T &) const
Definition: vector.h:1766
Matrix< T > to_column_matrix(void) const
Definition: vector.h:5196
Vector< T > to_vector(void) const
Definition: vector.h:5631
Vector< T > calculate_unscaled_mean_standard_deviation(const Vector< T > &, const Vector< T > &) const
Definition: vector.h:4470
void initialize_random(void)
Definition: vector.h:5649
double calculate_mean(void) const
Returns the mean of the elements in the vector.
Definition: vector.h:2068
Vector< T > calculate_scaled_mean_standard_deviation(const Vector< T > &, const Vector< T > &) const
Definition: vector.h:4342
size_t count_empty_bins(void) const
Returns the number of bins with zero variates.
Definition: vector.h:5838
void set_maximum(const double &)
Definition: vector.h:5603
T calculate_product(void) const
Returns the product of the elements in the vector.
Definition: vector.h:2048
double correlation
Correlation coefficient (R-value) of the logistic regression.
Definition: vector.h:5955
Vector< T > calculate_minimal_centers(void) const
Returns a vector with the centers of the less populated bins.
Definition: vector.h:5865
Vector< size_t > calculate_occurrence_indices(const T &) const
Definition: vector.h:1118
Vector< T > calculate_minimum_maximum_missing_values(const Vector< size_t > &) const
Returns a vector containing the smallest and the largest elements in the vector.
Definition: vector.h:1395
void set_standard_deviation(const double &)
Definition: vector.h:5621
T calculate_sum(void) const
Returns the sum of the elements in the vector.
Definition: vector.h:2005
void apply_lower_upper_bounds(const T &, const T &)
Definition: vector.h:3276
Histogram(void)
Default constructor.
Definition: vector.h:5790
std::string to_string(const std::string &=" ") const
Returns a string representation of this vector.
Definition: vector.h:5255
Vector< size_t > calculate_minimal_maximal_index(void) const
Returns a vector with the indexes of the smallest and the largest elements in the vector...
Definition: vector.h:1728
double calculate_standard_deviation_missing_values(const Vector< size_t > &) const
Returns the standard deviation of the elements in the vector.
Definition: vector.h:2231
virtual ~Histogram(void)
Destructor.
Definition: vector.h:5798
T standard_deviation
Standard deviation value of a set, function, etc.
Definition: vector.h:5558
size_t calculate_closest_index(const T &) const
Returns the index of the closest element in the vector to a given value.
Definition: vector.h:1990
bool operator==(const T &) const
Definition: vector.h:517
bool is_decrescent(void) const
Returns true if all the elements in the vector have values which decrease with the index...
Definition: vector.h:1075
std::vector< T > to_std_vector(void) const
Returns a std vector with the size and elements of this OpenNN vector.
Definition: vector.h:5153
size_t count_occurrences(const T &) const
Returns the number of times that a certain value is contained in the vector.
Definition: vector.h:1094
Vector< T > arrange_subvector_last(const size_t &) const
Definition: vector.h:4760