47 explicit Matrix(
const size_t&,
const size_t&);
49 explicit Matrix(
const size_t&,
const size_t&,
const T&);
51 explicit Matrix(
const std::string&);
65 inline T&
operator () (
const size_t&,
const size_t&);
67 inline const T&
operator () (
const size_t&,
const size_t&)
const;
81 bool operator < (const Matrix<T>&)
const;
89 bool operator <= (const Matrix<T>&)
const;
106 void set(
const size_t&,
const size_t&);
108 void set(
const size_t&,
const size_t&,
const T&);
113 void set(
const std::string&);
145 void set_row(
const size_t&,
const T&);
351 bool empty(
void)
const;
376 void print(
void)
const;
378 void load(
const std::string&);
380 void save(
const std::string&)
const;
382 void save_csv(
const std::string&)
const;
384 void parse(
const std::string&);
386 std::string
to_string(
const std::string& =
" ")
const;
425 Matrix<T>::Matrix(
const size_t& new_rows_number,
const size_t& new_columns_number) : std::vector<T>(new_rows_number*new_columns_number)
427 if(new_rows_number == 0 && new_columns_number == 0)
432 else if(new_rows_number == 0)
434 std::ostringstream buffer;
436 buffer <<
"OpenNN Exception: Matrix Template.\n"
437 <<
"Constructor Matrix(const size_t&, const size_t&).\n"
438 <<
"Number of rows must be greater than zero.\n";
440 throw std::logic_error(buffer.str());
442 else if(new_columns_number == 0)
444 std::ostringstream buffer;
446 buffer <<
"OpenNN Exception: Matrix Template.\n"
447 <<
"Constructor Matrix(const size_t&, const size_t&).\n"
448 <<
"Number of columns must be greater than zero.\n";
450 throw std::logic_error(buffer.str());
466 Matrix<T>::Matrix(
const size_t& new_rows_number,
const size_t& new_columns_number,
const T& value) : std::vector<T>(new_rows_number*new_columns_number)
468 if(new_rows_number == 0 && new_columns_number == 0)
473 else if(new_rows_number == 0)
475 std::ostringstream buffer;
477 buffer <<
"OpenNN Exception: Matrix Template.\n"
478 <<
"Constructor Matrix(const size_t&, const size_t&, const T&).\n"
479 <<
"Number of rows must be greater than zero.\n";
481 throw std::logic_error(buffer.str());
483 else if(new_columns_number == 0)
485 std::ostringstream buffer;
487 buffer <<
"OpenNN Exception: Matrix Template.\n"
488 <<
"Constructor Matrix(const size_t&, const size_t&, const T&).\n"
489 <<
"Number of columns must be greater than zero.\n";
491 throw std::logic_error(buffer.str());
500 (*this).initialize(value);
554 this->resize(rows_number*columns_number);
557 std::copy(other_matrix.begin(), other_matrix.end(), (*this).begin());
578 if(row >= rows_number)
580 std::ostringstream buffer;
582 buffer <<
"OpenNN Exception: Matrix Template.\n"
583 <<
"operator () (const size_t&, const size_t&).\n"
584 <<
"Row index (" << row <<
") must be less than number of rows (" << rows_number <<
").\n";
586 throw std::logic_error(buffer.str());
588 else if(column >= columns_number)
590 std::ostringstream buffer;
592 buffer <<
"OpenNN Exception: Matrix Template.\n"
593 <<
"operator () (const size_t&, const size_t&).\n"
594 <<
"Column index (" << column <<
") must be less than number of columns (" << columns_number <<
").\n";
596 throw std::logic_error(buffer.str());
603 return((*
this)[rows_number*column+row]);
620 if(row >= rows_number)
622 std::ostringstream buffer;
624 buffer <<
"OpenNN Exception: Matrix Template.\n"
625 <<
"operator () (const size_t&, const size_t&).\n"
626 <<
"Row index (" << row <<
") must be less than number of rows (" << rows_number <<
").\n";
628 throw std::logic_error(buffer.str());
630 else if(column >= columns_number)
632 std::ostringstream buffer;
634 buffer <<
"OpenNN Exception: Matrix Template.\n"
635 <<
"operator () (const size_t&, const size_t&).\n"
636 <<
"Column index (" << column <<
") must be less than number of columns (" << columns_number <<
").\n";
638 throw std::logic_error(buffer.str());
645 return((*
this)[rows_number*column+row]);
662 if(other_rows_number != rows_number)
666 else if(other_columns_number != columns_number)
672 for(
size_t i = 0; i < this->size(); i++)
674 if((*
this)[i] != other_matrix[i])
694 for(
size_t i = 0; i < this->size(); i++)
696 if((*
this)[i] != value)
722 if(other_rows_number != rows_number)
724 std::ostringstream buffer;
726 buffer <<
"OpenNN Exception: Matrix Template.\n"
727 <<
"bool operator != (const Matrix<T>&) const.\n"
728 <<
"Both numbers of rows must be the same.\n";
730 throw std::logic_error(buffer.str());
732 else if(other_columns_number != columns_number)
734 std::ostringstream buffer;
736 buffer <<
"OpenNN Exception: Matrix Template.\n"
737 <<
"bool operator != (const Matrix<T>&) const.\n"
738 <<
"Both numbers of columns must be the same.\n";
740 throw std::logic_error(buffer.str());
745 for(
size_t i = 0; i < this->size(); i++)
747 if((*
this)[i] != other_matrix[i])
768 for(
size_t i = 0; i < this->size(); i++)
770 if((*
this)[i] != value)
797 if(other_rows_number != rows_number)
799 std::ostringstream buffer;
801 buffer <<
"OpenNN Exception: Matrix Template.\n"
802 <<
"bool operator > (const Matrix<T>&) const.\n"
803 <<
"Both numbers of rows must be the same.\n";
805 throw std::logic_error(buffer.str());
807 else if(other_columns_number != columns_number)
809 std::ostringstream buffer;
811 buffer <<
"OpenNN Exception: Matrix Template.\n"
812 <<
"bool operator > (const Matrix<T>&) const.\n"
813 <<
"Both numbers of columns must be the same.\n";
815 throw std::logic_error(buffer.str());
820 for(
size_t i = 0; i < this->size(); i++)
822 if((*
this)[i] <= other_matrix[i])
841 for(
size_t i = 0; i < this->size(); i++)
843 if((*
this)[i] <= value)
870 if(other_rows_number != rows_number)
872 std::ostringstream buffer;
874 buffer <<
"OpenNN Exception: Matrix Template.\n"
875 <<
"bool operator < (const Matrix<T>&) const.\n"
876 <<
"Both numbers of rows must be the same.\n";
878 throw std::logic_error(buffer.str());
880 else if(other_columns_number != columns_number)
882 std::ostringstream buffer;
884 buffer <<
"OpenNN Exception: Matrix Template.\n"
885 <<
"bool operator < (const Matrix<T>&) const.\n"
886 <<
"Both numbers of columns must be the same.\n";
888 throw std::logic_error(buffer.str());
893 for(
size_t i = 0; i < this->size(); i++)
895 if((*
this)[i] >= other_matrix[i])
914 for(
size_t i = 0; i < this->size(); i++)
916 if((*
this)[i] >= value)
943 if(other_rows_number != rows_number)
945 std::ostringstream buffer;
947 buffer <<
"OpenNN Exception: Matrix Template.\n"
948 <<
"bool operator >= (const Matrix<T>&) const.\n"
949 <<
"Both numbers of rows must be the same.\n";
951 throw std::logic_error(buffer.str());
953 else if(other_columns_number != columns_number)
955 std::ostringstream buffer;
957 buffer <<
"OpenNN Exception: Matrix Template.\n"
958 <<
"bool operator >= (const Matrix<T>&) const.\n"
959 <<
"Both numbers of columns must be the same.\n";
961 throw std::logic_error(buffer.str());
966 for(
size_t i = 0; i < this->size(); i++)
968 if((*
this)[i] < other_matrix[i])
987 for(
size_t i = 0; i < this->size(); i++)
989 if((*
this)[i] < value)
1016 if(other_rows_number != rows_number)
1018 std::ostringstream buffer;
1020 buffer <<
"OpenNN Exception: Matrix Template.\n"
1021 <<
"bool operator >= (const Matrix<T>&) const.\n"
1022 <<
"Both numbers of rows must be the same.\n";
1024 throw std::logic_error(buffer.str());
1026 else if(other_columns_number != columns_number)
1028 std::ostringstream buffer;
1030 buffer <<
"OpenNN Exception: Matrix Template.\n"
1031 <<
"bool operator >= (const Matrix<T>&) const.\n"
1032 <<
"Both numbers of columns must be the same.\n";
1034 throw std::logic_error(buffer.str());
1039 for(
size_t i = 0; i < this->size(); i++)
1041 if((*
this)[i] > other_matrix[i])
1060 for(
size_t i = 0; i < this->size(); i++)
1062 if((*
this)[i] > value)
1081 return(rows_number);
1092 return(columns_number);
1120 if(new_rows_number == rows_number && new_columns_number == columns_number)
1124 else if(new_rows_number == 0 && new_columns_number == 0)
1128 else if(new_rows_number == 0)
1130 std::ostringstream buffer;
1132 buffer <<
"OpenNN Exception: Matrix Template.\n"
1133 <<
"void set(const size_t&, const size_t&) method.\n"
1134 <<
"Number of rows must be greater than zero.\n";
1136 throw std::logic_error(buffer.str());
1138 else if(new_columns_number == 0)
1140 std::ostringstream buffer;
1142 buffer <<
"OpenNN Exception: Matrix Template.\n"
1143 <<
"void set(const size_t&, const size_t&) method.\n"
1144 <<
"Number of columns must be greater than zero.\n";
1146 throw std::logic_error(buffer.str());
1150 rows_number = new_rows_number;
1151 columns_number = new_columns_number;
1153 this->resize(rows_number*columns_number);
1167 void Matrix<T>::set(
const size_t& new_rows_number,
const size_t& new_columns_number,
const T& value)
1169 if(new_rows_number == 0 && new_columns_number == 0)
1173 else if(new_rows_number == 0)
1175 std::ostringstream buffer;
1177 buffer <<
"OpenNN Exception: Matrix Template.\n"
1178 <<
"void set(const size_t&, const size_t&, const T&) method.\n"
1179 <<
"Number of rows must be greater than zero.\n";
1181 throw std::logic_error(buffer.str());
1183 else if(new_columns_number == 0)
1185 std::ostringstream buffer;
1187 buffer <<
"OpenNN Exception: Matrix Template.\n"
1188 <<
"void set(const size_t&, const size_t&, const T&) method.\n"
1189 <<
"Number of columns must be greater than zero.\n";
1191 throw std::logic_error(buffer.str());
1195 set(new_rows_number, new_columns_number);
1212 this->resize(rows_number*columns_number);
1214 for(
size_t i = 0; i < (size_t)this->size(); i++)
1216 (*this)[i] = other_matrix[i];
1241 set(new_size, new_size);
1242 initialize_identity();
1254 if(new_rows_number != rows_number)
1256 set(new_rows_number, columns_number);
1269 if(new_columns_number != columns_number)
1271 set(rows_number, new_columns_number);
1293 if(row_position + other_rows_number > rows_number)
1295 std::ostringstream buffer;
1297 buffer <<
"OpenNN Exception: Matrix Template.\n"
1298 <<
"void tuck_in(const size_t&, const size_t&, const Matrix<T>&) const method.\n"
1299 <<
"Cannot tuck in matrix.\n";
1301 throw std::logic_error(buffer.str());
1304 if(column_position + other_columns_number > columns_number)
1306 std::ostringstream buffer;
1308 buffer <<
"OpenNN Exception: Matrix Template.\n"
1309 <<
"void tuck_in(const size_t&, const size_t&, const Matrix<T>&) const method.\n"
1310 <<
"Cannot tuck in matrix.\n";
1312 throw std::logic_error(buffer.str());
1317 for(
size_t i = 0; i < other_rows_number; i++)
1319 for(
size_t j = 0; j < other_columns_number; j++)
1321 (*this)(row_position+i,column_position+j) = other_matrix(i,j);
1339 std::ostringstream buffer;
1341 buffer <<
"OpenNN Exception: Matrix Template.\n"
1342 <<
"size_t count_diagonal_elements(void) const method.\n"
1343 <<
"The matrix is not square.\n";
1345 throw std::logic_error(buffer.str());
1350 const size_t rows_number = get_rows_number();
1354 for(
size_t i = 0; i < rows_number; i++)
1356 if((*
this)(i,i) != 0)
1378 std::ostringstream buffer;
1380 buffer <<
"OpenNN Exception: Matrix Template.\n"
1381 <<
"size_t count_off_diagonal_elements(void) const method.\n"
1382 <<
"The matrix is not square.\n";
1384 throw std::logic_error(buffer.str());
1389 const size_t rows_number = get_rows_number();
1390 const size_t columns_number = get_columns_number();
1394 for(
size_t i = 0; i < rows_number; i++)
1396 for(
size_t j = 0; j < columns_number; j++)
1398 if(i != j && (*
this)(i,j) != 0)
1419 const size_t row_indices_size = row_indices.size();
1420 const size_t column_indices_size = column_indices.size();
1422 Matrix<T> sub_matrix(row_indices_size, column_indices_size);
1425 size_t column_index;
1427 for(
size_t i = 0; i < row_indices_size; i++)
1429 row_index = row_indices[i];
1431 for(
size_t j = 0; j < column_indices_size; j++)
1433 column_index = column_indices[j];
1435 sub_matrix(i,j) = (*this)(row_index,column_index);
1451 const size_t row_indices_size = row_indices.size();
1453 Matrix<T> sub_matrix(row_indices_size, columns_number);
1457 for(
size_t i = 0; i < row_indices_size; i++)
1459 row_index = row_indices[i];
1461 for(
size_t j = 0; j < columns_number; j++)
1463 sub_matrix(i,j) = (*this)(row_index,j);
1479 const size_t column_indices_size = column_indices.size();
1481 Matrix<T> sub_matrix(rows_number, column_indices_size);
1483 size_t column_index;
1485 for(
size_t i = 0; i < rows_number; i++)
1487 for(
size_t j = 0; j < column_indices_size; j++)
1489 column_index = column_indices[j];
1491 sub_matrix(i,j) = (*this)(i,column_index);
1511 if(i >= rows_number)
1513 std::ostringstream buffer;
1515 buffer <<
"OpenNN Exception: Matrix Template.\n"
1516 <<
"Vector<T> arrange_row(const size_t&) const method.\n"
1517 <<
"Row index (" << i <<
") must be less than number of rows (" << rows_number <<
").\n";
1519 throw std::logic_error(buffer.str());
1526 for(
size_t j = 0; j < columns_number; j++)
1528 row[j] = (*this)(i,j);
1548 if(row_index >= rows_number)
1550 std::ostringstream buffer;
1552 buffer <<
"OpenNN Exception: Matrix Template.\n"
1553 <<
"Vector<T> arrange_row(const size_t&, const Vector<size_t>&) const method.\n"
1554 <<
"Row index (" << row_index <<
") must be less than number of rows (" << rows_number <<
").\n";
1556 throw std::logic_error(buffer.str());
1561 const size_t size = column_indices.size();
1565 for(
size_t i = 0; i < size; i++)
1567 row[i] = (*this)(row_index,column_indices[i]);
1586 if(j >= columns_number)
1588 std::ostringstream buffer;
1590 buffer <<
"OpenNN Exception: Matrix Template.\n"
1591 <<
"Vector<T> arrange_column(const size_t&) const method.\n"
1592 <<
"Column index (" << j <<
") must be less than number of columns (" << columns_number <<
").\n";
1594 throw std::logic_error(buffer.str());
1601 for(
size_t i = 0; i < rows_number; i++)
1603 column[i] = (*this)(i,j);
1623 if(column_index >= columns_number)
1625 std::ostringstream buffer;
1627 buffer <<
"OpenNN Exception: Matrix Template.\n"
1628 <<
"Vector<T> arrange_column(const size_t&) const method.\n"
1629 <<
"Column index (" << column_index <<
") must be less than number of rows (" << columns_number <<
").\n";
1631 throw std::logic_error(buffer.str());
1636 const size_t size = row_indices.size();
1640 for(
size_t i = 0; i < size; i++)
1642 column[i] = (*this)(row_indices[i],column_index);
1660 if(rows_number != columns_number)
1662 std::ostringstream buffer;
1664 buffer <<
"OpenNN Exception: Matrix Template.\n"
1665 <<
"Vector<T> get_diagonal(void) const method.\n"
1666 <<
"Matrix must be squared.\n";
1668 throw std::logic_error(buffer.str());
1675 for(
size_t i = 0; i < rows_number; i++)
1677 diagonal[i] = (*this)(i,i);
1697 if(row_index >= rows_number)
1699 std::ostringstream buffer;
1701 buffer <<
"OpenNN Exception: Matrix Template.\n"
1702 <<
"set_row(const size_t&, const Vector<T>&) method.\n"
1703 <<
"Index must be less than number of rows.\n";
1705 throw std::logic_error(buffer.str());
1708 const size_t size = new_row.size();
1710 if(size != columns_number)
1712 std::ostringstream buffer;
1714 buffer <<
"OpenNN Exception: Matrix Template.\n"
1715 <<
"set_row(const size_t&, const Vector<T>&) method.\n"
1716 <<
"Size (" << size <<
") must be equal to number of columns (" << columns_number <<
").\n";
1718 throw std::logic_error(buffer.str());
1725 for(
size_t i = 0; i < columns_number; i++)
1727 (*this)(row_index,i) = new_row[i];
1745 if(row_index >= rows_number)
1747 std::ostringstream buffer;
1749 buffer <<
"OpenNN Exception: Matrix Template.\n"
1750 <<
"set_row(const size_t&, const T&) method.\n"
1751 <<
"Index must be less than number of rows.\n";
1753 throw std::logic_error(buffer.str());
1760 for(
size_t i = 0; i < columns_number; i++)
1762 (*this)(row_index,i) = value;
1780 if(column_index >= columns_number)
1782 std::ostringstream buffer;
1784 buffer <<
"OpenNN Exception: Matrix Template.\n"
1785 <<
"set_column(const size_t&, const Vector<T>&).\n"
1786 <<
"Index (" << column_index <<
") must be less than number of columns (" << columns_number <<
").\n";
1788 throw std::logic_error(buffer.str());
1791 const size_t size = new_column.size();
1793 if(size != rows_number)
1795 std::ostringstream buffer;
1797 buffer <<
"OpenNN Exception: Matrix Template.\n"
1798 <<
"set_column(const size_t&, const Vector<T>&).\n"
1799 <<
"Size must be equal to number of rows.\n";
1801 throw std::logic_error(buffer.str());
1808 for(
size_t i = 0; i < rows_number; i++)
1810 (*this)(i,column_index) = new_column[i];
1828 if(column_index >= columns_number)
1830 std::ostringstream buffer;
1832 buffer <<
"OpenNN Exception: Matrix Template.\n"
1833 <<
"set_column(const size_t&, const T&).\n"
1834 <<
"Index must be less than number of columns.\n";
1836 throw std::logic_error(buffer.str());
1843 for(
size_t i = 0; i < rows_number; i++)
1845 (*this)(i,column_index) = value;
1864 if(rows_number != columns_number)
1866 std::ostringstream buffer;
1868 buffer <<
"OpenNN Exception: Matrix Template.\n"
1869 <<
"set_diagonal(const T&).\n"
1870 <<
"Matrix must be square.\n";
1872 throw std::logic_error(buffer.str());
1879 for(
size_t i = 0; i < rows_number; i++)
1881 (*this)(i,i) = new_diagonal;
1899 if(rows_number != columns_number)
1901 std::ostringstream buffer;
1903 buffer <<
"OpenNN Exception: Matrix Template.\n"
1904 <<
"set_diagonal(const Vector<T>&) const.\n"
1905 <<
"Matrix is not square.\n";
1907 throw std::logic_error(buffer.str());
1910 const size_t size = new_diagonal.size();
1912 if(size != rows_number)
1914 std::ostringstream buffer;
1916 buffer <<
"OpenNN Exception: Matrix Template.\n"
1917 <<
"set_diagonal(const Vector<T>&) const.\n"
1918 <<
"Size of diagonal (" << size <<
") is not equal to size of matrix (" << rows_number <<
").\n";
1920 throw std::logic_error(buffer.str());
1927 for(
size_t i = 0; i < rows_number; i++)
1929 (*this)(i,i) = new_diagonal[i];
1945 set(new_size, new_size, 0.0);
1946 set_diagonal(new_value);
1965 const size_t new_values_size = new_values.size();
1967 if(new_values_size != new_size)
1969 std::ostringstream buffer;
1971 buffer <<
"OpenNN Exception: Matrix Template.\n"
1972 <<
"initialize_diagonal(const size_t&, const size_t&) const.\n"
1973 <<
"Size of new values is not equal to size of square matrix.\n";
1975 throw std::logic_error(buffer.str());
1980 set(new_size, new_size, 0.0);
1981 set_diagonal(new_values);
1998 if(rows_number != columns_number)
2000 std::ostringstream buffer;
2002 buffer <<
"OpenNN Exception: Matrix Template.\n"
2003 <<
"sum_diagonal(const T&) const.\n"
2004 <<
"Matrix must be square.\n";
2006 throw std::logic_error(buffer.str());
2013 for(
size_t i = 0; i < rows_number; i++)
2035 if(rows_number != columns_number)
2037 std::ostringstream buffer;
2039 buffer <<
"OpenNN Exception: Matrix Template.\n"
2040 <<
"sum_diagonal(const Vector<T>&) const.\n"
2041 <<
"Matrix must be square.\n";
2043 throw std::logic_error(buffer.str());
2046 const size_t size = new_summing_values.size();
2048 if(size != rows_number)
2050 std::ostringstream buffer;
2052 buffer <<
"OpenNN Exception: Matrix Template.\n"
2053 <<
"sum_diagonal(const Vector<T>&) const.\n"
2054 <<
"Size must be equal to number of rows.\n";
2056 throw std::logic_error(buffer.str());
2063 for(
size_t i = 0; i < rows_number; i++)
2065 sum(i,i) += new_summing_values[i];
2084 const size_t size = new_row.size();
2086 if(size != columns_number)
2088 std::ostringstream buffer;
2090 buffer <<
"OpenNN Exception: Matrix Template.\n"
2091 <<
"append_row(const Vector<T>&) const.\n"
2092 <<
"Size must be equal to number of columns.\n";
2094 throw std::logic_error(buffer.str());
2101 set(rows_number+1, columns_number);
2107 (*this)(i,j) = copy(i,j);
2111 set_row(rows_number-1, new_row);
2127 const size_t size = new_column.size();
2129 if(size != columns_number)
2131 std::ostringstream buffer;
2133 buffer <<
"OpenNN Exception: Matrix Template.\n"
2134 <<
"append_column(const Vector<T>&) const.\n"
2135 <<
"Size must be equal to number of columns.\n";
2137 throw std::logic_error(buffer.str());
2142 set(rows_number, columns_number+1);
2144 set_column(columns_number-1, new_column);
2160 if(position > rows_number)
2162 std::ostringstream buffer;
2164 buffer <<
"OpenNN Exception: Matrix Template.\n"
2165 <<
"insert_row(const size_t&, const Vector<T>&) const.\n"
2166 <<
"Position must be less or equal than number of rows.\n";
2168 throw std::logic_error(buffer.str());
2171 const size_t size = new_row.size();
2173 if(size != columns_number)
2175 std::ostringstream buffer;
2177 buffer <<
"OpenNN Exception: Matrix Template.\n"
2178 <<
"insert_row(const size_t&, const Vector<T>&) const.\n"
2179 <<
"Size must be equal to number of columns.\n";
2181 throw std::logic_error(buffer.str());
2186 const size_t new_rows_number = rows_number + 1;
2188 Matrix<T> new_matrix(new_rows_number, columns_number);
2190 for(
size_t i = 0; i < position; i++)
2192 for(
size_t j = 0; j < columns_number; j++)
2194 new_matrix(i,j) = (*this)(i,j);
2198 for(
size_t j = 0; j < columns_number; j++)
2200 new_matrix(position,j) = new_row[j];
2203 for(
size_t i = position+1; i < new_rows_number; i++)
2205 for(
size_t j = 0; j < columns_number; j++)
2207 new_matrix(i,j) = (*this)(i-1,j);
2227 if(position > columns_number)
2229 std::ostringstream buffer;
2231 buffer <<
"OpenNN Exception: Matrix Template.\n"
2232 <<
"insert_column(const size_t&, const Vector<T>&) const.\n"
2233 <<
"Position must be less or equal than number of columns.\n";
2235 throw std::logic_error(buffer.str());
2238 const size_t size = (size_t)new_column.size();
2240 if(size != rows_number)
2242 std::ostringstream buffer;
2244 buffer <<
"OpenNN Exception: Matrix Template.\n"
2245 <<
"insert_column(const size_t, const Vector<T>&) const.\n"
2246 <<
"Size must be equal to number of rows.\n";
2248 throw std::logic_error(buffer.str());
2253 const size_t new_columns_number = columns_number + 1;
2255 Matrix<T> new_matrix(rows_number, new_columns_number);
2257 for(
size_t i = 0; i < rows_number; i++)
2259 for(
size_t j = 0; j < position; j++)
2261 new_matrix(i,j) = (*this)(i,j);
2264 new_matrix(i,position) = new_column[i];
2266 for(
size_t j = position+1; j < new_columns_number; j++)
2268 new_matrix(i,j) = (*this)(i,j-1);
2287 if(row_index >= rows_number)
2289 std::ostringstream buffer;
2291 buffer <<
"OpenNN Exception: Matrix Template.\n"
2292 <<
"subtract_row(const size_t&) const.\n"
2293 <<
"Index of row must be less than number of rows.\n";
2295 throw std::logic_error(buffer.str());
2297 else if(rows_number < 2)
2299 std::ostringstream buffer;
2301 buffer <<
"OpenNN Exception: Matrix Template.\n"
2302 <<
"subtract_row(const size_t&) const.\n"
2303 <<
"Number of rows must be equal or greater than two.\n";
2305 throw std::logic_error(buffer.str());
2310 Matrix<T> new_matrix(rows_number-1, columns_number);
2312 for(
size_t i = 0; i < row_index; i++)
2314 for(
size_t j = 0; j < columns_number; j++)
2316 new_matrix(i,j) = (*this)(i,j);
2320 for(
size_t i = row_index+1; i < rows_number; i++)
2322 for(
size_t j = 0; j < columns_number; j++)
2324 new_matrix(i-1,j) = (*this)(i,j);
2343 if(column_index >= columns_number)
2345 std::ostringstream buffer;
2347 buffer <<
"OpenNN Exception: Matrix Template.\n"
2348 <<
"subtract_column(const size_t&) const.\n"
2349 <<
"Index of column must be less than number of columns.\n";
2351 throw std::logic_error(buffer.str());
2353 else if(columns_number < 2)
2355 std::ostringstream buffer;
2357 buffer <<
"OpenNN Exception: Matrix Template.\n"
2358 <<
"subtract_column(const size_t&) const.\n"
2359 <<
"Number of columns must be equal or greater than two.\n";
2361 throw std::logic_error(buffer.str());
2366 Matrix<T> new_matrix(rows_number, columns_number-1);
2368 for(
size_t i = 0; i < rows_number; i++)
2370 for(
size_t j = 0; j < column_index; j++)
2372 new_matrix(i,j) = (*this)(i,j);
2376 for(
size_t i = 0; i < rows_number; i++)
2378 for(
size_t j = column_index+1; j < columns_number; j++)
2380 new_matrix(i,j-1) = (*this)(i,j);
2400 if(other_columns_number != columns_number)
2402 std::ostringstream buffer;
2404 buffer <<
"OpenNN Exception: Matrix Template.\n"
2405 <<
"Matrix<T> assemble_rows(const Matrix<T>&) const method.\n"
2406 <<
"Number of columns of other matrix (" << other_columns_number <<
") must be equal to number of columns of this matrix (" << columns_number <<
").\n";
2408 throw std::logic_error(buffer.str());
2415 Matrix<T> assembly(rows_number + other_rows_number, columns_number);
2417 for(
size_t i = 0; i < rows_number; i++)
2419 for(
size_t j = 0; j < columns_number; j++)
2421 assembly(i,j) = (*this)(i,j);
2425 for(
size_t i = 0; i < other_rows_number; i++)
2427 for(
size_t j = 0; j < columns_number; j++)
2429 assembly(rows_number+i,j) = other_matrix(i,j);
2471 if(other_rows_number != rows_number)
2473 std::ostringstream buffer;
2475 buffer <<
"OpenNN Exception: Matrix Template.\n"
2476 <<
"Matrix<T> assemble_columns(const Matrix<T>&) const method.\n"
2477 <<
"Number of rows of other matrix (" << other_rows_number <<
") must be equal to number of rows of this matrix (" << rows_number <<
").\n";
2479 throw std::logic_error(buffer.str());
2486 Matrix<T> assembly(rows_number, columns_number + other_columns_number);
2488 for(
size_t i = 0; i < rows_number; i++)
2490 for(
size_t j = 0; j < columns_number; j++)
2492 assembly(i,j) = (*this)(i,j);
2494 for(
size_t j = 0; j < other_columns_number; j++)
2496 assembly(i,columns_number+j) = other_matrix(i,j);
2512 std::fill((*this).begin(), (*this).end(), value);
2530 if(minimum > maximum)
2532 std::ostringstream buffer;
2534 buffer <<
"OpenNN Exception: Matrix Template.\n"
2535 <<
"void randomize_uniform(const double&, const double&) const method.\n"
2536 <<
"Minimum value must be less or equal than maximum value.\n";
2538 throw std::logic_error(buffer.str());
2543 for(
size_t i = 0; i < this->size(); i++)
2545 (*this)[i] = (T)calculate_random_uniform(minimum, maximum);
2564 if(minimum > maximum)
2566 std::ostringstream buffer;
2568 buffer <<
"OpenNN Exception: Matrix Template.\n"
2569 <<
"void randomize_uniform(const Matrix<double>&, const Matrix<double>&) const method.\n"
2570 <<
"Minimum values must be less or equal than their respective maximum values.\n";
2572 throw std::logic_error(buffer.str());
2578 for(
size_t i = 0; i < this->size(); i++)
2580 (*this)[i] = calculate_random_uniform(minimum[i], maximum[i]);
2599 if(standard_deviation < 0.0)
2601 std::ostringstream buffer;
2603 buffer <<
"OpenNN Exception: Matrix Template.\n"
2604 <<
"void randomize_normal(const double&, const double&) method.\n"
2605 <<
"Standard deviation must be equal or greater than zero.\n";
2607 throw std::logic_error(buffer.str());
2612 for(
size_t i = 0; i < this->size(); i++)
2614 (*this)[i] = calculate_random_normal(mean, standard_deviation);
2633 if(standard_deviation < 0.0)
2635 std::ostringstream buffer;
2637 buffer <<
"OpenNN Exception: Matrix Template.\n"
2638 <<
"void randomize_normal(const Matrix<double>&, const Matrix<double>&) const method.\n"
2639 <<
"Standard deviations must be equal or greater than zero.\n";
2641 throw std::logic_error(buffer.str());
2646 for(
size_t i = 0; i < this->size(); i++)
2648 (*this)[i] = calculate_random_uniform(mean[i], standard_deviation[i]);
2665 if(rows_number != columns_number)
2667 std::ostringstream buffer;
2669 std::cout <<
"OpenNN Exception: Matrix Template.\n"
2670 <<
"initialize_identity(void) const method.\n"
2671 <<
"Matrix must be square.\n";
2673 throw std::logic_error(buffer.str());
2678 (*this).initialize(0);
2680 for(
size_t i = 0; i < rows_number; i++)
2699 if(rows_number != columns_number)
2701 std::ostringstream buffer;
2703 std::cout <<
"OpenNN Exception: Matrix Template.\n"
2704 <<
"initialize_diagonal(const T&) const method.\n"
2705 <<
"Matrix must be square.\n";
2707 throw std::logic_error(buffer.str());
2712 for(
size_t i = 0; i < rows_number; i++)
2714 for(
size_t j = 0; j < columns_number; j++)
2718 (*this)(i,j) = value;
2738 for(
size_t i = 0; i < this->size(); i++)
2760 std::ostringstream buffer;
2762 std::cout <<
"OpenNN Exception: Matrix Template.\n"
2763 <<
"Vector<T> calculate_rows_sum(void) const method.\n"
2764 <<
"Matrix is empty.\n";
2766 throw std::logic_error(buffer.str());
2773 for(
size_t i = 0; i < rows_number; i++)
2775 for(
size_t j = 0; j < columns_number; j++)
2777 rows_sum[j] += (*this)(i,j);
2797 if(vector.size() != columns_number)
2799 std::ostringstream buffer;
2801 std::cout <<
"OpenNN Exception: Matrix Template.\n"
2802 <<
"void sum_row(const size_t&, const Vector<T>&) method.\n"
2803 <<
"Size of vector must be equal to number of columns.\n";
2805 throw std::logic_error(buffer.str());
2810 for(
size_t j = 0; j < columns_number; j++)
2812 (*this)(row_index,j) += vector[j];
2831 std::ostringstream buffer;
2833 buffer <<
"OpenNN Exception: Matrix template.\n"
2834 <<
"double calculate_trace(void) const method.\n"
2835 <<
"Matrix is not square.\n";
2837 throw std::logic_error(buffer.str());
2844 for(
size_t i = 0; i < rows_number; i++)
2846 trace += (*this)(i,i);
2865 if(rows_number == 0)
2867 std::ostringstream buffer;
2869 buffer <<
"OpenNN Exception: Matrix template.\n"
2870 <<
"Vector<double> calculate_mean(void) const method.\n"
2871 <<
"Number of rows must be greater than one.\n";
2873 throw std::logic_error(buffer.str());
2882 for(
size_t j = 0; j < columns_number; j++)
2884 for(
size_t i = 0; i < rows_number; i++)
2886 mean[j] += (*this)(i,j);
2889 mean[j] /= (double)rows_number;
2908 if(rows_number == 0)
2910 std::ostringstream buffer;
2912 buffer <<
"OpenNN Exception: Matrix template.\n"
2913 <<
"double calculate_mean(const size_t&) const method.\n"
2914 <<
"Number of rows must be greater than one.\n";
2916 throw std::logic_error(buffer.str());
2919 if(column_index >= columns_number)
2921 std::ostringstream buffer;
2923 buffer <<
"OpenNN Exception: Matrix template.\n"
2924 <<
"double calculate_mean(const size_t&) const method.\n"
2925 <<
"Index of column must be less than number of columns.\n";
2927 throw std::logic_error(buffer.str());
2936 for(
size_t i = 0; i < rows_number; i++)
2938 mean += (*this)(i,column_index);
2941 mean /= (double)rows_number;
2956 const size_t column_indices_size = column_indices.size();
2958 size_t column_index;
2964 for(
size_t j = 0; j < column_indices_size; j++)
2966 column_index = column_indices[j];
2968 for(
size_t i = 0; i < rows_number; i++)
2970 mean[j] += (*this)(i,column_index);
2973 mean[j] /= (double)rows_number;
2990 const size_t row_indices_size = row_indices.size();
2991 const size_t column_indices_size = column_indices.size();
2999 if(row_indices_size > rows_number)
3001 std::ostringstream buffer;
3003 buffer <<
"OpenNN Exception: Matrix template.\n"
3004 <<
"Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3005 <<
"Size of row indices (" << row_indices_size <<
") is greater than number of rows (" << rows_number <<
").\n";
3007 throw std::logic_error(buffer.str());
3010 for(
size_t i = 0; i < row_indices_size; i++)
3012 if(row_indices[i] >= rows_number)
3014 std::ostringstream buffer;
3016 buffer <<
"OpenNN Exception: Matrix template.\n"
3017 <<
"Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3018 <<
"Row index " << i <<
" must be less than rows number.\n";
3020 throw std::logic_error(buffer.str());
3024 if(row_indices_size == 0)
3026 std::ostringstream buffer;
3028 buffer <<
"OpenNN Exception: Matrix template.\n"
3029 <<
"Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3030 <<
"Size of row indices must be greater than zero.\n";
3032 throw std::logic_error(buffer.str());
3037 if(column_indices_size > columns_number)
3039 std::ostringstream buffer;
3041 buffer <<
"OpenNN Exception: Matrix template.\n"
3042 <<
"Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3043 <<
"Column indices size must be equal or less than columns number.\n";
3045 throw std::logic_error(buffer.str());
3048 for(
size_t i = 0; i < column_indices_size; i++)
3050 if(column_indices[i] >= columns_number)
3052 std::ostringstream buffer;
3054 buffer <<
"OpenNN Exception: Matrix template.\n"
3055 <<
"Vector<double> calculate_mean(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3056 <<
"Column index " << i <<
" must be less than columns number.\n";
3058 throw std::logic_error(buffer.str());
3065 size_t column_index;
3071 for(
size_t j = 0; j < column_indices_size; j++)
3073 column_index = column_indices[j];
3075 for(
size_t i = 0; i < row_indices_size; i++)
3077 row_index = row_indices[i];
3079 mean[j] += (*this)(row_index,column_index);
3082 mean[j] /= (double)rows_number;
3101 return(calculate_mean_missing_values(row_indices, column_indices, missing_indices));
3116 const size_t row_indices_size = row_indices.size();
3117 const size_t column_indices_size = column_indices.size();
3125 if(row_indices_size > rows_number)
3127 std::ostringstream buffer;
3129 buffer <<
"OpenNN Exception: Matrix template.\n"
3130 <<
"Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3131 <<
"Size of row indices (" << row_indices_size <<
") is greater than number of rows (" << rows_number <<
").\n";
3133 throw std::logic_error(buffer.str());
3136 for(
size_t i = 0; i < row_indices_size; i++)
3138 if(row_indices[i] >= rows_number)
3140 std::ostringstream buffer;
3142 buffer <<
"OpenNN Exception: Matrix template.\n"
3143 <<
"Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, Vector< Vector<size_t> >&) const method.\n"
3144 <<
"Row index " << i <<
" must be less than rows number.\n";
3146 throw std::logic_error(buffer.str());
3150 if(row_indices_size == 0)
3152 std::ostringstream buffer;
3154 buffer <<
"OpenNN Exception: Matrix template.\n"
3155 <<
"Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3156 <<
"Size of row indices must be greater than zero.\n";
3158 throw std::logic_error(buffer.str());
3163 if(column_indices_size > columns_number)
3165 std::ostringstream buffer;
3167 buffer <<
"OpenNN Exception: Matrix template.\n"
3168 <<
"Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3169 <<
"Column indices size must be equal or less than columns number.\n";
3171 throw std::logic_error(buffer.str());
3174 for(
size_t i = 0; i < column_indices_size; i++)
3176 if(column_indices[i] >= columns_number)
3178 std::ostringstream buffer;
3180 buffer <<
"OpenNN Exception: Matrix template.\n"
3181 <<
"Vector<double> calculate_mean_missing_values(const Vector<size_t>&, const Vector<size_t>&, const Vector< Vector<size_t> >&) const method.\n"
3182 <<
"Column index " << i <<
" must be less than columns number.\n";
3184 throw std::logic_error(buffer.str());
3191 size_t column_index;
3199 for(
size_t j = 0; j < column_indices_size; j++)
3201 column_index = column_indices[j];
3203 for(
size_t i = 0; i < row_indices_size; i++)
3205 row_index = row_indices[i];
3207 if(!missing_indices[j].contains(row_index))
3209 mean[j] += (*this)(row_index,column_index);
3214 mean[j] /= (double)count[j];
3234 if(rows_number == 0)
3236 std::ostringstream buffer;
3238 buffer <<
"OpenNN Exception: Matrix template.\n"
3239 <<
"Vector<double> calculate_mean_standard_deviation(void) const method.\n"
3240 <<
"Number of rows must be greater than one.\n";
3242 throw std::logic_error(buffer.str());
3252 for(
size_t i = 0; i < columns_number; i++)
3263 mean_standard_deviation[0] = mean;
3264 mean_standard_deviation[1] = standard_deviation;
3266 return(mean_standard_deviation);
3280 const size_t column_indices_size = column_indices.size();
3285 size_t column_index;
3289 for(
size_t i = 0; i < column_indices_size; i++)
3291 column_index = column_indices[i];
3293 column = arrange_column(column_index);
3303 mean_standard_deviation[0] = mean;
3304 mean_standard_deviation[1] = standard_deviation;
3306 return(mean_standard_deviation);
3321 const size_t row_indices_size = row_indices.size();
3322 const size_t column_indices_size = column_indices.size();
3330 if(row_indices_size > rows_number)
3332 std::ostringstream buffer;
3334 buffer <<
"OpenNN Exception: Matrix template.\n"
3335 <<
"Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3336 <<
"Row indices size must be equal or less than rows number.\n";
3338 throw std::logic_error(buffer.str());
3341 for(
size_t i = 0; i < row_indices_size; i++)
3343 if(row_indices[i] >= rows_number)
3345 std::ostringstream buffer;
3347 buffer <<
"OpenNN Exception: Matrix template.\n"
3348 <<
"Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3349 <<
"Row index " << i <<
" must be less than rows number.\n";
3351 throw std::logic_error(buffer.str());
3355 if(row_indices_size == 0)
3357 std::ostringstream buffer;
3359 buffer <<
"OpenNN Exception: Matrix template.\n"
3360 <<
"Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3361 <<
"Size of row indices must be greater than zero.\n";
3363 throw std::logic_error(buffer.str());
3368 if(column_indices_size > columns_number)
3370 std::ostringstream buffer;
3372 buffer <<
"OpenNN Exception: Matrix template.\n"
3373 <<
"Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3374 <<
"Column indices size must be equal or less than columns number.\n";
3376 throw std::logic_error(buffer.str());
3379 for(
size_t i = 0; i < column_indices_size; i++)
3381 if(column_indices[i] >= columns_number)
3383 std::ostringstream buffer;
3385 buffer <<
"OpenNN Exception: Matrix template.\n"
3386 <<
"Vector<double> calculate_mean_standard_deviation(const Vector<size_t>&, const Vector<size_t>&) const method.\n"
3387 <<
"Column index " << i <<
" must be less than columns number.\n";
3389 throw std::logic_error(buffer.str());
3396 size_t column_index;
3402 for(
size_t j = 0; j < column_indices_size; j++)
3404 column_index = column_indices[j];
3408 for(
size_t i = 0; i < row_indices_size; i++)
3410 row_index = row_indices[i];
3412 mean[j] += (*this)(row_index,column_index);
3415 mean[j] /= (double)rows_number;
3422 for(
size_t j = 0; j < column_indices_size; j++)
3424 column_index = column_indices[j];
3426 standard_deviation[j] = 0.0;
3428 for(
size_t i = 0; i < row_indices_size; i++)
3430 row_index = row_indices[i];
3432 standard_deviation[j] += ((*this)(row_index,column_index) - mean[j])*((*this)(row_index,column_index) - mean[j]);
3435 standard_deviation[j] = sqrt(standard_deviation[j]/(rows_number-1.0));
3442 mean_standard_deviation[0] = mean;
3443 mean_standard_deviation[1] = standard_deviation;
3445 return(mean_standard_deviation);
3456 T minimum = (T)1.0e99;
3458 for(
size_t i = 0; i < this->size(); i++)
3460 if((*
this)[i] < minimum)
3462 minimum = (*this)[i];
3477 T maximum = (T)-1.0e99;
3479 for(
size_t i = 0; i < this->size(); i++)
3481 if((*
this)[i] > maximum)
3483 maximum = (*this)[i];
3502 Vector<T> minimum(columns_number, (T)1.0e99);
3503 Vector<T> maximum(columns_number, (T)-1.0e99);
3505 for(
size_t j = 0; j < columns_number; j++)
3507 for(
size_t i = 0; i < rows_number; i++)
3509 if((*
this)(i,j) < minimum[j])
3511 minimum[j] = (*this)(i,j);
3514 if((*
this)(i,j) > maximum[j])
3516 maximum[j] = (*this)(i,j);
3523 minimum_maximum[0] = minimum;
3524 minimum_maximum[1] = maximum;
3526 return(minimum_maximum);
3540 const size_t column_indices_size = column_indices.size();
3544 for(
size_t i = 0; i < column_indices_size; i++)
3546 if(column_indices[i] >= columns_number)
3548 std::ostringstream buffer;
3550 buffer <<
"OpenNN Exception: Matrix template."
3551 <<
"Vector<T> calculate_minimum_maximum(const Vector<size_t>&) const method.\n"
3552 <<
"Index of column must be less than number of columns.\n";
3554 throw std::logic_error(buffer.str());
3560 size_t column_index;
3562 Vector<T> minimum(column_indices_size, (T)1.0e99);
3563 Vector<T> maximum(column_indices_size, (T)-1.0e99);
3565 for(
size_t j = 0; j < column_indices_size; j++)
3567 column_index = column_indices[j];
3569 for(
size_t i = 0; i < rows_number; i++)
3571 if((*
this)(i,column_index) < minimum[j])
3573 minimum[j] = (*this)(i,column_index);
3576 if((*
this)(i,column_index) > maximum[j])
3578 maximum[j] = (*this)(i,column_index);
3587 minimum_maximum[0] = minimum;
3588 minimum_maximum[1] = maximum;
3590 return(minimum_maximum);
3605 const size_t row_indices_size = row_indices.size();
3606 const size_t column_indices_size = column_indices.size();
3608 Vector<T> minimum(column_indices_size, (T)1.0e99);
3609 Vector<T> maximum(column_indices_size, (T)-1.0e99);
3612 size_t column_index;
3614 for(
size_t j = 0; j < column_indices_size; j++)
3616 column_index = column_indices[j];
3618 for(
size_t i = 0; i < row_indices_size; i++)
3620 row_index = row_indices[i];
3622 if((*
this)(row_index,column_index) < minimum[j])
3624 minimum[j] = (*this)(row_index,column_index);
3627 if((*
this)(row_index,column_index) > maximum[j])
3629 maximum[j] = (*this)(row_index,column_index);
3638 minimum_maximum[0] = minimum;
3639 minimum_maximum[1] = maximum;
3641 return(minimum_maximum);
3658 if(rows_number == 0)
3660 std::ostringstream buffer;
3662 buffer <<
"OpenNN Exception: Matrix template.\n"
3663 <<
"Vector< Statistics<double> > calculate_statistics(void) const method.\n"
3664 <<
"Number of rows must be greater than one.\n";
3666 throw std::logic_error(buffer.str());
3675 for(
size_t i = 0; i < columns_number; i++)
3677 column = arrange_column(i);
3700 if(rows_number == 0)
3702 std::ostringstream buffer;
3704 buffer <<
"OpenNN Exception: Matrix template.\n"
3705 <<
"Vector< Statistics<double> > calculate_statistics_missing_values(const Vector< Vector<size_t> >&) const method.\n"
3706 <<
"Number of rows must be greater than one.\n";
3708 throw std::logic_error(buffer.str());
3711 if(missing_indices.size() != columns_number)
3713 std::ostringstream buffer;
3715 buffer <<
"OpenNN Exception: Matrix template.\n"
3716 <<
"Vector< Statistics<double> > calculate_statistics_missing_values(const Vector< Vector<size_t> >&) const method.\n"
3717 <<
"Size of missing indices (" << missing_indices.size() <<
") must be equal to to number of columns (" << columns_number <<
").\n";
3719 throw std::logic_error(buffer.str());
3728 for(
size_t i = 0; i < columns_number; i++)
3730 column = arrange_column(i);
3750 const size_t row_indices_size = row_indices.size();
3751 const size_t column_indices_size = column_indices.size();
3759 for(
size_t i = 0; i < column_indices_size; i++)
3761 index = column_indices[i];
3763 column = arrange_column(index, row_indices);
3782 const size_t row_indices_size = row_indices.size();
3788 for(
size_t i = 0; i < columns_number; i++)
3790 column = arrange_column(i, row_indices);
3810 const size_t row_indices_size = row_indices.size();
3816 for(
size_t i = 0; i < columns_number; i++)
3818 column = arrange_column(i, row_indices);
3837 const size_t column_indices_size = column_indices.size();
3844 for(
size_t i = 0; i < column_indices_size; i++)
3846 index = column_indices[i];
3848 column = arrange_column(index);
3868 const size_t column_indices_size = column_indices.size();
3875 for(
size_t i = 0; i < column_indices_size; i++)
3877 index = column_indices[i];
3879 column = arrange_column(index);
3903 for(
size_t i = 0; i < columns_number; i++)
3905 column = arrange_column(i);
3930 for(
size_t i = 0; i < columns_number; i++)
3932 column = arrange_column(i);
3953 for(
size_t i = 0; i < rows_number; i++)
3955 for(
size_t j = 0; j < columns_number; j++)
3957 if((*
this)(i,j) < value && indices.
empty())
3966 else if((*
this)(i,j) < value)
3992 for(
size_t i = 0; i < rows_number; i++)
3994 for(
size_t j = 0; j < columns_number; j++)
3996 if((*
this)(i,j) > value && indices.
empty())
4005 else if((*
this)(i,j) > value)
4032 const size_t size = statistics.size();
4034 if(size != columns_number)
4036 std::ostringstream buffer;
4038 buffer <<
"OpenNN Exception: Matrix template."
4039 <<
"void scale_mean_standard_deviation(const Vector< Statistics<T> >&) const method.\n"
4040 <<
"Size of statistics vector must be equal to number of columns.\n";
4042 throw std::logic_error(buffer.str());
4049 for(
size_t j = 0; j < columns_number; j++)
4051 if(statistics[j].standard_deviation < 1e-99)
4057 for(
size_t i = 0; i < rows_number; i++)
4059 (*this)(i,j) = ((*
this)(i,j) - statistics[j].mean)/statistics[j].standard_deviation;
4077 scale_mean_standard_deviation(statistics);
4096 const size_t statistics_size = statistics.size();
4098 if(statistics_size != columns_number)
4100 std::ostringstream buffer;
4102 buffer <<
"OpenNN Exception: Vector template.\n"
4103 <<
"void scale_rows_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4104 <<
"Size of statistics must be equal to number of columns.\n";
4106 throw std::logic_error(buffer.str());
4115 for(
size_t j = 0; j < columns_number; j++)
4117 if(statistics[j].standard_deviation < 1e-99)
4123 for(
size_t i = 0; i < row_indices.size(); i++)
4125 row_index = row_indices[i];
4127 (*this)(row_index,j) = ((*
this)(row_index,j) - statistics[j].mean)/statistics[j].standard_deviation;
4145 const size_t columns_indices_size = columns_indices.size();
4151 const size_t statistics_size = statistics.size();
4153 if(statistics_size != columns_indices_size)
4155 std::ostringstream buffer;
4157 buffer <<
"OpenNN Exception: Vector template.\n"
4158 <<
"void scale_columns_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4159 <<
"Size of statistics must be equal to size of columns indices.\n";
4161 throw std::logic_error(buffer.str());
4166 size_t column_index;
4170 for(
size_t j = 0; j < columns_indices_size; j++)
4172 if(statistics[j].standard_deviation < 1e-99)
4178 column_index = columns_indices[j];
4180 for(
size_t i = 0; i < rows_number; i++)
4182 (*this)(i,column_index) = ((*
this)(i,column_index) - statistics[j].mean)/statistics[j].standard_deviation;
4201 const size_t size = statistics.size();
4203 if(size != columns_number)
4205 std::ostringstream buffer;
4207 buffer <<
"OpenNN Exception: Matrix template."
4208 <<
"void scale_minimum_maximum(const Vector< Statistics<T> >&) method.\n"
4209 <<
"Size of statistics vector must be equal to number of columns.\n";
4211 throw std::logic_error(buffer.str());
4218 for(
size_t j = 0; j < columns_number; j++)
4220 if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4226 for(
size_t i = 0; i < rows_number; i++)
4228 (*this)(i,j) = 2.0*((*
this)(i,j) - statistics[j].minimum)/(statistics[j].maximum-statistics[j].minimum)-1.0;
4246 scale_minimum_maximum(statistics);
4263 const size_t row_indices_size = row_indices.size();
4267 const size_t statistics_size = statistics.size();
4269 if(statistics_size != columns_number)
4271 std::ostringstream buffer;
4273 buffer <<
"OpenNN Exception: Vector template.\n"
4274 <<
"void scale_rows_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4275 <<
"Size of statistics must be equal to number of columns.\n";
4277 throw std::logic_error(buffer.str());
4286 for(
size_t j = 0; j < columns_number; j++)
4288 if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4294 for(
size_t i = 0; i < row_indices_size; i++)
4296 row_index = row_indices[i];
4298 (*this)(row_index,j) = 2.0*((*
this)(row_index,j) - statistics[j].minimum)/(statistics[j].maximum-statistics[j].minimum) - 1.0;
4318 const size_t column_indices_size = column_indices.size();
4322 const size_t statistics_size = statistics.size();
4324 if(statistics_size != column_indices_size)
4326 std::ostringstream buffer;
4328 buffer <<
"OpenNN Exception: Vector template.\n"
4329 <<
"void scale_columns_minimum_maximum(const Vector< Statistics<T> >&, const Vector<size_t>&) method.\n"
4330 <<
"Size of statistics must be equal to size of columns indices.\n";
4332 throw std::logic_error(buffer.str());
4337 size_t column_index;
4341 for(
size_t j = 0; j < column_indices_size; j++)
4343 column_index = column_indices[j];
4345 if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4351 for(
size_t i = 0; i < rows_number; i++)
4353 (*this)(i,column_index) = 2.0*((*
this)(i,column_index) - statistics[j].minimum)/(statistics[j].maximum-statistics[j].minimum) - 1.0;
4372 const size_t size = statistics.size();
4374 if(size != columns_number)
4376 std::ostringstream buffer;
4378 buffer <<
"OpenNN Exception: Matrix template."
4379 <<
"void unscale_mean_standard_deviation(const Vector< Statistics<T> >&) const method.\n"
4380 <<
"Size of statistics vector must be equal to number of columns.\n";
4382 throw std::logic_error(buffer.str());
4387 for(
size_t j = 0; j < columns_number; j++)
4389 if(statistics[j].standard_deviation < 1e-99)
4395 for(
size_t i = 0; i < rows_number; i++)
4397 (*this)(i,j) = (*
this)(i,j)*statistics[j].standard_deviation + statistics[j].mean;
4418 for(
size_t j = 0; j < columns_number; j++)
4420 if(statistics[j].standard_deviation < 1e-99)
4426 for(
size_t i = 0; i < rows_number; i++)
4428 row_index = row_indices[i];
4430 (*this)(row_index,j) = (*
this)(row_index,j)*statistics[j].standard_deviation + statistics[j].mean;
4450 if(statistics.size() != columns_number)
4452 std::ostringstream buffer;
4454 buffer <<
"OpenNN Exception: Matrix template.\n"
4455 <<
"void unscale_columns_mean_standard_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) const method.\n"
4456 <<
"Size of statistics vector must be equal to number of columns.\n";
4458 throw std::logic_error(buffer.str());
4463 size_t column_index;
4467 for(
size_t j = 0; j < column_indices.size(); j++)
4469 column_index = column_indices[j];
4471 if(statistics[column_index].standard_deviation < 1e-99)
4477 for(
size_t i = 0; i < rows_number; i++)
4479 (*this)(i,column_index) = (*
this)(i,column_index)*statistics[column_index].standard_deviation + statistics[column_index].mean;
4497 const size_t size = statistics.size();
4499 if(size != columns_number)
4501 std::ostringstream buffer;
4503 buffer <<
"OpenNN Exception: Matrix template."
4504 <<
"void unscale_minimum_maximum(const Vector< Statistics<T> >&) method.\n"
4505 <<
"Size of minimum vector must be equal to number of columns.\n";
4507 throw std::logic_error(buffer.str());
4512 for(
size_t j = 0; j < columns_number; j++)
4514 if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4516 std::cout <<
"OpenNN Warning: Matrix template.\n"
4517 <<
"void unscale_minimum_maximum(const Vector< Statistics<T> >&) const method.\n"
4518 <<
"Minimum and maximum values of column " << j <<
" are equal.\n"
4519 <<
"Those columns won't be unscaled.\n";
4525 for(
size_t i = 0; i < rows_number; i++)
4527 (*this)(i,j) = 0.5*((*
this)(i,j) + 1.0)*(statistics[j].maximum-statistics[j].minimum) + statistics[j].minimum;
4548 for(
size_t j = 0; j < columns_number; j++)
4550 if(statistics[j].maximum - statistics[j].minimum < 1e-99)
4556 for(
size_t i = 0; i < rows_number; i++)
4558 row_index = row_indices[i];
4560 (*this)(row_index,j) = 0.5*((*
this)(row_index,j) + 1.0)*(statistics[j].maximum-statistics[j].minimum)
4561 + statistics[j].minimum;
4581 if(statistics.size() != columns_number)
4583 std::ostringstream buffer;
4585 buffer <<
"OpenNN Exception: Matrix template.\n"
4586 <<
"void unscale_columns_minimum_maximum_deviation(const Vector< Statistics<T> >&, const Vector<size_t>&) const method.\n"
4587 <<
"Size of statistics vector must be equal to number of columns.\n";
4589 throw std::logic_error(buffer.str());
4594 size_t column_index;
4598 for(
size_t j = 0; j < column_indices.size(); j++)
4600 column_index = column_indices[j];
4602 if(statistics[column_index].maximum - statistics[column_index].minimum < 1e-99)
4608 for(
size_t i = 0; i < rows_number; i++)
4610 (*this)(i,column_index) = 0.5*((*
this)(i,column_index) + 1.0)*(statistics[column_index].maximum-statistics[column_index].minimum)
4611 + statistics[column_index].minimum;
4625 T minimum = (*this)(0,0);
4628 for(
size_t i = 0; i < rows_number; i++)
4630 for(
size_t j = 0; j < columns_number; j++)
4632 if((*
this)(i,j) < minimum)
4634 minimum = (*this)(i,j);
4635 minimal_indices[0] = i;
4636 minimal_indices[1] = j;
4641 return(minimal_indices);
4652 T maximum = (*this)(0,0);
4656 for(
size_t i = 0; i < rows_number; i++)
4658 for(
size_t j = 0; j < columns_number; j++)
4660 if((*
this)(i,j) > maximum)
4662 maximum = (*this)(i,j);
4663 maximal_indices[0] = i;
4664 maximal_indices[1] = j;
4669 return(maximal_indices);
4683 T minimum = (*this)(0,0);
4684 T maximum = (*this)(0,0);
4689 for(
size_t i = 0; i < rows_number; i++)
4691 for(
size_t j = 0; j < columns_number; j++)
4693 if((*
this)(i,j) < minimum)
4695 minimum = (*this)(i,j);
4696 minimal_indices[0] = i;
4697 minimal_indices[1] = j;
4700 if((*
this)(i,j) > maximum)
4702 maximum = (*this)(i,j);
4703 maximal_indices[0] = i;
4704 maximal_indices[1] = j;
4710 minimal_maximal_indices[0] = minimal_indices;
4711 minimal_maximal_indices[1] = maximal_indices;
4713 return(minimal_maximal_indices);
4731 if(other_rows_number != rows_number)
4733 std::ostringstream buffer;
4735 buffer <<
"OpenNN Exception: Matrix Template.\n"
4736 <<
"double calculate_sum_squared_error(const Matrix<double>&) const method.\n"
4737 <<
"Other number of rows must be equal to this number of rows.\n";
4739 throw std::logic_error(buffer.str());
4744 if(other_columns_number != columns_number)
4746 std::ostringstream buffer;
4748 buffer <<
"OpenNN Exception: Matrix Template.\n"
4749 <<
"double calculate_sum_squared_error(const Matrix<double>&) const method.\n"
4750 <<
"Other number of columns must be equal to this number of columns.\n";
4752 throw std::logic_error(buffer.str());
4758 double sum_squared_error = 0.0;
4760 for(
size_t i = 0; i < rows_number; i++)
4762 sum_squared_error += ((*this)[i] - other_matrix[i])*((*
this)[i] - other_matrix[i]);
4765 return(sum_squared_error);
4782 const size_t size = vector.size();
4784 if(size != columns_number)
4786 std::ostringstream buffer;
4788 buffer <<
"OpenNN Exception: Matrix Template.\n"
4789 <<
"double calculate_sum_squared_error(const Vector<double>&) const method.\n"
4790 <<
"Size must be equal to number of columns.\n";
4792 throw std::logic_error(buffer.str());
4797 double sum_squared_error = 0.0;
4799 for(
size_t i = 0; i < rows_number; i++)
4801 for(
size_t j = 0; j < columns_number; j++)
4803 sum_squared_error += ((*this)(i,j) - vector[j])*((*this)(i,j) - vector[j]);
4807 return(sum_squared_error);
4821 for(
size_t i = 0; i < rows_number; i++)
4823 for(
size_t j = 0; j < columns_number; j++)
4825 rows_norm[i] += (*this)(i,j)*(*
this)(i,j);
4828 rows_norm[i] = sqrt(rows_norm[i]);
4842 Matrix<T> absolute_value(rows_number, columns_number);
4844 for(
size_t i = 0; i < this->size(); i++)
4848 absolute_value[i] = (*this)[i];
4852 absolute_value[i] = -(*this)[i];
4856 return(absolute_value);
4868 Matrix<T> transpose(columns_number, rows_number);
4870 for(
size_t i = 0; i < columns_number; i++)
4872 for(
size_t j = 0; j < rows_number; j++)
4874 transpose(i,j) = (*this)(j,i);
4895 std::ostringstream buffer;
4897 buffer <<
"OpenNN Exception: Matrix Template.\n"
4898 <<
"calculate_determinant(void) const method.\n"
4899 <<
"Matrix is empty.\n";
4901 throw std::logic_error(buffer.str());
4904 if(rows_number != columns_number)
4906 std::ostringstream buffer;
4908 buffer <<
"OpenNN Exception: Matrix Template.\n"
4909 <<
"calculate_determinant(void) const method.\n"
4910 <<
"Matrix must be square.\n";
4912 throw std::logic_error(buffer.str());
4919 if(rows_number == 1)
4921 determinant = (*this)(0,0);
4923 else if(rows_number == 2)
4925 determinant = (*this)(0,0)*(*
this)(1,1) - (*
this)(1,0)*(*
this)(0,1);
4931 for(
size_t row_index = 0; row_index < rows_number; row_index++)
4935 Matrix<T> sub_matrix(rows_number-1, columns_number-1);
4937 for(
size_t i = 1; i < rows_number; i++)
4941 for(
size_t j = 0; j < columns_number; j++)
4948 sub_matrix(i-1,j2) = (*this)(i,j);
4956 sign =
static_cast<int>( (((row_index + 2) % 2) == 0) ? 1 : -1 );
4962 return(determinant);
4973 Matrix<T> cofactor(rows_number, columns_number);
4975 Matrix<T> c(rows_number-1, columns_number-1);
4977 for(
size_t j = 0; j < rows_number; j++)
4979 for(
size_t i = 0; i < rows_number; i++)
4985 for(
size_t ii = 0; ii < rows_number; ii++)
4994 for(
size_t jj = 0; jj < rows_number; jj++)
5001 c(i1,j1) = (*this)(ii,jj);
5009 cofactor(i,j) =
static_cast<T
>((((i + j) % 2) == 0) ? 1 : -1)*determinant;
5032 std::ostringstream buffer;
5034 buffer <<
"OpenNN Exception: Matrix Template.\n"
5035 <<
"calculate_inverse(void) const method.\n"
5036 <<
"Matrix is empty.\n";
5038 throw std::logic_error(buffer.str());
5041 if(rows_number != columns_number)
5043 std::ostringstream buffer;
5045 buffer <<
"OpenNN Exception: Matrix Template.\n"
5046 <<
"calculate_inverse(void) const method.\n"
5047 <<
"Matrix must be square.\n";
5049 throw std::logic_error(buffer.str());
5054 const double determinant = calculate_determinant();
5056 if(determinant == 0.0)
5058 std::ostringstream buffer;
5060 buffer <<
"OpenNN Exception: Matrix Template.\n"
5061 <<
"calculate_inverse(void) const method.\n"
5062 <<
"Matrix is singular.\n";
5064 throw std::logic_error(buffer.str());
5067 if(rows_number == 1)
5069 Matrix<T> inverse(1, 1, 1.0/determinant);
5076 const Matrix<T> cofactor = calculate_cofactor();
5084 const Matrix<T> inverse = adjoint/determinant;
5098 Matrix<T> sum(rows_number, columns_number);
5100 std::transform(this->begin(), this->end(), sum.begin(), std::bind2nd(std::plus<T>(), scalar));
5118 const size_t size = vector.size();
5120 if(size != rows_number)
5122 std::ostringstream buffer;
5124 buffer <<
"OpenNN Exception: Matrix Template.\n"
5125 <<
"Matrix<T> operator + (const Vector<T>&) const.\n"
5126 <<
"Size of vector must be equal to number of rows.\n";
5128 throw std::logic_error(buffer.str());
5133 Matrix<T> sum(rows_number, columns_number);
5135 for(
size_t i = 0; i < rows_number; i++)
5137 for(
size_t j = 0; j < columns_number; j++)
5139 sum(i,j) = (*this)(i,j) + vector[i];
5162 if(other_rows_number != rows_number || other_columns_number != columns_number)
5164 std::ostringstream buffer;
5166 buffer <<
"OpenNN Exception: Matrix Template.\n"
5167 <<
"Matrix<T> operator + (const Matrix<T>&) const.\n"
5168 <<
"Sizes of other matrix (" << other_rows_number <<
"," << other_columns_number <<
") must be the same than sizes of this matrix (" << rows_number <<
"," << columns_number <<
").\n";
5170 throw std::logic_error(buffer.str());
5175 Matrix<T> sum(rows_number, columns_number);
5177 std::transform(this->begin(), this->end(), other_matrix.begin(), sum.begin(), std::plus<T>());
5191 Matrix<T> difference(rows_number, columns_number);
5193 std::transform( this->begin(), this->end(), difference.begin(), std::bind2nd(std::minus<T>(), scalar));
5211 const size_t size = vector.size();
5213 if(size != rows_number)
5215 std::ostringstream buffer;
5217 buffer <<
"OpenNN Exception: Matrix Template.\n"
5218 <<
"Matrix<T> operator - (const Vector<T>&) const.\n"
5219 <<
"Size of vector must be equal to number of rows.\n";
5221 throw std::logic_error(buffer.str());
5226 Matrix<T> difference(rows_number, columns_number);
5228 for(
size_t i = 0; i < rows_number; i++)
5230 for(
size_t j = 0; j < columns_number; j++)
5232 difference(i,j) = (*this)(i,j) - vector[i];
5255 if(other_rows_number != rows_number || other_columns_number != columns_number)
5257 std::ostringstream buffer;
5259 buffer <<
"OpenNN Exception: Matrix Template.\n"
5260 <<
"Matrix<T> operator - (const Matrix<T>&) const method.\n"
5261 <<
"Sizes of other matrix (" << other_rows_number <<
"," << other_columns_number <<
") must be equal to sizes of this matrix ("<< rows_number <<
"," << columns_number <<
").\n";
5263 throw std::logic_error(buffer.str());
5268 Matrix<T> difference(rows_number, columns_number);
5270 std::transform( this->begin(), this->end(), other_matrix.begin(), difference.begin(), std::minus<T>());
5284 Matrix<T> product(rows_number, columns_number);
5286 for(
size_t i = 0; i < this->size(); i++)
5288 product[i] = (*this)[i]*scalar;
5307 const size_t size = vector.size();
5309 if(size != rows_number)
5311 std::ostringstream buffer;
5313 buffer <<
"OpenNN Exception: Matrix Template.\n"
5314 <<
"Matrix<T> operator * (const Vector<T>&) const method.\n"
5315 <<
"Vector size (" << size <<
") must be equal to number of matrix rows (" << rows_number <<
").\n";
5317 throw std::logic_error(buffer.str());
5322 Matrix<T> product(rows_number, columns_number);
5324 for(
size_t i = 0; i < rows_number; i++)
5326 for(
size_t j = 0; j < columns_number; j++)
5328 product(i,j) = (*this)(i,j)*vector[i];
5351 if(other_rows_number != rows_number || other_columns_number != columns_number)
5353 std::ostringstream buffer;
5355 buffer <<
"OpenNN Exception: Matrix Template.\n"
5356 <<
"Matrix<T> operator * (const Matrix<T>&) const method.\n"
5357 <<
"Sizes of other matrix (" << other_rows_number <<
"," << other_columns_number <<
") must be equal to sizes of this matrix (" << rows_number <<
"," << columns_number <<
").\n";
5359 throw std::logic_error(buffer.str());
5364 Matrix<T> product(rows_number, columns_number);
5366 for(
size_t i = 0; i < this->size(); i++)
5368 product[i] = (*this)[i]*other_matrix[i];
5383 Matrix<T> results(rows_number, columns_number);
5385 for(
size_t i = 0; i < results.size(); i++)
5387 results[i] = (*this)[i]/scalar;
5406 const size_t size = vector.size();
5408 if(size != columns_number)
5410 std::ostringstream buffer;
5412 buffer <<
"OpenNN Exception: Matrix Template.\n"
5413 <<
"Matrix<T> operator / (const Vector<T>&) const.\n"
5414 <<
"Size of vector must be equal to number of columns.\n";
5416 throw std::logic_error(buffer.str());
5421 Matrix<T> cocient(rows_number, columns_number);
5423 for(
size_t i = 0; i < rows_number; i++)
5425 for(
size_t j = 0; j < columns_number; j++)
5427 cocient(i,j) = (*this)(i,j)/vector[j];
5450 if(other_rows_number != rows_number || other_columns_number != columns_number)
5452 std::ostringstream buffer;
5454 buffer <<
"OpenNN Exception: Matrix Template.\n"
5455 <<
"Matrix<T> operator / (const Matrix<T>&) const method.\n"
5456 <<
"Both matrix sizes must be the same.\n";
5458 throw std::logic_error(buffer.str());
5463 Matrix<T> cocient(rows_number, columns_number);
5465 for(
size_t i = 0; i < rows_number; i++)
5467 cocient[i] = (*this)[i]/other_matrix[i];
5482 for(
size_t i = 0; i < rows_number; i++)
5484 for(
size_t j = 0; j < columns_number; j++)
5486 (*this)(i,j) += value;
5506 if(other_rows_number != rows_number)
5508 std::ostringstream buffer;
5510 buffer <<
"OpenNN Exception: Matrix Template.\n"
5511 <<
"void operator += (const Matrix<T>&).\n"
5512 <<
"Both numbers of rows must be the same.\n";
5514 throw std::logic_error(buffer.str());
5519 if(other_columns_number != columns_number)
5521 std::ostringstream buffer;
5523 buffer <<
"OpenNN Exception: Matrix Template.\n"
5524 <<
"void operator += (const Matrix<T>&).\n"
5525 <<
"Both numbers of columns must be the same.\n";
5527 throw std::logic_error(buffer.str());
5532 for(
size_t i = 0; i < rows_number; i++)
5534 for(
size_t j = 0; j < columns_number; j++)
5536 (*this)(i,j) += other_matrix(i,j);
5550 for(
size_t i = 0; i < rows_number; i++)
5552 for(
size_t j = 0; j < columns_number; j++)
5554 (*this)(i,j) -= value;
5574 if(other_rows_number != rows_number)
5576 std::ostringstream buffer;
5578 buffer <<
"OpenNN Exception: Matrix Template.\n"
5579 <<
"void operator -= (const Matrix<T>&).\n"
5580 <<
"Both numbers of rows must be the same.\n";
5582 throw std::logic_error(buffer.str());
5587 if(other_columns_number != columns_number)
5589 std::ostringstream buffer;
5591 buffer <<
"OpenNN Exception: Matrix Template.\n"
5592 <<
"void operator -= (const Matrix<T>&).\n"
5593 <<
"Both numbers of columns must be the same.\n";
5595 throw std::logic_error(buffer.str());
5600 for(
size_t i = 0; i < rows_number; i++)
5602 for(
size_t j = 0; j < columns_number; j++)
5604 (*this)(i,j) -= other_matrix(i,j);
5618 for(
size_t i = 0; i < rows_number; i++)
5620 for(
size_t j = 0; j < columns_number; j++)
5622 (*this)(i,j) *= value;
5642 const size_t rows_number = get_rows_number();
5644 if(other_rows_number != rows_number)
5646 std::ostringstream buffer;
5648 buffer <<
"OpenNN Exception: Matrix Template.\n"
5649 <<
"void operator *= (const Matrix<T>&).\n"
5650 <<
"The number of rows in the other matrix (" << other_rows_number <<
")"
5651 <<
" is not equal to the number of rows in this matrix (" << rows_number <<
").\n";
5653 throw std::logic_error(buffer.str());
5658 for(
size_t i = 0; i < rows_number; i++)
5660 for(
size_t j = 0; j < columns_number; j++)
5662 (*this)(i,j) *= other_matrix(i,j);
5676 for(
size_t i = 0; i < rows_number; i++)
5678 for(
size_t j = 0; j < columns_number; j++)
5680 (*this)(i,j) /= value;
5700 if(other_rows_number != rows_number)
5702 std::ostringstream buffer;
5704 buffer <<
"OpenNN Exception: Matrix Template.\n"
5705 <<
"void operator /= (const Matrix<T>&).\n"
5706 <<
"Both numbers of rows must be the same.\n";
5708 throw std::logic_error(buffer.str());
5713 if(other_columns_number != columns_number)
5715 std::ostringstream buffer;
5717 buffer <<
"OpenNN Exception: Matrix Template.\n"
5718 <<
"void operator /= (const Matrix<T>&).\n"
5719 <<
"Both numbers of columns must be the same.\n";
5721 throw std::logic_error(buffer.str());
5726 for(
size_t i = 0; i < rows_number; i++)
5728 for(
size_t j = 0; j < columns_number; j++)
5730 (*this)(i,j) /= other_matrix(i,j);
5778 const size_t size = vector.size();
5780 if(size != columns_number)
5782 std::ostringstream buffer;
5784 buffer <<
"OpenNN Exception: Matrix Template.\n"
5785 <<
"Vector<T> dot(const Vector<T>&) const method.\n"
5786 <<
"Vector size must be equal to matrix number of columns.\n";
5788 throw std::logic_error(buffer.str());
5807 const Eigen::Map<Eigen::MatrixXd> matrix_eigen((
double*)this->data(), rows_number, columns_number);
5808 const Eigen::Map<Eigen::VectorXd> vector_eigen((
double*)vector.data(), columns_number);
5809 Eigen::Map<Eigen::VectorXd> product_eigen(product.data(), rows_number);
5811 product_eigen = matrix_eigen*vector_eigen;
5832 if(other_rows_number != columns_number)
5834 std::ostringstream buffer;
5836 buffer <<
"OpenNN Exception: Matrix Template.\n"
5837 <<
"Matrix<T> dot(const Matrix<T>&) const method.\n"
5838 <<
"The number of rows of the other matrix (" << other_rows_number <<
") must be equal to the number of columns of this matrix (" << columns_number <<
").\n";
5840 throw std::logic_error(buffer.str());
5845 Matrix<T> product(rows_number, other_columns_number);
5855 const Eigen::Map<Eigen::MatrixXd> this_eigen((
double*)this->data(), rows_number, columns_number);
5856 const Eigen::Map<Eigen::MatrixXd> other_eigen((
double*)other_matrix.data(), other_rows_number, other_columns_number);
5857 Eigen::Map<Eigen::MatrixXd> product_eigen(product.data(), rows_number, other_columns_number);
5859 product_eigen = this_eigen*other_eigen;
5877 Matrix<T> direct(rows_number*other_rows_number, columns_number*other_columns_number);
5882 for(
size_t i = 0; i < rows_number; i++)
5884 for(
size_t j = 0; j < columns_number; j++)
5886 for(
size_t k = 0; k < other_rows_number; k++)
5888 for(
size_t l = 0; l < other_columns_number; l++)
5890 alpha = other_rows_number*i+k;
5891 beta = other_columns_number*j+l;
5893 direct(alpha,beta) = (*this)(i,j)*other_matrix(k,l);
5910 if(rows_number == 0 && columns_number == 0)
5929 if(rows_number == columns_number)
5952 if(rows_number != columns_number)
5954 std::ostringstream buffer;
5956 buffer <<
"OpenNN Exception: Matrix Template.\n"
5957 <<
"bool is_symmetric(void) const method.\n"
5958 <<
"Matrix must be squared.\n";
5960 throw std::logic_error(buffer.str());
5965 const Matrix<T> transpose = calculate_transpose();
5967 if((*
this) == transpose)
5990 if(rows_number != columns_number)
5992 std::ostringstream buffer;
5994 buffer <<
"OpenNN Exception: Matrix Template.\n"
5995 <<
"bool is_antisymmetric(void) const method.\n"
5996 <<
"Matrix must be squared.\n";
5998 throw std::logic_error(buffer.str());
6003 const Matrix<T> transpose = calculate_transpose();
6005 if((*
this) == transpose*(-1))
6028 if(rows_number != columns_number)
6030 std::ostringstream buffer;
6032 buffer <<
"OpenNN Exception: Matrix Template.\n"
6033 <<
"bool is_diagonal(void) const method.\n"
6034 <<
"Matrix must be squared.\n";
6036 throw std::logic_error(buffer.str());
6041 for(
size_t i = 0; i < rows_number; i++)
6043 for(
size_t j = 0; j < columns_number; j++)
6045 if(i != j && (*
this)(i,j) != 0)
6068 if(rows_number != columns_number)
6070 std::ostringstream buffer;
6072 buffer <<
"OpenNN Exception: Matrix Template.\n"
6073 <<
"bool is_scalar(void) const method.\n"
6074 <<
"Matrix must be squared.\n";
6076 throw std::logic_error(buffer.str());
6097 if(rows_number != columns_number)
6099 std::ostringstream buffer;
6101 buffer <<
"OpenNN Exception: Matrix Template.\n"
6102 <<
"bool is_unity(void) const method.\n"
6103 <<
"Matrix must be squared.\n";
6105 throw std::logic_error(buffer.str());
6110 for(
size_t i = 0; i < rows_number; i++)
6112 for(
size_t j = 0; j < columns_number; j++)
6114 if(i != j && (*
this)(i,j) != 0)
6118 else if(i == j && (*
this)(i,j) != 1)
6139 const Vector<T> column = arrange_column(column_index);
6141 const size_t new_rows_number = rows_number
6144 Matrix<T> new_matrix(new_rows_number, columns_number);
6146 size_t row_index = 0;
6150 for(
size_t i = 0; i < rows_number; i++)
6152 if((*
this)(i,column_index) >= minimum && (*
this)(i,column_index) <= maximum)
6154 row = arrange_row(i);
6156 new_matrix.
set_row(row_index, row);
6175 const size_t new_rows_number = rows_number - lags_number;
6176 const size_t new_columns_number = columns_number*(1 + lags_number);
6178 Matrix<T> new_matrix(new_rows_number, new_columns_number);
6182 for(
size_t i = 0; i < new_rows_number; i++)
6184 row = arrange_row(i);
6186 for(
size_t j = 1; j <= lags_number; j++)
6188 row = row.
assemble(arrange_row(i+j));
6191 new_matrix.set_row(i, row);
6226 if(column_index >= columns_number)
6228 std::ostringstream buffer;
6230 buffer <<
"OpenNN Exception: Matrix Template.\n"
6231 <<
"void convert_angular_variables_degrees(const size_t&) method.\n"
6232 <<
"Index of column (" << column_index <<
") must be less than number of columns.\n";
6234 throw std::logic_error(buffer.str());
6239 const double pi = 4.0*atan(1.0);
6246 for(
size_t i = 0; i < rows_number; i++)
6248 if((*
this)(i,column_index) != -99.9)
6250 angle_rad = pi*(*this)(i,column_index)/180.0;
6252 sin_angle[i] = sin(angle_rad);
6253 cos_angle[i] = cos(angle_rad);
6257 sin_angle[i] = (T)-99.9;
6258 cos_angle[i] = (T)-99.9;
6262 set_column(column_index, sin_angle);
6263 insert_column(column_index+1, cos_angle);
6280 if(column_index >= columns_number)
6282 std::ostringstream buffer;
6284 buffer <<
"OpenNN Exception: Matrix Template.\n"
6285 <<
"void convert_angular_variables_radians(const size_t&) method.\n"
6286 <<
"Index of column (" << column_index <<
") must be less than number of columns.\n";
6288 throw std::logic_error(buffer.str());
6296 for(
size_t i = 0; i < rows_number; i++)
6298 sin_angle[i] = sin((*
this)(i,column_index));
6299 cos_angle[i] = cos((*
this)(i,column_index));
6302 set_column(column_index, sin_angle);
6303 insert_column(column_index+1, cos_angle);
6326 std::ifstream file(file_name.c_str());
6330 std::ostringstream buffer;
6332 buffer <<
"OpenNN Exception: Matrix template.\n"
6333 <<
"void load(const std::string&) method.\n"
6334 <<
"Cannot open matrix data file: " << file_name <<
"\n";
6336 throw std::logic_error(buffer.str());
6339 if(file.peek() == std::ifstream::traits_type::eof())
6360 std::getline(file, line);
6368 std::istringstream buffer(line);
6370 std::istream_iterator<std::string> it(buffer);
6371 std::istream_iterator<std::string> end;
6373 const std::vector<std::string> results(it, end);
6375 const size_t new_columns_number = (size_t)results.size();
6377 size_t new_rows_number = 1;
6381 getline(file, line);
6389 set(new_rows_number, new_columns_number);
6394 file.seekg(0, std::ios::beg);
6396 for(
size_t i = 0; i < rows_number; i++)
6398 for(
size_t j = 0; j < columns_number; j++)
6400 file >> (*this)(i,j);
6419 std::ofstream file(file_name.c_str());
6423 std::ostringstream buffer;
6425 buffer <<
"OpenNN Exception: Matrix template." << std::endl
6426 <<
"void save(const std::string) method." << std::endl
6427 <<
"Cannot open matrix data file." << std::endl;
6429 throw std::logic_error(buffer.str());
6434 for(
size_t i = 0; i < rows_number; i++)
6436 for(
size_t j = 0; j < columns_number; j++)
6438 file << (*this)(i,j) <<
" ";
6458 std::ofstream file(file_name.c_str());
6462 std::ostringstream buffer;
6464 buffer <<
"OpenNN Exception: Matrix template." << std::endl
6465 <<
"void save(const std::string) method." << std::endl
6466 <<
"Cannot open matrix data file." << std::endl;
6468 throw std::logic_error(buffer.str());
6473 for(
size_t j = 0; j < columns_number; j++)
6477 if(j != columns_number-1)
6485 for(
size_t i = 0; i < rows_number; i++)
6487 for(
size_t j = 0; j < columns_number; j++)
6489 file << (*this)(i,j);
6491 if(j != columns_number-1)
6523 std::istringstream str_buffer(str);
6527 std::getline(str_buffer, line);
6529 std::istringstream line_buffer(line);
6531 std::istream_iterator<std::string> it(line_buffer);
6532 std::istream_iterator<std::string> end;
6534 const std::vector<std::string> results(it, end);
6536 const size_t new_columns_number = (size_t)results.size();
6538 size_t new_rows_number = 1;
6540 while(str_buffer.good())
6542 getline(str_buffer, line);
6550 set(new_rows_number, new_columns_number);
6555 str_buffer.seekg(0, std::ios::beg);
6557 for(
size_t i = 0; i < rows_number; i++)
6559 for(
size_t j = 0; j < columns_number; j++)
6561 str_buffer >> (*this)(i,j);
6577 std::ostringstream buffer;
6579 if(rows_number > 0 && columns_number > 0)
6581 buffer << arrange_row(0).to_string(separator);
6583 for(
size_t i = 1; i < rows_number; i++)
6586 << arrange_row(i).to_string(separator);
6590 return(buffer.str());
6603 std::ostringstream buffer;
6605 for(
size_t i = 0; i < rows_number; i++)
6607 for(
size_t j = 0; j < columns_number; j++)
6610 buffer << std::setprecision(precision) << (*this)(i,j);
6612 string_matrix(i,j) = buffer.str();
6616 return(string_matrix);
6629 const std::vector<T> std_vector((*this).begin(), (*this).end());
6644 Vector<T> vector(rows_number*columns_number);
6646 for(
size_t i = 0; i < rows_number*columns_number; i++)
6648 vector[i] = (*this)[i];
6663 std::cout <<
"Rows number: " << rows_number << std::endl
6664 <<
"Columns number: " << columns_number << std::endl;
6668 const Vector<T> first_row = arrange_row(0);
6670 std::cout <<
"Row 0:\n" << first_row << std::endl;
6675 const Vector<T> second_row = arrange_row(1);
6677 std::cout <<
"Row 1:\n" << second_row << std::endl;
6682 const Vector<T> last_row = arrange_row(rows_number-1);
6684 std::cout <<
"Row " << rows_number <<
":\n" << last_row << std::endl;
6694 std::istream& operator >> (std::istream& is,
Matrix<T>& m)
6699 for(
size_t i = 0; i < rows_number; i++)
6701 for(
size_t j = 0; j < columns_number; j++)
6718 std::ostream& operator << (std::ostream& os, const Matrix<T>& m)
6723 if(rows_number > 0 && columns_number > 0)
6727 for(
size_t i = 1; i < rows_number; i++)
6745 std::ostream& operator << (std::ostream& os, const Matrix< Vector<T> >& m)
6750 for(
size_t i = 0; i < rows_number; i++)
6752 for(
size_t j = 0; j < columns_number; j++)
6754 os <<
"subvector_" << i <<
"_" << j <<
"\n"
6755 << m(i,j) << std::endl;
6770 std::ostream& operator << (std::ostream& os, const Matrix< Matrix<T> >& m)
6775 for(
size_t i = 0; i < rows_number; i++)
6777 for(
size_t j = 0; j < columns_number; j++)
6779 os <<
"submatrix_" << i <<
"_" << j <<
"\n"
Vector< Statistics< T > > calculate_rows_statistics_missing_values(const Vector< size_t > &, const Vector< Vector< size_t > > &) const
Vector< double > calculate_mean(void) const
size_t count_less_than(const T &) const
Vector< T > arrange_column(const size_t &) const
void scale_columns_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
Vector< size_t > calculate_minimal_indices(void) const
Returns the row and column indices corresponding to the entry with minimum value. ...
Vector< Histogram< T > > calculate_histograms_missing_values(const Vector< Vector< size_t > > &, const size_t &=10) const
bool is_scalar(void) const
T calculate_maximum(void) const
Returns the maximum value from all elements in the matrix.
Vector< Statistics< T > > calculate_statistics_missing_values(const Vector< Vector< size_t > > &) const
void insert_row(const size_t &, const Vector< T > &)
void tuck_in(const size_t &, const size_t &, const Matrix< T > &)
Matrix< T > direct(const Matrix< T > &) const
bool operator>=(const Matrix< T > &) const
void scale_rows_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
void print(void) const
Prints to the screen in the matrix object.
double calculate_trace(void) const
Vector< Histogram< T > > calculate_histograms(const size_t &=10) const
bool operator>(const Matrix< T > &) const
void set_columns_number(const size_t &)
void set_rows_number(const size_t &)
Vector< T > get_diagonal(void) const
Returns the diagonal of the matrix.
Matrix< T > calculate_cofactor(void) const
Returns the cofactor matrix.
T & operator()(const size_t &, const size_t &)
Reference operator.
void append_column(const Vector< T > &)
double calculate_sum_squared_error(const Matrix< double > &) const
void save(const std::string &) const
Vector< size_t > calculate_maximal_indices(void) const
Returns the row and column indices corresponding to the entry with maximum value. ...
bool is_diagonal(void) const
void unscale_columns_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
void operator-=(const T &)
Statistics< T > calculate_statistics_missing_values(const Vector< size_t > &) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
Matrix< T > operator+(const T &) const
Vector< double > calculate_mean_missing_values(const Vector< Vector< size_t > > &) const
void operator+=(const T &value)
void insert_column(const size_t &, const Vector< T > &)
std::string to_string(const std::string &=" ") const
void unscale_rows_mean_standard_deviation(const Vector< Statistics< T > > &, const Vector< size_t > &)
T calculate_minimum(void) const
Returns the minimum value from all elements in the matrix.
Matrix< T > arrange_submatrix(const Vector< size_t > &, const Vector< size_t > &) const
void initialize_diagonal(const size_t &, const T &)
Matrix< std::string > write_string_matrix(const size_t &=3) const
Returns a new matrix in which each entry has been converted to a string.
Vector< T > calculate_rows_sum(void) const
Returns the sum of all the rows in the matrix.
Vector< Statistics< T > > calculate_rows_statistics(const Vector< size_t > &) const
Vector< Vector< T > > calculate_minimum_maximum(void) const
Matrix< T > calculate_absolute_value(void) const
Returns a matrix with the absolute values of this matrix.
void load(const std::string &)
void convert_angular_variables_radians(const size_t &)
void set_diagonal(const T &)
bool is_identity(void) const
Matrix(void)
Default constructor. It creates a matrix with zero rows and zero columns.
size_t count_off_diagonal_elements(void) const
Histogram< T > calculate_histogram(const size_t &=10) const
size_t count_greater_than(const T &) const
void scale_columns_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
Matrix< size_t > calculate_greater_than_indices(const T &) const
Vector< T > assemble(const Vector< T > &) const
Matrix< T > arrange_submatrix_columns(const Vector< size_t > &) const
void convert_angular_variables_degrees(const size_t &)
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
size_t columns_number
Number of columns in the matrix.
void unscale_rows_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
void convert_autoassociation(void)
Matrix< size_t > calculate_less_than_indices(const T &) const
Matrix< T > calculate_inverse(void) const
Matrix< T > assemble_columns(const Matrix< T > &) const
void subtract_column(const size_t &)
Vector< Statistics< T > > calculate_statistics(void) const
Vector< Vector< size_t > > calculate_minimal_maximal_indices(void) const
void save_csv(const std::string &) const
void parse(const std::string &)
void unscale_mean_standard_deviation(const Vector< Statistics< T > > &)
bool is_square(void) const
void set_identity(const size_t &)
double calculate_standard_deviation(void) const
Returns the standard deviation of the elements in the vector.
void initialize_identity(void)
void set_column(const size_t &, const Vector< T > &)
void initialize(const T &)
Matrix< T > operator-(const T &scalar) const
Vector< Vector< double > > calculate_mean_standard_deviation(void) const
size_t count_diagonal_elements(void) const
void convert_time_series(const size_t &)
Matrix< T > operator*(const T &) const
void sort_less(const size_t &)
void set(void)
This method set the numbers of rows and columns of the matrix to zero.
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
void sort_greater(const size_t &)
Matrix< T > calculate_transpose(void) const
Returns the transpose of the matrix.
size_t rows_number
Number of rows in the matrix.
bool operator<(const Matrix< T > &) const
bool operator<=(const Matrix< T > &) const
Vector< Statistics< T > > calculate_columns_statistics_missing_values(const Vector< size_t > &, const Vector< Vector< size_t > >) const
void operator*=(const T &)
void print_preview(void) const
Histogram< T > calculate_histogram_missing_values(const Vector< size_t > &, const size_t &=10) const
void randomize_uniform(const double &=-1.0, const double &=1.0)
void append_row(const Vector< T > &)
Vector< double > calculate_rows_norm(void) const
Vector< double > dot(const Vector< double > &) const
Vector< Statistics< T > > scale_minimum_maximum(void)
Statistics< T > calculate_statistics(void) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
bool operator!=(const Matrix< T > &) const
Matrix< T > assemble_rows(const Matrix< T > &) const
Vector< Statistics< T > > scale_mean_standard_deviation(void)
std::vector< T > to_std_vector(void) const
bool is_antisymmetric(void) const
void operator/=(const T &)
bool is_symmetric(void) const
double calculate_mean(void) const
Returns the mean of the elements in the vector.
void sum_row(const size_t &, const Vector< T > &)
Matrix< T > filter(const size_t &, const T &, const T &) const
void unscale_columns_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
Matrix< T > sum_diagonal(const T &) const
bool empty(void) const
Returns true if number of rows and columns is zero.
Vector< Statistics< T > > calculate_columns_statistics(const Vector< size_t > &) const
Matrix< T > operator/(const T &) const
Vector< T > to_vector(void) const
void set_row(const size_t &, const Vector< T > &)
void unscale_minimum_maximum(const Vector< Statistics< T > > &)
Matrix< T > & operator=(const Matrix< T > &)
bool operator==(const Matrix< T > &) const
T calculate_sum(void) const
Returns the sum of all the elements in the matrix.
virtual ~Matrix(void)
Destructor.
Vector< T > arrange_row(const size_t &) const
Matrix< T > arrange_submatrix_rows(const Vector< size_t > &) const
void scale_rows_minimum_maximum(const Vector< Statistics< T > > &, const Vector< size_t > &)
void subtract_row(const size_t &)
void randomize_normal(const double &=0.0, const double &=1.0)
T calculate_determinant(void) const
Returns the determinant of a square matrix.