15 #pragma once // Ensure that file is included only once in a single compilation.
27 const char*
what()
const throw() {
return "LAPACK's function failed!"; }
59 template <
unsigned int system_size,
unsigned int n_rhs_cols = 1,
typename lapack_
float_t>
60 std::array<lapack_float_t, system_size * n_rhs_cols>
lapack_solve(
61 std::array<lapack_float_t, system_size * system_size>& dense_mat,
62 std::array<lapack_float_t, system_size * n_rhs_cols>& rhs);
81 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
82 lapack_float_t
lapack_det(std::array<lapack_float_t, n_rows * n_cols>& dense_mat);
96 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
98 std::array<lapack_float_t, n_rows * n_cols>& dense_mat);
113 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
115 std::array<lapack_float_t, n_rows * n_cols>& dense_mat);
130 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
131 void lapack_qr_decomp(std::array<lapack_float_t, n_rows * n_cols>& dense_mat,
132 std::array<lapack_float_t, n_rows * n_rows>& mat_q);
149 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
150 void lapack_qr_decomp(std::array<lapack_float_t, n_rows * n_cols>& dense_mat,
151 std::array<lapack_float_t, n_rows * n_rows>& mat_q,
152 std::array<lapack_float_t, n_cols * n_cols>& mat_r);
174 void daxpy_(
int* n,
double* alpha,
double* dx,
int* incx,
double* dy,
int* incy);
181 double dnrm2_(
int* n,
double* x,
int* incx);
188 void dgetrf_(
int* M,
int* N,
double*
A,
int* lda,
int* IPIV,
int* INFO);
195 void dgetrs_(
char* C,
210 void dgesv_(
int* n,
int* nrhs,
double* a,
int* lda,
int* ipiv,
double* b,
int* ldb,
int* info);
217 void dgeqr2_(
int* m,
int* n,
double* a,
int* lda,
double* tau,
double*
work,
int* info);
225 void saxpy_(
int* n,
float* alpha,
float* dx,
int* incx,
float* dy,
int* incy);
232 double snrm2_(
int* n,
float* x,
int* incx);
239 void sgetrf_(
int* M,
int* N,
float*
A,
int* lda,
int* IPIV,
int* INFO);
247 sgetrs_(
char* C,
int* N,
int* NRHS,
float*
A,
int* LDA,
int* IPIV,
float* B,
int* LDB,
int* INFO);
254 void sgesv_(
int* n,
int* nrhs,
float* a,
int* lda,
int* ipiv,
float* b,
int* ldb,
int* info);
261 void sgeqr2_(
int* m,
int* n,
float* a,
int* lda,
float* tau,
float*
work,
int* info);
335 inline void lapack_qr(
int n_rows,
int n_cols,
double* mat_a,
double* tau)
338 double*
work =
new double[n_cols];
339 dgeqr2_(&n_rows, &n_cols, mat_a, &n_rows, tau,
work, &info);
357 inline void lapack_qr(
int n_rows,
int n_cols,
float* mat_a,
float* tau)
360 float*
work =
new float[n_cols];
361 sgeqr2_(&n_rows, &n_cols, mat_a, &n_rows, tau,
work, &info);
380 template <
unsigned int system_size,
unsigned int n_rhs_cols,
typename lapack_
float_t>
382 std::array<lapack_float_t, system_size * system_size>& dense_mat,
383 std::array<lapack_float_t, system_size * n_rhs_cols>& rhs)
393 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
394 lapack_float_t
lapack_det(std::array<lapack_float_t, n_rows * n_cols>& dense_mat)
396 constexpr
unsigned int rank = std::min(n_rows, n_cols);
397 std::array<lapack_float_t, rank> tau;
398 lapack_qr(n_rows, n_cols, dense_mat.data(), tau.data());
401 for (
unsigned int i = 0; i < rank; ++i)
436 template <
unsigned int n_rows,
unsigned int n_cols,
unsigned int rank,
typename lapack_
float_t>
438 const std::array<lapack_float_t, n_rows * n_cols>& dense_mat,
439 const std::array<lapack_float_t, rank>& tau)
441 SmallMat unity = diagonal<n_rows, n_rows, lapack_float_t>(1.), matQ = unity;
444 if constexpr (rank > 0)
445 for (
unsigned int i = 0; i < rank; ++i)
447 for (
unsigned int j = 0; j < n_rows; ++j)
453 vec[j] = dense_mat[i * n_rows + j];
475 template <
unsigned int n_rows,
unsigned int n_cols,
unsigned int rank,
typename lapack_
float_t>
477 const std::array<lapack_float_t, n_rows * n_cols>& dense_mat,
478 const std::array<lapack_float_t, rank>& tau,
479 std::array<lapack_float_t, n_rows * n_rows>& mat_q)
481 SmallMat unity = diagonal<n_rows, n_rows, lapack_float_t>(1.);
486 for (
unsigned int i = 0; i < rank; ++i)
488 for (
unsigned int j = 0; j < n_rows; ++j)
494 vec[j] = dense_mat[i * n_rows + j];
498 mat_q = std::move(matQ.
data());
516 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
518 std::array<lapack_float_t, n_rows * n_cols>& dense_mat)
520 for (
unsigned int i = 0; i < n_cols; ++i)
521 for (
unsigned int j = 0; j < n_rows; ++j)
523 dense_mat[i * n_rows + j] = 0.;
541 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
543 const std::array<lapack_float_t, n_rows * n_cols>& lapack_mat,
544 std::array<lapack_float_t, n_cols * n_cols>& mat_r)
546 static_assert(n_rows >= n_cols,
"Function only defined for these matrices!");
547 for (
unsigned int i = 0; i < n_cols; ++i)
548 for (
unsigned int j = 0; j < n_cols; ++j)
550 mat_r[i * n_rows + j] = 0.;
552 mat_r[i * n_cols + j] = lapack_mat[i * n_rows + j];
568 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
570 std::array<lapack_float_t, n_rows * n_cols>& dense_mat)
572 constexpr
unsigned int rank = std::min(n_rows, n_cols);
573 std::array<lapack_float_t, rank> tau;
574 lapack_qr(n_rows, n_cols, dense_mat.data(), tau.data());
575 return get_q_from_lapack_qr_result<n_rows, n_cols, rank, lapack_float_t>(dense_mat, tau);
582 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
584 std::array<lapack_float_t, n_rows * n_cols>& dense_mat)
586 constexpr
unsigned int rank = std::min(n_rows, n_cols);
587 std::array<lapack_float_t, rank> tau;
588 lapack_qr(n_rows, n_cols, dense_mat.data(), tau.data());
589 return get_r_from_lapack_qr_result<n_rows, n_cols, rank, lapack_float_t>(dense_mat);
596 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
598 std::array<lapack_float_t, n_rows * n_rows>& mat_q)
600 constexpr
unsigned int rank = std::min(n_rows, n_cols);
601 std::array<lapack_float_t, rank> tau;
602 lapack_qr(n_rows, n_cols, dense_mat.data(), tau.data());
604 get_q_from_lapack_qr_result<n_rows, n_cols, rank, lapack_float_t>(dense_mat, tau, mat_q);
605 get_r_from_lapack_qr_result<n_rows, n_cols, lapack_float_t>(dense_mat);
612 template <
unsigned int n_rows,
unsigned int n_cols,
typename lapack_
float_t>
614 std::array<lapack_float_t, n_rows * n_rows>& mat_q,
615 std::array<lapack_float_t, n_cols * n_cols>& mat_r)
617 constexpr
unsigned int rank = std::min(n_rows, n_cols);
618 std::array<lapack_float_t, rank> tau;
619 lapack_qr(n_rows, n_cols, dense_mat.data(), tau.data());
621 get_q_from_lapack_qr_result<n_rows, n_cols, rank, lapack_float_t>(dense_mat, tau, mat_q);
622 get_r_from_lapack_qr_result<n_rows, n_cols, lapack_float_t>(dense_mat, mat_r);
623 get_r_from_lapack_qr_result<n_rows, n_cols, lapack_float_t>(dense_mat);