9#include <gsl/gsl_linalg.h>
10#include <gsl/gsl_blas.h>
11#include <gsl/gsl_machine.h>
12#include <gsl/gsl_version.h>
17#if defined(GSL_MAJOR_VERSION) && defined(GSL_MINOR_VERSION)
18#if GSL_MAJOR_VERSION > 2 || (GSL_MAJOR_VERSION == 2 && GSL_MINOR_VERSION >= 6)
19#define PSCF_LU_RECURSIVE
26#ifdef PSCF_LU_RECURSIVE
51 void decomposeLU(gsl_matrix* A,
size_t* ipiv)
53 const size_t M = A->size1;
54 const size_t N = A->size2;
58 for (j = 0; j < N; ++j) {
59 gsl_vector_view c = gsl_matrix_subcolumn(A, j, j, M - j);
60 ipiv[j] = j + gsl_blas_idamax(&c.vector);
61 if (gsl_matrix_get(A, ipiv[j], j) != 0.0) {
62 if (ipiv[j] != j) gsl_matrix_swap_rows(A, j, ipiv[j]);
63 double ajj = gsl_matrix_get(A, j, j);
64 if (j < M - 1 && std::fabs(ajj) >= GSL_DBL_MIN) {
65 c = gsl_matrix_subcolumn(A, j, j + 1, M - j - 1);
66 gsl_blas_dscal(1.0 / ajj, &c.vector);
68 for (i = j + 1; i < M; ++i) {
69 *gsl_matrix_ptr(A, i, j) /= ajj;
75 l = gsl_matrix_subcolumn(A, j, j + 1, M - j - 1);
76 u = gsl_matrix_subrow(A, j, j + 1, N - j - 1);
78 A22 = gsl_matrix_submatrix(A, j+1, j+1, M-j-1, N-j-1);
79 gsl_blas_dger(-1.0, &l.vector, &u.vector, &A22.matrix);
86 const size_t N1 = ((N + 8) / 16) * 8;
87 const size_t N2 = N - N1;
88 gsl_matrix_view A11 = gsl_matrix_submatrix(A, 0, 0, N1, N1);
89 gsl_matrix_view A12 = gsl_matrix_submatrix(A, 0, N1, N1, N2);
90 gsl_matrix_view A21 = gsl_matrix_submatrix(A, N1, 0, M - N1, N1);
91 gsl_matrix_view A22 = gsl_matrix_submatrix(A, N1, N1, M - N1, N2);
92 gsl_matrix_view AL = gsl_matrix_submatrix(A, 0, 0, M, N1);
93 gsl_matrix_view AR = gsl_matrix_submatrix(A, 0, N1, M, N2);
94 decomposeLU(&AL.matrix, ipiv);
95 for (i = 0; i < N1; ++i) {
96 if (ipiv[i] != i) gsl_matrix_swap_rows(&AR.matrix, i, ipiv[i]);
98 gsl_blas_dtrsm(CblasLeft, CblasLower, CblasNoTrans, CblasUnit,
99 1.0, &A11.matrix, &A12.matrix);
100 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, -1.0, &A21.matrix,
101 &A12.matrix, 1.0, &A22.matrix);
102 decomposeLU(&A22.matrix, ipiv + N1);
103 for (i = N1; i < N; ++i) {
104 if (ipiv[i] != i - N1) {
105 gsl_matrix_swap_rows(&A21.matrix, i - N1, ipiv[i]);
137 if (luPtr_) gsl_matrix_free(luPtr_);
138 if (gMatInverse_) gsl_matrix_free(gMatInverse_);
139 if (permPtr_) gsl_permutation_free(permPtr_);
152 luPtr_ = gsl_matrix_alloc(n, n);
153 gMatInverse_ = gsl_matrix_alloc(n, n);
154 permPtr_ = gsl_permutation_alloc(n);
171 for (i = 0; i < n_; ++i) {
172 for (j = 0; j < n_; ++j) {
173 luPtr_->data[k] = A(i,j);
187#ifdef PSCF_LU_RECURSIVE
188 std::vector<size_t> ipiv(n_);
189 decomposeLU(luPtr_, ipiv.data());
190 gsl_permutation_init(permPtr_);
192 for (i = 0; i < n_; ++i) {
193 if (ipiv[i] != (
size_t) i) {
194 gsl_permutation_swap(permPtr_, i, ipiv[i]);
199 gsl_linalg_LU_decomp(luPtr_, permPtr_, &signum_);
217 gsl_linalg_LU_solve(luPtr_, permPtr_, &b_, &x_);
230 for (
int i = 0; i < n_; ++i) {
231 if (gsl_matrix_get(luPtr_, i, i) == 0.0)
return true;
251 for (i = 0; i < n_; ++i) {
252 x[i] = b[gsl_permutation_get(permPtr_, i)];
256 for (i = 1; i < n_; ++i) {
258 for (j = 0; j < i; ++j) {
259 sum -= gsl_matrix_get(luPtr_, i, j)*x[j];
265 for (i = n_ - 1; i >= 0; --i) {
266 pivot = gsl_matrix_get(luPtr_, i, i);
271 for (j = i + 1; j < n_; ++j) {
272 sum -= gsl_matrix_get(luPtr_, i, j)*x[j];
286 gMatInverse_->data = inv.
cArray();
287 gsl_linalg_LU_invert(luPtr_, permPtr_, gMatInverse_);
void inverse(Matrix< double > &inv)
Compute inverse of matrix A.
void allocate(int n)
Allocate memory.
bool isSingular() const
Does the last LU decomposition have a zero pivot?
void solveBasic(Array< double > const &b, Array< double > &x) const
Solve Ax = b by a basic solution, also for a singular matrix A.
void solve(Array< double > &b, Array< double > &x)
Solve Ax = b for known b to compute x.
void computeLU(const Matrix< double > &A)
Compute the LU decomposition for later use.
Array container class template.
Data * cArray()
Return a pointer to the underlying C array (non-const data).
int capacity() const
Return allocated size.
Two-dimensional array container template (interface).
Data * cArray()
Return pointer to underlying one-dimensional C array.
int capacity2() const
Get number of columns (range of the second array index).
int capacity1() const
Get number of rows (range of the first array index).
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
PSCF package top-level namespace.