gsl_linalg__lu.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* linalg/lu.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "gsl_math.h"
  24. #include "gsl_vector.h"
  25. #include "gsl_matrix.h"
  26. #include "gsl_permute_vector.h"
  27. #include "gsl_blas.h"
  28. #include "gsl_linalg.h"
  29. #define REAL double
  30. /* Factorise a general N x N matrix A into,
  31. *
  32. * P A = L U
  33. *
  34. * where P is a permutation matrix, L is unit lower triangular and U
  35. * is upper triangular.
  36. *
  37. * L is stored in the strict lower triangular part of the input
  38. * matrix. The diagonal elements of L are unity and are not stored.
  39. *
  40. * U is stored in the diagonal and upper triangular part of the
  41. * input matrix.
  42. *
  43. * P is stored in the permutation p. Column j of P is column k of the
  44. * identity matrix, where k = permutation->data[j]
  45. *
  46. * signum gives the sign of the permutation, (-1)^n, where n is the
  47. * number of interchanges in the permutation.
  48. *
  49. * See Golub & Van Loan, Matrix Computations, Algorithm 3.4.1 (Gauss
  50. * Elimination with Partial Pivoting).
  51. */
  52. int
  53. gsl_linalg_LU_decomp (gsl_matrix * A, gsl_permutation * p, int *signum)
  54. {
  55. if (A->size1 != A->size2)
  56. {
  57. GSL_ERROR ("LU decomposition requires square matrix", GSL_ENOTSQR);
  58. }
  59. else if (p->size != A->size1)
  60. {
  61. GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
  62. }
  63. else
  64. {
  65. const size_t N = A->size1;
  66. size_t i, j, k;
  67. *signum = 1;
  68. gsl_permutation_init (p);
  69. for (j = 0; j < N - 1; j++)
  70. {
  71. /* Find maximum in the j-th column */
  72. REAL ajj, max = fabs (gsl_matrix_get (A, j, j));
  73. size_t i_pivot = j;
  74. for (i = j + 1; i < N; i++)
  75. {
  76. REAL aij = fabs (gsl_matrix_get (A, i, j));
  77. if (aij > max)
  78. {
  79. max = aij;
  80. i_pivot = i;
  81. }
  82. }
  83. if (i_pivot != j)
  84. {
  85. gsl_matrix_swap_rows (A, j, i_pivot);
  86. gsl_permutation_swap (p, j, i_pivot);
  87. *signum = -(*signum);
  88. }
  89. ajj = gsl_matrix_get (A, j, j);
  90. if (ajj != 0.0)
  91. {
  92. for (i = j + 1; i < N; i++)
  93. {
  94. REAL aij = gsl_matrix_get (A, i, j) / ajj;
  95. gsl_matrix_set (A, i, j, aij);
  96. for (k = j + 1; k < N; k++)
  97. {
  98. REAL aik = gsl_matrix_get (A, i, k);
  99. REAL ajk = gsl_matrix_get (A, j, k);
  100. gsl_matrix_set (A, i, k, aik - aij * ajk);
  101. }
  102. }
  103. }
  104. }
  105. return GSL_SUCCESS;
  106. }
  107. }
  108. int
  109. gsl_linalg_LU_solve (const gsl_matrix * LU, const gsl_permutation * p, const gsl_vector * b, gsl_vector * x)
  110. {
  111. if (LU->size1 != LU->size2)
  112. {
  113. GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
  114. }
  115. else if (LU->size1 != p->size)
  116. {
  117. GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
  118. }
  119. else if (LU->size1 != b->size)
  120. {
  121. GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
  122. }
  123. else if (LU->size2 != x->size)
  124. {
  125. GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
  126. }
  127. else
  128. {
  129. /* Copy x <- b */
  130. gsl_vector_memcpy (x, b);
  131. /* Solve for x */
  132. gsl_linalg_LU_svx (LU, p, x);
  133. return GSL_SUCCESS;
  134. }
  135. }
  136. int
  137. gsl_linalg_LU_svx (const gsl_matrix * LU, const gsl_permutation * p, gsl_vector * x)
  138. {
  139. if (LU->size1 != LU->size2)
  140. {
  141. GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
  142. }
  143. else if (LU->size1 != p->size)
  144. {
  145. GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
  146. }
  147. else if (LU->size1 != x->size)
  148. {
  149. GSL_ERROR ("matrix size must match solution/rhs size", GSL_EBADLEN);
  150. }
  151. else
  152. {
  153. /* Apply permutation to RHS */
  154. gsl_permute_vector (p, x);
  155. /* Solve for c using forward-substitution, L c = P b */
  156. gsl_blas_dtrsv (CblasLower, CblasNoTrans, CblasUnit, LU, x);
  157. /* Perform back-substitution, U x = c */
  158. gsl_blas_dtrsv (CblasUpper, CblasNoTrans, CblasNonUnit, LU, x);
  159. return GSL_SUCCESS;
  160. }
  161. }
  162. int
  163. 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)
  164. {
  165. if (A->size1 != A->size2)
  166. {
  167. GSL_ERROR ("matrix a must be square", GSL_ENOTSQR);
  168. }
  169. if (LU->size1 != LU->size2)
  170. {
  171. GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
  172. }
  173. else if (A->size1 != LU->size2)
  174. {
  175. GSL_ERROR ("LU matrix must be decomposition of a", GSL_ENOTSQR);
  176. }
  177. else if (LU->size1 != p->size)
  178. {
  179. GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
  180. }
  181. else if (LU->size1 != b->size)
  182. {
  183. GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
  184. }
  185. else if (LU->size1 != x->size)
  186. {
  187. GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
  188. }
  189. else
  190. {
  191. /* Compute residual, residual = (A * x - b) */
  192. gsl_vector_memcpy (residual, b);
  193. gsl_blas_dgemv (CblasNoTrans, 1.0, A, x, -1.0, residual);
  194. /* Find correction, delta = - (A^-1) * residual, and apply it */
  195. gsl_linalg_LU_svx (LU, p, residual);
  196. gsl_blas_daxpy (-1.0, residual, x);
  197. return GSL_SUCCESS;
  198. }
  199. }
  200. int
  201. gsl_linalg_LU_invert (const gsl_matrix * LU, const gsl_permutation * p, gsl_matrix * inverse)
  202. {
  203. size_t i, n = LU->size1;
  204. int status = GSL_SUCCESS;
  205. gsl_matrix_set_identity (inverse);
  206. for (i = 0; i < n; i++)
  207. {
  208. gsl_vector_view c = gsl_matrix_column (inverse, i);
  209. int status_i = gsl_linalg_LU_svx (LU, p, &(c.vector));
  210. if (status_i)
  211. status = status_i;
  212. }
  213. return status;
  214. }
  215. double
  216. gsl_linalg_LU_det (gsl_matrix * LU, int signum)
  217. {
  218. size_t i, n = LU->size1;
  219. double det = (double) signum;
  220. for (i = 0; i < n; i++)
  221. {
  222. det *= gsl_matrix_get (LU, i, i);
  223. }
  224. return det;
  225. }
  226. double
  227. gsl_linalg_LU_lndet (gsl_matrix * LU)
  228. {
  229. size_t i, n = LU->size1;
  230. double lndet = 0.0;
  231. for (i = 0; i < n; i++)
  232. {
  233. lndet += log (fabs (gsl_matrix_get (LU, i, i)));
  234. }
  235. return lndet;
  236. }
  237. int
  238. gsl_linalg_LU_sgndet (gsl_matrix * LU, int signum)
  239. {
  240. size_t i, n = LU->size1;
  241. int s = signum;
  242. for (i = 0; i < n; i++)
  243. {
  244. double u = gsl_matrix_get (LU, i, i);
  245. if (u < 0)
  246. {
  247. s *= -1;
  248. }
  249. else if (u == 0)
  250. {
  251. s = 0;
  252. break;
  253. }
  254. }
  255. return s;
  256. }