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>
38#include <itpp/itexports.h>
39
40namespace itpp
41{
42
48
50template<class T>
51int length(const Vec<T> &v) { return v.length(); }
52
54template<class T>
55int size(const Vec<T> &v) { return v.length(); }
56
58template<class T>
59T 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
76template<class T>
77Vec<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
100template<class T>
101T 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
115template<class T>
116T 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
133template<class T>
134Vec<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
156template<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
175template<class T>
176Mat<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
194template<class T>
195T 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
213template<class T>
214Vec<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
239template<class T>
240Vec<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
256template<class T>
257Vec<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
269template<class T>
271{
272 int n = pow2i(levels2bits(v.size()));
273
274 return (n == v.size()) ? v : zero_pad(v, n);
275}
276
278template<class T>
279Mat<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
296template<class T>
297T 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
307template<class T>
308void transpose(const Mat<T> &m, Mat<T> &out) { out = m.T(); }
309
311template<class T>
312Mat<T> transpose(const Mat<T> &m) { return m.T(); }
313
314
317template<class T>
318void hermitian_transpose(const Mat<T> &m, Mat<T> &out) { out = m.H(); }
319
321template<class T>
322Mat<T> hermitian_transpose(const Mat<T> &m) { return m.H(); }
323
324
325
335template<class Num_T>
337{
338
339 if (X == X.H())
340 return true;
341 else
342 return false;
343}
344
354template<class Num_T>
355bool 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
373template<class T>
374Vec<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
396template<class T>
397Mat<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
424template<class T> inline
425Mat<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
442template<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
467ITPP_EXPORT cmat sqrtm(const cmat& A);
468
481ITPP_EXPORT cmat sqrtm(const mat& A);
482
483
492template<class T>
493int 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
516template<> inline
517int rank(const imat &m, double tol)
518{
519 return rank(to_mat(m), tol);
520}
521
523template<> inline
524int rank(const smat &m, double tol)
525{
526 return rank(to_mat(m), tol);
527}
528
530template<> inline
531int 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
556template<class T>
557Mat<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
580template<class T>
581void 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
596template<class T>
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
616template<class T>
617Mat<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
642template<class T>
643void 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
665template<class T>
666void 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
689template<class T>
690Mat<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
716template<class T>
717void 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
740template<class T>
741void 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
761template<class T>
762T trace(const Mat<T> &m)
763{
764 return sum(diag(m));
765}
766
768
769
770// ----------------- reshaping vectors and matrices ------------------------
771
774
776template<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
788template<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
802template<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
821template<class T>
822Mat<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
845template<class T>
846Mat<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
863ITPP_EXPORT bool all(const bvec &testvec);
865ITPP_EXPORT bool any(const bvec &testvec);
866
868
869// ----------------------------------------------------------------------
870// Instantiations
871// ----------------------------------------------------------------------
872
873ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const vec &v);
874ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const cvec &v);
875ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const svec &v);
876ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const ivec &v);
877ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int length(const bvec &v);
878
879ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double sum(const vec &v);
880ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> sum(const cvec &v);
881ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short sum(const svec &v);
882ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int sum(const ivec &v);
883ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin sum(const bvec &v);
884
885ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double sum_sqr(const vec &v);
886ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> sum_sqr(const cvec &v);
887ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short sum_sqr(const svec &v);
888ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int sum_sqr(const ivec &v);
889ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin sum_sqr(const bvec &v);
890
891ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec cumsum(const vec &v);
892ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec cumsum(const cvec &v);
893ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec cumsum(const svec &v);
894ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec cumsum(const ivec &v);
895ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec cumsum(const bvec &v);
896
897ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double prod(const vec &v);
898ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> prod(const cvec &v);
899ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short prod(const svec &v);
900ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int prod(const ivec &v);
901ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin prod(const bvec &v);
902
903ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec cross(const vec &v1, const vec &v2);
904ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec cross(const cvec &v1, const cvec &v2);
905ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec cross(const ivec &v1, const ivec &v2);
906ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec cross(const svec &v1, const svec &v2);
907ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec cross(const bvec &v1, const bvec &v2);
908
909ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec reverse(const vec &in);
910ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec reverse(const cvec &in);
911ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec reverse(const svec &in);
912ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec reverse(const ivec &in);
913ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec reverse(const bvec &in);
914
915ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec zero_pad(const vec &v, int n);
916ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec zero_pad(const cvec &v, int n);
917ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec zero_pad(const ivec &v, int n);
918ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec zero_pad(const svec &v, int n);
919ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec zero_pad(const bvec &v, int n);
920
921ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec zero_pad(const vec &v);
922ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec zero_pad(const cvec &v);
923ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec zero_pad(const ivec &v);
924ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec zero_pad(const svec &v);
925ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec zero_pad(const bvec &v);
926
927ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat zero_pad(const mat &, int, int);
928ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat zero_pad(const cmat &, int, int);
929ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat zero_pad(const imat &, int, int);
930ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat zero_pad(const smat &, int, int);
931ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat zero_pad(const bmat &, int, int);
932
933ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec sum(const mat &m, int dim);
934ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec sum(const cmat &m, int dim);
935ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec sum(const smat &m, int dim);
936ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec sum(const imat &m, int dim);
937ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec sum(const bmat &m, int dim);
938
939ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double sumsum(const mat &X);
940ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> sumsum(const cmat &X);
941ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short sumsum(const smat &X);
942ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int sumsum(const imat &X);
943ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin sumsum(const bmat &X);
944
945ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec sum_sqr(const mat & m, int dim);
946ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec sum_sqr(const cmat &m, int dim);
947ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec sum_sqr(const smat &m, int dim);
948ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec sum_sqr(const imat &m, int dim);
949ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec sum_sqr(const bmat &m, int dim);
950
951ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat cumsum(const mat &m, int dim);
952ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat cumsum(const cmat &m, int dim);
953ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat cumsum(const smat &m, int dim);
954ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat cumsum(const imat &m, int dim);
955ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat cumsum(const bmat &m, int dim);
956
957ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec prod(const mat &m, int dim);
958ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec prod(const cmat &v, int dim);
959ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec prod(const smat &m, int dim);
960ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec prod(const imat &m, int dim);
961ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec prod(const bmat &m, int dim);
962
963ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec diag(const mat &in);
964ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec diag(const cmat &in);
965ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void diag(const vec &in, mat &m);
966ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void diag(const cvec &in, cmat &m);
967ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat diag(const vec &v, const int K);
968ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat diag(const cvec &v, const int K);
969
970ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat bidiag(const vec &, const vec &);
971ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat bidiag(const cvec &, const cvec &);
972ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const vec &, const vec &, mat &);
973ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const cvec &, const cvec &, cmat &);
974ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const mat &, vec &, vec &);
975ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void bidiag(const cmat &, cvec &, cvec &);
976
977ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat tridiag(const vec &main, const vec &, const vec &);
978ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat tridiag(const cvec &main, const cvec &, const cvec &);
979ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const vec &main, const vec &, const vec &, mat &);
980ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const cvec &main, const cvec &, const cvec &, cmat &);
981ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const mat &m, vec &, vec &, vec &);
982ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void tridiag(const cmat &m, cvec &, cvec &, cvec &);
983
984ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double trace(const mat &in);
985ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> trace(const cmat &in);
986ITPP_EXPORT_TEMPLATE template ITPP_EXPORT short trace(const smat &in);
987ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int trace(const imat &in);
988ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bin trace(const bmat &in);
989
990ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const mat &m, mat &out);
991ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const cmat &m, cmat &out);
992ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const smat &m, smat &out);
993ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const imat &m, imat &out);
994ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void transpose(const bmat &m, bmat &out);
995
996ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat transpose(const mat &m);
997ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat transpose(const cmat &m);
998ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat transpose(const smat &m);
999ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat transpose(const imat &m);
1000ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat transpose(const bmat &m);
1001
1002ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const mat &m, mat &out);
1003ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const cmat &m, cmat &out);
1004ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const smat &m, smat &out);
1005ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const imat &m, imat &out);
1006ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void hermitian_transpose(const bmat &m, bmat &out);
1007
1008ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat hermitian_transpose(const mat &m);
1009ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat hermitian_transpose(const cmat &m);
1010ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat hermitian_transpose(const smat &m);
1011ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat hermitian_transpose(const imat &m);
1012ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat hermitian_transpose(const bmat &m);
1013
1014ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_hermitian(const mat &X);
1015ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_hermitian(const cmat &X);
1016
1017ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_unitary(const mat &X);
1018ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bool is_unitary(const cmat &X);
1019
1020ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec rvectorize(const mat &m);
1021ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec rvectorize(const cmat &m);
1022ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec rvectorize(const imat &m);
1023ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec rvectorize(const smat &m);
1024ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec rvectorize(const bmat &m);
1025
1026ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec cvectorize(const mat &m);
1027ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec cvectorize(const cmat &m);
1028ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec cvectorize(const imat &m);
1029ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec cvectorize(const smat &m);
1030ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec cvectorize(const bmat &m);
1031
1032ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat reshape(const mat &m, int rows, int cols);
1033ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat reshape(const cmat &m, int rows, int cols);
1034ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat reshape(const imat &m, int rows, int cols);
1035ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat reshape(const smat &m, int rows, int cols);
1036ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat reshape(const bmat &m, int rows, int cols);
1037
1038ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat reshape(const vec &m, int rows, int cols);
1039ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat reshape(const cvec &m, int rows, int cols);
1040ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat reshape(const ivec &m, int rows, int cols);
1041ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat reshape(const svec &m, int rows, int cols);
1042ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat reshape(const bvec &m, int rows, int cols);
1043
1044ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat kron(const mat &X, const mat &Y);
1045ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat kron(const cmat &X, const cmat &Y);
1046ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat kron(const imat &X, const imat &Y);
1047ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat kron(const smat &X, const smat &Y);
1048ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat kron(const bmat &X, const bmat &Y);
1049
1050ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec repmat(const vec &v, int n);
1051ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec repmat(const cvec &v, int n);
1052ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec repmat(const ivec &v, int n);
1053ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec repmat(const svec &v, int n);
1054ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec repmat(const bvec &v, int n);
1055
1056ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat repmat(const vec &v, int m, int n, bool transpose);
1057ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat repmat(const cvec &v, int m, int n, bool transpose);
1058ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat repmat(const ivec &v, int m, int n, bool transpose);
1059ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat repmat(const svec &v, int m, int n, bool transpose);
1060ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat repmat(const bvec &v, int m, int n, bool transpose);
1061
1062ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat repmat(const mat &data, int m, int n);
1063ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat repmat(const cmat &data, int m, int n);
1064ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat repmat(const imat &data, int m, int n);
1065ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat repmat(const smat &data, int m, int n);
1066ITPP_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
Vec< Num_T > get_row(int r) const
Get row r.
Definition: mat.h:852
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
Mat< Num_T > H() const
Hermitian matrix transpose (conjugate transpose)
Definition: mat.h:341
void set_col(int c, const Vec< Num_T > &v)
Set column c to vector v.
Definition: mat.h:936
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
int rows() const
The number of rows.
Definition: mat.h:237
int cols() const
The number of columns.
Definition: mat.h:235
Mat< Num_T > T() const
Matrix transpose.
Definition: mat.h:337
int _datasize() const
Access to the internal data structure (not recommended to use)
Definition: mat.h:444
void set_row(int r, const Vec< Num_T > &v)
Set row r to vector v.
Definition: mat.h:927
Vec< Num_T > get_col(int c) const
Get column c.
Definition: mat.h:889
Num_T * _data()
Access of the internal data structure (not recommended to use)
Definition: mat.h:440
int size() const
The size of the vector.
Definition: vec.h:271
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition: vec.h:663
Mat< Num_T > T() const
Matrix transpose. Converts to a matrix with the vector in the first row.
Definition: vec.h:318
int length() const
The size of the vector.
Definition: vec.h:269
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
Binary arithmetic (boolean) class.
Definition: binary.h:57
Elementary mathematical functions - header file.
T trace(const Mat< T > &m)
The trace of the matrix m, i.e. the sum of the diagonal elements.
Definition: matfunc.h:762
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
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
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
#define it_error(s)
Abort unconditionally.
Definition: itassert.h:126
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
bool inv(const mat &X, mat &Y)
Inverse of real square matrix.
Definition: inv.cpp:87
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
Definition: log_exp.h:53
int levels2bits(int n)
Calculate the number of bits needed to represent n different values (levels).
Definition: log_exp.h:92
T index_zero_pad(const Vec< T > &v, const int index)
Definition: matfunc.h:297
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
int size(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:55
T sumsum(const Mat< T > &X)
Sum of all elements in the given matrix. Fast version of sum(sum(X))
Definition: matfunc.h:101
ITPP_EXPORT cmat sqrtm(const cmat &A)
Square root of the complex square matrix A.
bool is_unitary(const Mat< Num_T > &X)
Returns true if matrix X is unitary, false otherwise.
Definition: matfunc.h:355
Vec< T > zero_pad(const Vec< T > &v, int n)
Zero-pad a vector to size n.
Definition: matfunc.h:257
Vec< T > cumsum(const Vec< T > &v)
Cumulative sum of all elements in the vector.
Definition: matfunc.h:157
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition: matfunc.h:59
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
int rank(const Mat< T > &m, double tol=-1.0)
Calculate the rank of matrix m.
Definition: matfunc.h:493
bool is_hermitian(const Mat< Num_T > &X)
Returns true if matrix X is hermitian, false otherwise.
Definition: matfunc.h:336
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
Definition: matfunc.h:308
T sum_sqr(const Vec< T > &v)
Sum of square of the elements in a vector.
Definition: matfunc.h:116
Vec< T > repmat(const Vec< T > &v, int n)
Creates a vector with n copies of the vector v.
Definition: matfunc.h:374
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
void hermitian_transpose(const Mat< T > &m, Mat< T > &out)
Definition: matfunc.h:318
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
T prod(const Vec< T > &v)
The product of all elements in the vector.
Definition: matfunc.h:195
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
bool svd(const mat &A, vec &S)
Get singular values s of a real matrix A using SVD.
Definition: svd.cpp:185
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
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
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
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition: matfunc.h:777
Mat< T > reshape(const Mat< T > &m, int rows, int cols)
Reshape the matrix into an rows*cols matrix.
Definition: matfunc.h:822
Definitions of matrix inversion routines.
Logarithmic and exponenential functions - header file.
Matrix Class Definitions.
Mat< bin > bmat
bin matrix
Definition: mat.h:508
itpp namespace
Definition: itmex.h:37
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition: converters.h:216
ITPP_EXPORT bool any(const bvec &testvec)
Returns true if any element is one and false otherwise.
const double eps
Constant eps.
Definition: misc.h:109
ITPP_EXPORT bool all(const bvec &testvec)
Returns true if all elements are ones and false otherwise.
int abs(const itpp::bin &inbin)
absolute value of bin
Definition: binary.h:186
Definitions of Singular Value Decompositions.
SourceForge Logo

Generated on Sun Jun 5 2022 21:26:42 for IT++ by Doxygen 1.9.3