3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* Author: G. Jungman */
25 #include <gsl/gsl_math.h>
26 #include <gsl/gsl_vector.h>
27 #include <gsl/gsl_matrix.h>
28 #include <gsl/gsl_permute_vector.h>
29 #include <gsl/gsl_blas.h>
31 #include <gsl/gsl_linalg.h>
35 /* Factorise a general N x N matrix A into,
39 * where P is a permutation matrix, L is unit lower triangular and U
40 * is upper triangular.
42 * L is stored in the strict lower triangular part of the input
43 * matrix. The diagonal elements of L are unity and are not stored.
45 * U is stored in the diagonal and upper triangular part of the
48 * P is stored in the permutation p. Column j of P is column k of the
49 * identity matrix, where k = permutation->data[j]
51 * signum gives the sign of the permutation, (-1)^n, where n is the
52 * number of interchanges in the permutation.
54 * See Golub & Van Loan, Matrix Computations, Algorithm 3.4.1 (Gauss
55 * Elimination with Partial Pivoting).
59 gsl_linalg_LU_decomp (gsl_matrix * A, gsl_permutation * p, int *signum)
61 if (A->size1 != A->size2)
63 GSL_ERROR ("LU decomposition requires square matrix", GSL_ENOTSQR);
65 else if (p->size != A->size1)
67 GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
71 const size_t N = A->size1;
75 gsl_permutation_init (p);
77 for (j = 0; j < N - 1; j++)
79 /* Find maximum in the j-th column */
81 REAL ajj, max = fabs (gsl_matrix_get (A, j, j));
84 for (i = j + 1; i < N; i++)
86 REAL aij = fabs (gsl_matrix_get (A, i, j));
97 gsl_matrix_swap_rows (A, j, i_pivot);
98 gsl_permutation_swap (p, j, i_pivot);
102 ajj = gsl_matrix_get (A, j, j);
106 for (i = j + 1; i < N; i++)
108 REAL aij = gsl_matrix_get (A, i, j) / ajj;
109 gsl_matrix_set (A, i, j, aij);
111 for (k = j + 1; k < N; k++)
113 REAL aik = gsl_matrix_get (A, i, k);
114 REAL ajk = gsl_matrix_get (A, j, k);
115 gsl_matrix_set (A, i, k, aik - aij * ajk);
126 gsl_linalg_LU_solve (const gsl_matrix * LU, const gsl_permutation * p, const gsl_vector * b, gsl_vector * x)
128 if (LU->size1 != LU->size2)
130 GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
132 else if (LU->size1 != p->size)
134 GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
136 else if (LU->size1 != b->size)
138 GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
140 else if (LU->size2 != x->size)
142 GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
148 gsl_vector_memcpy (x, b);
152 gsl_linalg_LU_svx (LU, p, x);
160 gsl_linalg_LU_svx (const gsl_matrix * LU, const gsl_permutation * p, gsl_vector * x)
162 if (LU->size1 != LU->size2)
164 GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
166 else if (LU->size1 != p->size)
168 GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
170 else if (LU->size1 != x->size)
172 GSL_ERROR ("matrix size must match solution/rhs size", GSL_EBADLEN);
176 /* Apply permutation to RHS */
178 gsl_permute_vector (p, x);
180 /* Solve for c using forward-substitution, L c = P b */
182 gsl_blas_dtrsv (CblasLower, CblasNoTrans, CblasUnit, LU, x);
184 /* Perform back-substitution, U x = c */
186 gsl_blas_dtrsv (CblasUpper, CblasNoTrans, CblasNonUnit, LU, x);
194 gsl_linalg_LU_refine (const gsl_matrix * A, const gsl_matrix * LU, const gsl_permutation * p, const gsl_vector * b, gsl_vector * x, gsl_vector * residual)
196 if (A->size1 != A->size2)
198 GSL_ERROR ("matrix a must be square", GSL_ENOTSQR);
200 if (LU->size1 != LU->size2)
202 GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
204 else if (A->size1 != LU->size2)
206 GSL_ERROR ("LU matrix must be decomposition of a", GSL_ENOTSQR);
208 else if (LU->size1 != p->size)
210 GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
212 else if (LU->size1 != b->size)
214 GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
216 else if (LU->size1 != x->size)
218 GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
222 /* Compute residual, residual = (A * x - b) */
224 gsl_vector_memcpy (residual, b);
225 gsl_blas_dgemv (CblasNoTrans, 1.0, A, x, -1.0, residual);
227 /* Find correction, delta = - (A^-1) * residual, and apply it */
229 gsl_linalg_LU_svx (LU, p, residual);
230 gsl_blas_daxpy (-1.0, residual, x);
237 gsl_linalg_LU_invert (const gsl_matrix * LU, const gsl_permutation * p, gsl_matrix * inverse)
239 size_t i, n = LU->size1;
241 int status = GSL_SUCCESS;
243 gsl_matrix_set_identity (inverse);
245 for (i = 0; i < n; i++)
247 gsl_vector_view c = gsl_matrix_column (inverse, i);
248 int status_i = gsl_linalg_LU_svx (LU, p, &(c.vector));
258 gsl_linalg_LU_det (gsl_matrix * LU, int signum)
260 size_t i, n = LU->size1;
262 double det = (double) signum;
264 for (i = 0; i < n; i++)
266 det *= gsl_matrix_get (LU, i, i);
274 gsl_linalg_LU_lndet (gsl_matrix * LU)
276 size_t i, n = LU->size1;
280 for (i = 0; i < n; i++)
282 lndet += log (fabs (gsl_matrix_get (LU, i, i)));
290 gsl_linalg_LU_sgndet (gsl_matrix * LU, int signum)
292 size_t i, n = LU->size1;
296 for (i = 0; i < n; i++)
298 double u = gsl_matrix_get (LU, i, i);