5 #ifndef FML_CPU_LINALG_LINALG_QR_H
6 #define FML_CPU_LINALG_LINALG_QR_H
13 #include "../../_internals/linalgutils.hh"
14 #include "../../_internals/omp.hh"
16 #include "../internals/cpu_utils.hh"
19 #include "../cpumat.hh"
20 #include "../cpuvec.hh"
22 #include "internals/lapack.hh"
31 template <
typename REAL>
32 void qr_internals(
const bool pivot, cpumat<REAL> &x, cpuvec<REAL> &qraux, cpuvec<REAL> &work)
34 const len_t m = x.nrows();
35 const len_t n = x.ncols();
36 const len_t minmn = std::min(m, n);
43 fml::lapack::geqp3(m, n, NULL, m, NULL, NULL, &tmp, -1, &info);
45 fml::lapack::geqrf(m, n, NULL, m, NULL, &tmp, -1, &info);
47 int lwork = std::max((
int) tmp, 1);
48 if (lwork > work.size())
55 fml::lapack::geqp3(m, n, x.data_ptr(), m, p.data_ptr(), qraux.data_ptr(), work.data_ptr(), lwork, &info);
58 fml::lapack::geqrf(m, n, x.data_ptr(), m, qraux.data_ptr(), work.data_ptr(), lwork, &info);
63 fml::linalgutils::check_info(info,
"geqp3");
65 fml::linalgutils::check_info(info,
"geqrf");
93 template <
typename REAL>
97 qr_internals(pivot, x, qraux, work);
119 template <
typename REAL>
123 const len_t m = QR.
nrows();
124 const len_t n = QR.
ncols();
125 const len_t minmn = std::min(m, n);
129 fml::lapack::orgqr(m, minmn, minmn, QR.
data_ptr(), m, NULL,
132 int lwork = (int) tmp;
133 if (lwork > work.size())
139 fml::lapack::orgqr(m, minmn, minmn, Q.
data_ptr(), m, qraux.data_ptr(),
140 work.data_ptr(), lwork, &info);
141 fml::linalgutils::check_info(info,
"orgqr");
161 template <
typename REAL>
164 const len_t m = QR.
nrows();
165 const len_t n = QR.
ncols();
166 const len_t minmn = std::min(m, n);
177 template <
typename REAL>
180 const len_t m = x.
nrows();
181 const len_t n = x.
ncols();
182 const len_t minmn = std::min(m, n);
188 fml::lapack::gelqf(m, n, NULL, m, NULL, &tmp, -1, &info);
189 int lwork = std::max((
int) tmp, 1);
190 if (lwork > work.size())
193 fml::lapack::gelqf(m, n, x.
data_ptr(), m, lqaux.
data_ptr(), work.data_ptr(),
197 fml::linalgutils::check_info(info,
"gelqf");
222 template <
typename REAL>
226 lq_internals(x, lqaux, work);
246 template <
typename REAL>
249 const len_t m = LQ.
nrows();
250 const len_t n = LQ.
ncols();
251 const len_t minmn = std::min(m, n);
278 template <
typename REAL>
282 const len_t m = LQ.
nrows();
283 const len_t n = LQ.
ncols();
284 const len_t minmn = std::min(m, n);
288 fml::lapack::orglq(minmn, n, minmn, LQ.
data_ptr(), m, NULL,
291 int lwork = (int) tmp;
292 if (lwork > work.size())
299 work.data_ptr(), lwork, &info);
300 fml::linalgutils::check_info(info,
"orglq");