35 #include <itpp/itexports.h> 41 template<
class Num_T>
class Vec;
43 template<
class Num_T>
class Mat;
56 Mat<Num_T>
operator+(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2);
59 Mat<Num_T>
operator+(
const Mat<Num_T> &m, Num_T t);
62 Mat<Num_T>
operator+(Num_T t,
const Mat<Num_T> &m);
66 Mat<Num_T>
operator-(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2);
69 Mat<Num_T>
operator-(
const Mat<Num_T> &m, Num_T t);
72 Mat<Num_T>
operator-(Num_T t,
const Mat<Num_T> &m);
75 Mat<Num_T>
operator-(
const Mat<Num_T> &m);
79 Mat<Num_T>
operator*(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2);
82 Vec<Num_T>
operator*(
const Mat<Num_T> &m,
const Vec<Num_T> &v);
85 Mat<Num_T>
operator*(
const Mat<Num_T> &m, Num_T t);
88 Mat<Num_T>
operator*(Num_T t,
const Mat<Num_T> &m);
92 Mat<Num_T>
elem_mult(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2);
95 void elem_mult_out(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2,
99 void elem_mult_out(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2,
100 const Mat<Num_T> &m3, Mat<Num_T> &out);
102 template<
class Num_T>
103 void elem_mult_out(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2,
104 const Mat<Num_T> &m3,
const Mat<Num_T> &m4,
107 template<
class Num_T>
110 template<
class Num_T>
111 Num_T
elem_mult_sum(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2);
114 template<
class Num_T>
115 Mat<Num_T>
operator/(
const Mat<Num_T> &m, Num_T t);
117 template<
class Num_T>
118 Mat<Num_T>
operator/(Num_T t,
const Mat<Num_T> &m);
121 template<
class Num_T>
122 Mat<Num_T>
elem_div(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2);
124 template<
class Num_T>
125 void elem_div_out(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2,
128 template<
class Num_T>
129 Num_T
elem_div_sum(
const Mat<Num_T> &m1,
const Mat<Num_T> &m2);
200 template<
class Num_T>
228 Mat(
const Num_T *c_array,
int rows,
int cols,
bool row_major =
true,
249 void set(
const std::string &str);
251 void set(
const char *str);
262 const Num_T &
get(
int r,
int c)
const;
264 const Num_T &
get(
int i)
const;
266 void set(
int r,
int c, Num_T t);
279 Mat<Num_T> get(
int r1,
int r2,
int c1,
int c2)
const;
463 bool in_range(
int r,
int c)
const {
467 bool row_in_range(
int r)
const {
return ((r >= 0) && (r <
no_rows)); }
469 bool col_in_range(
int c)
const {
return ((c >= 0) && (c <
no_cols)); }
471 bool in_range(
int i)
const {
return ((i >= 0) && (i <
datasize)); }
526 template <
class Num_T>
527 std::ostream &operator<<(std::ostream &os, const Mat<Num_T> &m);
540 template <
class Num_T>
541 std::istream &
operator>>(std::istream &is, Mat<Num_T> &m);
547 template<
class Num_T>
inline 550 if ((rows > 0) && (cols > 0)) {
551 datasize = rows * cols;
564 template<
class Num_T>
inline 574 template<
class Num_T>
inline 576 datasize(0), no_rows(0), no_cols(0), data(0), factory(f) {}
578 template<
class Num_T>
inline 580 datasize(0), no_rows(0), no_cols(0), data(0), factory(f)
582 it_assert_debug((rows >= 0) && (cols >= 0),
"Mat<>::Mat(): Wrong size");
586 template<
class Num_T>
inline 588 datasize(0), no_rows(0), no_cols(0), data(0), factory(m.factory)
590 alloc(m.no_rows, m.no_cols);
591 copy_vector(m.datasize, m.data, data);
594 template<
class Num_T>
inline 596 datasize(0), no_rows(0), no_cols(0), data(0), factory(f)
598 alloc(m.no_rows, m.no_cols);
599 copy_vector(m.datasize, m.data, data);
602 template<
class Num_T>
inline 604 datasize(0), no_rows(0), no_cols(0), data(0), factory(f)
611 template<
class Num_T>
inline 613 datasize(0), no_rows(0), no_cols(0), data(0), factory(f)
618 template<
class Num_T>
inline 620 datasize(0), no_rows(0), no_cols(0), data(0), factory(f)
622 set(std::string(str));
625 template<
class Num_T>
628 datasize(0), no_rows(0), no_cols(0), data(0), factory(f)
632 copy_vector(datasize, c_array, data);
634 for (
int i = 0; i < rows; i++)
635 for (
int j = 0; j < cols; j++)
636 data[i+j*no_rows] = c_array[i*no_cols+j];
639 template<
class Num_T>
inline 646 template<
class Num_T>
650 "Mat<>::set_size(): Wrong size");
652 if ((no_rows == rows) && (no_cols == cols))
655 if ((rows == 0) || (cols == 0)) {
664 int old_datasize = datasize;
665 int old_rows = no_rows;
667 int min_r = (no_rows < rows) ? no_rows : rows;
668 int min_c = (no_cols < cols) ? no_cols : cols;
672 for (
int i = 0; i < min_c; ++i) {
673 copy_vector(min_r, &tmp[i*old_rows], &data[i*no_rows]);
676 for (
int i = min_r; i < rows; ++i)
677 for (
int j = 0; j < cols; ++j)
678 data[i+j*rows] = Num_T(0);
679 for (
int j = min_c; j < cols; ++j)
680 for (
int i = 0; i < min_r; ++i)
681 data[i+j*rows] = Num_T(0);
686 else if (datasize == rows * cols) {
697 template<
class Num_T>
inline 700 for (
int i = 0; i < datasize; i++)
704 template<
class Num_T>
inline 707 for (
int i = 0; i < datasize; i++)
711 template<
class Num_T>
inline 715 "Mat<>::operator(): Indexing out of range");
716 return data[r+c*no_rows];
719 template<
class Num_T>
inline 723 "Mat<>::operator(): Indexing out of range");
724 return data[r+c*no_rows];
727 template<
class Num_T>
inline 734 template<
class Num_T>
inline 741 template<
class Num_T>
inline 744 return (*
this)(r, c);
747 template<
class Num_T>
inline 753 template<
class Num_T>
inline 756 it_assert_debug(in_range(r, c),
"Mat<>::set(): Indexing out of range");
757 data[r+c*no_rows] = t;
761 template<
class Num_T>
773 std::string::size_type beg = 0;
774 std::string::size_type end = 0;
775 while (end != std::string::npos) {
777 end = str.find(
';', beg);
780 int v_size = v.
size();
784 if ((end != std::string::npos) || (v_size > 0)) {
787 set_size(maxrows, v_size,
true);
792 if ((rows == maxrows) || (v_size != no_cols)) {
794 if (rows == maxrows) {
798 if (v_size > no_cols) {
799 set_size(maxrows, v_size,
true);
802 set_size(maxrows, no_cols,
true);
804 v.set_size(no_cols,
true);
816 set_size(rows, no_cols,
true);
819 template<
class Num_T>
inline 822 set(std::string(str));
825 template<
class Num_T>
inline 828 if (r1 == -1) r1 = no_rows - 1;
829 if (r2 == -1) r2 = no_rows - 1;
830 if (c1 == -1) c1 = no_cols - 1;
831 if (c2 == -1) c2 = no_cols - 1;
834 (c1 >= 0) && (c1 <= c2) && (c2 < no_cols),
835 "Mat<>::operator()(r1, r2, c1, c2): Wrong indexing");
839 for (
int i = 0;i < s.no_cols;i++)
840 copy_vector(s.no_rows, data + r1 + (c1 + i)*no_rows, s.data + i*s.no_rows);
845 template<
class Num_T>
inline 848 return (*
this)(r1, r2, c1, c2);
851 template<
class Num_T>
inline 854 it_assert_debug(row_in_range(r),
"Mat<>::get_row(): Index out of range");
857 copy_vector(no_cols, data + r, no_rows, a.
_data(), 1);
861 template<
class Num_T>
865 "Mat<>::get_rows(): Wrong indexing");
868 for (
int i = 0; i < m.rows(); i++)
869 copy_vector(no_cols, data + i + r1, no_rows, m.data + i, m.no_rows);
874 template<
class Num_T>
879 for (
int i = 0;i < indexlist.
size();i++) {
881 "Mat<>::get_rows(indexlist): Indexing out of range");
882 copy_vector(no_cols, data + indexlist(i), no_rows, m.data + i, m.no_rows);
888 template<
class Num_T>
inline 891 it_assert_debug(col_in_range(c),
"Mat<>::get_col(): Index out of range");
894 copy_vector(no_rows, data + c*no_rows, a.
_data());
899 template<
class Num_T>
903 "Mat<>::get_cols(): Wrong indexing");
906 for (
int i = 0; i < m.cols(); i++)
907 copy_vector(no_rows, data + (i + c1)*no_rows, m.data + i*m.no_rows);
912 template<
class Num_T>
917 for (
int i = 0; i < indexlist.
size(); i++) {
919 "Mat<>::get_cols(indexlist): Indexing out of range");
920 copy_vector(no_rows, data + indexlist(i)*no_rows, m.data + i*m.no_rows);
926 template<
class Num_T>
inline 929 it_assert_debug(row_in_range(r),
"Mat<>::set_row(): Index out of range");
931 "Mat<>::set_row(): Wrong size of input vector");
932 copy_vector(v.
size(), v.
_data(), 1, data + r, no_rows);
935 template<
class Num_T>
inline 938 it_assert_debug(col_in_range(c),
"Mat<>::set_col(): Index out of range");
940 "Mat<>::set_col(): Wrong size of input vector");
941 copy_vector(v.
size(), v.
_data(), data + c*no_rows);
945 template<
class Num_T>
948 it_assert_debug(row_in_range(r),
"Mat<>::set_rows(): Index out of range");
950 "Mat<>::set_rows(): Column sizes do not match");
952 "Mat<>::set_rows(): Not enough rows");
954 for (
int i = 0; i < m.rows(); ++i) {
955 copy_vector(no_cols, m.data + i, m.no_rows, data + i + r, no_rows);
959 template<
class Num_T>
962 it_assert_debug(col_in_range(c),
"Mat<>::set_cols(): Index out of range");
964 "Mat<>::set_cols(): Row sizes do not match");
966 "Mat<>::set_cols(): Not enough colums");
968 for (
int i = 0; i < m.cols(); ++i) {
969 copy_vector(no_rows, m.data + i*no_rows, data + (i + c)*no_rows);
974 template<
class Num_T>
inline 978 "Mat<>::copy_row(): Indexing out of range");
982 copy_vector(no_cols, data + from, no_rows, data +
to, no_rows);
985 template<
class Num_T>
inline 989 "Mat<>::copy_col(): Indexing out of range");
993 copy_vector(no_rows, data + from*no_rows, data +
to*no_rows);
996 template<
class Num_T>
inline 1000 "Mat<>::swap_rows(): Indexing out of range");
1004 swap_vector(no_cols, data + r1, no_rows, data + r2, no_rows);
1007 template<
class Num_T>
inline 1011 "Mat<>::swap_cols(): Indexing out of range");
1015 swap_vector(no_rows, data + c1*no_rows, data + c2*no_rows);
1018 template<
class Num_T>
1021 it_warning(
"Mat<>::set_submatrix(r1, r2, r3, r4, m): This function is " 1022 "deprecated and might be removed from future IT++ releases. " 1023 "Please use Mat<>::set_submatrix(r, c, m) function instead.");
1024 set_submatrix(r1, c1, m);
1027 template<
class Num_T>
inline 1031 (c >= 0) && (c + m.no_cols <= no_cols),
1032 "Mat<>::set_submatrix(): Indexing out of range " 1033 "or wrong input matrix");
1034 for (
int i = 0; i < m.no_cols; i++)
1035 copy_vector(m.no_rows, m.data + i*m.no_rows, data + (c + i)*no_rows + r);
1040 template<
class Num_T>
inline 1043 if (r1 == -1) r1 = no_rows - 1;
1044 if (r2 == -1) r2 = no_rows - 1;
1045 if (c1 == -1) c1 = no_cols - 1;
1046 if (c2 == -1) c2 = no_cols - 1;
1048 (c1 >= 0) && (c1 <= c2) && (c2 < no_cols),
1049 "Mat<>::set_submatrix(): Wrong indexing");
1050 for (
int i = c1; i <= c2; i++) {
1051 int pos = i * no_rows + r1;
1052 for (
int j = r1; j <= r2; j++)
1057 template<
class Num_T>
1060 it_assert_debug(row_in_range(r),
"Mat<>::del_row(): Index out of range");
1062 set_size(no_rows - 1, no_cols,
false);
1063 for (
int i = 0 ; i < r ; i++) {
1064 copy_vector(no_cols, &Temp.data[i], no_rows + 1, &data[i], no_rows);
1066 for (
int i = r ; i < no_rows ; i++) {
1067 copy_vector(no_cols, &Temp.data[i+1], no_rows + 1, &data[i], no_rows);
1072 template<
class Num_T>
1076 "Mat<>::del_rows(): Indexing out of range");
1078 int no_del_rows = r2 - r1 + 1;
1079 set_size(no_rows - no_del_rows, no_cols,
false);
1080 for (
int i = 0; i < r1 ; ++i) {
1081 copy_vector(no_cols, &Temp.data[i], Temp.no_rows, &data[i], no_rows);
1083 for (
int i = r2 + 1; i < Temp.no_rows; ++i) {
1084 copy_vector(no_cols, &Temp.data[i], Temp.no_rows, &data[i-no_del_rows],
1089 template<
class Num_T>
1092 it_assert_debug(col_in_range(c),
"Mat<>::del_col(): Index out of range");
1095 set_size(no_rows, no_cols - 1,
false);
1096 copy_vector(c*no_rows, Temp.data, data);
1097 copy_vector((no_cols - c)*no_rows, &Temp.data[(c+1)*no_rows], &data[c*no_rows]);
1100 template<
class Num_T>
1104 "Mat<>::del_cols(): Indexing out of range");
1106 int n_deleted_cols = c2 - c1 + 1;
1107 set_size(no_rows, no_cols - n_deleted_cols,
false);
1108 copy_vector(c1*no_rows, Temp.data, data);
1109 copy_vector((no_cols - c1)*no_rows, &Temp.data[(c2+1)*no_rows], &data[c1*no_rows]);
1112 template<
class Num_T>
1116 "Mat<>::ins_row(): Index out of range");
1118 "Mat<>::ins_row(): Wrong size of the input vector");
1125 set_size(no_rows + 1, no_cols,
false);
1127 for (
int i = 0 ; i < r ; i++) {
1128 copy_vector(no_cols, &Temp.data[i], no_rows - 1, &data[i], no_rows);
1130 copy_vector(no_cols, v.
_data(), 1, &data[r], no_rows);
1131 for (
int i = r + 1 ; i < no_rows ; i++) {
1132 copy_vector(no_cols, &Temp.data[i-1], no_rows - 1, &data[i], no_rows);
1136 template<
class Num_T>
1140 "Mat<>::ins_col(): Index out of range");
1142 "Mat<>::ins_col(): Wrong size of the input vector");
1149 set_size(no_rows, no_cols + 1,
false);
1151 copy_vector(c*no_rows, Temp.data, data);
1152 copy_vector(no_rows, v.
_data(), &data[c*no_rows]);
1153 copy_vector((no_cols - c - 1)*no_rows, &Temp.data[c*no_rows], &data[(c+1)*no_rows]);
1156 template<
class Num_T>
inline 1159 ins_row(no_rows, v);
1162 template<
class Num_T>
inline 1165 ins_col(no_cols, v);
1168 template<
class Num_T>
1172 for (
int i = 0; i < no_rows; ++i) {
1173 copy_vector(no_cols, &data[i], no_rows, &temp.data[i * no_cols], 1);
1178 template<
class Num_T>
1182 for (
int i = 0; i < no_rows; ++i) {
1183 copy_vector(no_cols, &data[i], no_rows, &temp.data[i * no_cols], 1);
1193 template<
class Num_T>
1197 if (m1.no_cols == 0)
1199 if (m2.no_cols == 0)
1202 "Mat<>::concat_horizontal(): Wrong sizes");
1203 int no_rows = m1.no_rows;
1204 Mat<Num_T> temp(no_rows, m1.no_cols + m2.no_cols);
1205 for (
int i = 0; i < m1.no_cols; ++i) {
1206 copy_vector(no_rows, &m1.data[i * no_rows], &temp.data[i * no_rows]);
1208 for (
int i = 0; i < m2.no_cols; ++i) {
1209 copy_vector(no_rows, &m2.data[i * no_rows], &temp.data[(m1.no_cols + i)
1215 template<
class Num_T>
1219 if (m1.no_rows == 0)
1221 if (m2.no_rows == 0)
1224 "Mat<>::concat_vertical(): Wrong sizes");
1225 int no_cols = m1.no_cols;
1226 Mat<Num_T> temp(m1.no_rows + m2.no_rows, no_cols);
1227 for (
int i = 0; i < no_cols; ++i) {
1228 copy_vector(m1.no_rows, &m1.data[i * m1.no_rows],
1229 &temp.data[i * temp.no_rows]);
1230 copy_vector(m2.no_rows, &m2.data[i * m2.no_rows],
1231 &temp.data[i * temp.no_rows + m1.no_rows]);
1236 template<
class Num_T>
inline 1239 for (
int i = 0; i < datasize; i++)
1244 template<
class Num_T>
inline 1248 set_size(m.no_rows, m.no_cols,
false);
1249 if (m.datasize != 0)
1250 copy_vector(m.datasize, m.data, data);
1255 template<
class Num_T>
inline 1259 || ((no_cols == 1) && (no_rows == v.
size())),
1260 "Mat<>::operator=(): Wrong size of the input vector");
1261 set_size(v.
size(), 1,
false);
1266 template<
class Num_T>
inline 1273 template<
class Num_T>
inline 1276 set(std::string(str));
1282 template<
class Num_T>
1288 int i, j, m_pos = 0, pos = 0;
1289 it_assert_debug(m.no_rows == no_rows && m.no_cols == no_cols,
"Mat<Num_T>::operator+=: wrong sizes");
1290 for (i = 0; i < no_cols; i++) {
1291 for (j = 0; j < no_rows; j++)
1292 data[pos+j] += m.data[m_pos+j];
1300 template<
class Num_T>
inline 1303 for (
int i = 0; i < datasize; i++)
1308 template<
class Num_T>
1312 int i, j, m1_pos = 0, m2_pos = 0, r_pos = 0;
1314 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_cols == m2.no_cols),
1315 "Mat<>::operator+(): Wrong sizes");
1317 for (i = 0; i < r.no_cols; i++) {
1318 for (j = 0; j < r.no_rows; j++)
1319 r.data[r_pos+j] = m1.data[m1_pos+j] + m2.data[m2_pos+j];
1321 m1_pos += m1.no_rows;
1322 m2_pos += m2.no_rows;
1330 template<
class Num_T>
1335 for (
int i = 0; i < r.datasize; i++)
1336 r.data[i] = m.data[i] + t;
1341 template<
class Num_T>
1346 for (
int i = 0; i < r.datasize; i++)
1347 r.data[i] = t + m.data[i];
1352 template<
class Num_T>
1355 int i, j, m_pos = 0, pos = 0;
1357 if (datasize == 0) {
1358 set_size(m.no_rows, m.no_cols,
false);
1359 for (i = 0; i < no_cols; i++) {
1360 for (j = 0; j < no_rows; j++)
1361 data[pos+j] = -m.data[m_pos+j];
1369 "Mat<>::operator-=(): Wrong sizes");
1370 for (i = 0; i < no_cols; i++) {
1371 for (j = 0; j < no_rows; j++)
1372 data[pos+j] -= m.data[m_pos+j];
1381 template<
class Num_T>
1385 int i, j, m1_pos = 0, m2_pos = 0, r_pos = 0;
1386 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_cols == m2.no_cols),
1387 "Mat<>::operator-(): Wrong sizes");
1389 for (i = 0; i < r.no_cols; i++) {
1390 for (j = 0; j < r.no_rows; j++)
1391 r.data[r_pos+j] = m1.data[m1_pos+j] - m2.data[m2_pos+j];
1393 m1_pos += m1.no_rows;
1394 m2_pos += m2.no_rows;
1401 template<
class Num_T>
inline 1404 for (
int i = 0; i < datasize; i++)
1409 template<
class Num_T>
1413 int i, j, m_pos = 0, r_pos = 0;
1415 for (i = 0; i < r.no_cols; i++) {
1416 for (j = 0; j < r.no_rows; j++)
1417 r.data[r_pos+j] = m.data[m_pos+j] - t;
1426 template<
class Num_T>
1430 int i, j, m_pos = 0, r_pos = 0;
1432 for (i = 0; i < r.no_cols; i++) {
1433 for (j = 0; j < r.no_rows; j++)
1434 r.data[r_pos+j] = t - m.data[m_pos+j];
1443 template<
class Num_T>
1447 int i, j, m_pos = 0, r_pos = 0;
1449 for (i = 0; i < r.no_cols; i++) {
1450 for (j = 0; j < r.no_rows; j++)
1451 r.data[r_pos+j] = -m.data[m_pos+j];
1460 template<
class Num_T>
1463 it_assert_debug(no_cols == m.no_rows,
"Mat<>::operator*=(): Wrong sizes");
1468 int i, j, k, r_pos = 0, pos = 0, m_pos = 0;
1470 for (i = 0; i < r.no_cols; i++) {
1471 for (j = 0; j < r.no_rows; j++) {
1474 for (k = 0; k < no_cols; k++) {
1475 tmp += data[pos+j] * m.data[m_pos+k];
1478 r.data[r_pos+j] = tmp;
1488 template<> ITPP_EXPORT
mat& mat::operator*=(
const mat &m);
1489 template<> ITPP_EXPORT
cmat& cmat::operator*=(
const cmat &m);
1492 template<
class Num_T>
inline 1495 scal_vector(datasize, t, data);
1500 template<
class Num_T>
1504 "Mat<>::operator*(): Wrong sizes");
1509 Num_T *tr = r.
_data();
1510 const Num_T *t1;
const Num_T *t2 = m2._data();
1512 for (i = 0; i < r.cols(); i++) {
1513 for (j = 0; j < r.rows(); j++) {
1515 t1 = m1._data() + j;
1516 for (k = m1.cols(); k > 0; k--) {
1517 tmp += *(t1) * *(t2++);
1535 template<
class Num_T>
1539 "Mat<>::operator*(): Wrong sizes");
1543 for (i = 0; i < m.rows(); i++) {
1546 for (k = 0; k < m.cols(); k++) {
1547 r(i) += m._data()[m_pos+i] * v(k);
1561 template<
class Num_T>
1566 const Num_T* m_data = m._data();
1567 Num_T* r_data = r._data();
1568 for (
int i = 0; i < r._datasize(); i++)
1569 r_data[i] = m_data[i] * t;
1575 template<
class Num_T>
inline 1581 template<
class Num_T>
inline 1589 template<
class Num_T>
1593 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_cols == m2.no_cols),
1594 "Mat<>::elem_mult_out(): Wrong sizes");
1595 out.set_size(m1.no_rows, m1.no_cols);
1596 for (
int i = 0; i < out.datasize; i++)
1597 out.data[i] = m1.data[i] * m2.data[i];
1600 template<
class Num_T>
1604 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_rows == m3.no_rows)
1605 && (m1.no_cols == m2.no_cols) && (m1.no_cols == m3.no_cols),
1606 "Mat<>::elem_mult_out(): Wrong sizes");
1607 out.set_size(m1.no_rows, m1.no_cols);
1608 for (
int i = 0; i < out.datasize; i++)
1609 out.data[i] = m1.data[i] * m2.data[i] * m3.data[i];
1612 template<
class Num_T>
1617 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_rows == m3.no_rows)
1618 && (m1.no_rows == m4.no_rows) && (m1.no_cols == m2.no_cols)
1619 && (m1.no_cols == m3.no_cols) && (m1.no_cols == m4.no_cols),
1620 "Mat<>::elem_mult_out(): Wrong sizes");
1621 out.set_size(m1.no_rows, m1.no_cols);
1622 for (
int i = 0; i < out.datasize; i++)
1623 out.data[i] = m1.data[i] * m2.data[i] * m3.data[i] * m4.data[i];
1626 template<
class Num_T>
1632 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_cols == m2.no_cols),
1633 "Mat<>::elem_mult_inplace(): Wrong sizes");
1634 for (
int i = 0; i < m2.datasize; i++)
1635 m2.data[i] *= m1.data[i];
1638 template<
class Num_T>
inline 1641 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_cols == m2.no_cols),
1642 "Mat<>::elem_mult_sum(): Wrong sizes");
1645 for (
int i = 0; i < m1.datasize; i++)
1646 acc += m1.data[i] * m2.data[i];
1651 template<
class Num_T>
inline 1654 for (
int i = 0; i < datasize; i++)
1659 template<
class Num_T>
inline 1663 "Mat<>::operator/=(): Wrong sizes");
1664 for (
int i = 0; i < datasize; i++)
1665 data[i] /= m.data[i];
1669 template<
class Num_T>
1673 for (
int i = 0; i < r.datasize; ++i)
1674 r.data[i] = m.data[i] / t;
1678 template<
class Num_T>
1682 for (
int i = 0; i < r.datasize; ++i)
1683 r.data[i] = t / m.data[i];
1687 template<
class Num_T>
inline 1695 template<
class Num_T>
1699 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_cols == m2.no_cols),
1700 "Mat<>::elem_div_out(): Wrong sizes");
1702 if ((out.no_rows != m1.no_rows) || (out.no_cols != m1.no_cols))
1703 out.set_size(m1.no_rows, m1.no_cols);
1705 for (
int i = 0; i < out.datasize; i++)
1706 out.data[i] = m1.data[i] / m2.data[i];
1709 template<
class Num_T>
inline 1712 it_assert_debug((m1.no_rows == m2.no_rows) && (m1.no_cols == m2.no_cols),
1713 "Mat<>::elem_div_sum(): Wrong sizes");
1716 for (
int i = 0; i < m1.datasize; i++)
1717 acc += m1.data[i] / m2.data[i];
1722 template<
class Num_T>
1725 if (no_rows != m.no_rows || no_cols != m.no_cols)
return false;
1726 for (
int i = 0;i < datasize;i++) {
1727 if (data[i] != m.data[i])
return false;
1732 template<
class Num_T>
1735 if (no_rows != m.no_rows || no_cols != m.no_cols)
return true;
1736 for (
int i = 0;i < datasize;i++) {
1737 if (data[i] != m.data[i])
return true;
1742 template <
class Num_T>
1743 std::ostream &operator<<(std::ostream &os, const Mat<Num_T> &m)
1752 os <<
'[' << m.get_row(0) <<
']';
1755 os <<
'[' << m.get_row(0) << std::endl;
1756 for (i = 1; i < m.rows() - 1; i++)
1757 os <<
' ' << m.get_row(i) << std::endl;
1758 os <<
' ' << m.get_row(m.rows() - 1) <<
']';
1764 template <
class Num_T>
1767 std::ostringstream buffer;
1768 bool started =
false;
1769 bool finished =
false;
1770 bool brackets =
false;
1771 bool within_double_brackets =
false;
1781 if (is.eof() || (c ==
'\n')) {
1784 is.setstate(std::ios_base::failbit);
1787 else if (!((c ==
'\n') && !started)) {
1791 else if ((c ==
' ') || (c ==
'\t')) {
1796 else if (c ==
'[') {
1797 if ((started && !brackets) || within_double_brackets) {
1799 is.setstate(std::ios_base::failbit);
1802 else if (!started) {
1807 within_double_brackets =
true;
1810 else if (c ==
']') {
1811 if (!started || !brackets) {
1813 is.setstate(std::ios_base::failbit);
1816 else if (within_double_brackets) {
1817 within_double_brackets =
false;
1823 while (!is.eof() && (((c =
static_cast<char>(is.peek())) ==
' ')
1827 if (!is.eof() && (c ==
'\n')) {
1839 m.set_size(0,
false);
1842 m.set(buffer.str());
1856 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Mat<double>;
1857 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Mat<std::complex<double> >;
1858 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Mat<int>;
1859 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Mat<short int>;
1860 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Mat<bin>;
1864 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator+(
const mat &m1,
const mat &m2);
1870 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator+(
const mat &m,
double t);
1871 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator+(
const cmat &m, std::complex<double> t);
1873 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator+(
const smat &m,
short t);
1876 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator+(
double t,
const mat &m);
1877 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator+(std::complex<double> t,
const cmat &m);
1879 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator+(
short t,
const smat &m);
1884 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator-(
const mat &m1,
const mat &m2);
1890 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator-(
const mat &m,
double t);
1891 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator-(
const cmat &m, std::complex<double> t);
1893 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator-(
const smat &m,
short t);
1896 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator-(
double t,
const mat &m);
1897 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator-(std::complex<double> t,
const cmat &m);
1899 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator-(
short t,
const smat &m);
1904 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator-(
const mat &m);
1920 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator*(
const mat &m,
double t);
1921 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator*(
const cmat &m, std::complex<double> t);
1923 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator*(
const smat &m,
short t);
1926 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator*(
double t,
const mat &m);
1927 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator*(std::complex<double> t,
const cmat &m);
1929 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator*(
short t,
const smat &m);
1934 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat elem_mult(
const mat &m1,
const mat &m2);
1950 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2,
1951 const mat &m3,
mat &out);
1961 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2,
1962 const mat &m3,
const mat &m4,
mat &out);
1982 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
double elem_mult_sum(
const mat &m1,
const mat &m2);
1983 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::complex<double>
elem_mult_sum(
const cmat &m1,
1991 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator/(
double t,
const mat &m);
1992 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator/(std::complex<double> t,
const cmat &m);
1994 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator/(
short t,
const smat &m);
1997 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat operator/(
const mat &m,
double t);
1998 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
cmat operator/(
const cmat &m, std::complex<double> t);
2000 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
smat operator/(
const smat &m,
short t);
2005 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
mat elem_div(
const mat &m1,
const mat &m2);
2011 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
void elem_div_out(
const mat &m1,
const mat &m2,
mat &out);
2017 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
double elem_div_sum(
const mat &m1,
const mat &m2);
2018 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::complex<double>
elem_div_sum(
const cmat &m1,
2040 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const mat &m);
2041 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const cmat &m);
2042 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const imat &m);
2043 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const smat &m);
2044 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const bmat &m);
2046 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::istream &
operator>>(std::istream &is,
mat &m);
2047 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::istream &
operator>>(std::istream &is,
cmat &m);
2048 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::istream &
operator>>(std::istream &is,
imat &m);
2049 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::istream &
operator>>(std::istream &is,
smat &m);
2050 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::istream &
operator>>(std::istream &is,
bmat &m);
2056 #endif // #ifndef MAT_H int size() const
The size of the vector.
void del_cols(int c1, int c2)
Delete columns from c1 to c2.
void set_submatrix(int r1, int r2, int c1, int c2, const Mat< Num_T > &m)
This function is deprecated. Please use set_submatrix(int r, int c, const Mat<> &m) instead...
Mat< Num_T > elem_div(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise division of two matrices.
std::ostream & operator<<(std::ostream &os, const Array< T > &a)
Output stream for Array<T>. T must have ostream operator<< defined.
void alloc(int rows, int cols)
Allocate memory for the matrix.
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
Mat< Num_T > operator/(const Mat< Num_T > &m, Num_T t)
Element-wise division by a scalar.
Mat< double > mat
Default Matrix Type.
Num_T * data
Protected data pointer.
Mat< Num_T > concat_horizontal(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Horizontal concatenation of two matrices.
Miscellaneous functions - header file.
void append_row(const Vec< Num_T > &v)
Append vector v to the bottom of the matrix. The matrix can be empty.
std::istream & operator>>(std::istream &is, Array< T > &a)
Input stream for Array<T>. T must have istream operator>> defined.
Vec< Num_T > get_row(int r) const
Get row r.
Num_T * _data()
Get the pointer to the internal structure. Not recommended for use.
const Num_T & operator()(int r, int c) const
Get element (r,c) from matrix.
void elem_div_out(const Mat< Num_T > &m1, const Mat< Num_T > &m2, Mat< Num_T > &out)
Element wise division of two matrices, storing the result in matrix out.
void create_elements(T *&ptr, int n, const Factory &)
Create an n-length array of T to be used as Array, Vec or Mat elements.
bool operator==(const Mat< Num_T > &m) const
Compare two matrices. False if wrong sizes or different values.
Mat< Num_T > get_rows(int r1, int r2) const
Get rows r1 through r2.
Vec< std::complex< double > > cvec
Definition of complex<double> vector type.
Mat< Num_T > & operator=(Num_T t)
Set all elements of the matrix equal to t.
void destroy_elements(T *&ptr, int n)
Destroy an array of Array, Vec or Mat elements.
Vec< short int > svec
Definition of short vector type.
Mat< Num_T > elem_mult(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise multiplication of two matrices.
ITPP_EXPORT GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
vec operator/(const vec &v, const float &s)
Division operator for vec and float.
Num_T elem_div_sum(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise division of two matrices, followed by summation of the resultant elements. Fast version of sumsum(elem_div(A, B)).
Vec< bin > bvec
Definition of binary vector type.
void append_col(const Vec< Num_T > &v)
Append vector v to the right side of the matrix. The matrix can be empty.
const Num_T & _elem(int i) const
Get element i using linear addressing (by rows) without boundary check (not recommended to use) ...
vec operator-(const float &s, const vec &v)
Subtraction operator for float and vec.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Mat< Num_T > & operator-=(const Mat< Num_T > &m)
Subtraction of matrix.
Base class for class factories.
void set_col(int c, const Vec< Num_T > &v)
Set column c to vector v.
Mat< Num_T > & operator+=(const Mat< Num_T > &m)
Addition of matrices.
Mat< short int > smat
short int matrix
void elem_mult_out(const Mat< Num_T > &m1, const Mat< Num_T > &m2, const Mat< Num_T > &m3, const Mat< Num_T > &m4, Mat< Num_T > &out)
Element wise multiplication of four matrices, storing the result in matrix out.
ITPP_EXPORT vec zeros(int size)
A Double vector of zeros.
int cols() const
The number of columns.
Num_T & _elem(int r, int c)
Get element (r,c) from matrix without boundary check (not recommended to use)
void ins_row(int r, const Vec< Num_T > &v)
Insert vector v at row number r. The matrix can be empty.
Mat< Num_T > operator-(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Subtraction of two matrices.
void ones()
Set matrix equal to the all one matrix.
void ins_col(int c, const Vec< Num_T > &v)
Insert vector v at column number c. The matrix can be empty.
void clear()
Set matrix equal to the all zero matrix.
void swap_cols(int c1, int c2)
Swap the columns c1 and c2.
int _datasize() const
Access to the internal data structure (not recommended to use)
Mat< Num_T > hermitian_transpose() const
Hermitian matrix transpose (conjugate transpose)
Mat< std::complex< double > > cmat
Default Complex Matrix Type.
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
const Factory DEFAULT_FACTORY
Default (dummy) factory.
Vec< Num_T > get_col(int c) const
Get column c.
void set_row(int r, const Vec< Num_T > &v)
Set row r to vector v.
ITPP_EXPORT GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
int size(const Vec< T > &v)
Length of vector.
Base class for class factories and memory allocation functions.
void set_rows(int r, const Mat< Num_T > &m)
Set rows to matrix m, staring from row r.
Mat< Num_T > H() const
Hermitian matrix transpose (conjugate transpose)
void swap_rows(int r1, int r2)
Swap the rows r1 and r2.
Mat< Num_T > get_cols(int c1, int c2) const
Get columns c1 through c2.
void elem_mult_out(const Mat< Num_T > &m1, const Mat< Num_T > &m2, Mat< Num_T > &out)
Element wise multiplication of two matrices, storing the result in matrix out.
ITPP_EXPORT vec ones(int size)
A float vector of ones.
void del_row(int r)
Delete row number r.
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
bool operator!=(const Mat< Num_T > &m) const
Compare two matrices. True if different.
Mat< Num_T > concat_vertical(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Vertical concatenation of two matrices.
Error handling functions - header file.
void set_cols(int c, const Mat< Num_T > &m)
Set columns to matrix m, starting from column c.
void zeros()
Set matrix equal to the all zero matrix.
void elem_mult_inplace(const Mat< Num_T > &m1, Mat< Num_T > &m2)
In-place element wise multiplication of two matrices. Fast version of B = elem_mult(A, B).
Num_T * _data()
Access of the internal data structure (not recommended to use)
void hermitian_transpose(const Mat< T > &m, Mat< T > &out)
int size() const
The number of elements.
Num_T & _elem(int i)
Get element i using linear addressing (by rows) without boundary check (not recommended to use) ...
#define it_warning(s)
Display a warning message.
Mat(const Factory &f=DEFAULT_FACTORY)
Default constructor. An element factory f can be specified.
const Num_T & _elem(int r, int c) const
Get element (r,c) from matrix without boundary check (not recommended to use)
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
Mat< int > imat
Integer matrix.
Mat< Num_T > & operator*=(const Mat< Num_T > &m)
Matrix multiplication.
int rows() const
The number of rows.
const Num_T * _data() const
Access to the internal data structure (not recommended to use)
Vec< int > ivec
Definition of integer vector type.
T to(double x)
Convert double to T.
const Factory & factory
Element factory (set to DEFAULT_FACTORY to use Num_T default constructors only)
Num_T value_type
The type of the matrix values.
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
void del_rows(int r1, int r2)
Delete rows from r1 to r2.
Num_T elem_mult_sum(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise multiplication of two matrices, followed by summation of the resultant elements...
void copy_col(int to, int from)
Copy column from onto column to.
Mat< Num_T > transpose() const
Matrix transpose.
Mat< Num_T > & operator/=(Num_T t)
Division by a scalar.
void copy_row(int to, int from)
Copy row from onto row to.
Mat< Num_T > T() const
Matrix transpose.
Vec< double > vec
Definition of double vector type.
void del_col(int c)
Delete column number c.
Mat< bin > bmat
bin matrix
Templated Vector Class Definitions.
void free()
Free the memory space of the matrix.