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); }
void Matlab_Engine::get(cmat &m, const char *name){ mxArray *T; if((T = engGetArray(e, name)) == NULL) cout << "No variable with the given name exists in the Matlab workspace!\n"; else{ if(mxGetNumberOfDimensions(T)==2){ double *pt_r = (double*) mxGetPr(T); double *pt_i = (double*) mxGetPi(T); const int *dims = mxGetDimensions(T); m.set_size(dims[0], dims[1], false); if(mxIsComplex(T)) // Copy both real and imaginary part. for(int row=0; row<dims[0]; row++){ for(int col=0; col<dims[1]; col++){ complex<double> value(*pt_r++, *pt_i++); m(row, col) = value; } } else // Copy only the real part. for(int row=0; row<dims[0]; row++){ for(int col=0; col<dims[1]; col++){ complex<double> value(*pt_r++, 0); m(row, col) = value; } } } else cout << "The requested variable is not a matrix!\n"; } }
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 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); }
bool qr(const cmat &A, cmat &Q, 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); Q = R; Q.set_size(m, m, true); // 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); }