Debian Codegraph @

external/gsl/gsl_linalg__luc.c

raw symbols SCIP
1 /* linalg/luc.c
2 *
3 * Copyright (C) 2001, 2007 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
20 #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/
21 #include <stdlib.h>
22 #include <string.h>
23 #include "gsl_math.h"cxx . . $ `<file>/external/gsl/gsl_math.h`/
24 #include "gsl_vector.h"cxx . . $ `<file>/external/gsl/gsl_vector.h`/
25 #include "gsl_matrix.h"cxx . . $ `<file>/external/gsl/gsl_matrix.h`/
26 #include "gsl_complex.h"cxx . . $ `<file>/external/gsl/gsl_complex.h`/
27 #include "gsl_complex_math.h"cxx . . $ `<file>/external/gsl/gsl_complex_math.h`/
28 #include "gsl_permute_vector.h"cxx . . $ `<file>/external/gsl/gsl_permute_vector.h`/
29 #include "gsl_blas.h"cxx . . $ `<file>/external/gsl/gsl_blas.h`/
30 #include "gsl_complex_math.h"cxx . . $ `<file>/external/gsl/gsl_complex_math.h`/
31
32 #include "gsl_linalg.h"cxx . . $ `<file>/external/gsl/gsl_linalg.h`/
33
34 /* Factorise a general N x N complex matrix A into,
35 *
36 * P A = L U
37 *
38 * where P is a permutation matrix, L is unit lower triangular and U
39 * is upper triangular.
40 *
41 * L is stored in the strict lower triangular part of the input
42 * matrix. The diagonal elements of L are unity and are not stored.
43 *
44 * U is stored in the diagonal and upper triangular part of the
45 * input matrix.
46 *
47 * P is stored in the permutation p. Column j of P is column k of the
48 * identity matrix, where k = permutation->data[j]
49 *
50 * signum gives the sign of the permutation, (-1)^n, where n is the
51 * number of interchanges in the permutation.
52 *
53 * See Golub & Van Loan, Matrix Computations, Algorithm 3.4.1 (Gauss
54 * Elimination with Partial Pivoting).
55 */
56
57 int
58 gsl_linalg_complex_LU_decompcxx . . $ gsl_linalg_complex_LU_decomp(7277b4008777a0e5). (gsl_matrix_complexcxx . . $ gsl_matrix_complex# * A, gsl_permutationcxx . . $ gsl_permutation# * p, int *signum)
59 {
60 if (A->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != A->size2cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size2.)
61 {
62 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("LU decomposition requires square matrix", GSL_ENOTSQR);
63 }
64 else if (p->sizecxx . . $ gsl_permutation_struct#size. != A->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1.)
65 {
66 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("permutation length must match matrix size", GSL_EBADLEN);
67 }
68 else
69 {
70 const size_t N = A->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1.;
71 size_t i, j, k;
72
73 *signum = 1;
74 gsl_permutation_initcxx . . $ gsl_permutation_init(bfe6c130f63679fa). (p);
75
76 for (j = 0; j < N - 1; j++)
77 {
78 /* Find maximum in the j-th column */
79
80 gsl_complexcxx . . $ gsl_complex# ajj = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (A, j, j);
81 double max = gsl_complex_abscxx . . $ gsl_complex_abs(99532ffcb3ef1144). (ajj);
82 size_t i_pivot = j;
83
84 for (i = j + 1; i < N; i++)
85 {
86 gsl_complexcxx . . $ gsl_complex# aij = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (A, i, j);
87 double ai = gsl_complex_abscxx . . $ gsl_complex_abs(99532ffcb3ef1144). (aij);
88
89 if (ai > max)
90 {
91 max = ai;
92 i_pivot = i;
93 }
94 }
95
96 if (i_pivot != j)
97 {
98 gsl_matrix_complex_swap_rowscxx . . $ gsl_matrix_complex_swap_rows(2f91f8e2e784c5c8). (A, j, i_pivot);
99 gsl_permutation_swapcxx . . $ gsl_permutation_swap(f6414f4542e2521c). (p, j, i_pivot);
100 *signum = -(*signum);
101 }
102
103 ajj = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (A, j, j);
104
105 if (!(GSL_REALcxx . . $ `external/gsl/gsl_complex.h:88:9`!(ajj) == 0.0 && GSL_IMAGcxx . . $ `external/gsl/gsl_complex.h:89:9`!(ajj) == 0.0))
106 {
107 for (i = j + 1; i < N; i++)
108 {
109 gsl_complexcxx . . $ gsl_complex# aij_orig = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (A, i, j);
110 gsl_complexcxx . . $ gsl_complex# aij = gsl_complex_divcxx . . $ gsl_complex_div(97d196ab28e6deec). (aij_orig, ajj);
111 gsl_matrix_complex_setcxx . . $ gsl_matrix_complex_set(2f351991ff7604). (A, i, j, aij);
112
113 for (k = j + 1; k < N; k++)
114 {
115 gsl_complexcxx . . $ gsl_complex# aik = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (A, i, k);
116 gsl_complexcxx . . $ gsl_complex# ajk = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (A, j, k);
117
118 /* aik = aik - aij * ajk */
119
120 gsl_complexcxx . . $ gsl_complex# aijajk = gsl_complex_mulcxx . . $ gsl_complex_mul(97d196ab28e6deec). (aij, ajk);
121 gsl_complexcxx . . $ gsl_complex# aik_new = gsl_complex_subcxx . . $ gsl_complex_sub(97d196ab28e6deec). (aik, aijajk);
122
123 gsl_matrix_complex_setcxx . . $ gsl_matrix_complex_set(2f351991ff7604). (A, i, k, aik_new);
124 }
125 }
126 }
127 }
128
129 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
130 }
131 }
132
133 int
134 gsl_linalg_complex_LU_solvecxx . . $ gsl_linalg_complex_LU_solve(d571d387ddbd51e2). (const gsl_matrix_complexcxx . . $ gsl_matrix_complex# * LU, const gsl_permutationcxx . . $ gsl_permutation# * p, const gsl_vector_complexcxx . . $ gsl_vector_complex# * b, gsl_vector_complexcxx . . $ gsl_vector_complex# * x)
135 {
136 if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != LU->size2cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size2.)
137 {
138 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("LU matrix must be square", GSL_ENOTSQR);
139 }
140 else if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != p->sizecxx . . $ gsl_permutation_struct#size.)
141 {
142 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("permutation length must match matrix size", GSL_EBADLEN);
143 }
144 else if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != b->sizecxx . . $ $anonymous_type_8293e13329784d65_0#size.)
145 {
146 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match b size", GSL_EBADLEN);
147 }
148 else if (LU->size2cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size2. != x->sizecxx . . $ $anonymous_type_8293e13329784d65_0#size.)
149 {
150 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match solution size", GSL_EBADLEN);
151 }
152 else
153 {
154 /* Copy x <- b */
155
156 gsl_vector_complex_memcpycxx . . $ gsl_vector_complex_memcpy(d25704378b556445). (x, b);
157
158 /* Solve for x */
159
160 gsl_linalg_complex_LU_svxcxx . . $ gsl_linalg_complex_LU_svx(79343ddf289dcfc6). (LU, p, x);
161
162 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
163 }
164 }
165
166
167 int
168 gsl_linalg_complex_LU_svxcxx . . $ gsl_linalg_complex_LU_svx(79343ddf289dcfc6). (const gsl_matrix_complexcxx . . $ gsl_matrix_complex# * LU, const gsl_permutationcxx . . $ gsl_permutation# * p, gsl_vector_complexcxx . . $ gsl_vector_complex# * x)
169 {
170 if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != LU->size2cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size2.)
171 {
172 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("LU matrix must be square", GSL_ENOTSQR);
173 }
174 else if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != p->sizecxx . . $ gsl_permutation_struct#size.)
175 {
176 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("permutation length must match matrix size", GSL_EBADLEN);
177 }
178 else if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != x->sizecxx . . $ $anonymous_type_8293e13329784d65_0#size.)
179 {
180 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match solution/rhs size", GSL_EBADLEN);
181 }
182 else
183 {
184 /* Apply permutation to RHS */
185
186 gsl_permute_vector_complexcxx . . $ gsl_permute_vector_complex(d7c59369c412baaa). (p, x);
187
188 /* Solve for c using forward-substitution, L c = P b */
189
190 gsl_blas_ztrsvcxx . . $ gsl_blas_ztrsv(136538f36152c42). (CblasLowercxx . . $ CblasLower., CblasNoTranscxx . . $ CblasNoTrans., CblasUnitcxx . . $ CblasUnit., LU, x);
191
192 /* Perform back-substitution, U x = c */
193
194 gsl_blas_ztrsvcxx . . $ gsl_blas_ztrsv(136538f36152c42). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., LU, x);
195
196 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
197 }
198 }
199
200
201 int
202 gsl_linalg_complex_LU_refinecxx . . $ gsl_linalg_complex_LU_refine(853d77f0c48f4e4a). (const gsl_matrix_complexcxx . . $ gsl_matrix_complex# * A, const gsl_matrix_complexcxx . . $ gsl_matrix_complex# * LU, const gsl_permutationcxx . . $ gsl_permutation# * p, const gsl_vector_complexcxx . . $ gsl_vector_complex# * b, gsl_vector_complexcxx . . $ gsl_vector_complex# * x, gsl_vector_complexcxx . . $ gsl_vector_complex# * residual)
203 {
204 if (A->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != A->size2cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size2.)
205 {
206 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix a must be square", GSL_ENOTSQR);
207 }
208 if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != LU->size2cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size2.)
209 {
210 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("LU matrix must be square", GSL_ENOTSQR);
211 }
212 else if (A->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != LU->size2cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size2.)
213 {
214 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("LU matrix must be decomposition of a", GSL_ENOTSQR);
215 }
216 else if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != p->sizecxx . . $ gsl_permutation_struct#size.)
217 {
218 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("permutation length must match matrix size", GSL_EBADLEN);
219 }
220 else if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != b->sizecxx . . $ $anonymous_type_8293e13329784d65_0#size.)
221 {
222 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match b size", GSL_EBADLEN);
223 }
224 else if (LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1. != x->sizecxx . . $ $anonymous_type_8293e13329784d65_0#size.)
225 {
226 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match solution size", GSL_EBADLEN);
227 }
228 else
229 {
230 /* Compute residual, residual = (A * x - b) */
231
232 gsl_vector_complex_memcpycxx . . $ gsl_vector_complex_memcpy(d25704378b556445). (residual, b);
233
234 {
235 gsl_complexcxx . . $ gsl_complex# one = GSL_COMPLEX_ONEcxx . . $ `external/gsl/gsl_complex_math.h:51:9`!;
236 gsl_complexcxx . . $ gsl_complex# negone = GSL_COMPLEX_NEGONEcxx . . $ `external/gsl/gsl_complex_math.h:53:9`!;
237 gsl_blas_zgemvcxx . . $ gsl_blas_zgemv(478ea939ea106930). (CblasNoTranscxx . . $ CblasNoTrans., one, A, x, negone, residual);
238 }
239
240 /* Find correction, delta = - (A^-1) * residual, and apply it */
241
242 gsl_linalg_complex_LU_svxcxx . . $ gsl_linalg_complex_LU_svx(79343ddf289dcfc6). (LU, p, residual);
243
244 {
245 gsl_complexcxx . . $ gsl_complex# negone= GSL_COMPLEX_NEGONEcxx . . $ `external/gsl/gsl_complex_math.h:53:9`!;
246 gsl_blas_zaxpycxx . . $ gsl_blas_zaxpy(ebd6bc4bc7f4fcc7). (negone, residual, x);
247 }
248
249 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
250 }
251 }
252
253 int
254 gsl_linalg_complex_LU_invertcxx . . $ gsl_linalg_complex_LU_invert(5813dc4e90224213). (const gsl_matrix_complexcxx . . $ gsl_matrix_complex# * LU, const gsl_permutationcxx . . $ gsl_permutation# * p, gsl_matrix_complexcxx . . $ gsl_matrix_complex# * inverse)
255 {
256 size_t i, n = LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1.;
257
258 int status = GSL_SUCCESScxx . . $ GSL_SUCCESS.;
259
260 gsl_matrix_complex_set_identitycxx . . $ gsl_matrix_complex_set_identity(25e1448c5033e5cf). (inverse);
261
262 for (i = 0; i < n; i++)
263 {
264 gsl_vector_complex_viewcxx . . $ gsl_vector_complex_view# c = gsl_matrix_complex_columncxx . . $ gsl_matrix_complex_column(ca35bbd74ed0c6f4). (inverse, i);
265 int status_i = gsl_linalg_complex_LU_svxcxx . . $ gsl_linalg_complex_LU_svx(79343ddf289dcfc6). (LU, p, &(c.vectorcxx . . $ $anonymous_type_8293e13329784d65_1#vector.));
266
267 if (status_i)
268 status = status_i;
269 }
270
271 return status;
272 }
273
274 gsl_complexcxx . . $ gsl_complex#
275 gsl_linalg_complex_LU_detcxx . . $ gsl_linalg_complex_LU_det(f1eb548bca8e82c1). (gsl_matrix_complexcxx . . $ gsl_matrix_complex# * LU, int signum)
276 {
277 size_t i, n = LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1.;
278
279 gsl_complexcxx . . $ gsl_complex# det = gsl_complex_rectcxx . . $ gsl_complex_rect(ce072676009bb5ad).((double) signum, 0.0);
280
281 for (i = 0; i < n; i++)
282 {
283 gsl_complexcxx . . $ gsl_complex# zi = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (LU, i, i);
284 det = gsl_complex_mulcxx . . $ gsl_complex_mul(97d196ab28e6deec). (det, zi);
285 }
286
287 return det;
288 }
289
290
291 double
292 gsl_linalg_complex_LU_lndetcxx . . $ gsl_linalg_complex_LU_lndet(6cd77880b1eea845). (gsl_matrix_complexcxx . . $ gsl_matrix_complex# * LU)
293 {
294 size_t i, n = LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1.;
295
296 double lndet = 0.0;
297
298 for (i = 0; i < n; i++)
299 {
300 gsl_complexcxx . . $ gsl_complex# z = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (LU, i, i);
301 lndet += log (gsl_complex_abscxx . . $ gsl_complex_abs(99532ffcb3ef1144). (z));
302 }
303
304 return lndet;
305 }
306
307
308 gsl_complexcxx . . $ gsl_complex#
309 gsl_linalg_complex_LU_sgndetcxx . . $ gsl_linalg_complex_LU_sgndet(f1eb548bca8e82c1). (gsl_matrix_complexcxx . . $ gsl_matrix_complex# * LU, int signum)
310 {
311 size_t i, n = LU->size1cxx . . $ $anonymous_type_76f4ec40ad1923db_0#size1.;
312
313 gsl_complexcxx . . $ gsl_complex# phase = gsl_complex_rectcxx . . $ gsl_complex_rect(ce072676009bb5ad).((double) signum, 0.0);
314
315 for (i = 0; i < n; i++)
316 {
317 gsl_complexcxx . . $ gsl_complex# z = gsl_matrix_complex_getcxx . . $ gsl_matrix_complex_get(30564a92bc25dfb2). (LU, i, i);
318
319 double r = gsl_complex_abscxx . . $ gsl_complex_abs(99532ffcb3ef1144).(z);
320
321 if (r == 0)
322 {
323 phase = gsl_complex_rectcxx . . $ gsl_complex_rect(ce072676009bb5ad).(0.0, 0.0);
324 break;
325 }
326 else
327 {
328 z = gsl_complex_div_realcxx . . $ gsl_complex_div_real(c6889a7dee95c079).(z, r);
329 phase = gsl_complex_mulcxx . . $ gsl_complex_mul(97d196ab28e6deec).(phase, z);
330 }
331 }
332
333 return phase;
334 }