29 #include "opennn_strings.h"
51 explicit Matrix(
const size_t&,
const size_t&);
53 explicit Matrix(
const size_t&,
const size_t&,
const T&);
55 explicit Matrix(
const string&,
const char&,
const bool&);
61 Matrix(
const initializer_list<
Vector<T>>&,
const initializer_list<string>&);
71 inline T& operator()(
const size_t&,
const size_t&);
73 inline const T& operator()(
const size_t&,
const size_t&)
const;
75 inline T& operator()(
const size_t&,
const string&);
77 inline const T& operator()(
const size_t&,
const string&)
const;
79 bool operator == (
const Matrix<T>&)
const;
81 bool operator == (
const T&)
const;
83 bool operator != (
const Matrix<T>&)
const;
85 bool operator != (
const T& value)
const;
89 bool operator > (
const T& value)
const;
93 bool operator < (
const T& value)
const;
95 bool operator >= (
const Matrix<T>&)
const;
97 bool operator >= (
const T&)
const;
99 bool operator <= (
const Matrix<T>&)
const;
101 bool operator <= (
const T&)
const;
107 Matrix<T> operator - (
const T& scalar)
const;
123 void operator += (
const T& value);
125 void operator += (
const Matrix<T>& other_matrix);
127 void operator -= (
const T&);
131 void operator *= (
const T&);
135 void operator /= (
const T&);
141 const size_t& get_rows_number()
const;
143 const size_t& get_columns_number()
const;
147 const string get_header(
const size_t&)
const;
149 size_t get_column_index(
const string&)
const;
166 Vector<T> get_rows(
const size_t&,
const size_t&)
const;
170 Vector<T> get_column(
const size_t&)
const;
171 Vector<T> get_column(
const string&)
const;
179 T get_first(
const size_t&)
const;
180 T get_first(
const string&)
const;
182 T get_last(
const size_t&)
const;
183 T get_last(
const string&)
const;
187 Matrix<T> get_first_rows(
const size_t&)
const;
188 Matrix<T> get_first_columns(
const size_t&)
const;
189 Matrix<T> get_last_columns(
const size_t&)
const;
190 Matrix<T> get_last_rows(
const size_t&)
const;
196 void set(
const size_t&,
const size_t&);
198 void set(
const size_t&,
const size_t&,
const T&);
202 void set(
const string&);
204 void set(
const initializer_list<
Vector<T>>&);
206 void set_identity(
const size_t&);
208 void set_rows_number(
const size_t&);
210 void set_columns_number(
const size_t&);
214 void set_header(
const size_t&,
const string&);
216 void set_row(
const size_t&,
const Vector<T>&);
218 void set_row(
const size_t&,
const T&);
220 void set_submatrix_rows(
const size_t&,
const Matrix<T>&);
222 void set_column(
const size_t&,
const Vector<T>&,
const string& =
"");
223 void set_column(
const string&,
const Vector<T>&,
const string& =
"");
225 void set_column(
const string&,
const T&,
const string& =
"");
226 void set_column(
const size_t&,
const T&,
const string& =
"");
228 void set_diagonal(
const T&);
236 bool is_square()
const;
238 bool is_symmetric()
const;
240 bool is_antisymmetric()
const;
242 bool is_diagonal()
const;
244 bool is_scalar()
const;
246 bool is_identity()
const;
248 bool is_binary()
const;
250 bool is_column_binary(
const size_t&)
const;
252 bool is_column_constant(
const size_t&)
const;
254 bool is_positive()
const;
256 bool is_row_equal_to(
const size_t&,
const Vector<size_t>&,
const T&)
const;
258 bool has_column_value(
const size_t&,
const T&)
const;
262 size_t count_diagonal_elements()
const;
264 size_t count_off_diagonal_elements()
const;
266 size_t count_equal_to(
const T&)
const;
268 size_t count_equal_to(
const size_t&,
const T&)
const;
269 size_t count_equal_to(
const size_t&,
const Vector<T>&)
const;
271 size_t count_equal_to(
const size_t&,
const Vector<T>&,
272 const size_t&,
const T&)
const;
274 size_t count_equal_to(
const size_t&,
const Vector<T>&,
275 const size_t&,
const T&,
276 const size_t&,
const T&,
277 const size_t&,
const T&)
const;
279 size_t count_equal_to(
const size_t&,
const T&,
const size_t&,
const T&)
const;
280 size_t count_equal_to(
const size_t&,
const T&,
const size_t&,
const T&,
const size_t&,
const T&,
const size_t&,
const T&)
const;
282 size_t count_equal_to(
const string&,
const T&)
const;
283 size_t count_equal_to(
const string&,
const Vector<T>&)
const;
284 size_t count_equal_to(
const string&,
const T&,
const string&,
const T&)
const;
290 size_t count_not_equal_to(
const T&)
const;
292 size_t count_not_equal_to(
const size_t&,
const T&)
const;
293 size_t count_not_equal_to(
const size_t&,
const Vector<T>&)
const;
295 size_t count_not_equal_to(
const size_t&,
const T&,
const size_t&,
const T&)
const;
297 size_t count_not_equal_to(
const string&,
const T&)
const;
298 size_t count_not_equal_to(
const string&,
const T&,
const string&,
const T&)
const;
300 size_t count_rows_equal_to(
const T&)
const;
301 size_t count_rows_not_equal_to(
const T&)
const;
303 size_t count_rows_equal_to(
const Vector<size_t>&,
const T&)
const;
304 size_t count_columns_equal_to(
const Vector<T>&,
const T&)
const;
312 bool has_nan()
const;
314 bool has_nan_row(
const size_t&)
const;
315 bool has_nan_column(
const size_t&)
const;
317 size_t count_nan()
const;
318 size_t count_not_NAN()
const;
320 size_t count_rows_with_nan()
const;
322 size_t count_columns_with_nan()
const;
329 void filter(
const T&,
const T&);
331 Matrix<T> filter_column_equal_to(
const size_t&,
const T&)
const;
332 Matrix<T> filter_column_equal_to(
const string&,
const T&)
const;
338 const size_t&,
const T&)
const;
341 const string&,
const T&)
const;
343 Matrix<T> filter_column_equal_to(
const size_t&,
const T&,
344 const size_t&,
const T&,
345 const size_t&,
const T&,
346 const size_t&,
const T&)
const;
348 Matrix<T> filter_column_equal_to(
const string&,
const T&,
349 const string&,
const T&,
350 const string&,
const T&,
351 const string&,
const T&)
const;
354 const size_t&,
const T&,
355 const size_t&,
const T&,
356 const size_t&,
const T&)
const;
359 const string&,
const T&,
360 const string&,
const T&,
361 const string&,
const T&)
const;
363 Matrix<T> filter_column_not_equal_to(
const size_t&,
const T&)
const;
364 Matrix<T> filter_column_not_equal_to(
const string&,
const T&)
const;
369 Matrix<T> filter_column_less_than(
const size_t&,
const T&)
const;
370 Matrix<T> filter_column_less_than(
const string&,
const T&)
const;
372 Matrix<T> filter_column_greater_than(
const size_t&,
const T&)
const;
373 Matrix<T> filter_column_greater_than(
const string&,
const T&)
const;
375 Matrix<T> filter_column_less_than_string(
const string&,
const double&)
const;
376 Matrix<T> filter_column_greater_than_string(
const string&,
const double&)
const;
378 Matrix<T> filter_column_minimum_maximum(
const size_t&,
const T&,
const T&)
const;
379 Matrix<T> filter_column_minimum_maximum(
const string&,
const T&,
const T&)
const;
381 Matrix<T> filter_extreme_values(
const size_t&,
const double&,
const double&)
const;
382 Matrix<T> filter_extreme_values(
const string&,
const double&,
const double&)
const;
386 void initialize(
const T&);
387 void initialize_sequential();
389 void randomize_uniform(
const double& = -1.0,
const double& = 1.0);
393 void randomize_normal(
const double& = 0.0,
const double& = 1.0);
397 void initialize_identity();
399 void initialize_diagonal(
const T&);
401 void initialize_diagonal(
const size_t&,
const T&);
403 void initialize_diagonal(
const size_t&,
const Vector<T>&);
405 void append_header(
const string&);
407 void embed(
const size_t&,
const size_t&,
const Matrix<T>&);
409 void embed(
const size_t&,
const size_t&,
const Vector<T>&);
411 void sum_diagonal(
const T&);
413 void multiply_diagonal(
const T&);
423 void insert_row_values(
const size_t&,
const size_t&,
const Vector<T>&);
430 Matrix<T> insert_padding(
const size_t& ,
const size_t&)
const;
432 Matrix<T> add_columns(
const size_t&)
const;
433 Matrix<T> add_columns_first(
const size_t&)
const;
435 void split_column(
const string&,
const Vector<string>&,
const char& =
',',
const string& =
"NA");
437 void split_column(
const string&,
const string&,
const string&,
const size_t&,
const size_t&);
439 void swap_columns(
const size_t&,
const size_t&);
440 void swap_columns(
const string&,
const string&);
444 void merge_columns(
const string&,
const string&,
const string&,
const char&);
445 void merge_columns(
const size_t&,
const size_t&,
const char&);
447 Matrix<T> merge_matrices(
const Matrix<T>&,
const string&,
const string&,
const string& =
"",
const string& =
"")
const;
452 Matrix<T> right_join(
const Matrix<T>&,
const string&,
const string&,
const string& =
"",
const string& =
"")
const;
455 Matrix<T> left_join(
const Matrix<T>&,
const string&,
const string&,
const string& =
"",
const string& =
"")
const;
456 Matrix<T> left_join(
const Matrix<T>&,
const string&,
const string&,
const string&,
const string&,
const string& =
"",
const string& =
"")
const;
461 Matrix<T> delete_row(
const size_t&)
const;
464 Matrix<T> delete_rows_with_value(
const T&)
const;
465 Matrix<T> delete_columns_with_value(
const T&)
const;
467 Matrix<T> delete_rows_equal_to(
const T&)
const;
469 Matrix<T> delete_first_rows(
const size_t&)
const;
470 Matrix<T> delete_last_rows(
const size_t&)
const;
472 Matrix<T> delete_first_columns(
const size_t&)
const;
473 Matrix<T> delete_last_columns(
const size_t&)
const;
475 Matrix<T> delete_column(
const size_t&)
const;
478 Matrix<T> delete_column(
const string&)
const;
484 Matrix<T> delete_constant_columns()
const;
488 Matrix<T> delete_binary_columns(
const double&)
const;
498 Matrix<T> sort_ascending(
const size_t&)
const;
499 Matrix<T> sort_descending(
const size_t&)
const;
501 Matrix<T> sort_ascending_strings(
const size_t&)
const;
502 Matrix<T> sort_descending_strings(
const size_t&)
const;
511 void replace(
const T&,
const T&);
513 void replace_header(
const string&,
const string&);
515 void replace_in_row(
const size_t&,
const T&,
const T&);
516 void replace_in_column(
const size_t&,
const T&,
const T&);
517 void replace_in_column(
const string&,
const T&,
const T&);
519 void replace_substring(
const string&,
const string&);
520 void replace_substring(
const size_t&,
const string&,
const string&);
521 void replace_substring(
const string&,
const string&,
const string&);
523 void replace_contains(
const string&,
const string&);
524 void replace_contains_in_row(
const size_t&,
const string&,
const string&);
526 void replace_column_equal_to(
const size_t&,
const T&,
const T&);
527 void replace_column_equal_to(
const string&,
const T&,
const T&);
528 void replace_column_not_equal_to(
const string&,
const T&,
const T&);
529 void replace_column_not_equal_to(
const string&,
const Vector<T>&,
const T&);
531 void replace_column_less_than_string(
const string&,
const double&,
const T&);
533 void replace_column_contains(
const string&,
const string&,
const string&);
534 size_t count_column_contains(
const string&,
const string&)
const;
538 T calculate_sum()
const;
542 T calculate_column_sum(
const size_t&)
const;
543 T calculate_row_sum(
const size_t&)
const;
545 void sum_row(
const size_t&,
const Vector<T>&);
546 void sum_column(
const size_t&,
const Vector<T>&);
553 double calculate_trace()
const;
561 Matrix<T> calculate_reverse_columns()
const;
565 bool compare_rows(
const size_t&,
const Matrix<T>&,
const size_t&)
const;
571 string matrix_to_string(
const char& =
' ')
const;
586 vector<T> to_std_vector()
const;
592 Vector< Matrix<T> > to_vector_matrix(
const size_t&,
const size_t&,
const size_t&)
const;
594 Matrix<T> to_categorical(
const size_t&)
const;
601 void load_csv(
const string&,
const char& =
',',
const bool& =
false,
const string& =
"NA");
602 void load_csv_string(
const string&,
const char& =
',',
const bool& =
true);
603 void load_binary(
const string&);
605 void save_binary(
const string&)
const;
611 void parse(
const string&);
613 void print_preview()
const;
647 Matrix<T>::Matrix(
const size_t& new_rows_number,
const size_t& new_columns_number) : vector<T>(new_rows_number*new_columns_number)
649 if(new_rows_number == 0 && new_columns_number == 0)
653 else if(new_rows_number == 0)
657 else if(new_columns_number == 0)
663 set(new_rows_number,new_columns_number);
675 Matrix<T>::Matrix(
const size_t& new_rows_number,
const size_t& new_columns_number,
const T& value) : vector<T>(new_rows_number*new_columns_number)
677 if(new_rows_number == 0 && new_columns_number == 0)
681 else if(new_rows_number == 0)
683 ostringstream buffer;
685 buffer <<
"OpenNN Exception: Matrix Template.\n"
686 <<
"Constructor Matrix(const size_t&, const size_t&, const T&).\n"
687 <<
"Number of rows must be greater than zero.\n";
689 throw logic_error(buffer.str());
691 else if(new_columns_number == 0)
693 ostringstream buffer;
695 buffer <<
"OpenNN Exception: Matrix Template.\n"
696 <<
"Constructor Matrix(const size_t&, const size_t&, const T&).\n"
697 <<
"Number of columns must be greater than zero.\n";
699 throw logic_error(buffer.str());
705 set(new_rows_number,new_columns_number,value);
716 Matrix<T>::Matrix(
const string& file_name,
const char& separator,
const bool& header) : vector<T>()
721 load_csv(file_name, separator, header);
734 header = other_matrix.
header;
746 Matrix<T>::Matrix(
const initializer_list<Vector<T>>& new_columns,
const initializer_list<string>& new_header) : vector<T>()
750 set_header(new_header);
775 set(rows_number, columns_number);
778 copy(other_matrix.begin(), other_matrix.end(), this->begin());
780 copy(other_matrix.
header.begin(), other_matrix.
header.end(), header.begin());
797 #ifdef __OPENNN_DEBUG__
799 if(row >= rows_number)
801 ostringstream buffer;
803 buffer <<
"OpenNN Exception: Matrix Template.\n"
804 <<
"operator()(const size_t&, const size_t&).\n"
805 <<
"Row index (" << row <<
") must be less than number of rows (" << rows_number <<
").\n";
807 throw logic_error(buffer.str());
809 else if(column >= columns_number)
811 ostringstream buffer;
813 buffer <<
"OpenNN Exception: Matrix Template.\n"
814 <<
"operator()(const size_t&, const size_t&).\n"
815 <<
"Column index (" << column <<
") must be less than number of columns (" << columns_number <<
").\n";
817 throw logic_error(buffer.str());
824 return (*
this)[rows_number*column+row];
837 #ifdef __OPENNN_DEBUG__
839 if(row >= rows_number)
841 ostringstream buffer;
843 buffer <<
"OpenNN Exception: Matrix Template.\n"
844 <<
"operator()(const size_t&, const size_t&).\n"
845 <<
"Row index (" << row <<
") must be less than number of rows (" << rows_number <<
").\n";
847 throw logic_error(buffer.str());
849 else if(column >= columns_number)
851 ostringstream buffer;
853 buffer <<
"OpenNN Exception: Matrix Template.\n"
854 <<
"operator()(const size_t&, const size_t&).\n"
855 <<
"Column index (" << column <<
") must be less than number of columns (" << columns_number <<
").\n";
857 throw logic_error(buffer.str());
864 return (*
this)[rows_number*column+row];
871 const size_t column = get_column_index(column_name);
873 return (*
this)(row, column);
882 const size_t column = get_column_index(column_name);
884 return (*
this)(row, column);
898 if(other_rows_number != rows_number)
902 else if(other_columns_number != columns_number)
908 for(
size_t i = 0; i < this->size(); i++)
910 if((*
this)[i] != other_matrix[i])
928 for(
size_t i = 0; i < this->size(); i++)
930 if((*
this)[i] != value)
949 #ifdef __OPENNN_DEBUG__
954 if(other_rows_number != rows_number)
956 ostringstream buffer;
958 buffer <<
"OpenNN Exception: Matrix Template.\n"
959 <<
"bool operator != (const Matrix<T>&) const.\n"
960 <<
"Both numbers of rows must be the same.\n";
962 throw logic_error(buffer.str());
964 else if(other_columns_number != columns_number)
966 ostringstream buffer;
968 buffer <<
"OpenNN Exception: Matrix Template.\n"
969 <<
"bool operator != (const Matrix<T>&) const.\n"
970 <<
"Both numbers of columns must be the same.\n";
972 throw logic_error(buffer.str());
977 for(
size_t i = 0; i < this->size(); i++)
979 if((*
this)[i] != other_matrix[i])
998 for(
size_t i = 0; i < this->size(); i++)
1000 if((*
this)[i] != value)
1020 #ifdef __OPENNN_DEBUG__
1025 if(other_rows_number != rows_number)
1027 ostringstream buffer;
1029 buffer <<
"OpenNN Exception: Matrix Template.\n"
1030 <<
"bool operator >(const Matrix<T>&) const.\n"
1031 <<
"Both numbers of rows must be the same.\n";
1033 throw logic_error(buffer.str());
1035 else if(other_columns_number != columns_number)
1037 ostringstream buffer;
1039 buffer <<
"OpenNN Exception: Matrix Template.\n"
1040 <<
"bool operator >(const Matrix<T>&) const.\n"
1041 <<
"Both numbers of columns must be the same.\n";
1043 throw logic_error(buffer.str());
1048 for(
size_t i = 0; i < this->size(); i++)
1050 if((*
this)[i] <= other_matrix[i])
1067 for(
size_t i = 0; i < this->size(); i++)
1069 if((*
this)[i] <= value)
1089 #ifdef __OPENNN_DEBUG__
1094 if(other_rows_number != rows_number)
1096 ostringstream buffer;
1098 buffer <<
"OpenNN Exception: Matrix Template.\n"
1099 <<
"bool operator <(const Matrix<T>&) const.\n"
1100 <<
"Both numbers of rows must be the same.\n";
1102 throw logic_error(buffer.str());
1104 else if(other_columns_number != columns_number)
1106 ostringstream buffer;
1108 buffer <<
"OpenNN Exception: Matrix Template.\n"
1109 <<
"bool operator <(const Matrix<T>&) const.\n"
1110 <<
"Both numbers of columns must be the same.\n";
1112 throw logic_error(buffer.str());
1117 for(
size_t i = 0; i < this->size(); i++)
1119 if((*
this)[i] >= other_matrix[i])
1136 for(
size_t i = 0; i < this->size(); i++)
1138 if((*
this)[i] >= value)
1158 #ifdef __OPENNN_DEBUG__
1163 if(other_rows_number != rows_number)
1165 ostringstream buffer;
1167 buffer <<
"OpenNN Exception: Matrix Template.\n"
1168 <<
"bool operator >= (const Matrix<T>&) const.\n"
1169 <<
"Both numbers of rows must be the same.\n";
1171 throw logic_error(buffer.str());
1173 else if(other_columns_number != columns_number)
1175 ostringstream buffer;
1177 buffer <<
"OpenNN Exception: Matrix Template.\n"
1178 <<
"bool operator >= (const Matrix<T>&) const.\n"
1179 <<
"Both numbers of columns must be the same.\n";
1181 throw logic_error(buffer.str());
1186 for(
size_t i = 0; i < this->size(); i++)
1188 if((*
this)[i] < other_matrix[i])
1205 for(
size_t i = 0; i < this->size(); i++)
1207 if((*
this)[i] < value)
1225 #ifdef __OPENNN_DEBUG__
1230 if(other_rows_number != rows_number)
1232 ostringstream buffer;
1234 buffer <<
"OpenNN Exception: Matrix Template.\n"
1235 <<
"bool operator >= (const Matrix<T>&) const.\n"
1236 <<
"Both numbers of rows must be the same.\n";
1238 throw logic_error(buffer.str());
1240 else if(other_columns_number != columns_number)
1242 ostringstream buffer;
1244 buffer <<
"OpenNN Exception: Matrix Template.\n"
1245 <<
"bool operator >= (const Matrix<T>&) const.\n"
1246 <<
"Both numbers of columns must be the same.\n";
1248 throw logic_error(buffer.str());
1253 for(
size_t i = 0; i < this->size(); i++)
1255 if((*
this)[i] > other_matrix[i])
1272 for(
size_t i = 0; i < this->size(); i++)
1274 if((*
this)[i] > value)
1298 return columns_number;
1317 return(header[index]);
1327 if(rows_number == 0)
1329 ostringstream buffer;
1331 buffer <<
"OpenNN Exception: Matrix Template.\n"
1332 <<
"size_t get_column_index(const string&) const.\n"
1333 <<
"Number of rows must be greater than zero.\n";
1335 throw logic_error(buffer.str());
1340 for(
size_t i = 0; i < columns_number; i++)
1342 if(header[i] == column_name)
1350 ostringstream buffer;
1352 buffer <<
"OpenNN Exception: Matrix Template.\n"
1353 <<
"size_t get_column_index(const string&) const.\n"
1354 <<
"Header does not contain " << column_name <<
":\n"
1357 throw logic_error(buffer.str());
1362 ostringstream buffer;
1364 buffer <<
"OpenNN Exception: Matrix Template.\n"
1365 <<
"size_t get_column_index(const string&) const.\n"
1366 <<
"Multiple occurrences of column name " << column_name <<
".\n";
1368 throw logic_error(buffer.str());
1373 for(
size_t i = 0; i < columns_number; i++)
1375 if(header[i] == column_name)
1391 const size_t size = names.size();
1395 ostringstream buffer;
1397 buffer <<
"OpenNN Exception: Matrix Template.\n"
1398 <<
"Vector<size_t> get_columns_indices(const Vector<string>&) const.\n"
1399 <<
"Header is empty.\n";
1401 throw logic_error(buffer.str());
1406 for(
size_t i = 0; i < size; i++)
1408 for(
size_t j = 0; j < header.size(); j++)
1410 if(names[i] == header[j])
1420 ostringstream buffer;
1422 buffer <<
"OpenNN Exception: Matrix Template.\n"
1423 <<
"Vector<size_t> get_columns_indices(const Vector<string>&) const.\n"
1424 <<
"Header does not contain some name.\n";
1426 buffer <<
"Header: " << header << endl;
1429 buffer <<
"Names: " << names << endl;
1431 throw logic_error(buffer.str());
1436 for(
size_t i = 0; i < size; i++)
1438 indices[i] = get_column_index(names[i]);
1452 for(
size_t i = 0; i < columns_number; i++)
1454 if(is_column_binary(i))
1456 binary_columns.push_back(i);
1460 return binary_columns;
1474 vector<T>().swap(*
this);
1485 if(new_rows_number == 0 && new_columns_number == 0)
1489 else if(new_rows_number == 0)
1493 else if(new_columns_number == 0)
1499 rows_number = new_rows_number;
1500 columns_number = new_columns_number;
1502 this->resize(rows_number*columns_number);
1504 header.set(columns_number,
"");
1516 void Matrix<T>::set(
const size_t& new_rows_number,
const size_t& new_columns_number,
const T& value)
1518 if(new_rows_number == 0 && new_columns_number == 0)
1522 else if(new_rows_number == 0)
1524 ostringstream buffer;
1526 buffer <<
"OpenNN Exception: Matrix Template.\n"
1527 <<
"void set(const size_t&, const size_t&, const T&) method.\n"
1528 <<
"Number of rows must be greater than zero.\n";
1530 throw logic_error(buffer.str());
1532 else if(new_columns_number == 0)
1534 ostringstream buffer;
1536 buffer <<
"OpenNN Exception: Matrix Template.\n"
1537 <<
"void set(const size_t&, const size_t&, const T&) method.\n"
1538 <<
"Number of columns must be greater than zero.\n";
1540 throw logic_error(buffer.str());
1544 set(new_rows_number, new_columns_number);
1561 set(rows_number, columns_number);
1564 copy(other_matrix.begin(), other_matrix.end(), this->begin());
1566 copy(other_matrix.
header.begin(), other_matrix.
header.end(), header.begin());
1576 load_csv(file_name);
1585 if(columns.size() == 0)
1590 const size_t new_columns_number = columns.size();
1592 const size_t new_rows_number = (*columns.begin()).size();
1594 if(new_rows_number == 0)
1596 ostringstream buffer;
1598 buffer <<
"OpenNN Exception: Matrix Template.\n"
1599 <<
"void set(const initializer_list<Vector<T>>&) method.\n"
1600 <<
"Size of list vectors must be greater than zero.\n";
1602 throw logic_error(buffer.str());
1605 set(new_rows_number, new_columns_number);
1607 for(
size_t i = 0; i < new_columns_number; i++)
1609 const Vector<T> new_column(*(columns.begin() + i));
1611 if(new_column.size() != new_rows_number)
1613 ostringstream buffer;
1615 buffer <<
"OpenNN Exception: Matrix Template.\n"
1616 <<
"Matrix(const initializer_list<Vector<T>>& list) constructor.\n"
1617 <<
"Size of vector " << i <<
" (" << new_column.size() <<
") must be equal to number of rows (" << new_rows_number <<
").\n";
1619 throw logic_error(buffer.str());
1622 set_column(i, new_column);
1633 set(new_size, new_size);
1634 initialize_identity();
1644 if(new_rows_number != rows_number)
1646 set(new_rows_number, columns_number);
1657 if(new_columns_number != columns_number)
1659 set(rows_number, new_columns_number);
1670 this->header = new_header;
1681 header[index] = index_name;
1690 for(
size_t i = 0; i < header.size(); i++)
1692 header[i].append(str);
1708 #ifdef __OPENNN_DEBUG__
1710 if(row_position + other_rows_number > rows_number)
1712 ostringstream buffer;
1714 buffer <<
"OpenNN Exception: Matrix Template.\n"
1715 <<
"void insert(const size_t&, const size_t&, const Matrix<T>&) const method.\n"
1716 <<
"Cannot tuck in matrix.\n";
1718 throw logic_error(buffer.str());
1721 if(column_position + other_columns_number > columns_number)
1723 ostringstream buffer;
1725 buffer <<
"OpenNN Exception: Matrix Template.\n"
1726 <<
"void insert(const size_t&, const size_t&, const Matrix<T>&) const method.\n"
1727 <<
"Cannot tuck in matrix.\n";
1729 throw logic_error(buffer.str());
1734 for(
size_t i = 0; i < other_rows_number; i++)
1736 for(
size_t j = 0; j < other_columns_number; j++)
1738 (*this)(row_position+i,column_position+j) = other_matrix(i,j);
1752 const size_t other_columns_number = other_vector.size();
1754 #ifdef __OPENNN_DEBUG__
1756 if(row_position > rows_number)
1758 ostringstream buffer;
1760 buffer <<
"OpenNN Exception: Matrix Template.\n"
1761 <<
"void insert(const size_t&, const size_t&, const Vector<T>&) const method.\n"
1762 <<
"Cannot tuck in vector.\n";
1764 throw logic_error(buffer.str());
1767 if(column_position + other_columns_number > columns_number)
1769 ostringstream buffer;
1771 buffer <<
"OpenNN Exception: Matrix Template.\n"
1772 <<
"void insert(const size_t&, const size_t&, const Vector<T>&) const method.\n"
1773 <<
"Cannot tuck in vector.\n";
1775 throw logic_error(buffer.str());
1780 for(
size_t j = 0; j < other_columns_number; j++)
1782 (*this)(row_position,column_position+j) = other_vector[j];
1793 #ifdef __OPENNN_DEBUG__
1797 ostringstream buffer;
1799 buffer <<
"OpenNN Exception: Matrix Template.\n"
1800 <<
"size_t count_diagonal_elements() const method.\n"
1801 <<
"The matrix is not square.\n";
1803 throw logic_error(buffer.str());
1810 for(
size_t i = 0; i < rows_number; i++)
1812 if((*
this)(i,i) != 0)
1828 #ifdef __OPENNN_DEBUG__
1832 ostringstream buffer;
1834 buffer <<
"OpenNN Exception: Matrix Template.\n"
1835 <<
"size_t count_off_diagonal_elements() const method.\n"
1836 <<
"The matrix is not square.\n";
1838 throw logic_error(buffer.str());
1845 for(
size_t i = 0; i < rows_number; i++)
1847 for(
size_t j = 0; j < columns_number; j++)
1849 if(i != j &&(*
this)(i,j) != 0)
1868 for(
size_t i = 0; i < rows_number; i++)
1870 for(
size_t j = 0; j < columns_number; j++)
1872 if((*
this)(i, j) == value)
1892 for(
size_t i = 0; i < rows_number; i++)
1894 if((*
this)(i, column_index) == value)
1911 const size_t values_size = values.size();
1915 for(
size_t i = 0; i < rows_number; i++)
1917 for(
size_t j = 0; j < values_size; j++)
1919 if((*
this)(i, column_index) == values[j])
1941 const size_t& column_2_index,
const T& value_2)
const
1943 const size_t values_1_size = values_1.size();
1949 for(
size_t i = 0; i < rows_number; i++)
1951 if((*
this)(i, column_2_index) == value_2)
1953 matrix_element = (*this)(i, column_1_index);
1955 for(
size_t j = 0; j < values_1_size; j++)
1957 if(matrix_element == values_1[j])
1985 const size_t& column_2_index,
const T& value_2,
1986 const size_t& column_3_index,
const T& value_3,
1987 const size_t& column_4_index,
const T& value_4)
const
1989 const size_t values_1_size = values_1.size();
1995 for(
size_t i = 0; i < rows_number; i++)
1997 if((*
this)(i, column_2_index) == value_2
1998 &&(*
this)(i, column_3_index) == value_3
1999 &&(*
this)(i, column_4_index) == value_4)
2001 matrix_element = (*this)(i, column_1_index);
2003 for(
size_t j = 0; j < values_1_size; j++)
2005 if(matrix_element == values_1[j])
2029 const size_t& column_2_index,
const T& value_2)
const
2033 for(
size_t i = 0; i < rows_number; i++)
2035 if((*
this)(i, column_1_index) == value_1
2036 &&(*
this)(i, column_2_index) == value_2)
2059 const size_t& column_2_index,
const T& value_2,
2060 const size_t& column_3_index,
const T& value_3,
2061 const size_t& column_4_index,
const T& value_4)
const
2065 for(
size_t i = 0; i < rows_number; i++)
2067 if((*
this)(i, column_1_index) == value_1
2068 &&(*
this)(i, column_2_index) == value_2
2069 &&(*
this)(i, column_3_index) == value_3
2070 &&(*
this)(i, column_4_index) == value_4)
2088 const size_t column_index = get_column_index(column_name);
2090 return(count_equal_to(column_index, value));
2102 const size_t column_index = get_column_index(column_name);
2104 return(count_equal_to(column_index, values));
2118 const string& column_2_name,
const T& value_2)
const
2120 const size_t column_1_index = get_column_index(column_1_name);
2121 const size_t column_2_index = get_column_index(column_2_name);
2123 return(count_equal_to(column_1_index, value_1, column_2_index, value_2));
2137 for(
size_t i = 0; i < rows_number; i++)
2142 return count_by_rows;
2157 for(
size_t i = 0; i < rows_number; i++)
2159 count_by_rows[i] = this->get_row(i).
count_equal_to(value, weights);
2162 return count_by_rows;
2172 const size_t this_size = this->size();
2174 return(this_size-count_equal_to(value));
2187 for(
size_t i = 0; i < rows_number; i++)
2189 if((*
this)(i, column_index) != value)
2206 const size_t values_size = values.size();
2210 for(
size_t i = 0; i < rows_number; i++)
2214 for(
size_t j = 0; j < values_size; j++)
2216 if((*
this)(i, column_index) != values[j])
2222 if(count == values.size())
2242 const size_t& column_2_index,
const T& value_2)
const
2246 for(
size_t i = 0; i < rows_number; i++)
2248 if((*
this)(i, column_1_index) != value_1 &&(*
this)(i, column_2_index) != value_2)
2265 const size_t column_index = get_column_index(column_name);
2267 return(count_not_equal_to(column_index, value));
2281 const string& column_2_name,
const T& value_2)
const
2283 const size_t column_1_index = get_column_index(column_1_name);
2284 const size_t column_2_index = get_column_index(column_2_name);
2286 return(count_not_equal_to(column_1_index, value_1, column_2_index, value_2));
2297 size_t count = rows_number;
2299 for(
size_t i = 0; i < rows_number; i++)
2301 for(
size_t j = 0; j < columns_number; j++)
2303 if((*
this)(i,j) != value)
2325 for(
size_t i = 0; i < rows_number; i++)
2327 if(get_row(i) != value) count++;
2343 for(
size_t i = 0; i < rows_number; i++)
2345 if(get_row(i) == vector)
2347 indices.push_back(i);
2365 for(
size_t i = 0; i < rows_number; i++)
2367 if(is_row_equal_to(i, columns_indices, value))
2386 const size_t columns_indices_size = columns_indices.size();
2388 for(
size_t i = 0; i < columns_indices_size; i++)
2390 if((*
this)(row_index,columns_indices[i]) != value)
2407 const size_t row_indices_size = row_indices.size();
2408 const size_t columns_indices_size = columns_indices.size();
2410 Matrix<T> sub_matrix(row_indices_size, columns_indices_size);
2413 size_t column_index;
2415 for(
size_t i = 0; i < row_indices_size; i++)
2417 row_index = row_indices[i];
2419 for(
size_t j = 0; j < columns_indices_size; j++)
2421 column_index = columns_indices[j];
2422 sub_matrix(i,j) = (*this)(row_index,column_index);
2435 #ifdef __OPENNN_DEBUG__
2439 ostringstream buffer;
2441 buffer <<
"OpenNN Exception: Matrix Template.\n"
2442 <<
"Tensor<T> get_tensor(const Vector<size_t>&, const Vector<size_t>&, const Vector<size_t>&) const method.\n"
2443 <<
"Size of columns indices(" << columns_indices.size() <<
") must be equal to product of columns dimensions(" << columns_dimensions.
calculate_product() <<
").\n";
2445 throw logic_error(buffer.str());
2450 const size_t rows_number = rows_indices.size();
2451 const size_t columns_number = columns_indices.size();
2453 const Vector<size_t> dimensions = Vector<size_t>(1, rows_number).
assemble(columns_dimensions);
2455 Tensor<T> tensor(dimensions);
2458 size_t column_index;
2460 size_t tensor_index = 0;
2462 for(
size_t j = 0; j < columns_number; j++)
2464 column_index = columns_indices[j];
2466 for(
size_t i = 0; i < rows_number; i++)
2468 row_index = rows_indices[i];
2470 tensor[tensor_index] = (*this)(row_index, column_index);
2486 const size_t row_indices_size = row_indices.size();
2488 Matrix<T> sub_matrix(row_indices_size, columns_number);
2492 for(
size_t i = 0; i < row_indices_size; i++)
2494 row_index = row_indices[i];
2496 for(
size_t j = 0; j < columns_number; j++)
2498 sub_matrix(i,j) = (*this)(row_index,j);
2514 const size_t columns_indices_size = columns_indices.size();
2516 #ifdef __OPENNN_DEBUG__
2518 for(
size_t i = 0; i < columns_indices_size; i++)
2520 if(columns_indices[i] >= columns_number)
2522 ostringstream buffer;
2524 buffer <<
"OpenNN Exception: Matrix Template.\n"
2525 <<
"Matrix<T> get_submatrix_columns(const Vector<size_t>&) const method.\n"
2526 <<
"Column index (" << i <<
") must be less than number of columns(" << columns_number <<
").\n";
2528 throw logic_error(buffer.str());
2534 Matrix<T> sub_matrix(rows_number, columns_indices_size);
2536 size_t column_index;
2538 for(
size_t i = 0; i < rows_number; i++)
2540 for(
size_t j = 0; j < columns_indices_size; j++)
2542 column_index = columns_indices[j];
2544 sub_matrix(i,j) = (*this)(i,column_index);
2565 #ifdef __OPENNN_DEBUG__
2567 if(index >= rows_number)
2569 ostringstream buffer;
2571 buffer <<
"OpenNN Exception: Matrix Template.\n"
2572 <<
"Vector<T> get_row(const size_t&) const method.\n"
2573 <<
"Row index (" << index <<
") must be less than number of rows (" << rows_number <<
").\n";
2575 throw logic_error(buffer.str());
2582 for(
size_t j = 0; j < columns_number; j++)
2584 row[j] = (*this)(index,j);
2598 #ifdef __OPENNN_DEBUG__
2600 if(last_index > rows_number)
2602 ostringstream buffer;
2604 buffer <<
"OpenNN Exception: Matrix Template.\n"
2605 <<
"Vector<T> get_rows(const size_t&, const size_t&) const method.\n"
2606 <<
"Last index (" << last_index <<
") must be less than number of rows(" << rows_number <<
").\n";
2608 throw logic_error(buffer.str());
2615 for(
size_t i = first_index-1; i < last_index; i++)
2617 new_row = new_row.
assemble(get_row(i));
2631 #ifdef __OPENNN_DEBUG__
2633 if(row_index >= rows_number)
2635 ostringstream buffer;
2637 buffer <<
"OpenNN Exception: Matrix Template.\n"
2638 <<
"Vector<T> get_row(const size_t&, const Vector<size_t>&) const method.\n"
2639 <<
"Row index (" << row_index <<
") must be less than number of rows(" << rows_number <<
").\n";
2641 throw logic_error(buffer.str());
2646 const size_t size = columns_indices.size();
2650 for(
size_t i = 0; i < size; i++)
2652 row[i] = (*this)(row_index,columns_indices[i]);
2665 #ifdef __OPENNN_DEBUG__
2667 if(column_index >= columns_number)
2669 ostringstream buffer;
2671 buffer <<
"OpenNN Exception: Matrix Template.\n"
2672 <<
"Vector<T> get_column(const size_t&) const method.\n"
2673 <<
"Column index (" << column_index <<
") must be less than number of columns(" << columns_number <<
").\n";
2675 throw logic_error(buffer.str());
2682 for(
size_t i = 0; i < rows_number; i++)
2684 column[i] = (*this)(i,column_index);
2697 #ifdef __OPENNN_DEBUG__
2701 ostringstream buffer;
2703 buffer <<
"OpenNN Exception: Matrix Template.\n"
2704 <<
"Vector<T> get_column(const string&) const method.\n"
2705 <<
"Matrix is empty.\n";
2707 throw logic_error(buffer.str());
2712 const size_t column_index = get_column_index(column_name);
2714 return(get_column(column_index));
2725 const Vector<size_t> indices = get_columns_indices(column_names);
2727 return(get_submatrix_columns(indices));
2738 #ifdef __OPENNN_DEBUG__
2740 if(column_index >= columns_number)
2742 ostringstream buffer;
2744 buffer <<
"OpenNN Exception: Matrix Template.\n"
2745 <<
"Vector<T> get_column(const size_t&, const Vector<size_t>&) const method.\n"
2746 <<
"Column index (" << column_index <<
") must be less than number of rows(" << columns_number <<
").\n";
2748 throw logic_error(buffer.str());
2753 const size_t size = row_indices.size();
2757 for(
size_t i = 0; i < size; i++)
2759 column[i] = (*this)(row_indices[i],column_index);
2771 const size_t this_size = this->size();
2773 for(
size_t i = 0; i < this_size; i++)
2775 if(::isnan((*
this)[i]))
return true;
2787 for(
size_t j = 0; j < columns_number; j++)
2789 if(::isnan((*
this)(row_index, j)))
return true;
2802 for(
size_t i = 0; i < rows_number; i++)
2804 if(::isnan((*
this)(i, column_index)))
return true;
2816 const size_t this_size = this->size();
2820 for(
size_t i = 0; i < this_size; i++)
2822 if(::isnan((*
this)[i])) count++;
2836 const size_t this_size = this->size();
2838 for(
size_t i = 0; i < this_size; i++)
2840 if(!::isnan((*
this)[i])) count++;
2854 for(
size_t i = 0; i < rows_number; i++)
2856 for(
size_t j = 0; j < columns_number; j++)
2858 if(::isnan((*
this)(i,j)))
2879 for(
size_t i = 0; i < rows_number; i++)
2881 for(
size_t j = 0; j < columns_number; j++)
2883 if(::isnan((*
this)(i,j)))
2885 nan_indices.push_back(i);
2904 for(
size_t j = 0; j < columns_number; j++)
2906 for(
size_t i = 0; i < rows_number; i++)
2908 if(::isnan((*
this)(i,j))) count++;
2926 for(
size_t i = 0; i < rows_number; i++)
2928 for(
size_t j = 0; j < columns_number; j++)
2930 if(::isnan((*
this)(i,j))) count[i]++;
2947 for(
size_t i = 0; i < rows_number; i++)
2949 for(
size_t j = 0; j < columns_number; j++)
2951 if(::isnan((*
this)(i,j))) count[j]++;
2966 for(
size_t i = 0; i < columns_number; i++)
2968 unique_elements.push_back(this->get_column(i).get_unique_elements().size());
2971 return unique_elements;
2980 #ifdef __OPENNN_DEBUG__
2982 if(rows_number != columns_number)
2984 ostringstream buffer;
2986 buffer <<
"OpenNN Exception: Matrix Template.\n"
2987 <<
"Vector<T> get_diagonal() const method.\n"
2988 <<
"Matrix must be squared.\n";
2990 throw logic_error(buffer.str());
2997 for(
size_t i = 0; i < rows_number; i++)
2999 diagonal[i] = (*this)(i,i);
3013 #ifdef __OPENNN_DEBUG__
3015 if(row_index >= rows_number)
3017 ostringstream buffer;
3019 buffer <<
"OpenNN Exception: Matrix Template.\n"
3020 <<
"set_row(const size_t&, const Vector<T>&) method.\n"
3021 <<
"Index must be less than number of rows.\n";
3023 throw logic_error(buffer.str());
3026 const size_t size = new_row.size();
3028 if(size != columns_number)
3030 ostringstream buffer;
3032 buffer <<
"OpenNN Exception: Matrix Template.\n"
3033 <<
"set_row(const size_t&, const Vector<T>&) method.\n"
3034 <<
"Size(" << size <<
") must be equal to number of columns(" << columns_number <<
").\n";
3036 throw logic_error(buffer.str());
3043 for(
size_t i = 0; i < columns_number; i++)
3045 (*this)(row_index,i) = new_row[i];
3057 #ifdef __OPENNN_DEBUG__
3059 if(row_index >= rows_number)
3061 ostringstream buffer;
3063 buffer <<
"OpenNN Exception: Matrix Template.\n"
3064 <<
"set_row(const size_t&, const T&) method.\n"
3065 <<
"Index must be less than number of rows.\n";
3067 throw logic_error(buffer.str());
3074 for(
size_t i = 0; i < columns_number; i++)
3076 (*this)(row_index,i) = value;
3088 #ifdef __OPENNN_DEBUG__
3092 ostringstream buffer;
3094 buffer <<
"OpenNN Exception: Matrix Template.\n"
3095 <<
"set_row(const size_t&, const T&) method.\n"
3096 <<
"Submatrix doesn't fix in this matrix.\n";
3098 throw logic_error(buffer.str());
3102 ostringstream buffer;
3104 buffer <<
"OpenNN Exception: Matrix Template.\n"
3105 <<
"set_row(const size_t&, const T&) method.\n"
3106 <<
"Submatrix columns number is different than matrix columns number.\n";
3108 throw logic_error(buffer.str());
3117 this->set_row(row_index+i,submatrix.
get_row(i));
3120 if(header ==
"" && row_index == 0)
3135 #ifdef __OPENNN_DEBUG__
3137 if(column_index >= columns_number)
3139 ostringstream buffer;
3141 buffer <<
"OpenNN Exception: Matrix Template.\n"
3142 <<
"set_column(const size_t&, const Vector<T>&).\n"
3143 <<
"index (" << column_index <<
") must be less than number of columns(" << columns_number <<
").\n";
3145 throw logic_error(buffer.str());
3148 const size_t size = new_column.size();
3150 if(size != rows_number)
3152 ostringstream buffer;
3154 buffer <<
"OpenNN Exception: Matrix Template.\n"
3155 <<
"set_column(const size_t&, const Vector<T>&).\n"
3156 <<
"Size must be equal to number of rows.\n";
3158 throw logic_error(buffer.str());
3165 for(
size_t i = 0; i < rows_number; i++)
3167 (*this)(i,column_index) = new_column[i];
3170 header[column_index] = new_name;
3182 const size_t column_index = get_column_index(column_name);
3184 this->set_column(column_index, new_column, new_name);
3196 const size_t column_index = get_column_index(column_name);
3198 set_column(column_index, value, new_name);
3210 #ifdef __OPENNN_DEBUG__
3212 if(column_index >= columns_number)
3214 ostringstream buffer;
3216 buffer <<
"OpenNN Exception: Matrix Template.\n"
3217 <<
"set_column(const size_t&, const T&).\n"
3218 <<
"Index must be less than number of columns.\n";
3220 throw logic_error(buffer.str());
3227 for(
size_t i = 0; i < rows_number; i++)
3229 (*this)(i,column_index) = value;
3232 header[column_index] = new_name;
3243 #ifdef __OPENNN_DEBUG__
3245 if(rows_number != columns_number)
3247 ostringstream buffer;
3249 buffer <<
"OpenNN Exception: Matrix Template.\n"
3250 <<
"set_diagonal(const T&).\n"
3251 <<
"Matrix must be square.\n";
3253 throw logic_error(buffer.str());
3260 for(
size_t i = 0; i < rows_number; i++)
3262 (*this)(i,i) = new_diagonal;
3274 #ifdef __OPENNN_DEBUG__
3276 if(rows_number != columns_number)
3278 ostringstream buffer;
3280 buffer <<
"OpenNN Exception: Matrix Template.\n"
3281 <<
"set_diagonal(const Vector<T>&) const.\n"
3282 <<
"Matrix is not square.\n";
3284 throw logic_error(buffer.str());
3287 const size_t size = new_diagonal.size();
3289 if(size != rows_number)
3291 ostringstream buffer;
3293 buffer <<
"OpenNN Exception: Matrix Template.\n"
3294 <<
"set_diagonal(const Vector<T>&) const.\n"
3295 <<
"Size of diagonal(" << size <<
") is not equal to size of matrix (" << rows_number <<
").\n";
3297 throw logic_error(buffer.str());
3304 for(
size_t i = 0; i < rows_number; i++)
3306 (*this)(i,i) = new_diagonal[i];
3320 this->set(new_size, new_size, 0);
3321 this->set_diagonal(new_value);
3334 #ifdef __OPENNN_DEBUG__
3336 const size_t new_values_size = new_values.size();
3338 if(new_values_size != new_size)
3340 ostringstream buffer;
3342 buffer <<
"OpenNN Exception: Matrix Template.\n"
3343 <<
"initialize_diagonal(const size_t&, const size_t&) const.\n"
3344 <<
"Size of new values is not equal to size of square matrix.\n";
3346 throw logic_error(buffer.str());
3351 set(new_size, new_size, 0.0);
3352 set_diagonal(new_values);
3363 #ifdef __OPENNN_DEBUG__
3365 if(rows_number != columns_number)
3367 ostringstream buffer;
3369 buffer <<
"OpenNN Exception: Matrix Template.\n"
3370 <<
"sum_diagonal(const T&).\n"
3371 <<
"Matrix must be square.\n";
3373 throw logic_error(buffer.str());
3378 for(
size_t i = 0; i < rows_number; i++)
3380 (*this)(i,i) += value;
3392 #ifdef __OPENNN_DEBUG__
3394 if(rows_number != columns_number)
3396 ostringstream buffer;
3398 buffer <<
"OpenNN Exception: Matrix Template.\n"
3399 <<
"multiply_diagonal(const T&).\n"
3400 <<
"Matrix must be square.\n";
3402 throw logic_error(buffer.str());
3407 for(
size_t i = 0; i < rows_number; i++)
3409 (*this)(i,i) = (*
this)(i,i)*value;
3421 #ifdef __OPENNN_DEBUG__
3423 if(rows_number != columns_number)
3425 ostringstream buffer;
3427 buffer <<
"OpenNN Exception: Matrix Template.\n"
3428 <<
"sum_diagonal(const Vector<T>&).\n"
3429 <<
"Matrix must be square.\n";
3431 throw logic_error(buffer.str());
3434 const size_t size = new_summing_values.size();
3436 if(size != rows_number)
3438 ostringstream buffer;
3440 buffer <<
"OpenNN Exception: Matrix Template.\n"
3441 <<
"sum_diagonal(const Vector<T>&).\n"
3442 <<
"Size must be equal to number of rows.\n";
3444 throw logic_error(buffer.str());
3449 for(
size_t i = 0; i < rows_number; i++)
3451 (*this)(i,i) += new_summing_values[i];
3464 #ifdef __OPENNN_DEBUG__
3466 const size_t size = new_row.size();
3468 if(size != columns_number)
3470 ostringstream buffer;
3472 buffer <<
"OpenNN Exception: Matrix Template.\n"
3473 <<
"append_row(const Vector<T>&) const.\n"
3474 <<
"Size(" << size <<
") must be equal to number of columns(" << columns_number <<
").\n";
3476 throw logic_error(buffer.str());
3485 copy.
set(1,new_row.size());
3491 copy.
set(rows_number+1, columns_number);
3493 for(
size_t i = 0; i < rows_number; i++)
3495 for(
size_t j = 0; j < columns_number; j++)
3497 copy(i,j) = (*this)(i,j);
3501 copy.
set_row(rows_number, new_row);
3518 #ifdef __OPENNN_DEBUG__
3520 const size_t size = new_column.size();
3522 if(size != rows_number)
3524 ostringstream buffer;
3526 buffer <<
"OpenNN Exception: Matrix Template.\n"
3527 <<
"append_column(const Vector<T>&) const.\n"
3528 <<
"Size(" << size <<
") must be equal to number of rows(" << rows_number <<
").\n";
3530 throw logic_error(buffer.str());
3535 const size_t new_columns_number = columns_number + 1;
3541 new_matrix.
set(new_column.size(), 1);
3543 new_matrix.
set_column(0, new_column, new_name);
3549 new_matrix.
set(rows_number, new_columns_number);
3551 for(
size_t i = 0; i < rows_number; i++)
3553 for(
size_t j = 0; j < columns_number; j++)
3555 new_matrix(i,j) = (*this)(i,j);
3558 new_matrix(i,columns_number) = new_column[i];
3566 new_header.push_back(new_name);
3583 #ifdef __OPENNN_DEBUG__
3585 if(position > rows_number)
3587 ostringstream buffer;
3589 buffer <<
"OpenNN Exception: Matrix Template.\n"
3590 <<
"insert_row(const size_t&, const Vector<T>&) const.\n"
3591 <<
"Position must be less or equal than number of rows.\n";
3593 throw logic_error(buffer.str());
3596 const size_t size = new_row.size();
3598 if(size != columns_number)
3600 ostringstream buffer;
3602 buffer <<
"OpenNN Exception: Matrix Template.\n"
3603 <<
"insert_row(const size_t&, const Vector<T>&) const.\n"
3604 <<
"Size must be equal to number of columns.\n";
3606 throw logic_error(buffer.str());
3611 const size_t new_rows_number = rows_number + 1;
3613 Matrix<T> new_matrix(new_rows_number, columns_number);
3615 for(
size_t i = 0; i < position; i++)
3617 for(
size_t j = 0; j < columns_number; j++)
3619 new_matrix(i,j) = (*this)(i,j);
3623 for(
size_t j = 0; j < columns_number; j++)
3625 new_matrix(position,j) = new_row[j];
3628 for(
size_t i = position+1; i < new_rows_number; i++)
3630 for(
size_t j = 0; j < columns_number; j++)
3632 new_matrix(i,j) = (*this)(i-1,j);
3650 const size_t values_size = values.size();
3652 for(
size_t i = 0; i < values_size; i++)
3654 (*this)(row_index, column_position + i) = values[i];
3668 #ifdef __OPENNN_DEBUG__
3670 if(position > columns_number)
3672 ostringstream buffer;
3674 buffer <<
"OpenNN Exception: Matrix Template.\n"
3675 <<
"insert_column(const size_t&, const Vector<T>&) const.\n"
3676 <<
"Position must be less or equal than number of columns.\n";
3678 throw logic_error(buffer.str());
3681 const size_t size = static_cast<size_t>(new_column.size());
3683 if(size != rows_number)
3685 ostringstream buffer;
3687 buffer <<
"OpenNN Exception: Matrix Template.\n"
3688 <<
"insert_column(const size_t, const Vector<T>&) const.\n"
3689 <<
"Size(" << size <<
") must be equal to number of rows(" << rows_number <<
").\n";
3691 throw logic_error(buffer.str());
3696 const size_t new_columns_number = columns_number + 1;
3698 Matrix<T> new_matrix(rows_number, new_columns_number);
3700 for(
size_t i = 0; i < rows_number; i++)
3702 for(
size_t j = 0; j < position; j++)
3704 new_matrix(i,j) = (*this)(i,j);
3707 new_matrix(i,position) = new_column[i];
3709 for(
size_t j = position+1; j < new_columns_number; j++)
3711 new_matrix(i,j) = (*this)(i,j-1);
3735 const size_t column_index = get_column_index(column_name);
3737 return insert_column(column_index, new_column, new_name);
3750 #ifdef __OPENNN_DEBUG__
3752 if(position > columns_number)
3754 ostringstream buffer;
3756 buffer <<
"OpenNN Exception: Matrix Template.\n"
3757 <<
"insert_matrix(const size_t& , const Matrix<T>& ) const.\n"
3758 <<
"Position (" << position <<
") must be less or equal than number of columns (" << columns_number <<
").\n";
3760 throw logic_error(buffer.str());
3763 const size_t size = static_cast<size_t>(other_matrix.
get_rows_number());
3765 if(size != rows_number)
3767 ostringstream buffer;
3769 buffer <<
"OpenNN Exception: Matrix Template.\n"
3770 <<
"insert_matrix(const size_t& , const Matrix<T>& ) const.\n"
3771 <<
"Size(" << size <<
") must be equal to number of rows(" << rows_number <<
").\n";
3773 throw logic_error(buffer.str());
3779 const size_t new_columns_number = columns_number + columns_other_matrix;
3781 Matrix<T> new_matrix(rows_number, new_columns_number);
3783 for(
size_t i = 0; i < rows_number; i++)
3785 for(
size_t j = 0; j < position; j++)
3787 new_matrix(i,j) = (*this)(i,j);
3790 for(
size_t j = position ; j < position+columns_other_matrix ; j++)
3792 new_matrix(i,j) = other_matrix(i,j-position);
3795 for(
size_t j = position+columns_other_matrix; j < new_columns_number; j++)
3797 new_matrix(i,j) = (*this)(i,j-columns_other_matrix);
3807 new_matrix.
set_header(old_header.insert_elements(position,new_header));
3821 Matrix<T> new_matrix(rows_number,columns_number+columns_to_add, T());
3825 for(
size_t j = 0; j < columns_number; j++)
3827 new_header[j] = header[j];
3830 for(
int i = 0; i < rows_number; i++)
3832 for(
size_t j = 0; j < columns_number; j++)
3834 new_matrix(i,j) = (*this)(i,j);
3851 Matrix<T> new_matrix(rows_number,columns_number+columns_to_add, T());
3855 for(
size_t j = 0; j < columns_number; j++)
3857 new_header[columns_to_add+j] = header[j];
3860 for(
int i = 0; i < rows_number; i++)
3862 for(
size_t j = 0; j < columns_number; j++)
3864 new_matrix(i,columns_to_add+j) = (*this)(i,j);
3876 const char& delimiter,
const string& missing_value_label)
3878 const size_t column_index = get_column_index(column_name);
3880 const size_t new_columns_name_size = new_columns_name.size();
3882 const size_t new_columns_number = columns_number - 1 + new_columns_name_size;
3884 Matrix<T> new_matrix(rows_number, new_columns_number);
3888 Vector<string> missing_values_vector(new_columns_name_size, missing_value_label);
3890 new_row = get_row(0).replace_element(column_index, new_columns_name);
3891 new_matrix.set_row(0, new_row);
3893 for(
size_t i = 1; i < rows_number; i++)
3895 if((*
this)(i,column_index) == missing_value_label)
3897 new_row = get_row(i).replace_element(column_index, missing_values_vector);
3901 new_row = get_row(i).split_element(column_index, delimiter);
3904 new_matrix.set_row(i, new_row);
3912 void Matrix<T>::split_column(
const string& column_name,
const string& column_1_name,
const string& column_2_name,
3913 const size_t& size_1,
const size_t& size_2)
3915 const size_t column_index = get_column_index(column_name);
3917 const Vector<T> column = get_column(column_index);
3919 Vector<T> column_1(rows_number);
3920 Vector<T> column_2(rows_number);
3922 column_1[0] = column_1_name;
3923 column_2[0] = column_2_name;
3925 for(
size_t i = 1; i < rows_number; i++)
3927 column_1[i] = column[i].substr(0, size_1);
3928 column_2[i] = column[i].substr(size_1, size_2);
3931 set_column(column_index, column_1, column_1_name);
3932 (*this) = insert_column(column_index+1, column_2, column_2_name);
3943 const Vector<T> column_1 = get_column(column_1_index);
3944 const Vector<T> column_2 = get_column(column_2_index);
3946 const string header_1 = header[column_1_index];
3947 const string header_2 = header[column_2_index];
3949 set_column(column_1_index, column_2);
3951 set_column(column_2_index, column_1);
3953 header[column_1_index] = header_2;
3954 header[column_2_index] = header_1;
3965 const size_t column_1_index = get_column_index(column_1_name);
3966 const size_t column_2_index = get_column_index(column_2_name);
3968 swap_columns(column_1_index, column_2_index);
3979 void Matrix<T>::merge_columns(
const string& column_1_name,
const string& column_2_name,
const string& merged_column_name,
const char& separator)
3981 const size_t column_1_index = get_column_index(column_1_name);
3982 const size_t column_2_index = get_column_index(column_2_name);
3984 const Vector<T> column_1 = get_column(column_1_index);
3985 const Vector<T> column_2 = get_column(column_2_index);
3987 Vector<T> merged_column(column_1.size());
3989 for(
size_t i = 0; i < column_1.size(); i++)
3991 merged_column[i] = column_1[i] + separator + column_2[i];
3994 set_column(column_1_index, merged_column);
3996 set_header(column_1_index, merged_column_name);
3998 delete_column(column_2_index);
4010 const Vector<T> column_1 = get_column(column_1_index);
4011 const Vector<T> column_2 = get_column(column_2_index);
4013 Vector<T> merged_column(column_1.size());
4015 for(
size_t i = 0; i < column_1.size(); i++)
4017 merged_column[i] = column_1[i] + separator + column_2[i];
4020 set_column(column_1_index, merged_column);
4022 delete_column(column_2_index);
4028 const string& left_header_tag,
const string& right_header_tag)
const
4032 const size_t columns_1_index = this->get_column_index(columns_1_name);
4033 const size_t columns_2_index = other_matrix.
get_column_index(columns_2_name);
4035 const Vector<T> columns_1 = this->get_column(columns_1_index);
4038 const size_t columns_1_size = columns_1.size();
4040 const Vector<T> header_1 = this->get_header();
4045 Vector<T> merged_header = header_1.delete_element(columns_1_index) + left_header_tag;
4047 merged_header = merged_header.
assemble(header_2.delete_element(columns_2_index) + right_header_tag);
4049 merged_header = merged_header.
insert_element(columns_1_index, columns_1_name);
4051 if(merged_rows_number == 0)
4055 return merged_matrix;
4058 Matrix<T> merged_matrix(merged_rows_number,merged_header.size());
4062 size_t current_merged_row_index = 0;
4067 for(
size_t i = 0; i < columns_1_size; i++)
4069 const T current_index_value = columns_1[i];
4071 pair<typename vector<T>::iterator,
typename vector<T>::iterator> bounds = equal_range(columns_2_sorted_values.begin(), columns_2_sorted_values.end(), current_index_value);
4073 const size_t initial_index = bounds.first - columns_2_sorted_values.begin();
4074 const size_t final_index = bounds.second - columns_2_sorted_values.begin();
4076 for(
size_t j = initial_index; j < final_index; j++)
4078 const size_t current_row_2_index = columns_2_sorted_indices[j];
4080 for(
size_t k = 0; k < columns_number; k++)
4082 merged_matrix(current_merged_row_index,k) = (*this)(i,k);
4085 for(
size_t k = 0; k < other_columns_number; k++)
4087 if(k < columns_2_index)
4089 merged_matrix(current_merged_row_index,k+columns_number) = other_matrix(current_row_2_index,k);
4091 else if(k > columns_2_index)
4093 merged_matrix(current_merged_row_index,k+columns_number-1) = other_matrix(current_row_2_index,k);
4097 current_merged_row_index++;
4101 return merged_matrix;
4106 Matrix<T> Matrix<T>::merge_matrices(
const Matrix<T>& other_matrix,
const size_t& columns_1_index,
const size_t& columns_2_index)
const
4108 const size_t other_columns_number = other_matrix.get_columns_number();
4110 const Vector<T> columns_1 = this->get_column(columns_1_index);
4111 const Vector<T> columns_2 = other_matrix.get_column(columns_2_index);
4113 const size_t columns_1_size = columns_1.size();
4117 if(merged_rows_number == 0)
4119 Matrix<T> merged_matrix;
4121 return merged_matrix;
4124 Matrix<T> merged_matrix(merged_rows_number,columns_number + other_columns_number - 1);
4126 size_t current_merged_row_index = 0;
4128 Vector<T> columns_2_sorted_values = columns_2.sort_ascending_values();
4129 Vector<size_t> columns_2_sorted_indices = columns_2.sort_ascending_indices();
4131 for(
size_t i = 0; i < columns_1_size; i++)
4133 const T current_index_value = columns_1[i];
4135 pair<typename vector<T>::iterator,
typename vector<T>::iterator> bounds = equal_range(columns_2_sorted_values.begin(), columns_2_sorted_values.end(), current_index_value);
4137 const size_t initial_index = bounds.first - columns_2_sorted_values.begin();
4138 const size_t final_index = bounds.second - columns_2_sorted_values.begin();
4140 for(
size_t j = initial_index; j < final_index; j++)
4142 const size_t current_row_2_index = columns_2_sorted_indices[j];
4144 for(
size_t k = 0; k < columns_number; k++)
4146 merged_matrix(current_merged_row_index,k) = (*this)(i,k);
4149 for(
size_t k = 0; k < other_columns_number; k++)
4151 if(k < columns_2_index)
4153 merged_matrix(current_merged_row_index,k+columns_number) = other_matrix(current_row_2_index,k);
4155 else if(k > columns_2_index)
4157 merged_matrix(current_merged_row_index,k+columns_number-1) = other_matrix(current_row_2_index,k);
4161 current_merged_row_index++;
4165 return merged_matrix;
4170 Matrix<T> Matrix<T>::right_join(
const Matrix<T>& other_matrix,
const string& columns_1_name,
const string& columns_2_name,
4171 const string& left_header_tag,
const string& right_header_tag)
const
4173 const size_t columns_1_index = this->get_column_index(columns_1_name);
4174 const size_t columns_2_index = other_matrix.get_column_index(columns_2_name);
4176 const Vector<T> columns_1 = this->get_column(columns_1_index);
4177 const Vector<T> columns_2 = other_matrix.get_column(columns_2_index);
4179 const size_t columns_1_size = columns_1.size();
4180 const size_t columns_2_size = columns_2.size();
4182 const Vector<T> header_1 = this->get_header();
4183 const Vector<T> header_2 = other_matrix.get_header();
4185 Vector<T> merged_header = header_1.delete_element(columns_1_index) + left_header_tag;
4187 merged_header = merged_header.
assemble(header_2.delete_element(columns_2_index) + right_header_tag);
4189 merged_header = merged_header.insert_element(columns_1_index, columns_1_name);
4191 Matrix<T> merged_matrix = other_matrix.add_columns_first(columns_number-1);
4193 merged_matrix = merged_matrix.insert_column(columns_1_index, columns_2);
4194 merged_matrix = merged_matrix.delete_column(columns_number+columns_2_index);
4196 merged_matrix.set_header(merged_header);
4198 Vector<size_t> columns_1_sorted_indices = columns_1.sort_ascending_indices();
4199 Vector<size_t> columns_2_sorted_indices = columns_2.sort_ascending_indices();
4201 size_t columns_1_pointer = 0;
4203 for(
size_t i = 0; i < columns_2_size; i++)
4205 const size_t current_row_index = columns_2_sorted_indices[i];
4207 const T current_index_value = columns_2[current_row_index];
4209 if(columns_1[columns_1_sorted_indices[columns_1_pointer]] > current_index_value)
continue;
4211 while(columns_1_pointer < columns_1_size)
4213 const size_t current_row_1_index = columns_1_sorted_indices[columns_1_pointer];
4215 if(columns_1[current_row_1_index] < current_index_value)
4217 columns_1_pointer++;
4221 else if(columns_1[current_row_1_index] == current_index_value)
4223 for(
size_t k = 0; k < columns_number; k++)
4225 if(k < columns_1_index)
4227 merged_matrix(current_row_index,k) = (*this)(current_row_1_index,k);
4229 else if(k > columns_1_index)
4231 merged_matrix(current_row_index,k) = (*this)(current_row_1_index,k);
4237 else if(columns_1[current_row_1_index] > current_index_value)
4244 return merged_matrix;
4249 Matrix<T> Matrix<T>::left_join(
const Matrix<T>& other_matrix,
const string& columns_1_name,
const string& columns_2_name,
4250 const string& left_header_tag,
const string& right_header_tag)
const
4252 const size_t other_columns_number = other_matrix.get_columns_number();
4253 cout <<
"other_columns_number: " << other_columns_number << endl;
4254 const size_t columns_1_index = this->get_column_index(columns_1_name);
4255 cout <<
"columns_1_index: " << columns_1_index << endl;
4256 const size_t columns_2_index = other_matrix.get_column_index(columns_2_name);
4257 cout <<
"columns_2_index: " << columns_2_index << endl;
4258 const Vector<T> columns_1 = this->get_column(columns_1_index);
4259 cout <<
"columns_1: " << columns_1 << endl;
4260 const Vector<T> columns_2 = other_matrix.get_column(columns_2_index);
4261 cout <<
"columns_2: " << columns_2 << endl;
4263 const size_t columns_1_size = columns_1.size();
4264 const size_t columns_2_size = columns_2.size();
4266 const Vector<T> header_1 = this->get_header();
4267 const Vector<T> header_2 = other_matrix.get_header();
4269 Vector<T> merged_header = header_1.delete_index(columns_1_index) + left_header_tag;
4271 merged_header = merged_header.assemble(header_2.delete_index(columns_2_index) + right_header_tag);
4273 merged_header = merged_header.insert_element(columns_1_index, columns_1_name);
4275 Matrix<T> merged_matrix = this->add_columns(other_columns_number-1);
4277 merged_matrix.set_header(merged_header);
4279 Vector<size_t> columns_1_sorted_indices = columns_1.sort_ascending_indices();
4280 Vector<size_t> columns_2_sorted_indices = columns_2.sort_ascending_indices();
4282 size_t columns_2_pointer = 0;
4284 for(
size_t i = 0; i < columns_1_size; i++)
4286 const size_t current_row_index = columns_1_sorted_indices[i];
4288 const T current_index_value = columns_1[current_row_index];
4290 if(columns_2[columns_2_sorted_indices[columns_2_pointer]] > current_index_value)
continue;
4292 while(columns_2_pointer < columns_2_size)
4294 const size_t current_row_2_index = columns_2_sorted_indices[columns_2_pointer];
4296 if(columns_2[current_row_2_index] < current_index_value)
4298 columns_2_pointer++;
4302 else if(columns_2[current_row_2_index] == current_index_value)
4304 for(
size_t k = 0; k < other_columns_number; k++)
4306 if(k < columns_2_index)
4308 merged_matrix(current_row_index,k+columns_number) = other_matrix(current_row_2_index,k);
4310 else if(k > columns_2_index)
4312 merged_matrix(current_row_index,k+columns_number-1) = other_matrix(current_row_2_index,k);
4318 else if(columns_2[current_row_2_index] > current_index_value)
4324 if(columns_2_pointer >= columns_2_size)
4330 return merged_matrix;
4335 Matrix<T> Matrix<T>::left_join(
const Matrix<T>& other_matrix,
const size_t& columns_1_index,
const size_t& columns_2_index)
const
4337 const size_t other_columns_number = other_matrix.get_columns_number();
4339 const Vector<T> columns_1 = this->get_column(columns_1_index);
4340 const Vector<T> columns_2 = other_matrix.get_column(columns_2_index);
4342 const size_t columns_1_size = columns_1.size();
4343 const size_t columns_2_size = columns_2.size();
4345 Matrix<T> merged_matrix = this->add_columns(other_columns_number-1);
4347 Vector<size_t> columns_1_sorted_indices = columns_1.sort_ascending_indices();
4348 Vector<size_t> columns_2_sorted_indices = columns_2.sort_ascending_indices();
4350 size_t columns_2_pointer = 0;
4352 for(
size_t i = 0; i < columns_1_size; i++)
4354 const size_t current_row_index = columns_1_sorted_indices[i];
4356 const T current_index_value = columns_1[current_row_index];
4358 if(columns_2[columns_2_sorted_indices[columns_2_pointer]] > current_index_value)
continue;
4363 while(columns_2_pointer < columns_2_size)
4365 const size_t current_row_2_index = columns_2_sorted_indices[columns_2_pointer];
4367 if(columns_2[current_row_2_index] < current_index_value)
4369 columns_2_pointer++;
4373 else if(columns_2[current_row_2_index] == current_index_value)
4375 for(
size_t k = 0; k < other_columns_number; k++)
4377 if(k < columns_2_index)
4379 merged_matrix(current_row_index,k+columns_number) = other_matrix(current_row_2_index,k);
4381 else if(k > columns_2_index)
4383 merged_matrix(current_row_index,k+columns_number-1) = other_matrix(current_row_2_index,k);
4389 else if(columns_2[current_row_2_index] > current_index_value)
4395 if(columns_2_pointer >= columns_2_size)
4401 return merged_matrix;
4406 Matrix<T> Matrix<T>::left_join(
const Matrix<T>& other_matrix,
const string& matrix_1_name_1,
const string& matrix_1_name_2,
const string& matrix_2_name_1,
const string& matrix_2_name_2,
4407 const string& left_header_tag,
const string& right_header_tag)
const
4409 const size_t other_columns_number = other_matrix.get_columns_number();
4411 const size_t matrix_1_columns_1_index = this->get_column_index(matrix_1_name_1);
4412 const size_t matrix_1_columns_2_index = this->get_column_index(matrix_1_name_2);
4414 const size_t matrix_2_columns_1_index = other_matrix.get_column_index(matrix_2_name_1);
4415 const size_t matrix_2_columns_2_index = other_matrix.get_column_index(matrix_2_name_2);
4417 const Vector<T> matrix_1_columns_1 = this->get_column(matrix_1_columns_1_index);
4418 const Vector<T> matrix_1_columns_2 = this->get_column(matrix_1_columns_2_index);
4419 const Vector<T> matrix_2_columns_1 = other_matrix.get_column(matrix_2_columns_1_index);
4420 const Vector<T> matrix_2_columns_2 = other_matrix.get_column(matrix_2_columns_2_index);
4422 const size_t matrix_1_columns_1_size = matrix_1_columns_1.size();
4423 const size_t matrix_1_columns_2_size = matrix_1_columns_2.size();
4424 const size_t matrix_2_columns_1_size = matrix_2_columns_1.size();
4426 const Vector<T> header_1 = this->get_header();
4427 const Vector<T> header_2 = other_matrix.get_header();
4429 Vector<T> merged_header = header_1.delete_element(matrix_1_columns_2_index).delete_element(matrix_1_columns_1_index) + left_header_tag;
4431 merged_header = merged_header.assemble(header_2.delete_element(matrix_2_columns_2_index).delete_element(matrix_2_columns_1_index) + right_header_tag);
4433 merged_header = merged_header.insert_element(matrix_1_columns_1_index, matrix_1_name_1).insert_element(matrix_1_columns_2_index,matrix_1_name_2);
4435 Matrix<T> merged_matrix = this->add_columns(other_columns_number-2);
4437 merged_matrix.set_header(merged_header);
4439 Vector<size_t> columns_1_sorted_indices = matrix_1_columns_1.sort_ascending_indices();
4440 Vector<size_t> columns_2_sorted_indices = matrix_2_columns_1.sort_ascending_indices();
4442 size_t columns_2_pointer = 0;
4444 for(
size_t i = 0; i < matrix_1_columns_1_size; i++)
4446 const size_t current_row_index = columns_1_sorted_indices[i];
4448 const T current_index_1_value = matrix_1_columns_1[current_row_index];
4449 const T current_index_2_value = matrix_1_columns_2[current_row_index];
4451 if(matrix_2_columns_1[columns_2_sorted_indices[columns_2_pointer]] > current_index_1_value)
continue;
4453 while(columns_2_pointer < matrix_1_columns_2_size)
4455 const size_t current_row_2_index = columns_2_sorted_indices[columns_2_pointer];
4457 if(matrix_2_columns_1[current_row_2_index] < current_index_1_value)
4459 columns_2_pointer++;
4463 else if(matrix_2_columns_1[current_row_2_index] == current_index_1_value && stod(matrix_2_columns_2[current_row_2_index]) == stod(current_index_2_value))
4465 for(
size_t k = 0; k < other_columns_number; k++)
4467 if(k < matrix_2_columns_1_index && k < matrix_2_columns_2_index)
4469 merged_matrix(current_row_index,k+columns_number) = other_matrix(current_row_2_index,k);
4471 else if(k > matrix_2_columns_1_index && k < matrix_2_columns_2_index)
4473 merged_matrix(current_row_index,k+columns_number-1) = other_matrix(current_row_2_index,k);
4475 else if(k > matrix_2_columns_1_index && k > matrix_2_columns_2_index)
4477 merged_matrix(current_row_index,k+columns_number-2) = other_matrix(current_row_2_index,k);
4483 else if(matrix_2_columns_1[current_row_2_index] == current_index_1_value && stod(matrix_2_columns_2[current_row_2_index]) != stod(current_index_2_value))
4485 columns_2_pointer++;
4489 else if(matrix_2_columns_1[current_row_2_index] > current_index_1_value)
4495 if(columns_2_pointer >= matrix_2_columns_1_size)
break;
4498 return merged_matrix;
4509 #ifdef __OPENNN_DEBUG__
4511 if(row_index > rows_number)
4513 ostringstream buffer;
4515 buffer <<
"OpenNN Exception: Matrix Template.\n"
4516 <<
"Matrix<T> delete_row(const size_t&) const.\n"
4517 <<
"Index of row must be less than number of rows.\n"
4518 <<
"row index: " << row_index <<
"rows_number" << rows_number <<
"\n";
4520 throw logic_error(buffer.str());
4522 else if(rows_number < 2)
4524 ostringstream buffer;
4526 buffer <<
"OpenNN Exception: Matrix Template.\n"
4527 <<
"Matrix<T> delete_row(const size_t&) const.\n"
4528 <<
"Number of rows must be equal or greater than two.\n";
4530 throw logic_error(buffer.str());
4535 Matrix<T> new_matrix(rows_number-1, columns_number);
4537 for(
size_t i = 0; i < row_index; i++)
4539 for(
size_t j = 0; j < columns_number; j++)
4541 new_matrix(i,j) = (*this)(i,j);
4545 for(
size_t i = row_index+1; i < rows_number; i++)
4547 for(
size_t j = 0; j < columns_number; j++)
4549 new_matrix(i-1,j) = (*this)(i,j);
4569 const size_t rows_to_delete_number = rows_to_remove.size();
4571 if(rows_to_delete_number == 0)
return Matrix<T>(*
this);
4573 const size_t rows_to_keep_number = rows_number - rows_to_delete_number;
4579 for(
size_t i = 0; i < rows_number; i++)
4583 rows_to_keep[index] = i;
4589 return get_submatrix_rows(rows_to_keep);
4604 for(
size_t i = 0; i < rows_number; i++)
4625 for(
size_t i = 0; i < rows_number; i++)
4636 return get_submatrix_rows(indices);
4651 for(
size_t i = 0; i < columns_number; i++)
4653 column = get_column(i);
4672 for(
size_t i = 0; i < columns_number; i++)
4674 column = get_column(i);
4683 return get_submatrix_columns(indices);
4696 return delete_rows(indices);
4709 return get_submatrix_rows(indices);
4722 return get_submatrix_columns(indices);
4734 return get_submatrix_rows(indices);
4746 return get_submatrix_columns(indices);
4756 return (*
this)(0, column_index);
4766 const size_t column_index = get_column_index(column_name);
4768 return (*
this)(0, column_index);
4778 return (*
this)(rows_number-1, column_index);
4789 const size_t column_index = get_column_index(column_name);
4791 return (*
this)(rows_number-1, column_index);
4804 return get_submatrix_rows(indices);
4817 return get_submatrix_columns(indices);
4828 const size_t columns_number = get_columns_number();
4830 const Vector<size_t> indices(columns_number-number, 1, columns_number-1);
4832 return get_submatrix_columns(indices);
4843 const size_t rows_number = get_rows_number();
4845 const Vector<size_t> indices(rows_number-number, 1, rows_number-1);
4847 return get_submatrix_rows(indices);
4858 #ifdef __OPENNN_DEBUG__
4860 if(column_index >= columns_number)
4862 ostringstream buffer;
4864 buffer <<
"OpenNN Exception: Matrix Template.\n"
4865 <<
"Matrix<T> delete_column(const size_t&) const.\n"
4866 <<
"Index of column must be less than number of columns.\n";
4868 throw logic_error(buffer.str());
4870 else if(columns_number < 2)
4872 ostringstream buffer;
4874 buffer <<
"OpenNN Exception: Matrix Template.\n"
4875 <<
"Matrix<T> delete_column(const size_t&) const.\n"
4876 <<
"Number of columns must be equal or greater than two.\n";
4878 throw logic_error(buffer.str());
4883 Matrix<T> new_matrix(rows_number, columns_number-1);
4885 for(
size_t i = 0; i < rows_number; i++)
4887 for(
size_t j = 0; j < column_index; j++)
4889 new_matrix(i,j) = (*this)(i,j);
4893 for(
size_t i = 0; i < rows_number; i++)
4895 for(
size_t j = column_index+1; j < columns_number; j++)
4897 new_matrix(i,j-1) = (*this)(i,j);
4921 const size_t occurrences_number = indices.size();
4923 if(occurrences_number == 0)
4927 else if(occurrences_number == 1)
4929 return delete_column(indices[0]);
4933 ostringstream buffer;
4935 buffer <<
"OpenNN Exception: Matrix Template.\n"
4936 <<
"void Matrix<T>::delete_column_by_name(const string& column_name).\n"
4937 <<
"Number of columns with name " << column_name <<
" is " << indices.size() <<
").\n";
4939 throw logic_error(buffer.str());
4951 if(delete_indices.empty())
4960 for(
size_t i = 0; i < columns_number; i++)
4964 keep_indices.push_back(i);
4968 const size_t keep_indices_size = keep_indices.size();
4970 if(keep_indices_size != columns_number)
4972 new_data = get_submatrix_columns(keep_indices);
4974 if(!header.empty()) new_data.
set_header(header.get_subvector(keep_indices));
4988 const Vector<size_t> indices = get_columns_indices(delete_names);
4990 return delete_columns(indices);
5003 for(
size_t i = 0; i < columns_number; i++)
5005 for(
size_t j = 0; j < substrings.size(); j++)
5007 if(header[i].find(substrings[j]) != string::npos)
5009 indices.push_back(i);
5016 return delete_columns(indices);
5027 for(
size_t i = 0; i < columns_number; i++)
5029 if(is_column_constant(i))
5031 constant_columns.push_back(i);
5035 return constant_columns;
5045 return delete_columns(get_constant_columns_indices());
5055 const Vector<size_t> binary_columns = get_binary_columns_indices();
5057 return delete_columns(binary_columns);
5067 const Vector<size_t> binary_columns = get_binary_columns_indices();
5069 const size_t binary_columns_number = binary_columns.size();
5073 for(
size_t i = 0; i < binary_columns_number; i++)
5075 const double support = get_column(binary_columns[i]).
calculate_sum()/static_cast<double>(rows_number);
5077 if(support < minimum_support) columns_to_remove.push_back(binary_columns[i]);
5080 return delete_columns(columns_to_remove);
5094 for(
size_t i = 0; i < rows_number; i++)
5100 constant_rows.push_back(i);
5104 return delete_rows(constant_rows);
5114 #ifdef __OPENNN_DEBUG__
5118 if(other_columns_number != columns_number)
5120 ostringstream buffer;
5122 buffer <<
"OpenNN Exception: Matrix Template.\n"
5123 <<
"Matrix<T> assemble_rows(const Matrix<T>&) const method.\n"
5124 <<
"Number of columns of other matrix (" << other_columns_number <<
") must be equal to number of columns of this matrix (" << columns_number <<
").\n";
5126 throw logic_error(buffer.str());
5133 if(rows_number == 0 && other_rows_number == 0)
5137 else if(rows_number == 0)
5139 return other_matrix;
5142 Matrix<T> assembly(rows_number + other_rows_number, columns_number);
5144 for(
size_t i = 0; i < rows_number; i++)
5146 for(
size_t j = 0; j < columns_number; j++)
5148 assembly(i,j) = (*this)(i,j);
5152 for(
size_t i = 0; i < other_rows_number; i++)
5154 for(
size_t j = 0; j < columns_number; j++)
5156 assembly(rows_number+i,j) = other_matrix(i,j);
5180 Matrix<T> sorted(rows_number, columns_number);
5182 const Vector<T> column = get_column(column_index);
5188 for(
size_t i = 0; i < rows_number; i++)
5192 for(
size_t j = 0; j < columns_number; j++)
5194 sorted(i,j) = (*this)(index, j);
5209 Matrix<T> sorted(rows_number, columns_number);
5217 for(
size_t i = 0; i < rows_number; i++)
5221 for(
size_t j = 0; j < columns_number; j++)
5223 sorted(i,j) = (*this)(index, j);
5227 if(!header.empty()) sorted.
set_header(header);
5240 Matrix<T> sorted(rows_number, columns_number);
5248 for(
size_t i = 0; i < rows_number; i++)
5252 for(
size_t j = 0; j < columns_number; j++)
5254 sorted(i,j) = (*this)(index, j);
5258 if(!header.empty()) sorted.
set_header(header);
5267 #ifdef __OPENNN_DEBUG__
5268 const size_t rank_size = rank.size();
5270 if(rows_number != rank_size) {
5271 ostringstream buffer;
5273 buffer <<
"OpenNN Exception: Matrix Template.\n"
5274 <<
"Matrix<T> sort_rank_rows(const Vector<size_t>&) const.\n"
5275 <<
"Matrix number of rows is " << rows_number <<
" and rank size is " << rank_size
5276 <<
" and they must be the same.\n";
5278 throw logic_error(buffer.str());
5283 Matrix<T> sorted_matrix(rows_number,columns_number);
5285 for(
size_t i = 0; i < rows_number; i++)
5287 sorted_matrix.set_row(i,this->get_row(rank[i]));
5290 if(!header.empty()) sorted_matrix.set_header(header);
5292 return sorted_matrix;
5297 Matrix<T> Matrix<T>::sort_columns(
const Vector<size_t>& rank)
const
5299 #ifdef __OPENNN_DEBUG__
5301 const size_t rank_size = rank.size();
5303 if(rows_number != rank_size) {
5304 ostringstream buffer;
5306 buffer <<
"OpenNN Exception: Matrix Template.\n"
5307 <<
"Matrix<T> sort_rank_rows(const Vector<size_t>&) const.\n"
5308 <<
"Matrix number of rows is " << rows_number <<
" and rank size is " << rank_size
5309 <<
" and they must be the same.\n";
5311 throw logic_error(buffer.str());
5316 Matrix<T> sorted_matrix(rows_number,columns_number);
5318 for(
size_t i = 0; i < columns_number; i++)
5320 sorted_matrix.set_column(i,this->get_column(rank[i]),this->get_header()[rank[i]]);
5323 sorted_matrix.set_header(header);
5325 return sorted_matrix;
5330 Matrix<T> Matrix<T>::sort_columns(
const Vector<string>& new_header)
const
5332 #ifdef __OPENNN_DEBUG__
5334 if(columns_number != new_header.size()) {
5335 ostringstream buffer;
5337 buffer <<
"OpenNN Exception: Matrix Template.\n"
5338 <<
"Matrix<T> sort_columns(const Vector<string>& new_header) const.\n"
5339 <<
"New header size doesn't match with columns number.\n";
5341 throw logic_error(buffer.str());
5344 const size_t count = new_header.count_equal_to(this->get_header());
5346 if(count != new_header.size()) {
5347 ostringstream buffer;
5349 buffer <<
"OpenNN Exception: Matrix Template.\n"
5350 <<
"Matrix<T> sort_columns(const Vector<string>& new_header) const.\n"
5351 <<
"Occurrences number doesn't match with columns number\n";
5353 throw logic_error(buffer.str());
5358 Matrix<T> sorted_matrix(rows_number,columns_number);
5360 for(
size_t i = 0; i < new_header.size(); i++)
5362 const string current_variable = new_header[i];
5364 for(
size_t j = 0; j < columns_number; j++)
5366 const string current_column_name = this->get_header()[j];
5368 if(current_variable == current_column_name)
5370 sorted_matrix.set_column(i,this->get_column(j),current_variable);
5377 return sorted_matrix;
5388 Matrix<T> sorted(rows_number, columns_number);
5390 const Vector<T> column = get_column(column_index);
5396 for(
size_t i = 0; i < rows_number; i++)
5400 for(
size_t j = 0; j < columns_number; j++)
5402 sorted(i,j) = (*this)(index, j);
5415 #ifdef __OPENNN_DEBUG__
5419 if(other_rows_number != rows_number)
5421 ostringstream buffer;
5423 buffer <<
"OpenNN Exception: Matrix Template.\n"
5424 <<
"Matrix<T> assemble_columns(const Matrix<T>&) const method.\n"
5425 <<
"Number of rows of other matrix (" << other_rows_number <<
") must be equal to number of rows of this matrix (" << rows_number <<
").\n";
5427 throw logic_error(buffer.str());
5434 Matrix<T> assembly(rows_number, columns_number + other_columns_number);
5436 for(
size_t i = 0; i < rows_number; i++)
5438 for(
size_t j = 0; j < columns_number; j++)
5440 assembly(i,j) = (*this)(i,j);
5442 for(
size_t j = 0; j < other_columns_number; j++)
5444 assembly(i,columns_number+j) = other_matrix(i,j);
5460 fill(this->begin(),this->end(), value);
5469 for(
size_t i = 0; i < this->size(); i++) {
5470 (*this)[i] = static_cast<T>(i);
5482 const size_t size = this->size();
5484 for(
size_t i = 0; i < size; i++)
5486 if((*
this)[i] == find_what)
5488 (*this)[i] = replace_with;
5500 for(
size_t i = 0; i < columns_number; i++)
5501 if(header[i] == find_what)
5502 header[i] = replace_with;
5514 for(
size_t i = 0; i < columns_number; i++)
5516 if((*
this)(row_index,i) == find_what)
5518 (*this)(row_index,i) = replace_with;
5532 for(
size_t i = 0; i < rows_number; i++)
5534 if((*
this)(i, column_index) == find_what)
5536 (*this)(i, column_index) = replace_with;
5550 const size_t column_index = get_column_index(column_name);
5552 replace_in_column(column_index, find_what, replace_with);
5562 const size_t size = this->size();
5564 for(
size_t i = 0; i < size; i++)
5566 size_t position = 0;
5568 while((position = (*
this)[i].find(find_what, position)) != string::npos)
5570 (*this)[i].replace(position, find_what.length(), replace_with);
5572 position += replace_with.length();
5586 for(
size_t i = 0; i < rows_number; i++)
5588 size_t position = 0;
5590 while((position = (*
this)(i, column_index).find(find_what, position)) != string::npos)
5592 (*this)(i, column_index).replace(position, find_what.length(), replace_with);
5594 position += replace_with.length();
5608 const size_t column_index = get_column_index(column_name);
5610 replace_substring(column_index, find_what, replace_with);
5621 const size_t size = this->size();
5623 for(
size_t i = 0; i < size; i++)
5625 if((*
this)[i].find(find_what) != string::npos)
5627 (*this)[i] = replace_with;
5641 for(
size_t i = 0; i < columns_number; i++)
5643 if((*
this)(row_index, i).find(find_what) != string::npos)
5645 (*this)(row_index, i) = replace_with;
5659 const size_t column_index = get_column_index(column_name);
5661 for(
size_t i = 0; i < rows_number; i++)
5663 if((*
this)(i,column_index) == find_value)
5665 (*this)(i,column_index) = replace_value;
5679 for(
size_t i = 0; i < rows_number; i++)
5681 if((*
this)(i,column_index) == find_value)
5683 (*this)(i,column_index) = replace_value;
5697 const size_t column_index = get_column_index(column_name);
5699 for(
size_t i = 0; i < rows_number; i++)
5701 if((*
this)(i,column_index) != find_value)
5703 (*this)(i,column_index) = replace_value;
5717 const size_t column_index = get_column_index(column_name);
5719 for(
size_t i = 0; i < rows_number; i++)
5721 if(!find_values.
contains((*
this)(i,column_index)))
5723 (*this)(i,column_index) = replace_value;
5732 const size_t column_index = get_column_index(name);
5736 for(
size_t i = 0; i < row_indices.size(); i++)
5738 (*this)(row_indices[i], column_index) = replace;
5751 const size_t column_index = get_column_index(column_name);
5753 for(
size_t i = 0; i < rows_number; i++)
5755 if((*
this)(i,column_index).find(find_what) != string::npos)
5757 (*this)(i,column_index) = replace_with;
5770 const size_t column_index = get_column_index(column_name);
5774 for(
size_t i = 0; i < rows_number; i++)
5776 if((*
this)(i,column_index).find(find_what) != string::npos)
5793 for(
size_t i = 0; i < rows_number; i++)
5795 for(
size_t j = 0; j < columns_number; j++)
5797 if((*
this)(i,j) == value) occurrences[j]++;
5812 for(
size_t i = 0; i < rows_number; i++)
5814 if((*
this)(i,column_index) == value)
5832 #ifdef __OPENNN_DEBUG__
5834 if(minimum > maximum)
5836 ostringstream buffer;
5838 buffer <<
"OpenNN Exception: Matrix Template.\n"
5839 <<
"void randomize_uniform(const double&, const double&) const method.\n"
5840 <<
"Minimum value must be less or equal than maximum value.\n";
5842 throw logic_error(buffer.str());
5847 for(
size_t i = 0; i < this->size(); i++)
5849 (*this)[i] = static_cast<T>(calculate_random_uniform(minimum, maximum));
5864 #ifdef __OPENNN_DEBUG__
5866 if(minimums.size() != columns_number)
5868 ostringstream buffer;
5870 buffer <<
"OpenNN Exception: Matrix Template.\n"
5871 <<
"void randomize_uniform(const Vector<double>&, const Vector<double>&) const method.\n"
5872 <<
"Size of minimums must be equal to number of columns.\n";
5874 throw logic_error(buffer.str());
5877 if(maximums.size() != columns_number)
5879 ostringstream buffer;
5881 buffer <<
"OpenNN Exception: Matrix Template.\n"
5882 <<
"void randomize_uniform(const Vector<double>&, const Vector<double>&) const method.\n"
5883 <<
"Size of maximums must be equal to number of columns.\n";
5885 throw logic_error(buffer.str());
5888 if(minimums > maximums)
5890 ostringstream buffer;
5892 buffer <<
"OpenNN Exception: Matrix Template.\n"
5893 <<
"void randomize_uniform(const Vector<double>&, const Vector<double>&) const method.\n"
5894 <<
"Minimums must be less or equal than maximums.\n";
5896 throw logic_error(buffer.str());
5903 for(
size_t i = 0; i < columns_number; i++)
5907 set_column(i, column);
5922 #ifdef __OPENNN_DEBUG__
5924 if(minimum > maximum)
5926 ostringstream buffer;
5928 buffer <<
"OpenNN Exception: Matrix Template.\n"
5929 <<
"void randomize_uniform(const Matrix<double>&, const Matrix<double>&) const method.\n"
5930 <<
"Minimum values must be less or equal than their respective maximum values.\n";
5932 throw logic_error(buffer.str());
5938 for(
size_t i = 0; i < this->size(); i++)
5940 (*this)[i] = calculate_random_uniform(minimum[i], maximum[i]);
5954 #ifdef __OPENNN_DEBUG__
5956 if(standard_deviation < 0.0)
5958 ostringstream buffer;
5960 buffer <<
"OpenNN Exception: Matrix Template.\n"
5961 <<
"void randomize_normal(const double&, const double&) method.\n"
5962 <<
"Standard deviation must be equal or greater than zero.\n";
5964 throw logic_error(buffer.str());
5969 for(
size_t i = 0; i < this->size(); i++)
5971 (*this)[i] = calculate_random_normal(mean, standard_deviation);
5986 #ifdef __OPENNN_DEBUG__
5988 if(means.size() != columns_number)
5990 ostringstream buffer;
5992 buffer <<
"OpenNN Exception: Matrix Template.\n"
5993 <<
"void randomize_normal(const Vector<double>&, const Vector<double>&) const method.\n"
5994 <<
"Size of means must be equal to number of columns.\n";
5996 throw logic_error(buffer.str());
5999 if(standard_deviations.size() != columns_number)
6001 ostringstream buffer;
6003 buffer <<
"OpenNN Exception: Matrix Template.\n"
6004 <<
"void randomize_normal(const Vector<double>&, const Vector<double>&) const method.\n"
6005 <<
"Size of standard deviations must be equal to number of columns.\n";
6007 throw logic_error(buffer.str());
6012 ostringstream buffer;
6014 buffer <<
"OpenNN Exception: Matrix Template.\n"
6015 <<
"void randomize_normal(const Vector<double>&, const Vector<double>&) const method.\n"
6016 <<
"Means must be less or equal than zero.\n";
6018 throw logic_error(buffer.str());
6025 for(
size_t i = 0; i < columns_number; i++)
6029 set_column(i, column);
6042 #ifdef __OPENNN_DEBUG__
6044 if(standard_deviation < 0.0)
6046 ostringstream buffer;
6048 buffer <<
"OpenNN Exception: Matrix Template.\n"
6049 <<
"void randomize_normal(const Matrix<double>&, const Matrix<double>&) const method.\n"
6050 <<
"Standard deviations must be equal or greater than zero.\n";
6052 throw logic_error(buffer.str());
6057 for(
size_t i = 0; i < this->size(); i++)
6059 (*this)[i] = calculate_random_uniform(mean[i], standard_deviation[i]);
6071 #ifdef __OPENNN_DEBUG__
6073 if(rows_number != columns_number)
6075 ostringstream buffer;
6077 cout <<
"OpenNN Exception: Matrix Template.\n"
6078 <<
"initialize_identity() const method.\n"
6079 <<
"Matrix must be square.\n";
6081 throw logic_error(buffer.str());
6086 this->initialize(0);
6088 for(
size_t i = 0; i < rows_number; i++)
6103 #ifdef __OPENNN_DEBUG__
6105 if(rows_number != columns_number)
6107 ostringstream buffer;
6109 cout <<
"OpenNN Exception: Matrix Template.\n"
6110 <<
"initialize_diagonal(const T&) const method.\n"
6111 <<
"Matrix must be square.\n";
6113 throw logic_error(buffer.str());
6118 for(
size_t i = 0; i < rows_number; i++)
6120 for(
size_t j = 0; j < columns_number; j++)
6124 (*this)(i,j) = value;
6142 for(
size_t i = 0; i < this->size(); i++)
6156 #ifdef __OPENNN_DEBUG__
6160 ostringstream buffer;
6162 cout <<
"OpenNN Exception: Matrix Template.\n"
6163 <<
"Vector<T> calculate_rows_sum() const method.\n"
6164 <<
"Matrix is empty.\n";
6166 throw logic_error(buffer.str());
6173 for(
size_t i = 0; i < rows_number; i++)
6175 for(
size_t j = 0; j < columns_number; j++)
6177 rows_sum[i] += (*this)(i,j);
6192 #ifdef __OPENNN_DEBUG__
6196 ostringstream buffer;
6198 cout <<
"OpenNN Exception: Matrix Template.\n"
6199 <<
"Vector<T> calculate_columns_sum() const method.\n"
6200 <<
"Matrix is empty.\n";
6202 throw logic_error(buffer.str());
6207 Vector<T> columns_sum(columns_number, 0);
6209 for(
size_t i = 0; i < rows_number; i++)
6211 for(
size_t j = 0; j < columns_number; j++)
6213 columns_sum[j] += (*this)(i,j);
6217 return(columns_sum);
6229 for(
size_t i = 0; i < rows_number; i++)
6231 column_sum += (*this)(i,column_index);
6246 for(
size_t i = 0; i < columns_number; i++)
6248 row_sum += (*this)(row_index,i);
6263 #ifdef __OPENNN_DEBUG__
6265 if(vector.size() != columns_number)
6267 ostringstream buffer;
6269 cout <<
"OpenNN Exception: Matrix Template.\n"
6270 <<
"void sum_row(const size_t&, const Vector<T>&) method.\n"
6271 <<
"Size of vector must be equal to number of columns.\n";
6273 throw logic_error(buffer.str());
6278 for(
size_t j = 0; j < columns_number; j++)
6280 (*this)(row_index,j) += vector[j];
6293 #ifdef __OPENNN_DEBUG__
6295 if(vector.size() != rows_number)
6297 ostringstream buffer;
6299 cout <<
"OpenNN Exception: Matrix Template.\n"
6300 <<
"void sum_column(const size_t&, const Vector<T>&) method.\n"
6301 <<
"Size of vector must be equal to number of rows.\n";
6303 throw logic_error(buffer.str());
6308 for(
size_t i = 0; i < rows_number; i++)
6310 (*this)(i,column_index) += vector[i];
6323 #ifdef __OPENNN_DEBUG__
6325 if(vector.size() != columns_number)
6327 ostringstream buffer;
6329 cout <<
"OpenNN Exception: Matrix Template.\n"
6330 <<
"void sum_rows(const Vector<T>&) method.\n"
6331 <<
"Size of vector must be equal to number of columns.\n";
6333 throw logic_error(buffer.str());
6338 Matrix<T> new_matrix(rows_number, columns_number);
6340 for(
size_t i = 0; i < rows_number; i++)
6342 for(
size_t j = 0; j < columns_number; j++)
6344 new_matrix(i,j) = (*this)(i,j) + vector[j];
6360 #ifdef __OPENNN_DEBUG__
6362 if(vector.size() != columns_number)
6364 ostringstream buffer;
6366 cout <<
"OpenNN Exception: Matrix Template.\n"
6367 <<
"void subtract_rows(const Vector<T>&) method.\n"
6368 <<
"Size of vector must be equal to number of columns.\n";
6370 throw logic_error(buffer.str());
6375 Matrix<T> new_matrix(rows_number, columns_number);
6377 for(
size_t i = 0; i < rows_number; i++)
6379 for(
size_t j = 0; j < columns_number; j++)
6381 new_matrix(i,j) = (*this)(i,j) - vector[j];
6398 #ifdef __OPENNN_DEBUG__
6400 if(vector.size() != columns_number)
6402 ostringstream buffer;
6404 cout <<
"OpenNN Exception: Matrix Template.\n"
6405 <<
"void multiply_rows(const Vector<T>&) method.\n"
6406 <<
"Size of vector must be equal to number of columns.\n";
6408 throw logic_error(buffer.str());
6413 Matrix<T> new_matrix(rows_number, columns_number);
6415 for(
size_t i = 0; i < rows_number; i++)
6417 for(
size_t j = 0; j < columns_number; j++)
6419 new_matrix(i,j) = (*this)(i,j) * vector[j];
6437 for(
size_t point_number = 0; point_number < points_number; point_number++)
6439 new_vector_matrix[point_number].
set(rows_number, columns_number, 0.0);
6441 for(
size_t i = 0; i < rows_number; i++)
6443 for(
size_t j = 0; j < columns_number; j++)
6445 new_vector_matrix[point_number](i,j) = (*
this)(i,j)*matrix(point_number,j);
6450 return new_vector_matrix;
6462 #ifdef __OPENNN_DEBUG__
6466 ostringstream buffer;
6468 buffer <<
"OpenNN Exception: Matrix template.\n"
6469 <<
"double calculate_trace() const method.\n"
6470 <<
"Matrix is not square.\n";
6472 throw logic_error(buffer.str());
6479 for(
size_t i = 0; i < rows_number; i++)
6481 trace += (*this)(i,i);
6497 for(
size_t i = 0; i < columns_number; i++)
6499 column = get_column(i);
6501 missing_values[i] = column.
count_NAN()*100.0/static_cast<double>(rows_number-1.0);
6504 return missing_values;
6518 for(
size_t i = 0; i < rows_number; i++)
6520 for(
size_t j = 0; j < columns_number; j++)
6522 if((*
this)(i,j) < value && indices.
empty())
6531 else if((*
this)(i,j) < value)
6555 for(
size_t i = 0; i < rows_number; i++)
6557 for(
size_t j = 0; j < columns_number; j++)
6559 if((*
this)(i,j) > value && indices.
empty())
6568 else if((*
this)(i,j) > value)
6587 Matrix<T> reverse(rows_number,columns_number);
6589 for(
size_t i = 0; i < rows_number; i++)
6591 for(
size_t j = 0; j < columns_number; j++)
6593 reverse(i,j) = (*this)(i, columns_number-j-1);
6599 for(
size_t i = 0; i < columns_number; i++)
6601 reverse_header[i] = header[columns_number-i-1];
6615 Matrix<T> transpose(columns_number, rows_number);
6617 for(
size_t i = 0; i < columns_number; i++)
6619 for(
size_t j = 0; j < rows_number; j++)
6621 transpose(i,j) = (*this)(j,i);
6635 #ifdef __OPENNN_DEBUG__
6637 if(rows_number != vector.size())
6639 ostringstream buffer;
6641 buffer <<
"OpenNN Exception: Matrix Template.\n"
6642 <<
"divide_rows(const Vector<T>&) method.\n"
6643 <<
"Size of vector (" << vector.size() <<
") must be equal to number of rows (" << rows_number <<
").\n";
6645 throw logic_error(buffer.str());
6650 for(
size_t i = 0; i < rows_number; i++)
6652 for(
size_t j = 0; j < columns_number; j++)
6654 (*this)(i,j) /= vector[i];
6667 for(
size_t i = 0; i < this->size(); i++)
6669 if((*
this)[i] < minimum)(*
this)[i] = minimum;
6671 else if((*
this)[i] > maximum)(*
this)[i] = maximum;
6682 Matrix<T> sum(rows_number, columns_number);
6684 transform(this->begin(), this->end(), sum.begin(), bind2nd(plus<T>(), scalar));
6698 #ifdef __OPENNN_DEBUG__
6703 if(other_rows_number != rows_number || other_columns_number != columns_number)
6705 ostringstream buffer;
6707 buffer <<
"OpenNN Exception: Matrix Template.\n"
6708 <<
"Matrix<T> operator + (const Matrix<T>&) const.\n"
6709 <<
"Sizes of other matrix (" << other_rows_number <<
"," << other_columns_number <<
") must be the same than sizes of this matrix (" << rows_number <<
"," << columns_number <<
").\n";
6711 throw logic_error(buffer.str());
6716 Matrix<T> sum(rows_number, columns_number);
6718 transform(this->begin(), this->end(), other_matrix.begin(), sum.begin(), plus<T>());
6730 Matrix<T> difference(rows_number, columns_number);
6732 transform(this->begin(), this->end(), difference.begin(), bind2nd(minus<T>(), scalar));
6744 #ifdef __OPENNN_DEBUG__
6749 if(other_rows_number != rows_number || other_columns_number != columns_number)
6751 ostringstream buffer;
6753 buffer <<
"OpenNN Exception: Matrix Template.\n"
6754 <<
"Matrix<T> operator -(const Matrix<T>&) const method.\n"
6755 <<
"Sizes of other matrix (" << other_rows_number <<
"," << other_columns_number <<
") must be equal to sizes of this matrix ("<< rows_number <<
"," << columns_number <<
").\n";
6757 throw logic_error(buffer.str());
6762 Matrix<T> difference(rows_number, columns_number);
6764 transform(this->begin(), this->end(), other_matrix.begin(), difference.begin(), minus<T>());
6776 Matrix<T> product(rows_number, columns_number);
6778 for(
size_t i = 0; i < this->size(); i++)
6780 product[i] = (*this)[i]*scalar;
6793 #ifdef __OPENNN_DEBUG__
6798 if(other_rows_number != rows_number || other_columns_number != columns_number)
6800 ostringstream buffer;
6802 buffer <<
"OpenNN Exception: Matrix Template.\n"
6803 <<
"Matrix<T> operator *(const Matrix<T>&) const method.\n"
6804 <<
"Sizes of other matrix (" << other_rows_number <<
"," << other_columns_number <<
") must be equal to sizes of this matrix (" << rows_number <<
"," << columns_number <<
").\n";
6806 throw logic_error(buffer.str());
6811 Matrix<T> product(rows_number, columns_number);
6813 for(
size_t i = 0; i < this->size(); i++)
6815 product[i] = (*this)[i]*other_matrix[i];
6825 #ifdef __OPENNN_DEBUG__
6828 const size_t other_columns_number = tensor.
get_dimension(1);
6830 if(other_rows_number != rows_number || other_columns_number != columns_number)
6832 ostringstream buffer;
6834 buffer <<
"OpenNN Exception: Matrix Template.\n"
6835 <<
"Matrix<T> operator *(const Tensor<T>&) const method.\n"
6836 <<
"Sizes of tensor (" << other_rows_number <<
"," << other_columns_number <<
") must be equal to sizes of this matrix (" << rows_number <<
"," << columns_number <<
").\n";
6838 throw logic_error(buffer.str());
6843 Matrix<T> product(rows_number, columns_number);
6845 for(
size_t i = 0; i < this->size(); i++)
6847 product[i] = (*this)[i]*tensor[i];
6860 Matrix<T> results(rows_number, columns_number);
6862 for(
size_t i = 0; i < results.size(); i++)
6864 results[i] = (*this)[i]/scalar;
6877 #ifdef __OPENNN_DEBUG__
6879 const size_t size = vector.size();
6881 if(size != rows_number)
6883 ostringstream buffer;
6885 buffer <<
"OpenNN Exception: Matrix Template.\n"
6886 <<
"Matrix<T> operator /(const Vector<T>&) const.\n"
6887 <<
"Size of vector must be equal to number of rows.\n";
6889 throw logic_error(buffer.str());
6894 Matrix<T> cocient(rows_number, columns_number);
6896 for(
size_t i = 0; i < rows_number; i++)
6898 for(
size_t j = 0; j < columns_number; j++)
6900 cocient(i,j) = (*this)(i,j)/vector[i];
6914 #ifdef __OPENNN_DEBUG__
6919 if(other_rows_number != rows_number || other_columns_number != columns_number)
6921 ostringstream buffer;
6923 buffer <<
"OpenNN Exception: Matrix Template.\n"
6924 <<
"Matrix<T> operator /(const Matrix<T>&) const method.\n"
6925 <<
"Both matrix sizes must be the same.\n";
6927 throw logic_error(buffer.str());
6932 Matrix<T> cocient(rows_number, columns_number);
6934 for(
size_t i = 0; i < rows_number; i++)
6936 cocient[i] = (*this)[i]/other_matrix[i];
6949 for(
size_t i = 0; i < rows_number; i++)
6951 for(
size_t j = 0; j < columns_number; j++)
6953 (*this)(i,j) += value;
6965 #ifdef __OPENNN_DEBUG__
6969 if(other_rows_number != rows_number)
6971 ostringstream buffer;
6973 buffer <<
"OpenNN Exception: Matrix Template.\n"
6974 <<
"void operator += (const Matrix<T>&).\n"
6975 <<
"Both numbers of rows must be the same.\n";
6977 throw logic_error(buffer.str());
6982 if(other_columns_number != columns_number)
6984 ostringstream buffer;
6986 buffer <<
"OpenNN Exception: Matrix Template.\n"
6987 <<
"void operator += (const Matrix<T>&).\n"
6988 <<
"Both numbers of columns must be the same.\n";
6990 throw logic_error(buffer.str());
6995 for(
size_t i = 0; i < this->size(); i++)
6997 (*this)[i] += other_matrix[i];
7008 for(
size_t i = 0; i < rows_number; i++)
7010 for(
size_t j = 0; j < columns_number; j++)
7012 (*this)(i,j) -= value;
7024 #ifdef __OPENNN_DEBUG__
7028 if(other_rows_number != rows_number)
7030 ostringstream buffer;
7032 buffer <<
"OpenNN Exception: Matrix Template.\n"
7033 <<
"void operator -= (const Matrix<T>&).\n"
7034 <<
"Both numbers of rows must be the same.\n";
7036 throw logic_error(buffer.str());
7041 if(other_columns_number != columns_number)
7043 ostringstream buffer;
7045 buffer <<
"OpenNN Exception: Matrix Template.\n"
7046 <<
"void operator -= (const Matrix<T>&).\n"
7047 <<
"Both numbers of columns must be the same.\n";
7049 throw logic_error(buffer.str());
7054 for(
size_t i = 0; i < this->size(); i++)
7056 (*this)[i] -= other_matrix[i];
7067 for(
size_t i = 0; i < this->size(); i++)
7069 (*this)[i] *= value;
7080 #ifdef __OPENNN_DEBUG__
7084 if(other_rows_number != rows_number)
7086 ostringstream buffer;
7088 buffer <<
"OpenNN Exception: Matrix Template.\n"
7089 <<
"void operator *= (const Matrix<T>&).\n"
7090 <<
"The number of rows in the other matrix (" << other_rows_number <<
")"
7091 <<
" is not equal to the number of rows in this matrix (" << rows_number <<
").\n";
7093 throw logic_error(buffer.str());
7098 for(
size_t i = 0; i < this->size(); i++)
7100 (*this)[i] *= other_matrix[i];
7111 for(
size_t i = 0; i < this->size(); i++)
7113 (*this)[i] /= value;
7126 #ifdef __OPENNN_DEBUG__
7130 if(other_rows_number != rows_number)
7132 ostringstream buffer;
7134 buffer <<
"OpenNN Exception: Matrix Template.\n"
7135 <<
"void operator /= (const Matrix<T>&).\n"
7136 <<
"Both numbers of rows must be the same.\n";
7138 throw logic_error(buffer.str());
7143 if(other_columns_number != columns_number)
7145 ostringstream buffer;
7147 buffer <<
"OpenNN Exception: Matrix Template.\n"
7148 <<
"void operator /= (const Matrix<T>&).\n"
7149 <<
"Both numbers of columns must be the same.\n";
7151 throw logic_error(buffer.str());
7156 for(
size_t i = 0; i < this->size(); i++)
7158 (*this)[i] /= other_matrix[i];
7168 if(rows_number == 0 && columns_number == 0)
7185 if(rows_number == columns_number)
7202 #ifdef __OPENNN_DEBUG__
7204 if(rows_number != columns_number)
7206 ostringstream buffer;
7208 buffer <<
"OpenNN Exception: Matrix Template.\n"
7209 <<
"bool is_symmetric() const method.\n"
7210 <<
"Matrix must be squared.\n";
7212 throw logic_error(buffer.str());
7217 const Matrix<T> transpose = calculate_transpose();
7219 if((*
this) == transpose)
7236 #ifdef __OPENNN_DEBUG__
7238 if(rows_number != columns_number)
7240 ostringstream buffer;
7242 buffer <<
"OpenNN Exception: Matrix Template.\n"
7243 <<
"bool is_antisymmetric() const method.\n"
7244 <<
"Matrix must be squared.\n";
7246 throw logic_error(buffer.str());
7251 const Matrix<T> transpose = calculate_transpose();
7253 if((*
this) == transpose*(-1))
7270 #ifdef __OPENNN_DEBUG__
7272 if(rows_number != columns_number)
7274 ostringstream buffer;
7276 buffer <<
"OpenNN Exception: Matrix Template.\n"
7277 <<
"bool is_diagonal() const method.\n"
7278 <<
"Matrix must be squared.\n";
7280 throw logic_error(buffer.str());
7285 for(
size_t i = 0; i < rows_number; i++)
7287 for(
size_t j = 0; j < columns_number; j++)
7289 if(i != j &&(*
this)(i,j) != 0)
7306 #ifdef __OPENNN_DEBUG__
7308 if(rows_number != columns_number)
7310 ostringstream buffer;
7312 buffer <<
"OpenNN Exception: Matrix Template.\n"
7313 <<
"bool is_scalar() const method.\n"
7314 <<
"Matrix must be squared.\n";
7316 throw logic_error(buffer.str());
7321 return get_diagonal().is_constant();
7331 #ifdef __OPENNN_DEBUG__
7333 if(rows_number != columns_number)
7335 ostringstream buffer;
7337 buffer <<
"OpenNN Exception: Matrix Template.\n"
7338 <<
"bool is_unity() const method.\n"
7339 <<
"Matrix must be squared.\n";
7341 throw logic_error(buffer.str());
7346 for(
size_t i = 0; i < rows_number; i++)
7348 for(
size_t j = 0; j < columns_number; j++)
7350 if(i != j &&(*
this)(i,j) != 0)
7354 else if(i == j &&(*
this)(i,j) != 1)
7370 for(
size_t i = 0; i < this->size(); i++)
7372 if((*
this)[i] != 0 && (*this)[i] != 1)
7387 #ifdef __OPENNN_DEBUG__
7389 if(j >= columns_number)
7391 ostringstream buffer;
7393 buffer <<
"OpenNN Exception: Matrix Template.\n"
7394 <<
"bool is_column_binary(const size_t) const method method.\n"
7395 <<
"Index of column(" << j <<
") must be less than number of columns("<<columns_number<<
").\n";
7397 throw logic_error(buffer.str());
7402 for(
size_t i = 0; i < rows_number; i++)
7404 if((*
this)(i,j) != 0 &&(*
this)(i,j) != 1 &&(*
this)(i,j) != static_cast<double>(NAN))
7422 for(
size_t j = 0; j < columns_number; j++)
7424 if((*
this)(row_index, j) != other_matrix(other_row_index, j))
7442 if(columns_number == 0)
7447 const T initial_value = (*this)(0, column_index);
7449 for(
size_t i = 1; i < rows_number; i++)
7451 if((*
this)(i,column_index) != initial_value)
7467 for(
size_t i = 0; i < this->size(); i++)
7487 const Vector<T> column = get_column(column_index);
7489 const size_t new_rows_number = column.
count_between(minimum, maximum);
7491 Matrix<T> new_matrix(new_rows_number, columns_number);
7493 size_t row_index = 0;
7497 for(
size_t i = 0; i < rows_number; i++)
7499 if((*
this)(i,column_index) >= minimum && (*
this)(i,column_index) <= maximum)
7503 new_matrix.
set_row(row_index, row);
7516 const size_t column_index = get_column_index(column_name);
7518 return filter_column_minimum_maximum(column_index, minimum, maximum);
7523 Matrix<T> Matrix<T>::filter_extreme_values(
const size_t& column_index,
const double& lower_ratio,
const double& upper_ratio)
const
7525 const size_t lower_index = rows_number*lower_ratio;
7526 const size_t upper_index = rows_number*upper_ratio;
7528 const Vector<T> column = get_column(column_index).sort_ascending_values();
7530 const T lower_value = column[lower_index];
7531 const T upper_value = column[upper_index];
7534 return Matrix<double>();
7539 Matrix<T> Matrix<T>::filter_extreme_values(
const string& column_name,
const double& lower_ratio,
const double& upper_ratio)
const
7541 const size_t column_index = get_column_index(column_name);
7543 return filter_extreme_values(column_index, lower_ratio, upper_ratio);
7554 const size_t count = count_equal_to(column_index, value);
7558 const size_t new_rows_number = count;
7560 Matrix<T> new_matrix(new_rows_number, columns_number);
7562 size_t row_index = 0;
7566 for(
size_t i = 0; i < rows_number; i++)
7568 if((*
this)(i,column_index) == value)
7572 new_matrix.
set_row(row_index, row);
7588 const size_t& column_2,
const T& value_2,
7589 const size_t& column_3,
const T& value_3,
7590 const size_t& column_4,
const T& value_4)
const
7592 const size_t count = count_equal_to(column_1, value_1, column_2, value_2, column_3, value_3, column_4, value_4);
7594 const size_t new_rows_number = count;
7596 Matrix<T> new_matrix(new_rows_number, columns_number);
7599 size_t row_index = 0;
7601 for(
size_t i = 0; i < rows_number; i++)
7603 if((*
this)(i,column_1) == value_1
7604 &&(*
this)(i,column_2) == value_2
7605 &&(*
this)(i,column_3) == value_3
7606 &&(*
this)(i,column_4) == value_4)
7610 new_matrix.
set_row(row_index, row);
7624 const string& column_2_name,
const T& value_2,
7625 const string& column_3_name,
const T& value_3,
7626 const string& column_4_name,
const T& value_4)
const
7628 const size_t column_1_index = get_column_index(column_1_name);
7629 const size_t column_2_index = get_column_index(column_2_name);
7630 const size_t column_3_index = get_column_index(column_3_name);
7631 const size_t column_4_index = get_column_index(column_4_name);
7633 return(filter_column_equal_to(column_1_index, value_1, column_2_index, value_2, column_3_index, value_3, column_4_index, value_4));
7644 const size_t values_size = values.size();
7646 const size_t count = count_equal_to(column_index, values);
7648 const size_t new_rows_number = count;
7650 Matrix<T> new_matrix(new_rows_number, columns_number);
7654 size_t row_index = 0;
7656 for(
size_t i = 0; i < rows_number; i++)
7658 for(
size_t j = 0; j < values_size; j++)
7660 if((*
this)(i,column_index) == values[j])
7664 new_matrix.
set_row(row_index, row);
7686 const size_t values_size = values.size();
7688 const size_t new_rows_number = count_not_equal_to(column_index, values);
7690 Matrix<T> new_matrix(new_rows_number, columns_number);
7692 size_t row_index = 0;
7696 for(
size_t i = 0; i < rows_number; i++)
7700 for(
size_t j = 0; j < values_size; j++)
7702 if((*
this)(i,column_index) != values[j])
7708 if(count == values.size())
7712 new_matrix.
set_row(row_index, row);
7728 const size_t& index_2,
const T& value_2)
const
7730 const size_t count = count_equal_to(index_1, values_1, index_2, value_2);
7732 const size_t new_rows_number = count;
7734 Matrix<T> new_matrix(new_rows_number, columns_number);
7736 size_t row_index = 0;
7740 const size_t values_1_size = values_1.size();
7742 for(
size_t i = 0; i < rows_number; i++)
7744 if((*
this)(i, index_2) == value_2)
7746 for(
size_t j = 0; j < values_1_size; j++)
7748 if((*
this)(i, index_1) == values_1[j])
7752 new_matrix.
set_row(row_index, row);
7770 const size_t& column_2_index,
const T& value_2,
7771 const size_t& column_3_index,
const T& value_3,
7772 const size_t& column_4_index,
const T& value_4)
const
7774 const size_t new_rows_number = count_equal_to(column_1_index, values_1,
7775 column_2_index, value_2,
7776 column_3_index, value_3,
7777 column_4_index, value_4);
7779 Matrix<T> new_matrix(new_rows_number, columns_number);
7781 const size_t values_1_size = values_1.size();
7783 size_t row_index = 0;
7789 for(
size_t i = 0; i < rows_number; i++)
7791 if((*
this)(i,column_2_index) == value_2
7792 &&(*
this)(i,column_3_index) == value_3
7793 &&(*
this)(i,column_4_index) == value_4)
7795 matrix_element = (*this)(i,column_1_index);
7797 for(
size_t j = 0; j < values_1_size; j++)
7799 if(values_1[j] == matrix_element)
7803 new_matrix.
set_row(row_index, row);
7821 const string& column_2,
const T& value_2,
7822 const string& column_3,
const T& value_3,
7823 const string& column_4,
const T& value_4)
const
7825 const size_t column_1_index = get_column_index(column_1);
7826 const size_t column_2_index = get_column_index(column_2);
7827 const size_t column_3_index = get_column_index(column_3);
7828 const size_t column_4_index = get_column_index(column_4);
7830 return(filter_column_equal_to(column_1_index, values_1, column_2_index, value_2, column_3_index, value_3, column_4_index, value_4));
7839 const size_t column_index = get_column_index(column_name);
7841 return(filter_column_equal_to(column_index, value));
7850 const size_t column_index = get_column_index(column_name);
7852 return(filter_column_equal_to(column_index, values));
7861 const size_t column_index = get_column_index(column_name);
7863 return(filter_column_not_equal_to(column_index, values));
7871 const string& name_2,
const T& value_2)
const
7873 const size_t index_1 = get_column_index(name_1);
7874 const size_t index_2 = get_column_index(name_2);
7876 return(filter_column_equal_to(index_1, values_1, index_2, value_2));
7885 const size_t new_rows_number = count_not_equal_to(column_index, value);
7887 Matrix<T> new_matrix(new_rows_number, columns_number);
7889 size_t row_index = 0;
7893 for(
size_t i = 0; i < rows_number; i++)
7895 if((*
this)(i,column_index) != value)
7899 new_matrix.
set_row(row_index, row);
7916 const size_t column_index = get_column_index(column_name);
7918 return(filter_column_not_equal_to(column_index, value));
7927 const Vector<T> column = get_column(column_index);
7931 if(new_rows_number == 0)
7938 Matrix<T> new_matrix(new_rows_number, columns_number);
7940 size_t row_index = 0;
7944 for(
size_t i = 0; i < rows_number; i++)
7946 if((*
this)(i,column_index) < value)
7950 new_matrix.
set_row(row_index, row);
7967 const size_t column_index = get_column_index(column_name);
7969 return(filter_column_less_than(column_index, value));
7978 const Vector<T> column = get_column(column_index);
7982 if(new_rows_number == 0)
7989 Matrix<T> new_matrix(new_rows_number, columns_number);
7991 size_t row_index = 0;
7995 for(
size_t i = 0; i < rows_number; i++)
7997 if((*
this)(i,column_index) > value)
8001 new_matrix.
set_row(row_index, row);
8018 const size_t column_index = get_column_index(column_name);
8020 return(filter_column_greater_than(column_index, value));
8031 return delete_rows(indices);
8042 return delete_rows(indices);
8051 cout << *
this << endl;
8061 void Matrix<T>::load_csv(
const string& file_name,
const char& delim,
const bool& has_columns_names,
const string& missing_label)
8063 ifstream file(file_name.c_str());
8067 ostringstream buffer;
8069 buffer <<
"OpenNN Exception: Matrix template.\n"
8070 <<
"void load_csv(const string&, const char&) method.\n"
8071 <<
"Cannot open matrix data file: " << file_name <<
"\n";
8073 throw logic_error(buffer.str());
8076 if(file.peek() == ifstream::traits_type::eof())
8087 getline(file, line);
8095 istringstream buffer(line);
8099 vector<string> results;
8101 while(getline(buffer, token, delim))
8103 results.push_back(token);
8106 size_t new_columns_number = static_cast<size_t>(results.size());
8108 size_t new_rows_number = 1;
8112 getline(file, line);
8124 if(has_columns_names)
8127 header.set(results);
8130 set(new_rows_number, new_columns_number);
8132 if(!has_columns_names)
8140 file.seekg(0, ios::beg);
8142 if(has_columns_names)
8144 getline(file, line);
8146 istringstream header_buffer(line);
8148 for(
size_t j = 0; j < columns_number; j++)
8152 getline(header_buffer, token, delim);
8158 for(
size_t i = 0; i < rows_number; i++)
8160 getline(file, line);
8162 istringstream buffer(line);
8164 for(
size_t j = 0; j < columns_number; j++)
8168 getline(buffer, token, delim);
8170 if(is_same<T, double>::value)
8172 if(token == missing_label) (*this)(i,j) = NAN;
8173 else (*
this)(i,j) = stod(token);
8175 else if(is_same<T, float>::value)
8177 if(token == missing_label) (*this)(i,j) = NAN;
8178 else (*
this)(i,j) = stof(token);
8180 else if(is_same<T, int>::value)
8182 if(token == missing_label) (*this)(i,j) = NAN;
8183 else (*
this)(i,j) = stoi(token);
8185 else if(is_same<T, long int>::value)
8187 if(token == missing_label) (*this)(i,j) = NAN;
8188 else (*
this)(i,j) = stol(token);
8190 else if(is_same<T, unsigned long>::value)
8192 if(token == missing_label) (*this)(i,j) = NAN;
8193 else (*
this)(i,j) = stoul(token);
8195 else if(is_same<T, size_t>::value)
8197 if(token == missing_label) (*this)(i,j) = NAN;
8198 else (*
this)(i,j) = stoul(token);
8202 ostringstream buffer;
8204 buffer <<
"OpenNN Exception: Matrix template.\n"
8205 <<
"void load_csv(const string&, const char&) method.\n"
8206 <<
"Template could not be recognized \n";
8208 throw logic_error(buffer.str());
8226 ifstream file(file_name.c_str());
8230 ostringstream buffer;
8232 buffer <<
"OpenNN Exception: Matrix template.\n"
8233 <<
"void load_csv(const string&,const char&) method.\n"
8234 <<
"Cannot open matrix data file: " << file_name <<
"\n";
8236 throw logic_error(buffer.str());
8239 if(file.peek() == ifstream::traits_type::eof())
8250 getline(file, line);
8258 istringstream buffer(line);
8262 vector<string> results;
8264 while(getline(buffer, token, delim))
8266 results.push_back(token);
8269 const size_t new_columns_number = static_cast<size_t>(results.size());
8271 size_t new_rows_number = 1;
8275 getline(file, line);
8283 if(has_columns_names)
8286 header.set(results);
8289 set(new_rows_number, new_columns_number);
8291 if(!has_columns_names)
8299 file.seekg(0, ios::beg);
8301 if(has_columns_names)
8303 getline(file, line);
8305 istringstream header_buffer(line);
8307 for(
size_t j = 0; j < columns_number; j++)
8311 getline(header_buffer, token, delim);
8317 for(
size_t i = 0; i < rows_number; i++)
8319 getline(file, line);
8321 istringstream buffer(line);
8323 for(
size_t j = 0; j < columns_number; j++)
8327 getline(buffer, token, delim);
8329 (*this)(i,j) = token;
8347 file.open(file_name.c_str(), ios::binary);
8351 ostringstream buffer;
8353 buffer <<
"OpenNN Exception: Matrix template.\n"
8354 <<
"void load_binary(const string&) method.\n"
8355 <<
"Cannot open binary file: " << file_name <<
"\n";
8357 throw logic_error(buffer.str());
8360 streamsize size =
sizeof(size_t);
8362 size_t columns_number;
8365 file.read(reinterpret_cast<char*>(&columns_number), size);
8366 file.read(reinterpret_cast<char*>(&rows_number), size);
8372 this->set(rows_number, columns_number);
8374 for(
size_t i = 0; i < this->size(); i++)
8376 file.read(reinterpret_cast<char*>(&value), size);
8391 ofstream file(file_name.c_str(), ios::binary);
8395 ostringstream buffer;
8397 buffer <<
"OpenNN Exception: Matrix template." << endl
8398 <<
"void save(const string) method." << endl
8399 <<
"Cannot open matrix binary file." << endl;
8401 throw logic_error(buffer.str());
8406 streamsize size =
sizeof(size_t);
8408 size_t m = columns_number;
8409 size_t n = rows_number;
8411 file.write(reinterpret_cast<char*>(&m), size);
8412 file.write(reinterpret_cast<char*>(&n), size);
8414 size =
sizeof(double);
8418 for(
int i = 0; i < this->size(); i++)
8422 file.write(reinterpret_cast<char*>(&value), size);
8436 ofstream file(file_name.c_str());
8440 ostringstream buffer;
8442 buffer <<
"OpenNN Exception: Matrix template." << endl
8443 <<
"void save_csv(const string&, const char&, const Vector<string>&, const Vector<string>&) method." << endl
8444 <<
"Cannot open matrix data file: " << file_name << endl;
8446 throw logic_error(buffer.str());
8449 if(row_names.size() != 0 && row_names.size() != rows_number)
8451 ostringstream buffer;
8453 buffer <<
"OpenNN Exception: Matrix template." << endl
8454 <<
"void save_csv(const string&, const char&, const Vector<string>&, const Vector<string>&) method." << endl
8455 <<
"Row names must have size 0 or " << rows_number <<
"." << endl;
8457 throw logic_error(buffer.str());
8462 if(!header.empty() && header !=
"")
8464 if(!row_names.empty())
8466 file << nameID << separator;
8469 for(
size_t j = 0; j < columns_number; j++)
8473 if(j != columns_number-1)
8484 for(
size_t i = 0; i < rows_number; i++)
8486 if(!row_names.empty())
8488 file << row_names[i] << separator;
8491 for(
size_t j = 0; j < columns_number; j++)
8493 file <<(*this)(i,j);
8495 if(j != columns_number-1)
8515 ofstream file(file_name.c_str());
8519 ostringstream buffer;
8521 buffer <<
"OpenNN Exception: Matrix template." << endl
8522 <<
"void save_json(const string&, const Vector<string>&) method." << endl
8523 <<
"Cannot open matrix data file." << endl;
8525 throw logic_error(buffer.str());
8528 if(column_names.size() != 0 && column_names.size() != columns_number)
8530 ostringstream buffer;
8532 buffer <<
"OpenNN Exception: Matrix template." << endl
8533 <<
"void save_json(const string&, const Vector<string>&) method." << endl
8534 <<
"Column names must have size 0 or " << columns_number <<
"." << endl;
8536 throw logic_error(buffer.str());
8543 if(column_names.empty())
8545 header.
set(columns_number);
8547 for(
size_t i = 0; i < columns_number; i++)
8549 header[i] =
"variable_" + to_string(i);
8554 header = column_names;
8559 file <<
"{ \"rows_number\": " << rows_number
8560 <<
", \"columns_number\": " << columns_number <<
", ";
8562 for(
size_t i = 0; i < columns_number; i++)
8564 file <<
"\"" << header[i] <<
"\": [";
8566 for(
size_t j = 0; j < rows_number; j++)
8568 file <<(*this)(j,i);
8570 if(j != rows_number-1)
8578 if(i != columns_number-1)
8607 istringstream str_buffer(str);
8611 getline(str_buffer, line);
8613 istringstream line_buffer(line);
8615 istream_iterator<string> it(line_buffer);
8616 istream_iterator<string> end;
8618 const vector<string> results(it, end);
8620 const size_t new_columns_number = static_cast<size_t>(results.size());
8622 size_t new_rows_number = 1;
8624 while(str_buffer.good())
8626 getline(str_buffer, line);
8634 set(new_rows_number, new_columns_number);
8639 str_buffer.seekg(0, ios::beg);
8641 for(
size_t i = 0; i < rows_number; i++)
8643 for(
size_t j = 0; j < columns_number; j++)
8645 str_buffer >>(*this)(i,j);
8659 ostringstream buffer;
8661 if(rows_number > 0 && columns_number > 0)
8663 buffer << get_header().vector_to_string(separator);
8665 for(
size_t i = 0; i < rows_number; i++)
8668 << get_row(i).vector_to_string(separator);
8672 return buffer.str();
8683 const size_t this_size = this->size();
8685 for(
size_t i = 0; i < this_size; i++)
8687 size_t_matrix[i] = static_cast<size_t>((*
this)[i]);
8690 return(size_t_matrix);
8701 const size_t this_size = this->size();
8703 for(
size_t i = 0; i < this_size; i++)
8705 new_matrix[i] = static_cast<float>((*
this)[i]);
8719 const size_t this_size = this->size();
8721 for(
size_t i = 0; i < this_size; i++)
8723 new_matrix[i] = static_cast<double>((*
this)[i]);
8737 const size_t this_size = this->size();
8739 for(
size_t i = 0; i < this_size; i++)
8743 new_matrix[i] = (*this)[i] ? 1.0 : 0.0;
8745 catch(
const logic_error&)
8747 new_matrix[i] = nan(
"NA");
8764 const size_t this_size = this->size();
8766 for(
size_t i = 0; i < this_size; i++)
8770 new_matrix[i] = stod((*
this)[i]);
8772 catch(
const logic_error&)
8774 new_matrix[i] = nan(
"NA");
8791 const size_t this_size = this->size();
8793 for(
size_t i = 0; i < this_size; i++)
8797 new_matrix[i] = static_cast<size_t>(stoi((*
this)[i]));
8799 catch(
const logic_error&)
8801 new_matrix[i] = numeric_limits<size_t>::max();
8816 ostringstream buffer;
8818 for(
size_t i = 0; i < rows_number; i++)
8820 for(
size_t j = 0; j < columns_number; j++)
8823 buffer << setprecision(precision) <<(*this)(i,j);
8825 string_matrix(i,j) = buffer.str();
8829 if(!header.empty()) string_matrix.
set_header(header);
8831 return string_matrix;
8840 return Matrix<T>(rows_number, columns_number, 0);
8849 return Matrix<T>(rows_number, columns_number, 1);
8860 const vector<T> std_vector(this->begin(),this->end());
8873 const Vector<T> vector(this->begin(),this->end());
8888 for(
size_t i = 0; i < columns_number; i++)
8890 vector_of_vectors[i] = this->get_column(i);
8893 return vector_of_vectors;
8903 cout <<
"Rows number: " << rows_number << endl
8904 <<
"Columns number: " << columns_number << endl;
8906 cout <<
"Header:\n" << header << endl;
8912 cout <<
"Row 0:\n" << first_row << endl;
8917 const Vector<T> second_row = get_row(1);
8919 cout <<
"Row 1:\n" << second_row << endl;
8924 const Vector<T> row = get_row(rows_number-2);
8926 cout <<
"Row " << rows_number-2 <<
":\n" << row << endl;
8931 const Vector<T> last_row = get_row(rows_number-1);
8933 cout <<
"Row " << rows_number-1 <<
":\n" << last_row << endl;
8943 istream& operator >>(istream& is,
Matrix<T>& m)
8948 for(
size_t i = 0; i < rows_number; i++)
8950 for(
size_t j = 0; j < columns_number; j++)
8965 ostream& operator <<(ostream& os,
const Matrix<T>& m)
8967 const size_t rows_number = m.get_rows_number();
8968 const size_t columns_number = m.get_columns_number();
8970 if(m.get_header() !=
"") cout << m.get_header() << endl;
8972 if(rows_number > 0 && columns_number > 0)
8976 for(
size_t i = 1; i < rows_number; i++)
8992 ostream& operator << (ostream& os,
const Matrix<Vector<T>>& m)
8994 const size_t rows_number = m.get_rows_number();
8995 const size_t columns_number = m.get_columns_number();
8997 for(
size_t i = 0; i < rows_number; i++)
8999 for(
size_t j = 0; j < columns_number; j++)
9001 os <<
"subvector_" << i <<
"_" << j <<
"\n"
9015 ostream& operator << (ostream& os,
const Matrix< Matrix<T> >& m)
9017 const size_t rows_number = m.get_rows_number();
9018 const size_t columns_number = m.get_columns_number();
9020 for(
size_t i = 0; i < rows_number; i++)
9022 for(
size_t j = 0; j < columns_number; j++)
9024 os <<
"submatrix_" << i <<
"_" << j <<
"\n"
9034 Matrix<T> Matrix<T>::insert_padding(
const size_t& width,
const size_t& height)
const
9036 Matrix<T> input(*
this);
9038 if(!width && !height)
return input;
9040 Matrix<T> zero_padding_matrix(input);
9042 for(
size_t i = 0; i < width; i++)
9044 const size_t rows_number = zero_padding_matrix.get_rows_number();
9046 const Vector<double> zero_vector_rows(rows_number, 0.0);
9048 zero_padding_matrix = zero_padding_matrix.append_column(zero_vector_rows);
9050 zero_padding_matrix = zero_padding_matrix.insert_column(0, zero_vector_rows);
9053 for(
size_t i = 0; i < height; i++)
9055 const size_t columns_number = zero_padding_matrix.get_columns_number();
9057 const Vector<double> zero_vector_columns(columns_number, 0.0);
9059 zero_padding_matrix = zero_padding_matrix.append_row(zero_vector_columns);
9061 zero_padding_matrix = zero_padding_matrix.insert_row(0, zero_vector_columns);
9064 return zero_padding_matrix;
9075 const size_t rows_number = get_rows_number();
9077 const Vector<T> column = get_column(column_index);
9081 const size_t new_columns_number = categories.size();
9085 Matrix<T> new_columns(rows_number,new_columns_number,0);
9087 for(
size_t i = 0 ; i < new_columns_number ; i++)
9089 for(
size_t j = 0 ; j < rows_number ; j++)
9091 if(column[j] == categories[i]) new_columns(j,i) = 1;
9095 if (!header.empty())
9102 (
this)->delete_column(column_index);
9104 return (
this)->insert_matrix(column_index,new_columns);
9115 Tensor<T> tensor(rows_number, columns_number);
9117 for(
size_t i = 0; i < this->size(); i++)
9119 tensor[i] = (*this)[i];
9136 for(
size_t i = 0; i < vector_size; i++)