OpenNN  2.2
Open Neural Networks Library
matrix.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* M A T R I X C O N T A I N E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __MATRIX_H__
15 #define __MATRIX_H__
16 
17 // System includes
18 
19 #include <cmath>
20 #include <cstdlib>
21 #include <fstream>
22 #include <iomanip>
23 #include <iostream>
24 #include <sstream>
25 #include <stdexcept>
26 
27 // OpenNN includes
28 
29 #include "vector.h"
30 
31 namespace OpenNN
32 {
33 
36 
37 template <class T>
38 class Matrix : public std::vector<T>
39 {
40 
41 public:
42 
43  // CONSTRUCTORS
44 
45  explicit Matrix(void);
46 
47  explicit Matrix(const size_t&, const size_t&);
48 
49  explicit Matrix(const size_t&, const size_t&, const T&);
50 
51  explicit Matrix(const std::string&);
52 
53  Matrix(const Matrix&);
54 
55  // DESTRUCTOR
56 
57  virtual ~Matrix(void);
58 
59  // ASSIGNMENT OPERATORS
60 
61  inline Matrix<T>& operator = (const Matrix<T>&);
62 
63  // REFERENCE OPERATORS
64 
65  inline T& operator () (const size_t&, const size_t&);
66 
67  inline const T& operator () (const size_t&, const size_t&) const;
68 
69  bool operator == (const Matrix<T>&) const;
70 
71  bool operator == (const T&) const;
72 
73  bool operator != (const Matrix<T>&) const;
74 
75  bool operator != (const T& value) const;
76 
77  bool operator > (const Matrix<T>&) const;
78 
79  bool operator > (const T& value) const;
80 
81  bool operator < (const Matrix<T>&) const;
82 
83  bool operator < (const T& value) const;
84 
85  bool operator >= (const Matrix<T>&) const;
86 
87  bool operator >= (const T&) const;
88 
89  bool operator <= (const Matrix<T>&) const;
90 
91  bool operator <= (const T&) const;
92 
93  // METHODS
94 
95  // Get methods
96 
97  const size_t& get_rows_number(void) const;
98 
99  const size_t& get_columns_number(void) const;
100 
101  // Set methods
102 
103 
104  void set(void);
105 
106  void set(const size_t&, const size_t&);
107 
108  void set(const size_t&, const size_t&, const T&);
109 
110 
111  void set(const Matrix<T>&);
112 
113  void set(const std::string&);
114 
115  void set_identity(const size_t&);
116 
117  void set_rows_number(const size_t&);
118 
119  void set_columns_number(const size_t&);
120 
121  void tuck_in(const size_t&, const size_t&, const Matrix<T>&);
122 
123  size_t count_diagonal_elements(void) const;
124 
125  size_t count_off_diagonal_elements(void) const;
126 
128 
130 
132 
133  Vector<T> arrange_row(const size_t&) const;
134 
135  Vector<T> arrange_row(const size_t&, const Vector<size_t>&) const;
136 
137  Vector<T> arrange_column(const size_t&) const;
138 
139  Vector<T> arrange_column(const size_t&, const Vector<size_t>&) const;
140 
141  Vector<T> get_diagonal(void) const;
142 
143  void set_row(const size_t&, const Vector<T>&);
144 
145  void set_row(const size_t&, const T&);
146 
147  void set_column(const size_t&, const Vector<T>&);
148 
149  void set_column(const size_t&, const T&);
150 
151  void set_diagonal(const T&);
152 
153  void set_diagonal(const Vector<T>&);
154 
155  void initialize_diagonal(const size_t&, const T&);
156 
157  void initialize_diagonal(const size_t&, const Vector<T>&);
158 
159  Matrix<T> sum_diagonal(const T&) const;
160 
161  Matrix<T> sum_diagonal(const Vector<T>&) const;
162 
163  void append_row(const Vector<T>&);
164 
165  void append_column(const Vector<T>&) ;
166 
167  void insert_row(const size_t&, const Vector<T>&);
168 
169  void insert_column(const size_t&, const Vector<T>&);
170 
171  void subtract_row(const size_t&);
172 
173  void subtract_column(const size_t&);
174 
175  Matrix<T> assemble_rows(const Matrix<T>&) const;
176 
177  Matrix<T> assemble_columns(const Matrix<T>&) const;
178 
179  void sort_less(const size_t&);
180  void sort_greater(const size_t&);
181 
182  void initialize(const T&);
183 
184  void randomize_uniform(const double& = -1.0, const double& = 1.0);
185  void randomize_uniform(const Matrix<double>&, const Matrix<double>&);
186 
187  void randomize_normal(const double& = 0.0, const double& = 1.0);
188  void randomize_normal(const Matrix<double>&, const Matrix<double>&);
189 
190  void initialize_identity(void);
191 
192  void initialize_diagonal(const T&);
193 
194  // Mathematical methods
195 
196  T calculate_sum(void) const;
197 
198  Vector<T> calculate_rows_sum(void) const;
199 
200  void sum_row(const size_t&, const Vector<T>&);
201 
202 
203  double calculate_trace(void) const;
204 
205  Vector<double> calculate_mean(void) const;
206  double calculate_mean(const size_t&) const;
207 
209 
211 
214 
216 
218 
220 
221  T calculate_minimum(void) const;
222 
223  T calculate_maximum(void) const;
224 
226 
228 
230 
232 
234 
236 
238 
240 
242 
244 
245  Vector< Histogram<T> > calculate_histograms(const size_t& = 10) const;
246 
247  Vector< Histogram<T> > calculate_histograms_missing_values(const Vector< Vector<size_t> >&, const size_t& = 10) const;
248 
250 
252 
254 
256 
258 
260 
262 
264 
266 
268 
270 
272 
274 
276 
278 
280 
282 
284 
286 
287  double calculate_sum_squared_error(const Matrix<double>&) const;
288 
289  double calculate_sum_squared_error(const Vector<double>&) const;
290 
292 
294 
295  Matrix<T> calculate_transpose(void) const;
296 
297  T calculate_determinant(void) const;
298 
299  Matrix<T> calculate_cofactor(void) const;
300 
301  Matrix<T> calculate_inverse(void) const;
302 
303  Matrix<T> operator + (const T&) const;
304 
305  Matrix<T> operator + (const Vector<T>&) const;
306 
307  Matrix<T> operator + (const Matrix<T>&) const;
308 
309  Matrix<T> operator - (const T& scalar) const;
310 
311  Matrix<T> operator - (const Vector<T>&) const;
312 
313  Matrix<T> operator - (const Matrix<T>&) const;
314 
315  Matrix<T> operator * (const T&) const;
316 
317  Matrix<T> operator * (const Vector<T>&) const;
318 
319  Matrix<T> operator * (const Matrix<T>&) const;
320 
321  Matrix<T> operator / (const T&) const;
322 
323  Matrix<T> operator / (const Vector<T>&) const;
324 
325  Matrix<T> operator / (const Matrix<T>&) const;
326 
327  void operator += (const T& value);
328 
329  void operator += (const Matrix<T>& other_matrix);
330 
331  void operator -= (const T&);
332 
333  void operator -= (const Matrix<T>&);
334 
335  void operator *= (const T&);
336 
337  void operator *= (const Matrix<T>&);
338 
339  void operator /= (const T&);
340 
341  void operator /= (const Matrix<T>&);
342 
343 // void sum_diagonal(const T&);
344 
345  Vector<double> dot(const Vector<double>&) const;
346 
347  Matrix<double> dot(const Matrix<double>&) const;
348 
349  Matrix<T> direct(const Matrix<T>&) const;
350 
351  bool empty(void) const;
352 
353  bool is_square(void) const;
354 
355  bool is_symmetric(void) const;
356 
357  bool is_antisymmetric(void) const;
358 
359  bool is_diagonal(void) const;
360 
361  bool is_scalar(void) const;
362 
363  bool is_identity(void) const;
364 
365  Matrix<T> filter(const size_t&, const T&, const T&) const;
366 
367  void convert_time_series(const size_t&);
368  void convert_autoassociation(void);
369 
370  void convert_angular_variables_degrees(const size_t&);
371 
372  void convert_angular_variables_radians(const size_t&);
373 
374  // Serialization methods
375 
376  void print(void) const;
377 
378  void load(const std::string&);
379 
380  void save(const std::string&) const;
381 
382  void save_csv(const std::string&) const;
383 
384  void parse(const std::string&);
385 
386  std::string to_string(const std::string& = " ") const;
387 
388  Matrix<std::string> write_string_matrix(const size_t& = 3) const;
389 
390  std::vector<T> to_std_vector(void) const;
391 
392  Vector<T> to_vector(void) const;
393 
394  void print_preview(void) const;
395 
396 private:
397 
399 
400  size_t rows_number;
401 
403 
405 };
406 
407 
408 // CONSTRUCTORS
409 
411 
412 template <class T>
413 Matrix<T>::Matrix(void) : std::vector<T>()
414 {
415  rows_number = 0;
416  columns_number = 0;
417 }
418 
419 
423 
424 template <class T>
425 Matrix<T>::Matrix(const size_t& new_rows_number, const size_t& new_columns_number) : std::vector<T>(new_rows_number*new_columns_number)
426 {
427  if(new_rows_number == 0 && new_columns_number == 0)
428  {
429  rows_number = 0;
430  columns_number = 0;
431  }
432  else if(new_rows_number == 0)
433  {
434  std::ostringstream buffer;
435 
436  buffer << "OpenNN Exception: Matrix Template.\n"
437  << "Constructor Matrix(const size_t&, const size_t&).\n"
438  << "Number of rows must be greater than zero.\n";
439 
440  throw std::logic_error(buffer.str());
441  }
442  else if(new_columns_number == 0)
443  {
444  std::ostringstream buffer;
445 
446  buffer << "OpenNN Exception: Matrix Template.\n"
447  << "Constructor Matrix(const size_t&, const size_t&).\n"
448  << "Number of columns must be greater than zero.\n";
449 
450  throw std::logic_error(buffer.str());
451  }
452  else
453  {
454  rows_number = new_rows_number;
455  columns_number = new_columns_number;
456  }
457 }
458 
459 
464 
465 template <class T>
466 Matrix<T>::Matrix(const size_t& new_rows_number, const size_t& new_columns_number, const T& value) : std::vector<T>(new_rows_number*new_columns_number)
467 {
468  if(new_rows_number == 0 && new_columns_number == 0)
469  {
470  rows_number = 0;
471  columns_number = 0;
472  }
473  else if(new_rows_number == 0)
474  {
475  std::ostringstream buffer;
476 
477  buffer << "OpenNN Exception: Matrix Template.\n"
478  << "Constructor Matrix(const size_t&, const size_t&, const T&).\n"
479  << "Number of rows must be greater than zero.\n";
480 
481  throw std::logic_error(buffer.str());
482  }
483  else if(new_columns_number == 0)
484  {
485  std::ostringstream buffer;
486 
487  buffer << "OpenNN Exception: Matrix Template.\n"
488  << "Constructor Matrix(const size_t&, const size_t&, const T&).\n"
489  << "Number of columns must be greater than zero.\n";
490 
491  throw std::logic_error(buffer.str());
492  }
493  else
494  {
495  // Set sizes
496 
497  rows_number = new_rows_number;
498  columns_number = new_columns_number;
499 
500  (*this).initialize(value);
501  }
502 }
503 
504 
507 
508 template <class T>
509 Matrix<T>::Matrix(const std::string& file_name) : std::vector<T>()
510 {
511  rows_number = 0;
512  columns_number = 0;
513 
514  load(file_name);
515 }
516 
517 
520 
521 template <class T>
522 Matrix<T>::Matrix(const Matrix& other_matrix) : std::vector<T>(other_matrix.begin(), other_matrix.end())
523 {
524  rows_number = other_matrix.rows_number;
525  columns_number = other_matrix.columns_number;
526 }
527 
528 
529 // DESTRUCTOR
530 
532 
533 template <class T>
535 {
536 }
537 
538 
539 // ASSIGNMENT OPERATORS
540 
543 
544 template <class T>
546 {
547  if(other_matrix.rows_number != rows_number || other_matrix.columns_number != columns_number)
548  {
549  rows_number = other_matrix.rows_number;
550  columns_number = other_matrix.columns_number;
551 
552  this->clear();
553 
554  this->resize(rows_number*columns_number);
555  }
556 
557  std::copy(other_matrix.begin(), other_matrix.end(), (*this).begin());
558 
559  return(*this);
560 }
561 
562 
563 // REFERENCE OPERATORS
564 
566 
570 
571 template <class T>
572 inline T& Matrix<T>::operator () (const size_t& row, const size_t& column)
573 {
574  // Control sentence (if debug)
575 
576  #ifndef NDEBUG
577 
578  if(row >= rows_number)
579  {
580  std::ostringstream buffer;
581 
582  buffer << "OpenNN Exception: Matrix Template.\n"
583  << "operator () (const size_t&, const size_t&).\n"
584  << "Row index (" << row << ") must be less than number of rows (" << rows_number << ").\n";
585 
586  throw std::logic_error(buffer.str());
587  }
588  else if(column >= columns_number)
589  {
590  std::ostringstream buffer;
591 
592  buffer << "OpenNN Exception: Matrix Template.\n"
593  << "operator () (const size_t&, const size_t&).\n"
594  << "Column index (" << column << ") must be less than number of columns (" << columns_number << ").\n";
595 
596  throw std::logic_error(buffer.str());
597  }
598 
599  #endif
600 
601  // Return matrix element
602 
603  return((*this)[rows_number*column+row]);
604 }
605 
606 
608 
612 
613 template <class T>
614 inline const T& Matrix<T>::operator () (const size_t& row, const size_t& column) const
615 {
616  // Control sentence (if debug)
617 
618  #ifndef NDEBUG
619 
620  if(row >= rows_number)
621  {
622  std::ostringstream buffer;
623 
624  buffer << "OpenNN Exception: Matrix Template.\n"
625  << "operator () (const size_t&, const size_t&).\n"
626  << "Row index (" << row << ") must be less than number of rows (" << rows_number << ").\n";
627 
628  throw std::logic_error(buffer.str());
629  }
630  else if(column >= columns_number)
631  {
632  std::ostringstream buffer;
633 
634  buffer << "OpenNN Exception: Matrix Template.\n"
635  << "operator () (const size_t&, const size_t&).\n"
636  << "Column index (" << column << ") must be less than number of columns (" << columns_number << ").\n";
637 
638  throw std::logic_error(buffer.str());
639  }
640 
641  #endif
642 
643  // Return matrix element
644 
645  return((*this)[rows_number*column+row]);
646 
647 }
648 
649 
650 // bool operator == (const Matrix<T>&) const
651 
655 
656 template <class T>
657 bool Matrix<T>::operator == (const Matrix<T>& other_matrix) const
658 {
659  const size_t other_rows_number = other_matrix.get_rows_number();
660  const size_t other_columns_number = other_matrix.get_columns_number();
661 
662  if(other_rows_number != rows_number)
663  {
664  return(false);
665  }
666  else if(other_columns_number != columns_number)
667  {
668  return(false);
669  }
670  else
671  {
672  for(size_t i = 0; i < this->size(); i++)
673  {
674  if((*this)[i] != other_matrix[i])
675  {
676  return(false);
677  }
678  }
679  }
680 
681  return(true);
682 }
683 
684 
685 // bool operator == (const T&)
686 
690 
691 template <class T>
692 bool Matrix<T>::operator == (const T& value) const
693 {
694  for(size_t i = 0; i < this->size(); i++)
695  {
696  if((*this)[i] != value)
697  {
698  return(false);
699  }
700  }
701 
702  return(true);
703 }
704 
705 
706 // bool operator != (const Matrix<T>&)
707 
711 
712 template <class T>
713 bool Matrix<T>::operator != (const Matrix<T>& other_matrix) const
714 {
715  // Control sentence (if debug)
716 
717  #ifndef NDEBUG
718 
719  const size_t other_rows_number = other_matrix.get_rows_number();
720  const size_t other_columns_number = other_matrix.get_columns_number();
721 
722  if(other_rows_number != rows_number)
723  {
724  std::ostringstream buffer;
725 
726  buffer << "OpenNN Exception: Matrix Template.\n"
727  << "bool operator != (const Matrix<T>&) const.\n"
728  << "Both numbers of rows must be the same.\n";
729 
730  throw std::logic_error(buffer.str());
731  }
732  else if(other_columns_number != columns_number)
733  {
734  std::ostringstream buffer;
735 
736  buffer << "OpenNN Exception: Matrix Template.\n"
737  << "bool operator != (const Matrix<T>&) const.\n"
738  << "Both numbers of columns must be the same.\n";
739 
740  throw std::logic_error(buffer.str());
741  }
742 
743  #endif
744 
745  for(size_t i = 0; i < this->size(); i++)
746  {
747  if((*this)[i] != other_matrix[i])
748  {
749  return(true);
750  }
751  }
752 
753  return(false);
754 }
755 
756 
757 // bool operator != (const T&) const
758 
762 
763 template <class T>
764 bool Matrix<T>::operator != (const T& value) const
765 {
766  // Control sentence (if debug)
767 
768  for(size_t i = 0; i < this->size(); i++)
769  {
770  if((*this)[i] != value)
771  {
772  return(true);
773  }
774  }
775 
776  return(false);
777 }
778 
779 
780 // bool operator > (const Matrix<T>&) const
781 
786 
787 template <class T>
788 bool Matrix<T>::operator > (const Matrix<T>& other_matrix) const
789 {
790  // Control sentence (if debug)
791 
792  #ifndef NDEBUG
793 
794  const size_t other_rows_number = other_matrix.get_rows_number();
795  const size_t other_columns_number = other_matrix.get_columns_number();
796 
797  if(other_rows_number != rows_number)
798  {
799  std::ostringstream buffer;
800 
801  buffer << "OpenNN Exception: Matrix Template.\n"
802  << "bool operator > (const Matrix<T>&) const.\n"
803  << "Both numbers of rows must be the same.\n";
804 
805  throw std::logic_error(buffer.str());
806  }
807  else if(other_columns_number != columns_number)
808  {
809  std::ostringstream buffer;
810 
811  buffer << "OpenNN Exception: Matrix Template.\n"
812  << "bool operator > (const Matrix<T>&) const.\n"
813  << "Both numbers of columns must be the same.\n";
814 
815  throw std::logic_error(buffer.str());
816  }
817 
818  #endif
819 
820  for(size_t i = 0; i < this->size(); i++)
821  {
822  if((*this)[i] <= other_matrix[i])
823  {
824  return(false);
825  }
826  }
827 
828  return(true);
829 }
830 
831 
832 // bool operator > (const T&) const
833 
837 
838 template <class T>
839 bool Matrix<T>::operator > (const T& value) const
840 {
841  for(size_t i = 0; i < this->size(); i++)
842  {
843  if((*this)[i] <= value)
844  {
845  return(false);
846  }
847  }
848 
849  return(true);
850 }
851 
852 
853 // bool operator < (const Matrix<T>&) const
854 
859 
860 template <class T>
861 bool Matrix<T>::operator < (const Matrix<T>& other_matrix) const
862 {
863  // Control sentence (if debug)
864 
865  #ifndef NDEBUG
866 
867  const size_t other_rows_number = other_matrix.get_rows_number();
868  const size_t other_columns_number = other_matrix.get_columns_number();
869 
870  if(other_rows_number != rows_number)
871  {
872  std::ostringstream buffer;
873 
874  buffer << "OpenNN Exception: Matrix Template.\n"
875  << "bool operator < (const Matrix<T>&) const.\n"
876  << "Both numbers of rows must be the same.\n";
877 
878  throw std::logic_error(buffer.str());
879  }
880  else if(other_columns_number != columns_number)
881  {
882  std::ostringstream buffer;
883 
884  buffer << "OpenNN Exception: Matrix Template.\n"
885  << "bool operator < (const Matrix<T>&) const.\n"
886  << "Both numbers of columns must be the same.\n";
887 
888  throw std::logic_error(buffer.str());
889  }
890 
891  #endif
892 
893  for(size_t i = 0; i < this->size(); i++)
894  {
895  if((*this)[i] >= other_matrix[i])
896  {
897  return(false);
898  }
899  }
900 
901  return(true);
902 }
903 
904 
905 // bool operator < (const T&) const
906 
910 
911 template <class T>
912 bool Matrix<T>::operator < (const T& value) const
913 {
914  for(size_t i = 0; i < this->size(); i++)
915  {
916  if((*this)[i] >= value)
917  {
918  return(false);
919  }
920  }
921 
922  return(true);
923 }
924 
925 
926 // bool operator >= (const Matrix<T>&) const
927 
932 
933 template <class T>
934 bool Matrix<T>::operator >= (const Matrix<T>& other_matrix) const
935 {
936  // Control sentence (if debug)
937 
938  #ifndef NDEBUG
939 
940  const size_t other_rows_number = other_matrix.get_rows_number();
941  const size_t other_columns_number = other_matrix.get_columns_number();
942 
943  if(other_rows_number != rows_number)
944  {
945  std::ostringstream buffer;
946 
947  buffer << "OpenNN Exception: Matrix Template.\n"
948  << "bool operator >= (const Matrix<T>&) const.\n"
949  << "Both numbers of rows must be the same.\n";
950 
951  throw std::logic_error(buffer.str());
952  }
953  else if(other_columns_number != columns_number)
954  {
955  std::ostringstream buffer;
956 
957  buffer << "OpenNN Exception: Matrix Template.\n"
958  << "bool operator >= (const Matrix<T>&) const.\n"
959  << "Both numbers of columns must be the same.\n";
960 
961  throw std::logic_error(buffer.str());
962  }
963 
964  #endif
965 
966  for(size_t i = 0; i < this->size(); i++)
967  {
968  if((*this)[i] < other_matrix[i])
969  {
970  return(false);
971  }
972  }
973 
974  return(true);
975 }
976 
977 
978 // bool operator >= (const T&) const
979 
983 
984 template <class T>
985 bool Matrix<T>::operator >= (const T& value) const
986 {
987  for(size_t i = 0; i < this->size(); i++)
988  {
989  if((*this)[i] < value)
990  {
991  return(false);
992  }
993  }
994 
995  return(true);
996 }
997 
998 
999 // bool operator <= (const Matrix<T>&) const
1000 
1005 
1006 template <class T>
1007 bool Matrix<T>::operator <= (const Matrix<T>& other_matrix) const
1008 {
1009  // Control sentence (if debug)
1010 
1011  #ifndef NDEBUG
1012 
1013  const size_t other_rows_number = other_matrix.get_rows_number();
1014  const size_t other_columns_number = other_matrix.get_columns_number();
1015 
1016  if(other_rows_number != rows_number)
1017  {
1018  std::ostringstream buffer;
1019 
1020  buffer << "OpenNN Exception: Matrix Template.\n"
1021  << "bool operator >= (const Matrix<T>&) const.\n"
1022  << "Both numbers of rows must be the same.\n";
1023 
1024  throw std::logic_error(buffer.str());
1025  }
1026  else if(other_columns_number != columns_number)
1027  {
1028  std::ostringstream buffer;
1029 
1030  buffer << "OpenNN Exception: Matrix Template.\n"
1031  << "bool operator >= (const Matrix<T>&) const.\n"
1032  << "Both numbers of columns must be the same.\n";
1033 
1034  throw std::logic_error(buffer.str());
1035  }
1036 
1037  #endif
1038 
1039  for(size_t i = 0; i < this->size(); i++)
1040  {
1041  if((*this)[i] > other_matrix[i])
1042  {
1043  return(false);
1044  }
1045  }
1046 
1047  return(true);
1048 }
1049 
1050 
1051 // bool operator <= (const T&) const
1052 
1056 
1057 template <class T>
1058 bool Matrix<T>::operator <= (const T& value) const
1059 {
1060  for(size_t i = 0; i < this->size(); i++)
1061  {
1062  if((*this)[i] > value)
1063  {
1064  return(false);
1065  }
1066  }
1067 
1068  return(true);
1069 }
1070 
1071 
1072 // METHODS
1073 
1074 // size_t get_rows_number(void) const method
1075 
1077 
1078 template <class T>
1079 const size_t& Matrix<T>::get_rows_number(void) const
1080 {
1081  return(rows_number);
1082 }
1083 
1084 
1085 // size_t get_columns_number(void) const method
1086 
1088 
1089 template <class T>
1090 const size_t& Matrix<T>::get_columns_number(void) const
1091 {
1092  return(columns_number);
1093 }
1094 
1095 
1096 // void set(void) method
1097 
1099 
1100 template <class T>
1101 void Matrix<T>::set(void)
1102 {
1103  rows_number = 0;
1104  columns_number = 0;
1105  this->clear();
1106 }
1107 
1108 
1109 // void set(const size_t&, const size_t&) method
1110 
1114 
1115 template <class T>
1116 void Matrix<T>::set(const size_t& new_rows_number, const size_t& new_columns_number)
1117 {
1118  // Control sentence (if debug)
1119 
1120  if(new_rows_number == rows_number && new_columns_number == columns_number)
1121  {
1122  // do nothing
1123  }
1124  else if(new_rows_number == 0 && new_columns_number == 0)
1125  {
1126  set();
1127  }
1128  else if(new_rows_number == 0)
1129  {
1130  std::ostringstream buffer;
1131 
1132  buffer << "OpenNN Exception: Matrix Template.\n"
1133  << "void set(const size_t&, const size_t&) method.\n"
1134  << "Number of rows must be greater than zero.\n";
1135 
1136  throw std::logic_error(buffer.str());
1137  }
1138  else if(new_columns_number == 0)
1139  {
1140  std::ostringstream buffer;
1141 
1142  buffer << "OpenNN Exception: Matrix Template.\n"
1143  << "void set(const size_t&, const size_t&) method.\n"
1144  << "Number of columns must be greater than zero.\n";
1145 
1146  throw std::logic_error(buffer.str());
1147  }
1148  else
1149  {
1150  rows_number = new_rows_number;
1151  columns_number = new_columns_number;
1152 
1153  this->resize(rows_number*columns_number);
1154  }
1155 }
1156 
1157 
1158 // void set(const size_t&, const size_t&, const T&) method
1159 
1165 
1166 template <class T>
1167 void Matrix<T>::set(const size_t& new_rows_number, const size_t& new_columns_number, const T& value)
1168 {
1169  if(new_rows_number == 0 && new_columns_number == 0)
1170  {
1171  set();
1172  }
1173  else if(new_rows_number == 0)
1174  {
1175  std::ostringstream buffer;
1176 
1177  buffer << "OpenNN Exception: Matrix Template.\n"
1178  << "void set(const size_t&, const size_t&, const T&) method.\n"
1179  << "Number of rows must be greater than zero.\n";
1180 
1181  throw std::logic_error(buffer.str());
1182  }
1183  else if(new_columns_number == 0)
1184  {
1185  std::ostringstream buffer;
1186 
1187  buffer << "OpenNN Exception: Matrix Template.\n"
1188  << "void set(const size_t&, const size_t&, const T&) method.\n"
1189  << "Number of columns must be greater than zero.\n";
1190 
1191  throw std::logic_error(buffer.str());
1192  }
1193  else
1194  {
1195  set(new_rows_number, new_columns_number);
1196  initialize(value);
1197  }
1198 }
1199 
1200 
1201 // void set(const Matrix&) method
1202 
1205 
1206 template <class T>
1207 void Matrix<T>::set(const Matrix<T>& other_matrix)
1208 {
1209  rows_number = other_matrix.rows_number;
1210  columns_number = other_matrix.columns_number;
1211 
1212  this->resize(rows_number*columns_number);
1213 
1214  for(size_t i = 0; i < (size_t)this->size(); i++)
1215  {
1216  (*this)[i] = other_matrix[i];
1217  }
1218 }
1219 
1220 
1221 // void set(const std::string&) method
1222 
1225 
1226 template <class T>
1227 void Matrix<T>::set(const std::string& file_name)
1228 {
1229  load(file_name);
1230 }
1231 
1232 
1233 // void set_identity(const size_t&) method
1234 
1237 
1238 template <class T>
1239 void Matrix<T>::set_identity(const size_t& new_size)
1240 {
1241  set(new_size, new_size);
1242  initialize_identity();
1243 }
1244 
1245 
1246 // void set_rows_number(const size_t&) method
1247 
1250 
1251 template <class T>
1252 void Matrix<T>::set_rows_number(const size_t& new_rows_number)
1253 {
1254  if(new_rows_number != rows_number)
1255  {
1256  set(new_rows_number, columns_number);
1257  }
1258 }
1259 
1260 
1261 // void set_columns_number(const size_t&) method
1262 
1265 
1266 template <class T>
1267 void Matrix<T>::set_columns_number(const size_t& new_columns_number)
1268 {
1269  if(new_columns_number != columns_number)
1270  {
1271  set(rows_number, new_columns_number);
1272  }
1273 }
1274 
1275 
1276 // void tuck_in(const size_t&, const size_t&, const Matrix<T>&) const method
1277 
1282 
1283 template <class T>
1284 void Matrix<T>::tuck_in(const size_t& row_position, const size_t& column_position, const Matrix<T>& other_matrix)
1285 {
1286  const size_t other_rows_number = other_matrix.get_rows_number();
1287  const size_t other_columns_number = other_matrix.get_columns_number();
1288 
1289  // Control sentence (if debug)
1290 
1291  #ifndef NDEBUG
1292 
1293  if(row_position + other_rows_number > rows_number)
1294  {
1295  std::ostringstream buffer;
1296 
1297  buffer << "OpenNN Exception: Matrix Template.\n"
1298  << "void tuck_in(const size_t&, const size_t&, const Matrix<T>&) const method.\n"
1299  << "Cannot tuck in matrix.\n";
1300 
1301  throw std::logic_error(buffer.str());
1302  }
1303 
1304  if(column_position + other_columns_number > columns_number)
1305  {
1306  std::ostringstream buffer;
1307 
1308  buffer << "OpenNN Exception: Matrix Template.\n"
1309  << "void tuck_in(const size_t&, const size_t&, const Matrix<T>&) const method.\n"
1310  << "Cannot tuck in matrix.\n";
1311 
1312  throw std::logic_error(buffer.str());
1313  }
1314 
1315  #endif
1316 
1317  for(size_t i = 0; i < other_rows_number; i++)
1318  {
1319  for(size_t j = 0; j < other_columns_number; j++)
1320  {
1321  (*this)(row_position+i,column_position+j) = other_matrix(i,j);
1322  }
1323  }
1324 }
1325 
1326 
1327 // size_t count_diagonal_elements(void) const method
1328 
1331 
1332 template <class T>
1334 {
1335  #ifndef NDEBUG
1336 
1337  if(!is_square())
1338  {
1339  std::ostringstream buffer;
1340 
1341  buffer << "OpenNN Exception: Matrix Template.\n"
1342  << "size_t count_diagonal_elements(void) const method.\n"
1343  << "The matrix is not square.\n";
1344 
1345  throw std::logic_error(buffer.str());
1346  }
1347 
1348  #endif
1349 
1350  const size_t rows_number = get_rows_number();
1351 
1352  size_t count = 0;
1353 
1354  for(size_t i = 0; i < rows_number; i++)
1355  {
1356  if((*this)(i,i) != 0)
1357  {
1358  count++;
1359  }
1360  }
1361 
1362  return(count);
1363 }
1364 
1365 
1366 // size_t count_off_diagonal_elements(void) const method
1367 
1370 
1371 template <class T>
1373 {
1374  #ifndef NDEBUG
1375 
1376  if(!is_square())
1377  {
1378  std::ostringstream buffer;
1379 
1380  buffer << "OpenNN Exception: Matrix Template.\n"
1381  << "size_t count_off_diagonal_elements(void) const method.\n"
1382  << "The matrix is not square.\n";
1383 
1384  throw std::logic_error(buffer.str());
1385  }
1386 
1387  #endif
1388 
1389  const size_t rows_number = get_rows_number();
1390  const size_t columns_number = get_columns_number();
1391 
1392  size_t count = 0;
1393 
1394  for(size_t i = 0; i < rows_number; i++)
1395  {
1396  for(size_t j = 0; j < columns_number; j++)
1397  {
1398  if(i != j && (*this)(i,j) != 0)
1399  {
1400  count++;
1401  }
1402  }
1403  }
1404 
1405  return(count);
1406 }
1407 
1408 
1409 
1410 // Matrix<T> arrange_submatrix(const Vector<size_t>&, const Vector<size_t>&) const method
1411 
1415 
1416 template <class T>
1417 Matrix<T> Matrix<T>::arrange_submatrix(const Vector<size_t>& row_indices, const Vector<size_t>& column_indices) const
1418 {
1419  const size_t row_indices_size = row_indices.size();
1420  const size_t column_indices_size = column_indices.size();
1421 
1422  Matrix<T> sub_matrix(row_indices_size, column_indices_size);
1423 
1424  size_t row_index;
1425  size_t column_index;
1426 
1427  for(size_t i = 0; i < row_indices_size; i++)
1428  {
1429  row_index = row_indices[i];
1430 
1431  for(size_t j = 0; j < column_indices_size; j++)
1432  {
1433  column_index = column_indices[j];
1434 
1435  sub_matrix(i,j) = (*this)(row_index,column_index);
1436  }
1437  }
1438 
1439  return(sub_matrix);
1440 }
1441 
1442 
1443 // Matrix<T> arrange_submatrix_rows(const Vector<size_t>&) const method
1444 
1447 
1448 template <class T>
1450 {
1451  const size_t row_indices_size = row_indices.size();
1452 
1453  Matrix<T> sub_matrix(row_indices_size, columns_number);
1454 
1455  size_t row_index;
1456 
1457  for(size_t i = 0; i < row_indices_size; i++)
1458  {
1459  row_index = row_indices[i];
1460 
1461  for(size_t j = 0; j < columns_number; j++)
1462  {
1463  sub_matrix(i,j) = (*this)(row_index,j);
1464  }
1465  }
1466 
1467  return(sub_matrix);
1468 }
1469 
1470 
1471 // Matrix<T> arrange_submatrix_columns(const Vector<size_t>&) const method
1472 
1475 
1476 template <class T>
1478 {
1479  const size_t column_indices_size = column_indices.size();
1480 
1481  Matrix<T> sub_matrix(rows_number, column_indices_size);
1482 
1483  size_t column_index;
1484 
1485  for(size_t i = 0; i < rows_number; i++)
1486  {
1487  for(size_t j = 0; j < column_indices_size; j++)
1488  {
1489  column_index = column_indices[j];
1490 
1491  sub_matrix(i,j) = (*this)(i,column_index);
1492  }
1493  }
1494 
1495  return(sub_matrix);
1496 }
1497 
1498 
1499 // Vector<T> arrange_row(const size_t&) const method
1500 
1503 
1504 template <class T>
1505 Vector<T> Matrix<T>::arrange_row(const size_t& i) const
1506 {
1507  // Control sentence (if debug)
1508 
1509  #ifndef NDEBUG
1510 
1511  if(i >= rows_number)
1512  {
1513  std::ostringstream buffer;
1514 
1515  buffer << "OpenNN Exception: Matrix Template.\n"
1516  << "Vector<T> arrange_row(const size_t&) const method.\n"
1517  << "Row index (" << i << ") must be less than number of rows (" << rows_number << ").\n";
1518 
1519  throw std::logic_error(buffer.str());
1520  }
1521 
1522  #endif
1523 
1524  Vector<T> row(columns_number);
1525 
1526  for(size_t j = 0; j < columns_number; j++)
1527  {
1528  row[j] = (*this)(i,j);
1529  }
1530 
1531  return(row);
1532 }
1533 
1534 
1535 // Vector<T> arrange_row(const size_t&, const Vector<size_t>&) const method
1536 
1540 
1541 template <class T>
1542 Vector<T> Matrix<T>::arrange_row(const size_t& row_index, const Vector<size_t>& column_indices) const
1543 {
1544  // Control sentence (if debug)
1545 
1546  #ifndef NDEBUG
1547 
1548  if(row_index >= rows_number)
1549  {
1550  std::ostringstream buffer;
1551 
1552  buffer << "OpenNN Exception: Matrix Template.\n"
1553  << "Vector<T> arrange_row(const size_t&, const Vector<size_t>&) const method.\n"
1554  << "Row index (" << row_index << ") must be less than number of rows (" << rows_number << ").\n";
1555 
1556  throw std::logic_error(buffer.str());
1557  }
1558 
1559  #endif
1560 
1561  const size_t size = column_indices.size();
1562 
1563  Vector<T> row(size);
1564 
1565  for(size_t i = 0; i < size; i++)
1566  {
1567  row[i] = (*this)(row_index,column_indices[i]);
1568  }
1569 
1570  return(row);
1571 }
1572 
1573 
1574 // Vector<T> arrange_column(const size_t&) const method
1575 
1578 
1579 template <class T>
1580 Vector<T> Matrix<T>::arrange_column(const size_t& j) const
1581 {
1582  // Control sentence (if debug)
1583 
1584  #ifndef NDEBUG
1585 
1586  if(j >= columns_number)
1587  {
1588  std::ostringstream buffer;
1589 
1590  buffer << "OpenNN Exception: Matrix Template.\n"
1591  << "Vector<T> arrange_column(const size_t&) const method.\n"
1592  << "Column index (" << j << ") must be less than number of columns (" << columns_number << ").\n";
1593 
1594  throw std::logic_error(buffer.str());
1595  }
1596 
1597  #endif
1598 
1599  Vector<T> column(rows_number);
1600 
1601  for(size_t i = 0; i < rows_number; i++)
1602  {
1603  column[i] = (*this)(i,j);
1604  }
1605 
1606  return(column);
1607 }
1608 
1609 
1610 // Vector<T> arrange_column(const size_t&) const method
1611 
1615 
1616 template <class T>
1617 Vector<T> Matrix<T>::arrange_column(const size_t& column_index, const Vector<size_t>& row_indices) const
1618 {
1619  // Control sentence (if debug)
1620 
1621  #ifndef NDEBUG
1622 
1623  if(column_index >= columns_number)
1624  {
1625  std::ostringstream buffer;
1626 
1627  buffer << "OpenNN Exception: Matrix Template.\n"
1628  << "Vector<T> arrange_column(const size_t&) const method.\n"
1629  << "Column index (" << column_index << ") must be less than number of rows (" << columns_number << ").\n";
1630 
1631  throw std::logic_error(buffer.str());
1632  }
1633 
1634  #endif
1635 
1636  const size_t size = row_indices.size();
1637 
1638  Vector<T> column(size);
1639 
1640  for(size_t i = 0; i < size; i++)
1641  {
1642  column[i] = (*this)(row_indices[i],column_index);
1643  }
1644 
1645  return(column);
1646 }
1647 
1648 
1649 // Vector<T> get_diagonal(void) const method
1650 
1652 
1653 template <class T>
1655 {
1656  // Control sentence (if debug)
1657 
1658  #ifndef NDEBUG
1659 
1660  if(rows_number != columns_number)
1661  {
1662  std::ostringstream buffer;
1663 
1664  buffer << "OpenNN Exception: Matrix Template.\n"
1665  << "Vector<T> get_diagonal(void) const method.\n"
1666  << "Matrix must be squared.\n";
1667 
1668  throw std::logic_error(buffer.str());
1669  }
1670 
1671  #endif
1672 
1673  Vector<T> diagonal(rows_number);
1674 
1675  for(size_t i = 0; i < rows_number; i++)
1676  {
1677  diagonal[i] = (*this)(i,i);
1678  }
1679 
1680  return(diagonal);
1681 }
1682 
1683 
1684 // void set_row(const size_t&, const Vector<T>&) const method
1685 
1689 
1690 template <class T>
1691 void Matrix<T>::set_row(const size_t& row_index, const Vector<T>& new_row)
1692 {
1693  // Control sentence (if debug)
1694 
1695  #ifndef NDEBUG
1696 
1697  if(row_index >= rows_number)
1698  {
1699  std::ostringstream buffer;
1700 
1701  buffer << "OpenNN Exception: Matrix Template.\n"
1702  << "set_row(const size_t&, const Vector<T>&) method.\n"
1703  << "Index must be less than number of rows.\n";
1704 
1705  throw std::logic_error(buffer.str());
1706  }
1707 
1708  const size_t size = new_row.size();
1709 
1710  if(size != columns_number)
1711  {
1712  std::ostringstream buffer;
1713 
1714  buffer << "OpenNN Exception: Matrix Template.\n"
1715  << "set_row(const size_t&, const Vector<T>&) method.\n"
1716  << "Size (" << size << ") must be equal to number of columns (" << columns_number << ").\n";
1717 
1718  throw std::logic_error(buffer.str());
1719  }
1720 
1721  #endif
1722 
1723  // Set new row
1724 
1725  for(size_t i = 0; i < columns_number; i++)
1726  {
1727  (*this)(row_index,i) = new_row[i];
1728  }
1729 }
1730 
1731 
1732 // void set_row(const size_t&, const T&) method
1733 
1737 
1738 template <class T>
1739 void Matrix<T>::set_row(const size_t& row_index, const T& value)
1740 {
1741  // Control sentence (if debug)
1742 
1743  #ifndef NDEBUG
1744 
1745  if(row_index >= rows_number)
1746  {
1747  std::ostringstream buffer;
1748 
1749  buffer << "OpenNN Exception: Matrix Template.\n"
1750  << "set_row(const size_t&, const T&) method.\n"
1751  << "Index must be less than number of rows.\n";
1752 
1753  throw std::logic_error(buffer.str());
1754  }
1755 
1756  #endif
1757 
1758  // Set new row
1759 
1760  for(size_t i = 0; i < columns_number; i++)
1761  {
1762  (*this)(row_index,i) = value;
1763  }
1764 }
1765 
1766 
1767 // void set_column(const size_t&, const Vector<T>&) method
1768 
1772 
1773 template <class T>
1774 void Matrix<T>::set_column(const size_t& column_index, const Vector<T>& new_column)
1775 {
1776  // Control sentence (if debug)
1777 
1778  #ifndef NDEBUG
1779 
1780  if(column_index >= columns_number)
1781  {
1782  std::ostringstream buffer;
1783 
1784  buffer << "OpenNN Exception: Matrix Template.\n"
1785  << "set_column(const size_t&, const Vector<T>&).\n"
1786  << "Index (" << column_index << ") must be less than number of columns (" << columns_number << ").\n";
1787 
1788  throw std::logic_error(buffer.str());
1789  }
1790 
1791  const size_t size = new_column.size();
1792 
1793  if(size != rows_number)
1794  {
1795  std::ostringstream buffer;
1796 
1797  buffer << "OpenNN Exception: Matrix Template.\n"
1798  << "set_column(const size_t&, const Vector<T>&).\n"
1799  << "Size must be equal to number of rows.\n";
1800 
1801  throw std::logic_error(buffer.str());
1802  }
1803 
1804  #endif
1805 
1806  // Set new column
1807 
1808  for(size_t i = 0; i < rows_number; i++)
1809  {
1810  (*this)(i,column_index) = new_column[i];
1811  }
1812 }
1813 
1814 
1815 // void set_column(const size_t&, const T&) method
1816 
1820 
1821 template <class T>
1822 void Matrix<T>::set_column(const size_t& column_index, const T& value)
1823 {
1824  // Control sentence (if debug)
1825 
1826  #ifndef NDEBUG
1827 
1828  if(column_index >= columns_number)
1829  {
1830  std::ostringstream buffer;
1831 
1832  buffer << "OpenNN Exception: Matrix Template.\n"
1833  << "set_column(const size_t&, const T&).\n"
1834  << "Index must be less than number of columns.\n";
1835 
1836  throw std::logic_error(buffer.str());
1837  }
1838 
1839  #endif
1840 
1841  // Set new column
1842 
1843  for(size_t i = 0; i < rows_number; i++)
1844  {
1845  (*this)(i,column_index) = value;
1846  }
1847 }
1848 
1849 
1850 // void set_diagonal(const T&) method
1851 
1852 
1856 
1857 template <class T>
1858 void Matrix<T>::set_diagonal(const T& new_diagonal)
1859 {
1860  // Control sentence (if debug)
1861 
1862  #ifndef NDEBUG
1863 
1864  if(rows_number != columns_number)
1865  {
1866  std::ostringstream buffer;
1867 
1868  buffer << "OpenNN Exception: Matrix Template.\n"
1869  << "set_diagonal(const T&).\n"
1870  << "Matrix must be square.\n";
1871 
1872  throw std::logic_error(buffer.str());
1873  }
1874 
1875  #endif
1876 
1877  // Set new column
1878 
1879  for(size_t i = 0; i < rows_number; i++)
1880  {
1881  (*this)(i,i) = new_diagonal;
1882  }
1883 }
1884 
1885 
1886 // void set_diagonal(const Vector<T>&) method
1887 
1891 
1892 template <class T>
1893 void Matrix<T>::set_diagonal(const Vector<T>& new_diagonal)
1894 {
1895  // Control sentence (if debug)
1896 
1897  #ifndef NDEBUG
1898 
1899  if(rows_number != columns_number)
1900  {
1901  std::ostringstream buffer;
1902 
1903  buffer << "OpenNN Exception: Matrix Template.\n"
1904  << "set_diagonal(const Vector<T>&) const.\n"
1905  << "Matrix is not square.\n";
1906 
1907  throw std::logic_error(buffer.str());
1908  }
1909 
1910  const size_t size = new_diagonal.size();
1911 
1912  if(size != rows_number)
1913  {
1914  std::ostringstream buffer;
1915 
1916  buffer << "OpenNN Exception: Matrix Template.\n"
1917  << "set_diagonal(const Vector<T>&) const.\n"
1918  << "Size of diagonal (" << size << ") is not equal to size of matrix (" << rows_number << ").\n";
1919 
1920  throw std::logic_error(buffer.str());
1921  }
1922 
1923  #endif
1924 
1925  // Set new column
1926 
1927  for(size_t i = 0; i < rows_number; i++)
1928  {
1929  (*this)(i,i) = new_diagonal[i];
1930  }
1931 }
1932 
1933 
1934 // void initialize_diagonal(const size_t&, const T&) method
1935 
1941 
1942 template <class T>
1943 void Matrix<T>::initialize_diagonal(const size_t& new_size, const T& new_value)
1944 {
1945  set(new_size, new_size, 0.0);
1946  set_diagonal(new_value);
1947 }
1948 
1949 
1950 // void initialize_diagonal(const size_t&, const Vector<T>&) method
1951 
1957 
1958 template <class T>
1959 void Matrix<T>::initialize_diagonal(const size_t& new_size, const Vector<T>& new_values)
1960 {
1961  // Control sentence (if debug)
1962 
1963  #ifndef NDEBUG
1964 
1965  const size_t new_values_size = new_values.size();
1966 
1967  if(new_values_size != new_size)
1968  {
1969  std::ostringstream buffer;
1970 
1971  buffer << "OpenNN Exception: Matrix Template.\n"
1972  << "initialize_diagonal(const size_t&, const size_t&) const.\n"
1973  << "Size of new values is not equal to size of square matrix.\n";
1974 
1975  throw std::logic_error(buffer.str());
1976  }
1977 
1978  #endif
1979 
1980  set(new_size, new_size, 0.0);
1981  set_diagonal(new_values);
1982 }
1983 
1984 
1985 // Matrix<T> sum_diagonal(const T&) const method
1986 
1990 
1991 template <class T>
1992 Matrix<T> Matrix<T>::sum_diagonal(const T& value) const
1993 {
1994  // Control sentence (if debug)
1995 
1996  #ifndef NDEBUG
1997 
1998  if(rows_number != columns_number)
1999  {
2000  std::ostringstream buffer;
2001 
2002  buffer << "OpenNN Exception: Matrix Template.\n"
2003  << "sum_diagonal(const T&) const.\n"
2004  << "Matrix must be square.\n";
2005 
2006  throw std::logic_error(buffer.str());
2007  }
2008 
2009  #endif
2010 
2011  Matrix<T> sum(*this);
2012 
2013  for(size_t i = 0; i < rows_number; i++)
2014  {
2015  sum(i,i) += value;
2016  }
2017 
2018  return(sum);
2019 }
2020 
2021 
2022 // Matrix<T> sum_diagonal(const Vector<T>&) const method
2023 
2027 
2028 template <class T>
2029 Matrix<T> Matrix<T>::sum_diagonal(const Vector<T>& new_summing_values) const
2030 {
2031  // Control sentence (if debug)
2032 
2033  #ifndef NDEBUG
2034 
2035  if(rows_number != columns_number)
2036  {
2037  std::ostringstream buffer;
2038 
2039  buffer << "OpenNN Exception: Matrix Template.\n"
2040  << "sum_diagonal(const Vector<T>&) const.\n"
2041  << "Matrix must be square.\n";
2042 
2043  throw std::logic_error(buffer.str());
2044  }
2045 
2046  const size_t size = new_summing_values.size();
2047 
2048  if(size != rows_number)
2049  {
2050  std::ostringstream buffer;
2051 
2052  buffer << "OpenNN Exception: Matrix Template.\n"
2053  << "sum_diagonal(const Vector<T>&) const.\n"
2054  << "Size must be equal to number of rows.\n";
2055 
2056  throw std::logic_error(buffer.str());
2057  }
2058 
2059  #endif
2060 
2061  Matrix<T> sum(*this);
2062 
2063  for(size_t i = 0; i < rows_number; i++)
2064  {
2065  sum(i,i) += new_summing_values[i];
2066  }
2067 
2068  return(sum);
2069 }
2070 
2071 
2072 // void append_row(const Vector<T>&) const method
2073 
2078 
2079 template <class T>
2080 void Matrix<T>::append_row(const Vector<T>& new_row)
2081 {
2082  #ifndef NDEBUG
2083 
2084  const size_t size = new_row.size();
2085 
2086  if(size != columns_number)
2087  {
2088  std::ostringstream buffer;
2089 
2090  buffer << "OpenNN Exception: Matrix Template.\n"
2091  << "append_row(const Vector<T>&) const.\n"
2092  << "Size must be equal to number of columns.\n";
2093 
2094  throw std::logic_error(buffer.str());
2095  }
2096 
2097  #endif
2098 
2099  Matrix<T> copy(*this);
2100 
2101  set(rows_number+1, columns_number);
2102 
2103  for(size_t i = 0; i < copy.get_rows_number(); i++)
2104  {
2105  for(size_t j = 0; j < copy.get_columns_number(); j++)
2106  {
2107  (*this)(i,j) = copy(i,j);
2108  }
2109  }
2110 
2111  set_row(rows_number-1, new_row);
2112 }
2113 
2114 
2115 // void append_column(const Vector<T>&) const method
2116 
2121 
2122 template <class T>
2123 void Matrix<T>::append_column(const Vector<T>& new_column)
2124 {
2125  #ifndef NDEBUG
2126 
2127  const size_t size = new_column.size();
2128 
2129  if(size != columns_number)
2130  {
2131  std::ostringstream buffer;
2132 
2133  buffer << "OpenNN Exception: Matrix Template.\n"
2134  << "append_column(const Vector<T>&) const.\n"
2135  << "Size must be equal to number of columns.\n";
2136 
2137  throw std::logic_error(buffer.str());
2138  }
2139 
2140  #endif
2141 
2142  set(rows_number, columns_number+1);
2143 
2144  set_column(columns_number-1, new_column);
2145 }
2146 
2147 
2148 // void insert_row(const size_t&, const Vector<T>&) const method
2149 
2154 
2155 template <class T>
2156 void Matrix<T>::insert_row(const size_t& position, const Vector<T>& new_row)
2157 {
2158  #ifndef NDEBUG
2159 
2160  if(position > rows_number)
2161  {
2162  std::ostringstream buffer;
2163 
2164  buffer << "OpenNN Exception: Matrix Template.\n"
2165  << "insert_row(const size_t&, const Vector<T>&) const.\n"
2166  << "Position must be less or equal than number of rows.\n";
2167 
2168  throw std::logic_error(buffer.str());
2169  }
2170 
2171  const size_t size = new_row.size();
2172 
2173  if(size != columns_number)
2174  {
2175  std::ostringstream buffer;
2176 
2177  buffer << "OpenNN Exception: Matrix Template.\n"
2178  << "insert_row(const size_t&, const Vector<T>&) const.\n"
2179  << "Size must be equal to number of columns.\n";
2180 
2181  throw std::logic_error(buffer.str());
2182  }
2183 
2184  #endif
2185 
2186  const size_t new_rows_number = rows_number + 1;
2187 
2188  Matrix<T> new_matrix(new_rows_number, columns_number);
2189 
2190  for(size_t i = 0; i < position; i++)
2191  {
2192  for(size_t j = 0; j < columns_number; j++)
2193  {
2194  new_matrix(i,j) = (*this)(i,j);
2195  }
2196  }
2197 
2198  for(size_t j = 0; j < columns_number; j++)
2199  {
2200  new_matrix(position,j) = new_row[j];
2201  }
2202 
2203  for(size_t i = position+1; i < new_rows_number; i++)
2204  {
2205  for(size_t j = 0; j < columns_number; j++)
2206  {
2207  new_matrix(i,j) = (*this)(i-1,j);
2208  }
2209  }
2210 
2211  set(new_matrix);
2212 }
2213 
2214 
2215 // void insert_column(const size_t&, const Vector<T>&) const method
2216 
2221 
2222 template <class T>
2223 void Matrix<T>::insert_column(const size_t& position, const Vector<T>& new_column)
2224 {
2225  #ifndef NDEBUG
2226 
2227  if(position > columns_number)
2228  {
2229  std::ostringstream buffer;
2230 
2231  buffer << "OpenNN Exception: Matrix Template.\n"
2232  << "insert_column(const size_t&, const Vector<T>&) const.\n"
2233  << "Position must be less or equal than number of columns.\n";
2234 
2235  throw std::logic_error(buffer.str());
2236  }
2237 
2238  const size_t size = (size_t)new_column.size();
2239 
2240  if(size != rows_number)
2241  {
2242  std::ostringstream buffer;
2243 
2244  buffer << "OpenNN Exception: Matrix Template.\n"
2245  << "insert_column(const size_t, const Vector<T>&) const.\n"
2246  << "Size must be equal to number of rows.\n";
2247 
2248  throw std::logic_error(buffer.str());
2249  }
2250 
2251  #endif
2252 
2253  const size_t new_columns_number = columns_number + 1;
2254 
2255  Matrix<T> new_matrix(rows_number, new_columns_number);
2256 
2257  for(size_t i = 0; i < rows_number; i++)
2258  {
2259  for(size_t j = 0; j < position; j++)
2260  {
2261  new_matrix(i,j) = (*this)(i,j);
2262  }
2263 
2264  new_matrix(i,position) = new_column[i];
2265 
2266  for(size_t j = position+1; j < new_columns_number; j++)
2267  {
2268  new_matrix(i,j) = (*this)(i,j-1);
2269  }
2270  }
2271 
2272  set(new_matrix);
2273 }
2274 
2275 
2276 // void subtract_row(const size_t&) const method
2277 
2281 
2282 template <class T>
2283 void Matrix<T>::subtract_row(const size_t& row_index)
2284 {
2285  #ifndef NDEBUG
2286 
2287  if(row_index >= rows_number)
2288  {
2289  std::ostringstream buffer;
2290 
2291  buffer << "OpenNN Exception: Matrix Template.\n"
2292  << "subtract_row(const size_t&) const.\n"
2293  << "Index of row must be less than number of rows.\n";
2294 
2295  throw std::logic_error(buffer.str());
2296  }
2297  else if(rows_number < 2)
2298  {
2299  std::ostringstream buffer;
2300 
2301  buffer << "OpenNN Exception: Matrix Template.\n"
2302  << "subtract_row(const size_t&) const.\n"
2303  << "Number of rows must be equal or greater than two.\n";
2304 
2305  throw std::logic_error(buffer.str());
2306  }
2307 
2308  #endif
2309 
2310  Matrix<T> new_matrix(rows_number-1, columns_number);
2311 
2312  for(size_t i = 0; i < row_index; i++)
2313  {
2314  for(size_t j = 0; j < columns_number; j++)
2315  {
2316  new_matrix(i,j) = (*this)(i,j);
2317  }
2318  }
2319 
2320  for(size_t i = row_index+1; i < rows_number; i++)
2321  {
2322  for(size_t j = 0; j < columns_number; j++)
2323  {
2324  new_matrix(i-1,j) = (*this)(i,j);
2325  }
2326  }
2327 
2328  *this = new_matrix;
2329 }
2330 
2331 
2332 // void subtract_column(const size_t&) method
2333 
2337 
2338 template <class T>
2339 void Matrix<T>::subtract_column(const size_t& column_index)
2340 {
2341  #ifndef NDEBUG
2342 
2343  if(column_index >= columns_number)
2344  {
2345  std::ostringstream buffer;
2346 
2347  buffer << "OpenNN Exception: Matrix Template.\n"
2348  << "subtract_column(const size_t&) const.\n"
2349  << "Index of column must be less than number of columns.\n";
2350 
2351  throw std::logic_error(buffer.str());
2352  }
2353  else if(columns_number < 2)
2354  {
2355  std::ostringstream buffer;
2356 
2357  buffer << "OpenNN Exception: Matrix Template.\n"
2358  << "subtract_column(const size_t&) const.\n"
2359  << "Number of columns must be equal or greater than two.\n";
2360 
2361  throw std::logic_error(buffer.str());
2362  }
2363 
2364  #endif
2365 
2366  Matrix<T> new_matrix(rows_number, columns_number-1);
2367 
2368  for(size_t i = 0; i < rows_number; i++)
2369  {
2370  for(size_t j = 0; j < column_index; j++)
2371  {
2372  new_matrix(i,j) = (*this)(i,j);
2373  }
2374  }
2375 
2376  for(size_t i = 0; i < rows_number; i++)
2377  {
2378  for(size_t j = column_index+1; j < columns_number; j++)
2379  {
2380  new_matrix(i,j-1) = (*this)(i,j);
2381  }
2382  }
2383 
2384  *this = new_matrix;
2385 }
2386 
2387 
2388 // Matrix<T> assemble_rows(const Matrix<T>&) const method
2389 
2392 
2393 template <class T>
2395 {
2396  #ifndef NDEBUG
2397 
2398  const size_t other_columns_number = other_matrix.get_columns_number();
2399 
2400  if(other_columns_number != columns_number)
2401  {
2402  std::ostringstream buffer;
2403 
2404  buffer << "OpenNN Exception: Matrix Template.\n"
2405  << "Matrix<T> assemble_rows(const Matrix<T>&) const method.\n"
2406  << "Number of columns of other matrix (" << other_columns_number << ") must be equal to number of columns of this matrix (" << columns_number << ").\n";
2407 
2408  throw std::logic_error(buffer.str());
2409  }
2410 
2411  #endif
2412 
2413  const size_t other_rows_number = other_matrix.get_rows_number();
2414 
2415  Matrix<T> assembly(rows_number + other_rows_number, columns_number);
2416 
2417  for(size_t i = 0; i < rows_number; i++)
2418  {
2419  for(size_t j = 0; j < columns_number; j++)
2420  {
2421  assembly(i,j) = (*this)(i,j);
2422  }
2423  }
2424 
2425  for(size_t i = 0; i < other_rows_number; i++)
2426  {
2427  for(size_t j = 0; j < columns_number; j++)
2428  {
2429  assembly(rows_number+i,j) = other_matrix(i,j);
2430  }
2431  }
2432 
2433  return(assembly);
2434 }
2435 
2436 
2437 // void sort_less(const size_t&) method
2438 
2440 
2441 template <class T>
2442 void Matrix<T>::sort_less(const size_t&)
2443 {
2444 
2445 }
2446 
2447 
2448 // void sort_greater(const size_t&) method
2449 
2451 
2452 template <class T>
2453 void Matrix<T>::sort_greater(const size_t&)
2454 {
2455 
2456 }
2457 
2458 
2459 // Matrix<T> assemble_columns(const Matrix<T>&) const method
2460 
2463 
2464 template <class T>
2466 {
2467  #ifndef NDEBUG
2468 
2469  const size_t other_rows_number = other_matrix.get_rows_number();
2470 
2471  if(other_rows_number != rows_number)
2472  {
2473  std::ostringstream buffer;
2474 
2475  buffer << "OpenNN Exception: Matrix Template.\n"
2476  << "Matrix<T> assemble_columns(const Matrix<T>&) const method.\n"
2477  << "Number of rows of other matrix (" << other_rows_number << ") must be equal to number of rows of this matrix (" << rows_number << ").\n";
2478 
2479  throw std::logic_error(buffer.str());
2480  }
2481 
2482  #endif
2483 
2484  const size_t other_columns_number = other_matrix.get_columns_number();
2485 
2486  Matrix<T> assembly(rows_number, columns_number + other_columns_number);
2487 
2488  for(size_t i = 0; i < rows_number; i++)
2489  {
2490  for(size_t j = 0; j < columns_number; j++)
2491  {
2492  assembly(i,j) = (*this)(i,j);
2493  }
2494  for(size_t j = 0; j < other_columns_number; j++)
2495  {
2496  assembly(i,columns_number+j) = other_matrix(i,j);
2497  }
2498  }
2499 
2500  return(assembly);
2501 }
2502 
2503 
2504 // void initialize(const T&) method
2505 
2508 
2509 template <class T>
2510 void Matrix<T>::initialize(const T& value)
2511 {
2512  std::fill((*this).begin(), (*this).end(), value);
2513 }
2514 
2515 
2516 // void randomize_uniform(const double&, const double&) method
2517 
2522 
2523 template <class T>
2524 void Matrix<T>::randomize_uniform(const double& minimum, const double& maximum)
2525 {
2526  // Control sentence (if debug)
2527 
2528  #ifndef NDEBUG
2529 
2530  if(minimum > maximum)
2531  {
2532  std::ostringstream buffer;
2533 
2534  buffer << "OpenNN Exception: Matrix Template.\n"
2535  << "void randomize_uniform(const double&, const double&) const method.\n"
2536  << "Minimum value must be less or equal than maximum value.\n";
2537 
2538  throw std::logic_error(buffer.str());
2539  }
2540 
2541  #endif
2542 
2543  for(size_t i = 0; i < this->size(); i++)
2544  {
2545  (*this)[i] = (T)calculate_random_uniform(minimum, maximum);
2546  }
2547 }
2548 
2549 
2550 // void randomize_uniform(const Matrix<double>&, const Matrix<double>&) const method
2551 
2556 
2557 template <class T>
2558 void Matrix<T>::randomize_uniform(const Matrix<double>& minimum, const Matrix<double>& maximum)
2559 {
2560  // Control sentence (if debug)
2561 
2562  #ifndef NDEBUG
2563 
2564  if(minimum > maximum)
2565  {
2566  std::ostringstream buffer;
2567 
2568  buffer << "OpenNN Exception: Matrix Template.\n"
2569  << "void randomize_uniform(const Matrix<double>&, const Matrix<double>&) const method.\n"
2570  << "Minimum values must be less or equal than their respective maximum values.\n";
2571 
2572  throw std::logic_error(buffer.str());
2573  }
2574 
2575  #endif
2576 
2577 
2578  for(size_t i = 0; i < this->size(); i++)
2579  {
2580  (*this)[i] = calculate_random_uniform(minimum[i], maximum[i]);
2581  }
2582 }
2583 
2584 
2585 // void randomize_normal(const double&, const double&) method
2586 
2591 
2592 template <class T>
2593 void Matrix<T>::randomize_normal(const double& mean, const double& standard_deviation)
2594 {
2595  // Control sentence (if debug)
2596 
2597  #ifndef NDEBUG
2598 
2599  if(standard_deviation < 0.0)
2600  {
2601  std::ostringstream buffer;
2602 
2603  buffer << "OpenNN Exception: Matrix Template.\n"
2604  << "void randomize_normal(const double&, const double&) method.\n"
2605  << "Standard deviation must be equal or greater than zero.\n";
2606 
2607  throw std::logic_error(buffer.str());
2608  }
2609 
2610  #endif
2611 
2612  for(size_t i = 0; i < this->size(); i++)
2613  {
2614  (*this)[i] = calculate_random_normal(mean, standard_deviation);
2615  }
2616 }
2617 
2618 
2619 // void randomize_normal(const Matrix<double>&, const Matrix<double>&) const method
2620 
2625 
2626 template <class T>
2627 void Matrix<T>::randomize_normal(const Matrix<double>& mean, const Matrix<double>& standard_deviation)
2628 {
2629  // Control sentence (if debug)
2630 
2631  #ifndef NDEBUG
2632 
2633  if(standard_deviation < 0.0)
2634  {
2635  std::ostringstream buffer;
2636 
2637  buffer << "OpenNN Exception: Matrix Template.\n"
2638  << "void randomize_normal(const Matrix<double>&, const Matrix<double>&) const method.\n"
2639  << "Standard deviations must be equal or greater than zero.\n";
2640 
2641  throw std::logic_error(buffer.str());
2642  }
2643 
2644  #endif
2645 
2646  for(size_t i = 0; i < this->size(); i++)
2647  {
2648  (*this)[i] = calculate_random_uniform(mean[i], standard_deviation[i]);
2649  }
2650 }
2651 
2652 
2653 // void initialize_identity(void) const method
2654 
2657 
2658 template <class T>
2660 {
2661  // Control sentence (if debug)
2662 
2663  #ifndef NDEBUG
2664 
2665  if(rows_number != columns_number)
2666  {
2667  std::ostringstream buffer;
2668 
2669  std::cout << "OpenNN Exception: Matrix Template.\n"
2670  << "initialize_identity(void) const method.\n"
2671  << "Matrix must be square.\n";
2672 
2673  throw std::logic_error(buffer.str());
2674  }
2675 
2676  #endif
2677 
2678  (*this).initialize(0);
2679 
2680  for(size_t i = 0; i < rows_number; i++)
2681  {
2682  (*this)(i,i) = 1;
2683  }
2684 }
2685 
2686 
2687 // void initialize_diagonal(const T&) method
2688 
2691 
2692 template <class T>
2694 {
2695  // Control sentence (if debug)
2696 
2697  #ifndef NDEBUG
2698 
2699  if(rows_number != columns_number)
2700  {
2701  std::ostringstream buffer;
2702 
2703  std::cout << "OpenNN Exception: Matrix Template.\n"
2704  << "initialize_diagonal(const T&) const method.\n"
2705  << "Matrix must be square.\n";
2706 
2707  throw std::logic_error(buffer.str());
2708  }
2709 
2710  #endif
2711 
2712  for(size_t i = 0; i < rows_number; i++)
2713  {
2714  for(size_t j = 0; j < columns_number; j++)
2715  {
2716  if(i==j)
2717  {
2718  (*this)(i,j) = value;
2719  }
2720  else
2721  {
2722  (*this)(i,j) = 0;
2723  }
2724  }
2725  }
2726 }
2727 
2728 
2729 // T calculate_sum(void) const method
2730 
2732 
2733 template <class T>
2735 {
2736  T sum = 0;
2737 
2738  for(size_t i = 0; i < this->size(); i++)
2739  {
2740  sum += (*this)[i];
2741  }
2742 
2743  return(sum);
2744 }
2745 
2746 
2747 // Vector<T> calculate_rows_sum(void) const method
2748 
2750 
2751 template <class T>
2753 {
2754  // Control sentence (if debug)
2755 
2756  #ifndef NDEBUG
2757 
2758  if(this->empty())
2759  {
2760  std::ostringstream buffer;
2761 
2762  std::cout << "OpenNN Exception: Matrix Template.\n"
2763  << "Vector<T> calculate_rows_sum(void) const method.\n"
2764  << "Matrix is empty.\n";
2765 
2766  throw std::logic_error(buffer.str());
2767  }
2768 
2769  #endif
2770 
2771  Vector<T> rows_sum(columns_number, 0);
2772 
2773  for(size_t i = 0; i < rows_number; i++)
2774  {
2775  for(size_t j = 0; j < columns_number; j++)
2776  {
2777  rows_sum[j] += (*this)(i,j);
2778  }
2779  }
2780 
2781  return(rows_sum);
2782 }
2783 
2784 
2785 // void sum_row(const size_t&, const Vector<T>&) method
2786 
2789 
2790 template <class T>
2791 void Matrix<T>::sum_row(const size_t& row_index, const Vector<T>& vector)
2792 {
2793  // Control sentence (if debug)
2794 
2795  #ifndef NDEBUG
2796 
2797  if(vector.size() != columns_number)
2798  {
2799  std::ostringstream buffer;
2800 
2801  std::cout << "OpenNN Exception: Matrix Template.\n"
2802  << "void sum_row(const size_t&, const Vector<T>&) method.\n"
2803  << "Size of vector must be equal to number of columns.\n";
2804 
2805  throw std::logic_error(buffer.str());
2806  }
2807 
2808  #endif
2809 
2810  for(size_t j = 0; j < columns_number; j++)
2811  {
2812  (*this)(row_index,j) += vector[j];
2813  }
2814 }
2815 
2816 
2817 // double calculate_trace(void) const method
2818 
2821 
2822 template <class T>
2823 double Matrix<T>::calculate_trace(void) const
2824 {
2825  // Control sentence (if debug)
2826 
2827  #ifndef NDEBUG
2828 
2829  if(!is_square())
2830  {
2831  std::ostringstream buffer;
2832 
2833  buffer << "OpenNN Exception: Matrix template.\n"
2834  << "double calculate_trace(void) const method.\n"
2835  << "Matrix is not square.\n";
2836 
2837  throw std::logic_error(buffer.str());
2838  }
2839 
2840  #endif
2841 
2842  double trace = 0.0;
2843 
2844  for(size_t i = 0; i < rows_number; i++)
2845  {
2846  trace += (*this)(i,i);
2847  }
2848 
2849  return(trace);
2850 }
2851 
2852 
2853 // Vector<double> calculate_mean(void) const method
2854 
2857 
2858 template <class T>
2860 {
2861  // Control sentence (if debug)
2862 
2863  #ifndef NDEBUG
2864 
2865  if(rows_number == 0)
2866  {
2867  std::ostringstream buffer;
2868 
2869  buffer << "OpenNN Exception: Matrix template.\n"
2870  << "Vector<double> calculate_mean(void) const method.\n"
2871  << "Number of rows must be greater than one.\n";
2872 
2873  throw std::logic_error(buffer.str());
2874  }
2875 
2876  #endif
2877 
2878  // Mean
2879 
2880  Vector<double> mean(columns_number, 0.0);
2881 
2882  for(size_t j = 0; j < columns_number; j++)
2883  {
2884  for(size_t i = 0; i < rows_number; i++)
2885  {
2886  mean[j] += (*this)(i,j);
2887  }
2888 
2889  mean[j] /= (double)rows_number;
2890  }
2891 
2892  return(mean);
2893 }
2894 
2895 
2896 // double calculate_mean(const size_t&) const method
2897 
2900 
2901 template <class T>
2902 double Matrix<T>::calculate_mean(const size_t& column_index) const
2903 {
2904  // Control sentence (if debug)
2905 
2906  #ifndef NDEBUG
2907 
2908  if(rows_number == 0)
2909  {
2910  std::ostringstream buffer;
2911 
2912  buffer << "OpenNN Exception: Matrix template.\n"
2913  << "double calculate_mean(const size_t&) const method.\n"
2914  << "Number of rows must be greater than one.\n";
2915 
2916  throw std::logic_error(buffer.str());
2917  }
2918 
2919  if(column_index >= columns_number)
2920  {
2921  std::ostringstream buffer;
2922 
2923  buffer << "OpenNN Exception: Matrix template.\n"
2924  << "double calculate_mean(const size_t&) const method.\n"
2925  << "Index of column must be less than number of columns.\n";
2926 
2927  throw std::logic_error(buffer.str());
2928  }
2929 
2930  #endif
2931 
2932  // Mean
2933 
2934  double mean = 0.0;
2935 
2936  for(size_t i = 0; i < rows_number; i++)
2937  {
2938  mean += (*this)(i,column_index);
2939  }
2940 
2941  mean /= (double)rows_number;
2942 
2943  return(mean);
2944 }
2945 
2946 
2947 // Vector<double> calculate_mean(const Vector<size_t>&) const method
2948 
2952 
2953 template <class T>
2955 {
2956  const size_t column_indices_size = column_indices.size();
2957 
2958  size_t column_index;
2959 
2960  // Mean
2961 
2962  Vector<double> mean(column_indices_size, 0.0);
2963 
2964  for(size_t j = 0; j < column_indices_size; j++)
2965  {
2966  column_index = column_indices[j];
2967 
2968  for(size_t i = 0; i < rows_number; i++)
2969  {
2970  mean[j] += (*this)(i,column_index);
2971  }
2972 
2973  mean[j] /= (double)rows_number;
2974  }
2975 
2976  return(mean);
2977 }
2978 
2979 
2980 // Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method
2981 
2986 
2987 template <class T>
2988 Vector<double> Matrix<T>::calculate_mean(const Vector<size_t>& row_indices, const Vector<size_t>& column_indices) const
2989 {
2990  const size_t row_indices_size = row_indices.size();
2991  const size_t column_indices_size = column_indices.size();
2992 
2993  // Control sentence (if debug)
2994 
2995  #ifndef NDEBUG
2996 
2997  // Rows check
2998 
2999  if(row_indices_size > rows_number)
3000  {
3001  std::ostringstream buffer;
3002 
3003  buffer << "OpenNN Exception: Matrix template.\n"
3004  << "Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3005  << "Size of row indices (" << row_indices_size << ") is greater than number of rows (" << rows_number << ").\n";
3006 
3007  throw std::logic_error(buffer.str());
3008  }
3009 
3010  for(size_t i = 0; i < row_indices_size; i++)
3011  {
3012  if(row_indices[i] >= rows_number)
3013  {
3014  std::ostringstream buffer;
3015 
3016  buffer << "OpenNN Exception: Matrix template.\n"
3017  << "Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3018  << "Row index " << i << " must be less than rows number.\n";
3019 
3020  throw std::logic_error(buffer.str());
3021  }
3022  }
3023 
3024  if(row_indices_size == 0)
3025  {
3026  std::ostringstream buffer;
3027 
3028  buffer << "OpenNN Exception: Matrix template.\n"
3029  << "Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3030  << "Size of row indices must be greater than zero.\n";
3031 
3032  throw std::logic_error(buffer.str());
3033  }
3034 
3035  // Columns check
3036 
3037  if(column_indices_size > columns_number)
3038  {
3039  std::ostringstream buffer;
3040 
3041  buffer << "OpenNN Exception: Matrix template.\n"
3042  << "Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3043  << "Column indices size must be equal or less than columns number.\n";
3044 
3045  throw std::logic_error(buffer.str());
3046  }
3047 
3048  for(size_t i = 0; i < column_indices_size; i++)
3049  {
3050  if(column_indices[i] >= columns_number)
3051  {
3052  std::ostringstream buffer;
3053 
3054  buffer << "OpenNN Exception: Matrix template.\n"
3055  << "Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3056  << "Column index " << i << " must be less than columns number.\n";
3057 
3058  throw std::logic_error(buffer.str());
3059  }
3060  }
3061 
3062  #endif
3063 
3064  size_t row_index;
3065  size_t column_index;
3066 
3067  // Mean
3068 
3069  Vector<double> mean(column_indices_size, 0.0);
3070 
3071  for(size_t j = 0; j < column_indices_size; j++)
3072  {
3073  column_index = column_indices[j];
3074 
3075  for(size_t i = 0; i < row_indices_size; i++)
3076  {
3077  row_index = row_indices[i];
3078 
3079  mean[j] += (*this)(row_index,column_index);
3080  }
3081 
3082  mean[j] /= (double)rows_number;
3083  }
3084 
3085  return(mean);
3086 }
3087 
3088 
3089 // Vector<double> calculate_mean_missing_values(const Vector< Vector<size_t> >&) const method
3090 
3094 
3095 template <class T>
3097 {
3098  Vector<size_t> row_indices(0, 1, rows_number-1);
3099  Vector<size_t> column_indices(0, 1, columns_number-1);
3100 
3101  return(calculate_mean_missing_values(row_indices, column_indices, missing_indices));
3102 }
3103 
3104 
3105 // Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method
3106 
3112 
3113 template <class T>
3114 Vector<double> Matrix<T>::calculate_mean_missing_values(const Vector<size_t>& row_indices, const Vector<size_t>& column_indices, const Vector< Vector<size_t> >& missing_indices) const
3115 {
3116  const size_t row_indices_size = row_indices.size();
3117  const size_t column_indices_size = column_indices.size();
3118 
3119  // Control sentence (if debug)
3120 
3121  #ifndef NDEBUG
3122 
3123  // Rows check
3124 
3125  if(row_indices_size > rows_number)
3126  {
3127  std::ostringstream buffer;
3128 
3129  buffer << "OpenNN Exception: Matrix template.\n"
3130  << "Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3131  << "Size of row indices (" << row_indices_size << ") is greater than number of rows (" << rows_number << ").\n";
3132 
3133  throw std::logic_error(buffer.str());
3134  }
3135 
3136  for(size_t i = 0; i < row_indices_size; i++)
3137  {
3138  if(row_indices[i] >= rows_number)
3139  {
3140  std::ostringstream buffer;
3141 
3142  buffer << "OpenNN Exception: Matrix template.\n"
3143  << "Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, Vector< Vector<size_t> >&) const method.\n"
3144  << "Row index " << i << " must be less than rows number.\n";
3145 
3146  throw std::logic_error(buffer.str());
3147  }
3148  }
3149 
3150  if(row_indices_size == 0)
3151  {
3152  std::ostringstream buffer;
3153 
3154  buffer << "OpenNN Exception: Matrix template.\n"
3155  << "Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3156  << "Size of row indices must be greater than zero.\n";
3157 
3158  throw std::logic_error(buffer.str());
3159  }
3160 
3161  // Columns check
3162 
3163  if(column_indices_size > columns_number)
3164  {
3165  std::ostringstream buffer;
3166 
3167  buffer << "OpenNN Exception: Matrix template.\n"
3168  << "Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3169  << "Column indices size must be equal or less than columns number.\n";
3170 
3171  throw std::logic_error(buffer.str());
3172  }
3173 
3174  for(size_t i = 0; i < column_indices_size; i++)
3175  {
3176  if(column_indices[i] >= columns_number)
3177  {
3178  std::ostringstream buffer;
3179 
3180  buffer << "OpenNN Exception: Matrix template.\n"
3181  << "Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3182  << "Column index " << i << " must be less than columns number.\n";
3183 
3184  throw std::logic_error(buffer.str());
3185  }
3186  }
3187 
3188  #endif
3189 
3190  size_t row_index;
3191  size_t column_index;
3192 
3193  // Mean
3194 
3195  Vector<double> mean(column_indices_size, 0.0);
3196 
3197  Vector<size_t> count(column_indices_size, 0);
3198 
3199  for(size_t j = 0; j < column_indices_size; j++)
3200  {
3201  column_index = column_indices[j];
3202 
3203  for(size_t i = 0; i < row_indices_size; i++)
3204  {
3205  row_index = row_indices[i];
3206 
3207  if(!missing_indices[j].contains(row_index))
3208  {
3209  mean[j] += (*this)(row_index,column_index);
3210  count[j]++;
3211  }
3212  }
3213 
3214  mean[j] /= (double)count[j];
3215  }
3216 
3217  return(mean);
3218 }
3219 
3220 
3221 // Vector<double> calculate_mean_standard_deviation(void) const method
3222 
3226 
3227 template <class T>
3229 {
3230  // Control sentence (if debug)
3231 
3232  #ifndef NDEBUG
3233 
3234  if(rows_number == 0)
3235  {
3236  std::ostringstream buffer;
3237 
3238  buffer << "OpenNN Exception: Matrix template.\n"
3239  << "Vector<double> calculate_mean_standard_deviation(void) const method.\n"
3240  << "Number of rows must be greater than one.\n";
3241 
3242  throw std::logic_error(buffer.str());
3243  }
3244 
3245  #endif
3246 
3247  // Mean
3248 
3249  Vector<double> mean(columns_number, 0.0);
3250  Vector<double> standard_deviation(columns_number, 0.0);
3251 
3252  for(size_t i = 0; i < columns_number; i++)
3253  {
3254  mean[i] = arrange_column(i).calculate_mean();
3255  standard_deviation[i] = arrange_column(i).calculate_standard_deviation();
3256 
3257  }
3258 
3259  // Mean and standard deviation of data
3260 
3261  Vector< Vector<double> > mean_standard_deviation(2);
3262 
3263  mean_standard_deviation[0] = mean;
3264  mean_standard_deviation[1] = standard_deviation;
3265 
3266  return(mean_standard_deviation);
3267 }
3268 
3269 
3270 // Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&) const method
3271 
3276 
3277 template <class T>
3279 {
3280  const size_t column_indices_size = column_indices.size();
3281 
3282  Vector<double> mean(column_indices_size);
3283  Vector<double> standard_deviation(column_indices_size);
3284 
3285  size_t column_index;
3286 
3287  Vector<double> column(rows_number);
3288 
3289  for(size_t i = 0; i < column_indices_size; i++)
3290  {
3291  column_index = column_indices[i];
3292 
3293  column = arrange_column(column_index);
3294 
3295  mean[i] = column.calculate_mean();
3296  standard_deviation[i] = column.calculate_standard_deviation();
3297  }
3298 
3299  // Mean and standard deviation
3300 
3301  Vector< Vector<double> > mean_standard_deviation(2);
3302 
3303  mean_standard_deviation[0] = mean;
3304  mean_standard_deviation[1] = standard_deviation;
3305 
3306  return(mean_standard_deviation);
3307 }
3308 
3309 
3310 // Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method
3311 
3317 
3318 template <class T>
3320 {
3321  const size_t row_indices_size = row_indices.size();
3322  const size_t column_indices_size = column_indices.size();
3323 
3324  // Control sentence (if debug)
3325 
3326  #ifndef NDEBUG
3327 
3328  // Rows check
3329 
3330  if(row_indices_size > rows_number)
3331  {
3332  std::ostringstream buffer;
3333 
3334  buffer << "OpenNN Exception: Matrix template.\n"
3335  << "Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3336  << "Row indices size must be equal or less than rows number.\n";
3337 
3338  throw std::logic_error(buffer.str());
3339  }
3340 
3341  for(size_t i = 0; i < row_indices_size; i++)
3342  {
3343  if(row_indices[i] >= rows_number)
3344  {
3345  std::ostringstream buffer;
3346 
3347  buffer << "OpenNN Exception: Matrix template.\n"
3348  << "Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3349  << "Row index " << i << " must be less than rows number.\n";
3350 
3351  throw std::logic_error(buffer.str());
3352  }
3353  }
3354 
3355  if(row_indices_size == 0)
3356  {
3357  std::ostringstream buffer;
3358 
3359  buffer << "OpenNN Exception: Matrix template.\n"
3360  << "Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3361  << "Size of row indices must be greater than zero.\n";
3362 
3363  throw std::logic_error(buffer.str());
3364  }
3365 
3366  // Columns check
3367 
3368  if(column_indices_size > columns_number)
3369  {
3370  std::ostringstream buffer;
3371 
3372  buffer << "OpenNN Exception: Matrix template.\n"
3373  << "Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3374  << "Column indices size must be equal or less than columns number.\n";
3375 
3376  throw std::logic_error(buffer.str());
3377  }
3378 
3379  for(size_t i = 0; i < column_indices_size; i++)
3380  {
3381  if(column_indices[i] >= columns_number)
3382  {
3383  std::ostringstream buffer;
3384 
3385  buffer << "OpenNN Exception: Matrix template.\n"
3386  << "Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3387  << "Column index " << i << " must be less than columns number.\n";
3388 
3389  throw std::logic_error(buffer.str());
3390  }
3391  }
3392 
3393  #endif
3394 
3395  size_t row_index;
3396  size_t column_index;
3397 
3398  // Mean
3399 
3400  Vector<double> mean(column_indices_size, 0.0);
3401 
3402  for(size_t j = 0; j < column_indices_size; j++)
3403  {
3404  column_index = column_indices[j];
3405 
3406  mean[j] = 0.0;
3407 
3408  for(size_t i = 0; i < row_indices_size; i++)
3409  {
3410  row_index = row_indices[i];
3411 
3412  mean[j] += (*this)(row_index,column_index);
3413  }
3414 
3415  mean[j] /= (double)rows_number;
3416  }
3417 
3418  // Standard deviation
3419 
3420  Vector<double> standard_deviation(column_indices_size, 0.0);
3421 
3422  for(size_t j = 0; j < column_indices_size; j++)
3423  {
3424  column_index = column_indices[j];
3425 
3426  standard_deviation[j] = 0.0;
3427 
3428  for(size_t i = 0; i < row_indices_size; i++)
3429  {
3430  row_index = row_indices[i];
3431 
3432  standard_deviation[j] += ((*this)(row_index,column_index) - mean[j])*((*this)(row_index,column_index) - mean[j]);
3433  }
3434 
3435  standard_deviation[j] = sqrt(standard_deviation[j]/(rows_number-1.0));
3436  }
3437 
3438  // Mean and standard deviation
3439 
3440  Vector< Vector<double> > mean_standard_deviation(2);
3441 
3442  mean_standard_deviation[0] = mean;
3443  mean_standard_deviation[1] = standard_deviation;
3444 
3445  return(mean_standard_deviation);
3446 }
3447 
3448 
3449 // Type calculate_minimum(void) const method
3450 
3452 
3453 template <class T>
3455 {
3456  T minimum = (T)1.0e99;
3457 
3458  for(size_t i = 0; i < this->size(); i++)
3459  {
3460  if((*this)[i] < minimum)
3461  {
3462  minimum = (*this)[i];
3463  }
3464  }
3465 
3466  return(minimum);
3467 }
3468 
3469 
3470 // Type calculate_maximum(void) const method
3471 
3473 
3474 template <class T>
3476 {
3477  T maximum = (T)-1.0e99;
3478 
3479  for(size_t i = 0; i < this->size(); i++)
3480  {
3481  if((*this)[i] > maximum)
3482  {
3483  maximum = (*this)[i];
3484  }
3485  }
3486 
3487  return(maximum);
3488 }
3489 
3490 
3491 // Vector< Vector<T> > calculate_minimum_maximum(void) const method
3492 
3496 
3497 template <class T>
3499 {
3500  Vector< Vector<T> > minimum_maximum(2);
3501 
3502  Vector<T> minimum(columns_number, (T)1.0e99);
3503  Vector<T> maximum(columns_number, (T)-1.0e99);
3504 
3505  for(size_t j = 0; j < columns_number; j++)
3506  {
3507  for(size_t i = 0; i < rows_number; i++)
3508  {
3509  if((*this)(i,j) < minimum[j])
3510  {
3511  minimum[j] = (*this)(i,j);
3512  }
3513 
3514  if((*this)(i,j) > maximum[j])
3515  {
3516  maximum[j] = (*this)(i,j);
3517  }
3518  }
3519  }
3520 
3521  // Minimum and maximum
3522 
3523  minimum_maximum[0] = minimum;
3524  minimum_maximum[1] = maximum;
3525 
3526  return(minimum_maximum);
3527 }
3528 
3529 
3530 // Vector<double> calculate_minimum_maximum(const Vector<size_t>&) const method
3531 
3536 
3537 template <class T>
3539 {
3540  const size_t column_indices_size = column_indices.size();
3541 
3542  #ifndef NDEBUG
3543 
3544  for(size_t i = 0; i < column_indices_size; i++)
3545  {
3546  if(column_indices[i] >= columns_number)
3547  {
3548  std::ostringstream buffer;
3549 
3550  buffer << "OpenNN Exception: Matrix template."
3551  << "Vector<T> calculate_minimum_maximum(const Vector<size_t>&) const method.\n"
3552  << "Index of column must be less than number of columns.\n";
3553 
3554  throw std::logic_error(buffer.str());
3555  }
3556  }
3557 
3558  #endif
3559 
3560  size_t column_index;
3561 
3562  Vector<T> minimum(column_indices_size, (T)1.0e99);
3563  Vector<T> maximum(column_indices_size, (T)-1.0e99);
3564 
3565  for(size_t j = 0; j < column_indices_size; j++)
3566  {
3567  column_index = column_indices[j];
3568 
3569  for(size_t i = 0; i < rows_number; i++)
3570  {
3571  if((*this)(i,column_index) < minimum[j])
3572  {
3573  minimum[j] = (*this)(i,column_index);
3574  }
3575 
3576  if((*this)(i,column_index) > maximum[j])
3577  {
3578  maximum[j] = (*this)(i,column_index);
3579  }
3580  }
3581  }
3582 
3583  // Minimum and maximum
3584 
3585  Vector< Vector<T> > minimum_maximum(2);
3586 
3587  minimum_maximum[0] = minimum;
3588  minimum_maximum[1] = maximum;
3589 
3590  return(minimum_maximum);
3591 }
3592 
3593 
3594 // Vector<double> calculate_minimum_maximum(const Vector<size_t>&, const Vector<size_t>&) const method
3595 
3601 
3602 template <class T>
3604 {
3605  const size_t row_indices_size = row_indices.size();
3606  const size_t column_indices_size = column_indices.size();
3607 
3608  Vector<T> minimum(column_indices_size, (T)1.0e99);
3609  Vector<T> maximum(column_indices_size, (T)-1.0e99);
3610 
3611  size_t row_index;
3612  size_t column_index;
3613 
3614  for(size_t j = 0; j < column_indices_size; j++)
3615  {
3616  column_index = column_indices[j];
3617 
3618  for(size_t i = 0; i < row_indices_size; i++)
3619  {
3620  row_index = row_indices[i];
3621 
3622  if((*this)(row_index,column_index) < minimum[j])
3623  {
3624  minimum[j] = (*this)(row_index,column_index);
3625  }
3626 
3627  if((*this)(row_index,column_index) > maximum[j])
3628  {
3629  maximum[j] = (*this)(row_index,column_index);
3630  }
3631  }
3632  }
3633 
3634  // Minimum and maximum
3635 
3636  Vector< Vector<T> > minimum_maximum(2);
3637 
3638  minimum_maximum[0] = minimum;
3639  minimum_maximum[1] = maximum;
3640 
3641  return(minimum_maximum);
3642 }
3643 
3644 
3645 // Vector< Statistics<T> > calculate_statistics(void) const method
3646 
3650 
3651 template <class T>
3653 {
3654  // Control sentence (if debug)
3655 
3656  #ifndef NDEBUG
3657 
3658  if(rows_number == 0)
3659  {
3660  std::ostringstream buffer;
3661 
3662  buffer << "OpenNN Exception: Matrix template.\n"
3663  << "Vector< Statistics<double> > calculate_statistics(void) const method.\n"
3664  << "Number of rows must be greater than one.\n";
3665 
3666  throw std::logic_error(buffer.str());
3667  }
3668 
3669  #endif
3670 
3671  Vector< Statistics<T> > statistics(columns_number);
3672 
3673  Vector<T> column(rows_number);
3674 
3675  for(size_t i = 0; i < columns_number; i++)
3676  {
3677  column = arrange_column(i);
3678 
3679  statistics[i] = column.calculate_statistics();
3680  }
3681 
3682  return(statistics);
3683 }
3684 
3685 
3686 // Vector< Statistics<T> > calculate_statistics_missing_values(const Vector< Vector<size_t> >&) const method
3687 
3692 
3693 template <class T>
3695 {
3696  // Control sentence (if debug)
3697 
3698  #ifndef NDEBUG
3699 
3700  if(rows_number == 0)
3701  {
3702  std::ostringstream buffer;
3703 
3704  buffer << "OpenNN Exception: Matrix template.\n"
3705  << "Vector< Statistics<double> > calculate_statistics_missing_values(const Vector< Vector<size_t> >&) const method.\n"
3706  << "Number of rows must be greater than one.\n";
3707 
3708  throw std::logic_error(buffer.str());
3709  }
3710 
3711  if(missing_indices.size() != columns_number)
3712  {
3713  std::ostringstream buffer;
3714 
3715  buffer << "OpenNN Exception: Matrix template.\n"
3716  << "Vector< Statistics<double> > calculate_statistics_missing_values(const Vector< Vector<size_t> >&) const method.\n"
3717  << "Size of missing indices (" << missing_indices.size() << ") must be equal to to number of columns (" << columns_number << ").\n";
3718 
3719  throw std::logic_error(buffer.str());
3720  }
3721 
3722  #endif
3723 
3724  Vector< Statistics<T> > statistics(columns_number);
3725 
3726  Vector<T> column(rows_number);
3727 
3728  for(size_t i = 0; i < columns_number; i++)
3729  {
3730  column = arrange_column(i);
3731 
3732  statistics[i] = column.calculate_statistics_missing_values(missing_indices[i]);
3733  }
3734 
3735  return(statistics);
3736 }
3737 
3738 
3739 // Vector< Statistics<T> > calculate_statistics(const Vector<size_t>&, const Vector<size_t>&) const method
3740 
3746 
3747 template <class T>
3749 {
3750  const size_t row_indices_size = row_indices.size();
3751  const size_t column_indices_size = column_indices.size();
3752 
3753  Vector< Statistics<T> > statistics(column_indices_size);
3754 
3755  size_t index;
3756 
3757  Vector<T> column(row_indices_size);
3758 
3759  for(size_t i = 0; i < column_indices_size; i++)
3760  {
3761  index = column_indices[i];
3762 
3763  column = arrange_column(index, row_indices);
3764 
3765  statistics[i] = column.calculate_statistics();
3766  }
3767 
3768  return statistics;
3769 }
3770 
3771 
3772 // Vector< Statistics<T> > calculate_rows_statistics(const Vector<size_t>&) const method
3773 
3778 
3779 template <class T>
3781 {
3782  const size_t row_indices_size = row_indices.size();
3783 
3784  Vector< Statistics<T> > statistics(columns_number);
3785 
3786  Vector<T> column(row_indices_size);
3787 
3788  for(size_t i = 0; i < columns_number; i++)
3789  {
3790  column = arrange_column(i, row_indices);
3791 
3792  statistics[i] = column.calculate_statistics();
3793  }
3794 
3795  return statistics;
3796 }
3797 
3798 
3799 // Vector< Statistics<T> > calculate_rows_statistics_missing_values(const Vector<size_t>&, const Vector< Vector<size_t> >&) const method
3800 
3806 
3807 template <class T>
3809 {
3810  const size_t row_indices_size = row_indices.size();
3811 
3812  Vector< Statistics<T> > statistics(columns_number);
3813 
3814  Vector<T> column(row_indices_size);
3815 
3816  for(size_t i = 0; i < columns_number; i++)
3817  {
3818  column = arrange_column(i, row_indices);
3819 
3820  statistics[i] = column.calculate_statistics_missing_values(missing_indices[i]);
3821  }
3822 
3823  return statistics;
3824 }
3825 
3826 
3827 // Vector< Statistics<T> > calculate_columns_statistics(const Vector<size_t>&) const method
3828 
3833 
3834 template <class T>
3836 {
3837  const size_t column_indices_size = column_indices.size();
3838 
3839  Vector< Statistics<T> > statistics(column_indices_size);
3840 
3841  size_t index;
3842  Vector<T> column(rows_number);
3843 
3844  for(size_t i = 0; i < column_indices_size; i++)
3845  {
3846  index = column_indices[i];
3847 
3848  column = arrange_column(index);
3849 
3850  statistics[i] = column.calculate_statistics();
3851  }
3852 
3853  return statistics;
3854 }
3855 
3856 
3857 // Vector< Statistics<T> > calculate_columns_statistics_missing_values(const Vector<size_t>&) const method
3858 
3864 
3865 template <class T>
3867 {
3868  const size_t column_indices_size = column_indices.size();
3869 
3870  Vector< Statistics<T> > statistics(column_indices_size);
3871 
3872  size_t index;
3873  Vector<T> column(rows_number);
3874 
3875  for(size_t i = 0; i < column_indices_size; i++)
3876  {
3877  index = column_indices[i];
3878 
3879  column = arrange_column(index);
3880 
3881  statistics[i] = column.calculate_statistics_missing_values(missing_indices[index]);
3882  }
3883 
3884  return statistics;
3885 }
3886 
3887 
3888 // Vector<Histogram<T> > calculate_histograms(const size_t&) const method
3889 
3895 
3896 template <class T>
3897 Vector< Histogram<T> > Matrix<T>::calculate_histograms(const size_t& bins_number) const
3898 {
3899  Vector< Histogram<T> > histograms(columns_number);
3900 
3901  Vector<T> column(rows_number);
3902 
3903  for(size_t i = 0; i < columns_number; i++)
3904  {
3905  column = arrange_column(i);
3906 
3907  histograms[i] = column.calculate_histogram(bins_number);
3908  }
3909 
3910  return(histograms);
3911 }
3912 
3913 
3914 // Vector<Histogram<T> > calculate_histograms_missing_values(const Vector<size_t>&, const size_t&) const method
3915 
3922 
3923 template <class T>
3924 Vector< Histogram<T> > Matrix<T>::calculate_histograms_missing_values(const Vector< Vector<size_t> >& missing_indices, const size_t& bins_number) const
3925 {
3926  Vector< Histogram<T> > histograms(columns_number);
3927 
3928  Vector<T> column(rows_number);
3929 
3930  for(size_t i = 0; i < columns_number; i++)
3931  {
3932  column = arrange_column(i);
3933 
3934  histograms[i] = column.calculate_histogram_missing_values(missing_indices[i], bins_number);
3935  }
3936 
3937  return(histograms);
3938 }
3939 
3940 
3941 // Matrix<size_t> calculate_less_than_indices(const T&) const method
3942 
3945 
3946 template <class T>
3948 {
3949  Matrix<size_t> indices;
3950 
3951  Vector<size_t> row(2);
3952 
3953  for(size_t i = 0; i < rows_number; i++)
3954  {
3955  for(size_t j = 0; j < columns_number; j++)
3956  {
3957  if((*this)(i,j) < value && indices.empty())
3958  {
3959  indices.set(1, 2);
3960 
3961  row[0] = i;
3962  row[1] = j;
3963 
3964  indices.set_row(0, row);
3965  }
3966  else if((*this)(i,j) < value)
3967  {
3968  row[0] = i;
3969  row[1] = j;
3970 
3971  indices.append_row(row);
3972  }
3973  }
3974  }
3975 
3976  return(indices);
3977 }
3978 
3979 
3980 // Matrix<size_t> calculate_greater_than_indices(const T&) const method
3981 
3984 
3985 template <class T>
3987 {
3988  Matrix<size_t> indices;
3989 
3990  Vector<size_t> row(2);
3991 
3992  for(size_t i = 0; i < rows_number; i++)
3993  {
3994  for(size_t j = 0; j < columns_number; j++)
3995  {
3996  if((*this)(i,j) > value && indices.empty())
3997  {
3998  indices.set(1, 2);
3999 
4000  row[0] = i;
4001  row[1] = j;
4002 
4003  indices.set_row(0, row);
4004  }
4005  else if((*this)(i,j) > value)
4006  {
4007  row[0] = i;
4008  row[1] = j;
4009 
4010  indices.append_row(row);
4011  }
4012  }
4013  }
4014 
4015  return(indices);
4016 }
4017 
4018 
4019 
4020 // void scale_mean_standard_deviation(const Vector< Statistics<T> >&) method
4021 
4026 
4027 template <class T>
4029 {
4030  #ifndef NDEBUG
4031 
4032  const size_t size = statistics.size();
4033 
4034  if(size != columns_number)
4035  {
4036  std::ostringstream buffer;
4037 
4038  buffer << "OpenNN Exception: Matrix template."
4039  << "void scale_mean_standard_deviation(const Vector< Statistics<T> >&) const method.\n"
4040  << "Size of statistics vector must be equal to number of columns.\n";
4041 
4042  throw std::logic_error(buffer.str());
4043  }
4044 
4045  #endif
4046 
4047  // Rescale data
4048 
4049  for(size_t j = 0; j < columns_number; j++)
4050  {
4051  if(statistics[j].standard_deviation < 1e-99)
4052  {
4053  // Do nothing
4054  }
4055  else
4056  {
4057  for(size_t i = 0; i < rows_number; i++)
4058  {
4059  (*this)(i,j) = ((*this)(i,j) - statistics[j].mean)/statistics[j].standard_deviation;
4060  }
4061  }
4062  }
4063 }
4064 
4065 
4066 // Vector< Statistics<T> > scale_mean_standard_deviation(void) method
4067 
4071 
4072 template <class T>
4074 {
4075  const Vector< Statistics<T> > statistics = calculate_statistics();
4076 
4077  scale_mean_standard_deviation(statistics);
4078 
4079  return(statistics);
4080 }
4081 
4082 
4083 // void scale_rows_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) const
4084 
4088 
4089 template <class T>
4091 {
4092  // Control sentence (if debug)
4093 
4094  #ifndef NDEBUG
4095 
4096  const size_t statistics_size = statistics.size();
4097 
4098  if(statistics_size != columns_number)
4099  {
4100  std::ostringstream buffer;
4101 
4102  buffer << "OpenNN Exception: Vector template.\n"
4103  << "void scale_rows_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4104  << "Size of statistics must be equal to number of columns.\n";
4105 
4106  throw std::logic_error(buffer.str());
4107  }
4108 
4109  #endif
4110 
4111  size_t row_index;
4112 
4113  // Scale columns
4114 
4115  for(size_t j = 0; j < columns_number; j++)
4116  {
4117  if(statistics[j].standard_deviation < 1e-99)
4118  {
4119  // Do nothing
4120  }
4121  else
4122  {
4123  for(size_t i = 0; i < row_indices.size(); i++)
4124  {
4125  row_index = row_indices[i];
4126 
4127  (*this)(row_index,j) = ((*this)(row_index,j) - statistics[j].mean)/statistics[j].standard_deviation;
4128  }
4129  }
4130  }
4131 }
4132 
4133 
4134 // void scale_columns_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) method
4135 
4141 
4142 template <class T>
4144 {
4145  const size_t columns_indices_size = columns_indices.size();
4146 
4147  // Control sentence (if debug)
4148 
4149  #ifndef NDEBUG
4150 
4151  const size_t statistics_size = statistics.size();
4152 
4153  if(statistics_size != columns_indices_size)
4154  {
4155  std::ostringstream buffer;
4156 
4157  buffer << "OpenNN Exception: Vector template.\n"
4158  << "void scale_columns_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4159  << "Size of statistics must be equal to size of columns indices.\n";
4160 
4161  throw std::logic_error(buffer.str());
4162  }
4163 
4164  #endif
4165 
4166  size_t column_index;
4167 
4168  // Scale columns
4169 
4170  for(size_t j = 0; j < columns_indices_size; j++)
4171  {
4172  if(statistics[j].standard_deviation < 1e-99)
4173  {
4174  // Do nothing
4175  }
4176  else
4177  {
4178  column_index = columns_indices[j];
4179 
4180  for(size_t i = 0; i < rows_number; i++)
4181  {
4182  (*this)(i,column_index) = ((*this)(i,column_index) - statistics[j].mean)/statistics[j].standard_deviation;
4183  }
4184  }
4185  }
4186 }
4187 
4188 
4189 // void scale_minimum_maximum(const Vector< Statistics<T> >&) method
4190 
4195 
4196 template <class T>
4198 {
4199  #ifndef NDEBUG
4200 
4201  const size_t size = statistics.size();
4202 
4203  if(size != columns_number)
4204  {
4205  std::ostringstream buffer;
4206 
4207  buffer << "OpenNN Exception: Matrix template."
4208  << "void scale_minimum_maximum(const Vector< Statistics<T> >&) method.\n"
4209  << "Size of statistics vector must be equal to number of columns.\n";
4210 
4211  throw std::logic_error(buffer.str());
4212  }
4213 
4214  #endif
4215 
4216  // Rescale data
4217 
4218  for(size_t j = 0; j < columns_number; j++)
4219  {
4220  if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4221  {
4222  // Do nothing
4223  }
4224  else
4225  {
4226  for(size_t i = 0; i < rows_number; i++)
4227  {
4228  (*this)(i,j) = 2.0*((*this)(i,j) - statistics[j].minimum)/(statistics[j].maximum-statistics[j].minimum)-1.0;
4229  }
4230  }
4231  }
4232 }
4233 
4234 
4235 // Vector< Statistics<T> > scale_minimum_maximum(void) method
4236 
4240 
4241 template <class T>
4243 {
4244  const Vector< Statistics<T> > statistics = calculate_statistics();
4245 
4246  scale_minimum_maximum(statistics);
4247 
4248  return(statistics);
4249 }
4250 
4251 
4252 // void scale_rows_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&)
4253 
4257 
4258 template <class T>
4259 void Matrix<T>::scale_rows_minimum_maximum(const Vector< Statistics<T> >& statistics, const Vector<size_t>& row_indices)
4260 {
4261  // Control sentence (if debug)
4262 
4263  const size_t row_indices_size = row_indices.size();
4264 
4265  #ifndef NDEBUG
4266 
4267  const size_t statistics_size = statistics.size();
4268 
4269  if(statistics_size != columns_number)
4270  {
4271  std::ostringstream buffer;
4272 
4273  buffer << "OpenNN Exception: Vector template.\n"
4274  << "void scale_rows_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4275  << "Size of statistics must be equal to number of columns.\n";
4276 
4277  throw std::logic_error(buffer.str());
4278  }
4279 
4280  #endif
4281 
4282  // Rescale targets data
4283 
4284  size_t row_index;
4285 
4286  for(size_t j = 0; j < columns_number; j++)
4287  {
4288  if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4289  {
4290  // Do nothing
4291  }
4292  else
4293  {
4294  for(size_t i = 0; i < row_indices_size; i++)
4295  {
4296  row_index = row_indices[i];
4297 
4298  (*this)(row_index,j) = 2.0*((*this)(row_index,j) - statistics[j].minimum)/(statistics[j].maximum-statistics[j].minimum) - 1.0;
4299  }
4300  }
4301  }
4302 }
4303 
4304 
4305 // void scale_columns_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&) method
4306 
4312 
4313 template <class T>
4314 void Matrix<T>::scale_columns_minimum_maximum(const Vector< Statistics<T> >& statistics, const Vector<size_t>& column_indices)
4315 {
4316  // Control sentence (if debug)
4317 
4318  const size_t column_indices_size = column_indices.size();
4319 
4320  #ifndef NDEBUG
4321 
4322  const size_t statistics_size = statistics.size();
4323 
4324  if(statistics_size != column_indices_size)
4325  {
4326  std::ostringstream buffer;
4327 
4328  buffer << "OpenNN Exception: Vector template.\n"
4329  << "void scale_columns_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4330  << "Size of statistics must be equal to size of columns indices.\n";
4331 
4332  throw std::logic_error(buffer.str());
4333  }
4334 
4335  #endif
4336 
4337  size_t column_index;
4338 
4339  // Rescale targets data
4340 
4341  for(size_t j = 0; j < column_indices_size; j++)
4342  {
4343  column_index = column_indices[j];
4344 
4345  if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4346  {
4347  // Do nothing
4348  }
4349  else
4350  {
4351  for(size_t i = 0; i < rows_number; i++)
4352  {
4353  (*this)(i,column_index) = 2.0*((*this)(i,column_index) - statistics[j].minimum)/(statistics[j].maximum-statistics[j].minimum) - 1.0;
4354  }
4355  }
4356  }
4357 }
4358 
4359 
4360 // void unscale_mean_standard_deviation(const Vector< Statistics<T> >&) method
4361 
4366 
4367 template <class T>
4369 {
4370  #ifndef NDEBUG
4371 
4372  const size_t size = statistics.size();
4373 
4374  if(size != columns_number)
4375  {
4376  std::ostringstream buffer;
4377 
4378  buffer << "OpenNN Exception: Matrix template."
4379  << "void unscale_mean_standard_deviation(const Vector< Statistics<T> >&) const method.\n"
4380  << "Size of statistics vector must be equal to number of columns.\n";
4381 
4382  throw std::logic_error(buffer.str());
4383  }
4384 
4385  #endif
4386 
4387  for(size_t j = 0; j < columns_number; j++)
4388  {
4389  if(statistics[j].standard_deviation < 1e-99)
4390  {
4391  // Do nothing
4392  }
4393  else
4394  {
4395  for(size_t i = 0; i < rows_number; i++)
4396  {
4397  (*this)(i,j) = (*this)(i,j)*statistics[j].standard_deviation + statistics[j].mean;
4398  }
4399  }
4400  }
4401 }
4402 
4403 
4404 // void unscale_rows_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) method
4405 
4410 
4411 template <class T>
4413 {
4414  size_t row_index;
4415 
4416  // Unscale columns
4417 
4418  for(size_t j = 0; j < columns_number; j++)
4419  {
4420  if(statistics[j].standard_deviation < 1e-99)
4421  {
4422  // Do nothing
4423  }
4424  else
4425  {
4426  for(size_t i = 0; i < rows_number; i++)
4427  {
4428  row_index = row_indices[i];
4429 
4430  (*this)(row_index,j) = (*this)(row_index,j)*statistics[j].standard_deviation + statistics[j].mean;
4431  }
4432  }
4433  }
4434 }
4435 
4436 
4437 // void unscale_columns_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) method
4438 
4444 
4445 template <class T>
4447 {
4448  #ifndef NDEBUG
4449 
4450  if(statistics.size() != columns_number)
4451  {
4452  std::ostringstream buffer;
4453 
4454  buffer << "OpenNN Exception: Matrix template.\n"
4455  << "void unscale_columns_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) const method.\n"
4456  << "Size of statistics vector must be equal to number of columns.\n";
4457 
4458  throw std::logic_error(buffer.str());
4459  }
4460 
4461  #endif
4462 
4463  size_t column_index;
4464 
4465  // Unscale columns
4466 
4467  for(size_t j = 0; j < column_indices.size(); j++)
4468  {
4469  column_index = column_indices[j];
4470 
4471  if(statistics[column_index].standard_deviation < 1e-99)
4472  {
4473  // Do nothing
4474  }
4475  else
4476  {
4477  for(size_t i = 0; i < rows_number; i++)
4478  {
4479  (*this)(i,column_index) = (*this)(i,column_index)*statistics[column_index].standard_deviation + statistics[column_index].mean;
4480  }
4481  }
4482  }
4483 }
4484 
4485 
4486 // void unscale_minimum_maximum(const Vector< Statistics<T> >&) method
4487 
4491 
4492 template <class T>
4494 {
4495  #ifndef NDEBUG
4496 
4497  const size_t size = statistics.size();
4498 
4499  if(size != columns_number)
4500  {
4501  std::ostringstream buffer;
4502 
4503  buffer << "OpenNN Exception: Matrix template."
4504  << "void unscale_minimum_maximum(const Vector< Statistics<T> >&) method.\n"
4505  << "Size of minimum vector must be equal to number of columns.\n";
4506 
4507  throw std::logic_error(buffer.str());
4508  }
4509 
4510  #endif
4511 
4512  for(size_t j = 0; j < columns_number; j++)
4513  {
4514  if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4515  {
4516  std::cout << "OpenNN Warning: Matrix template.\n"
4517  << "void unscale_minimum_maximum(const Vector< Statistics<T> >&) const method.\n"
4518  << "Minimum and maximum values of column " << j << " are equal.\n"
4519  << "Those columns won't be unscaled.\n";
4520 
4521  // Do nothing
4522  }
4523  else
4524  {
4525  for(size_t i = 0; i < rows_number; i++)
4526  {
4527  (*this)(i,j) = 0.5*((*this)(i,j) + 1.0)*(statistics[j].maximum-statistics[j].minimum) + statistics[j].minimum;
4528  }
4529  }
4530  }
4531 }
4532 
4533 
4534 // void unscale_rows_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&) method
4535 
4540 
4541 template <class T>
4542 void Matrix<T>::unscale_rows_minimum_maximum(const Vector< Statistics<T> >& statistics, const Vector<size_t>& row_indices)
4543 {
4544  size_t row_index;
4545 
4546  // Unscale rows
4547 
4548  for(size_t j = 0; j < columns_number; j++)
4549  {
4550  if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4551  {
4552  // Do nothing
4553  }
4554  else
4555  {
4556  for(size_t i = 0; i < rows_number; i++)
4557  {
4558  row_index = row_indices[i];
4559 
4560  (*this)(row_index,j) = 0.5*((*this)(row_index,j) + 1.0)*(statistics[j].maximum-statistics[j].minimum)
4561  + statistics[j].minimum;
4562  }
4563  }
4564  }
4565 }
4566 
4567 
4568 // void unscale_columns_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&) method
4569 
4575 
4576 template <class T>
4577 void Matrix<T>::unscale_columns_minimum_maximum(const Vector< Statistics<T> >& statistics, const Vector<size_t>& column_indices)
4578 {
4579  #ifndef NDEBUG
4580 
4581  if(statistics.size() != columns_number)
4582  {
4583  std::ostringstream buffer;
4584 
4585  buffer << "OpenNN Exception: Matrix template.\n"
4586  << "void unscale_columns_minimum_maximum_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) const method.\n"
4587  << "Size of statistics vector must be equal to number of columns.\n";
4588 
4589  throw std::logic_error(buffer.str());
4590  }
4591 
4592  #endif
4593 
4594  size_t column_index;
4595 
4596  // Unscale columns
4597 
4598  for(size_t j = 0; j < column_indices.size(); j++)
4599  {
4600  column_index = column_indices[j];
4601 
4602  if(statistics[column_index].maximum - statistics[column_index].minimum < 1e-99)
4603  {
4604  // Do nothing
4605  }
4606  else
4607  {
4608  for(size_t i = 0; i < rows_number; i++)
4609  {
4610  (*this)(i,column_index) = 0.5*((*this)(i,column_index) + 1.0)*(statistics[column_index].maximum-statistics[column_index].minimum)
4611  + statistics[column_index].minimum;
4612  }
4613  }
4614  }
4615 }
4616 
4617 
4618 // Vector<size_t> calculate_minimal_indices(void) const method
4619 
4621 
4622 template <class T>
4624 {
4625  T minimum = (*this)(0,0);
4626  Vector<size_t> minimal_indices(2, 0);
4627 
4628  for(size_t i = 0; i < rows_number; i++)
4629  {
4630  for(size_t j = 0; j < columns_number; j++)
4631  {
4632  if((*this)(i,j) < minimum)
4633  {
4634  minimum = (*this)(i,j);
4635  minimal_indices[0] = i;
4636  minimal_indices[1] = j;
4637  }
4638  }
4639  }
4640 
4641  return(minimal_indices);
4642 }
4643 
4644 
4645 // Vector<size_t> calculate_maximal_indices(void) const method
4646 
4648 
4649 template <class T>
4651 {
4652  T maximum = (*this)(0,0);
4653 
4654  Vector<size_t> maximal_indices(2, 0);
4655 
4656  for(size_t i = 0; i < rows_number; i++)
4657  {
4658  for(size_t j = 0; j < columns_number; j++)
4659  {
4660  if((*this)(i,j) > maximum)
4661  {
4662  maximum = (*this)(i,j);
4663  maximal_indices[0] = i;
4664  maximal_indices[1] = j;
4665  }
4666  }
4667  }
4668 
4669  return(maximal_indices);
4670 }
4671 
4672 
4673 // Vector< Vector<size_t> > calculate_minimal_maximal_indices(void) const method
4674 
4679 
4680 template <class T>
4682 {
4683  T minimum = (*this)(0,0);
4684  T maximum = (*this)(0,0);
4685 
4686  Vector<size_t> minimal_indices(2, 0);
4687  Vector<size_t> maximal_indices(2, 0);
4688 
4689  for(size_t i = 0; i < rows_number; i++)
4690  {
4691  for(size_t j = 0; j < columns_number; j++)
4692  {
4693  if((*this)(i,j) < minimum)
4694  {
4695  minimum = (*this)(i,j);
4696  minimal_indices[0] = i;
4697  minimal_indices[1] = j;
4698  }
4699 
4700  if((*this)(i,j) > maximum)
4701  {
4702  maximum = (*this)(i,j);
4703  maximal_indices[0] = i;
4704  maximal_indices[1] = j;
4705  }
4706  }
4707  }
4708 
4709  Vector< Vector<size_t> > minimal_maximal_indices(2);
4710  minimal_maximal_indices[0] = minimal_indices;
4711  minimal_maximal_indices[1] = maximal_indices;
4712 
4713  return(minimal_maximal_indices);
4714 }
4715 
4716 
4717 // double calculate_sum_squared_error(const Matrix<double>&) const method
4718 
4721 
4722 template <class T>
4724 {
4725  // Control sentence (if debug)
4726 
4727  #ifndef NDEBUG
4728 
4729  const size_t other_rows_number = other_matrix.get_rows_number();
4730 
4731  if(other_rows_number != rows_number)
4732  {
4733  std::ostringstream buffer;
4734 
4735  buffer << "OpenNN Exception: Matrix Template.\n"
4736  << "double calculate_sum_squared_error(const Matrix<double>&) const method.\n"
4737  << "Other number of rows must be equal to this number of rows.\n";
4738 
4739  throw std::logic_error(buffer.str());
4740  }
4741 
4742  const size_t other_columns_number = other_matrix.get_columns_number();
4743 
4744  if(other_columns_number != columns_number)
4745  {
4746  std::ostringstream buffer;
4747 
4748  buffer << "OpenNN Exception: Matrix Template.\n"
4749  << "double calculate_sum_squared_error(const Matrix<double>&) const method.\n"
4750  << "Other number of columns must be equal to this number of columns.\n";
4751 
4752  throw std::logic_error(buffer.str());
4753  }
4754 
4755  #endif
4756 
4757 
4758  double sum_squared_error = 0.0;
4759 
4760  for(size_t i = 0; i < rows_number; i++)
4761  {
4762  sum_squared_error += ((*this)[i] - other_matrix[i])*((*this)[i] - other_matrix[i]);
4763  }
4764 
4765  return(sum_squared_error);
4766 }
4767 
4768 
4769 // double calculate_sum_squared_error(const Vector<double>&) const method
4770 
4774 
4775 template <class T>
4777 {
4778  // Control sentence (if debug)
4779 
4780  #ifndef NDEBUG
4781 
4782  const size_t size = vector.size();
4783 
4784  if(size != columns_number)
4785  {
4786  std::ostringstream buffer;
4787 
4788  buffer << "OpenNN Exception: Matrix Template.\n"
4789  << "double calculate_sum_squared_error(const Vector<double>&) const method.\n"
4790  << "Size must be equal to number of columns.\n";
4791 
4792  throw std::logic_error(buffer.str());
4793  }
4794 
4795  #endif
4796 
4797  double sum_squared_error = 0.0;
4798 
4799  for(size_t i = 0; i < rows_number; i++)
4800  {
4801  for(size_t j = 0; j < columns_number; j++)
4802  {
4803  sum_squared_error += ((*this)(i,j) - vector[j])*((*this)(i,j) - vector[j]);
4804  }
4805  }
4806 
4807  return(sum_squared_error);
4808 }
4809 
4810 
4811 // Vector<double> calculate_rows_norm(void) const method
4812 
4815 
4816 template <class T>
4818 {
4819  Vector<double> rows_norm(rows_number, 0.0);
4820 
4821  for(size_t i = 0; i < rows_number; i++)
4822  {
4823  for(size_t j = 0; j < columns_number; j++)
4824  {
4825  rows_norm[i] += (*this)(i,j)*(*this)(i,j);
4826  }
4827 
4828  rows_norm[i] = sqrt(rows_norm[i]);
4829  }
4830 
4831  return(rows_norm);
4832 }
4833 
4834 
4835 // Matrix<T> calculate_absolute_value(void) const method
4836 
4838 
4839 template <class T>
4841 {
4842  Matrix<T> absolute_value(rows_number, columns_number);
4843 
4844  for(size_t i = 0; i < this->size(); i++)
4845  {
4846  if((*this)[i] > 0)
4847  {
4848  absolute_value[i] = (*this)[i];
4849  }
4850  else
4851  {
4852  absolute_value[i] = -(*this)[i];
4853  }
4854  }
4855 
4856  return(absolute_value);
4857 }
4858 
4859 
4860 
4861 // Matrix<T> calculate_transpose(void) const method
4862 
4864 
4865 template <class T>
4867 {
4868  Matrix<T> transpose(columns_number, rows_number);
4869 
4870  for(size_t i = 0; i < columns_number; i++)
4871  {
4872  for(size_t j = 0; j < rows_number; j++)
4873  {
4874  transpose(i,j) = (*this)(j,i);
4875  }
4876  }
4877 
4878  return(transpose);
4879 }
4880 
4881 
4882 // Type calculate_determinant(void) const method
4883 
4885 
4886 template <class T>
4888 {
4889  // Control sentence (if debug)
4890 
4891  #ifndef NDEBUG
4892 
4893  if(empty())
4894  {
4895  std::ostringstream buffer;
4896 
4897  buffer << "OpenNN Exception: Matrix Template.\n"
4898  << "calculate_determinant(void) const method.\n"
4899  << "Matrix is empty.\n";
4900 
4901  throw std::logic_error(buffer.str());
4902  }
4903 
4904  if(rows_number != columns_number)
4905  {
4906  std::ostringstream buffer;
4907 
4908  buffer << "OpenNN Exception: Matrix Template.\n"
4909  << "calculate_determinant(void) const method.\n"
4910  << "Matrix must be square.\n";
4911 
4912  throw std::logic_error(buffer.str());
4913  }
4914 
4915  #endif
4916 
4917  T determinant = 0;
4918 
4919  if(rows_number == 1)
4920  {
4921  determinant = (*this)(0,0);
4922  }
4923  else if(rows_number == 2)
4924  {
4925  determinant = (*this)(0,0)*(*this)(1,1) - (*this)(1,0)*(*this)(0,1);
4926  }
4927  else
4928  {
4929  int sign;
4930 
4931  for(size_t row_index = 0; row_index < rows_number; row_index++)
4932  {
4933  // Calculate sub data
4934 
4935  Matrix<T> sub_matrix(rows_number-1, columns_number-1);
4936 
4937  for(size_t i = 1; i < rows_number; i++)
4938  {
4939  size_t j2 = 0;
4940 
4941  for(size_t j = 0; j < columns_number; j++)
4942  {
4943  if(j == row_index)
4944  {
4945  continue;
4946  }
4947 
4948  sub_matrix(i-1,j2) = (*this)(i,j);
4949 
4950  j2++;
4951  }
4952  }
4953 
4954  //sign = (size_t)(pow(-1.0, row_index+2.0));
4955 
4956  sign = static_cast<int>( (((row_index + 2) % 2) == 0) ? 1 : -1 );
4957 
4958  determinant += sign*(*this)(0,row_index)*sub_matrix.calculate_determinant();
4959  }
4960  }
4961 
4962  return(determinant);
4963 }
4964 
4965 
4966 // Matrix<T> calculate_cofactor(void) const method
4967 
4969 
4970 template <class T>
4972 {
4973  Matrix<T> cofactor(rows_number, columns_number);
4974 
4975  Matrix<T> c(rows_number-1, columns_number-1);
4976 
4977  for(size_t j = 0; j < rows_number; j++)
4978  {
4979  for(size_t i = 0; i < rows_number; i++)
4980  {
4981  // Form the adjoint a(i,j)
4982 
4983  size_t i1 = 0;
4984 
4985  for(size_t ii = 0; ii < rows_number; ii++)
4986  {
4987  if(ii == i)
4988  {
4989  continue;
4990  }
4991 
4992  size_t j1 = 0;
4993 
4994  for(size_t jj = 0; jj < rows_number; jj++)
4995  {
4996  if(jj == j)
4997  {
4998  continue;
4999  }
5000 
5001  c(i1,j1) = (*this)(ii,jj);
5002  j1++;
5003  }
5004  i1++;
5005  }
5006 
5007  const double determinant = c.calculate_determinant();
5008 
5009  cofactor(i,j) = static_cast<T>((((i + j) % 2) == 0) ? 1 : -1)*determinant;
5010  //cofactor(i,j) = pow(-1.0, i+j+2.0)*determinant;
5011  }
5012  }
5013 
5014  return(cofactor);
5015 }
5016 
5017 
5018 // Matrix<T> calculate_inverse(void) const method
5019 
5022 
5023 template <class T>
5025 {
5026  // Control sentence (if debug)
5027 
5028  #ifndef NDEBUG
5029 
5030  if(empty())
5031  {
5032  std::ostringstream buffer;
5033 
5034  buffer << "OpenNN Exception: Matrix Template.\n"
5035  << "calculate_inverse(void) const method.\n"
5036  << "Matrix is empty.\n";
5037 
5038  throw std::logic_error(buffer.str());
5039  }
5040 
5041  if(rows_number != columns_number)
5042  {
5043  std::ostringstream buffer;
5044 
5045  buffer << "OpenNN Exception: Matrix Template.\n"
5046  << "calculate_inverse(void) const method.\n"
5047  << "Matrix must be square.\n";
5048 
5049  throw std::logic_error(buffer.str());
5050  }
5051 
5052  #endif
5053 
5054  const double determinant = calculate_determinant();
5055 
5056  if(determinant == 0.0)
5057  {
5058  std::ostringstream buffer;
5059 
5060  buffer << "OpenNN Exception: Matrix Template.\n"
5061  << "calculate_inverse(void) const method.\n"
5062  << "Matrix is singular.\n";
5063 
5064  throw std::logic_error(buffer.str());
5065  }
5066 
5067  if(rows_number == 1)
5068  {
5069  Matrix<T> inverse(1, 1, 1.0/determinant);
5070 
5071  return(inverse);
5072  }
5073 
5074  // Calculate cofactor matrix
5075 
5076  const Matrix<T> cofactor = calculate_cofactor();
5077 
5078  // Adjoint matrix is the transpose of cofactor matrix
5079 
5080  const Matrix<T> adjoint = cofactor.calculate_transpose();
5081 
5082  // Inverse matrix is adjoint matrix divided by matrix determinant
5083 
5084  const Matrix<T> inverse = adjoint/determinant;
5085 
5086  return(inverse);
5087 }
5088 
5089 
5090 // Matrix<T> operator + (const T&) const method
5091 
5094 
5095 template <class T>
5096 Matrix<T> Matrix<T>::operator + (const T& scalar) const
5097 {
5098  Matrix<T> sum(rows_number, columns_number);
5099 
5100  std::transform(this->begin(), this->end(), sum.begin(), std::bind2nd(std::plus<T>(), scalar));
5101 
5102  return(sum);
5103 }
5104 
5105 
5106 // Matrix<T> operator + (const Vector<T>&) const method
5107 
5110 
5111 template <class T>
5113 {
5114  // Control sentence (if debug)
5115 
5116  #ifndef NDEBUG
5117 
5118  const size_t size = vector.size();
5119 
5120  if(size != rows_number)
5121  {
5122  std::ostringstream buffer;
5123 
5124  buffer << "OpenNN Exception: Matrix Template.\n"
5125  << "Matrix<T> operator + (const Vector<T>&) const.\n"
5126  << "Size of vector must be equal to number of rows.\n";
5127 
5128  throw std::logic_error(buffer.str());
5129  }
5130 
5131  #endif
5132 
5133  Matrix<T> sum(rows_number, columns_number);
5134 
5135  for(size_t i = 0; i < rows_number; i++)
5136  {
5137  for(size_t j = 0; j < columns_number; j++)
5138  {
5139  sum(i,j) = (*this)(i,j) + vector[i];
5140  }
5141  }
5142 
5143  return(sum);
5144 }
5145 
5146 
5147 // Matrix<T> operator + (const Matrix<T>&) const method
5148 
5151 
5152 template <class T>
5153 Matrix<T> Matrix<T>::operator + (const Matrix<T>& other_matrix) const
5154 {
5155  // Control sentence (if debug)
5156 
5157  #ifndef NDEBUG
5158 
5159  const size_t other_rows_number = other_matrix.get_rows_number();
5160  const size_t other_columns_number = other_matrix.get_columns_number();
5161 
5162  if(other_rows_number != rows_number || other_columns_number != columns_number)
5163  {
5164  std::ostringstream buffer;
5165 
5166  buffer << "OpenNN Exception: Matrix Template.\n"
5167  << "Matrix<T> operator + (const Matrix<T>&) const.\n"
5168  << "Sizes of other matrix (" << other_rows_number << "," << other_columns_number << ") must be the same than sizes of this matrix (" << rows_number << "," << columns_number << ").\n";
5169 
5170  throw std::logic_error(buffer.str());
5171  }
5172 
5173  #endif
5174 
5175  Matrix<T> sum(rows_number, columns_number);
5176 
5177  std::transform(this->begin(), this->end(), other_matrix.begin(), sum.begin(), std::plus<T>());
5178 
5179  return(sum);
5180 }
5181 
5182 
5183 // Matrix<T> operator - (const T&) const method
5184 
5187 
5188 template <class T>
5189 Matrix<T> Matrix<T>::operator - (const T& scalar) const
5190 {
5191  Matrix<T> difference(rows_number, columns_number);
5192 
5193  std::transform( this->begin(), this->end(), difference.begin(), std::bind2nd(std::minus<T>(), scalar));
5194 
5195  return(difference);
5196 }
5197 
5198 
5199 // Matrix<T> operator - (const Vector<T>&) const method
5200 
5203 
5204 template <class T>
5206 {
5207  // Control sentence (if debug)
5208 
5209  #ifndef NDEBUG
5210 
5211  const size_t size = vector.size();
5212 
5213  if(size != rows_number)
5214  {
5215  std::ostringstream buffer;
5216 
5217  buffer << "OpenNN Exception: Matrix Template.\n"
5218  << "Matrix<T> operator - (const Vector<T>&) const.\n"
5219  << "Size of vector must be equal to number of rows.\n";
5220 
5221  throw std::logic_error(buffer.str());
5222  }
5223 
5224  #endif
5225 
5226  Matrix<T> difference(rows_number, columns_number);
5227 
5228  for(size_t i = 0; i < rows_number; i++)
5229  {
5230  for(size_t j = 0; j < columns_number; j++)
5231  {
5232  difference(i,j) = (*this)(i,j) - vector[i];
5233  }
5234  }
5235 
5236  return(difference);
5237 }
5238 
5239 
5240 // Matrix<T> operator - (const Matrix<T>&) const method
5241 
5244 
5245 template <class T>
5246 Matrix<T> Matrix<T>::operator - (const Matrix<T>& other_matrix) const
5247 {
5248  // Control sentence (if debug)
5249 
5250  #ifndef NDEBUG
5251 
5252  const size_t other_rows_number = other_matrix.get_rows_number();
5253  const size_t other_columns_number = other_matrix.get_columns_number();
5254 
5255  if(other_rows_number != rows_number || other_columns_number != columns_number)
5256  {
5257  std::ostringstream buffer;
5258 
5259  buffer << "OpenNN Exception: Matrix Template.\n"
5260  << "Matrix<T> operator - (const Matrix<T>&) const method.\n"
5261  << "Sizes of other matrix (" << other_rows_number << "," << other_columns_number << ") must be equal to sizes of this matrix ("<< rows_number << "," << columns_number <<").\n";
5262 
5263  throw std::logic_error(buffer.str());
5264  }
5265 
5266  #endif
5267 
5268  Matrix<T> difference(rows_number, columns_number);
5269 
5270  std::transform( this->begin(), this->end(), other_matrix.begin(), difference.begin(), std::minus<T>());
5271 
5272  return(difference);
5273 }
5274 
5275 
5276 // Matrix<T> operator * (const T&) const method
5277 
5280 
5281 template <class T>
5282 Matrix<T> Matrix<T>::operator * (const T& scalar) const
5283 {
5284  Matrix<T> product(rows_number, columns_number);
5285 
5286  for(size_t i = 0; i < this->size(); i++)
5287  {
5288  product[i] = (*this)[i]*scalar;
5289  }
5290 
5291  return(product);
5292 }
5293 
5294 
5295 // Matrix<T> operator * (const Vector<T>&) const method
5296 
5299 
5300 template <class T>
5302 {
5303  // Control sentence (if debug)
5304 
5305  #ifndef NDEBUG
5306 
5307  const size_t size = vector.size();
5308 
5309  if(size != rows_number)
5310  {
5311  std::ostringstream buffer;
5312 
5313  buffer << "OpenNN Exception: Matrix Template.\n"
5314  << "Matrix<T> operator * (const Vector<T>&) const method.\n"
5315  << "Vector size (" << size << ") must be equal to number of matrix rows (" << rows_number << ").\n";
5316 
5317  throw std::logic_error(buffer.str());
5318  }
5319 
5320  #endif
5321 
5322  Matrix<T> product(rows_number, columns_number);
5323 
5324  for(size_t i = 0; i < rows_number; i++)
5325  {
5326  for(size_t j = 0; j < columns_number; j++)
5327  {
5328  product(i,j) = (*this)(i,j)*vector[i];
5329  }
5330  }
5331 
5332  return(product);
5333 }
5334 
5335 
5336 // Matrix<T> operator * (const Matrix<T>&) const method
5337 
5340 
5341 template <class T>
5342 Matrix<T> Matrix<T>::operator * (const Matrix<T>& other_matrix) const
5343 {
5344  // Control sentence (if debug)
5345 
5346  #ifndef NDEBUG
5347 
5348  const size_t other_rows_number = other_matrix.get_rows_number();
5349  const size_t other_columns_number = other_matrix.get_columns_number();
5350 
5351  if(other_rows_number != rows_number || other_columns_number != columns_number)
5352  {
5353  std::ostringstream buffer;
5354 
5355  buffer << "OpenNN Exception: Matrix Template.\n"
5356  << "Matrix<T> operator * (const Matrix<T>&) const method.\n"
5357  << "Sizes of other matrix (" << other_rows_number << "," << other_columns_number << ") must be equal to sizes of this matrix (" << rows_number << "," << columns_number << ").\n";
5358 
5359  throw std::logic_error(buffer.str());
5360  }
5361 
5362  #endif
5363 
5364  Matrix<T> product(rows_number, columns_number);
5365 
5366  for(size_t i = 0; i < this->size(); i++)
5367  {
5368  product[i] = (*this)[i]*other_matrix[i];
5369  }
5370 
5371  return(product);
5372 }
5373 
5374 
5375 // Matrix<T> operator / (const T&) const method
5376 
5379 
5380 template <class T>
5381 Matrix<T> Matrix<T>::operator / (const T& scalar) const
5382 {
5383  Matrix<T> results(rows_number, columns_number);
5384 
5385  for(size_t i = 0; i < results.size(); i++)
5386  {
5387  results[i] = (*this)[i]/scalar;
5388  }
5389 
5390  return(results);
5391 }
5392 
5393 
5394 // Matrix<T> operator / (const Vector<T>&) const method
5395 
5398 
5399 template <class T>
5401 {
5402  // Control sentence (if debug)
5403 
5404  #ifndef NDEBUG
5405 
5406  const size_t size = vector.size();
5407 
5408  if(size != columns_number)
5409  {
5410  std::ostringstream buffer;
5411 
5412  buffer << "OpenNN Exception: Matrix Template.\n"
5413  << "Matrix<T> operator / (const Vector<T>&) const.\n"
5414  << "Size of vector must be equal to number of columns.\n";
5415 
5416  throw std::logic_error(buffer.str());
5417  }
5418 
5419  #endif
5420 
5421  Matrix<T> cocient(rows_number, columns_number);
5422 
5423  for(size_t i = 0; i < rows_number; i++)
5424  {
5425  for(size_t j = 0; j < columns_number; j++)
5426  {
5427  cocient(i,j) = (*this)(i,j)/vector[j];
5428  }
5429  }
5430 
5431  return(cocient);
5432 }
5433 
5434 
5435 // Matrix<T> operator / (const Matrix<T>&) const method
5436 
5439 
5440 template <class T>
5441 Matrix<T> Matrix<T>::operator / (const Matrix<T>& other_matrix) const
5442 {
5443  // Control sentence (if debug)
5444 
5445  #ifndef NDEBUG
5446 
5447  const size_t other_rows_number = other_matrix.get_rows_number();
5448  const size_t other_columns_number = other_matrix.get_columns_number();
5449 
5450  if(other_rows_number != rows_number || other_columns_number != columns_number)
5451  {
5452  std::ostringstream buffer;
5453 
5454  buffer << "OpenNN Exception: Matrix Template.\n"
5455  << "Matrix<T> operator / (const Matrix<T>&) const method.\n"
5456  << "Both matrix sizes must be the same.\n";
5457 
5458  throw std::logic_error(buffer.str());
5459  }
5460 
5461  #endif
5462 
5463  Matrix<T> cocient(rows_number, columns_number);
5464 
5465  for(size_t i = 0; i < rows_number; i++)
5466  {
5467  cocient[i] = (*this)[i]/other_matrix[i];
5468  }
5469 
5470  return(cocient);
5471 }
5472 
5473 
5474 // void operator += (const T&)
5475 
5478 
5479 template <class T>
5480 void Matrix<T>::operator += (const T& value)
5481 {
5482  for(size_t i = 0; i < rows_number; i++)
5483  {
5484  for(size_t j = 0; j < columns_number; j++)
5485  {
5486  (*this)(i,j) += value;
5487  }
5488  }
5489 }
5490 
5491 
5492 // void operator += (const Matrix<T>&)
5493 
5496 
5497 template <class T>
5498 void Matrix<T>::operator += (const Matrix<T>& other_matrix)
5499 {
5500  // Control sentence (if debug)
5501 
5502  #ifndef NDEBUG
5503 
5504  const size_t other_rows_number = other_matrix.get_rows_number();
5505 
5506  if(other_rows_number != rows_number)
5507  {
5508  std::ostringstream buffer;
5509 
5510  buffer << "OpenNN Exception: Matrix Template.\n"
5511  << "void operator += (const Matrix<T>&).\n"
5512  << "Both numbers of rows must be the same.\n";
5513 
5514  throw std::logic_error(buffer.str());
5515  }
5516 
5517  const size_t other_columns_number = other_matrix.get_columns_number();
5518 
5519  if(other_columns_number != columns_number)
5520  {
5521  std::ostringstream buffer;
5522 
5523  buffer << "OpenNN Exception: Matrix Template.\n"
5524  << "void operator += (const Matrix<T>&).\n"
5525  << "Both numbers of columns must be the same.\n";
5526 
5527  throw std::logic_error(buffer.str());
5528  }
5529 
5530  #endif
5531 
5532  for(size_t i = 0; i < rows_number; i++)
5533  {
5534  for(size_t j = 0; j < columns_number; j++)
5535  {
5536  (*this)(i,j) += other_matrix(i,j);
5537  }
5538  }
5539 }
5540 
5541 
5542 // void operator -= (const T&)
5543 
5546 
5547 template <class T>
5548 void Matrix<T>::operator -= (const T& value)
5549 {
5550  for(size_t i = 0; i < rows_number; i++)
5551  {
5552  for(size_t j = 0; j < columns_number; j++)
5553  {
5554  (*this)(i,j) -= value;
5555  }
5556  }
5557 }
5558 
5559 
5560 // void operator -= (const Matrix<T>&)
5561 
5564 
5565 template <class T>
5566 void Matrix<T>::operator -= (const Matrix<T>& other_matrix)
5567 {
5568  // Control sentence (if debug)
5569 
5570  #ifndef NDEBUG
5571 
5572  const size_t other_rows_number = other_matrix.get_rows_number();
5573 
5574  if(other_rows_number != rows_number)
5575  {
5576  std::ostringstream buffer;
5577 
5578  buffer << "OpenNN Exception: Matrix Template.\n"
5579  << "void operator -= (const Matrix<T>&).\n"
5580  << "Both numbers of rows must be the same.\n";
5581 
5582  throw std::logic_error(buffer.str());
5583  }
5584 
5585  const size_t other_columns_number = other_matrix.get_columns_number();
5586 
5587  if(other_columns_number != columns_number)
5588  {
5589  std::ostringstream buffer;
5590 
5591  buffer << "OpenNN Exception: Matrix Template.\n"
5592  << "void operator -= (const Matrix<T>&).\n"
5593  << "Both numbers of columns must be the same.\n";
5594 
5595  throw std::logic_error(buffer.str());
5596  }
5597 
5598  #endif
5599 
5600  for(size_t i = 0; i < rows_number; i++)
5601  {
5602  for(size_t j = 0; j < columns_number; j++)
5603  {
5604  (*this)(i,j) -= other_matrix(i,j);
5605  }
5606  }
5607 }
5608 
5609 
5610 // void operator *= (const T&)
5611 
5614 
5615 template <class T>
5616 void Matrix<T>::operator *= (const T& value)
5617 {
5618  for(size_t i = 0; i < rows_number; i++)
5619  {
5620  for(size_t j = 0; j < columns_number; j++)
5621  {
5622  (*this)(i,j) *= value;
5623  }
5624  }
5625 }
5626 
5627 
5628 // void operator *= (const Matrix<T>&)
5629 
5632 
5633 template <class T>
5634 void Matrix<T>::operator *= (const Matrix<T>& other_matrix)
5635 {
5636  // Control sentence (if debug)
5637 
5638  #ifndef NDEBUG
5639 
5640  const size_t other_rows_number = other_matrix.get_rows_number();
5641 
5642  const size_t rows_number = get_rows_number();
5643 
5644  if(other_rows_number != rows_number)
5645  {
5646  std::ostringstream buffer;
5647 
5648  buffer << "OpenNN Exception: Matrix Template.\n"
5649  << "void operator *= (const Matrix<T>&).\n"
5650  << "The number of rows in the other matrix (" << other_rows_number << ")"
5651  << " is not equal to the number of rows in this matrix (" << rows_number << ").\n";
5652 
5653  throw std::logic_error(buffer.str());
5654  }
5655 
5656  #endif
5657 
5658  for(size_t i = 0; i < rows_number; i++)
5659  {
5660  for(size_t j = 0; j < columns_number; j++)
5661  {
5662  (*this)(i,j) *= other_matrix(i,j);
5663  }
5664  }
5665 }
5666 
5667 
5668 // void operator /= (const T&)
5669 
5672 
5673 template <class T>
5674 void Matrix<T>::operator /= (const T& value)
5675 {
5676  for(size_t i = 0; i < rows_number; i++)
5677  {
5678  for(size_t j = 0; j < columns_number; j++)
5679  {
5680  (*this)(i,j) /= value;
5681  }
5682  }
5683 }
5684 
5685 
5686 // void operator /= (const Matrix<T>&)
5687 
5690 
5691 template <class T>
5692 void Matrix<T>::operator /= (const Matrix<T>& other_matrix)
5693 {
5694  // Control sentence (if debug)
5695 
5696  #ifndef NDEBUG
5697 
5698  const size_t other_rows_number = other_matrix.get_rows_number();
5699 
5700  if(other_rows_number != rows_number)
5701  {
5702  std::ostringstream buffer;
5703 
5704  buffer << "OpenNN Exception: Matrix Template.\n"
5705  << "void operator /= (const Matrix<T>&).\n"
5706  << "Both numbers of rows must be the same.\n";
5707 
5708  throw std::logic_error(buffer.str());
5709  }
5710 
5711  const size_t other_columns_number = other_matrix.get_columns_number();
5712 
5713  if(other_columns_number != columns_number)
5714  {
5715  std::ostringstream buffer;
5716 
5717  buffer << "OpenNN Exception: Matrix Template.\n"
5718  << "void operator /= (const Matrix<T>&).\n"
5719  << "Both numbers of columns must be the same.\n";
5720 
5721  throw std::logic_error(buffer.str());
5722  }
5723 
5724  #endif
5725 
5726  for(size_t i = 0; i < rows_number; i++)
5727  {
5728  for(size_t j = 0; j < columns_number; j++)
5729  {
5730  (*this)(i,j) /= other_matrix(i,j);
5731  }
5732  }
5733 }
5734 
5735 
5736 // void sum_diagonal(const T&) method
5737 /*
5738 template <class T>
5739 void Matrix<T>::sum_diagonal(const T& value)
5740 {
5741  // Control sentence (if debug)
5742 
5743  #ifndef NDEBUG
5744 
5745  if(!is_square())
5746  {
5747  std::ostringstream buffer;
5748 
5749  buffer << "OpenNN Exception: Matrix Template.\n"
5750  << "void sum_diagonal(const T&) method.\n"
5751  << "Matrix must be squared.\n";
5752 
5753  throw std::logic_error(buffer.str());
5754  }
5755 
5756  #endif
5757 
5758  for(size_t i = 0; i < rows_number; i++)
5759  {
5760  (*this)(i,i) += value;
5761  }
5762 }
5763 */
5764 
5765 // Vector<double> dot(const Vector<double>&) const method
5766 
5770 
5771 template <class T>
5773 {
5774  // Control sentence (if debug)
5775 
5776  #ifndef NDEBUG
5777 
5778  const size_t size = vector.size();
5779 
5780  if(size != columns_number)
5781  {
5782  std::ostringstream buffer;
5783 
5784  buffer << "OpenNN Exception: Matrix Template.\n"
5785  << "Vector<T> dot(const Vector<T>&) const method.\n"
5786  << "Vector size must be equal to matrix number of columns.\n";
5787 
5788  throw std::logic_error(buffer.str());
5789  }
5790 
5791  #endif
5792 
5793  // Calculate matrix-vector poduct
5794 
5795  Vector<double> product(rows_number);
5796 
5797 // for(size_t i = 0; i < rows_number; i++)
5798 // {
5799 // product[i] = 0;
5800 
5801 // for(size_t j = 0; j < columns_number; j++)
5802 // {
5803 // product[i] += vector[j]*(*this)(i,j);
5804 // }
5805 // }
5806 
5807  const Eigen::Map<Eigen::MatrixXd> matrix_eigen((double*)this->data(), rows_number, columns_number);
5808  const Eigen::Map<Eigen::VectorXd> vector_eigen((double*)vector.data(), columns_number);
5809  Eigen::Map<Eigen::VectorXd> product_eigen(product.data(), rows_number);
5810 
5811  product_eigen = matrix_eigen*vector_eigen;
5812 
5813  return(product);
5814 }
5815 
5816 
5817 // Matrix<double> dot(const Matrix<double>&) const method
5818 
5821 
5822 template <class T>
5824 {
5825  const size_t other_columns_number = other_matrix.get_columns_number();
5826  const size_t other_rows_number = other_matrix.get_rows_number();
5827 
5828  // Control sentence (if debug)
5829 
5830  #ifndef NDEBUG
5831 
5832  if(other_rows_number != columns_number)
5833  {
5834  std::ostringstream buffer;
5835 
5836  buffer << "OpenNN Exception: Matrix Template.\n"
5837  << "Matrix<T> dot(const Matrix<T>&) const method.\n"
5838  << "The number of rows of the other matrix (" << other_rows_number << ") must be equal to the number of columns of this matrix (" << columns_number << ").\n";
5839 
5840  throw std::logic_error(buffer.str());
5841  }
5842 
5843  #endif
5844 
5845  Matrix<T> product(rows_number, other_columns_number);
5846 
5847 // for(size_t i = 0; i < rows_number; i++) {
5848 // for(size_t j = 0; j < other_columns_number; j++) {
5849 // for(size_t k = 0; k < columns_number; k++) {
5850 // product(i,j) += (*this)(i,k)*other_matrix(k,j);
5851 // }
5852 // }
5853 // }
5854 
5855  const Eigen::Map<Eigen::MatrixXd> this_eigen((double*)this->data(), rows_number, columns_number);
5856  const Eigen::Map<Eigen::MatrixXd> other_eigen((double*)other_matrix.data(), other_rows_number, other_columns_number);
5857  Eigen::Map<Eigen::MatrixXd> product_eigen(product.data(), rows_number, other_columns_number);
5858 
5859  product_eigen = this_eigen*other_eigen;
5860 
5861  return(product);
5862 }
5863 
5864 
5865 // Matrix<T> direct(const Matrix<T>&) const method
5866 
5870 
5871 template <class T>
5872 Matrix<T> Matrix<T>::direct(const Matrix<T>& other_matrix) const
5873 {
5874  const size_t other_rows_number = other_matrix.get_rows_number();
5875  const size_t other_columns_number = other_matrix.get_columns_number();
5876 
5877  Matrix<T> direct(rows_number*other_rows_number, columns_number*other_columns_number);
5878 
5879  size_t alpha;
5880  size_t beta;
5881 
5882  for(size_t i = 0; i < rows_number; i++)
5883  {
5884  for(size_t j = 0; j < columns_number; j++)
5885  {
5886  for(size_t k = 0; k < other_rows_number; k++)
5887  {
5888  for(size_t l = 0; l < other_columns_number; l++)
5889  {
5890  alpha = other_rows_number*i+k;
5891  beta = other_columns_number*j+l;
5892 
5893  direct(alpha,beta) = (*this)(i,j)*other_matrix(k,l);
5894  }
5895  }
5896  }
5897  }
5898 
5899  return(direct);
5900 }
5901 
5902 
5903 // bool empty(void) const method
5904 
5906 
5907 template <class T>
5908 bool Matrix<T>::empty(void) const
5909 {
5910  if(rows_number == 0 && columns_number == 0)
5911  {
5912  return(true);
5913  }
5914  else
5915  {
5916  return(false);
5917  }
5918 }
5919 
5920 
5921 // bool is_square(void) const method
5922 
5925 
5926 template <class T>
5927 bool Matrix<T>::is_square(void) const
5928 {
5929  if(rows_number == columns_number)
5930  {
5931  return(true);
5932  }
5933  else
5934  {
5935  return(false);
5936  }
5937 }
5938 
5939 
5940 // bool is_symmetric(void) const method
5941 
5944 
5945 template <class T>
5946 bool Matrix<T>::is_symmetric(void) const
5947 {
5948  // Control sentence (if debug)
5949 
5950  #ifndef NDEBUG
5951 
5952  if(rows_number != columns_number)
5953  {
5954  std::ostringstream buffer;
5955 
5956  buffer << "OpenNN Exception: Matrix Template.\n"
5957  << "bool is_symmetric(void) const method.\n"
5958  << "Matrix must be squared.\n";
5959 
5960  throw std::logic_error(buffer.str());
5961  }
5962 
5963  #endif
5964 
5965  const Matrix<T> transpose = calculate_transpose();
5966 
5967  if((*this) == transpose)
5968  {
5969  return(true);
5970  }
5971  else
5972  {
5973  return(false);
5974  }
5975 }
5976 
5977 
5978 // bool is_antisymmetric(void) const method
5979 
5982 
5983 template <class T>
5985 {
5986  // Control sentence (if debug)
5987 
5988  #ifndef NDEBUG
5989 
5990  if(rows_number != columns_number)
5991  {
5992  std::ostringstream buffer;
5993 
5994  buffer << "OpenNN Exception: Matrix Template.\n"
5995  << "bool is_antisymmetric(void) const method.\n"
5996  << "Matrix must be squared.\n";
5997 
5998  throw std::logic_error(buffer.str());
5999  }
6000 
6001  #endif
6002 
6003  const Matrix<T> transpose = calculate_transpose();
6004 
6005  if((*this) == transpose*(-1))
6006  {
6007  return(true);
6008  }
6009  else
6010  {
6011  return(false);
6012  }
6013 }
6014 
6015 
6016 // bool is_diagonal(void) const method
6017 
6020 
6021 template <class T>
6022 bool Matrix<T>::is_diagonal(void) const
6023 {
6024  // Control sentence (if debug)
6025 
6026  #ifndef NDEBUG
6027 
6028  if(rows_number != columns_number)
6029  {
6030  std::ostringstream buffer;
6031 
6032  buffer << "OpenNN Exception: Matrix Template.\n"
6033  << "bool is_diagonal(void) const method.\n"
6034  << "Matrix must be squared.\n";
6035 
6036  throw std::logic_error(buffer.str());
6037  }
6038 
6039  #endif
6040 
6041  for(size_t i = 0; i < rows_number; i++)
6042  {
6043  for(size_t j = 0; j < columns_number; j++)
6044  {
6045  if(i != j && (*this)(i,j) != 0)
6046  {
6047  return(false);
6048  }
6049  }
6050  }
6051 
6052  return(true);
6053 }
6054 
6055 
6056 // bool is_scalar(void) const method
6057 
6060 
6061 template <class T>
6062 bool Matrix<T>::is_scalar(void) const
6063 {
6064  // Control sentence (if debug)
6065 
6066  #ifndef NDEBUG
6067 
6068  if(rows_number != columns_number)
6069  {
6070  std::ostringstream buffer;
6071 
6072  buffer << "OpenNN Exception: Matrix Template.\n"
6073  << "bool is_scalar(void) const method.\n"
6074  << "Matrix must be squared.\n";
6075 
6076  throw std::logic_error(buffer.str());
6077  }
6078 
6079  #endif
6080 
6081  // @todo
6082 
6083  return(false);
6084 }
6085 
6086 
6089 
6090 template <class T>
6091 bool Matrix<T>::is_identity(void) const
6092 {
6093  // Control sentence (if debug)
6094 
6095  #ifndef NDEBUG
6096 
6097  if(rows_number != columns_number)
6098  {
6099  std::ostringstream buffer;
6100 
6101  buffer << "OpenNN Exception: Matrix Template.\n"
6102  << "bool is_unity(void) const method.\n"
6103  << "Matrix must be squared.\n";
6104 
6105  throw std::logic_error(buffer.str());
6106  }
6107 
6108  #endif
6109 
6110  for(size_t i = 0; i < rows_number; i++)
6111  {
6112  for(size_t j = 0; j < columns_number; j++)
6113  {
6114  if(i != j && (*this)(i,j) != 0)
6115  {
6116  return(false);
6117  }
6118  else if(i == j && (*this)(i,j) != 1)
6119  {
6120  return(false);
6121  }
6122  }
6123  }
6124 
6125  return(true);
6126 }
6127 
6128 
6129 // Matrix<T> filter(const size_t&, const T&, const T&) const method
6130 
6135 
6136 template <class T>
6137 Matrix<T> Matrix<T>::filter(const size_t& column_index, const T& minimum, const T& maximum) const
6138 {
6139  const Vector<T> column = arrange_column(column_index);
6140 
6141  const size_t new_rows_number = rows_number
6142  - column.count_less_than(minimum) - column.count_greater_than(maximum);
6143 
6144  Matrix<T> new_matrix(new_rows_number, columns_number);
6145 
6146  size_t row_index = 0;
6147 
6148  Vector<T> row(columns_number);
6149 
6150  for(size_t i = 0; i < rows_number; i++)
6151  {
6152  if((*this)(i,column_index) >= minimum && (*this)(i,column_index) <= maximum)
6153  {
6154  row = arrange_row(i);
6155 
6156  new_matrix.set_row(row_index, row);
6157 
6158  row_index++;
6159  }
6160  }
6161 
6162  return(new_matrix);
6163 }
6164 
6165 // void convert_time_series(const size_t&) method
6166 
6171 
6172 template <class T>
6173 void Matrix<T>::convert_time_series(const size_t& lags_number)
6174 {
6175  const size_t new_rows_number = rows_number - lags_number;
6176  const size_t new_columns_number = columns_number*(1 + lags_number);
6177 
6178  Matrix<T> new_matrix(new_rows_number, new_columns_number);
6179 
6180  Vector<T> row(rows_number);
6181 
6182  for(size_t i = 0; i < new_rows_number; i++)
6183  {
6184  row = arrange_row(i);
6185 
6186  for(size_t j = 1; j <= lags_number; j++)
6187  {
6188  row = row.assemble(arrange_row(i+j));
6189  }
6190 
6191  new_matrix.set_row(i, row);
6192  }
6193 
6194  set(new_matrix);
6195 }
6196 
6197 
6198 
6199 // void convert_autoassociation(void) method
6200 
6203 
6204 template <class T>
6206 {
6207  Matrix<T> copy(*this);
6208 
6209  set(copy.assemble_columns(copy));
6210 }
6211 
6212 
6213 // void convert_angular_variables_degrees(const size_t&) method
6214 
6218 
6219 template <class T>
6220 void Matrix<T>::convert_angular_variables_degrees(const size_t& column_index)
6221 {
6222  // Control sentence (if debug)
6223 
6224  #ifndef NDEBUG
6225 
6226  if(column_index >= columns_number)
6227  {
6228  std::ostringstream buffer;
6229 
6230  buffer << "OpenNN Exception: Matrix Template.\n"
6231  << "void convert_angular_variables_degrees(const size_t&) method.\n"
6232  << "Index of column (" << column_index << ") must be less than number of columns.\n";
6233 
6234  throw std::logic_error(buffer.str());
6235  }
6236 
6237  #endif
6238 
6239  const double pi = 4.0*atan(1.0);
6240 
6241  Vector<T> sin_angle(rows_number);
6242  Vector<T> cos_angle(rows_number);
6243 
6244  double angle_rad;
6245 
6246  for(size_t i = 0; i < rows_number; i++)
6247  {
6248  if((*this)(i,column_index) != -99.9)
6249  {
6250  angle_rad = pi*(*this)(i,column_index)/180.0;
6251 
6252  sin_angle[i] = sin(angle_rad);
6253  cos_angle[i] = cos(angle_rad);
6254  }
6255  else
6256  {
6257  sin_angle[i] = (T)-99.9;
6258  cos_angle[i] = (T)-99.9;
6259  }
6260  }
6261 
6262  set_column(column_index, sin_angle);
6263  insert_column(column_index+1, cos_angle);
6264 }
6265 
6266 
6267 // void convert_angular_variables_radians(const size_t&) method
6268 
6272 
6273 template <class T>
6274 void Matrix<T>::convert_angular_variables_radians(const size_t& column_index)
6275 {
6276  // Control sentence (if debug)
6277 
6278  #ifndef NDEBUG
6279 
6280  if(column_index >= columns_number)
6281  {
6282  std::ostringstream buffer;
6283 
6284  buffer << "OpenNN Exception: Matrix Template.\n"
6285  << "void convert_angular_variables_radians(const size_t&) method.\n"
6286  << "Index of column (" << column_index << ") must be less than number of columns.\n";
6287 
6288  throw std::logic_error(buffer.str());
6289  }
6290 
6291  #endif
6292 
6293  Vector<T> sin_angle(rows_number);
6294  Vector<T> cos_angle(rows_number);
6295 
6296  for(size_t i = 0; i < rows_number; i++)
6297  {
6298  sin_angle[i] = sin((*this)(i,column_index));
6299  cos_angle[i] = cos((*this)(i,column_index));
6300  }
6301 
6302  set_column(column_index, sin_angle);
6303  insert_column(column_index+1, cos_angle);
6304 }
6305 
6306 
6307 // void print(void) const method
6308 
6310 
6311 template <class T>
6312 void Matrix<T>::print(void) const
6313 {
6314  std::cout << *this;
6315 }
6316 
6317 
6318 // void load(const std::string&) method
6319 
6322 
6323 template <class T>
6324 void Matrix<T>::load(const std::string& file_name)
6325 {
6326  std::ifstream file(file_name.c_str());
6327 
6328  if(!file.is_open())
6329  {
6330  std::ostringstream buffer;
6331 
6332  buffer << "OpenNN Exception: Matrix template.\n"
6333  << "void load(const std::string&) method.\n"
6334  << "Cannot open matrix data file: " << file_name << "\n";
6335 
6336  throw std::logic_error(buffer.str());
6337  }
6338 
6339  if(file.peek() == std::ifstream::traits_type::eof())
6340  {
6341  //std::ostringstream buffer;
6342 
6343  //buffer << "OpenNN Exception: Matrix template.\n"
6344  // << "void load(const std::string&) method.\n"
6345  // << "Data file " << file_name << " is empty.\n";
6346 
6347  //throw std::logic_error(buffer.str());
6348 
6349  this->set();
6350 
6351  return;
6352  }
6353 
6354  //file.is
6355 
6356  // Set matrix sizes
6357 
6358  std::string line;
6359 
6360  std::getline(file, line);
6361 
6362  if(line.empty())
6363  {
6364  set();
6365  }
6366  else
6367  {
6368  std::istringstream buffer(line);
6369 
6370  std::istream_iterator<std::string> it(buffer);
6371  std::istream_iterator<std::string> end;
6372 
6373  const std::vector<std::string> results(it, end);
6374 
6375  const size_t new_columns_number = (size_t)results.size();
6376 
6377  size_t new_rows_number = 1;
6378 
6379  while(file.good())
6380  {
6381  getline(file, line);
6382 
6383  if(!line.empty())
6384  {
6385  new_rows_number++;
6386  }
6387  }
6388 
6389  set(new_rows_number, new_columns_number);
6390 
6391  // Clear file
6392 
6393  file.clear();
6394  file.seekg(0, std::ios::beg);
6395 
6396  for(size_t i = 0; i < rows_number; i++)
6397  {
6398  for(size_t j = 0; j < columns_number; j++)
6399  {
6400  file >> (*this)(i,j);
6401  }
6402  }
6403  }
6404 
6405  // Close file
6406 
6407  file.close();
6408 }
6409 
6410 
6411 // void save(const std::string&) const method
6412 
6415 
6416 template <class T>
6417 void Matrix<T>::save(const std::string& file_name) const
6418 {
6419  std::ofstream file(file_name.c_str());
6420 
6421  if(!file.is_open())
6422  {
6423  std::ostringstream buffer;
6424 
6425  buffer << "OpenNN Exception: Matrix template." << std::endl
6426  << "void save(const std::string) method." << std::endl
6427  << "Cannot open matrix data file." << std::endl;
6428 
6429  throw std::logic_error(buffer.str());
6430  }
6431 
6432  // Write file
6433 
6434  for(size_t i = 0; i < rows_number; i++)
6435  {
6436  for(size_t j = 0; j < columns_number; j++)
6437  {
6438  file << (*this)(i,j) << " ";
6439  }
6440 
6441  file << std::endl;
6442  }
6443 
6444  // Close file
6445 
6446  file.close();
6447 }
6448 
6449 
6450 // void save_csv(const std::string&) const method
6451 
6454 
6455 template <class T>
6456 void Matrix<T>::save_csv(const std::string& file_name) const
6457 {
6458  std::ofstream file(file_name.c_str());
6459 
6460  if(!file.is_open())
6461  {
6462  std::ostringstream buffer;
6463 
6464  buffer << "OpenNN Exception: Matrix template." << std::endl
6465  << "void save(const std::string) method." << std::endl
6466  << "Cannot open matrix data file." << std::endl;
6467 
6468  throw std::logic_error(buffer.str());
6469  }
6470 
6471  // Write file
6472 
6473  for(size_t j = 0; j < columns_number; j++)
6474  {
6475  file << "c" << j+1;
6476 
6477  if(j != columns_number-1)
6478  {
6479  file << ",";
6480  }
6481  }
6482 
6483  file << std::endl;
6484 
6485  for(size_t i = 0; i < rows_number; i++)
6486  {
6487  for(size_t j = 0; j < columns_number; j++)
6488  {
6489  file << (*this)(i,j);
6490 
6491  if(j != columns_number-1)
6492  {
6493  file << ",";
6494  }
6495  }
6496 
6497  file << std::endl;
6498  }
6499 
6500  // Close file
6501 
6502  file.close();
6503 }
6504 
6505 
6506 // void parse(const std::string&) method
6507 
6511 
6512 template <class T>
6513 void Matrix<T>::parse(const std::string& str)
6514 {
6515  if(str.empty())
6516  {
6517  set();
6518  }
6519  else
6520  {
6521  // Set matrix sizes
6522 
6523  std::istringstream str_buffer(str);
6524 
6525  std::string line;
6526 
6527  std::getline(str_buffer, line);
6528 
6529  std::istringstream line_buffer(line);
6530 
6531  std::istream_iterator<std::string> it(line_buffer);
6532  std::istream_iterator<std::string> end;
6533 
6534  const std::vector<std::string> results(it, end);
6535 
6536  const size_t new_columns_number = (size_t)results.size();
6537 
6538  size_t new_rows_number = 1;
6539 
6540  while(str_buffer.good())
6541  {
6542  getline(str_buffer, line);
6543 
6544  if(!line.empty())
6545  {
6546  new_rows_number++;
6547  }
6548  }
6549 
6550  set(new_rows_number, new_columns_number);
6551 
6552  // Clear file
6553 
6554  str_buffer.clear();
6555  str_buffer.seekg(0, std::ios::beg);
6556 
6557  for(size_t i = 0; i < rows_number; i++)
6558  {
6559  for(size_t j = 0; j < columns_number; j++)
6560  {
6561  str_buffer >> (*this)(i,j);
6562  }
6563  }
6564  }
6565 }
6566 
6567 
6568 // std::string to_string(const std::string&) const method
6569 
6573 
6574 template <class T>
6575 std::string Matrix<T>::to_string(const std::string& separator) const
6576 {
6577  std::ostringstream buffer;
6578 
6579  if(rows_number > 0 && columns_number > 0)
6580  {
6581  buffer << arrange_row(0).to_string(separator);
6582 
6583  for(size_t i = 1; i < rows_number; i++)
6584  {
6585  buffer << "\n"
6586  << arrange_row(i).to_string(separator);
6587  }
6588  }
6589 
6590  return(buffer.str());
6591 }
6592 
6593 
6594 // Matrix<std::string> write_string_matrix(const size_t&) const
6595 
6597 
6598 template <class T>
6600 {
6601  Matrix<std::string> string_matrix(rows_number, columns_number);
6602 
6603  std::ostringstream buffer;
6604 
6605  for(size_t i = 0; i < rows_number; i++)
6606  {
6607  for(size_t j = 0; j < columns_number; j++)
6608  {
6609  buffer.str("");
6610  buffer << std::setprecision(precision) << (*this)(i,j);
6611 
6612  string_matrix(i,j) = buffer.str();
6613  }
6614  }
6615 
6616  return(string_matrix);
6617 }
6618 
6619 
6620 // vector<T> to_std_vector(void) const
6621 
6625 
6626 template <class T>
6627 std::vector<T> Matrix<T>::to_std_vector(void) const
6628 {
6629  const std::vector<T> std_vector((*this).begin(), (*this).end());
6630 
6631  return(std_vector);
6632 }
6633 
6634 
6635 // Vector<T> to_vector(void) const
6636 
6640 
6641 template <class T>
6643 {
6644  Vector<T> vector(rows_number*columns_number);
6645 
6646  for(size_t i = 0; i < rows_number*columns_number; i++)
6647  {
6648  vector[i] = (*this)[i];
6649  }
6650 
6651  return(vector);
6652 }
6653 
6654 
6655 // void print_preview(void) const method
6656 
6659 
6660 template <class T>
6662 {
6663  std::cout << "Rows number: " << rows_number << std::endl
6664  << "Columns number: " << columns_number << std::endl;
6665 
6666  if(rows_number > 0)
6667  {
6668  const Vector<T> first_row = arrange_row(0);
6669 
6670  std::cout << "Row 0:\n" << first_row << std::endl;
6671  }
6672 
6673  if(rows_number > 1)
6674  {
6675  const Vector<T> second_row = arrange_row(1);
6676 
6677  std::cout << "Row 1:\n" << second_row << std::endl;
6678  }
6679 
6680  if(rows_number > 2)
6681  {
6682  const Vector<T> last_row = arrange_row(rows_number-1);
6683 
6684  std::cout << "Row " << rows_number << ":\n" << last_row << std::endl;
6685  }
6686 }
6687 
6688 
6692 
6693 template<class T>
6694 std::istream& operator >> (std::istream& is, Matrix<T>& m)
6695 {
6696  const size_t rows_number = m.get_rows_number();
6697  const size_t columns_number = m.get_columns_number();
6698 
6699  for(size_t i = 0; i < rows_number; i++)
6700  {
6701  for(size_t j = 0; j < columns_number; j++)
6702  {
6703  is >> m(i,j);
6704  }
6705  }
6706 
6707  return(is);
6708 }
6709 
6710 
6711 // Output operator
6712 
6716 
6717 template<class T>
6718 std::ostream& operator << (std::ostream& os, const Matrix<T>& m)
6719 {
6720  const size_t rows_number = m.get_rows_number();
6721  const size_t columns_number = m.get_columns_number();
6722 
6723  if(rows_number > 0 && columns_number > 0)
6724  {
6725  os << m.arrange_row(0);
6726 
6727  for(size_t i = 1; i < rows_number; i++)
6728  {
6729  os << "\n"
6730  << m.arrange_row(i);
6731  }
6732  }
6733 
6734  return(os);
6735 }
6736 
6737 
6738 // Output operator
6739 
6743 
6744 template<class T>
6745 std::ostream& operator << (std::ostream& os, const Matrix< Vector<T> >& m)
6746 {
6747  const size_t rows_number = m.get_rows_number();
6748  const size_t columns_number = m.get_columns_number();
6749 
6750  for(size_t i = 0; i < rows_number; i++)
6751  {
6752  for(size_t j = 0; j < columns_number; j++)
6753  {
6754  os << "subvector_" << i << "_" << j << "\n"
6755  << m(i,j) << std::endl;
6756  }
6757  }
6758 
6759  return(os);
6760 }
6761 
6762 
6763 // Output operator
6764 
6768 
6769 template<class T>
6770 std::ostream& operator << (std::ostream& os, const Matrix< Matrix<T> >& m)
6771 {
6772  const size_t rows_number = m.get_rows_number();
6773  const size_t columns_number = m.get_columns_number();
6774 
6775  for(size_t i = 0; i < rows_number; i++)
6776  {
6777  for(size_t j = 0; j < columns_number; j++)
6778  {
6779  os << "submatrix_" << i << "_" << j << "\n"
6780  << m(i,j);
6781  }
6782  }
6783 
6784  return(os);
6785 }
6786 
6787 } // end namespace
6788 
6789 #endif
6790 
6791 
6792 // OpenNN: Open Neural Networks Library.
6793 // Copyright (c) 2005-2015 Roberto Lopez.
6794 //
6795 // This library is free software; you can redistribute it and/or
6796 // modify it under the terms of the GNU Lesser General Public
6797 // License as published by the Free Software Foundation; either
6798 // version 2.1 of the License, or any later version.
6799 //
6800 // This library is distributed in the hope that it will be useful,
6801 // but WITHOUT ANY WARRANTY; without even the implied warranty of
6802 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6803 // Lesser General Public License for more details.
6804 
6805 // You should have received a copy of the GNU Lesser General Public
6806 // License along with this library; if not, write to the Free Software
6807 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
6808 
Vector< Statistics< T > > calculate_rows_statistics_missing_values(const Vector< size_t > &, const Vector< Vector< size_t > > &) const
Definition: matrix.h:3808
Vector< double > calculate_mean(void) const
Definition: matrix.h:2859
size_t count_less_than(const T &) const
Definition: vector.h:1171
Vector< T > arrange_column(const size_t &) const
Definition: matrix.h:1580
void scale_columns_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4143
Vector< size_t > calculate_minimal_indices(void) const
Returns the row and column indices corresponding to the entry with minimum value. ...
Definition: matrix.h:4623
Vector< Histogram< T > > calculate_histograms_missing_values(const Vector< Vector< size_t > > &, const size_t &=10) const
Definition: matrix.h:3924
bool is_scalar(void) const
Definition: matrix.h:6062
T calculate_maximum(void) const
Returns the maximum value from all elements in the matrix.
Definition: matrix.h:3475
Vector< Statistics< T > > calculate_statistics_missing_values(const Vector< Vector< size_t > > &) const
Definition: matrix.h:3694
void insert_row(const size_t &, const Vector< T > &)
Definition: matrix.h:2156
void tuck_in(const size_t &, const size_t &, const Matrix< T > &)
Definition: matrix.h:1284
Matrix< T > direct(const Matrix< T > &) const
Definition: matrix.h:5872
bool operator>=(const Matrix< T > &) const
Definition: matrix.h:934
void scale_rows_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4090
void print(void) const
Prints to the screen in the matrix object.
Definition: matrix.h:6312
double calculate_trace(void) const
Definition: matrix.h:2823
Vector< Histogram< T > > calculate_histograms(const size_t &=10) const
Definition: matrix.h:3897
bool operator>(const Matrix< T > &) const
Definition: matrix.h:788
void set_columns_number(const size_t &)
Definition: matrix.h:1267
void set_rows_number(const size_t &)
Definition: matrix.h:1252
Vector< T > get_diagonal(void) const
Returns the diagonal of the matrix.
Definition: matrix.h:1654
Matrix< T > calculate_cofactor(void) const
Returns the cofactor matrix.
Definition: matrix.h:4971
T & operator()(const size_t &, const size_t &)
Reference operator.
Definition: matrix.h:572
void append_column(const Vector< T > &)
Definition: matrix.h:2123
double calculate_sum_squared_error(const Matrix< double > &) const
Definition: matrix.h:4723
void save(const std::string &) const
Definition: matrix.h:6417
Vector< size_t > calculate_maximal_indices(void) const
Returns the row and column indices corresponding to the entry with maximum value. ...
Definition: matrix.h:4650
bool is_diagonal(void) const
Definition: matrix.h:6022
void unscale_columns_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4446
void operator-=(const T &)
Definition: matrix.h:5548
Statistics< T > calculate_statistics_missing_values(const Vector< size_t > &) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
Definition: vector.h:2322
Matrix< T > operator+(const T &) const
Definition: matrix.h:5096
Vector< double > calculate_mean_missing_values(const Vector< Vector< size_t > > &) const
Definition: matrix.h:3096
void operator+=(const T &value)
Definition: matrix.h:5480
void insert_column(const size_t &, const Vector< T > &)
Definition: matrix.h:2223
std::string to_string(const std::string &=" ") const
Definition: matrix.h:6575
void unscale_rows_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4412
T calculate_minimum(void) const
Returns the minimum value from all elements in the matrix.
Definition: matrix.h:3454
Matrix< T > arrange_submatrix(const Vector< size_t > &, const Vector< size_t > &) const
Definition: matrix.h:1417
void initialize_diagonal(const size_t &, const T &)
Definition: matrix.h:1943
Matrix< std::string > write_string_matrix(const size_t &=3) const
Returns a new matrix in which each entry has been converted to a string.
Definition: matrix.h:6599
Vector< T > calculate_rows_sum(void) const
Returns the sum of all the rows in the matrix.
Definition: matrix.h:2752
Vector< Statistics< T > > calculate_rows_statistics(const Vector< size_t > &) const
Definition: matrix.h:3780
Vector< Vector< T > > calculate_minimum_maximum(void) const
Definition: matrix.h:3498
Matrix< T > calculate_absolute_value(void) const
Returns a matrix with the absolute values of this matrix.
Definition: matrix.h:4840
void load(const std::string &)
Definition: matrix.h:6324
void convert_angular_variables_radians(const size_t &)
Definition: matrix.h:6274
void set_diagonal(const T &)
Definition: matrix.h:1858
bool is_identity(void) const
Definition: matrix.h:6091
Matrix(void)
Default constructor. It creates a matrix with zero rows and zero columns.
Definition: matrix.h:413
size_t count_off_diagonal_elements(void) const
Definition: matrix.h:1372
Histogram< T > calculate_histogram(const size_t &=10) const
Definition: vector.h:1445
size_t count_greater_than(const T &) const
Definition: vector.h:1147
void scale_columns_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4314
Matrix< size_t > calculate_greater_than_indices(const T &) const
Definition: matrix.h:3986
Vector< T > assemble(const Vector< T > &) const
Definition: vector.h:5110
Matrix< T > arrange_submatrix_columns(const Vector< size_t > &) const
Definition: matrix.h:1477
void convert_angular_variables_degrees(const size_t &)
Definition: matrix.h:6220
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
Definition: matrix.h:1090
size_t columns_number
Number of columns in the matrix.
Definition: matrix.h:404
void unscale_rows_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4542
void convert_autoassociation(void)
Definition: matrix.h:6205
Matrix< size_t > calculate_less_than_indices(const T &) const
Definition: matrix.h:3947
Matrix< T > calculate_inverse(void) const
Definition: matrix.h:5024
Matrix< T > assemble_columns(const Matrix< T > &) const
Definition: matrix.h:2465
void subtract_column(const size_t &)
Definition: matrix.h:2339
Vector< Statistics< T > > calculate_statistics(void) const
Definition: matrix.h:3652
Vector< Vector< size_t > > calculate_minimal_maximal_indices(void) const
Definition: matrix.h:4681
void save_csv(const std::string &) const
Definition: matrix.h:6456
void parse(const std::string &)
Definition: matrix.h:6513
void unscale_mean_standard_deviation(const Vector< Statistics< T > > &)
Definition: matrix.h:4368
bool is_square(void) const
Definition: matrix.h:5927
void set_identity(const size_t &)
Definition: matrix.h:1239
double calculate_standard_deviation(void) const
Returns the standard deviation of the elements in the vector.
Definition: vector.h:2102
void initialize_identity(void)
Definition: matrix.h:2659
void set_column(const size_t &, const Vector< T > &)
Definition: matrix.h:1774
void initialize(const T &)
Definition: matrix.h:2510
Matrix< T > operator-(const T &scalar) const
Definition: matrix.h:5189
Vector< Vector< double > > calculate_mean_standard_deviation(void) const
Definition: matrix.h:3228
size_t count_diagonal_elements(void) const
Definition: matrix.h:1333
void convert_time_series(const size_t &)
Definition: matrix.h:6173
Matrix< T > operator*(const T &) const
Definition: matrix.h:5282
void sort_less(const size_t &)
Definition: matrix.h:2442
void set(void)
This method set the numbers of rows and columns of the matrix to zero.
Definition: matrix.h:1101
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
Definition: matrix.h:1079
void sort_greater(const size_t &)
Definition: matrix.h:2453
Matrix< T > calculate_transpose(void) const
Returns the transpose of the matrix.
Definition: matrix.h:4866
size_t rows_number
Number of rows in the matrix.
Definition: matrix.h:400
bool operator<(const Matrix< T > &) const
Definition: matrix.h:861
bool operator<=(const Matrix< T > &) const
Definition: matrix.h:1007
Vector< Statistics< T > > calculate_columns_statistics_missing_values(const Vector< size_t > &, const Vector< Vector< size_t > >) const
Definition: matrix.h:3866
void operator*=(const T &)
Definition: matrix.h:5616
void print_preview(void) const
Definition: matrix.h:6661
Histogram< T > calculate_histogram_missing_values(const Vector< size_t > &, const size_t &=10) const
Definition: vector.h:1533
void randomize_uniform(const double &=-1.0, const double &=1.0)
Definition: matrix.h:2524
void append_row(const Vector< T > &)
Definition: matrix.h:2080
Vector< double > calculate_rows_norm(void) const
Definition: matrix.h:4817
Vector< double > dot(const Vector< double > &) const
Definition: matrix.h:5772
Vector< Statistics< T > > scale_minimum_maximum(void)
Definition: matrix.h:4242
Statistics< T > calculate_statistics(void) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
Definition: vector.h:2285
bool operator!=(const Matrix< T > &) const
Definition: matrix.h:713
Matrix< T > assemble_rows(const Matrix< T > &) const
Definition: matrix.h:2394
Vector< Statistics< T > > scale_mean_standard_deviation(void)
Definition: matrix.h:4073
std::vector< T > to_std_vector(void) const
Definition: matrix.h:6627
bool is_antisymmetric(void) const
Definition: matrix.h:5984
void operator/=(const T &)
Definition: matrix.h:5674
bool is_symmetric(void) const
Definition: matrix.h:5946
double calculate_mean(void) const
Returns the mean of the elements in the vector.
Definition: vector.h:2068
void sum_row(const size_t &, const Vector< T > &)
Definition: matrix.h:2791
Matrix< T > filter(const size_t &, const T &, const T &) const
Definition: matrix.h:6137
void unscale_columns_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4577
Matrix< T > sum_diagonal(const T &) const
Definition: matrix.h:1992
bool empty(void) const
Returns true if number of rows and columns is zero.
Definition: matrix.h:5908
Vector< Statistics< T > > calculate_columns_statistics(const Vector< size_t > &) const
Definition: matrix.h:3835
Matrix< T > operator/(const T &) const
Definition: matrix.h:5381
Vector< T > to_vector(void) const
Definition: matrix.h:6642
void set_row(const size_t &, const Vector< T > &)
Definition: matrix.h:1691
void unscale_minimum_maximum(const Vector< Statistics< T > > &)
Definition: matrix.h:4493
Matrix< T > & operator=(const Matrix< T > &)
Definition: matrix.h:545
bool operator==(const Matrix< T > &) const
Definition: matrix.h:657
T calculate_sum(void) const
Returns the sum of all the elements in the matrix.
Definition: matrix.h:2734
virtual ~Matrix(void)
Destructor.
Definition: matrix.h:534
Vector< T > arrange_row(const size_t &) const
Definition: matrix.h:1505
Matrix< T > arrange_submatrix_rows(const Vector< size_t > &) const
Definition: matrix.h:1449
void scale_rows_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
Definition: matrix.h:4259
void subtract_row(const size_t &)
Definition: matrix.h:2283
void randomize_normal(const double &=0.0, const double &=1.0)
Definition: matrix.h:2593
T calculate_determinant(void) const
Returns the determinant of a square matrix.
Definition: matrix.h:4887