qr.cpp
Go to the documentation of this file.
#ifndef _MSC_VER
# include <itpp/config.h>
#else
# include <itpp/config_msvc.h>
#endif
#if defined(HAVE_LAPACK)
# include <itpp/base/algebra/lapack.h>
#endif
#include <itpp/base/algebra/qr.h>
#include <itpp/base/specmat.h>
namespace itpp
{
#if defined(HAVE_LAPACK)
bool qr(const mat &A, mat &Q, 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);
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;
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 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 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 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);
}
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 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);
}
#else
bool qr(const mat &A, mat &Q, mat &R)
{
it_error("LAPACK library is needed to use qr() function");
return false;
}
bool qr(const mat &A, mat &R)
{
it_error("LAPACK library is needed to use qr() function");
return false;
}
bool qr(const mat &A, mat &Q, mat &R, bmat &P)
{
it_error("LAPACK library is needed to use qr() function");
return false;
}
bool qr(const cmat &A, cmat &Q, cmat &R)
{
it_error("LAPACK library is needed to use qr() function");
return false;
}
bool qr(const cmat &A, cmat &R)
{
it_error("LAPACK library is needed to use qr() function");
return false;
}
bool qr(const cmat &A, cmat &Q, cmat &R, bmat &P)
{
it_error("LAPACK library is needed to use qr() function");
return false;
}
#endif // HAVE_LAPACK
} // namespace itpp
it_error
#define it_error(s)
Abort unconditionally.
Definition itassert.h:126
itpp::qr
bool qr(const mat &A, mat &Q, mat &R)
QR factorisation of real matrix.
Definition qr.cpp:303
itpp::real
vec real(const cvec &data)
Real part of complex values.
Definition elem_math.cpp:157
itpp::zeros_b
ITPP_EXPORT bvec zeros_b(int size)
A Binary vector of zeros.
bmat
Mat< bin > bmat
bin matrix
Definition mat.h:508
itpp
itpp namespace
Definition itmex.h:37
qr.h
Definitions of QR factorisation functions.
specmat.h
Definitions of special vectors and matrices.

Generated on Mon Jun 10 2024 11:49:16 for IT++ by Doxygen 1.9.8