numerics 0.1.0
Loading...
Searching...
No Matches
lu.cpp
Go to the documentation of this file.
1/// @file linalg/factorization/backends/lapack/lu.cpp
2/// @brief LAPACK LU factorization via LAPACKE_dgetrf.
3///
4/// Falls back to seq::lu() with a warning when NUMERICS_HAS_LAPACK is not set;
5/// this keeps the build working but reminds the caller that LAPACK is absent.
6#include "../seq/impl.hpp"
7#include "impl.hpp"
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12#if defined(NUMERICS_HAS_LAPACK)
13#include <lapacke.h>
14#endif
15
16namespace num::backends::lapack {
17
18LUResult lu(const Matrix &A) {
19#if defined(NUMERICS_HAS_LAPACK)
20 const idx n = A.rows();
21 LUResult f;
22 f.LU = A;
23 f.piv.resize(n);
24 f.singular = false;
25
26 std::vector<lapack_int> ipiv(n);
27 int info = LAPACKE_dgetrf(LAPACK_ROW_MAJOR, static_cast<lapack_int>(n),
28 static_cast<lapack_int>(n), f.LU.data(),
29 static_cast<lapack_int>(n), ipiv.data());
30 if (info < 0)
31 throw std::runtime_error("lu (lapack): dgetrf argument error, info=" +
32 std::to_string(info));
33 if (info > 0)
34 f.singular = true;
35
36 // Convert 1-based LAPACK IPIV to 0-based sequential swap targets
37 for (idx k = 0; k < n; ++k)
38 f.piv[k] = static_cast<idx>(ipiv[k] - 1);
39
40 return f;
41#else
42 return seq::lu(A);
43#endif
44}
45
46} // namespace num::backends::lapack
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
real * data()
Definition matrix.hpp:28
constexpr idx rows() const noexcept
Definition matrix.hpp:24
LUResult lu(const Matrix &A)
Definition lu.cpp:18
LUResult lu(const Matrix &A)
Definition lu.cpp:9
std::size_t idx
Definition types.hpp:11
std::experimental::simd butterfly for FFT.
Result of an LU factorization with partial pivoting (PA = LU)
Definition lu.hpp:19
Matrix LU
Definition lu.hpp:20
std::vector< idx > piv
Definition lu.hpp:21
bool singular
Definition lu.hpp:22