vector.h
1 // OpenNN: Open Neural Networks Library
2 // www.opennn.net
3 //
4 // V E C T O R C O N T A I N E R
5 //
6 // Artificial Intelligence Techniques SL
7 // artelnics@artelnics.com
8 
9 #ifndef VECTOR_H
10 #define VECTOR_H
11 
12 // System includes
13 
14 #include <algorithm>
15 #include <cassert>
16 #include <cmath>
17 #include <cstdlib>
18 #include <functional>
19 #include <fstream>
20 #include <iomanip>
21 #include <iostream>
22 #include <iterator>
23 #include <istream>
24 #include <numeric>
25 #include <ostream>
26 #include <sstream>
27 #include <stdexcept>
28 #include <string>
29 #include <vector>
30 #include <limits>
31 #include <climits>
32 #include <ctime>
33 #include <time.h>
34 
35 using namespace std;
36 
37 namespace OpenNN {
38 
39 // Forward declarations
40 
41 template <class T> class Matrix;
42 template <class T> class Tensor;
43 
44 template <class T> T calculate_random_uniform(const T&, const T&);
45 template <class T> T calculate_random_normal(const T&, const T&);
46 
47 
49 
52 
53 template <typename T>
54 class Vector : public vector<T> {
55 
56 public:
57 
58  // Constructors
59 
60  explicit Vector();
61 
62  explicit Vector(const size_t &);
63 
64  explicit Vector(const size_t &, const T&);
65 
66  explicit Vector(const string &);
67 
68  explicit Vector(const T&, const double &, const T&);
69 
70  template <class InputIterator> explicit Vector(const InputIterator&, const InputIterator&);
71 
72  Vector(const vector<T>&);
73 
74  Vector(const Vector<T>&);
75 
76  Vector(const initializer_list<T>&);
77 
78  Vector(const Vector<Vector<T>>&);
79 
80  // Destructor
81 
82  virtual ~Vector();
83 
84  // Operators
85 
86  inline Vector<T>& operator = (const Vector<T>&);
87 
88  bool operator == (const T&) const;
89 
90  bool operator != (const T&) const;
91 
92  bool operator > (const T&) const;
93 
94  bool operator < (const T&) const;
95 
96  bool operator >= (const T&) const;
97 
98  bool operator <= (const T&) const;
99 
100  // Set methods
101 
102  void set();
103 
104  void set(const size_t &);
105 
106  void set(const size_t &, const T&);
107 
108  void set(const string &);
109 
110  void set(const T&, const double &, const T&);
111 
112  void set(const Vector<T> &);
113 
114  // Get methods
115 
116  const T& get_first() const;
117  const T& get_last() const;
118  const T& get_before_last() const;
119 
120  Vector<T> get_positive_elements() const;
121  Vector<T> get_negative_elements() const;
122 
123  // Initialization methods
124 
125  void initialize(const T&);
126  void initialize_first(const size_t&, const T&);
127 
128  void initialize_sequential();
129 
130  void randomize_uniform(const T&, const T&);
131  void randomize_uniform(const Vector<T>&, const Vector<T>&);
132 
133  void randomize_normal(const double & = 0.0, const double & = 1.0);
134  void randomize_normal(const Vector<double>&, const Vector<double>&);
135 
136  void randomize_binary(const double & = 0.5, const double & = 0.5);
137 
138  // String methods
139 
140  void trim();
141 
142  Vector<T> trimmed() const;
143 
144  // Fill method
145 
146  Vector<T> fill_from(const size_t&, const Vector<T>&) const;
147 
148  // Checking methods
149 
150  bool contains(const T&) const;
151  bool contains(const Vector<T>&) const;
152 
153  bool contains_greater_than(const T&) const;
154 
155  bool has_same_elements(const Vector<T>&) const;
156 
157  bool is_in(const T&, const T&) const;
158 
159  bool is_constant(const double & = 0.0) const;
160  bool is_constant_string() const;
161 
162  bool is_crescent() const;
163  bool is_decrescent() const;
164 
165  bool is_binary() const;
166  bool is_binary_missing_values() const;
167  bool is_binary_0_1() const;
168 
169  bool is_integer() const;
170  bool is_integer_missing_values() const;
171 
172  bool is_positive() const;
173  bool is_negative() const;
174 
175  bool check_period(const double& period) const;
176 
177  bool has_nan() const;
178 
179  Vector<T> get_reverse() const;
180 
181  // Replace methods
182 
183  void replace_value(const T&, const T&);
184 
185  // Count methods
186 
187  size_t count_equal_to(const T&) const;
188  size_t count_equal_to(const Vector<T>&) const;
189 
190  size_t count_NAN() const;
191  size_t count_not_NAN() const;
192 
193  size_t count_not_equal_to(const T&) const;
194  size_t count_not_equal_to(const Vector<T>&) const;
195 
196  size_t count_positive() const;
197  size_t count_negative() const;
198 
199  size_t count_integers(const size_t&) const;
200  size_t count_integers_missing_values(const size_t&) const;
201 
202  size_t count_greater_than(const T&) const;
203  size_t count_less_than(const T&) const;
204  size_t count_greater_equal_to(const T&) const;
205 
206  size_t count_less_equal_to(const T&) const;
207 
208  size_t count_between(const T&, const T&) const;
209 
210  size_t count_contains(const string&) const;
211 
212  Vector<size_t> count_unique() const;
213 
214  // Indices methods
215 
216  size_t get_first_index(const T&) const;
217 
218  Vector<size_t> get_indices_equal_to(const T&) const;
219  Vector<size_t> get_indices_equal_to(const Vector<T>&) const;
220 
221  Vector<size_t> get_indices_not_equal_to(const T&) const;
222  Vector<size_t> get_indices_not_equal_to(const Vector<T>&) const;
223 
224  Vector<size_t> get_indices_less_than(const T&) const;
225  Vector<size_t> get_indices_greater_than(const T&) const;
226 
227  Vector<size_t> get_indices_less_equal_to(const T&) const;
228  Vector<size_t> get_indices_greater_equal_to(const T&) const;
229 
230  Vector<size_t> get_between_indices(const T&, const T&) const;
231 
232  Vector<size_t> get_indices_that_contains(const string&) const;
233 
234  Vector<T> merge(const string&, const char&) const;
235 
236  Vector< Matrix<T> > to_vector_matrix(const size_t&, const size_t&, const size_t&) const;
237 
238  // Filtering methods
239 
240  Vector<T> filter_equal_to(const T&) const;
241  Vector<T> filter_not_equal_to(const T&) const;
242 
243  Vector<T> filter_equal_to(const Vector<T>&) const;
244  Vector<T> filter_not_equal_to(const Vector<T>&) const;
245 
246  Vector<T> filter_positive() const;
247  Vector<T> filter_negative() const;
248 
249  Vector<T> filter_minimum_maximum(const T&, const T&) const;
250 
251  Vector<double> perform_Box_Cox_transformation(const double& = 1) const;
252 
253  // Descriptives methods
254 
255  Vector<double> calculate_percentage(const size_t&) const;
256  Vector<double> calculate_percentage() const;
257 
258  size_t calculate_cumulative_index(const T&) const;
259 
260  T calculate_sum() const;
261 
262  T calculate_partial_sum(const Vector<size_t>&) const;
263 
264  T calculate_sum_missing_values() const;
265 
266  T calculate_product() const;
267 
268  // Rank methods
269 
270  Vector<size_t> sort_ascending_indices() const;
271  Vector<T> sort_ascending_values() const;
272 
273  Vector<size_t> calculate_lower_indices(const size_t&) const;
274  Vector<T> calculate_lower_values(const size_t&) const;
275 
276  Vector<size_t> sort_descending_indices() const;
277  Vector<T> sort_descending_values() const;
278 
279  Vector<size_t> calculate_less_rank() const;
280  Vector<size_t> calculate_greater_rank() const;
281 
282  Vector<T> sort_rank(const Vector<size_t>&) const;
283 
284  // Mathematical operators
285 
286  inline Vector<T> operator = (const initializer_list<T>&) const;
287 
288  inline Vector<T> operator + (const T&) const;
289 
290  inline Vector<T> operator + (const Vector<T>&) const;
291 
292  inline Vector<T> operator - (const T&) const;
293 
294  inline Vector<T> operator - (const Vector<T>&) const;
295 
296  inline Vector<T> operator * (const T&) const;
297 
298  inline Vector<T> operator * (const Vector<T>&) const;
299 
300  inline Matrix<T> operator * (const Matrix<T>&) const;
301 
302  Vector<T> operator / (const T&) const;
303 
304  Vector<T> operator / (const Vector<T>&) const;
305 
306  void operator += (const T&);
307 
308  void operator += (const Vector<T>&);
309 
310  void operator -= (const T&);
311 
312  void operator -= (const Vector<T>&);
313 
314  void operator *= (const T&);
315 
316  void operator *= (const Vector<T>&);
317 
318  void operator /= (const T&);
319 
320  void operator /= (const Vector<T>&);
321 
322 
323  // Arranging methods
324 
325  Matrix<T> to_diagonal_matrix() const;
326 
327  Vector<T> get_subvector(const size_t&, const size_t&) const;
328 
329  Vector<T> get_subvector(const Vector<size_t>&) const;
330 
331  Vector<T> get_subvector(const Vector<bool>&) const;
332 
333  Vector<T> get_subvector_random(const size_t&) const;
334 
335  Vector<T> get_first(const size_t &) const;
336 
337  Vector<T> get_last(const size_t &) const;
338 
339  Vector<T> delete_first(const size_t &) const;
340 
341  Vector<T> delete_last(const size_t &) const;
342 
343  Vector<T> get_integer_elements(const size_t&) const;
344  Vector<T> get_integer_elements_missing_values(const size_t&) const;
345 
346  // File operations
347 
348  void load(const string &);
349 
350  void save(const string &, const char& = ' ') const;
351 
352  void embed(const size_t &, const Vector<T>&);
353 
354  Vector<T> insert_elements(const size_t &, const Vector<T>&);
355 
356  Vector<T> insert_element(const size_t &, const T&) const;
357 
358  Vector<T> delete_index(const size_t &) const;
359 
360  Vector<T> delete_indices(const Vector<size_t>&) const;
361 
362  Vector<T> delete_value(const T&) const;
363  Vector<T> delete_values(const Vector<T>&) const;
364 
365  Vector<T> assemble(const Vector<T>&) const;
366  static Vector<T> assemble(const Vector<Vector<T>>&);
367 
368  Vector<T> get_difference(const Vector<T>&) const;
369  Vector<T> get_union(const Vector<T>&) const;
370  Vector<T> get_intersection(const Vector<T>&) const;
371 
372  Vector<T> get_unique_elements() const;
373 
374  void print_unique_string() const;
375  void print_unique_number() const;
376 
377  Vector<T> calculate_top_string(const size_t&) const;
378  Vector<T> calculate_top_number(const size_t&) const;
379 
380  void print_top_string(const size_t&) const;
381 
382  vector<T> to_std_vector() const;
383 
384  Vector<float> to_float_vector() const;
385  Vector<double> to_double_vector() const;
386  Vector<int> to_int_vector() const;
387  Vector<size_t> to_size_t_vector() const;
388  Vector<time_t> to_time_t_vector() const;
389  Vector<bool> to_bool_vector() const;
390 
391  Matrix<T> to_binary_matrix() const;
392 
393  Vector<string> to_string_vector() const;
394 
395  Vector<double> string_to_double() const;
396  Vector<int> string_to_int() const;
397  Vector<size_t> string_to_size_t() const;
398  Vector<time_t> string_to_time_t() const;
399 
400  Vector<Vector<T>> split(const size_t&) const;
401 
402  Matrix<T> to_row_matrix() const;
403 
404  Matrix<T> to_column_matrix() const;
405 
406  void parse(const string &);
407 
408  string to_text(const char& = ',') const;
409  string to_text(const string& = ",") const;
410 
411  string vector_to_string(const char&, const char&) const;
412  string vector_to_string(const char&) const;
413  string vector_to_string() const;
414 
415  string stack_vector_to_string() const;
416 
417  Vector<string> write_string_vector(const size_t & = 5) const;
418 
419  Matrix<T> to_matrix(const size_t &, const size_t &) const;
420  Tensor<T> to_tensor(const Vector<size_t>&) const;
421 
422 
423 };
424 
425 
428 
429 template <class T> Vector<T>::Vector() : vector<T>() {}
430 
431 
435 
436 template <class T>
437 Vector<T>::Vector(const size_t &new_size)
438  : vector<T>(new_size) {}
439 
440 
446 
447 template <class T>
448 Vector<T>::Vector(const size_t &new_size, const T&value)
449  : vector<T>(new_size, value) {}
450 
451 
455 
456 template <class T>
457 Vector<T>::Vector(const string &file_name)
458  : vector<T>() {
459  load(file_name);
460 }
461 
462 
470 
471 template <class T>
472 Vector<T>::Vector(const T&first, const double &step, const T&last)
473  : vector<T>() {
474  set(first, step, last);
475 }
476 
477 
479 
480 template <class T>
481 template <class InputIterator>
482 Vector<T>::Vector(const InputIterator& first, const InputIterator& last): vector<T>(first, last) {}
483 
484 
488 
489 template <class T>
490 Vector<T>::Vector(const Vector<T>&other_vector) : vector<T>(other_vector) {}
491 
492 
496 
497 template <class T>
498 Vector<T>::Vector(const vector<T>&other_vector) : vector<T>(other_vector) {}
499 
500 
504 
505 template <class T>
506 Vector<T>::Vector(const initializer_list<T>&list) : vector<T>(list) {}
507 
512 
513 template <class T>
515 {
516  const size_t vectors_size = vectors.size();
517 
518  size_t new_size = 0;
519 
520  for(size_t i = 0; i < vectors_size; i++)
521  {
522  new_size += vectors[i].size();
523  }
524 
525  set(new_size);
526 
527  size_t index = 0;
528 
529  for(size_t i = 0; i < vectors_size; i++)
530  {
531  for(size_t j = 0; j < vectors[i].size(); j++)
532  {
533  (*this)[index] = vectors[i][j];
534  index++;
535  }
536  }
537 }
538 
539 
541 
542 template <class T>
544 {
545 }
546 
547 
548 template <class T>
549 Vector<T>& Vector<T>::operator = (const Vector<T>& other_vector)
550 {
551  if(other_vector.size() != this->size())
552  {
553  this->resize(other_vector.size());
554  }
555 
556  copy(other_vector.begin(), other_vector.end(), this->begin());
557 
558  return *this;
559 }
560 
561 
566 
567 template <class T> bool Vector<T>::operator == (const T&value) const {
568  const size_t this_size = this->size();
569 
570  for(size_t i = 0; i < this_size; i++) {
571  if((*this)[i] != value) {
572  return false;
573  }
574  }
575 
576  return true;
577 }
578 
579 
584 
585 template <class T> bool Vector<T>::operator!= (const T&value) const {
586  const size_t this_size = this->size();
587 
588  for(size_t i = 0; i < this_size; i++) {
589  if((*this)[i] != value) {
590  return true;
591  }
592  }
593 
594  return false;
595 }
596 
597 
602 
603 template <class T>
604 bool Vector<T>::operator>(const T&value) const
605 {
606  const size_t this_size = this->size();
607 
608  for(size_t i = 0; i < this_size; i++) {
609  if((*this)[i] <= value) {
610  return false;
611  }
612  }
613 
614  return true;
615 }
616 
617 
622 
623 template <class T>
624 bool Vector<T>::operator<(const T&value) const
625 {
626  const size_t this_size = this->size();
627 
628  for(size_t i = 0; i < this_size; i++) {
629  if((*this)[i] >= value) {
630  return false;
631  }
632  }
633 
634  return true;
635 }
636 
637 
643 
644 template <class T>
645 bool Vector<T>::operator>= (const T&value) const
646 {
647  const size_t this_size = this->size();
648 
649  for(size_t i = 0; i < this_size; i++) {
650  if((*this)[i] < value) {
651  return false;
652  }
653  }
654 
655  return true;
656 }
657 
658 
664 
665 template <class T>
666 bool Vector<T>::operator<= (const T&value) const
667 {
668  const size_t this_size = this->size();
669 
670  for(size_t i = 0; i < this_size; i++) {
671  if((*this)[i] > value) {
672  return false;
673  }
674  }
675 
676  return true;
677 }
678 
679 
681 
682 template <class T> void Vector<T>::set() { this->resize(0); }
683 
684 
688 
689 template <class T>
690 void Vector<T>::set(const size_t &new_size)
691 {
692  this->resize(new_size);
693 }
694 
695 
699 
700 template <class T>
701 void Vector<T>::set(const size_t &new_size, const T&new_value)
702 {
703  this->resize(new_size);
704 
705  initialize(new_value);
706 }
707 
708 
712 
713 template <class T> void Vector<T>::set(const string &file_name) {
714  load(file_name);
715 }
716 
717 
725 
726 template <class T>
727 void Vector<T>::set(const T&first, const double &step, const T&last) {
728  if(first > last && step > 0) {
729  this->resize(0);
730  } else if(first < last && step < 0) {
731  this->resize(0);
732  } else {
733  const size_t new_size = 1 + static_cast<size_t>((last - first) / step + 0.5);
734 
735  this->resize(new_size);
736 
737  for(size_t i = 0; i < new_size; i++) {
738  (*this)[i] = first + static_cast<T>(i * step);
739  }
740  }
741 }
742 
743 
746 
747 template <class T>
748 void Vector<T>::set(const Vector<T> &other_vector)
749 {
750  if(other_vector.size() != this->size())
751  {
752  this->resize(other_vector.size());
753  }
754 
755  copy(other_vector.begin(), other_vector.end(), this->begin());
756 }
757 
758 
760 
761 template <class T>
762 const T& Vector<T>::get_first() const
763 {
764  return (*this)[0];
765 }
766 
768 
769 template <class T>
770 const T& Vector<T>::get_last() const
771 {
772  const size_t this_size = this->size();
773 
774  return (*this)[this_size-1];
775 }
776 
778 
779 template <class T>
781 {
782  const size_t this_size = this->size();
783 
784  return (*this)[this_size-2];
785 }
786 
787 
790 
791 template <class T>
792 Vector<T> Vector<T>::delete_first(const size_t & elements_number) const
793 {
794  const size_t new_size = this->size() - elements_number;
795 
796  return get_last(new_size);
797 }
798 
799 
802 
803 template <class T>
804 Vector<T> Vector<T>::delete_last(const size_t & elements_number) const
805 {
806  const size_t new_size = this->size() - elements_number;
807 
808  return get_first(new_size);
809 }
810 
811 
814 
815 template <class T>
816 void Vector<T>::initialize(const T&value)
817 {
818  fill(this->begin(),this->end(), value);
819 }
820 
824 
825 template <class T>
826 void Vector<T>::initialize_first(const size_t& first, const T&value)
827 {
828  for(size_t i = 0; i < first; i++) (*this)[i] = value;
829 }
830 
831 
833 
834 template <class T> void Vector<T>::initialize_sequential() {
835  for(size_t i = 0; i < this->size(); i++) {
836  (*this)[i] = static_cast<T>(i);
837  }
838 }
839 
840 
845 
846 template <class T>
847 void Vector<T>::randomize_uniform(const T& minimum,
848  const T& maximum)
849 {
850 
851 
852 #ifdef __OPENNN_DEBUG__
853 
854  if(minimum > maximum) {
855  ostringstream buffer;
856 
857  buffer << "OpenNN Exception: Vector Template.\n"
858  << "void randomize_uniform(const T&, const T&) method.\n"
859  << "Minimum value must be less or equal than maximum value.\n";
860 
861  throw logic_error(buffer.str());
862  }
863 
864 #endif
865 
866  const size_t this_size = this->size();
867 
868  for(size_t i = 0; i < this_size; i++)
869  {
870  (*this)[i] = calculate_random_uniform(minimum, maximum);
871  }
872 }
873 
874 
879 
880 template <class T>
882  const Vector<T>&maximums)
883 {
884 
885  const size_t this_size = this->size();
886 
887 
888 
889 #ifdef __OPENNN_DEBUG__
890 
891  const size_t minimums_size = minimums.size();
892  const size_t maximums_size = maximums.size();
893 
894  if(minimums_size != this_size || maximums_size != this_size) {
895  ostringstream buffer;
896 
897  buffer << "OpenNN Exception: Vector Template.\n"
898  << "void randomize_uniform(const Vector<T>&, const Vector<T>&) method.\n"
899  << "Minimum and maximum sizes must be equal to vector size.\n";
900 
901  throw logic_error(buffer.str());
902  }
903 
904  if(minimums > maximums) {
905  ostringstream buffer;
906 
907  buffer << "OpenNN Exception: Vector Template.\n"
908  << "void randomize_uniform(const Vector<double>&, const "
909  "Vector<double>&) method.\n"
910  << "Minimum values must be less or equal than their corresponding "
911  "maximum values.\n";
912 
913  throw logic_error(buffer.str());
914  }
915 
916 #endif
917 
918  for(size_t i = 0; i < this_size; i++)
919  {
920  (*this)[i] = calculate_random_uniform(minimums[i], maximums[i]);
921  }
922 }
923 
924 
930 
931 template <class T>
932 void Vector<T>::randomize_normal(const double &mean,
933  const double &standard_deviation)
934 {
935 
936 #ifdef __OPENNN_DEBUG__
937 
938  if(standard_deviation < 0.0) {
939  ostringstream buffer;
940 
941  buffer << "OpenNN Exception: Vector Template.\n"
942  << "void randomize_normal(const double&, const double&) method.\n"
943  << "Standard deviation must be equal or greater than zero.\n";
944 
945  throw logic_error(buffer.str());
946  }
947 
948 #endif
949 
950  const size_t this_size = this->size();
951 
952  for(size_t i = 0; i < this_size; i++)
953  {
954  (*this)[i] = calculate_random_normal(mean, standard_deviation);
955  }
956 }
957 
963 
964 template <class T>
966  const Vector<double>&standard_deviation)
967 {
968  const size_t this_size = this->size();
969 
970 #ifdef __OPENNN_DEBUG__
971 
972  const size_t mean_size = mean.size();
973  const size_t standard_deviation_size = standard_deviation.size();
974 
975  if(mean_size != this_size || standard_deviation_size != this_size) {
976  ostringstream buffer;
977 
978  buffer
979  << "OpenNN Exception: Vector Template.\n"
980  << "void randomize_normal(const Vector<double>&, const "
981  "Vector<double>&) method.\n"
982  << "Mean and standard deviation sizes must be equal to vector size.\n";
983 
984  throw logic_error(buffer.str());
985  }
986 
987  if(standard_deviation < 0.0) {
988  ostringstream buffer;
989 
990  buffer << "OpenNN Exception: Vector Template.\n"
991  << "void randomize_normal(const Vector<double>&, const "
992  "Vector<double>&) method.\n"
993  << "Standard deviations must be equal or greater than zero.\n";
994 
995  throw logic_error(buffer.str());
996  }
997 
998 #endif
999 
1000  for(size_t i = 0; i < this_size; i++) {
1001  (*this)[i] = calculate_random_normal(mean[i], standard_deviation[i]);
1002  }
1003 }
1004 
1005 
1010 
1011 template <class T>
1012 void Vector<T>::randomize_binary(const double& negatives_ratio, const double& positives_ratio)
1013 {
1014  const size_t this_size = this->size();
1015 
1016  if(this_size == 0)
1017  {
1018  return;
1019  }
1020 
1021  const double total_ratio = abs(negatives_ratio) + positives_ratio;
1022 
1023  // Get number of instances for training, selection and testing
1024 
1025  const size_t positives_number = static_cast<size_t>(positives_ratio*this_size/total_ratio);
1026  const size_t negatives_number = this_size - positives_number;
1027 
1028  Vector<size_t> indices(0, 1, this_size-1);
1029  random_shuffle(indices.begin(), indices.end());
1030 
1031  size_t i = 0;
1032  size_t index;
1033 
1034  // Positives
1035 
1036  size_t count_positives = 0;
1037 
1038  while(count_positives != positives_number)
1039  {
1040  index = indices[i];
1041 
1042  (*this)[index] = 1;
1043  count_positives++;
1044 
1045  i++;
1046  }
1047 
1048  // Negatives
1049 
1050  size_t count_negatives = 0;
1051 
1052  while(count_negatives != negatives_number)
1053  {
1054  index = indices[i];
1055 
1056  (*this)[index] = 0;
1057  count_negatives++;
1058 
1059  i++;
1060  }
1061 }
1062 
1063 
1065 
1066 template <class T>
1068 {
1069  const size_t this_size = this->size();
1070 
1071  for(size_t i = 0; i < this_size; i++)
1072  {
1073  //prefixing spaces
1074 
1075  (*this)[i] = (*this)[i].erase(0,(*this)[i].find_first_not_of(' '));
1076 
1077  //surfixing spaces
1078 
1079  (*this)[i] = (*this)[i].erase((*this)[i].find_last_not_of(' ') + 1);
1080  }
1081 }
1082 
1083 
1085 
1086 template <class T>
1088 {
1089  Vector<T> new_vector(*this);
1090  new_vector.trim();
1091 
1092  return new_vector;
1093 }
1094 
1100 
1101 template <class T>
1102 Vector<T> Vector<T>::fill_from(const size_t& from_index, const Vector<T>& fill_with) const
1103 {
1104  Vector<T> new_vector(*this);
1105 
1106  size_t counter = 0;
1107 
1108  for(size_t i = from_index; i < from_index+fill_with.size(); i++)
1109  {
1110  new_vector[i] = fill_with[counter];
1111 
1112  counter++;
1113  }
1114 
1115  return new_vector;
1116 }
1117 
1118 
1121 
1122 template <class T>
1123 bool Vector<T>::contains(const T&value) const
1124 {
1125  Vector<T> copy(*this);
1126 
1127  typename vector<T>::iterator it = find(copy.begin(), copy.end(), value);
1128 
1129  return(it != copy.end());
1130 }
1131 
1132 
1136 
1137 template <class T>
1138 bool Vector<T>::contains_greater_than(const T&value) const
1139 {
1140 
1141  for(size_t i = 0; i < this->size(); i++)
1142  {
1143  if((*this)[i] > value) return true;
1144  }
1145 
1146  return false;
1147 }
1148 
1149 
1153 
1154 template <class T>
1155 bool Vector<T>::contains(const Vector<T>&values) const
1156 {
1157  if(values.empty()) {
1158  return false;
1159  }
1160 
1161  Vector<T> copy(*this);
1162 
1163  const size_t values_size = values.size();
1164 
1165  for(size_t j = 0; j < values_size; j++)
1166  {
1167  typename vector<T>::iterator it = find(copy.begin(), copy.end(), values[j]);
1168 
1169  if(it != copy.end())
1170  {
1171  return true;
1172  }
1173  }
1174 
1175  return false;
1176 }
1177 
1178 
1182 
1183 template <class T>
1184 bool Vector<T>::has_same_elements(const Vector<T>& other_vector) const
1185 {
1186  const size_t this_size = this->size();
1187 
1188  if(this_size != other_vector.size())
1189  {
1190  return false;
1191  }
1192 
1193  for(size_t i = 0; i < this_size; i++)
1194  {
1195  if(!other_vector.contains((*this)[i]))
1196  {
1197  return false;
1198  }
1199  }
1200 
1201  return true;
1202 }
1203 
1204 
1209 
1210 template <class T>
1211 bool Vector<T>::is_in(const T&minimum, const T&maximum) const
1212 {
1213  const size_t this_size = this->size();
1214 
1215  for(size_t i = 0; i < this_size; i++) {
1216  if((*this)[i] < minimum ||(*this)[i] > maximum) {
1217  return false;
1218  }
1219  }
1220 
1221  return true;
1222 }
1223 
1224 
1229 
1230 template <class T>
1231 bool Vector<T>::is_constant(const double &tolerance) const
1232 {
1233  const size_t this_size = this->size();
1234 
1235  if(this_size == 0) {
1236  return false;
1237  }
1238 
1239  for(size_t i = 0; i < this_size; i++)
1240  {
1241  if(abs((*this)[i] - (*this)[0]) > tolerance) return false;
1242  }
1243 
1244  return true;
1245 }
1246 
1247 
1249 
1250 template <class T>
1252 {
1253 
1254  const size_t this_size = this->size();
1255 
1256  if(this_size == 0) {
1257  return false;
1258  }
1259 
1260  for(size_t i = 1; i < this_size; i++)
1261  {
1262  if((*this)[i] != (*this)[0])
1263  {
1264  return false;
1265  }
1266  }
1267 
1268  return true;
1269 }
1270 
1271 
1274 
1275 template <class T>
1277 {
1278  if(this->size() == 1) return true;
1279 
1280  for(size_t i = 0; i < this->size() - 1; i++)
1281  {
1282  if((*this)[i] > (*this)[i + 1]) return false;
1283  }
1284 
1285  return true;
1286 }
1287 
1288 
1291 
1292 template <class T>
1294 {
1295  if(this->size() == 1) return true;
1296 
1297  for(size_t i = 0; i < this->size() - 1; i++)
1298  {
1299  if((*this)[i] < (*this)[i+1]) return false;
1300  }
1301 
1302  return true;
1303 }
1304 
1305 
1307 
1308 template <class T>
1310 {
1311  for(size_t i = 0; i < this->size(); i++)
1312  {
1313  if((*this)[i] < 0.0)
1314  {
1315  return false;
1316  }
1317  }
1318 
1319  return true;
1320 }
1321 
1323 
1324 template <class T>
1326 {
1327  for(size_t i = 0; i < this->size(); i++)
1328  {
1329  if((*this)[i] > 0.0)
1330  {
1331  return false;
1332  }
1333  }
1334 
1335  return true;
1336 }
1337 
1338 
1341 
1342 template <class T>
1343 bool Vector<T>::check_period(const double& period) const
1344 {
1345  for(size_t i = 1; i < this->size(); i++)
1346  {
1347  if((*this)[i] != (*this)[i-1] + period)
1348  {
1349  return false;
1350  }
1351  }
1352 
1353  return true;
1354 }
1355 
1356 
1358 
1359 template <class T>
1361 {
1362  const size_t this_size = this->size();
1363 
1364  Vector<T> values(1,(*this)[0]);
1365 
1366  for(size_t i = 1; i < this_size; i++)
1367  {
1368  const bool contains_value = values.contains((*this)[i]);
1369 
1370  if(!contains_value && values.size() == 1)
1371  {
1372  values.push_back((*this)[i]);
1373  }
1374  else if(!contains_value)
1375  {
1376  return false;
1377  }
1378  }
1379 
1380  return true;
1381 }
1382 
1383 
1385 
1386 template <class T>
1388 {
1389  if(this->get_unique_elements().size() != 2)
1390  {
1391  return false;
1392  }
1393  const size_t this_size = this->size();
1394 
1395  for(size_t i = 0; i < this_size; i++)
1396  {
1397  if(!::isnan((*this)[i]))
1398  {
1399  if((*this)[i] != 0 &&(*this)[i] != 1)
1400  {
1401  return false;
1402  }
1403  }
1404  }
1405 
1406  return true;
1407 }
1408 
1409 
1411 
1412 template <class T>
1414 {
1415  if(this->get_unique_elements().size() != 2)
1416  {
1417  return false;
1418  }
1419  const size_t this_size = this->size();
1420 
1421  for(size_t i = 0; i < this_size; i++)
1422  {
1423  if((*this)[i] != 0 &&(*this)[i] != 1)
1424  {
1425  return false;
1426  }
1427  }
1428 
1429 
1430  return true;
1431 }
1432 
1433 
1435 
1436 template <class T>
1438 {
1439  const size_t this_size = this->size();
1440 
1441  for(size_t i = 0; i < this_size; i++)
1442  {
1443  if(floor((*this)[i]) != (*this)[i])
1444  {
1445  return false;
1446  }
1447  }
1448 
1449  return true;
1450 }
1451 
1452 
1454 
1455 template <class T>
1457 {
1458  const size_t this_size = this->size();
1459 
1460  for(size_t i = 0; i < this_size; i++)
1461  {
1462  if(!::isnan((*this)[i]))
1463  {
1464  if(floor((*this)[i]) != (*this)[i])
1465  {
1466  return false;
1467  }
1468  }
1469  }
1470 
1471  return true;
1472 }
1473 
1474 
1477 
1478 template <class T>
1480 {
1481  const size_t this_size = this->size();
1482 
1483  Vector<T> reverse(this_size);
1484 
1485  for(size_t i = 0; i < this_size; i++)
1486  {
1487  reverse[i] = (*this)[this_size - 1 - i];
1488  }
1489 
1490  return reverse;
1491 }
1492 
1493 
1496 
1497 template <class T>
1498 size_t Vector<T>::count_equal_to(const T& value) const
1499 {
1500  return count(this->begin(), this->end(), value);
1501 }
1502 
1503 
1506 
1507 template <class T>
1508 size_t Vector<T>::count_equal_to(const Vector<T>& values) const
1509 {
1510  size_t count = 0;
1511 
1512  const size_t this_size = this->size();
1513  const size_t values_size = values.size();
1514 
1515  for(size_t i = 0; i < this_size; i++)
1516  {
1517  for(size_t j = 0; j < values_size; j++)
1518  {
1519  if((*this)[i] == values[j]) count++;
1520  }
1521  }
1522 
1523  return count;
1524 }
1525 
1526 
1528 
1529 template <class T>
1530 size_t Vector<T>::count_NAN() const
1531 {
1532  size_t count = 0;
1533 
1534  const size_t this_size = this->size();
1535 
1536  for(size_t i = 0; i < this_size; i++)
1537  {
1538  if(::isnan((*this)[i])) count++;
1539  }
1540 
1541  return count;
1542 }
1543 
1544 
1546 
1547 template <class T>
1549 {
1550  size_t count = 0;
1551 
1552  const size_t this_size = this->size();
1553 
1554  for(size_t i = 0; i < this_size; i++)
1555  {
1556  if(!::isnan((*this)[i])) count++;
1557  }
1558 
1559  return count;
1560 }
1561 
1562 
1565 
1566 template <class T>
1567 size_t Vector<T>::count_not_equal_to(const T&value) const
1568 {
1569  const size_t this_size = this->size();
1570 
1571  size_t count = 0;
1572 
1573  for(size_t i = 0; i < this_size; i++)
1574  {
1575  if((*this)[i] != value)
1576  {
1577  count++;
1578  }
1579  }
1580 
1581  return count;
1582 }
1583 
1584 
1587 
1588 template <class T> size_t Vector<T>::count_not_equal_to(const Vector<T>&values) const
1589 {
1590  const size_t this_size = this->size();
1591 
1592  const size_t equal_to_count = count_equal_to(values);
1593 
1594  return(this_size - equal_to_count);
1595 }
1596 
1597 
1599 
1600 template <class T>
1602 {
1603  const size_t this_size = this->size();
1604 
1605  size_t count = 0;
1606 
1607  for(size_t i = 0; i < this_size; i++)
1608  {
1609  if((*this)[i] >= 0)
1610  {
1611  count++;
1612  }
1613  }
1614 
1615  return count;
1616 }
1617 
1619 
1620 template <class T>
1622 {
1623  const size_t this_size = this->size();
1624 
1625  size_t count = 0;
1626 
1627  for(size_t i = 0; i < this_size; i++)
1628  {
1629  if((*this)[i] < 0)
1630  {
1631  count++;
1632  }
1633  }
1634 
1635  return count;
1636 }
1637 
1638 
1641 
1642 template <class T>
1644 {
1645  const size_t this_size = this->size();
1646 
1647  const size_t new_size = count_equal_to(value);
1648 
1649  Vector<T> new_vector(new_size);
1650 
1651  size_t index = 0;
1652 
1653  for(size_t i = 0; i < this_size; i++)
1654  {
1655  if((*this)[i] == value)
1656  {
1657  new_vector[index] = (*this)[i];
1658  index++;
1659  }
1660  }
1661 
1662  return new_vector;
1663 }
1664 
1667 
1668 template <class T>
1670 {
1671  const Vector<size_t> indices = get_indices_equal_to(values);
1672 
1673  return get_subvector(indices);
1674 }
1675 
1676 
1679 
1680 template <class T>
1682 {
1683  const size_t this_size = this->size();
1684 
1685  const size_t new_size = count_not_equal_to(value);
1686 
1687  Vector<T> new_vector(new_size);
1688 
1689  size_t index = 0;
1690 
1691  for(size_t i = 0; i < this_size; i++)
1692  {
1693  if((*this)[i] != value)
1694  {
1695  new_vector[index] = (*this)[i];
1696  index++;
1697  }
1698  }
1699 
1700  return new_vector;
1701 }
1702 
1703 
1706 
1707 template <class T>
1709 {
1710  const Vector<size_t> indices = get_indices_not_equal_to(values);
1711 
1712  return get_subvector(indices);
1713 }
1714 
1715 
1717 
1718 template <class T>
1720 {
1721  const size_t this_size = this->size();
1722  const size_t new_size = count_positive();
1723 
1724  Vector<T> new_vector(new_size);
1725 
1726  size_t count = 0;
1727 
1728  for(size_t i = 0; i < this_size; i++)
1729  {
1730  if((*this)[i] >= 0)
1731  {
1732  new_vector[count] = (*this)[i];
1733  count++;
1734  }
1735  }
1736 
1737  return new_vector;
1738 
1739 }
1740 
1741 
1743 
1744 template <class T>
1746 {
1747  const size_t this_size = this->size();
1748  const size_t new_size = count_negative();
1749 
1750  Vector<T> new_vector(new_size);
1751 
1752  size_t count = 0;
1753 
1754  for(size_t i = 0; i < this_size; i++)
1755  {
1756  if((*this)[i] < 0)
1757  {
1758  new_vector[count] = (*this)[i];
1759  count++;
1760  }
1761  }
1762 
1763  return new_vector;
1764 }
1765 
1767 
1768 template <class T>
1770 {
1771  const size_t this_size = this->size();
1772 
1773  for(size_t i = 0; i < this_size; i++)
1774  {
1775  if(::isnan((*this)[i])) return true;
1776  }
1777 
1778  return false;
1779 }
1780 
1781 
1786 
1787 template <class T>
1788 size_t Vector<T>::count_integers(const size_t& maximum_integers) const
1789 {
1790  if(!this->is_integer())
1791  {
1792  return 0;
1793  }
1794 
1795  const size_t this_size = this->size();
1796 
1797  Vector<T> integers;
1798  size_t integers_count = 0;
1799 
1800  for(size_t i = 0; i < this_size; i++)
1801  {
1802  if(!integers.contains((*this)[i]))
1803  {
1804  integers.push_back((*this)[i]);
1805  integers_count++;
1806  }
1807 
1808  if(integers_count > maximum_integers)
1809  {
1810  return 0;
1811  }
1812  }
1813 
1814  return integers_count;
1815 }
1816 
1817 
1822 
1823 template <class T>
1824 size_t Vector<T>::count_integers_missing_values(const size_t& maximum_integers) const
1825 {
1826  if(!this->is_integer_missing_values())
1827  {
1828  return 0;
1829  }
1830 
1831  const size_t this_size = this->size();
1832 
1833  Vector<T> integers;
1834  size_t integers_count = 0;
1835 
1836  for(size_t i = 0; i < this_size; i++)
1837  {
1838  if(!::isnan((*this)[i]))
1839  {
1840  if(!integers.contains((*this)[i]))
1841  {
1842  integers.push_back((*this)[i]);
1843  integers_count++;
1844  }
1845 
1846  if(integers_count > maximum_integers)
1847  {
1848  return 0;
1849  }
1850  }
1851  }
1852 
1853  return integers_count;
1854 }
1855 
1856 
1860 
1861 template <class T>
1862 Vector<size_t> Vector<T>::get_between_indices(const T& minimum, const T& maximum) const
1863 {
1864  const size_t this_size = this->size();
1865 
1866  const size_t new_size = count_between(minimum, maximum);
1867 
1868  Vector<size_t> indices(new_size);
1869 
1870  size_t index = 0;
1871 
1872  for(size_t i = 0; i < this_size; i++)
1873  {
1874  if((*this)[i] >= minimum &&(*this)[i] <= maximum)
1875  {
1876  indices[index] = i;
1877 
1878  index++;
1879  }
1880  }
1881 
1882  return indices;
1883 }
1884 
1885 
1888 
1889 template <class T>
1891 {
1892  const size_t this_size = this->size();
1893 
1894  const size_t occurrences_number = count_equal_to(values);
1895 
1896  if(occurrences_number == 0)
1897  {
1898  Vector<size_t> occurrence_indices;
1899 
1900  return(occurrence_indices);
1901  }
1902 
1903  Vector<size_t> occurrence_indices(occurrences_number);
1904 
1905  size_t index = 0;
1906 
1907  for(size_t i = 0; i < this_size; i++)
1908  {
1909  if(values.contains((*this)[i]))
1910  {
1911  occurrence_indices[index] = i;
1912  index++;
1913  }
1914  }
1915 
1916  return(occurrence_indices);
1917 }
1918 
1919 
1922 
1923 template <class T>
1925 {
1926 
1927  const size_t this_size = this->size();
1928 
1929  const size_t not_equal_to_count = count_not_equal_to(value);
1930 
1931  Vector<size_t> not_equal_to_indices(not_equal_to_count);
1932 
1933  size_t index = 0;
1934 
1935  for(size_t i = 0; i < this_size; i++) {
1936  if((*this)[i] != value) {
1937  not_equal_to_indices[index] = i;
1938  index++;
1939  }
1940  }
1941 
1942  return(not_equal_to_indices);
1943 }
1944 
1945 
1948 
1949 template <class T>
1951 {
1952  const size_t this_size = this->size();
1953 
1954  const size_t occurrences_number = count_not_equal_to(values);
1955 
1956  if(occurrences_number == 0) return Vector<size_t>();
1957 
1958  Vector<size_t> occurrence_indices(occurrences_number);
1959 
1960  size_t index = 0;
1961 
1962  for(size_t i = 0; i < this_size; i++)
1963  {
1964  if(!values.contains((*this)[i]))
1965  {
1966  occurrence_indices[index] = i;
1967  index++;
1968  }
1969  }
1970 
1971  return(occurrence_indices);
1972 }
1973 
1976 
1977 template <class T>
1979 {
1980 
1981  const size_t this_size = this->size();
1982 
1983  const size_t equal_to_count = count_equal_to(value);
1984 
1985  if(equal_to_count == 0)
1986  {
1987  Vector<size_t> occurrence_indices;
1988 
1989  return(occurrence_indices);
1990  }
1991 
1992  Vector<size_t> equal_to_indices(equal_to_count);
1993 
1994  size_t index = 0;
1995 
1996  for(size_t i = 0; i < this_size; i++)
1997  {
1998  if((*this)[i] == value)
1999  {
2000  equal_to_indices[index] = i;
2001  index++;
2002  }
2003  }
2004 
2005  return(equal_to_indices);
2006 }
2007 
2008 
2011 
2012 template <class T>
2014 {
2015 
2016  const size_t this_size = this->size();
2017 
2018  const size_t less_than_count = count_less_than(value);
2019 
2020  Vector<size_t> less_than_indices(less_than_count);
2021 
2022  size_t index = 0;
2023 
2024  for(size_t i = 0; i < this_size; i++)
2025  {
2026  if((*this)[i] < value)
2027  {
2028  less_than_indices[index] = i;
2029  index++;
2030  }
2031  }
2032 
2033  return(less_than_indices);
2034 }
2035 
2036 
2039 
2040 template <class T>
2042 {
2043  const size_t this_size = this->size();
2044 
2045  const size_t count = count_greater_than(value);
2046 
2047  Vector<size_t> indices(count);
2048 
2049  size_t index = 0;
2050 
2051  for(size_t i = 0; i < this_size; i++)
2052  {
2053  if((*this)[i] > value)
2054  {
2055  indices[index] = i;
2056  index++;
2057  }
2058  }
2059 
2060  return indices;
2061 }
2062 
2063 
2066 
2067 template <class T>
2068 size_t Vector<T>::count_greater_than(const T&value) const
2069 {
2070  const size_t this_size = this->size();
2071 
2072  size_t count = 0;
2073 
2074  for(size_t i = 0; i < this_size; i++)
2075  {
2076  if((*this)[i] > value)
2077  {
2078  count++;
2079  }
2080  }
2081 
2082  return count;
2083 }
2084 
2085 
2088 
2089 template <class T>
2090 size_t Vector<T>::count_less_than(const T&value) const
2091 {
2092  const size_t this_size = this->size();
2093 
2094  size_t count = 0;
2095 
2096  for(size_t i = 0; i < this_size; i++)
2097  {
2098  if((*this)[i] < value)
2099  {
2100  count++;
2101  }
2102  }
2103 
2104  return count;
2105 }
2106 
2107 
2110 
2111 template <class T>
2112 size_t Vector<T>::count_greater_equal_to(const T&value) const
2113 {
2114  const size_t this_size = this->size();
2115 
2116  size_t count = 0;
2117 
2118  for(size_t i = 0; i < this_size; i++)
2119  {
2120  if((*this)[i] >= value)
2121  {
2122  count++;
2123  }
2124  }
2125 
2126  return count;
2127 }
2128 
2129 
2132 
2133 template <class T>
2134 size_t Vector<T>::count_less_equal_to(const T&value) const
2135 {
2136  const size_t count = count_if(this->begin(), this->end(), [value](T elem){ return elem <= value; });
2137 
2138  return count;
2139 }
2140 
2141 
2146 
2147 template <class T>
2148 size_t Vector<T>::count_between(const T&minimum, const T&maximum) const
2149 {
2150  const size_t this_size = this->size();
2151 
2152  size_t count = 0;
2153 
2154  for(size_t i = 0; i < this_size; i++)
2155  {
2156  if((*this)[i] >= minimum &&(*this)[i] <= maximum) count++;
2157  }
2158 
2159  return count;
2160 }
2161 
2162 
2165 
2166 template <class T>
2167 size_t Vector<T>::count_contains(const string& find_what) const
2168 {
2169  const size_t this_size = this->size();
2170 
2171  size_t count = 0;
2172 
2173  for(size_t i = 0; i < this_size; i++)
2174  {
2175  if((*this)[i].find(find_what) != string::npos)
2176  {
2177  count++;
2178  }
2179  }
2180 
2181  return count;
2182 }
2183 
2184 
2188 
2189 template <class T>
2190 Vector<T> Vector<T>::merge(const string& substring, const char& separator) const
2191 {
2192  const size_t this_size = this->size();
2193 
2194  Vector<T> merged(this_size);
2195 
2196  for(size_t i = 0; i < this_size; i++)
2197  {
2198  merged[i] = (*this)[i] + separator + substring;
2199  }
2200 
2201  return(merged);
2202 }
2203 
2204 
2209 
2210 template <class T>
2211 Vector<T> Vector<T>::filter_minimum_maximum(const T&minimum, const T&maximum) const
2212 {
2213  const size_t this_size = this->size();
2214  const size_t new_size = count_between(minimum, maximum);
2215 
2216  Vector<T> new_vector(new_size);
2217 
2218  size_t count = 0;
2219 
2220  for(size_t i = 0; i < this_size; i++)
2221  {
2222  if((*this)[i] >= minimum && (*this)[i] <= maximum)
2223  {
2224  new_vector[count] = (*this)[i];
2225  count++;
2226  }
2227  }
2228 
2229  return new_vector;
2230 }
2231 
2232 
2235 
2236 template <class T>
2238 {
2239  const size_t this_size = this->size();
2240 
2241  Vector<size_t> indices;
2242 
2243  for(size_t i = 0; i < this_size; i++)
2244  {
2245  if((*this)[i].find(find_what) != string::npos)
2246  {
2247  indices.push_back(i);
2248  }
2249  }
2250 
2251  return indices;
2252 }
2253 
2254 
2257 
2258 template <class T>
2260 {
2261  const size_t this_size = this->size();
2262 
2263  Vector<size_t> less_than_indices;
2264 
2265  for(size_t i = 0; i < this_size; i++)
2266  {
2267  if((*this)[i] <= value)
2268  {
2269  less_than_indices.push_back(i);
2270  }
2271  }
2272 
2273  return less_than_indices;
2274 }
2275 
2276 
2279 
2280 template <class T>
2282 {
2283 
2284  const size_t this_size = this->size();
2285 
2286  Vector<size_t> greater_than_indices;
2287 
2288  for(size_t i = 0; i < this_size; i++)
2289  {
2290  if((*this)[i] >= value)
2291  {
2292  greater_than_indices.push_back(i);
2293  }
2294  }
2295 
2296  return greater_than_indices;
2297 }
2298 
2299 
2302 
2303 template <class T> Vector<double> Vector<T>::perform_Box_Cox_transformation(const double& lambda) const
2304 {
2305  const size_t size = this->size();
2306 
2307  Vector<double> vector_tranformation(size);
2308 
2309  for(size_t i = 0; i < size; i++)
2310  {
2311  if(abs(lambda - 0) < numeric_limits<double>::epsilon())
2312  {
2313  vector_tranformation[i] = log(static_cast<double>((*this)[i]));
2314  }
2315  else
2316  {
2317  vector_tranformation[i] = (pow(static_cast<double>((*this)[i]), lambda) - 1)/lambda;
2318  }
2319  }
2320 
2321  return vector_tranformation;
2322 }
2323 
2324 
2327 
2328 template <class T>
2329 Vector<double> Vector<T>::calculate_percentage(const size_t& total_sum) const
2330 {
2331  const size_t this_size = this->size();
2332 
2333  Vector<double> percentage_vector(this_size);
2334 
2335  for(size_t i = 0; i < this_size; i++)
2336  {
2337  percentage_vector[i] = static_cast<double>((*this)[i])*100.0/static_cast<double>(total_sum);
2338  }
2339 
2340  return percentage_vector;
2341 }
2342 
2343 
2345 
2347 {
2348  const size_t this_size = this->size();
2349 
2350  const size_t total_sum = this->calculate_sum();
2351 
2352  Vector<double> percentage_vector(this_size);
2353 
2354  for(size_t i = 0; i < this_size; i++)
2355  {
2356  percentage_vector[i] = static_cast<double>((*this)[i])*100.0/static_cast<double>(total_sum);
2357  }
2358 
2359  return percentage_vector;
2360 }
2361 
2362 
2365 
2366 template <class T>
2367 size_t Vector<T>::get_first_index(const T& value) const
2368 {
2369  const size_t this_size = this->size();
2370 
2371  for(size_t i = 0; i < this_size; i++)
2372  {
2373  if((*this)[i] == value) return i;
2374  }
2375 
2376  ostringstream buffer;
2377 
2378  buffer << "OpenNN Exception: Vector Template.\n"
2379  << "size_t get_first_index(const T&) const.\n"
2380  << "Value not found in vector.\n";
2381 
2382  throw logic_error(buffer.str());
2383 }
2384 
2385 
2389 
2390 template <class T>
2391 size_t Vector<T>::calculate_cumulative_index(const T&value) const
2392 {
2393  const size_t this_size = this->size();
2394 
2395 
2396 #ifdef __OPENNN_DEBUG__
2397 
2398  if(this_size == 0) {
2399  ostringstream buffer;
2400 
2401  buffer << "OpenNN Exception: Vector Template.\n"
2402  << "size_t calculate_cumulative_index(const T&) const.\n"
2403  << "Size must be greater than zero.\n";
2404 
2405  throw logic_error(buffer.str());
2406  }
2407 
2408  T cumulative_value = (*this)[this_size - 1];
2409 
2410  if(value > cumulative_value)
2411  {
2412  ostringstream buffer;
2413 
2414  buffer << "OpenNN Exception: Vector Template.\n"
2415  << "size_t calculate_cumulative_index(const T&) const.\n"
2416  << "Value(" << value << ") must be less than cumulative value("
2417  << cumulative_value << ").\n";
2418 
2419  throw logic_error(buffer.str());
2420  }
2421 
2422  for(size_t i = 1; i < this_size; i++)
2423  {
2424  if((*this)[i] <(*this)[i - 1])
2425  {
2426  ostringstream buffer;
2427 
2428  buffer << "OpenNN Exception: Vector Template.\n"
2429  << "int calculate_cumulative_index(const T&) const.\n"
2430  << "Vector elements must be crescent.\n";
2431 
2432  throw logic_error(buffer.str());
2433  }
2434  }
2435 
2436 #endif
2437 
2438  if(value <= (*this)[0])
2439  {
2440  return 0;
2441  }
2442 
2443  for(size_t i = 1; i < this_size; i++)
2444  {
2445  if(value >(*this)[i - 1] && value <= (*this)[i])
2446  {
2447  return(i);
2448  }
2449  }
2450 
2451  return(this_size - 1);
2452 }
2453 
2454 
2456 
2457 template <class T>
2459 {
2460  const size_t this_size = this->size();
2461 
2462  T sum = 0;
2463 
2464  for(size_t i = 0; i < this_size; i++)
2465  {
2466  sum += (*this)[i];
2467  }
2468 
2469  return sum;
2470 }
2471 
2472 
2475 
2476 template <class T>
2478 {
2479  const size_t this_size = this->size();
2480 
2481  T sum = 0;
2482 
2483  for(size_t i = 0; i < this_size; i++)
2484  {
2485  if(indices.contains(i))
2486  {
2487  sum += (*this)[i];
2488  }
2489  }
2490 
2491  return sum;
2492 }
2493 
2494 
2496 
2497 template <class T>
2499 {
2500  const size_t this_size = this->size();
2501 
2502  T sum = 0;
2503 
2504  for(size_t i = 0; i < this_size; i++)
2505  {
2506  if(!::isnan((*this)[i]))
2507  {
2508  sum += (*this)[i];
2509  }
2510  }
2511 
2512  return sum;
2513 }
2514 
2515 
2517 
2518 template <class T>
2520 {
2521  const size_t this_size = this->size();
2522 
2523  T product = 1;
2524 
2525  for(size_t i = 0; i < this_size; i++)
2526  {
2527  product *= (*this)[i];
2528  }
2529 
2530  return product;
2531 }
2532 
2533 
2535 
2536 template <class T>
2538 {
2539  Vector<size_t> indices(this->size());
2540 
2541 #ifdef __Cpp11__
2542 
2543  const Vector<size_t> less_rank = this->calculate_less_rank();
2544 
2545  for(size_t i = 0; i < this->size(); i++)
2546  {
2547  indices[less_rank[i]] = i;
2548  }
2549 
2550 #else
2551 
2552  indices.initialize_sequential();
2553  sort(indices.begin(), indices.end(), [this](size_t i1, size_t i2) {return (*this)[i1] < (*this)[i2];});
2554 
2555 #endif
2556 
2557  return indices;
2558 }
2559 
2560 
2562 
2563 template <class T>
2565 {
2566  Vector<T> sorted(*this);
2567 
2568  sort(sorted.begin(), sorted.end());
2569 
2570  return sorted;
2571 }
2572 
2573 
2577 
2578 template <class T>
2579 Vector<size_t> Vector<T>::calculate_lower_indices(const size_t& indices_number) const
2580 {
2581  return sort_ascending_indices().get_subvector(0,indices_number-1);
2582 }
2583 
2584 
2588 
2589 template <class T>
2590 Vector<T> Vector<T>::calculate_lower_values(const size_t& indices_number) const
2591 {
2592  return sort_ascending_values().get_subvector(0,indices_number-1);
2593 }
2594 
2595 
2597 
2598 template <class T>
2600 {
2601  Vector<size_t> indices(this->size());
2602 
2603 #ifdef __Cpp11__
2604 
2605  const Vector<size_t> greater_rank = this->calculate_greater_rank();
2606 
2607  for(size_t i = 0; i < this->size(); i++)
2608  {
2609  indices[greater_rank[i]] = i;
2610  }
2611 
2612 #else
2613 
2614  indices.initialize_sequential();
2615 
2616  sort(indices.begin(), indices.end(), [this](size_t i1, size_t i2) {return (*this)[i1] > (*this)[i2];});
2617 
2618 #endif
2619 
2620  return indices;
2621 }
2622 
2623 
2625 
2626 template <class T>
2628 {
2629  Vector<T> sorted(*this);
2630 
2631  sort(sorted.begin(), sorted.end());
2632 
2633  return sorted.get_reverse();
2634 }
2635 
2636 
2641 
2642 template <class T>
2644 {
2645  const size_t this_size = this->size();
2646 
2647  Vector<size_t> rank(this_size);
2648 
2649  Vector<T> sorted_vector(*this);
2650 
2651  sort(sorted_vector.begin(), sorted_vector.end(), less<double>());
2652 
2653  Vector<size_t> previous_rank;
2654  previous_rank.set(this_size);
2655 
2656  for(size_t i = 0; i < this_size; i++)
2657  {
2658  for(size_t j = 0; j < this_size; j++)
2659  {
2660  if(previous_rank.contains(static_cast<size_t>(j))) continue;
2661 
2662  if((*this)[i] == sorted_vector[j])
2663  {
2664  rank[static_cast<size_t>(i)] = j;
2665 
2666  previous_rank[static_cast<size_t>(i)] = j;
2667 
2668  break;
2669  }
2670  }
2671  }
2672 
2673  return rank;
2674 }
2675 
2676 
2681 
2682 template <class T>
2684 {
2685  const size_t this_size = this->size();
2686 
2687  Vector<size_t> rank(this_size);
2688 
2689  Vector<T> sorted_vector(*this);
2690 
2691  sort(sorted_vector.begin(), sorted_vector.end(), greater<T>());
2692 
2693  Vector<size_t> previous_rank;
2694  previous_rank.set(this_size);
2695 
2696  for(size_t i = 0; i < this_size; i++)
2697  {
2698  for(size_t j = 0; j < this_size; j++)
2699  {
2700  if(previous_rank.contains(j)) continue;
2701 
2702  if(sorted_vector[i] == (*this)[j])
2703  {
2704  rank[i] = j;
2705 
2706  previous_rank[i] = j;
2707 
2708  break;
2709  }
2710  }
2711  }
2712 
2713  return rank;
2714 }
2715 
2716 
2719 
2720 template <class T>
2722 {
2723  const size_t this_size = this->size();
2724 
2725  #ifdef __OPENNN_DEBUG__
2726  const size_t rank_size = rank.size();
2727 
2728  if(this_size != rank_size) {
2729  ostringstream buffer;
2730 
2731  buffer << "OpenNN Exception: Vector Template.\n"
2732  << "Vector<T> sort_rank(const Vector<size_t>&) const.\n"
2733  << "Sizes of vectors are " << this_size << " and " << rank_size
2734  << " and they must be the same.\n";
2735 
2736  throw logic_error(buffer.str());
2737  }
2738 
2739  #endif
2740 
2741  Vector<T> sorted_vector(this_size);
2742 
2743  for(size_t i = 0; i < this_size; i++)
2744  {
2745  sorted_vector[i] = (*this)[rank[i]];
2746  }
2747 
2748  return sorted_vector;
2749 }
2750 
2751 
2754 
2755 template <class T>
2756 inline Vector<T> Vector<T>::operator = (const initializer_list<T>& list) const
2757 {
2758  return Vector<T>(list);
2759 }
2760 
2761 
2764 
2765 template <class T>
2766 inline Vector<T> Vector<T>::operator+ (const T&scalar) const
2767 {
2768  const size_t this_size = this->size();
2769 
2770  Vector<T> sum(this_size);
2771 
2772  transform(this->begin(), this->end(), sum.begin(),
2773  bind2nd(plus<T>(), scalar));
2774 
2775  return sum;
2776 }
2777 
2778 
2781 
2782 template <class T>
2783 inline Vector<T> Vector<T>::operator+ (const Vector<T>&other_vector) const
2784 {
2785  const size_t this_size = this->size();
2786 
2787 #ifdef __OPENNN_DEBUG__
2788 
2789  const size_t other_size = other_vector.size();
2790 
2791  if(other_size != this_size) {
2792  ostringstream buffer;
2793 
2794  buffer << "OpenNN Exception: Vector Template.\n"
2795  << "Vector<T> operator + (const Vector<T>) const.\n"
2796  << "Sizes of vectors are " << this_size << " and " << other_size
2797  << " and they must be the same.\n";
2798 
2799  throw logic_error(buffer.str());
2800  }
2801 
2802 #endif
2803 
2804  Vector<T> sum(this_size);
2805 
2806  transform(this->begin(), this->end(), other_vector.begin(), sum.begin(),
2807  plus<T>());
2808 
2809  return sum;
2810 }
2811 
2812 
2815 
2816 template <class T>
2817 inline Vector<T> Vector<T>::operator-(const T&scalar) const
2818 {
2819  const size_t this_size = this->size();
2820 
2821  Vector<T> difference(this_size);
2822 
2823  transform(this->begin(), this->end(), difference.begin(),
2824  bind2nd(minus<T>(), scalar));
2825 
2826  return(difference);
2827 }
2828 
2829 
2832 
2833 template <class T>
2834 inline Vector<T> Vector<T>::operator-(const Vector<T>&other_vector) const
2835 {
2836  const size_t this_size = this->size();
2837 
2838 
2839 
2840 #ifdef __OPENNN_DEBUG__
2841 
2842  const size_t other_size = other_vector.size();
2843 
2844  if(other_size != this_size) {
2845  ostringstream buffer;
2846 
2847  buffer << "OpenNN Exception: Vector Template.\n"
2848  << "Vector<T> operator -(const Vector<T>&) const.\n"
2849  << "Sizes of vectors are " << this_size << " and " << other_size
2850  << " and they must be the same.\n";
2851 
2852  throw logic_error(buffer.str());
2853  }
2854 
2855 #endif
2856 
2857  Vector<T> difference(this_size);
2858 
2859  transform(this->begin(), this->end(), other_vector.begin(),
2860  difference.begin(), minus<T>());
2861 
2862  return(difference);
2863 }
2864 
2865 
2868 
2869 template <class T> Vector<T> Vector<T>::operator*(const T&scalar) const
2870 {
2871  const size_t this_size = this->size();
2872 
2873  Vector<T> product(this_size);
2874 
2875  transform(this->begin(), this->end(), product.begin(),
2876  bind2nd(multiplies<T>(), scalar));
2877 
2878  return product;
2879 }
2880 
2881 
2884 
2885 template <class T>
2886 inline Vector<T> Vector<T>::operator*(const Vector<T>&other_vector) const
2887 {
2888  const size_t this_size = this->size();
2889 
2890 #ifdef __OPENNN_DEBUG__
2891 
2892  const size_t other_size = other_vector.size();
2893 
2894  if(other_size != this_size) {
2895  ostringstream buffer;
2896 
2897  buffer << "OpenNN Exception: Vector Template.\n"
2898  << "Vector<T> operator *(const Vector<T>&) const.\n"
2899  << "Size of other vector(" << other_size
2900  << ") must be equal to size of this vector(" << this_size << ").\n";
2901 
2902  throw logic_error(buffer.str());
2903  }
2904 
2905 #endif
2906 
2907  Vector<T> product(this_size);
2908 
2909  transform(this->begin(), this->end(), other_vector.begin(),
2910  product.begin(), multiplies<T>());
2911 
2912  return product;
2913 }
2914 
2915 
2918 
2919 template <class T>
2921 {
2922  const size_t rows_number = matrix.get_rows_number();
2923  const size_t columns_number = matrix.get_columns_number();
2924 
2925 #ifdef __OPENNN_DEBUG__
2926 
2927  const size_t this_size = this->size();
2928 
2929  if(rows_number != this_size) {
2930  ostringstream buffer;
2931 
2932  buffer << "OpenNN Exception: Vector Template.\n"
2933  << "Matrix<T> operator *(const Matrix<T>&) const.\n"
2934  << "Number of matrix rows(" << rows_number << ") must be equal to vector size(" << this_size << ").\n";
2935 
2936  throw logic_error(buffer.str());
2937  }
2938 
2939 #endif
2940 
2941  Matrix<T> product(rows_number, columns_number);
2942 
2943  for(size_t i = 0; i < rows_number; i++) {
2944  for(size_t j = 0; j < columns_number; j++) {
2945  product(i, j) = (*this)[i] * matrix(i, j);
2946  }
2947  }
2948 
2949  return product;
2950 }
2951 
2952 
2955 
2956 template <class T> Vector<T> Vector<T>::operator/(const T&scalar) const
2957 {
2958  const size_t this_size = this->size();
2959 
2960  Vector<T> cocient(this_size);
2961 
2962  transform(this->begin(), this->end(), cocient.begin(),
2963  bind2nd(divides<T>(), scalar));
2964 
2965  return(cocient);
2966 }
2967 
2968 
2971 
2972 template <class T>
2973 Vector<T> Vector<T>::operator/(const Vector<T>&other_vector) const
2974 {
2975  const size_t this_size = this->size();
2976 
2977 
2978 
2979 #ifdef __OPENNN_DEBUG__
2980 
2981  const size_t other_size = other_vector.size();
2982 
2983  if(other_size != this_size) {
2984  ostringstream buffer;
2985 
2986  buffer << "OpenNN Exception: Vector Template.\n"
2987  << "Vector<T> operator /(const Vector<T>&) const.\n"
2988  << "Both vector sizes must be the same.\n";
2989 
2990  throw logic_error(buffer.str());
2991  }
2992 
2993 #endif
2994 
2995  Vector<T> cocient(this_size);
2996 
2997  transform(this->begin(), this->end(), other_vector.begin(),
2998  cocient.begin(), divides<T>());
2999 
3000  return(cocient);
3001 }
3002 
3003 
3006 
3007 template <class T> void Vector<T>::operator+= (const T&value)
3008 {
3009  const size_t this_size = this->size();
3010 
3011  for(size_t i = 0; i < this_size; i++) {
3012  (*this)[i] = (*this)[i] + value;
3013  }
3014 }
3015 
3016 
3019 
3020 template <class T> void Vector<T>::operator+= (const Vector<T>&other_vector)
3021 {
3022  const size_t this_size = this->size();
3023 
3024 
3025 
3026 #ifdef __OPENNN_DEBUG__
3027 
3028  const size_t other_size = other_vector.size();
3029 
3030  if(other_size != this_size) {
3031  ostringstream buffer;
3032 
3033  buffer << "OpenNN Exception: Vector Template.\n"
3034  << "void operator += (const Vector<T>&).\n"
3035  << "Both vector sizes must be the same.\n";
3036 
3037  throw logic_error(buffer.str());
3038  }
3039 
3040 #endif
3041 
3042  for(size_t i = 0; i < this_size; i++) {
3043  (*this)[i] = (*this)[i] + other_vector[i];
3044  }
3045 }
3046 
3047 
3050 
3051 template <class T> void Vector<T>::operator-= (const T&value)
3052 {
3053  const size_t this_size = this->size();
3054 
3055  for(size_t i = 0; i < this_size; i++) {
3056  (*this)[i] = (*this)[i] - value;
3057  }
3058 }
3059 
3060 
3063 
3064 template <class T> void Vector<T>::operator-= (const Vector<T>&other_vector)
3065 {
3066  const size_t this_size = this->size();
3067 
3068 
3069 
3070 #ifdef __OPENNN_DEBUG__
3071 
3072  const size_t other_size = other_vector.size();
3073 
3074  if(other_size != this_size) {
3075  ostringstream buffer;
3076 
3077  buffer << "OpenNN Exception: Vector Template.\n"
3078  << "void operator -= (const Vector<T>&).\n"
3079  << "Both vector sizes must be the same.\n";
3080 
3081  throw logic_error(buffer.str());
3082  }
3083 
3084 #endif
3085 
3086  for(size_t i = 0; i < this_size; i++) {
3087  (*this)[i] = (*this)[i] - other_vector[i];
3088  }
3089 }
3090 
3091 
3094 
3095 template <class T> void Vector<T>::operator*= (const T&value)
3096 {
3097  const size_t this_size = this->size();
3098 
3099  for(size_t i = 0; i < this_size; i++) {
3100  (*this)[i] = (*this)[i] * value;
3101  }
3102 }
3103 
3104 
3107 
3108 template <class T>
3109 void Vector<T>::operator*= (const Vector<T>&other_vector)
3110 {
3111  const size_t this_size = this->size();
3112 
3113 #ifdef __OPENNN_DEBUG__
3114 
3115  const size_t other_size = other_vector.size();
3116 
3117  if(other_size != this_size) {
3118  ostringstream buffer;
3119 
3120  buffer << "OpenNN Exception: Vector Template.\n"
3121  << "void operator *= (const Vector<T>&).\n"
3122  << "Both vector sizes must be the same.\n";
3123 
3124  throw logic_error(buffer.str());
3125  }
3126 
3127 #endif
3128 
3129  for(size_t i = 0; i < this_size; i++) {
3130  (*this)[i] = (*this)[i] * other_vector[i];
3131  }
3132 }
3133 
3134 
3137 
3138 template <class T> void Vector<T>::operator/= (const T&value)
3139 {
3140  const size_t this_size = this->size();
3141 
3142  for(size_t i = 0; i < this_size; i++)
3143  {
3144  (*this)[i] = (*this)[i] / value;
3145  }
3146 }
3147 
3148 
3151 
3152 template <class T> void Vector<T>::operator/= (const Vector<T>&other_vector)
3153 {
3154  const size_t this_size = this->size();
3155 
3156 
3157 
3158 #ifdef __OPENNN_DEBUG__
3159 
3160  const size_t other_size = other_vector.size();
3161 
3162  if(other_size != this_size) {
3163  ostringstream buffer;
3164 
3165  buffer << "OpenNN Exception: Vector Template.\n"
3166  << "void operator /= (const Vector<T>&).\n"
3167  << "Both vector sizes must be the same.\n";
3168 
3169  throw logic_error(buffer.str());
3170  }
3171 
3172 #endif
3173 
3174  for(size_t i = 0; i < this_size; i++)
3175  {
3176  (*this)[i] = (*this)[i] / other_vector[i];
3177  }
3178 }
3179 
3180 
3182 
3183 template <class T>
3185 {
3186  Vector<T> new_vector(*this);
3187 
3188  for(size_t i = 0; i < this->size(); i++)
3189  {
3190  if(new_vector[i] < 0)
3191  {
3192  new_vector[i] = 0;
3193  }
3194  }
3195 
3196  return new_vector;
3197 }
3198 
3199 
3201 
3202 template <class T>
3204 {
3205  Vector<T> new_vector(*this);
3206  for(size_t i = 0; i < this->size(); i++)
3207  {
3208  if(new_vector[i] > 0)
3209  {
3210  new_vector[i] = 0;
3211  }
3212  }
3213  return new_vector;
3214 }
3215 
3216 
3220 
3221 template <class T>
3223 {
3224  const size_t this_size = this->size();
3225 
3226  Matrix<T> matrix(this_size, this_size, 0.0);
3227 
3228  matrix.set_diagonal(*this);
3229 
3230  return matrix;
3231 }
3232 
3233 
3238 
3239 template <class T>
3240 Vector<T> Vector<T>::get_subvector(const size_t& first_index, const size_t& last_index) const
3241 {
3242 #ifdef __OPENNN_DEBUG__
3243 
3244  const size_t this_size = this->size();
3245 
3246  if(first_index >= this_size) {
3247  ostringstream buffer;
3248 
3249  buffer << "OpenNN Exception: Vector Template.\n"
3250  << "Vector<T> get_subvector(const size_t&, const size_t&) const method.\n"
3251  << "First index (" << first_index << ") is equal or greater than size (" << this_size << ").\n";
3252 
3253  throw logic_error(buffer.str());
3254  }
3255 
3256  if(last_index >= this_size) {
3257  ostringstream buffer;
3258 
3259  buffer << "OpenNN Exception: Vector Template.\n"
3260  << "Vector<T> get_subvector(const size_t&, const size_t&) const method.\n"
3261  << "Last index (" << last_index << ") is equal or greater than size (" << this_size << ").\n";
3262 
3263  throw logic_error(buffer.str());
3264  }
3265 
3266 #endif
3267 
3268  Vector<T> subvector(last_index-first_index + 1);
3269 
3270  for(size_t i = first_index ; i < last_index+1 ; i++)
3271  {
3272  subvector[i-first_index] = (*this)[i];
3273  }
3274 
3275  return subvector;
3276 }
3277 
3278 
3283 
3284 template <class T>
3286 {
3287  const size_t new_size = indices.size();
3288 
3289  if(new_size == 0) return Vector<T>();
3290 
3291 #ifdef __OPENNN_DEBUG__
3292 
3293  const size_t this_size = this->size();
3294 
3295  for(size_t i = 0; i < new_size; i++) {
3296  if(indices[i] > this_size) {
3297  ostringstream buffer;
3298 
3299  buffer << "OpenNN Exception: Vector Template.\n"
3300  << "Vector<T> get_subvector(const Vector<T>&) const method.\n"
3301  << "Index (" << indices[i] << ") is equal or greater than this size.\n";
3302 
3303  throw logic_error(buffer.str());
3304  }
3305  }
3306 
3307 #endif
3308 
3309  Vector<T> subvector(new_size);
3310 
3311  for(size_t i = 0; i < new_size; i++)
3312  {
3313  subvector[i] = (*this)[indices[i]];
3314  }
3315 
3316  return subvector;
3317 }
3318 
3319 
3322 
3323 template <class T>
3325 {
3326  const Vector<size_t> indices = selection.get_indices_equal_to(true);
3327 
3328  return(get_subvector(indices));
3329 }
3330 
3331 
3334 
3335 template <class T>
3336 Vector<T> Vector<T>::get_subvector_random(const size_t& new_size) const
3337 {
3338  if(new_size == this->size()) return Vector<T>(*this);
3339 
3340  Vector<T> new_vector(*this);
3341 
3342  random_shuffle(new_vector.begin(), new_vector.end());
3343 
3344  return new_vector.get_first(new_size);
3345 }
3346 
3347 
3350 
3351 template <class T>
3352 Vector<T> Vector<T>::get_first(const size_t &elements_number) const
3353 {
3354 
3355 
3356 #ifdef __OPENNN_DEBUG__
3357 
3358  const size_t this_size = this->size();
3359 
3360  if(elements_number > this_size) {
3361  ostringstream buffer;
3362 
3363  buffer << "OpenNN Exception: Vector Template.\n"
3364  << "Vector<T> get_first(const size_t&) const method.\n"
3365  << "Number of elements must be equal or greater than this size.\n";
3366 
3367  throw logic_error(buffer.str());
3368  }
3369 
3370 #endif
3371 
3372  Vector<T> subvector(elements_number);
3373 
3374  for(size_t i = 0; i < elements_number; i++)
3375  {
3376  subvector[i] = (*this)[i];
3377  }
3378 
3379  return subvector;
3380 }
3381 
3382 
3385 
3386 template <class T>
3387 Vector<T> Vector<T>::get_last(const size_t &elements_number) const
3388 {
3389  const size_t this_size = this->size();
3390 
3391 
3392 #ifdef __OPENNN_DEBUG__
3393 
3394  if(elements_number > this_size) {
3395  ostringstream buffer;
3396 
3397  buffer << "OpenNN Exception: Vector Template.\n"
3398  << "Vector<T> get_last(const size_t&) const method.\n"
3399  << "Number of elements must be equal or greater than this size.\n";
3400 
3401  throw logic_error(buffer.str());
3402  }
3403 
3404 #endif
3405 
3406  Vector<T> subvector(elements_number);
3407 
3408  for(size_t i = 0; i < elements_number; i++)
3409  {
3410  subvector[i] = (*this)[i + this_size - elements_number];
3411  }
3412 
3413  return subvector;
3414 }
3415 
3416 
3420 
3421 template <class T>
3422 Vector<T> Vector<T>::get_integer_elements(const size_t& maximum_integers) const
3423 {
3424 
3425  const size_t this_size = this->size();
3426 
3427  const size_t integers_number = this->count_integers(maximum_integers);
3428 
3429  Vector<T> integers(integers_number);
3430  size_t index = 0;
3431 
3432  for(size_t i = 0; i < this_size; i++)
3433  {
3434  if(!integers.contains((*this)[i]))
3435  {
3436  integers[index] = (*this)[i];
3437  index++;
3438 
3439  if(index > integers_number)
3440  {
3441  break;
3442  }
3443  }
3444  }
3445 
3446  return integers;
3447 }
3448 
3449 
3451 
3452 template <class T>
3453 Vector<T> Vector<T>::get_integer_elements_missing_values(const size_t& maximum_integers) const
3454 {
3455 
3456  const size_t this_size = this->size();
3457 
3458  const size_t integers_number = this->count_integers_missing_values(maximum_integers);
3459 
3460  Vector<T> integers(integers_number);
3461  size_t index = 0;
3462 
3463  for(size_t i = 0; i < this_size; i++)
3464  {
3465  if(!::isnan((*this)[i]))
3466  {
3467  if(!integers.contains((*this)[i]))
3468  {
3469  integers[index] = (*this)[i];
3470  index++;
3471 
3472  if(index > integers_number) break;
3473  }
3474  }
3475  }
3476 
3477  return integers;
3478 }
3479 
3480 
3485 
3486 template <class T> void Vector<T>::load(const string &file_name)
3487 {
3488  ifstream file(file_name.c_str());
3489 
3490  stringstream buffer;
3491 
3492  string line;
3493 
3494  while(file.good())
3495  {
3496  getline(file, line);
3497 
3498  buffer << line;
3499  }
3500 
3501  istream_iterator<string> it(buffer);
3502  istream_iterator<string> end;
3503 
3504  const vector<string> results(it, end);
3505 
3506  const size_t new_size = static_cast<size_t>(results.size());
3507 
3508  this->resize(new_size);
3509 
3510  file.clear();
3511  file.seekg(0, ios::beg);
3512 
3513  // Read data
3514 
3515  for(size_t i = 0; i < new_size; i++) {
3516  file >>(*this)[i];
3517  }
3518 
3519  file.close();
3520 }
3521 
3522 
3528 
3529 template <class T> void Vector<T>::save(const string &file_name, const char& separator) const
3530 {
3531  ofstream file(file_name.c_str());
3532 
3533  if(!file.is_open()) {
3534  ostringstream buffer;
3535 
3536  buffer << "OpenNN Exception: Vector template.\n"
3537  << "void save(const string&) const method.\n"
3538  << "Cannot open vector data file.\n";
3539 
3540  throw logic_error(buffer.str());
3541  }
3542 
3543  // Write file
3544 
3545  const size_t this_size = this->size();
3546 
3547  if(this_size > 0) {
3548  file <<(*this)[0];
3549 
3550  for(size_t i = 1; i < this_size; i++) {
3551  file << separator << (*this)[i];
3552  }
3553 
3554  file << endl;
3555  }
3556 
3557  // Close file
3558 
3559  file.close();
3560 }
3561 
3562 
3567 
3568 template <class T>
3569 void Vector<T>::embed(const size_t &position, const Vector<T>&other_vector)
3570 {
3571  const size_t other_size = other_vector.size();
3572 
3573 #ifdef __OPENNN_DEBUG__
3574 
3575  const size_t this_size = this->size();
3576 
3577  if(position + other_size > this_size)
3578  {
3579  ostringstream buffer;
3580 
3581  buffer << "OpenNN Exception: Vector Template.\n"
3582  << "void tuck_in(const size_t &, const Vector<T>&) method.\n"
3583  << "Cannot tuck in vector.\n";
3584 
3585  throw logic_error(buffer.str());
3586  }
3587 
3588 #endif
3589 
3590  for(size_t i = 0; i < other_size; i++)
3591  {
3592  (*this)[position + i] = other_vector[i];
3593  }
3594 }
3595 
3596 
3597 template <class T>
3598 Vector<T> Vector<T>::insert_elements(const size_t & position, const Vector<T>& other_vector)
3599 {
3600 
3601  const size_t other_size =other_vector.size();
3602  const size_t new_size = this->size() + other_size;
3603 
3604  Vector<T> new_vector(new_size);
3605 
3606  for(size_t i = 0 ; i < position ; i++)
3607  {
3608  new_vector[i] = (*this)[i];
3609  }
3610  for(size_t i = position ; i < position + other_size ; i++)
3611  {
3612  new_vector[i] = other_vector[i-position];
3613  }
3614  for(size_t i = position + other_size ; i < new_size ; i++)
3615  {
3616  new_vector[i] = (*this)[i-other_size];
3617  }
3618 
3619  return new_vector;
3620 }
3621 
3625 
3626 template <class T>
3627 Vector<T> Vector<T>::insert_element(const size_t &index, const T&value) const
3628 {
3629 #ifdef __OPENNN_DEBUG__
3630 
3631  const size_t this_size = this->size();
3632 
3633  if(index > this_size) {
3634  ostringstream buffer;
3635 
3636  buffer
3637  << "OpenNN Exception: Vector Template.\n"
3638  << "void insert_element(const size_t& index, const T& value) method.\n"
3639  << "Index is greater than vector size.\n";
3640 
3641  throw logic_error(buffer.str());
3642  }
3643 
3644 #endif
3645 
3646  Vector<T> other_vector(*this);
3647 
3648  const auto it = other_vector.begin();
3649 
3650  other_vector.insert(it+index, value);
3651 
3652  return other_vector;
3653 }
3654 
3655 
3659 
3660 template <class T>
3661 void Vector<T>::replace_value(const T& find_value, const T& replace_value)
3662 {
3663  for(size_t i = 0; i < this->size(); i++)
3664  {
3665  if((*this)[i] == find_value) (*this)[i] = replace_value;
3666  }
3667 }
3668 
3669 
3674 
3675 template <class T>
3676 Vector<T> Vector<T>::delete_index(const size_t &index) const
3677 {
3678  const size_t this_size = this->size();
3679 
3680 #ifdef __OPENNN_DEBUG__
3681 
3682  if(index >= this_size) {
3683  ostringstream buffer;
3684 
3685  buffer << "OpenNN Exception: Vector Template.\n"
3686  << "Vector<T> remove_element(const size_t&) const method.\n"
3687  << "Index("<<index<<") is equal or greater than vector size("<<this_size<<").\n";
3688 
3689  throw logic_error(buffer.str());
3690  }
3691 
3692 #endif
3693 
3694  Vector<T> other_vector(this_size - 1);
3695 
3696  for(size_t i = 0; i < this_size; i++) {
3697  if(i < index)
3698  {
3699  other_vector[i] = (*this)[i];
3700  }
3701  else if(i > index) {
3702  other_vector[i - 1] = (*this)[i];
3703  }
3704  }
3705 
3706  return other_vector;
3707 }
3708 
3709 
3714 
3715 template <class T>
3717 {
3718  const size_t this_size = this->size();
3719  const size_t indices_size = indices.size();
3720 
3721 
3722 
3723  #ifdef __OPENNN_DEBUG__
3724 
3725 // if(maximum(indices) >= this_size) {
3726 // ostringstream buffer;
3727 
3728 // buffer << "OpenNN Exception: Vector Template.\n"
3729 // << "Vector<T> remove_elements(const Vector<size_t>&) const method.\n"
3730 // << "Maximum index is equal or greater than vector size.\n";
3731 
3732 // throw logic_error(buffer.str());
3733 // }
3734 
3735  if(indices_size > this_size) {
3736  ostringstream buffer;
3737 
3738  buffer << "OpenNN Exception: Vector Template.\n"
3739  << "Vector<T> delete_indices(const Vector<size_t>&) const method.\n"
3740  << "Number of indices("<< indices_size << ") to remove is greater than vector size(" << this_size << ").\n";
3741 
3742  throw logic_error(buffer.str());
3743  }
3744 
3745  #endif
3746 
3747  Vector<T> other_vector(this_size - indices_size);
3748 
3749  size_t index = 0;
3750 
3751  for(size_t i = 0; i < this_size; i++)
3752  {
3753  if(!indices.contains(i))
3754  {
3755  other_vector[index] = (*this)[i];
3756 
3757  index++;
3758  }
3759  }
3760 
3761  return other_vector;
3762 }
3763 
3764 
3768 
3769 template <class T>
3771 {
3772  const size_t this_size = this->size();
3773 
3774  const size_t value_count = count_equal_to(value);
3775 
3776  if(value_count == 0) return Vector<T>(*this);
3777 
3778  const size_t other_size = this_size - value_count;
3779 
3780  Vector<T> other_vector(other_size);
3781 
3782  size_t other_index = 0;
3783 
3784  for(size_t i = 0; i < this_size; i++)
3785  {
3786  if((*this)[i] != value)
3787  {
3788  other_vector[other_index] = (*this)[i];
3789 
3790  other_index++;
3791  }
3792  }
3793 
3794  return other_vector;
3795 }
3796 
3800 
3801 template <class T>
3803 {
3804  Vector<T> new_vector(*this);
3805 
3806  for(size_t i = 0; i < values.size(); i++)
3807  {
3808  new_vector = new_vector.delete_value(values[i]);
3809  }
3810 
3811  return new_vector;
3812 }
3813 
3814 
3817 
3818 template <class T>
3819 Vector<T> Vector<T>::assemble(const Vector<T>&other_vector) const
3820 {
3821  const size_t this_size = this->size();
3822  const size_t other_size = other_vector.size();
3823 
3824  if(this_size == 0 && other_size == 0)
3825  {
3826  Vector<T> assembly;
3827 
3828  return assembly;
3829  }
3830  else if(this_size == 0)
3831  {
3832  return other_vector;
3833  }
3834  else if(other_size == 0)
3835  {
3836  return *this;
3837  }
3838  else
3839  {
3840  Vector<T> assembly(this_size + other_size);
3841 
3842  for(size_t i = 0; i < this_size; i++)
3843  {
3844  assembly[i] = (*this)[i];
3845  }
3846 
3847  for(size_t i = 0; i < other_size; i++)
3848  {
3849  assembly[this_size + i] = other_vector[i];
3850  }
3851 
3852  return assembly;
3853  }
3854 }
3855 
3856 
3859 
3860 template <class T>
3862 {
3863  const size_t vectors_size = vectors.size();
3864 
3865  size_t new_size = 0;
3866 
3867  for(size_t i = 0; i < vectors_size; i++)
3868  {
3869  new_size += vectors[i].size();
3870  }
3871 
3872  Vector<T> new_vector(new_size);
3873 
3874  size_t index = 0;
3875 
3876  for(size_t i = 0; i < vectors_size; i++)
3877  {
3878  for(size_t j = 0; j < vectors[i].size(); j++)
3879  {
3880  new_vector[index] = vectors[i][j];
3881  index++;
3882  }
3883  }
3884 
3885  return new_vector;
3886 }
3887 
3888 
3893 
3894 template <class T>
3896 {
3897  if(this->empty())
3898  {
3899  return other_vector;
3900  }
3901 
3902  if(other_vector.empty())
3903  {
3904  Vector<T> copy(*this);
3905  return copy;
3906  }
3907 
3908  const size_t this_size = this->size();
3909 
3910  Vector<T> difference(this_size);
3911  typename vector<T>::iterator iterator;
3912 
3913  Vector<T> copy_this(*this);
3914  Vector<T> copy_other_vector(other_vector);
3915 
3916  sort(copy_this.begin(), copy_this.end());
3917  sort(copy_other_vector.begin(), copy_other_vector.end());
3918 
3919  iterator = set_difference(copy_this.begin(),copy_this.end(), copy_other_vector.begin(), copy_other_vector.end(), difference.begin());
3920 
3921  difference.resize(iterator - difference.begin());
3922 
3923  return(difference);
3924 }
3925 
3926 
3931 
3932 template <class T>
3933 Vector<T> Vector<T>::get_union(const Vector<T>& other_vector) const
3934 {
3935  Vector<T> this_copy(*this);
3936  sort(this_copy.begin(), this_copy.end());
3937 
3938  Vector<T> other_copy(other_vector);
3939  sort(other_copy.begin(), other_copy.end());
3940 
3941  Vector<T> union_vector;
3942 
3943  set_union(this_copy.begin(), this_copy.end(), other_copy.begin(), other_copy.end(), back_inserter(union_vector));
3944 
3945  return union_vector;
3946 }
3947 
3948 
3953 
3954 template <class T>
3956 {
3957  Vector<T> this_copy(*this);
3958  sort(this_copy.begin(), this_copy.end());
3959 
3960  Vector<T> other_copy(other_vector);
3961  sort(other_copy.begin(), other_copy.end());
3962 
3963  Vector<T> intersection;
3964 
3965  set_intersection(this_copy.begin(), this_copy.end(), other_copy.begin(), other_copy.end(), back_inserter(intersection));
3966 
3967  return intersection;
3968 }
3969 
3970 
3973 
3974 template <class T>
3976 {
3977  Vector<T> copy_vector(*this);
3978 
3979  sort(copy_vector.begin(), copy_vector.end());
3980 
3981  const auto last = unique(copy_vector.begin(), copy_vector.end());
3982 
3983  copy_vector.erase(last, copy_vector.end());
3984 
3985  return copy_vector;
3986 }
3987 
3988 
3991 
3992 template <class T>
3994 {
3995  const Vector<T> unique = get_unique_elements();
3996 
3997  const size_t unique_size = unique.size();
3998 
3999  Vector<size_t> unique_count(unique_size);
4000 
4001  #pragma omp parallel for
4002  for(int i = 0; i < static_cast<int>(unique_size); i++)
4003  {
4004  unique_count[i] = count_equal_to(unique[i]);
4005  }
4006 
4007  return(unique_count);
4008 }
4009 
4010 
4016 
4017 template <class T>
4019 {
4020  const size_t this_size = this->size();
4021 
4022  const Vector<T> unique = get_unique_elements();
4023 
4024  const Vector<size_t> count = count_unique();
4025 
4026  const Vector<double> percentage = count_unique().to_double_vector()*(100.0/static_cast<double>(this_size));
4027 
4028  const size_t unique_size = unique.size();
4029 
4030 
4031  Matrix<T> unique_matrix(unique_size, 3);
4032  unique_matrix.set_column(0, unique.to_string_vector());
4033  unique_matrix.set_column(1, count.to_string_vector());
4034  unique_matrix.set_column(2, percentage.to_string_vector());
4035 
4036  unique_matrix = unique_matrix.sort_descending_strings(1);
4037 
4038  cout << "Total: " << this_size << endl;
4039 
4040  for(size_t i = 0; i < unique_size; i++)
4041  {
4042  cout << unique_matrix(i,0) << ": " << unique_matrix(i,1) << " (" << unique_matrix(i,2) << "%)" << endl;
4043  }
4044 }
4045 
4046 
4052 
4053 template <class T>
4055 {
4056  const size_t this_size = this->size();
4057 
4058  const Vector<T> unique = get_unique_elements();
4059 
4060  const Vector<size_t> count = count_unique();
4061 
4062  const Vector<double> percentage = count_unique().to_double_vector()*(100.0/static_cast<double>(this_size));
4063 
4064  const size_t unique_size = unique.size();
4065 
4066 
4067  Matrix<T> unique_matrix(unique_size, 3);
4068  unique_matrix.set_column(0, unique);
4069  unique_matrix.set_column(1, count);
4070  unique_matrix.set_column(2, percentage);
4071 
4072  unique_matrix = unique_matrix.sort_descending(1);
4073 
4074  cout << "Total: " << this_size << endl;
4075 
4076  for(size_t i = 0; i < unique_size; i++)
4077  {
4078  cout << unique_matrix(i,0) << ": " << unique_matrix(i,1) << " (" << unique_matrix(i,2) << "%)" << endl;
4079  }
4080 }
4081 
4082 
4087 
4088 template <class T>
4090 {
4091  const Vector<T> unique = get_unique_elements();
4092 
4093  const Vector<size_t> count = count_unique();
4094 
4095  const size_t unique_size = unique.size();
4096 
4097  Matrix<T> unique_matrix(unique_size, 2);
4098  unique_matrix.set_column(0, unique.to_string_vector());
4099  unique_matrix.set_column(1, count.to_string_vector());
4100 
4101  unique_matrix = unique_matrix.sort_descending_strings(1);
4102 
4103  const size_t end = unique_size < rank ? unique_size : rank;
4104 
4105  const Vector<T> top = unique_matrix.get_column(0).get_first(end);
4106 
4107  return(top);
4108 }
4109 
4110 
4115 
4116 template <class T>
4118 {
4119  const Vector<T> unique = get_unique_elements();
4120 
4121  const Vector<size_t> count = count_unique();
4122 
4123  const Vector<double> count_double = count.to_double_vector();
4124 
4125  const size_t unique_size = unique.size();
4126 
4127  Matrix<T> unique_matrix(unique_size, 2);
4128  unique_matrix.set_column(0, unique);
4129  unique_matrix.set_column(1, count_double);
4130 
4131  unique_matrix = unique_matrix.sort_descending(1);
4132 
4133  const size_t end = unique_size < rank ? unique_size : rank;
4134 
4135  const Vector<T> top = unique_matrix.get_column(0).get_first(end);
4136 
4137  return top;
4138 }
4139 
4143 
4144 template <class T>
4145 void Vector<T>::print_top_string(const size_t& rank) const
4146 {
4147  const Vector<T> unique = get_unique_elements();
4148 
4149  const Vector<size_t> count = count_unique();
4150 
4151  const size_t this_size = this->size();
4152 
4153  const Vector<double> percentage = count_unique().to_double_vector()*(100.0/static_cast<double>(this_size));
4154 
4155  const size_t unique_size = unique.size();
4156 
4157  if(unique_size == 0) return;
4158 
4159  Matrix<T> unique_matrix(unique_size, 3);
4160  unique_matrix.set_column(0, unique.to_string_vector());
4161  unique_matrix.set_column(1, count.to_string_vector());
4162  unique_matrix.set_column(2, percentage.to_string_vector());
4163 
4164  unique_matrix = unique_matrix.sort_descending_strings(1);
4165 
4166  const size_t end = unique_size < rank ? unique_size : rank;
4167 
4168  for(size_t i = 0; i < end; i++)
4169  {
4170  cout << i+1 << ". " << unique_matrix(i,0) << ": " << unique_matrix(i,1) << " (" << unique_matrix(i,2) << "%)" << endl;
4171  }
4172 }
4173 
4174 
4176 
4177 template <class T>
4178 vector<T> Vector<T>::to_std_vector() const
4179 {
4180  const size_t this_size = this->size();
4181 
4182  vector<T> std_vector(this_size);
4183 
4184  for(size_t i = 0; i < this_size; i++)
4185  {
4186  std_vector[i] = (*this)[i];
4187  }
4188 
4189  return(std_vector);
4190 }
4191 
4192 
4194 
4195 template <class T>
4197 {
4198  const size_t this_size = this->size();
4199 
4200  Vector<float> float_vector(this_size);
4201 
4202  for(size_t i = 0; i < this_size; i++)
4203  {
4204  float_vector[i] = static_cast<float>((*this)[i]);
4205  }
4206 
4207  return(float_vector);
4208 }
4209 
4210 
4212 
4213 template <class T>
4215 {
4216  const size_t this_size = this->size();
4217 
4218  Vector<double> double_vector(this_size);
4219 
4220  for(size_t i = 0; i < this_size; i++)
4221  {
4222  double_vector[i] = static_cast<double>((*this)[i]);
4223  }
4224 
4225  return(double_vector);
4226 }
4227 
4228 
4230 
4231 template <class T>
4233 {
4234  const size_t this_size = this->size();
4235 
4236  Vector<int> int_vector(this_size);
4237 
4238  for(size_t i = 0; i < this_size; i++)
4239  {
4240  int_vector[i] = static_cast<int>((*this)[i]);
4241  }
4242 
4243  return(int_vector);
4244 }
4245 
4246 
4248 
4249 template <class T>
4251 {
4252  const size_t this_size = this->size();
4253 
4254  Vector<size_t> size_t_vector(this_size);
4255 
4256  for(size_t i = 0; i < this_size; i++)
4257  {
4258  size_t_vector[i] = static_cast<size_t>((*this)[i]);
4259  }
4260 
4261  return(size_t_vector);
4262 }
4263 
4264 
4266 
4267 template <class T>
4269 {
4270  const size_t this_size = this->size();
4271 
4272  Vector<time_t> size_t_vector(this_size);
4273 
4274  for(size_t i = 0; i < this_size; i++)
4275  {
4276  size_t_vector[i] = static_cast<time_t>((*this)[i]);
4277  }
4278 
4279  return(size_t_vector);
4280 }
4281 
4282 
4285 
4286 template <class T>
4288 {
4289  const Vector<T> unique = get_unique_elements();
4290 
4291 // if(unique.size() != 2)
4292 // {
4293 // ostringstream buffer;
4294 
4295 // buffer << "OpenNN Exception: Vector Template.\n"
4296 // << "Vector<bool> to_bool_vector() const.\n"
4297 // << "Number of unique items(" << get_unique_elements().size() << ") must be 2.\n";
4298 
4299 // throw logic_error(buffer.str());
4300 // }
4301 
4302  const size_t this_size = this->size();
4303 
4304  Vector<bool> new_vector(this_size);
4305 
4306  for(size_t i = 0; i < this_size; i++)
4307  {
4308  if((*this)[i] == unique[0])
4309  {
4310  new_vector[i] = true;
4311  }
4312  else
4313  {
4314  new_vector[i] = false;
4315  }
4316  }
4317 
4318  return new_vector;
4319 }
4320 
4321 
4326 
4327 template <class T>
4329 {
4330  const Vector<T> unique = get_unique_elements();
4331 
4332  const size_t unique_number = unique.size();
4333 
4334  const size_t this_size = this->size();
4335 
4336  Matrix<T> new_matrix(this_size, unique_number, 0.0);
4337 
4338  for(size_t i = 0; i < unique_number; i++)
4339  {
4340  for(size_t j = 0; j < this_size; j++)
4341  {
4342  if((*this)[j] == unique[j])
4343  {
4344  new_matrix(j, i) = 1.0;
4345  }
4346  }
4347  }
4348 
4349  return new_matrix;
4350 }
4351 
4352 
4353 
4355 
4356 template <class T>
4358 {
4359  const size_t this_size = this->size();
4360 
4361  Vector<string> string_vector(this_size);
4362  ostringstream buffer;
4363 
4364  for(size_t i = 0; i < this_size; i++)
4365  {
4366  buffer.str("");
4367  buffer << (*this)[i];
4368 
4369  string_vector[i] = buffer.str();
4370  }
4371 
4372  return(string_vector);
4373 }
4374 
4375 
4377 
4378 template <class T>
4380 {
4381  const size_t this_size = this->size();
4382 
4383  Vector<double> double_vector(this_size);
4384 
4385  for(size_t i = 0; i < this_size; i++)
4386  {
4387  try
4388  {
4389  stringstream buffer;
4390 
4391  buffer << (*this)[i];
4392 
4393  double_vector[i] = stod(buffer.str());
4394  }
4395  catch(const logic_error&)
4396  {
4397  double_vector[i] = nan("");
4398  }
4399  }
4400 
4401  return double_vector;
4402 }
4403 
4404 
4406 
4407 template <class T>
4409 {
4410  const size_t this_size = this->size();
4411 
4412  Vector<int> int_vector(this_size);
4413 
4414  for(size_t i = 0; i < this_size; i++)
4415  {
4416  try
4417  {
4418  int_vector[i] = stoi((*this)[i]);
4419  }
4420  catch(const logic_error&)
4421  {
4422  int_vector[i] = -999999999;
4423  }
4424  }
4425 
4426  return int_vector;
4427 }
4428 
4429 
4431 
4432 template <class T>
4434 {
4435  const size_t this_size = this->size();
4436 
4437  Vector<size_t> size_t_vector(this_size);
4438 
4439  for(size_t i = 0; i < this_size; i++)
4440  {
4441  try
4442  {
4443  size_t_vector[i] = static_cast<size_t>(stoi((*this)[i]));
4444  }
4445  catch(const logic_error&)
4446  {
4447  size_t_vector[i] = 999999;
4448  }
4449  }
4450 
4451  return size_t_vector;
4452 }
4453 
4454 
4456 
4457 template <class T>
4459 {
4460  const size_t this_size = this->size();
4461 
4462  Vector<time_t> time_vector(this_size);
4463 
4464  for(size_t i = 0; i < this_size; i++)
4465  {
4466  try
4467  {
4468  time_vector[i] = static_cast<time_t>(stoi((*this)[i]));
4469  }
4470  catch(const logic_error&)
4471  {
4472  time_vector[i] = -1;
4473  }
4474  }
4475 
4476  return time_vector;
4477 }
4478 
4482 
4483 template <class T>
4484 Vector<Vector<T>> Vector<T>::split(const size_t& n) const
4485 {
4486  if(this->size() < n)
4487  {
4488  return Vector<Vector<T>>({*this});
4489  }
4490 
4491  // determine number of sub-vectors of size n
4492 
4493  // const size_t batches_number = (this->size() - 1) / n + 1;
4494  const size_t batches_number = this->size() / n;
4495 
4496  // create array of vectors to store the sub-vectors
4497 
4498  Vector<Vector<T>> batches(batches_number);
4499 
4500  // each iteration of this loop process next set of n elements
4501  // and store it in a vector at k'th index in vec
4502 
4503  for(size_t k = 0; k < batches_number; ++k)
4504  {
4505  // get range for next set of n elements
4506 
4507  auto start_itr = next(this->cbegin(), k*n);
4508 
4509  auto end_itr = k*n + n > this->size() ? this->cend() : next(this->cbegin(), k*n + n);
4510 
4511  // allocate memory for the sub-vector
4512 
4513  batches[k].resize(n);
4514 
4515  // code to handle the last sub-vector as it might
4516  // contain less elements
4517 
4518 // if(k*n + n > this->size())
4519 // {
4520 // batches[k].resize(this->size() - k*n);
4521 // }
4522 
4523  // copy elements from the input range to the sub-vector
4524 
4525  copy(start_itr, end_itr, batches[k].begin());
4526  }
4527 
4528  return batches;
4529 }
4530 
4531 
4534 
4535 template <class T>
4537 {
4538 
4539  const size_t this_size = this->size();
4540 
4541  Matrix<T> matrix(1, this_size);
4542 
4543  for(size_t i = 0; i < this_size; i++)
4544  {
4545  matrix(0, i) = (*this)[i];
4546  }
4547 
4548  return matrix;
4549 }
4550 
4551 
4554 
4555 template <class T>
4557 {
4558  const size_t this_size = this->size();
4559 
4560  Matrix<T> matrix(this_size, 1);
4561 
4562  for(size_t i = 0; i < this_size; i++) {
4563  matrix(i, 0) = (*this)[i];
4564  }
4565 
4566  return matrix;
4567 }
4568 
4569 
4573 
4574 template <class T>
4575 void Vector<T>::parse(const string &str)
4576 {
4577  if(str.empty())
4578  {
4579  set();
4580  }
4581  else
4582  {
4583  istringstream buffer(str);
4584 
4585  istream_iterator<string> first(buffer);
4586  istream_iterator<string> last;
4587 
4588  Vector<string> str_vector(first, last);
4589 
4590  const size_t new_size = str_vector.size();
4591 
4592  if(new_size > 0)
4593  {
4594  this->resize(new_size);
4595 
4596  buffer.clear();
4597  buffer.seekg(0, ios::beg);
4598 
4599  for(size_t i = 0; i < new_size; i++)
4600  {
4601  buffer >>(*this)[i];
4602  }
4603  }
4604 
4605  }
4606 }
4607 
4608 
4612 
4613 template <class T>
4614 string Vector<T>::vector_to_string(const char& separator, const char& quotation) const
4615 {
4616  ostringstream buffer;
4617 
4618  const size_t this_size = this->size();
4619 
4620  if(this_size > 0)
4621  {
4622 
4623  buffer << quotation <<(*this)[0] << quotation;
4624 
4625  for(size_t i = 1; i < this_size; i++)
4626  {
4627 
4628  buffer << separator << quotation << (*this)[i] << quotation;
4629  }
4630  }
4631 
4632  return buffer.str();
4633 }
4634 
4635 
4638 
4639 template <class T>
4640 string Vector<T>::vector_to_string(const char& separator) const
4641 {
4642  ostringstream buffer;
4643 
4644  const size_t this_size = this->size();
4645 
4646  if(this_size > 0)
4647  {
4648  buffer <<(*this)[0];
4649 
4650  for(size_t i = 1; i < this_size; i++)
4651  {
4652  buffer << separator << (*this)[i];
4653  }
4654  }
4655 
4656  return buffer.str();
4657 }
4658 
4659 
4661 
4662 template <class T>
4664 {
4665  ostringstream buffer;
4666 
4667  const size_t this_size = this->size();
4668 
4669  if(this_size > 0)
4670  {
4671  buffer <<(*this)[0];
4672 
4673  for(size_t i = 1; i < this_size; i++)
4674  {
4675  buffer << ' ' << (*this)[i];
4676  }
4677  }
4678 
4679  return buffer.str();
4680 }
4681 
4682 
4684 
4685 template <class T>
4687 {
4688  ostringstream buffer;
4689 
4690  const size_t this_size = this->size();
4691 
4692  if(this_size > 0)
4693  {
4694  buffer <<(*this)[0];
4695 
4696  for(size_t i = 1 ; i < this_size ; i++)
4697  {
4698  buffer << (*this)[i];
4699  }
4700  }
4701 
4702  return buffer.str();
4703 }
4704 
4705 
4707 
4708 template <class T>
4709 string Vector<T>::to_text(const char& separator) const
4710 {
4711  ostringstream buffer;
4712 
4713  const size_t this_size = this->size();
4714 
4715  if(this_size > 0)
4716  {
4717  buffer <<(*this)[0];
4718 
4719  for(size_t i = 1; i < this_size; i++)
4720  {
4721  buffer << separator << (*this)[i];
4722  }
4723  }
4724 
4725  return buffer.str();
4726 }
4727 
4729 
4730 template <class T>
4731 string Vector<T>::to_text(const string& separator) const
4732 {
4733  ostringstream buffer;
4734 
4735  const size_t this_size = this->size();
4736 
4737  if(this_size > 0)
4738  {
4739  buffer <<(*this)[0];
4740 
4741  for(size_t i = 1; i < this_size; i++)
4742  {
4743  buffer << separator << (*this)[i];
4744  }
4745  }
4746 
4747  return buffer.str();
4748 }
4749 
4750 
4754 
4755 template <class T>
4756 Vector<string> Vector<T>::write_string_vector(const size_t &precision) const
4757 {
4758  const size_t this_size = this->size();
4759 
4760  Vector<string> string_vector(this_size);
4761 
4762  ostringstream buffer;
4763 
4764  for(size_t i = 0; i < this_size; i++)
4765  {
4766  buffer.str("");
4767  buffer << setprecision(precision) << (*this)[i];
4768 
4769  string_vector[i] = buffer.str();
4770  }
4771 
4772  return(string_vector);
4773 }
4774 
4775 
4782 
4783 template <class T>
4784 Matrix<T> Vector<T>::to_matrix(const size_t &rows_number,
4785  const size_t &columns_number) const
4786 {
4787 
4788 
4789 #ifdef __OPENNN_DEBUG__
4790 
4791  const size_t this_size = this->size();
4792 
4793  if(rows_number * columns_number != this_size) {
4794  ostringstream buffer;
4795 
4796  buffer << "OpenNN Exception: Vector Template.\n"
4797  << "Matrix<T> to_matrix(const size_t&, const size_t&) method.\n"
4798  << "The number of rows(" << rows_number
4799  << ") times the number of colums(" << columns_number
4800  << ") must be equal to the size of the vector(" << this_size
4801  << ").\n";
4802 
4803  throw logic_error(buffer.str());
4804  }
4805 
4806 #endif
4807 
4808  Matrix<T> matrix(rows_number, columns_number);
4809 
4810  for(size_t i = 0; i < this->size(); i++)
4811  {
4812  matrix[i] = (*this)[i];
4813  }
4814 
4815  return matrix;
4816 }
4817 
4818 
4821 
4822 template <class T>
4824 {
4825 // Tensor<T> tensor({dimensions});
4826  Tensor<T> tensor(dimensions); // for linux
4827 
4828  for(size_t i = 0; i < this->size(); i++)
4829  {
4830  tensor[i] = (*this)[i];
4831  }
4832 
4833  return tensor;
4834 }
4835 
4836 
4840 
4841 template <class T>
4842 istream &operator>>(istream &is, Vector<T>&v)
4843 {
4844  const size_t size = v.size();
4845 
4846  for(size_t i = 0; i < size; i++)
4847  {
4848  is >> v[i];
4849  }
4850 
4851  return is;
4852 }
4853 
4854 
4858 
4859 template <class T>
4860 ostream &operator<<(ostream &os, const Vector<T>&v)
4861 {
4862  const size_t this_size = v.size();
4863 
4864  if(this_size > 0)
4865  {
4866  os << v[0];
4867 
4868  const char space = ' ';
4869 
4870  for(size_t i = 1; i < this_size; i++)
4871  {
4872  os << space << v[i];
4873  }
4874  }
4875 
4876  return os;
4877 }
4878 
4879 
4883 
4884 template <class T>
4885 ostream &operator<<(ostream &os, const Vector<Vector<T>>&v)
4886 {
4887  for(size_t i = 0; i < v.size(); i++)
4888  {
4889  os << "subvector_" << i << "\n" << v[i] << endl;
4890  }
4891 
4892  return os;
4893 }
4894 
4895 
4899 
4900 template <class T>
4901 ostream &operator<<(ostream &os, const Vector< Matrix<T> >&v)
4902 {
4903  for(size_t i = 0; i < v.size(); i++)
4904  {
4905  os << "submatrix_" << i << "\n" << v[i] << endl;
4906  }
4907 
4908  return os;
4909 }
4910 
4911 
4915 
4916 template <class T>
4917 T calculate_random_uniform(const T&minimum, const T&maximum)
4918 {
4919  const T random = static_cast<T>(rand() /(RAND_MAX + 1.0));
4920 
4921  const T random_uniform = minimum + (maximum - minimum) * random;
4922 
4923  return(random_uniform);
4924 }
4925 
4926 
4928 
4929 template <class T>
4930 string number_to_string(const T& value)
4931 {
4932  ostringstream ss;
4933 
4934  ss << value;
4935 
4936  return(ss.str());
4937 }
4938 
4939 
4943 
4944 template <class T>
4945 T calculate_random_normal(const T& mean, const T& standard_deviation)
4946 {
4947  const double pi = 4.0 * atan(1.0);
4948 
4949  T random_uniform_1;
4950 
4951  do
4952  {
4953  random_uniform_1 = static_cast<T>(rand()) /(RAND_MAX + 1.0);
4954 
4955  }
4956  while(random_uniform_1 == 0.0);
4957 
4958  const T random_uniform_2 = static_cast<T>(rand()) /(RAND_MAX + 1.0);
4959 
4960  // Box-Muller transformation
4961 
4962  const T random_normal = mean +
4963  sqrt(-2.0 * log(random_uniform_1)) *
4964  sin(2.0 * pi * random_uniform_2) *
4965  standard_deviation;
4966 
4967  return(random_normal);
4968 }
4969 
4970 
4974 
4975 template <class T>
4976 string write_elapsed_time(const T& elapsed_time)
4977 {
4978  string elapsed_time_string;
4979 
4980  const size_t hours = static_cast<size_t>(elapsed_time/3600);
4981 
4982  size_t minutes = static_cast<size_t>(elapsed_time) - hours*3600;
4983 
4984  minutes = static_cast<size_t>(minutes/60);
4985 
4986  const size_t seconds = static_cast<size_t>(elapsed_time) - hours*3600 - minutes*60;
4987 
4988  if(hours != 0)
4989  {
4990  elapsed_time_string = to_string(hours) + ":";
4991  }
4992 
4993  if(minutes < 10)
4994  {
4995  elapsed_time_string += "0";
4996  }
4997 
4998  elapsed_time_string += to_string(minutes) + ":";
4999 
5000  if(seconds < 10)
5001  {
5002  elapsed_time_string += "0";
5003  }
5004  elapsed_time_string += to_string(seconds);
5005 
5006  return elapsed_time_string;
5007 }
5008 
5009 
5012 
5013 template <class T>
5014 Vector<T> to_vector(const Vector< Matrix<T> >& array)
5015 {
5016 
5017  Vector<T> new_vector;
5018 
5019  for(size_t i = 0; i < array.size(); i++)
5020  {
5021  const Vector<T> other_vector = array[i].to_vector();
5022  new_vector.insert(new_vector.end(), other_vector.begin(), other_vector.end());
5023  }
5024 
5025  return new_vector;
5026 }
5027 
5028 
5033 
5034 template <class T>
5036  const size_t& columns_number,
5037  const size_t& channels_number) const
5038 {
5039  Vector< Matrix<T> > new_vector_matrix(channels_number, Matrix<T>(rows_number, columns_number, 0.0));
5040  const size_t vector_size = this->size();
5041  Vector<Vector<T>> channels_vector = this->split(vector_size / channels_number);
5042  for(size_t i = 0; i < channels_number; i++)
5043  {
5044  new_vector_matrix[i] = channels_vector[i].to_matrix(rows_number, columns_number);
5045  }
5046 
5047  return new_vector_matrix;
5048 }
5049 
5050 } // end namespace OpenNN
5051 
5052 #endif
5053 
5054 // OpenNN: Open Neural Networks Library.
5055 // Copyright(C) 2005-2019 Artificial Intelligence Techniques, SL.
5056 //
5057 // This library is free software; you can redistribute it and/or
5058 // modify it under the terms of the GNU Lesser General Public
5059 // License as published by the Free Software Foundation; either
5060 // version 2.1 of the License, or any later version.
5061 //
5062 // This library is distributed in the hope that it will be useful,
5063 // but WITHOUT ANY WARRANTY; without even the implied warranty of
5064 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5065 // Lesser General Public License for more details.
5066 
5067 // You should have received a copy of the GNU Lesser General Public
5068 // License along with this library; if not, write to the Free Software
5069 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
OpenNN::Matrix::set_column
void set_column(const size_t &, const Vector< T > &, const string &="")
Definition: matrix.h:3133
OpenNN::Matrix::sort_descending_strings
Matrix< T > sort_descending_strings(const size_t &) const
Definition: matrix.h:5238
OpenNN::Matrix::get_rows_number
const size_t & get_rows_number() const
Returns the number of rows in the matrix.
Definition: matrix.h:1287
OpenNN::Vector::get_first
const T & get_first() const
Return the first element of the vector.
Definition: vector.h:762
OpenNN::Vector::to_string_vector
Vector< string > to_string_vector() const
Returns a new vector with the elements of this vector converted to string.
Definition: vector.h:4357
OpenNN::Matrix::sort_descending
Matrix< T > sort_descending(const size_t &) const
Definition: matrix.h:5386
OpenNN::Vector::initialize_sequential
void initialize_sequential()
Initializes all the elements of the vector in a sequential order.
Definition: vector.h:834
OpenNN::Matrix
This template class defines a matrix for general purpose use.
Definition: matrix.h:42
OpenNN::Vector::get_indices_equal_to
Vector< size_t > get_indices_equal_to(const T &) const
Definition: vector.h:1978
OpenNN::Tensor
This template class defines a tensor for general purpose use.
Definition: tensor.h:37
OpenNN::Vector::trim
void trim()
Removes whitespaces from the start and the end of each element in this vector of strings.
Definition: vector.h:1067
OpenNN::Vector::get_subvector
Vector< T > get_subvector(const size_t &, const size_t &) const
Definition: vector.h:3240
OpenNN::Vector::set
void set()
Sets the size of this vector to zero.
Definition: vector.h:682
OpenNN::Vector::contains
bool contains(const T &) const
Definition: vector.h:1123
OpenNN::Vector::get_reverse
Vector< T > get_reverse() const
Definition: vector.h:1479
OpenNN::Matrix::get_columns_number
const size_t & get_columns_number() const
Returns the number of columns in the matrix.
Definition: matrix.h:1296
OpenNN::Vector
This template represents an array of any kind of numbers or objects.
Definition: vector.h:54
OpenNN::Vector::to_double_vector
Vector< double > to_double_vector() const
Returns a new vector with the elements of this vector casted to double.
Definition: vector.h:4214
OpenNN::Matrix::get_column
Vector< T > get_column(const size_t &) const
Definition: matrix.h:2663
OpenNN::Vector::to_matrix
Matrix< T > to_matrix(const size_t &, const size_t &) const
Definition: vector.h:4784
OpenNN::Matrix::set_diagonal
void set_diagonal(const T &)
Definition: matrix.h:3241
OpenNN::Vector::delete_value
Vector< T > delete_value(const T &) const
Definition: vector.h:3770