bool qr(const cmat &A, cmat &R) { int info; int m = A.rows(); int n = A.cols(); int lwork = n; int k = std::min(m, n); cvec tau(k); cvec work(lwork); R = A; // perform workspace query for optimum lwork value int lwork_tmp = -1; zgeqrf_(&m, &n, R._data(), &m, tau._data(), work._data(), &lwork_tmp, &info); if (info == 0) { lwork = static_cast<int>(real(work(0))); work.set_size(lwork, false); } zgeqrf_(&m, &n, R._data(), &m, tau._data(), work._data(), &lwork, &info); // construct R for (int i = 0; i < m; i++) for (int j = 0; j < std::min(i, n); j++) R(i, j) = 0; return (info == 0); }
bool schur(const cmat &A, cmat &U, cmat &T) { it_assert_debug(A.rows() == A.cols(), "schur(): Matrix is not square"); char jobvs = 'V'; char sort = 'N'; int info; int n = A.rows(); int lda = n; int ldvs = n; int lwork = 2 * n; // This may be choosen better! int sdim = 0; vec rwork(n); cvec w(n); cvec work(lwork); T.set_size(lda, n, false); U.set_size(ldvs, n, false); T = A; // The routine overwrites input matrix with eigenvectors zgees_(&jobvs, &sort, 0, &n, T._data(), &lda, &sdim, w._data(), U._data(), &ldvs, work._data(), &lwork, rwork._data(), 0, &info); return (info == 0); }
bool qr(const cmat &A, cmat &Q, cmat &R, bmat &P) { int info; int m = A.rows(); int n = A.cols(); int lwork = n; int k = std::min(m, n); cvec tau(k); cvec work(lwork); vec rwork(std::max(1, 2*n)); ivec jpvt(n); jpvt.zeros(); R = A; // perform workspace query for optimum lwork value int lwork_tmp = -1; zgeqp3_(&m, &n, R._data(), &m, jpvt._data(), tau._data(), work._data(), &lwork_tmp, rwork._data(), &info); if (info == 0) { lwork = static_cast<int>(real(work(0))); work.set_size(lwork, false); } zgeqp3_(&m, &n, R._data(), &m, jpvt._data(), tau._data(), work._data(), &lwork, rwork._data(), &info); Q = R; Q.set_size(m, m, true); // construct permutation matrix P = zeros_b(n, n); for (int j = 0; j < n; j++) P(jpvt(j) - 1, j) = 1; // construct R for (int i = 0; i < m; i++) for (int j = 0; j < std::min(i, n); j++) R(i, j) = 0; // perform workspace query for optimum lwork value lwork_tmp = -1; zungqr_(&m, &m, &k, Q._data(), &m, tau._data(), work._data(), &lwork_tmp, &info); if (info == 0) { lwork = static_cast<int>(real(work(0))); work.set_size(lwork, false); } zungqr_(&m, &m, &k, Q._data(), &m, tau._data(), work._data(), &lwork, &info); return (info == 0); }
bool inv(const cmat &X, cmat &Y) { // it_assert1(X.rows() == X.cols(), "inv: matrix is not square"); int m = X.rows(), info, lwork; lwork = m; // may be choosen better ivec p(m); Y = X; cvec work(lwork); zgetrf_(&m, &m, Y._data(), &m, p._data(), &info); // LU-factorization if (info!=0) return false; zgetri_(&m, Y._data(), &m, p._data(), work._data(), &lwork, &info); return (info==0); }
bool svd(const cmat &A, cmat &U, vec &S, cmat &V) { char jobu='A', jobvt='A'; int m, n, lda, ldu, ldvt, lwork, info; m = lda = ldu = A.rows(); n = ldvt = A.cols(); lwork = 2*min(m,n)+max(m,n); U.set_size(m,m, false); V.set_size(n,n, false); S.set_size(min(m,n), false); cvec work(lwork); vec rwork(max(1, 5*min(m, n))); cmat B(A); zgesvd_(&jobu, &jobvt, &m, &n, B._data(), &lda, S._data(), U._data(), &ldu, V._data(), &ldvt, work._data(), &lwork, rwork._data(), &info); V = transpose(conj(V)); // This is slow!!! return (info==0); }