bool qr(const mat &A, mat &R) { int info; int m = A.rows(); int n = A.cols(); int lwork = n; int k = std::min(m, n); vec tau(k); vec work(lwork); R = A; // perform workspace query for optimum lwork value int lwork_tmp = -1; dgeqrf_(&m, &n, R._data(), &m, tau._data(), work._data(), &lwork_tmp, &info); if (info == 0) { lwork = static_cast<int>(work(0)); work.set_size(lwork, false); } dgeqrf_(&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 mat &A, mat &U, mat &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 = 3 * n; // This may be choosen better! int sdim = 0; vec wr(n); vec wi(n); vec work(lwork); T.set_size(lda, n, false); U.set_size(ldvs, n, false); T = A; // The routine overwrites input matrix with eigenvectors dgees_(&jobvs, &sort, 0, &n, T._data(), &lda, &sdim, wr._data(), wi._data(), U._data(), &ldvs, work._data(), &lwork, 0, &info); return (info == 0); }
bool qr(const mat &A, mat &Q, mat &R, bmat &P) { int info; int m = A.rows(); int n = A.cols(); int lwork = n; int k = std::min(m, n); vec tau(k); vec work(lwork); ivec jpvt(n); jpvt.zeros(); R = A; // perform workspace query for optimum lwork value int lwork_tmp = -1; dgeqp3_(&m, &n, R._data(), &m, jpvt._data(), tau._data(), work._data(), &lwork_tmp, &info); if (info == 0) { lwork = static_cast<int>(work(0)); work.set_size(lwork, false); } dgeqp3_(&m, &n, R._data(), &m, jpvt._data(), tau._data(), work._data(), &lwork, &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; dorgqr_(&m, &m, &k, Q._data(), &m, tau._data(), work._data(), &lwork_tmp, &info); if (info == 0) { lwork = static_cast<int>(work(0)); work.set_size(lwork, false); } dorgqr_(&m, &m, &k, Q._data(), &m, tau._data(), work._data(), &lwork, &info); return (info == 0); }
bool inv(const mat &X, mat &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; vec work(lwork); dgetrf_(&m, &m, Y._data(), &m, p._data(), &info); // LU-factorization if (info!=0) return false; dgetri_(&m, Y._data(), &m, p._data(), work._data(), &lwork, &info); return (info==0); }
cmat operator*(const std::complex<double> &s, const mat &m) { it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator*(): Matrix of zero length"); cmat temp(m.rows(), m.cols()); for (int i = 0;i < m._datasize();i++) { temp._data()[i] = s * m._data()[i]; } return temp; }
bool svd(const mat &A, mat &U, vec &S, mat &V) { char jobu='A', jobvt='A'; int m, n, lda, ldu, ldvt, lwork, info; m = lda = ldu = A.rows(); n = ldvt = A.cols(); lwork = max(3*min(m,n)+max(m,n), 5*min(m,n)); U.set_size(m,m, false); V.set_size(n,n, false); S.set_size(min(m,n), false); vec work(lwork); mat B(A); dgesvd_(&jobu, &jobvt, &m, &n, B._data(), &lda, S._data(), U._data(), &ldu, V._data(), &ldvt, work._data(), &lwork, &info); V = transpose(V); // This is probably slow!!! return (info==0); }