IT++ Logo
matfunc.h
Go to the documentation of this file.
1 
30 #ifndef MATFUNC_H
31 #define MATFUNC_H
32 
33 #include <itpp/base/mat.h>
34 #include <itpp/base/math/log_exp.h>
36 #include <itpp/base/algebra/inv.h>
37 #include <itpp/base/algebra/svd.h>
38 #include <itpp/itexports.h>
39 
40 namespace itpp
41 {
42 
47 
50 template<class T>
51 int length(const Vec<T> &v) { return v.length(); }
52 
54 template<class T>
55 int size(const Vec<T> &v) { return v.length(); }
56 
58 template<class T>
59 T sum(const Vec<T> &v)
60 {
61  T M = 0;
62 
63  for (int i = 0;i < v.length();i++)
64  M += v[i];
65 
66  return M;
67 }
68 
76 template<class T>
77 Vec<T> sum(const Mat<T> &m, int dim = 1)
78 {
79  it_assert((dim == 1) || (dim == 2), "sum: dimension need to be 1 or 2");
80  Vec<T> out;
81 
82  if (dim == 1) {
83  out.set_size(m.cols(), false);
84 
85  for (int i = 0; i < m.cols(); i++)
86  out(i) = sum(m.get_col(i));
87  }
88  else {
89  out.set_size(m.rows(), false);
90 
91  for (int i = 0; i < m.rows(); i++)
92  out(i) = sum(m.get_row(i));
93  }
94 
95  return out;
96 }
97 
98 
100 template<class T>
101 T sumsum(const Mat<T> &X)
102 {
103  const T * X_data = X._data();
104  const int X_datasize = X._datasize();
105  T acc = 0;
106 
107  for (int i = 0;i < X_datasize;i++)
108  acc += X_data[i];
109 
110  return acc;
111 }
112 
113 
115 template<class T>
116 T sum_sqr(const Vec<T> &v)
117 {
118  T M = 0;
119 
120  for (int i = 0; i < v.length(); i++)
121  M += v[i] * v[i];
122 
123  return M;
124 }
125 
133 template<class T>
134 Vec<T> sum_sqr(const Mat<T> &m, int dim = 1)
135 {
136  it_assert((dim == 1) || (dim == 2), "sum_sqr: dimension need to be 1 or 2");
137  Vec<T> out;
138 
139  if (dim == 1) {
140  out.set_size(m.cols(), false);
141 
142  for (int i = 0; i < m.cols(); i++)
143  out(i) = sum_sqr(m.get_col(i));
144  }
145  else {
146  out.set_size(m.rows(), false);
147 
148  for (int i = 0; i < m.rows(); i++)
149  out(i) = sum_sqr(m.get_row(i));
150  }
151 
152  return out;
153 }
154 
156 template<class T>
158 {
159  Vec<T> out(v.size());
160 
161  out(0) = v(0);
162  for (int i = 1; i < v.size(); i++)
163  out(i) = out(i - 1) + v(i);
164 
165  return out;
166 }
167 
175 template<class T>
176 Mat<T> cumsum(const Mat<T> &m, int dim = 1)
177 {
178  it_assert((dim == 1) || (dim == 2), "cumsum: dimension need to be 1 or 2");
179  Mat<T> out(m.rows(), m.cols());
180 
181  if (dim == 1) {
182  for (int i = 0; i < m.cols(); i++)
183  out.set_col(i, cumsum(m.get_col(i)));
184  }
185  else {
186  for (int i = 0; i < m.rows(); i++)
187  out.set_row(i, cumsum(m.get_row(i)));
188  }
189 
190  return out;
191 }
192 
194 template<class T>
195 T prod(const Vec<T> &v)
196 {
197  it_assert(v.size() >= 1, "prod: size of vector should be at least 1");
198  T out = v(0);
199 
200  for (int i = 1; i < v.size(); i++)
201  out *= v(i);
202 
203  return out;
204 }
205 
213 template<class T>
214 Vec<T> prod(const Mat<T> &m, int dim = 1)
215 {
216  it_assert((dim == 1) || (dim == 2), "prod: dimension need to be 1 or 2");
217  Vec<T> out(m.cols());
218 
219  if (dim == 1) {
220  it_assert((m.cols() >= 1) && (m.rows() >= 1),
221  "prod: number of columns should be at least 1");
222  out.set_size(m.cols(), false);
223 
224  for (int i = 0; i < m.cols(); i++)
225  out(i) = prod(m.get_col(i));
226  }
227  else {
228  it_assert((m.cols() >= 1) && (m.rows() >= 1),
229  "prod: number of rows should be at least 1");
230  out.set_size(m.rows(), false);
231 
232  for (int i = 0; i < m.rows(); i++)
233  out(i) = prod(m.get_row(i));
234  }
235  return out;
236 }
237 
239 template<class T>
240 Vec<T> cross(const Vec<T> &v1, const Vec<T> &v2)
241 {
242  it_assert((v1.size() == 3) && (v2.size() == 3),
243  "cross: vectors should be of size 3");
244 
245  Vec<T> r(3);
246 
247  r(0) = v1(1) * v2(2) - v1(2) * v2(1);
248  r(1) = v1(2) * v2(0) - v1(0) * v2(2);
249  r(2) = v1(0) * v2(1) - v1(1) * v2(0);
250 
251  return r;
252 }
253 
254 
256 template<class T>
257 Vec<T> zero_pad(const Vec<T> &v, int n)
258 {
259  it_assert(n >= v.size(), "zero_pad() cannot shrink the vector!");
260  Vec<T> v2(n);
261  v2.set_subvector(0, v);
262  if (n > v.size())
263  v2.set_subvector(v.size(), n - 1, T(0));
264 
265  return v2;
266 }
267 
269 template<class T>
271 {
272  int n = pow2i(levels2bits(v.size()));
273 
274  return (n == v.size()) ? v : zero_pad(v, n);
275 }
276 
278 template<class T>
279 Mat<T> zero_pad(const Mat<T> &m, int rows, int cols)
280 {
281  it_assert((rows >= m.rows()) && (cols >= m.cols()),
282  "zero_pad() cannot shrink the matrix!");
283  Mat<T> m2(rows, cols);
284  m2.set_submatrix(0, 0, m);
285  if (cols > m.cols()) // Zero
286  m2.set_submatrix(0, m.rows() - 1, m.cols(), cols - 1, T(0));
287  if (rows > m.rows()) // Zero
288  m2.set_submatrix(m.rows(), rows - 1, 0, cols - 1, T(0));
289 
290  return m2;
291 }
292 
293 
296 template<class T>
297 T index_zero_pad(const Vec<T> &v, const int index)
298 {
299  if (index >= 0 && index < v.size())
300  return v(index);
301  else
302  return T(0);
303 }
304 
305 
307 template<class T>
308 void transpose(const Mat<T> &m, Mat<T> &out) { out = m.T(); }
309 
311 template<class T>
312 Mat<T> transpose(const Mat<T> &m) { return m.T(); }
313 
314 
317 template<class T>
318 void hermitian_transpose(const Mat<T> &m, Mat<T> &out) { out = m.H(); }
319 
321 template<class T>
322 Mat<T> hermitian_transpose(const Mat<T> &m) { return m.H(); }
323 
324 
325 
335 template<class Num_T>
336 bool is_hermitian(const Mat<Num_T>& X)
337 {
338 
339  if (X == X.H())
340  return true;
341  else
342  return false;
343 }
344 
354 template<class Num_T>
355 bool is_unitary(const Mat<Num_T>& X)
356 {
357 
358  if (inv(X) == X.H())
359  return true;
360  else
361  return false;
362 }
363 
364 
373 template<class T>
374 Vec<T> repmat(const Vec<T> &v, int n)
375 {
376  it_assert(n > 0, "repmat(): Wrong repetition parameter");
377  int data_length = v.length();
378  it_assert(data_length > 0, "repmat(): Input vector can not be empty");
379  Vec<T> assembly(data_length * n);
380  for (int j = 0; j < n; ++j) {
381  assembly.set_subvector(j * data_length, v);
382  }
383  return assembly;
384 }
385 
386 
396 template<class T>
397 Mat<T> repmat(const Mat<T> &data, int m, int n)
398 {
399  it_assert((m > 0) && (n > 0), "repmat(): Wrong repetition parameters");
400  int data_rows = data.rows();
401  int data_cols = data.cols();
402  it_assert((data_rows > 0) && (data_cols > 0), "repmat(): Input matrix can "
403  "not be empty");
404  Mat<T> assembly(data_rows*m, data_cols*n);
405  for (int i = 0; i < m; ++i) {
406  for (int j = 0; j < n; ++j) {
407  assembly.set_submatrix(i*data_rows, j*data_cols, data);
408  }
409  }
410  return assembly;
411 }
412 
424 template<class T> inline
425 Mat<T> repmat(const Vec<T> &v, int m, int n, bool transpose = false)
426 {
427  return repmat((transpose ? v.T() : Mat<T>(v)), m, n);
428 }
429 
430 
442 template<class Num_T>
444 {
445  Mat<Num_T> result(X.rows() * Y.rows(), X.cols() * Y.cols());
446 
447  for (int i = 0; i < X.rows(); i++)
448  for (int j = 0; j < X.cols(); j++)
449  result.set_submatrix(i * Y.rows(), j * Y.cols(), X(i, j) * Y);
450 
451  return result;
452 }
453 
454 
467 ITPP_EXPORT cmat sqrtm(const cmat& A);
468 
481 ITPP_EXPORT cmat sqrtm(const mat& A);
482 
483 
492 template<class T>
493 int rank(const Mat<T> &m, double tol = -1.0)
494 {
495  int rows = m.rows();
496  int cols = m.cols();
497  if ((rows == 0) || (cols == 0))
498  return 0;
499 
500  vec sing_val = svd(m);
501 
502  if (tol < 0.0) { // Calculate default tolerance
503  tol = eps * sing_val(0) * (rows > cols ? rows : cols);
504  }
505 
506  // Count number of nonzero singular values
507  int r = 0;
508  while ((r < sing_val.length()) && (sing_val(r) > tol)) {
509  r++;
510  }
511 
512  return r;
513 }
514 
516 template<> inline
517 int rank(const imat &m, double tol)
518 {
519  return rank(to_mat(m), tol);
520 }
521 
523 template<> inline
524 int rank(const smat &m, double tol)
525 {
526  return rank(to_mat(m), tol);
527 }
528 
530 template<> inline
531 int rank(const bmat &, double)
532 {
533  it_error("rank(bmat): Function not implemented for GF(2) algebra");
534  return 0;
535 }
536 
538 
539 
540 
541 // -------------------- Diagonal matrix functions -------------------------
542 
545 
556 template<class T>
557 Mat<T> diag(const Vec<T> &v, const int K = 0)
558 {
559  Mat<T> m(v.size() + std::abs(K), v.size() + std::abs(K));
560  m = T(0);
561  if (K > 0)
562  for (int i = v.size() - 1; i >= 0; i--)
563  m(i, i + K) = v(i);
564  else
565  for (int i = v.size() - 1; i >= 0; i--)
566  m(i - K, i) = v(i);
567 
568  return m;
569 }
570 
580 template<class T>
581 void diag(const Vec<T> &v, Mat<T> &m)
582 {
583  m.set_size(v.size(), v.size(), false);
584  m = T(0);
585  for (int i = v.size() - 1; i >= 0; i--)
586  m(i, i) = v(i);
587 }
588 
596 template<class T>
597 Vec<T> diag(const Mat<T> &m)
598 {
599  Vec<T> t(std::min(m.rows(), m.cols()));
600 
601  for (int i = 0; i < t.size(); i++)
602  t(i) = m(i, i);
603 
604  return t;
605 }
606 
616 template<class T>
617 Mat<T> bidiag(const Vec<T> &main, const Vec<T> &sup)
618 {
619  it_assert(main.size() == sup.size() + 1, "bidiag()");
620 
621  int n = main.size();
622  Mat<T> m(n, n);
623  m = T(0);
624  for (int i = 0; i < n - 1; i++) {
625  m(i, i) = main(i);
626  m(i, i + 1) = sup(i);
627  }
628  m(n - 1, n - 1) = main(n - 1);
629 
630  return m;
631 }
632 
642 template<class T>
643 void bidiag(const Vec<T> &main, const Vec<T> &sup, Mat<T> &m)
644 {
645  it_assert(main.size() == sup.size() + 1, "bidiag()");
646 
647  int n = main.size();
648  m.set_size(n, n);
649  m = T(0);
650  for (int i = 0; i < n - 1; i++) {
651  m(i, i) = main(i);
652  m(i, i + 1) = sup(i);
653  }
654  m(n - 1, n - 1) = main(n - 1);
655 }
656 
665 template<class T>
666 void bidiag(const Mat<T> &m, Vec<T> &main, Vec<T> &sup)
667 {
668  it_assert(m.rows() == m.cols(), "bidiag(): Matrix must be square!");
669 
670  int n = m.cols();
671  main.set_size(n);
672  sup.set_size(n - 1);
673  for (int i = 0; i < n - 1; i++) {
674  main(i) = m(i, i);
675  sup(i) = m(i, i + 1);
676  }
677  main(n - 1) = m(n - 1, n - 1);
678 }
679 
689 template<class T>
690 Mat<T> tridiag(const Vec<T> &main, const Vec<T> &sup, const Vec<T> &sub)
691 {
692  it_assert(main.size() == sup.size() + 1 && main.size() == sub.size() + 1, "bidiag()");
693 
694  int n = main.size();
695  Mat<T> m(n, n);
696  m = T(0);
697  for (int i = 0; i < n - 1; i++) {
698  m(i, i) = main(i);
699  m(i, i + 1) = sup(i);
700  m(i + 1, i) = sub(i);
701  }
702  m(n - 1, n - 1) = main(n - 1);
703 
704  return m;
705 }
706 
716 template<class T>
717 void tridiag(const Vec<T> &main, const Vec<T> &sup, const Vec<T> &sub, Mat<T> &m)
718 {
719  it_assert(main.size() == sup.size() + 1 && main.size() == sub.size() + 1, "bidiag()");
720 
721  int n = main.size();
722  m.set_size(n, n);
723  m = T(0);
724  for (int i = 0; i < n - 1; i++) {
725  m(i, i) = main(i);
726  m(i, i + 1) = sup(i);
727  m(i + 1, i) = sub(i);
728  }
729  m(n - 1, n - 1) = main(n - 1);
730 }
731 
740 template<class T>
741 void tridiag(const Mat<T> &m, Vec<T> &main, Vec<T> &sup, Vec<T> &sub)
742 {
743  it_assert(m.rows() == m.cols(), "tridiag(): Matrix must be square!");
744 
745  int n = m.cols();
746  main.set_size(n);
747  sup.set_size(n - 1);
748  sub.set_size(n - 1);
749  for (int i = 0; i < n - 1; i++) {
750  main(i) = m(i, i);
751  sup(i) = m(i, i + 1);
752  sub(i) = m(i + 1, i);
753  }
754  main(n - 1) = m(n - 1, n - 1);
755 }
756 
757 
761 template<class T>
762 T trace(const Mat<T> &m)
763 {
764  return sum(diag(m));
765 }
766 
768 
769 
770 // ----------------- reshaping vectors and matrices ------------------------
771 
774 
776 template<class T>
778 {
779  int i, s = in.length();
780 
781  Vec<T> out(s);
782  for (i = 0;i < s;i++)
783  out[i] = in[s-1-i];
784  return out;
785 }
786 
788 template<class T>
790 {
791  int i, j, n = 0, r = m.rows(), c = m.cols();
792  Vec<T> v(r * c);
793 
794  for (i = 0; i < r; i++)
795  for (j = 0; j < c; j++)
796  v(n++) = m(i, j);
797 
798  return v;
799 }
800 
802 template<class T>
804 {
805  int i, j, n = 0, r = m.rows(), c = m.cols();
806  Vec<T> v(r * c);
807 
808  for (j = 0; j < c; j++)
809  for (i = 0; i < r; i++)
810  v(n++) = m(i, j);
811 
812  return v;
813 }
814 
821 template<class T>
822 Mat<T> reshape(const Mat<T> &m, int rows, int cols)
823 {
824  it_assert_debug(m.rows()*m.cols() == rows*cols, "Mat<T>::reshape: Sizes must match");
825  Mat<T> temp(rows, cols);
826  int i, j, ii = 0, jj = 0;
827  for (j = 0; j < m.cols(); j++) {
828  for (i = 0; i < m.rows(); i++) {
829  temp(ii++, jj) = m(i, j);
830  if (ii == rows) {
831  jj++;
832  ii = 0;
833  }
834  }
835  }
836  return temp;
837 }
838 
845 template<class T>
846 Mat<T> reshape(const Vec<T> &v, int rows, int cols)
847 {
848  it_assert_debug(v.size() == rows*cols, "Mat<T>::reshape: Sizes must match");
849  Mat<T> temp(rows, cols);
850  int i, j, ii = 0;
851  for (j = 0; j < cols; j++) {
852  for (i = 0; i < rows; i++) {
853  temp(i, j) = v(ii++);
854  }
855  }
856  return temp;
857 }
858 
860 
861 
863 ITPP_EXPORT bool all(const bvec &testvec);
865 ITPP_EXPORT bool any(const bvec &testvec);
866 
868 
869 // ----------------------------------------------------------------------
870 // Instantiations
871 // ----------------------------------------------------------------------
872 
873 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const vec &v);
874 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const cvec &v);
875 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const svec &v);
876 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const ivec &v);
877 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const bvec &v);
878 
879 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double sum(const vec &v);
880 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> sum(const cvec &v);
881 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short sum(const svec &v);
882 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int sum(const ivec &v);
883 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin sum(const bvec &v);
884 
885 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double sum_sqr(const vec &v);
886 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> sum_sqr(const cvec &v);
887 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short sum_sqr(const svec &v);
888 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int sum_sqr(const ivec &v);
889 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin sum_sqr(const bvec &v);
890 
891 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec cumsum(const vec &v);
892 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec cumsum(const cvec &v);
893 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec cumsum(const svec &v);
894 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec cumsum(const ivec &v);
895 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec cumsum(const bvec &v);
896 
897 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double prod(const vec &v);
898 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> prod(const cvec &v);
899 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short prod(const svec &v);
900 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int prod(const ivec &v);
901 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin prod(const bvec &v);
902 
903 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec cross(const vec &v1, const vec &v2);
904 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec cross(const cvec &v1, const cvec &v2);
905 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec cross(const ivec &v1, const ivec &v2);
906 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec cross(const svec &v1, const svec &v2);
907 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec cross(const bvec &v1, const bvec &v2);
908 
909 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec reverse(const vec &in);
910 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec reverse(const cvec &in);
911 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec reverse(const svec &in);
912 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec reverse(const ivec &in);
913 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec reverse(const bvec &in);
914 
915 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec zero_pad(const vec &v, int n);
916 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec zero_pad(const cvec &v, int n);
917 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec zero_pad(const ivec &v, int n);
918 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec zero_pad(const svec &v, int n);
919 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec zero_pad(const bvec &v, int n);
920 
921 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec zero_pad(const vec &v);
922 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec zero_pad(const cvec &v);
923 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec zero_pad(const ivec &v);
924 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec zero_pad(const svec &v);
925 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec zero_pad(const bvec &v);
926 
927 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat zero_pad(const mat &, int, int);
928 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat zero_pad(const cmat &, int, int);
929 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat zero_pad(const imat &, int, int);
930 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat zero_pad(const smat &, int, int);
931 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat zero_pad(const bmat &, int, int);
932 
933 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec sum(const mat &m, int dim);
934 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec sum(const cmat &m, int dim);
935 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec sum(const smat &m, int dim);
936 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec sum(const imat &m, int dim);
937 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec sum(const bmat &m, int dim);
938 
939 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double sumsum(const mat &X);
940 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> sumsum(const cmat &X);
941 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short sumsum(const smat &X);
942 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int sumsum(const imat &X);
943 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin sumsum(const bmat &X);
944 
945 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec sum_sqr(const mat & m, int dim);
946 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec sum_sqr(const cmat &m, int dim);
947 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec sum_sqr(const smat &m, int dim);
948 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec sum_sqr(const imat &m, int dim);
949 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec sum_sqr(const bmat &m, int dim);
950 
951 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat cumsum(const mat &m, int dim);
952 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat cumsum(const cmat &m, int dim);
953 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat cumsum(const smat &m, int dim);
954 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat cumsum(const imat &m, int dim);
955 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat cumsum(const bmat &m, int dim);
956 
957 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec prod(const mat &m, int dim);
958 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec prod(const cmat &v, int dim);
959 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec prod(const smat &m, int dim);
960 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec prod(const imat &m, int dim);
961 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec prod(const bmat &m, int dim);
962 
963 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec diag(const mat &in);
964 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec diag(const cmat &in);
965 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void diag(const vec &in, mat &m);
966 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void diag(const cvec &in, cmat &m);
967 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat diag(const vec &v, const int K);
968 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat diag(const cvec &v, const int K);
969 
970 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat bidiag(const vec &, const vec &);
971 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat bidiag(const cvec &, const cvec &);
972 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const vec &, const vec &, mat &);
973 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const cvec &, const cvec &, cmat &);
974 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const mat &, vec &, vec &);
975 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const cmat &, cvec &, cvec &);
976 
977 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat tridiag(const vec &main, const vec &, const vec &);
978 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat tridiag(const cvec &main, const cvec &, const cvec &);
979 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const vec &main, const vec &, const vec &, mat &);
980 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const cvec &main, const cvec &, const cvec &, cmat &);
981 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const mat &m, vec &, vec &, vec &);
982 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const cmat &m, cvec &, cvec &, cvec &);
983 
984 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double trace(const mat &in);
985 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> trace(const cmat &in);
986 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short trace(const smat &in);
987 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int trace(const imat &in);
988 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin trace(const bmat &in);
989 
990 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const mat &m, mat &out);
991 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const cmat &m, cmat &out);
992 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const smat &m, smat &out);
993 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const imat &m, imat &out);
994 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const bmat &m, bmat &out);
995 
996 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat transpose(const mat &m);
997 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat transpose(const cmat &m);
998 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat transpose(const smat &m);
999 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat transpose(const imat &m);
1000 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat transpose(const bmat &m);
1001 
1002 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const mat &m, mat &out);
1003 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const cmat &m, cmat &out);
1004 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const smat &m, smat &out);
1005 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const imat &m, imat &out);
1006 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const bmat &m, bmat &out);
1007 
1008 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat hermitian_transpose(const mat &m);
1009 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat hermitian_transpose(const cmat &m);
1010 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat hermitian_transpose(const smat &m);
1011 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat hermitian_transpose(const imat &m);
1012 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat hermitian_transpose(const bmat &m);
1013 
1014 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_hermitian(const mat &X);
1015 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_hermitian(const cmat &X);
1016 
1017 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_unitary(const mat &X);
1018 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_unitary(const cmat &X);
1019 
1020 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec rvectorize(const mat &m);
1021 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec rvectorize(const cmat &m);
1022 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec rvectorize(const imat &m);
1023 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec rvectorize(const smat &m);
1024 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec rvectorize(const bmat &m);
1025 
1026 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec cvectorize(const mat &m);
1027 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec cvectorize(const cmat &m);
1028 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec cvectorize(const imat &m);
1029 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec cvectorize(const smat &m);
1030 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec cvectorize(const bmat &m);
1031 
1032 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat reshape(const mat &m, int rows, int cols);
1033 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat reshape(const cmat &m, int rows, int cols);
1034 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat reshape(const imat &m, int rows, int cols);
1035 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat reshape(const smat &m, int rows, int cols);
1036 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat reshape(const bmat &m, int rows, int cols);
1037 
1038 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat reshape(const vec &m, int rows, int cols);
1039 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat reshape(const cvec &m, int rows, int cols);
1040 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat reshape(const ivec &m, int rows, int cols);
1041 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat reshape(const svec &m, int rows, int cols);
1042 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat reshape(const bvec &m, int rows, int cols);
1043 
1044 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat kron(const mat &X, const mat &Y);
1045 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat kron(const cmat &X, const cmat &Y);
1046 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat kron(const imat &X, const imat &Y);
1047 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat kron(const smat &X, const smat &Y);
1048 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat kron(const bmat &X, const bmat &Y);
1049 
1050 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec repmat(const vec &v, int n);
1051 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec repmat(const cvec &v, int n);
1052 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec repmat(const ivec &v, int n);
1053 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec repmat(const svec &v, int n);
1054 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec repmat(const bvec &v, int n);
1055 
1056 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat repmat(const vec &v, int m, int n, bool transpose);
1057 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat repmat(const cvec &v, int m, int n, bool transpose);
1058 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat repmat(const ivec &v, int m, int n, bool transpose);
1059 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat repmat(const svec &v, int m, int n, bool transpose);
1060 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat repmat(const bvec &v, int m, int n, bool transpose);
1061 
1062 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat repmat(const mat &data, int m, int n);
1063 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat repmat(const cmat &data, int m, int n);
1064 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat repmat(const imat &data, int m, int n);
1065 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat repmat(const smat &data, int m, int n);
1066 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat repmat(const bmat &data, int m, int n);
1067 
1069 
1070 } // namespace itpp
1071 
1072 #endif // #ifndef MATFUNC_H
int size() const
The size of the vector.
Definition: vec.h:271
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...
Definition: mat.h:1019
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
Definition: mat.h:647
int rank(const Mat< T > &m, double tol=-1.0)
Calculate the rank of matrix m.
Definition: matfunc.h:493
T index_zero_pad(const Vec< T > &v, const int index)
Definition: matfunc.h:297
bool is_hermitian(const Mat< Num_T > &X)
Returns true if matrix X is hermitian, false otherwise.
Definition: matfunc.h:336
Vec< Num_T > get_row(int r) const
Get row r.
Definition: mat.h:852
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition: matfunc.h:777
Mat< T > bidiag(const Vec< T > &main, const Vec< T > &sup)
Returns a matrix with the elements of the input vector main on the diagonal and the elements of the i...
Definition: matfunc.h:617
Vec< T > repmat(const Vec< T > &v, int n)
Creates a vector with n copies of the vector v.
Definition: matfunc.h:374
Vec< T > cross(const Vec< T > &v1, const Vec< T > &v2)
Vector cross product. Vectors need to be of size 3.
Definition: matfunc.h:240
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition: matfunc.h:59
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition: vec.h:663
Definitions of matrix inversion routines.
Vec< T > rvectorize(const Mat< T > &m)
Row vectorize the matrix [(0,0) (0,1) ... (N-1,N-2) (N-1,N-1)].
Definition: matfunc.h:789
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
Logarithmic and exponenential functions - header file.
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition: converters.h:216
bool is_unitary(const Mat< Num_T > &X)
Returns true if matrix X is unitary, false otherwise.
Definition: matfunc.h:355
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
void set_col(int c, const Vec< Num_T > &v)
Set column c to vector v.
Definition: mat.h:936
T prod(const Vec< T > &v)
The product of all elements in the vector.
Definition: matfunc.h:195
T sum_sqr(const Vec< T > &v)
Sum of square of the elements in a vector.
Definition: matfunc.h:116
const double eps
Constant eps.
Definition: misc.h:109
int cols() const
The number of columns.
Definition: mat.h:235
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
bool svd(const mat &A, vec &S)
Get singular values s of a real matrix A using SVD.
Definition: svd.cpp:185
ITPP_EXPORT bool all(const bvec &testvec)
Returns true if all elements are ones and false otherwise.
int _datasize() const
Access to the internal data structure (not recommended to use)
Definition: mat.h:444
Matrix Class Definitions.
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
Definition: log_exp.h:53
T sumsum(const Mat< T > &X)
Sum of all elements in the given matrix. Fast version of sum(sum(X))
Definition: matfunc.h:101
Vec< T > cumsum(const Vec< T > &v)
Cumulative sum of all elements in the vector.
Definition: matfunc.h:157
Vec< Num_T > get_col(int c) const
Get column c.
Definition: mat.h:889
Mat< T > tridiag(const Vec< T > &main, const Vec< T > &sup, const Vec< T > &sub)
Returns a matrix with the elements of main on the diagonal, the elements of sup on the diagonal row a...
Definition: matfunc.h:690
int size(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:55
Mat< Num_T > H() const
Hermitian matrix transpose (conjugate transpose)
Definition: mat.h:341
Mat< T > repmat(const Mat< T > &data, int m, int n)
Creates a matrix with m by n copies of the matrix data.
Definition: matfunc.h:397
itpp namespace
Definition: itmex.h:36
int length() const
The size of the vector.
Definition: vec.h:269
bool inv(const mat &X, mat &Y)
Inverse of real square matrix.Calculate the inverse of the real matrix .
Definition: inv.cpp:87
Num_T * _data()
Access of the internal data structure (not recommended to use)
Definition: mat.h:440
void hermitian_transpose(const Mat< T > &m, Mat< T > &out)
Definition: matfunc.h:318
#define it_error(s)
Abort unconditionally.
Definition: itassert.h:126
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
Definition: matfunc.h:308
int rows() const
The number of rows.
Definition: mat.h:237
Mat< T > reshape(const Mat< T > &m, int rows, int cols)
Reshape the matrix into an rows*cols matrix.
Definition: matfunc.h:822
T trace(const Mat< T > &m)
The trace of the matrix m, i.e. the sum of the diagonal elements.
Definition: matfunc.h:762
void set_subvector(int i1, int i2, const Vec< Num_T > &v)
This function is deprecated. Please use set_subvector(i, v) instead.
Definition: vec.h:1404
Definitions of Singular Value Decompositions.
Mat< Num_T > kron(const Mat< Num_T > &X, const Mat< Num_T > &Y)
Computes the Kronecker product of two matrices.
Definition: matfunc.h:443
Mat< T > diag(const Vec< T > &v, const int K=0)
Create a diagonal matrix using vector v as its diagonal.
Definition: matfunc.h:557
bin abs(const bin &inbin)
absolute value of bin
Definition: binary.h:174
Mat< Num_T > T() const
Matrix transpose. Converts to a matrix with the vector in the first row.
Definition: vec.h:318
Mat< T > repmat(const Vec< T > &v, int m, int n, bool transpose=false)
Returns a matrix with m by n copies of the vector data.
Definition: matfunc.h:425
ITPP_EXPORT bool any(const bvec &testvec)
Returns true if any element is one and false otherwise.
Elementary mathematical functions - header file.
ITPP_EXPORT cmat sqrtm(const cmat &A)
Square root of the complex square matrix A.
Vec< T > cvectorize(const Mat< T > &m)
Column vectorize the matrix [(0,0) (1,0) ... (N-2,N-1) (N-1,N-1)].
Definition: matfunc.h:803
int levels2bits(int n)
Calculate the number of bits needed to represent n different values (levels).
Definition: log_exp.h:92
Mat< Num_T > T() const
Matrix transpose.
Definition: mat.h:337
Vec< T > zero_pad(const Vec< T > &v, int n)
Zero-pad a vector to size n.
Definition: matfunc.h:257
Mat< bin > bmat
bin matrix
Definition: mat.h:508
SourceForge Logo

Generated on Sun Apr 10 2022 12:00:00 for IT++ by Doxygen 1.8.14