3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 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.
24 #include <gsl/gsl_test.h>
25 #include <gsl/gsl_math.h>
26 #include <gsl/gsl_ieee_utils.h>
27 #include <gsl/gsl_permute_vector.h>
28 #include <gsl/gsl_blas.h>
29 #include <gsl/gsl_complex_math.h>
30 #include <gsl/gsl_linalg.h>
32 #define TEST_SVD_4X4 1
34 int check (double x, double actual, double eps);
35 gsl_matrix * create_hilbert_matrix(unsigned long size);
36 gsl_matrix * create_general_matrix(unsigned long size1, unsigned long size2);
37 gsl_matrix * create_vandermonde_matrix(unsigned long size);
38 gsl_matrix * create_moler_matrix(unsigned long size);
39 gsl_matrix * create_row_matrix(unsigned long size1, unsigned long size2);
40 gsl_matrix * create_2x2_matrix(double a11, double a12, double a21, double a22);
41 gsl_matrix * create_diagonal_matrix(double a[], unsigned long size);
43 int test_matmult(void);
44 int test_matmult_mod(void);
45 int test_LU_solve_dim(const gsl_matrix * m, const double * actual, double eps);
46 int test_LU_solve(void);
47 int test_LUc_solve_dim(const gsl_matrix_complex * m, const double * actual, double eps);
48 int test_LUc_solve(void);
49 int test_QR_solve_dim(const gsl_matrix * m, const double * actual, double eps);
50 int test_QR_solve(void);
51 int test_QR_QRsolve_dim(const gsl_matrix * m, const double * actual, double eps);
52 int test_QR_QRsolve(void);
53 int test_QR_lssolve_dim(const gsl_matrix * m, const double * actual, double eps);
54 int test_QR_lssolve(void);
55 int test_QR_decomp_dim(const gsl_matrix * m, double eps);
56 int test_QR_decomp(void);
57 int test_QRPT_solve_dim(const gsl_matrix * m, const double * actual, double eps);
58 int test_QRPT_solve(void);
59 int test_QRPT_QRsolve_dim(const gsl_matrix * m, const double * actual, double eps);
60 int test_QRPT_QRsolve(void);
61 int test_QRPT_decomp_dim(const gsl_matrix * m, double eps);
62 int test_QRPT_decomp(void);
63 int test_QR_update_dim(const gsl_matrix * m, double eps);
64 int test_QR_update(void);
66 int test_LQ_solve_dim(const gsl_matrix * m, const double * actual, double eps);
67 int test_LQ_solve(void);
68 int test_LQ_LQsolve_dim(const gsl_matrix * m, const double * actual, double eps);
69 int test_LQ_LQsolve(void);
70 int test_LQ_lssolve_dim(const gsl_matrix * m, const double * actual, double eps);
71 int test_LQ_lssolve(void);
72 int test_LQ_decomp_dim(const gsl_matrix * m, double eps);
73 int test_LQ_decomp(void);
74 int test_PTLQ_solve_dim(const gsl_matrix * m, const double * actual, double eps);
75 int test_PTLQ_solve(void);
76 int test_PTLQ_LQsolve_dim(const gsl_matrix * m, const double * actual, double eps);
77 int test_PTLQ_LQsolve(void);
78 int test_PTLQ_decomp_dim(const gsl_matrix * m, double eps);
79 int test_PTLQ_decomp(void);
80 int test_LQ_update_dim(const gsl_matrix * m, double eps);
81 int test_LQ_update(void);
83 int test_SV_solve_dim(const gsl_matrix * m, const double * actual, double eps);
84 int test_SV_solve(void);
85 int test_SV_decomp_dim(const gsl_matrix * m, double eps);
86 int test_SV_decomp(void);
87 int test_SV_decomp_mod_dim(const gsl_matrix * m, double eps);
88 int test_SV_decomp_mod(void);
89 int test_SV_decomp_jacobi_dim(const gsl_matrix * m, double eps);
90 int test_SV_decomp_jacobi(void);
91 int test_cholesky_solve_dim(const gsl_matrix * m, const double * actual, double eps);
92 int test_cholesky_solve(void);
93 int test_cholesky_decomp_dim(const gsl_matrix * m, double eps);
94 int test_cholesky_decomp(void);
95 int test_HH_solve_dim(const gsl_matrix * m, const double * actual, double eps);
96 int test_HH_solve(void);
97 int test_TDS_solve_dim(unsigned long dim, double d, double od, const double * actual, double eps);
98 int test_TDS_solve(void);
99 int test_TDN_solve_dim(unsigned long dim, double d, double a, double b, const double * actual, double eps);
100 int test_TDN_solve(void);
101 int test_TDS_cyc_solve_one(const unsigned long dim, const double * d, const double * od, const double * r,
102 const double * actual, double eps);
103 int test_TDS_cyc_solve(void);
104 int test_TDN_cyc_solve_dim(unsigned long dim, double d, double a, double b, const double * actual, double eps);
105 int test_TDN_cyc_solve(void);
106 int test_bidiag_decomp_dim(const gsl_matrix * m, double eps);
107 int test_bidiag_decomp(void);
110 check (double x, double actual, double eps)
116 else if (actual == 0)
118 return fabs(x) > eps;
122 return (fabs(x - actual)/fabs(actual)) > eps;
127 create_hilbert_matrix(unsigned long size)
130 gsl_matrix * m = gsl_matrix_alloc(size, size);
131 for(i=0; i<size; i++) {
132 for(j=0; j<size; j++) {
133 gsl_matrix_set(m, i, j, 1.0/(i+j+1.0));
140 create_general_matrix(unsigned long size1, unsigned long size2)
143 gsl_matrix * m = gsl_matrix_alloc(size1, size2);
144 for(i=0; i<size1; i++) {
145 for(j=0; j<size2; j++) {
146 gsl_matrix_set(m, i, j, 1.0/(i+j+1.0));
153 create_singular_matrix(unsigned long size1, unsigned long size2)
156 gsl_matrix * m = gsl_matrix_alloc(size1, size2);
157 for(i=0; i<size1; i++) {
158 for(j=0; j<size2; j++) {
159 gsl_matrix_set(m, i, j, 1.0/(i+j+1.0));
163 /* zero the first column */
164 for(j = 0; j <size2; j++)
165 gsl_matrix_set(m,0,j,0.0);
172 create_vandermonde_matrix(unsigned long size)
175 gsl_matrix * m = gsl_matrix_alloc(size, size);
176 for(i=0; i<size; i++) {
177 for(j=0; j<size; j++) {
178 gsl_matrix_set(m, i, j, pow(i + 1.0, size - j - 1.0));
185 create_moler_matrix(unsigned long size)
188 gsl_matrix * m = gsl_matrix_alloc(size, size);
189 for(i=0; i<size; i++) {
190 for(j=0; j<size; j++) {
191 gsl_matrix_set(m, i, j, GSL_MIN(i+1,j+1)-2.0);
198 create_complex_matrix(unsigned long size)
201 gsl_matrix_complex * m = gsl_matrix_complex_alloc(size, size);
202 for(i=0; i<size; i++) {
203 for(j=0; j<size; j++) {
204 gsl_complex z = gsl_complex_rect(1.0/(i+j+1.0), 1/(i*i+j*j+0.5));
205 gsl_matrix_complex_set(m, i, j, z);
212 create_row_matrix(unsigned long size1, unsigned long size2)
215 gsl_matrix * m = gsl_matrix_calloc(size1, size2);
216 for(i=0; i<size1; i++) {
217 gsl_matrix_set(m, i, 0, 1.0/(i+1.0));
224 create_2x2_matrix(double a11, double a12, double a21, double a22)
226 gsl_matrix * m = gsl_matrix_alloc(2, 2);
227 gsl_matrix_set(m, 0, 0, a11);
228 gsl_matrix_set(m, 0, 1, a12);
229 gsl_matrix_set(m, 1, 0, a21);
230 gsl_matrix_set(m, 1, 1, a22);
235 create_diagonal_matrix(double a[], unsigned long size)
238 gsl_matrix * m = gsl_matrix_calloc(size, size);
239 for(i=0; i<size; i++) {
240 gsl_matrix_set(m, i, i, a[i]);
270 gsl_matrix_complex * c7;
272 gsl_matrix * inf5; double inf5_data[] = {1.0, 0.0, -3.0, 0.0, -5.0};
274 gsl_matrix * dblmin3, * dblmin5;
276 double m53_lssolution[] = {52.5992295702070, -337.7263113752073,
278 double hilb2_solution[] = {-8.0, 18.0} ;
279 double hilb3_solution[] = {27.0, -192.0, 210.0};
280 double hilb4_solution[] = {-64.0, 900.0, -2520.0, 1820.0};
281 double hilb12_solution[] = {-1728.0, 245388.0, -8528520.0,
282 127026900.0, -1009008000.0, 4768571808.0,
283 -14202796608.0, 27336497760.0, -33921201600.0,
284 26189163000.0, -11437874448.0, 2157916488.0 };
286 double c7_solution[] = { 2.40717272023734e+01, -9.84612797621247e+00,
287 -2.69338853034031e+02, 8.75455232472528e+01,
288 2.96661356736296e+03, -1.02624473923993e+03,
289 -1.82073812124749e+04, 5.67384473042410e+03,
290 5.57693879019068e+04, -1.61540963210502e+04,
291 -7.88941207561151e+04, 1.95053812987858e+04,
292 3.95548551241728e+04, -7.76593696255317e+03 };
294 gsl_matrix * vander2;
295 gsl_matrix * vander3;
296 gsl_matrix * vander4;
297 gsl_matrix * vander12;
299 double vander2_solution[] = {1.0, 0.0};
300 double vander3_solution[] = {0.0, 1.0, 0.0};
301 double vander4_solution[] = {0.0, 0.0, 1.0, 0.0};
302 double vander12_solution[] = {0.0, 0.0, 0.0, 0.0,
306 gsl_matrix * moler10;
308 /* matmult now obsolete */
315 gsl_matrix * A = gsl_matrix_calloc(2, 2);
316 gsl_matrix * B = gsl_matrix_calloc(2, 3);
317 gsl_matrix * C = gsl_matrix_calloc(2, 3);
319 gsl_matrix_set(A, 0, 0, 10.0);
320 gsl_matrix_set(A, 0, 1, 5.0);
321 gsl_matrix_set(A, 1, 0, 1.0);
322 gsl_matrix_set(A, 1, 1, 20.0);
324 gsl_matrix_set(B, 0, 0, 10.0);
325 gsl_matrix_set(B, 0, 1, 5.0);
326 gsl_matrix_set(B, 0, 2, 2.0);
327 gsl_matrix_set(B, 1, 0, 1.0);
328 gsl_matrix_set(B, 1, 1, 3.0);
329 gsl_matrix_set(B, 1, 2, 2.0);
331 gsl_linalg_matmult(A, B, C);
333 s += ( fabs(gsl_matrix_get(C, 0, 0) - 105.0) > GSL_DBL_EPSILON );
334 s += ( fabs(gsl_matrix_get(C, 0, 1) - 65.0) > GSL_DBL_EPSILON );
335 s += ( fabs(gsl_matrix_get(C, 0, 2) - 30.0) > GSL_DBL_EPSILON );
336 s += ( fabs(gsl_matrix_get(C, 1, 0) - 30.0) > GSL_DBL_EPSILON );
337 s += ( fabs(gsl_matrix_get(C, 1, 1) - 65.0) > GSL_DBL_EPSILON );
338 s += ( fabs(gsl_matrix_get(C, 1, 2) - 42.0) > GSL_DBL_EPSILON );
349 test_matmult_mod(void)
353 gsl_matrix * A = gsl_matrix_calloc(3, 3);
354 gsl_matrix * B = gsl_matrix_calloc(3, 3);
355 gsl_matrix * C = gsl_matrix_calloc(3, 3);
356 gsl_matrix * D = gsl_matrix_calloc(2, 3);
357 gsl_matrix * E = gsl_matrix_calloc(2, 3);
358 gsl_matrix * F = gsl_matrix_calloc(2, 2);
360 gsl_matrix_set(A, 0, 0, 10.0);
361 gsl_matrix_set(A, 0, 1, 5.0);
362 gsl_matrix_set(A, 0, 2, 1.0);
363 gsl_matrix_set(A, 1, 0, 1.0);
364 gsl_matrix_set(A, 1, 1, 20.0);
365 gsl_matrix_set(A, 1, 2, 5.0);
366 gsl_matrix_set(A, 2, 0, 1.0);
367 gsl_matrix_set(A, 2, 1, 3.0);
368 gsl_matrix_set(A, 2, 2, 7.0);
370 gsl_matrix_set(B, 0, 0, 10.0);
371 gsl_matrix_set(B, 0, 1, 5.0);
372 gsl_matrix_set(B, 0, 2, 2.0);
373 gsl_matrix_set(B, 1, 0, 1.0);
374 gsl_matrix_set(B, 1, 1, 3.0);
375 gsl_matrix_set(B, 1, 2, 2.0);
376 gsl_matrix_set(B, 2, 0, 1.0);
377 gsl_matrix_set(B, 2, 1, 3.0);
378 gsl_matrix_set(B, 2, 2, 2.0);
380 gsl_matrix_set(D, 0, 0, 10.0);
381 gsl_matrix_set(D, 0, 1, 5.0);
382 gsl_matrix_set(D, 0, 2, 1.0);
383 gsl_matrix_set(D, 1, 0, 1.0);
384 gsl_matrix_set(D, 1, 1, 20.0);
385 gsl_matrix_set(D, 1, 2, 5.0);
387 gsl_matrix_set(E, 0, 0, 10.0);
388 gsl_matrix_set(E, 0, 1, 5.0);
389 gsl_matrix_set(E, 0, 2, 2.0);
390 gsl_matrix_set(E, 1, 0, 1.0);
391 gsl_matrix_set(E, 1, 1, 3.0);
392 gsl_matrix_set(E, 1, 2, 2.0);
394 gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_NONE, B, GSL_LINALG_MOD_NONE, C);
395 s += ( fabs(gsl_matrix_get(C, 0, 0) - 106.0) > GSL_DBL_EPSILON );
396 s += ( fabs(gsl_matrix_get(C, 0, 1) - 68.0) > GSL_DBL_EPSILON );
397 s += ( fabs(gsl_matrix_get(C, 0, 2) - 32.0) > GSL_DBL_EPSILON );
398 s += ( fabs(gsl_matrix_get(C, 1, 0) - 35.0) > GSL_DBL_EPSILON );
399 s += ( fabs(gsl_matrix_get(C, 1, 1) - 80.0) > GSL_DBL_EPSILON );
400 s += ( fabs(gsl_matrix_get(C, 1, 2) - 52.0) > GSL_DBL_EPSILON );
401 s += ( fabs(gsl_matrix_get(C, 2, 0) - 20.0) > GSL_DBL_EPSILON );
402 s += ( fabs(gsl_matrix_get(C, 2, 1) - 35.0) > GSL_DBL_EPSILON );
403 s += ( fabs(gsl_matrix_get(C, 2, 2) - 22.0) > GSL_DBL_EPSILON );
405 gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_TRANSPOSE, B, GSL_LINALG_MOD_NONE, C);
406 s += ( fabs(gsl_matrix_get(C, 0, 0) - 102.0) > GSL_DBL_EPSILON );
407 s += ( fabs(gsl_matrix_get(C, 0, 1) - 56.0) > GSL_DBL_EPSILON );
408 s += ( fabs(gsl_matrix_get(C, 0, 2) - 24.0) > GSL_DBL_EPSILON );
409 s += ( fabs(gsl_matrix_get(C, 1, 0) - 73.0) > GSL_DBL_EPSILON );
410 s += ( fabs(gsl_matrix_get(C, 1, 1) - 94.0) > GSL_DBL_EPSILON );
411 s += ( fabs(gsl_matrix_get(C, 1, 2) - 56.0) > GSL_DBL_EPSILON );
412 s += ( fabs(gsl_matrix_get(C, 2, 0) - 22.0) > GSL_DBL_EPSILON );
413 s += ( fabs(gsl_matrix_get(C, 2, 1) - 41.0) > GSL_DBL_EPSILON );
414 s += ( fabs(gsl_matrix_get(C, 2, 2) - 26.0) > GSL_DBL_EPSILON );
416 gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_NONE, B, GSL_LINALG_MOD_TRANSPOSE, C);
417 s += ( fabs(gsl_matrix_get(C, 0, 0) - 127.0) > GSL_DBL_EPSILON );
418 s += ( fabs(gsl_matrix_get(C, 0, 1) - 27.0) > GSL_DBL_EPSILON );
419 s += ( fabs(gsl_matrix_get(C, 0, 2) - 27.0) > GSL_DBL_EPSILON );
420 s += ( fabs(gsl_matrix_get(C, 1, 0) - 120.0) > GSL_DBL_EPSILON );
421 s += ( fabs(gsl_matrix_get(C, 1, 1) - 71.0) > GSL_DBL_EPSILON );
422 s += ( fabs(gsl_matrix_get(C, 1, 2) - 71.0) > GSL_DBL_EPSILON );
423 s += ( fabs(gsl_matrix_get(C, 2, 0) - 39.0) > GSL_DBL_EPSILON );
424 s += ( fabs(gsl_matrix_get(C, 2, 1) - 24.0) > GSL_DBL_EPSILON );
425 s += ( fabs(gsl_matrix_get(C, 2, 2) - 24.0) > GSL_DBL_EPSILON );
427 gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_TRANSPOSE, B, GSL_LINALG_MOD_TRANSPOSE, C);
428 s += ( fabs(gsl_matrix_get(C, 0, 0) - 107.0) > GSL_DBL_EPSILON );
429 s += ( fabs(gsl_matrix_get(C, 0, 1) - 15.0) > GSL_DBL_EPSILON );
430 s += ( fabs(gsl_matrix_get(C, 0, 2) - 15.0) > GSL_DBL_EPSILON );
431 s += ( fabs(gsl_matrix_get(C, 1, 0) - 156.0) > GSL_DBL_EPSILON );
432 s += ( fabs(gsl_matrix_get(C, 1, 1) - 71.0) > GSL_DBL_EPSILON );
433 s += ( fabs(gsl_matrix_get(C, 1, 2) - 71.0) > GSL_DBL_EPSILON );
434 s += ( fabs(gsl_matrix_get(C, 2, 0) - 49.0) > GSL_DBL_EPSILON );
435 s += ( fabs(gsl_matrix_get(C, 2, 1) - 30.0) > GSL_DBL_EPSILON );
436 s += ( fabs(gsl_matrix_get(C, 2, 2) - 30.0) > GSL_DBL_EPSILON );
438 /* now try for non-symmetric matrices */
439 gsl_linalg_matmult_mod(D, GSL_LINALG_MOD_TRANSPOSE, E, GSL_LINALG_MOD_NONE, C);
440 s += ( fabs(gsl_matrix_get(C, 0, 0) - 101.0) > GSL_DBL_EPSILON );
441 s += ( fabs(gsl_matrix_get(C, 0, 1) - 53.0) > GSL_DBL_EPSILON );
442 s += ( fabs(gsl_matrix_get(C, 0, 2) - 22.0) > GSL_DBL_EPSILON );
443 s += ( fabs(gsl_matrix_get(C, 1, 0) - 70.0) > GSL_DBL_EPSILON );
444 s += ( fabs(gsl_matrix_get(C, 1, 1) - 85.0) > GSL_DBL_EPSILON );
445 s += ( fabs(gsl_matrix_get(C, 1, 2) - 50.0) > GSL_DBL_EPSILON );
446 s += ( fabs(gsl_matrix_get(C, 2, 0) - 15.0) > GSL_DBL_EPSILON );
447 s += ( fabs(gsl_matrix_get(C, 2, 1) - 20.0) > GSL_DBL_EPSILON );
448 s += ( fabs(gsl_matrix_get(C, 2, 2) - 12.0) > GSL_DBL_EPSILON );
451 gsl_linalg_matmult_mod(D, GSL_LINALG_MOD_NONE, E, GSL_LINALG_MOD_TRANSPOSE, F);
452 s += ( fabs(gsl_matrix_get(F, 0, 0) - 127.0) > GSL_DBL_EPSILON );
453 s += ( fabs(gsl_matrix_get(F, 0, 1) - 27.0) > GSL_DBL_EPSILON );
454 s += ( fabs(gsl_matrix_get(F, 1, 0) - 120.0) > GSL_DBL_EPSILON );
455 s += ( fabs(gsl_matrix_get(F, 1, 1) - 71.0) > GSL_DBL_EPSILON );
470 test_LU_solve_dim(const gsl_matrix * m, const double * actual, double eps)
474 unsigned long i, dim = m->size1;
476 gsl_permutation * perm = gsl_permutation_alloc(dim);
477 gsl_vector * rhs = gsl_vector_alloc(dim);
478 gsl_matrix * lu = gsl_matrix_alloc(dim,dim);
479 gsl_vector * x = gsl_vector_alloc(dim);
480 gsl_vector * residual = gsl_vector_alloc(dim);
481 gsl_matrix_memcpy(lu,m);
482 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
483 s += gsl_linalg_LU_decomp(lu, perm, &signum);
484 s += gsl_linalg_LU_solve(lu, perm, rhs, x);
486 for(i=0; i<dim; i++) {
487 int foo = check(gsl_vector_get(x, i),actual[i],eps);
489 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
494 s += gsl_linalg_LU_refine(m, lu, perm, rhs, x, residual);
496 for(i=0; i<dim; i++) {
497 int foo = check(gsl_vector_get(x, i),actual[i],eps);
499 printf("%3lu[%lu]: %22.18g %22.18g (improved)\n", dim, i, gsl_vector_get(x, i), actual[i]);
504 gsl_vector_free(residual);
507 gsl_vector_free(rhs);
508 gsl_permutation_free(perm);
514 int test_LU_solve(void)
519 f = test_LU_solve_dim(hilb2, hilb2_solution, 8.0 * GSL_DBL_EPSILON);
520 gsl_test(f, " LU_solve hilbert(2)");
523 f = test_LU_solve_dim(hilb3, hilb3_solution, 64.0 * GSL_DBL_EPSILON);
524 gsl_test(f, " LU_solve hilbert(3)");
527 f = test_LU_solve_dim(hilb4, hilb4_solution, 2048.0 * GSL_DBL_EPSILON);
528 gsl_test(f, " LU_solve hilbert(4)");
531 f = test_LU_solve_dim(hilb12, hilb12_solution, 0.5);
532 gsl_test(f, " LU_solve hilbert(12)");
535 f = test_LU_solve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
536 gsl_test(f, " LU_solve vander(2)");
539 f = test_LU_solve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
540 gsl_test(f, " LU_solve vander(3)");
543 f = test_LU_solve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
544 gsl_test(f, " LU_solve vander(4)");
547 f = test_LU_solve_dim(vander12, vander12_solution, 0.05);
548 gsl_test(f, " LU_solve vander(12)");
556 test_LUc_solve_dim(const gsl_matrix_complex * m, const double * actual, double eps)
560 unsigned long i, dim = m->size1;
562 gsl_permutation * perm = gsl_permutation_alloc(dim);
563 gsl_vector_complex * rhs = gsl_vector_complex_alloc(dim);
564 gsl_matrix_complex * lu = gsl_matrix_complex_alloc(dim,dim);
565 gsl_vector_complex * x = gsl_vector_complex_alloc(dim);
566 gsl_vector_complex * residual = gsl_vector_complex_alloc(dim);
567 gsl_matrix_complex_memcpy(lu,m);
570 gsl_complex z = gsl_complex_rect (2.0*i+1.0, 2.0*i+2.0);
571 gsl_vector_complex_set(rhs, i, z);
573 s += gsl_linalg_complex_LU_decomp(lu, perm, &signum);
574 s += gsl_linalg_complex_LU_solve(lu, perm, rhs, x);
576 for(i=0; i<dim; i++) {
577 gsl_complex z = gsl_vector_complex_get(x, i);
578 int foo_r = check(GSL_REAL(z),actual[2*i],eps);
579 int foo_i = check(GSL_IMAG(z),actual[2*i+1],eps);
581 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, GSL_REAL(z), actual[2*i]);
582 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, GSL_IMAG(z), actual[2*i+1]);
587 s += gsl_linalg_complex_LU_refine(m, lu, perm, rhs, x, residual);
589 for(i=0; i<dim; i++) {
590 gsl_complex z = gsl_vector_complex_get(x, i);
591 int foo_r = check(GSL_REAL(z),actual[2*i],eps);
592 int foo_i = check(GSL_IMAG(z),actual[2*i+1],eps);
594 printf("%3lu[%lu]: %22.18g %22.18g (improved)\n", dim, i, GSL_REAL(z), actual[2*i]);
595 printf("%3lu[%lu]: %22.18g %22.18g (improved)\n", dim, i, GSL_IMAG(z), actual[2*i+1]);
600 gsl_vector_complex_free(residual);
601 gsl_vector_complex_free(x);
602 gsl_matrix_complex_free(lu);
603 gsl_vector_complex_free(rhs);
604 gsl_permutation_free(perm);
610 int test_LUc_solve(void)
615 f = test_LUc_solve_dim(c7, c7_solution, 1024.0 * 1024.0 * GSL_DBL_EPSILON);
616 gsl_test(f, " complex_LU_solve complex(7)");
624 test_QR_solve_dim(const gsl_matrix * m, const double * actual, double eps)
627 unsigned long i, dim = m->size1;
629 gsl_vector * rhs = gsl_vector_alloc(dim);
630 gsl_matrix * qr = gsl_matrix_alloc(dim,dim);
631 gsl_vector * d = gsl_vector_alloc(dim);
632 gsl_vector * x = gsl_vector_alloc(dim);
634 gsl_matrix_memcpy(qr,m);
635 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
636 s += gsl_linalg_QR_decomp(qr, d);
637 s += gsl_linalg_QR_solve(qr, d, rhs, x);
638 for(i=0; i<dim; i++) {
639 int foo = check(gsl_vector_get(x, i), actual[i], eps);
641 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
649 gsl_vector_free(rhs);
654 int test_QR_solve(void)
659 f = test_QR_solve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
660 gsl_test(f, " QR_solve hilbert(2)");
663 f = test_QR_solve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
664 gsl_test(f, " QR_solve hilbert(3)");
667 f = test_QR_solve_dim(hilb4, hilb4_solution, 2 * 1024.0 * GSL_DBL_EPSILON);
668 gsl_test(f, " QR_solve hilbert(4)");
671 f = test_QR_solve_dim(hilb12, hilb12_solution, 0.5);
672 gsl_test(f, " QR_solve hilbert(12)");
675 f = test_QR_solve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
676 gsl_test(f, " QR_solve vander(2)");
679 f = test_QR_solve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
680 gsl_test(f, " QR_solve vander(3)");
683 f = test_QR_solve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
684 gsl_test(f, " QR_solve vander(4)");
687 f = test_QR_solve_dim(vander12, vander12_solution, 0.05);
688 gsl_test(f, " QR_solve vander(12)");
696 test_QR_QRsolve_dim(const gsl_matrix * m, const double * actual, double eps)
699 unsigned long i, dim = m->size1;
701 gsl_vector * rhs = gsl_vector_alloc(dim);
702 gsl_matrix * qr = gsl_matrix_alloc(dim,dim);
703 gsl_matrix * q = gsl_matrix_alloc(dim,dim);
704 gsl_matrix * r = gsl_matrix_alloc(dim,dim);
705 gsl_vector * d = gsl_vector_alloc(dim);
706 gsl_vector * x = gsl_vector_alloc(dim);
708 gsl_matrix_memcpy(qr,m);
709 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
710 s += gsl_linalg_QR_decomp(qr, d);
711 s += gsl_linalg_QR_unpack(qr, d, q, r);
712 s += gsl_linalg_QR_QRsolve(q, r, rhs, x);
713 for(i=0; i<dim; i++) {
714 int foo = check(gsl_vector_get(x, i), actual[i], eps);
716 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
726 gsl_vector_free(rhs);
730 int test_QR_QRsolve(void)
735 f = test_QR_QRsolve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
736 gsl_test(f, " QR_QRsolve hilbert(2)");
739 f = test_QR_QRsolve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
740 gsl_test(f, " QR_QRsolve hilbert(3)");
743 f = test_QR_QRsolve_dim(hilb4, hilb4_solution, 2 * 1024.0 * GSL_DBL_EPSILON);
744 gsl_test(f, " QR_QRsolve hilbert(4)");
747 f = test_QR_QRsolve_dim(hilb12, hilb12_solution, 0.5);
748 gsl_test(f, " QR_QRsolve hilbert(12)");
751 f = test_QR_QRsolve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
752 gsl_test(f, " QR_QRsolve vander(2)");
755 f = test_QR_QRsolve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
756 gsl_test(f, " QR_QRsolve vander(3)");
759 f = test_QR_QRsolve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
760 gsl_test(f, " QR_QRsolve vander(4)");
763 f = test_QR_QRsolve_dim(vander12, vander12_solution, 0.05);
764 gsl_test(f, " QR_QRsolve vander(12)");
772 test_QR_lssolve_dim(const gsl_matrix * m, const double * actual, double eps)
775 unsigned long i, M = m->size1, N = m->size2;
777 gsl_vector * rhs = gsl_vector_alloc(M);
778 gsl_matrix * qr = gsl_matrix_alloc(M,N);
779 gsl_vector * d = gsl_vector_alloc(N);
780 gsl_vector * x = gsl_vector_alloc(N);
781 gsl_vector * r = gsl_vector_alloc(M);
782 gsl_vector * res = gsl_vector_alloc(M);
784 gsl_matrix_memcpy(qr,m);
785 for(i=0; i<M; i++) gsl_vector_set(rhs, i, i+1.0);
786 s += gsl_linalg_QR_decomp(qr, d);
787 s += gsl_linalg_QR_lssolve(qr, d, rhs, x, res);
790 int foo = check(gsl_vector_get(x, i), actual[i], eps);
792 printf("(%3lu,%3lu)[%lu]: %22.18g %22.18g\n", M, N, i, gsl_vector_get(x, i), actual[i]);
797 /* compute residual r = b - m x */
799 gsl_vector_set_zero(r);
801 gsl_vector_memcpy(r, rhs);
802 gsl_blas_dgemv(CblasNoTrans, -1.0, m, x, 1.0, r);
806 int foo = check(gsl_vector_get(res, i), gsl_vector_get(r,i), sqrt(eps));
808 printf("(%3lu,%3lu)[%lu]: %22.18g %22.18g\n", M, N, i, gsl_vector_get(res, i), gsl_vector_get(r,i));
814 gsl_vector_free(res);
818 gsl_vector_free(rhs);
823 int test_QR_lssolve(void)
828 f = test_QR_lssolve_dim(m53, m53_lssolution, 2 * 64.0 * GSL_DBL_EPSILON);
829 gsl_test(f, " QR_lssolve m(5,3)");
832 f = test_QR_lssolve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
833 gsl_test(f, " QR_lssolve hilbert(2)");
836 f = test_QR_lssolve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
837 gsl_test(f, " QR_lssolve hilbert(3)");
840 f = test_QR_lssolve_dim(hilb4, hilb4_solution, 2 * 1024.0 * GSL_DBL_EPSILON);
841 gsl_test(f, " QR_lssolve hilbert(4)");
844 f = test_QR_lssolve_dim(hilb12, hilb12_solution, 0.5);
845 gsl_test(f, " QR_lssolve hilbert(12)");
848 f = test_QR_lssolve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
849 gsl_test(f, " QR_lssolve vander(2)");
852 f = test_QR_lssolve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
853 gsl_test(f, " QR_lssolve vander(3)");
856 f = test_QR_lssolve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
857 gsl_test(f, " QR_lssolve vander(4)");
860 f = test_QR_lssolve_dim(vander12, vander12_solution, 0.05);
861 gsl_test(f, " QR_lssolve vander(12)");
869 test_QR_decomp_dim(const gsl_matrix * m, double eps)
872 unsigned long i,j, M = m->size1, N = m->size2;
874 gsl_matrix * qr = gsl_matrix_alloc(M,N);
875 gsl_matrix * a = gsl_matrix_alloc(M,N);
876 gsl_matrix * q = gsl_matrix_alloc(M,M);
877 gsl_matrix * r = gsl_matrix_alloc(M,N);
878 gsl_vector * d = gsl_vector_alloc(GSL_MIN(M,N));
880 gsl_matrix_memcpy(qr,m);
882 s += gsl_linalg_QR_decomp(qr, d);
883 s += gsl_linalg_QR_unpack(qr, d, q, r);
885 /* compute a = q r */
886 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, q, r, 0.0, a);
890 double aij = gsl_matrix_get(a, i, j);
891 double mij = gsl_matrix_get(m, i, j);
892 int foo = check(aij, mij, eps);
894 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
909 int test_QR_decomp(void)
914 f = test_QR_decomp_dim(m35, 2 * 8.0 * GSL_DBL_EPSILON);
915 gsl_test(f, " QR_decomp m(3,5)");
918 f = test_QR_decomp_dim(m53, 2 * 64.0 * GSL_DBL_EPSILON);
919 gsl_test(f, " QR_decomp m(5,3)");
922 f = test_QR_decomp_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
923 gsl_test(f, " QR_decomp hilbert(2)");
926 f = test_QR_decomp_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
927 gsl_test(f, " QR_decomp hilbert(3)");
930 f = test_QR_decomp_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
931 gsl_test(f, " QR_decomp hilbert(4)");
934 f = test_QR_decomp_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
935 gsl_test(f, " QR_decomp hilbert(12)");
938 f = test_QR_decomp_dim(vander2, 8.0 * GSL_DBL_EPSILON);
939 gsl_test(f, " QR_decomp vander(2)");
942 f = test_QR_decomp_dim(vander3, 64.0 * GSL_DBL_EPSILON);
943 gsl_test(f, " QR_decomp vander(3)");
946 f = test_QR_decomp_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
947 gsl_test(f, " QR_decomp vander(4)");
950 f = test_QR_decomp_dim(vander12, 0.0005); /* FIXME: bad accuracy */
951 gsl_test(f, " QR_decomp vander(12)");
958 test_QRPT_solve_dim(const gsl_matrix * m, const double * actual, double eps)
962 unsigned long i, dim = m->size1;
964 gsl_permutation * perm = gsl_permutation_alloc(dim);
965 gsl_vector * rhs = gsl_vector_alloc(dim);
966 gsl_matrix * qr = gsl_matrix_alloc(dim,dim);
967 gsl_vector * d = gsl_vector_alloc(dim);
968 gsl_vector * x = gsl_vector_alloc(dim);
969 gsl_vector * norm = gsl_vector_alloc(dim);
971 gsl_matrix_memcpy(qr,m);
972 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
973 s += gsl_linalg_QRPT_decomp(qr, d, perm, &signum, norm);
974 s += gsl_linalg_QRPT_solve(qr, d, perm, rhs, x);
975 for(i=0; i<dim; i++) {
976 int foo = check(gsl_vector_get(x, i), actual[i], eps);
978 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
983 gsl_vector_free(norm);
987 gsl_vector_free(rhs);
988 gsl_permutation_free(perm);
993 int test_QRPT_solve(void)
998 f = test_QRPT_solve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
999 gsl_test(f, " QRPT_solve hilbert(2)");
1002 f = test_QRPT_solve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
1003 gsl_test(f, " QRPT_solve hilbert(3)");
1006 f = test_QRPT_solve_dim(hilb4, hilb4_solution, 2 * 2048.0 * GSL_DBL_EPSILON);
1007 gsl_test(f, " QRPT_solve hilbert(4)");
1010 f = test_QRPT_solve_dim(hilb12, hilb12_solution, 0.5);
1011 gsl_test(f, " QRPT_solve hilbert(12)");
1014 f = test_QRPT_solve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
1015 gsl_test(f, " QRPT_solve vander(2)");
1018 f = test_QRPT_solve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
1019 gsl_test(f, " QRPT_solve vander(3)");
1022 f = test_QRPT_solve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
1023 gsl_test(f, " QRPT_solve vander(4)");
1026 f = test_QRPT_solve_dim(vander12, vander12_solution, 0.05);
1027 gsl_test(f, " QRPT_solve vander(12)");
1034 test_QRPT_QRsolve_dim(const gsl_matrix * m, const double * actual, double eps)
1038 unsigned long i, dim = m->size1;
1040 gsl_permutation * perm = gsl_permutation_alloc(dim);
1041 gsl_vector * rhs = gsl_vector_alloc(dim);
1042 gsl_matrix * qr = gsl_matrix_alloc(dim,dim);
1043 gsl_matrix * q = gsl_matrix_alloc(dim,dim);
1044 gsl_matrix * r = gsl_matrix_alloc(dim,dim);
1045 gsl_vector * d = gsl_vector_alloc(dim);
1046 gsl_vector * x = gsl_vector_alloc(dim);
1047 gsl_vector * norm = gsl_vector_alloc(dim);
1049 gsl_matrix_memcpy(qr,m);
1050 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
1051 s += gsl_linalg_QRPT_decomp2(qr, q, r, d, perm, &signum, norm);
1052 s += gsl_linalg_QRPT_QRsolve(q, r, perm, rhs, x);
1053 for(i=0; i<dim; i++) {
1054 int foo = check(gsl_vector_get(x, i), actual[i], eps);
1056 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
1061 gsl_vector_free(norm);
1064 gsl_matrix_free(qr);
1067 gsl_vector_free(rhs);
1068 gsl_permutation_free(perm);
1073 int test_QRPT_QRsolve(void)
1078 f = test_QRPT_QRsolve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
1079 gsl_test(f, " QRPT_QRsolve hilbert(2)");
1082 f = test_QRPT_QRsolve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
1083 gsl_test(f, " QRPT_QRsolve hilbert(3)");
1086 f = test_QRPT_QRsolve_dim(hilb4, hilb4_solution, 2 * 2048.0 * GSL_DBL_EPSILON);
1087 gsl_test(f, " QRPT_QRsolve hilbert(4)");
1090 f = test_QRPT_QRsolve_dim(hilb12, hilb12_solution, 0.5);
1091 gsl_test(f, " QRPT_QRsolve hilbert(12)");
1094 f = test_QRPT_QRsolve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
1095 gsl_test(f, " QRPT_QRsolve vander(2)");
1098 f = test_QRPT_QRsolve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
1099 gsl_test(f, " QRPT_QRsolve vander(3)");
1102 f = test_QRPT_QRsolve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
1103 gsl_test(f, " QRPT_QRsolve vander(4)");
1106 f = test_QRPT_QRsolve_dim(vander12, vander12_solution, 0.05);
1107 gsl_test(f, " QRPT_QRsolve vander(12)");
1114 test_QRPT_decomp_dim(const gsl_matrix * m, double eps)
1117 unsigned long i,j, M = m->size1, N = m->size2;
1119 gsl_matrix * qr = gsl_matrix_alloc(M,N);
1120 gsl_matrix * a = gsl_matrix_alloc(M,N);
1121 gsl_matrix * q = gsl_matrix_alloc(M,M);
1122 gsl_matrix * r = gsl_matrix_alloc(M,N);
1123 gsl_vector * d = gsl_vector_alloc(GSL_MIN(M,N));
1124 gsl_vector * norm = gsl_vector_alloc(N);
1126 gsl_permutation * perm = gsl_permutation_alloc(N);
1128 gsl_matrix_memcpy(qr,m);
1130 s += gsl_linalg_QRPT_decomp(qr, d, perm, &signum, norm);
1131 s += gsl_linalg_QR_unpack(qr, d, q, r);
1133 /* compute a = q r */
1134 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, q, r, 0.0, a);
1137 /* Compute QR P^T by permuting the elements of the rows of QR */
1139 for (i = 0; i < M; i++) {
1140 gsl_vector_view row = gsl_matrix_row (a, i);
1141 gsl_permute_vector_inverse (perm, &row.vector);
1144 for(i=0; i<M; i++) {
1145 for(j=0; j<N; j++) {
1146 double aij = gsl_matrix_get(a, i, j);
1147 double mij = gsl_matrix_get(m, i, j);
1148 int foo = check(aij, mij, eps);
1150 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
1156 gsl_permutation_free (perm);
1157 gsl_vector_free(norm);
1159 gsl_matrix_free(qr);
1167 int test_QRPT_decomp(void)
1172 f = test_QRPT_decomp_dim(m35, 2 * 8.0 * GSL_DBL_EPSILON);
1173 gsl_test(f, " QRPT_decomp m(3,5)");
1176 f = test_QRPT_decomp_dim(m53, 2 * 8.0 * GSL_DBL_EPSILON);
1177 gsl_test(f, " QRPT_decomp m(5,3)");
1180 f = test_QRPT_decomp_dim(s35, 2 * 8.0 * GSL_DBL_EPSILON);
1181 gsl_test(f, " QRPT_decomp s(3,5)");
1184 f = test_QRPT_decomp_dim(s53, 2 * 8.0 * GSL_DBL_EPSILON);
1185 gsl_test(f, " QRPT_decomp s(5,3)");
1188 f = test_QRPT_decomp_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
1189 gsl_test(f, " QRPT_decomp hilbert(2)");
1192 f = test_QRPT_decomp_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
1193 gsl_test(f, " QRPT_decomp hilbert(3)");
1196 f = test_QRPT_decomp_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
1197 gsl_test(f, " QRPT_decomp hilbert(4)");
1200 f = test_QRPT_decomp_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
1201 gsl_test(f, " QRPT_decomp hilbert(12)");
1204 f = test_QRPT_decomp_dim(vander2, 8.0 * GSL_DBL_EPSILON);
1205 gsl_test(f, " QRPT_decomp vander(2)");
1208 f = test_QRPT_decomp_dim(vander3, 64.0 * GSL_DBL_EPSILON);
1209 gsl_test(f, " QRPT_decomp vander(3)");
1212 f = test_QRPT_decomp_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
1213 gsl_test(f, " QRPT_decomp vander(4)");
1216 f = test_QRPT_decomp_dim(vander12, 0.0005); /* FIXME: bad accuracy */
1217 gsl_test(f, " QRPT_decomp vander(12)");
1225 test_QR_update_dim(const gsl_matrix * m, double eps)
1228 unsigned long i,j,k, M = m->size1, N = m->size2;
1230 gsl_vector * rhs = gsl_vector_alloc(N);
1231 gsl_matrix * qr1 = gsl_matrix_alloc(M,N);
1232 gsl_matrix * qr2 = gsl_matrix_alloc(M,N);
1233 gsl_matrix * q1 = gsl_matrix_alloc(M,M);
1234 gsl_matrix * r1 = gsl_matrix_alloc(M,N);
1235 gsl_matrix * q2 = gsl_matrix_alloc(M,M);
1236 gsl_matrix * r2 = gsl_matrix_alloc(M,N);
1237 gsl_vector * d = gsl_vector_alloc(GSL_MIN(M,N));
1238 gsl_vector * solution1 = gsl_vector_alloc(N);
1239 gsl_vector * solution2 = gsl_vector_alloc(N);
1240 gsl_vector * u = gsl_vector_alloc(M);
1241 gsl_vector * v = gsl_vector_alloc(N);
1242 gsl_vector * w = gsl_vector_alloc(M);
1244 gsl_matrix_memcpy(qr1,m);
1245 gsl_matrix_memcpy(qr2,m);
1246 for(i=0; i<N; i++) gsl_vector_set(rhs, i, i+1.0);
1247 for(i=0; i<M; i++) gsl_vector_set(u, i, sin(i+1.0));
1248 for(i=0; i<N; i++) gsl_vector_set(v, i, cos(i+2.0) + sin(i*i+3.0));
1252 double ui = gsl_vector_get(u, i);
1255 double vj = gsl_vector_get(v, j);
1256 double qij = gsl_matrix_get(qr1, i, j);
1257 gsl_matrix_set(qr1, i, j, qij + ui * vj);
1261 s += gsl_linalg_QR_decomp(qr2, d);
1262 s += gsl_linalg_QR_unpack(qr2, d, q2, r2);
1264 /* compute w = Q^T u */
1266 for (j = 0; j < M; j++)
1269 for (i = 0; i < M; i++)
1270 sum += gsl_matrix_get (q2, i, j) * gsl_vector_get (u, i);
1271 gsl_vector_set (w, j, sum);
1274 s += gsl_linalg_QR_update(q2, r2, w, v);
1276 /* compute qr2 = q2 * r2 */
1278 for (i = 0; i < M; i++)
1280 for (j = 0; j< N; j++)
1283 for (k = 0; k <= GSL_MIN(j,M-1); k++)
1285 double qik = gsl_matrix_get(q2, i, k);
1286 double rkj = gsl_matrix_get(r2, k, j);
1289 gsl_matrix_set (qr2, i, j, sum);
1293 for(i=0; i<M; i++) {
1294 for(j=0; j<N; j++) {
1295 double s1 = gsl_matrix_get(qr1, i, j);
1296 double s2 = gsl_matrix_get(qr2, i, j);
1298 int foo = check(s1, s2, eps);
1300 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, s1, s2);
1306 gsl_vector_free(solution1);
1307 gsl_vector_free(solution2);
1312 gsl_matrix_free(qr1);
1313 gsl_matrix_free(qr2);
1314 gsl_matrix_free(q1);
1315 gsl_matrix_free(r1);
1316 gsl_matrix_free(q2);
1317 gsl_matrix_free(r2);
1318 gsl_vector_free(rhs);
1323 int test_QR_update(void)
1328 f = test_QR_update_dim(m35, 2 * 512.0 * GSL_DBL_EPSILON);
1329 gsl_test(f, " QR_update m(3,5)");
1332 f = test_QR_update_dim(m53, 2 * 512.0 * GSL_DBL_EPSILON);
1333 gsl_test(f, " QR_update m(5,3)");
1336 f = test_QR_update_dim(hilb2, 2 * 512.0 * GSL_DBL_EPSILON);
1337 gsl_test(f, " QR_update hilbert(2)");
1340 f = test_QR_update_dim(hilb3, 2 * 512.0 * GSL_DBL_EPSILON);
1341 gsl_test(f, " QR_update hilbert(3)");
1344 f = test_QR_update_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
1345 gsl_test(f, " QR_update hilbert(4)");
1348 f = test_QR_update_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
1349 gsl_test(f, " QR_update hilbert(12)");
1352 f = test_QR_update_dim(vander2, 8.0 * GSL_DBL_EPSILON);
1353 gsl_test(f, " QR_update vander(2)");
1356 f = test_QR_update_dim(vander3, 64.0 * GSL_DBL_EPSILON);
1357 gsl_test(f, " QR_update vander(3)");
1360 f = test_QR_update_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
1361 gsl_test(f, " QR_update vander(4)");
1364 f = test_QR_update_dim(vander12, 0.0005); /* FIXME: bad accuracy */
1365 gsl_test(f, " QR_update vander(12)");
1372 test_LQ_solve_dim(const gsl_matrix * m, const double * actual, double eps)
1375 unsigned long i, dim = m->size1;
1377 gsl_vector * rhs = gsl_vector_alloc(dim);
1378 gsl_matrix * lq = gsl_matrix_alloc(dim,dim);
1379 gsl_vector * d = gsl_vector_alloc(dim);
1380 gsl_vector * x = gsl_vector_alloc(dim);
1382 gsl_matrix_transpose_memcpy(lq,m);
1383 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
1384 s += gsl_linalg_LQ_decomp(lq, d);
1385 s += gsl_linalg_LQ_solve_T(lq, d, rhs, x);
1386 for(i=0; i<dim; i++) {
1387 int foo = check(gsl_vector_get(x, i), actual[i], eps);
1389 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
1396 gsl_matrix_free(lq);
1397 gsl_vector_free(rhs);
1402 int test_LQ_solve(void)
1407 f = test_LQ_solve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
1408 gsl_test(f, " LQ_solve hilbert(2)");
1411 f = test_LQ_solve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
1412 gsl_test(f, " LQ_solve hilbert(3)");
1415 f = test_LQ_solve_dim(hilb4, hilb4_solution, 4 * 1024.0 * GSL_DBL_EPSILON);
1416 gsl_test(f, " LQ_solve hilbert(4)");
1419 f = test_LQ_solve_dim(hilb12, hilb12_solution, 0.5);
1420 gsl_test(f, " LQ_solve hilbert(12)");
1423 f = test_LQ_solve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
1424 gsl_test(f, " LQ_solve vander(2)");
1427 f = test_LQ_solve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
1428 gsl_test(f, " LQ_solve vander(3)");
1431 f = test_LQ_solve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
1432 gsl_test(f, " LQ_solve vander(4)");
1435 f = test_LQ_solve_dim(vander12, vander12_solution, 0.05);
1436 gsl_test(f, " LQ_solve vander(12)");
1446 test_LQ_LQsolve_dim(const gsl_matrix * m, const double * actual, double eps)
1449 unsigned long i, dim = m->size1;
1451 gsl_vector * rhs = gsl_vector_alloc(dim);
1452 gsl_matrix * lq = gsl_matrix_alloc(dim,dim);
1453 gsl_matrix * q = gsl_matrix_alloc(dim,dim);
1454 gsl_matrix * l = gsl_matrix_alloc(dim,dim);
1455 gsl_vector * d = gsl_vector_alloc(dim);
1456 gsl_vector * x = gsl_vector_alloc(dim);
1458 gsl_matrix_transpose_memcpy(lq,m);
1459 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
1460 s += gsl_linalg_LQ_decomp(lq, d);
1461 s += gsl_linalg_LQ_unpack(lq, d, q, l);
1462 s += gsl_linalg_LQ_LQsolve(q, l, rhs, x);
1463 for(i=0; i<dim; i++) {
1464 int foo = check(gsl_vector_get(x, i), actual[i], eps);
1466 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
1473 gsl_matrix_free(lq);
1476 gsl_vector_free(rhs);
1481 int test_LQ_LQsolve(void)
1486 f = test_LQ_LQsolve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
1487 gsl_test(f, " LQ_LQsolve hilbert(2)");
1490 f = test_LQ_LQsolve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
1491 gsl_test(f, " LQ_LQsolve hilbert(3)");
1494 f = test_LQ_LQsolve_dim(hilb4, hilb4_solution, 4 * 1024.0 * GSL_DBL_EPSILON);
1495 gsl_test(f, " LQ_LQsolve hilbert(4)");
1498 f = test_LQ_LQsolve_dim(hilb12, hilb12_solution, 0.5);
1499 gsl_test(f, " LQ_LQsolve hilbert(12)");
1502 f = test_LQ_LQsolve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
1503 gsl_test(f, " LQ_LQsolve vander(2)");
1506 f = test_LQ_LQsolve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
1507 gsl_test(f, " LQ_LQsolve vander(3)");
1510 f = test_LQ_LQsolve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
1511 gsl_test(f, " LQ_LQsolve vander(4)");
1514 f = test_LQ_LQsolve_dim(vander12, vander12_solution, 0.05);
1515 gsl_test(f, " LQ_LQsolve vander(12)");
1523 test_LQ_lssolve_dim(const gsl_matrix * m, const double * actual, double eps)
1526 unsigned long i, M = m->size1, N = m->size2;
1528 gsl_vector * rhs = gsl_vector_alloc(M);
1529 gsl_matrix * lq = gsl_matrix_alloc(N,M);
1530 gsl_vector * d = gsl_vector_alloc(N);
1531 gsl_vector * x = gsl_vector_alloc(N);
1532 gsl_vector * r = gsl_vector_alloc(M);
1533 gsl_vector * res = gsl_vector_alloc(M);
1535 gsl_matrix_transpose_memcpy(lq,m);
1536 for(i=0; i<M; i++) gsl_vector_set(rhs, i, i+1.0);
1537 s += gsl_linalg_LQ_decomp(lq, d);
1538 s += gsl_linalg_LQ_lssolve_T(lq, d, rhs, x, res);
1540 for(i=0; i<N; i++) {
1541 int foo = check(gsl_vector_get(x, i), actual[i], eps);
1543 printf("(%3lu,%3lu)[%lu]: %22.18g %22.18g\n", M, N, i, gsl_vector_get(x, i), actual[i]);
1549 /* compute residual r = b - m x */
1551 gsl_vector_set_zero(r);
1553 gsl_vector_memcpy(r, rhs);
1554 gsl_blas_dgemv(CblasNoTrans, -1.0, m, x, 1.0, r);
1557 for(i=0; i<N; i++) {
1558 int foo = check(gsl_vector_get(res, i), gsl_vector_get(r,i), sqrt(eps));
1560 printf("(%3lu,%3lu)[%lu]: %22.18g %22.18g\n", M, N, i, gsl_vector_get(res, i), gsl_vector_get(r,i));
1566 gsl_vector_free(res);
1569 gsl_matrix_free(lq);
1570 gsl_vector_free(rhs);
1575 int test_LQ_lssolve(void)
1580 f = test_LQ_lssolve_dim(m53, m53_lssolution, 2 * 64.0 * GSL_DBL_EPSILON);
1581 gsl_test(f, " LQ_lssolve m(5,3)");
1584 f = test_LQ_lssolve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
1585 gsl_test(f, " LQ_lssolve hilbert(2)");
1588 f = test_LQ_lssolve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
1589 gsl_test(f, " LQ_lssolve hilbert(3)");
1592 f = test_LQ_lssolve_dim(hilb4, hilb4_solution, 4 * 1024.0 * GSL_DBL_EPSILON);
1593 gsl_test(f, " LQ_lssolve hilbert(4)");
1596 f = test_LQ_lssolve_dim(hilb12, hilb12_solution, 0.5);
1597 gsl_test(f, " LQ_lssolve hilbert(12)");
1600 f = test_LQ_lssolve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
1601 gsl_test(f, " LQ_lssolve vander(2)");
1604 f = test_LQ_lssolve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
1605 gsl_test(f, " LQ_lssolve vander(3)");
1608 f = test_LQ_lssolve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
1609 gsl_test(f, " LQ_lssolve vander(4)");
1612 f = test_LQ_lssolve_dim(vander12, vander12_solution, 0.05);
1613 gsl_test(f, " LQ_lssolve vander(12)");
1627 test_LQ_decomp_dim(const gsl_matrix * m, double eps)
1630 unsigned long i,j, M = m->size1, N = m->size2;
1632 gsl_matrix * lq = gsl_matrix_alloc(M,N);
1633 gsl_matrix * a = gsl_matrix_alloc(M,N);
1634 gsl_matrix * q = gsl_matrix_alloc(N,N);
1635 gsl_matrix * l = gsl_matrix_alloc(M,N);
1636 gsl_vector * d = gsl_vector_alloc(GSL_MIN(M,N));
1638 gsl_matrix_memcpy(lq,m);
1640 s += gsl_linalg_LQ_decomp(lq, d);
1641 s += gsl_linalg_LQ_unpack(lq, d, q, l);
1643 /* compute a = q r */
1644 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, l, q, 0.0, a);
1646 for(i=0; i<M; i++) {
1647 for(j=0; j<N; j++) {
1648 double aij = gsl_matrix_get(a, i, j);
1649 double mij = gsl_matrix_get(m, i, j);
1650 int foo = check(aij, mij, eps);
1652 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
1659 gsl_matrix_free(lq);
1667 int test_LQ_decomp(void)
1672 f = test_LQ_decomp_dim(m35, 2 * 8.0 * GSL_DBL_EPSILON);
1673 gsl_test(f, " LQ_decomp m(3,5)");
1676 f = test_LQ_decomp_dim(m53, 2 * 64.0 * GSL_DBL_EPSILON);
1677 gsl_test(f, " LQ_decomp m(5,3)");
1680 f = test_LQ_decomp_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
1681 gsl_test(f, " LQ_decomp hilbert(2)");
1684 f = test_LQ_decomp_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
1685 gsl_test(f, " LQ_decomp hilbert(3)");
1688 f = test_LQ_decomp_dim(hilb4, 4 * 1024.0 * GSL_DBL_EPSILON);
1689 gsl_test(f, " LQ_decomp hilbert(4)");
1692 f = test_LQ_decomp_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
1693 gsl_test(f, " LQ_decomp hilbert(12)");
1696 f = test_LQ_decomp_dim(vander2, 8.0 * GSL_DBL_EPSILON);
1697 gsl_test(f, " LQ_decomp vander(2)");
1700 f = test_LQ_decomp_dim(vander3, 64.0 * GSL_DBL_EPSILON);
1701 gsl_test(f, " LQ_decomp vander(3)");
1704 f = test_LQ_decomp_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
1705 gsl_test(f, " LQ_decomp vander(4)");
1708 f = test_LQ_decomp_dim(vander12, 0.0005); /* FIXME: bad accuracy */
1709 gsl_test(f, " LQ_decomp vander(12)");
1719 test_PTLQ_solve_dim(const gsl_matrix * m, const double * actual, double eps)
1723 unsigned long i, dim = m->size1;
1725 gsl_permutation * perm = gsl_permutation_alloc(dim);
1726 gsl_vector * rhs = gsl_vector_alloc(dim);
1727 gsl_matrix * lq = gsl_matrix_alloc(dim,dim);
1728 gsl_vector * d = gsl_vector_alloc(dim);
1729 gsl_vector * x = gsl_vector_alloc(dim);
1730 gsl_vector * norm = gsl_vector_alloc(dim);
1732 gsl_matrix_transpose_memcpy(lq,m);
1733 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
1734 s += gsl_linalg_PTLQ_decomp(lq, d, perm, &signum, norm);
1735 s += gsl_linalg_PTLQ_solve_T(lq, d, perm, rhs, x);
1736 for(i=0; i<dim; i++) {
1737 int foo = check(gsl_vector_get(x, i), actual[i], eps);
1739 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
1744 gsl_vector_free(norm);
1747 gsl_matrix_free(lq);
1748 gsl_vector_free(rhs);
1749 gsl_permutation_free(perm);
1754 int test_PTLQ_solve(void)
1759 f = test_PTLQ_solve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
1760 gsl_test(f, " PTLQ_solve hilbert(2)");
1763 f = test_PTLQ_solve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
1764 gsl_test(f, " PTLQ_solve hilbert(3)");
1767 f = test_PTLQ_solve_dim(hilb4, hilb4_solution, 2 * 2048.0 * GSL_DBL_EPSILON);
1768 gsl_test(f, " PTLQ_solve hilbert(4)");
1771 f = test_PTLQ_solve_dim(hilb12, hilb12_solution, 0.5);
1772 gsl_test(f, " PTLQ_solve hilbert(12)");
1775 f = test_PTLQ_solve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
1776 gsl_test(f, " PTLQ_solve vander(2)");
1779 f = test_PTLQ_solve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
1780 gsl_test(f, " PTLQ_solve vander(3)");
1783 f = test_PTLQ_solve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
1784 gsl_test(f, " PTLQ_solve vander(4)");
1787 f = test_PTLQ_solve_dim(vander12, vander12_solution, 0.05);
1788 gsl_test(f, " PTLQ_solve vander(12)");
1796 test_PTLQ_LQsolve_dim(const gsl_matrix * m, const double * actual, double eps)
1800 unsigned long i, dim = m->size1;
1802 gsl_permutation * perm = gsl_permutation_alloc(dim);
1803 gsl_vector * rhs = gsl_vector_alloc(dim);
1804 gsl_matrix * lq = gsl_matrix_alloc(dim,dim);
1805 gsl_matrix * q = gsl_matrix_alloc(dim,dim);
1806 gsl_matrix * l = gsl_matrix_alloc(dim,dim);
1807 gsl_vector * d = gsl_vector_alloc(dim);
1808 gsl_vector * x = gsl_vector_alloc(dim);
1809 gsl_vector * norm = gsl_vector_alloc(dim);
1811 gsl_matrix_transpose_memcpy(lq,m);
1812 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
1813 s += gsl_linalg_PTLQ_decomp2(lq, q, l, d, perm, &signum, norm);
1814 s += gsl_linalg_PTLQ_LQsolve_T(q, l, perm, rhs, x);
1815 for(i=0; i<dim; i++) {
1816 int foo = check(gsl_vector_get(x, i), actual[i], eps);
1818 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
1823 gsl_vector_free(norm);
1826 gsl_matrix_free(lq);
1827 gsl_vector_free(rhs);
1828 gsl_permutation_free(perm);
1833 int test_PTLQ_LQsolve(void)
1838 f = test_PTLQ_LQsolve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
1839 gsl_test(f, " PTLQ_LQsolve hilbert(2)");
1842 f = test_PTLQ_LQsolve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
1843 gsl_test(f, " PTLQ_LQsolve hilbert(3)");
1846 f = test_PTLQ_LQsolve_dim(hilb4, hilb4_solution, 2 * 2048.0 * GSL_DBL_EPSILON);
1847 gsl_test(f, " PTLQ_LQsolve hilbert(4)");
1850 f = test_PTLQ_LQsolve_dim(hilb12, hilb12_solution, 0.5);
1851 gsl_test(f, " PTLQ_LQsolve hilbert(12)");
1854 f = test_PTLQ_LQsolve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
1855 gsl_test(f, " PTLQ_LQsolve vander(2)");
1858 f = test_PTLQ_LQsolve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
1859 gsl_test(f, " PTLQ_LQsolve vander(3)");
1862 f = test_PTLQ_LQsolve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
1863 gsl_test(f, " PTLQ_LQsolve vander(4)");
1866 f = test_PTLQ_LQsolve_dim(vander12, vander12_solution, 0.05);
1867 gsl_test(f, " PTLQ_LQsolve vander(12)");
1875 test_PTLQ_decomp_dim(const gsl_matrix * m, double eps)
1878 unsigned long i,j, M = m->size1, N = m->size2;
1880 gsl_matrix * lq = gsl_matrix_alloc(N,M);
1881 gsl_matrix * a = gsl_matrix_alloc(N,M);
1882 gsl_matrix * q = gsl_matrix_alloc(M,M);
1883 gsl_matrix * l = gsl_matrix_alloc(N,M);
1884 gsl_vector * d = gsl_vector_alloc(GSL_MIN(M,N));
1885 gsl_vector * norm = gsl_vector_alloc(N);
1887 gsl_permutation * perm = gsl_permutation_alloc(N);
1889 gsl_matrix_transpose_memcpy(lq,m);
1891 s += gsl_linalg_PTLQ_decomp(lq, d, perm, &signum, norm);
1892 s += gsl_linalg_LQ_unpack(lq, d, q, l);
1894 /* compute a = l q */
1895 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, l, q, 0.0, a);
1898 /* Compute P LQ by permuting the rows of LQ */
1900 for (i = 0; i < M; i++) {
1901 gsl_vector_view col = gsl_matrix_column (a, i);
1902 gsl_permute_vector_inverse (perm, &col.vector);
1905 for(i=0; i<M; i++) {
1906 for(j=0; j<N; j++) {
1907 double aij = gsl_matrix_get(a, j, i);
1908 double mij = gsl_matrix_get(m, i, j);
1909 int foo = check(aij, mij, eps);
1911 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
1917 gsl_permutation_free (perm);
1918 gsl_vector_free(norm);
1920 gsl_matrix_free(lq);
1928 int test_PTLQ_decomp(void)
1933 f = test_PTLQ_decomp_dim(m35, 2 * 8.0 * GSL_DBL_EPSILON);
1934 gsl_test(f, " PTLQ_decomp m(3,5)");
1937 f = test_PTLQ_decomp_dim(m53, 2 * 8.0 * GSL_DBL_EPSILON);
1938 gsl_test(f, " PTLQ_decomp m(5,3)");
1941 f = test_PTLQ_decomp_dim(s35, 2 * 8.0 * GSL_DBL_EPSILON);
1942 gsl_test(f, " PTLQ_decomp s(3,5)");
1945 f = test_PTLQ_decomp_dim(s53, 2 * 8.0 * GSL_DBL_EPSILON);
1946 gsl_test(f, " PTLQ_decomp s(5,3)");
1949 f = test_PTLQ_decomp_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
1950 gsl_test(f, " PTLQ_decomp hilbert(2)");
1953 f = test_PTLQ_decomp_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
1954 gsl_test(f, " PTLQ_decomp hilbert(3)");
1957 f = test_PTLQ_decomp_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
1958 gsl_test(f, " PTLQ_decomp hilbert(4)");
1961 f = test_PTLQ_decomp_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
1962 gsl_test(f, " PTLQ_decomp hilbert(12)");
1965 f = test_PTLQ_decomp_dim(vander2, 8.0 * GSL_DBL_EPSILON);
1966 gsl_test(f, " PTLQ_decomp vander(2)");
1969 f = test_PTLQ_decomp_dim(vander3, 64.0 * GSL_DBL_EPSILON);
1970 gsl_test(f, " PTLQ_decomp vander(3)");
1973 f = test_PTLQ_decomp_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
1974 gsl_test(f, " PTLQ_decomp vander(4)");
1977 f = test_PTLQ_decomp_dim(vander12, 0.0005); /* FIXME: bad accuracy */
1978 gsl_test(f, " PTLQ_decomp vander(12)");
1986 test_LQ_update_dim(const gsl_matrix * m, double eps)
1989 unsigned long i,j, M = m->size1, N = m->size2;
1991 gsl_matrix * lq1 = gsl_matrix_alloc(N,M);
1992 gsl_matrix * lq2 = gsl_matrix_alloc(N,M);
1993 gsl_matrix * q1 = gsl_matrix_alloc(M,M);
1994 gsl_matrix * l1 = gsl_matrix_alloc(N,M);
1995 gsl_matrix * q2 = gsl_matrix_alloc(M,M);
1996 gsl_matrix * l2 = gsl_matrix_alloc(N,M);
1997 gsl_vector * d2 = gsl_vector_alloc(GSL_MIN(M,N));
1998 gsl_vector * u = gsl_vector_alloc(M);
1999 gsl_vector * v = gsl_vector_alloc(N);
2000 gsl_vector * w = gsl_vector_alloc(M);
2002 gsl_matrix_transpose_memcpy(lq1,m);
2003 gsl_matrix_transpose_memcpy(lq2,m);
2004 for(i=0; i<M; i++) gsl_vector_set(u, i, sin(i+1.0));
2005 for(i=0; i<N; i++) gsl_vector_set(v, i, cos(i+2.0) + sin(i*i+3.0));
2007 /* lq1 is updated */
2009 gsl_blas_dger(1.0, v, u, lq1);
2011 /* lq2 is first decomposed, updated later */
2013 s += gsl_linalg_LQ_decomp(lq2, d2);
2014 s += gsl_linalg_LQ_unpack(lq2, d2, q2, l2);
2016 /* compute w = Q^T u */
2018 gsl_blas_dgemv(CblasNoTrans, 1.0, q2, u, 0.0, w);
2020 /* now lq2 is updated */
2022 s += gsl_linalg_LQ_update(q2, l2, v, w);
2024 /* multiply q2*l2 */
2026 gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1.0,l2,q2,0.0,lq2);
2028 /* check lq1==lq2 */
2030 for(i=0; i<N; i++) {
2031 for(j=0; j<M; j++) {
2032 double s1 = gsl_matrix_get(lq1, i, j);
2033 double s2 = gsl_matrix_get(lq2, i, j);
2035 int foo = check(s1, s2, eps);
2038 printf("LQ:(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, s1, s2);
2045 gsl_vector_free(d2);
2049 gsl_matrix_free(lq1);
2050 gsl_matrix_free(lq2);
2051 gsl_matrix_free(q1);
2052 gsl_matrix_free(l1);
2053 gsl_matrix_free(q2);
2054 gsl_matrix_free(l2);
2059 int test_LQ_update(void)
2064 f = test_LQ_update_dim(m35, 2 * 512.0 * GSL_DBL_EPSILON);
2065 gsl_test(f, " LQ_update m(3,5)");
2068 f = test_LQ_update_dim(m53, 2 * 512.0 * GSL_DBL_EPSILON);
2069 gsl_test(f, " LQ_update m(5,3)");
2072 f = test_LQ_update_dim(hilb2, 2 * 512.0 * GSL_DBL_EPSILON);
2073 gsl_test(f, " LQ_update hilbert(2)");
2076 f = test_LQ_update_dim(hilb3, 2 * 512.0 * GSL_DBL_EPSILON);
2077 gsl_test(f, " LQ_update hilbert(3)");
2080 f = test_LQ_update_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
2081 gsl_test(f, " LQ_update hilbert(4)");
2084 f = test_LQ_update_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
2085 gsl_test(f, " LQ_update hilbert(12)");
2088 f = test_LQ_update_dim(vander2, 8.0 * GSL_DBL_EPSILON);
2089 gsl_test(f, " LQ_update vander(2)");
2092 f = test_LQ_update_dim(vander3, 64.0 * GSL_DBL_EPSILON);
2093 gsl_test(f, " LQ_update vander(3)");
2096 f = test_LQ_update_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
2097 gsl_test(f, " LQ_update vander(4)");
2100 f = test_LQ_update_dim(vander12, 0.0005); /* FIXME: bad accuracy */
2101 gsl_test(f, " LQ_update vander(12)");
2108 test_SV_solve_dim(const gsl_matrix * m, const double * actual, double eps)
2111 unsigned long i, dim = m->size1;
2113 gsl_vector * rhs = gsl_vector_alloc(dim);
2114 gsl_matrix * u = gsl_matrix_alloc(dim,dim);
2115 gsl_matrix * q = gsl_matrix_alloc(dim,dim);
2116 gsl_vector * d = gsl_vector_alloc(dim);
2117 gsl_vector * x = gsl_vector_calloc(dim);
2118 gsl_matrix_memcpy(u,m);
2119 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
2120 s += gsl_linalg_SV_decomp(u, q, d, x);
2121 s += gsl_linalg_SV_solve(u, q, d, rhs, x);
2122 for(i=0; i<dim; i++) {
2123 int foo = check(gsl_vector_get(x, i), actual[i], eps);
2125 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
2133 gsl_vector_free(rhs);
2138 int test_SV_solve(void)
2143 f = test_SV_solve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
2144 gsl_test(f, " SV_solve hilbert(2)");
2147 f = test_SV_solve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
2148 gsl_test(f, " SV_solve hilbert(3)");
2151 f = test_SV_solve_dim(hilb4, hilb4_solution, 2 * 1024.0 * GSL_DBL_EPSILON);
2152 gsl_test(f, " SV_solve hilbert(4)");
2155 f = test_SV_solve_dim(hilb12, hilb12_solution, 0.5);
2156 gsl_test(f, " SV_solve hilbert(12)");
2159 f = test_SV_solve_dim(vander2, vander2_solution, 64.0 * GSL_DBL_EPSILON);
2160 gsl_test(f, " SV_solve vander(2)");
2163 f = test_SV_solve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
2164 gsl_test(f, " SV_solve vander(3)");
2167 f = test_SV_solve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
2168 gsl_test(f, " SV_solve vander(4)");
2171 f = test_SV_solve_dim(vander12, vander12_solution, 0.05);
2172 gsl_test(f, " SV_solve vander(12)");
2179 test_SV_decomp_dim(const gsl_matrix * m, double eps)
2183 unsigned long i,j, M = m->size1, N = m->size2;
2185 gsl_matrix * v = gsl_matrix_alloc(M,N);
2186 gsl_matrix * a = gsl_matrix_alloc(M,N);
2187 gsl_matrix * q = gsl_matrix_alloc(N,N);
2188 gsl_matrix * dqt = gsl_matrix_alloc(N,N);
2189 gsl_vector * d = gsl_vector_alloc(N);
2190 gsl_vector * w = gsl_vector_alloc(N);
2192 gsl_matrix_memcpy(v,m);
2194 s += gsl_linalg_SV_decomp(v, q, d, w);
2196 /* Check that singular values are non-negative and in non-decreasing
2201 for (i = 0; i < N; i++)
2203 double di = gsl_vector_get (d, i);
2207 continue; /* skip NaNs */
2212 printf("singular value %lu = %22.18g < 0\n", i, di);
2215 if(i > 0 && di > di1) {
2217 printf("singular value %lu = %22.18g vs previous %22.18g\n", i, di, di1);
2223 /* Scale dqt = D Q^T */
2225 for (i = 0; i < N ; i++)
2227 double di = gsl_vector_get (d, i);
2229 for (j = 0; j < N; j++)
2231 double qji = gsl_matrix_get(q, j, i);
2232 gsl_matrix_set (dqt, i, j, qji * di);
2236 /* compute a = v dqt */
2237 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, v, dqt, 0.0, a);
2239 for(i=0; i<M; i++) {
2240 for(j=0; j<N; j++) {
2241 double aij = gsl_matrix_get(a, i, j);
2242 double mij = gsl_matrix_get(m, i, j);
2243 int foo = check(aij, mij, eps);
2245 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
2255 gsl_matrix_free(dqt);
2260 int test_SV_decomp(void)
2265 f = test_SV_decomp_dim(m11, 2 * GSL_DBL_EPSILON);
2266 gsl_test(f, " SV_decomp m(1,1)");
2269 f = test_SV_decomp_dim(m51, 2 * 64.0 * GSL_DBL_EPSILON);
2270 gsl_test(f, " SV_decomp m(5,1)");
2273 /* M<N not implemented yet */
2275 f = test_SV_decomp_dim(m35, 2 * 8.0 * GSL_DBL_EPSILON);
2276 gsl_test(f, " SV_decomp m(3,5)");
2279 f = test_SV_decomp_dim(m53, 2 * 64.0 * GSL_DBL_EPSILON);
2280 gsl_test(f, " SV_decomp m(5,3)");
2283 f = test_SV_decomp_dim(moler10, 2 * 64.0 * GSL_DBL_EPSILON);
2284 gsl_test(f, " SV_decomp moler(10)");
2287 f = test_SV_decomp_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
2288 gsl_test(f, " SV_decomp hilbert(2)");
2291 f = test_SV_decomp_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
2292 gsl_test(f, " SV_decomp hilbert(3)");
2295 f = test_SV_decomp_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
2296 gsl_test(f, " SV_decomp hilbert(4)");
2299 f = test_SV_decomp_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
2300 gsl_test(f, " SV_decomp hilbert(12)");
2303 f = test_SV_decomp_dim(vander2, 8.0 * GSL_DBL_EPSILON);
2304 gsl_test(f, " SV_decomp vander(2)");
2307 f = test_SV_decomp_dim(vander3, 64.0 * GSL_DBL_EPSILON);
2308 gsl_test(f, " SV_decomp vander(3)");
2311 f = test_SV_decomp_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
2312 gsl_test(f, " SV_decomp vander(4)");
2315 f = test_SV_decomp_dim(vander12, 1e-4);
2316 gsl_test(f, " SV_decomp vander(12)");
2319 f = test_SV_decomp_dim(row3, 10 * GSL_DBL_EPSILON);
2320 gsl_test(f, " SV_decomp row3");
2323 f = test_SV_decomp_dim(row5, 128 * GSL_DBL_EPSILON);
2324 gsl_test(f, " SV_decomp row5");
2327 f = test_SV_decomp_dim(row12, 1024 * GSL_DBL_EPSILON);
2328 gsl_test(f, " SV_decomp row12");
2331 f = test_SV_decomp_dim(inf5, 1024 * GSL_DBL_EPSILON);
2332 gsl_test(f, " SV_decomp inf5");
2335 f = test_SV_decomp_dim(nan5, 1024 * GSL_DBL_EPSILON);
2336 gsl_test(f, " SV_decomp nan5");
2339 f = test_SV_decomp_dim(dblmin3, 1024 * GSL_DBL_EPSILON);
2340 gsl_test(f, " SV_decomp dblmin3");
2343 f = test_SV_decomp_dim(dblmin5, 1024 * GSL_DBL_EPSILON);
2344 gsl_test(f, " SV_decomp dblmin5");
2349 double i1, i2, i3, i4;
2350 double lower = -2, upper = 2;
2352 for (i1 = lower; i1 <= upper; i1++)
2354 for (i2 = lower; i2 <= upper; i2++)
2356 for (i3 = lower; i3 <= upper; i3++)
2358 for (i4 = lower; i4 <= upper; i4++)
2360 gsl_matrix_set (A22, 0,0, i1);
2361 gsl_matrix_set (A22, 0,1, i2);
2362 gsl_matrix_set (A22, 1,0, i3);
2363 gsl_matrix_set (A22, 1,1, i4);
2365 f = test_SV_decomp_dim(A22, 16 * GSL_DBL_EPSILON);
2366 gsl_test(f, " SV_decomp (2x2) A=[%g, %g; %g, %g]", i1,i2,i3,i4);
2376 double carry = 0, lower = 0, upper = 1;
2377 double *a = A33->data;
2379 for (i=0; i<9; i++) {
2383 while (carry == 0.0) {
2384 f = test_SV_decomp_dim(A33, 64 * GSL_DBL_EPSILON);
2385 gsl_test(f, " SV_decomp (3x3) A=[ %g, %g, %g; %g, %g, %g; %g, %g, %g]",
2386 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
2390 for (i=9; carry > 0.0 && i>0 && i--;)
2392 double v=a[i]+carry;
2393 carry = (v>upper) ? 1.0 : 0.0;
2394 a[i] = (v>upper) ? lower : v;
2402 double carry = 0, lower = 0, upper = 1;
2403 double *a = A44->data;
2405 for (i=0; i<16; i++) {
2409 while (carry == 0.0) {
2410 f = test_SV_decomp_dim(A44, 64 * GSL_DBL_EPSILON);
2411 gsl_test(f, " SV_decomp (4x4) A=[ %g, %g, %g, %g; %g, %g, %g, %g; %g, %g, %g, %g; %g, %g, %g, %g]",
2412 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
2413 a[10], a[11], a[12], a[13], a[14], a[15]);
2417 for (i=16; carry > 0.0 && i>0 && i--;)
2419 double v=a[i]+carry;
2420 carry = (v>upper) ? 1.0 : 0.0;
2421 a[i] = (v>upper) ? lower : v;
2432 test_SV_decomp_mod_dim(const gsl_matrix * m, double eps)
2436 unsigned long i,j, M = m->size1, N = m->size2;
2438 gsl_matrix * v = gsl_matrix_alloc(M,N);
2439 gsl_matrix * a = gsl_matrix_alloc(M,N);
2440 gsl_matrix * q = gsl_matrix_alloc(N,N);
2441 gsl_matrix * x = gsl_matrix_alloc(N,N);
2442 gsl_matrix * dqt = gsl_matrix_alloc(N,N);
2443 gsl_vector * d = gsl_vector_alloc(N);
2444 gsl_vector * w = gsl_vector_alloc(N);
2446 gsl_matrix_memcpy(v,m);
2448 s += gsl_linalg_SV_decomp_mod(v, x, q, d, w);
2450 /* Check that singular values are non-negative and in non-decreasing
2455 for (i = 0; i < N; i++)
2457 double di = gsl_vector_get (d, i);
2461 continue; /* skip NaNs */
2466 printf("singular value %lu = %22.18g < 0\n", i, di);
2469 if(i > 0 && di > di1) {
2471 printf("singular value %lu = %22.18g vs previous %22.18g\n", i, di, di1);
2477 /* Scale dqt = D Q^T */
2479 for (i = 0; i < N ; i++)
2481 double di = gsl_vector_get (d, i);
2483 for (j = 0; j < N; j++)
2485 double qji = gsl_matrix_get(q, j, i);
2486 gsl_matrix_set (dqt, i, j, qji * di);
2490 /* compute a = v dqt */
2491 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, v, dqt, 0.0, a);
2493 for(i=0; i<M; i++) {
2494 for(j=0; j<N; j++) {
2495 double aij = gsl_matrix_get(a, i, j);
2496 double mij = gsl_matrix_get(m, i, j);
2497 int foo = check(aij, mij, eps);
2499 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
2509 gsl_matrix_free(dqt);
2510 gsl_matrix_free (x);
2515 int test_SV_decomp_mod(void)
2520 f = test_SV_decomp_mod_dim(m11, 2 * GSL_DBL_EPSILON);
2521 gsl_test(f, " SV_decomp_mod m(1,1)");
2524 f = test_SV_decomp_mod_dim(m51, 2 * 64.0 * GSL_DBL_EPSILON);
2525 gsl_test(f, " SV_decomp_mod m(5,1)");
2528 /* M<N not implemented yet */
2530 f = test_SV_decomp_mod_dim(m35, 2 * 8.0 * GSL_DBL_EPSILON);
2531 gsl_test(f, " SV_decomp_mod m(3,5)");
2534 f = test_SV_decomp_mod_dim(m53, 2 * 64.0 * GSL_DBL_EPSILON);
2535 gsl_test(f, " SV_decomp_mod m(5,3)");
2538 f = test_SV_decomp_mod_dim(moler10, 2 * 64.0 * GSL_DBL_EPSILON);
2539 gsl_test(f, " SV_decomp_mod moler(10)");
2542 f = test_SV_decomp_mod_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
2543 gsl_test(f, " SV_decomp_mod hilbert(2)");
2546 f = test_SV_decomp_mod_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
2547 gsl_test(f, " SV_decomp_mod hilbert(3)");
2550 f = test_SV_decomp_mod_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
2551 gsl_test(f, " SV_decomp_mod hilbert(4)");
2554 f = test_SV_decomp_mod_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
2555 gsl_test(f, " SV_decomp_mod hilbert(12)");
2558 f = test_SV_decomp_mod_dim(vander2, 8.0 * GSL_DBL_EPSILON);
2559 gsl_test(f, " SV_decomp_mod vander(2)");
2562 f = test_SV_decomp_mod_dim(vander3, 64.0 * GSL_DBL_EPSILON);
2563 gsl_test(f, " SV_decomp_mod vander(3)");
2566 f = test_SV_decomp_mod_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
2567 gsl_test(f, " SV_decomp_mod vander(4)");
2570 f = test_SV_decomp_mod_dim(vander12, 1e-4);
2571 gsl_test(f, " SV_decomp_mod vander(12)");
2574 f = test_SV_decomp_mod_dim(row3, 10 * GSL_DBL_EPSILON);
2575 gsl_test(f, " SV_decomp_mod row3");
2578 f = test_SV_decomp_mod_dim(row5, 128 * GSL_DBL_EPSILON);
2579 gsl_test(f, " SV_decomp_mod row5");
2582 f = test_SV_decomp_mod_dim(row12, 1024 * GSL_DBL_EPSILON);
2583 gsl_test(f, " SV_decomp_mod row12");
2586 f = test_SV_decomp_mod_dim(inf5, 1024 * GSL_DBL_EPSILON);
2587 gsl_test(f, " SV_decomp_mod inf5");
2590 f = test_SV_decomp_mod_dim(nan5, 1024 * GSL_DBL_EPSILON);
2591 gsl_test(f, " SV_decomp_mod nan5");
2596 double i1, i2, i3, i4;
2597 double lower = -2, upper = 2;
2599 for (i1 = lower; i1 <= upper; i1++)
2601 for (i2 = lower; i2 <= upper; i2++)
2603 for (i3 = lower; i3 <= upper; i3++)
2605 for (i4 = lower; i4 <= upper; i4++)
2607 gsl_matrix_set (A22, 0,0, i1);
2608 gsl_matrix_set (A22, 0,1, i2);
2609 gsl_matrix_set (A22, 1,0, i3);
2610 gsl_matrix_set (A22, 1,1, i4);
2612 f = test_SV_decomp_mod_dim(A22, 16 * GSL_DBL_EPSILON);
2613 gsl_test(f, " SV_decomp_mod (2x2) A=[%g, %g; %g, %g]", i1,i2,i3,i4);
2623 double carry = 0, lower = 0, upper = 1;
2624 double *a = A33->data;
2626 for (i=0; i<9; i++) {
2630 while (carry == 0.0) {
2631 f = test_SV_decomp_mod_dim(A33, 64 * GSL_DBL_EPSILON);
2632 gsl_test(f, " SV_decomp_mod (3x3) A=[ %g, %g, %g; %g, %g, %g; %g, %g, %g]",
2633 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
2637 for (i=9; carry > 0.0 && i>0 && i--;)
2639 double v=a[i]+carry;
2640 carry = (v>upper) ? 1.0 : 0.0;
2641 a[i] = (v>upper) ? lower : v;
2649 double carry = 0, lower = 0, upper = 1;
2650 double *a = A44->data;
2652 for (i=0; i<16; i++) {
2656 while (carry == 0.0) {
2657 f = test_SV_decomp_mod_dim(A44, 64 * GSL_DBL_EPSILON);
2658 gsl_test(f, " SV_decomp_mod (4x4) A=[ %g, %g, %g, %g; %g, %g, %g, %g; %g, %g, %g, %g; %g, %g, %g, %g]",
2659 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
2660 a[10], a[11], a[12], a[13], a[14], a[15]);
2664 for (i=16; carry>0.0 && i>0 && i--;)
2666 double v=a[i]+carry;
2667 carry = (v>upper) ? 1.0 : 0.0;
2668 a[i] = (v>upper) ? lower : v;
2679 test_SV_decomp_jacobi_dim(const gsl_matrix * m, double eps)
2683 unsigned long i,j, M = m->size1, N = m->size2;
2685 gsl_matrix * v = gsl_matrix_alloc(M,N);
2686 gsl_matrix * a = gsl_matrix_alloc(M,N);
2687 gsl_matrix * q = gsl_matrix_alloc(N,N);
2688 gsl_matrix * dqt = gsl_matrix_alloc(N,N);
2689 gsl_vector * d = gsl_vector_alloc(N);
2691 gsl_matrix_memcpy(v,m);
2693 s += gsl_linalg_SV_decomp_jacobi(v, q, d);
2695 printf("call returned status = %d\n", s);
2697 /* Check that singular values are non-negative and in non-decreasing
2702 for (i = 0; i < N; i++)
2704 double di = gsl_vector_get (d, i);
2708 continue; /* skip NaNs */
2713 printf("singular value %lu = %22.18g < 0\n", i, di);
2716 if(i > 0 && di > di1) {
2718 printf("singular value %lu = %22.18g vs previous %22.18g\n", i, di, di1);
2724 /* Scale dqt = D Q^T */
2726 for (i = 0; i < N ; i++)
2728 double di = gsl_vector_get (d, i);
2730 for (j = 0; j < N; j++)
2732 double qji = gsl_matrix_get(q, j, i);
2733 gsl_matrix_set (dqt, i, j, qji * di);
2737 /* compute a = v dqt */
2738 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, v, dqt, 0.0, a);
2740 for(i=0; i<M; i++) {
2741 for(j=0; j<N; j++) {
2742 double aij = gsl_matrix_get(a, i, j);
2743 double mij = gsl_matrix_get(m, i, j);
2744 int foo = check(aij, mij, eps);
2746 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
2755 gsl_matrix_free(dqt);
2760 int test_SV_decomp_jacobi(void)
2765 f = test_SV_decomp_jacobi_dim(m11, 2 * GSL_DBL_EPSILON);
2766 gsl_test(f, " SV_decomp_jacobi m(1,1)");
2769 f = test_SV_decomp_jacobi_dim(m51, 2 * 64.0 * GSL_DBL_EPSILON);
2770 gsl_test(f, " SV_decomp_jacobi m(5,1)");
2773 /* M<N not implemented yet */
2775 f = test_SV_decomp_jacobi_dim(m35, 2 * 8.0 * GSL_DBL_EPSILON);
2776 gsl_test(f, " SV_decomp_jacobi m(3,5)");
2779 f = test_SV_decomp_jacobi_dim(m53, 2 * 64.0 * GSL_DBL_EPSILON);
2780 gsl_test(f, " SV_decomp_jacobi m(5,3)");
2783 f = test_SV_decomp_jacobi_dim(moler10, 2 * 64.0 * GSL_DBL_EPSILON);
2784 gsl_test(f, " SV_decomp_jacobi moler(10)");
2787 f = test_SV_decomp_jacobi_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
2788 gsl_test(f, " SV_decomp_jacobi hilbert(2)");
2791 f = test_SV_decomp_jacobi_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
2792 gsl_test(f, " SV_decomp_jacobi hilbert(3)");
2795 f = test_SV_decomp_jacobi_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
2796 gsl_test(f, " SV_decomp_jacobi hilbert(4)");
2799 f = test_SV_decomp_jacobi_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
2800 gsl_test(f, " SV_decomp_jacobi hilbert(12)");
2803 f = test_SV_decomp_jacobi_dim(vander2, 8.0 * GSL_DBL_EPSILON);
2804 gsl_test(f, " SV_decomp_jacobi vander(2)");
2807 f = test_SV_decomp_jacobi_dim(vander3, 64.0 * GSL_DBL_EPSILON);
2808 gsl_test(f, " SV_decomp_jacobi vander(3)");
2811 f = test_SV_decomp_jacobi_dim(vander4, 1024.0 * GSL_DBL_EPSILON);
2812 gsl_test(f, " SV_decomp_jacobi vander(4)");
2815 f = test_SV_decomp_jacobi_dim(vander12, 1e-4);
2816 gsl_test(f, " SV_decomp_jacobi vander(12)");
2819 f = test_SV_decomp_jacobi_dim(row3, 10 * GSL_DBL_EPSILON);
2820 gsl_test(f, " SV_decomp_jacobi row3");
2823 f = test_SV_decomp_jacobi_dim(row5, 128 * GSL_DBL_EPSILON);
2824 gsl_test(f, " SV_decomp_jacobi row5");
2827 f = test_SV_decomp_jacobi_dim(row12, 1024 * GSL_DBL_EPSILON);
2828 gsl_test(f, " SV_decomp_jacobi row12");
2832 #ifdef TEST_JACOBI_INF
2833 f = test_SV_decomp_jacobi_dim(inf5, 1024 * GSL_DBL_EPSILON);
2834 gsl_test(f, " SV_decomp_jacobi inf5");
2837 f = test_SV_decomp_jacobi_dim(nan5, 1024 * GSL_DBL_EPSILON);
2838 gsl_test(f, " SV_decomp_jacobi nan5");
2843 double i1, i2, i3, i4;
2844 double lower = -2, upper = 2;
2846 for (i1 = lower; i1 <= upper; i1++)
2848 for (i2 = lower; i2 <= upper; i2++)
2850 for (i3 = lower; i3 <= upper; i3++)
2852 for (i4 = lower; i4 <= upper; i4++)
2854 gsl_matrix_set (A22, 0,0, i1);
2855 gsl_matrix_set (A22, 0,1, i2);
2856 gsl_matrix_set (A22, 1,0, i3);
2857 gsl_matrix_set (A22, 1,1, i4);
2859 f = test_SV_decomp_jacobi_dim(A22, 16 * GSL_DBL_EPSILON);
2860 gsl_test(f, " SV_decomp_jacobi (2x2) A=[%g, %g; %g, %g]", i1,i2,i3,i4);
2870 double carry = 0, lower = 0, upper = 1;
2871 double *a = A33->data;
2873 for (i=0; i<9; i++) {
2877 while (carry == 0.0) {
2878 f = test_SV_decomp_jacobi_dim(A33, 64 * GSL_DBL_EPSILON);
2879 gsl_test(f, " SV_decomp_jacobi (3x3) A=[ %g, %g, %g; %g, %g, %g; %g, %g, %g]",
2880 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
2884 for (i=9; carry > 0.0 && i>0 && i--;)
2886 double v=a[i]+carry;
2887 carry = (v>upper) ? 1.0 : 0.0;
2888 a[i] = (v>upper) ? lower : v;
2896 unsigned long k = 0;
2897 double carry = 0, lower = 0, upper = 1;
2898 double *a = A44->data;
2900 for (i=0; i<16; i++) {
2904 while (carry == 0.0) {
2906 f = test_SV_decomp_jacobi_dim(A44, 64 * GSL_DBL_EPSILON);
2907 gsl_test(f, " SV_decomp_jacobi (4x4) A=[ %g, %g, %g, %g; %g, %g, %g, %g; %g, %g, %g, %g; %g, %g, %g, %g] %lu",
2908 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
2909 a[10], a[11], a[12], a[13], a[14], a[15], k);
2912 for (i=16; carry > 0.0 && i>0 && i--;)
2914 double v=a[i]+carry;
2915 carry = (v>upper) ? 1.0 : 0.0;
2916 a[i] = (v>upper) ? lower : v;
2924 unsigned long k = 0;
2925 double carry = 0, lower = 0, upper = 1;
2926 double *a = A55->data;
2928 for (i=0; i<25; i++) {
2932 while (carry == 0.0) {
2937 f = test_SV_decomp_jacobi_dim(A55, 64 * GSL_DBL_EPSILON);
2938 gsl_test(f, " SV_decomp_jacobi (5x5) case=%lu",k);
2943 for (i=25; carry >0.0 && i>0 && i--;)
2945 double v=a[i]+carry;
2946 carry = (v>upper) ? 1.0 : 0.0;
2947 a[i] = (v>upper) ? lower : v;
2958 test_cholesky_solve_dim(const gsl_matrix * m, const double * actual, double eps)
2961 unsigned long i, dim = m->size1;
2963 gsl_vector * rhs = gsl_vector_alloc(dim);
2964 gsl_matrix * u = gsl_matrix_alloc(dim,dim);
2965 gsl_vector * x = gsl_vector_calloc(dim);
2966 gsl_matrix_memcpy(u,m);
2967 for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);
2968 s += gsl_linalg_cholesky_decomp(u);
2969 s += gsl_linalg_cholesky_solve(u, rhs, x);
2970 for(i=0; i<dim; i++) {
2971 int foo = check(gsl_vector_get(x, i), actual[i], eps);
2973 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
2979 gsl_vector_free(rhs);
2984 int test_cholesky_solve(void)
2989 f = test_cholesky_solve_dim(hilb2, hilb2_solution, 2 * 8.0 * GSL_DBL_EPSILON);
2990 gsl_test(f, " cholesky_solve hilbert(2)");
2993 f = test_cholesky_solve_dim(hilb3, hilb3_solution, 2 * 64.0 * GSL_DBL_EPSILON);
2994 gsl_test(f, " cholesky_solve hilbert(3)");
2997 f = test_cholesky_solve_dim(hilb4, hilb4_solution, 2 * 1024.0 * GSL_DBL_EPSILON);
2998 gsl_test(f, " cholesky_solve hilbert(4)");
3001 f = test_cholesky_solve_dim(hilb12, hilb12_solution, 0.5);
3002 gsl_test(f, " cholesky_solve hilbert(12)");
3010 test_cholesky_decomp_dim(const gsl_matrix * m, double eps)
3013 unsigned long i,j, M = m->size1, N = m->size2;
3015 gsl_matrix * v = gsl_matrix_alloc(M,N);
3016 gsl_matrix * a = gsl_matrix_alloc(M,N);
3017 gsl_matrix * l = gsl_matrix_alloc(M,N);
3018 gsl_matrix * lt = gsl_matrix_alloc(N,N);
3020 gsl_matrix_memcpy(v,m);
3022 s += gsl_linalg_cholesky_decomp(v);
3026 for (i = 0; i < N ; i++)
3028 for (j = 0; j < N; j++)
3030 double vij = gsl_matrix_get(v, i, j);
3031 gsl_matrix_set (l, i, j, i>=j ? vij : 0);
3032 gsl_matrix_set (lt, i, j, i<=j ? vij : 0);
3036 /* compute a = l lt */
3037 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, l, lt, 0.0, a);
3039 for(i=0; i<M; i++) {
3040 for(j=0; j<N; j++) {
3041 double aij = gsl_matrix_get(a, i, j);
3042 double mij = gsl_matrix_get(m, i, j);
3043 int foo = check(aij, mij, eps);
3045 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
3054 gsl_matrix_free(lt);
3059 int test_cholesky_decomp(void)
3064 f = test_cholesky_decomp_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
3065 gsl_test(f, " cholesky_decomp hilbert(2)");
3068 f = test_cholesky_decomp_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
3069 gsl_test(f, " cholesky_decomp hilbert(3)");
3072 f = test_cholesky_decomp_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
3073 gsl_test(f, " cholesky_decomp hilbert(4)");
3076 f = test_cholesky_decomp_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
3077 gsl_test(f, " cholesky_decomp hilbert(12)");
3085 test_cholesky_decomp_unit_dim(const gsl_matrix * m, double eps)
3088 const unsigned long M = m->size1;
3089 const unsigned long N = m->size2;
3092 gsl_matrix * v = gsl_matrix_alloc(M,N);
3093 gsl_matrix * a = gsl_matrix_alloc(M,N);
3094 gsl_matrix * l = gsl_matrix_alloc(M,N);
3095 gsl_matrix * lt = gsl_matrix_alloc(N,N);
3096 gsl_matrix * dm = gsl_matrix_alloc(M,N);
3097 gsl_vector * dv = gsl_vector_alloc(M);
3099 gsl_matrix_memcpy(v,m);
3101 s += gsl_linalg_cholesky_decomp_unit(v, dv);
3104 for(i = 0; i < M; i++)
3106 for(j = 0; j < N; j++)
3108 printf("v[%lu,%lu]: %22.18e\n", i,j, gsl_matrix_get(v, i, j));
3113 for(i = 0; i < M; i++)
3115 printf("d[%lu]: %22.18e\n", i, gsl_vector_get(dv, i));
3119 /* put L and transpose(L) into separate matrices */
3121 for(i = 0; i < N ; i++)
3123 for(j = 0; j < N; j++)
3125 const double vij = gsl_matrix_get(v, i, j);
3126 gsl_matrix_set (l, i, j, i>=j ? vij : 0);
3127 gsl_matrix_set (lt, i, j, i<=j ? vij : 0);
3131 /* put D into its own matrix */
3133 gsl_matrix_set_zero(dm);
3134 for(i = 0; i < M; ++i) gsl_matrix_set(dm, i, i, gsl_vector_get(dv, i));
3136 /* compute a = L * D * transpose(L); uses v for temp space */
3138 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, dm, lt, 0.0, v);
3139 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, l, v, 0.0, a);
3141 for(i = 0; i < M; i++)
3143 for(j = 0; j < N; j++)
3145 const double aij = gsl_matrix_get(a, i, j);
3146 const double mij = gsl_matrix_get(m, i, j);
3147 int foo = check(aij, mij, eps);
3150 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
3156 gsl_vector_free(dv);
3157 gsl_matrix_free(dm);
3158 gsl_matrix_free(lt);
3166 int test_cholesky_decomp_unit(void)
3171 f = test_cholesky_decomp_unit_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
3172 gsl_test(f, " cholesky_decomp_unit hilbert(2)");
3175 f = test_cholesky_decomp_unit_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
3176 gsl_test(f, " cholesky_decomp_unit hilbert(3)");
3179 f = test_cholesky_decomp_unit_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
3180 gsl_test(f, " cholesky_decomp_unit hilbert(4)");
3183 f = test_cholesky_decomp_unit_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
3184 gsl_test(f, " cholesky_decomp_unit hilbert(12)");
3191 test_choleskyc_solve_dim(const gsl_matrix_complex * m, const gsl_vector_complex * actual, double eps)
3194 unsigned long i, dim = m->size1;
3196 gsl_vector_complex * rhs = gsl_vector_complex_alloc(dim);
3197 gsl_matrix_complex * u = gsl_matrix_complex_alloc(dim,dim);
3198 gsl_vector_complex * x = gsl_vector_complex_calloc(dim);
3199 GSL_SET_IMAG(&z, 0.0);
3200 gsl_matrix_complex_memcpy(u,m);
3201 for(i=0; i<dim; i++)
3203 GSL_SET_REAL(&z, i + 1.0);
3204 gsl_vector_complex_set(rhs, i, z);
3206 s += gsl_linalg_complex_cholesky_decomp(u);
3207 s += gsl_linalg_complex_cholesky_solve(u, rhs, x);
3208 for(i=0; i<dim; i++) {
3209 gsl_complex y = gsl_vector_complex_get(x, i);
3210 gsl_complex a = gsl_vector_complex_get(actual, i);
3211 int foo = check(GSL_REAL(y), GSL_REAL(a), eps);
3212 int foo2 = check(GSL_IMAG(y), GSL_IMAG(a), eps);
3214 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, GSL_REAL(y), GSL_REAL(a));
3215 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, GSL_IMAG(y), GSL_IMAG(a));
3219 gsl_vector_complex_free(x);
3220 gsl_matrix_complex_free(u);
3221 gsl_vector_complex_free(rhs);
3224 } /* test_choleskyc_solve_dim() */
3227 test_choleskyc_solve(void)
3229 double data7[] = { 66,0, 0,64, 126,63, 124,-62, 61,-61, 60,60, 0,-59,
3230 0,-64, 65,0, 62,-124, -61,-122, -60,-60, 59,-59, -58,0,
3231 126,-63, 62,124, 308,0, 180,-240, 59,-177, 174,58, -57,-114,
3232 124,62, -61,122, 180,240, 299,0, 174,-58, 57,171, 56,-112,
3233 61,61, -60,60, 59,177, 174,58, 119,0, 0,112, 55,-55,
3234 60,-60, 59,59, 174,-58, 57,-171, 0,-112, 116,0, -54,-54,
3235 0,59, -58,0, -57,114, 56,112, 55,55, -54,54, 60,0 };
3236 double data7_sol[] = { -0.524944196428570,0.209123883928571,
3237 1.052873883928572,0.712444196428571,
3238 0.117568824404762,0.443191964285714,
3239 0.412862723214286,-0.356696428571429,
3240 0.815931919642858,-0.265820312500000,
3241 0.777929687500000,0.119484747023810,
3242 1.058733258928571,-0.132087053571429 };
3243 gsl_matrix_complex_view cp7 = gsl_matrix_complex_view_array(data7, 7, 7);
3244 gsl_vector_complex_view cp7_sol = gsl_vector_complex_view_array(data7_sol, 7);
3248 f = test_choleskyc_solve_dim(&cp7.matrix, &cp7_sol.vector, 1024.0 * 1024.0 * GSL_DBL_EPSILON);
3249 gsl_test(f, " complex_cholesky_solve complex(7)");
3253 } /* test_choleskyc_solve() */
3256 test_choleskyc_decomp_dim(const gsl_matrix_complex * m, double eps)
3259 unsigned long i,j, M = m->size1, N = m->size2;
3261 gsl_matrix_complex * v = gsl_matrix_complex_alloc(M,N);
3262 gsl_matrix_complex * a = gsl_matrix_complex_alloc(M,N);
3263 gsl_matrix_complex * l = gsl_matrix_complex_alloc(M,N);
3264 gsl_matrix_complex * lh = gsl_matrix_complex_alloc(N,N);
3266 gsl_matrix_complex_memcpy(v, m);
3267 gsl_matrix_complex_set_zero(l);
3268 gsl_matrix_complex_set_zero(lh);
3270 s += gsl_linalg_complex_cholesky_decomp(v);
3274 for (i = 0; i < N ; i++)
3276 for (j = 0; j <= i; j++)
3278 gsl_complex vij = gsl_matrix_complex_get(v, i, j);
3279 gsl_matrix_complex_set (l, i, j, vij);
3280 gsl_matrix_complex_set (lh, j, i, gsl_complex_conjugate(vij));
3284 /* compute a = l lh */
3285 gsl_blas_zgemm (CblasNoTrans,
3293 for(i=0; i<M; i++) {
3294 for(j=0; j<N; j++) {
3295 gsl_complex aij = gsl_matrix_complex_get(a, i, j);
3296 gsl_complex mij = gsl_matrix_complex_get(m, i, j);
3297 int foo_r = check(GSL_REAL(aij), GSL_REAL(mij), eps);
3298 int foo_i = check(GSL_IMAG(aij), GSL_IMAG(mij), eps);
3299 if(foo_r || foo_i) {
3300 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, GSL_REAL(aij), GSL_REAL(mij));
3301 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, GSL_IMAG(aij), GSL_IMAG(mij));
3307 gsl_matrix_complex_free(v);
3308 gsl_matrix_complex_free(a);
3309 gsl_matrix_complex_free(l);
3310 gsl_matrix_complex_free(lh);
3316 test_choleskyc_decomp(void)
3320 double dat3[] = { 59.75,0, 49.25,172.25, 66.75,-162.75,
3321 49.25,-172.25, 555.5,0, -429,-333.5,
3322 66.75,162.75, -429,333.5, 536.5,0 };
3323 gsl_matrix_complex_view p3 = gsl_matrix_complex_view_array(dat3, 3, 3);
3325 f = test_choleskyc_decomp_dim(&p3.matrix, 2 * 8.0 * GSL_DBL_EPSILON);
3326 gsl_test(f, " complex_cholesky_decomp complex(3)");
3333 test_HH_solve_dim(const gsl_matrix * m, const double * actual, double eps)
3336 unsigned long i, dim = m->size1;
3338 gsl_permutation * perm = gsl_permutation_alloc(dim);
3339 gsl_matrix * hh = gsl_matrix_alloc(dim,dim);
3340 gsl_vector * d = gsl_vector_alloc(dim);
3341 gsl_vector * x = gsl_vector_alloc(dim);
3342 gsl_matrix_memcpy(hh,m);
3343 for(i=0; i<dim; i++) gsl_vector_set(x, i, i+1.0);
3344 s += gsl_linalg_HH_svx(hh, x);
3345 for(i=0; i<dim; i++) {
3346 int foo = check(gsl_vector_get(x, i),actual[i],eps);
3348 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
3354 gsl_matrix_free(hh);
3355 gsl_permutation_free(perm);
3360 int test_HH_solve(void)
3365 f = test_HH_solve_dim(hilb2, hilb2_solution, 8.0 * GSL_DBL_EPSILON);
3366 gsl_test(f, " HH_solve hilbert(2)");
3369 f = test_HH_solve_dim(hilb3, hilb3_solution, 128.0 * GSL_DBL_EPSILON);
3370 gsl_test(f, " HH_solve hilbert(3)");
3373 f = test_HH_solve_dim(hilb4, hilb4_solution, 2.0 * 1024.0 * GSL_DBL_EPSILON);
3374 gsl_test(f, " HH_solve hilbert(4)");
3377 f = test_HH_solve_dim(hilb12, hilb12_solution, 0.5);
3378 gsl_test(f, " HH_solve hilbert(12)");
3381 f = test_HH_solve_dim(vander2, vander2_solution, 8.0 * GSL_DBL_EPSILON);
3382 gsl_test(f, " HH_solve vander(2)");
3385 f = test_HH_solve_dim(vander3, vander3_solution, 64.0 * GSL_DBL_EPSILON);
3386 gsl_test(f, " HH_solve vander(3)");
3389 f = test_HH_solve_dim(vander4, vander4_solution, 1024.0 * GSL_DBL_EPSILON);
3390 gsl_test(f, " HH_solve vander(4)");
3393 f = test_HH_solve_dim(vander12, vander12_solution, 0.05);
3394 gsl_test(f, " HH_solve vander(12)");
3402 test_TDS_solve_dim(unsigned long dim, double d, double od, const double * actual, double eps)
3407 gsl_vector * offdiag = gsl_vector_alloc(dim-1);
3408 gsl_vector * diag = gsl_vector_alloc(dim);
3409 gsl_vector * rhs = gsl_vector_alloc(dim);
3410 gsl_vector * x = gsl_vector_alloc(dim);
3412 for(i=0; i<dim; i++) {
3413 gsl_vector_set(diag, i, d);
3414 gsl_vector_set(rhs, i, i + 1.0);
3416 for(i=0; i<dim-1; i++) {
3417 gsl_vector_set(offdiag, i, od);
3420 s += gsl_linalg_solve_symm_tridiag(diag, offdiag, rhs, x);
3422 for(i=0; i<dim; i++) {
3423 double si = gsl_vector_get(x, i);
3424 double ai = actual[i];
3425 int foo = check(si, ai, eps);
3427 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
3433 gsl_vector_free(rhs);
3434 gsl_vector_free(diag);
3435 gsl_vector_free(offdiag);
3441 int test_TDS_solve(void)
3447 double actual[] = {0.0, 2.0};
3448 f = test_TDS_solve_dim(2, 1.0, 0.5, actual, 8.0 * GSL_DBL_EPSILON);
3449 gsl_test(f, " solve_TDS dim=2 A");
3454 double actual[] = {3.0/8.0, 15.0/8.0};
3455 f = test_TDS_solve_dim(2, 1.0, 1.0/3.0, actual, 8.0 * GSL_DBL_EPSILON);
3456 gsl_test(f, " solve_TDS dim=2 B");
3461 double actual[] = {5.0/8.0, 9.0/8.0, 2.0, 15.0/8.0, 35.0/8.0};
3462 f = test_TDS_solve_dim(5, 1.0, 1.0/3.0, actual, 8.0 * GSL_DBL_EPSILON);
3463 gsl_test(f, " solve_TDS dim=5");
3471 test_TDS_cyc_solve_one(const unsigned long dim, const double * d, const double * od,
3472 const double * r, const double * actual, double eps)
3477 gsl_vector * offdiag = gsl_vector_alloc(dim);
3478 gsl_vector * diag = gsl_vector_alloc(dim);
3479 gsl_vector * rhs = gsl_vector_alloc(dim);
3480 gsl_vector * x = gsl_vector_alloc(dim);
3482 for(i=0; i<dim; i++) {
3483 gsl_vector_set(diag, i, d[i]);
3484 gsl_vector_set(rhs, i, r[i]);
3485 gsl_vector_set(offdiag, i, od[i]);
3488 s += gsl_linalg_solve_symm_cyc_tridiag(diag, offdiag, rhs, x);
3490 for(i=0; i<dim; i++) {
3491 double si = gsl_vector_get(x, i);
3492 double ai = actual[i];
3493 int foo = check(si, ai, eps);
3495 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
3501 gsl_vector_free(rhs);
3502 gsl_vector_free(diag);
3503 gsl_vector_free(offdiag);
3508 int test_TDS_cyc_solve(void)
3513 #ifdef SUPPORT_UNDERSIZE_CYC
3515 unsigned long dim = 1;
3516 double diag[] = { 2 };
3517 double offdiag[] = { 3 };
3518 double rhs[] = { 7 };
3519 double actual[] = { 3.5 };
3521 f = test_TDS_cyc_solve_one(dim, diag, offdiag, rhs, actual, 28.0 * GSL_DBL_EPSILON);
3522 gsl_test(f, " solve_TDS_cyc dim=%lu A", dim);
3527 unsigned long dim = 2;
3528 double diag[] = { 1, 2 };
3529 double offdiag[] = { 3, 4 };
3530 double rhs[] = { 7, -7 };
3531 double actual[] = { -5, 4 };
3533 f = test_TDS_cyc_solve_one(dim, diag, offdiag, rhs, actual, 28.0 * GSL_DBL_EPSILON);
3534 gsl_test(f, " solve_TDS_cyc dim=%lu A", dim);
3540 unsigned long dim = 3;
3541 double diag[] = { 1, 1, 1 };
3542 double offdiag[] = { 3, 3, 3 };
3543 double rhs[] = { 7, -7, 7 };
3544 double actual[] = { -2, 5, -2 };
3546 f = test_TDS_cyc_solve_one(dim, diag, offdiag, rhs, actual, 28.0 * GSL_DBL_EPSILON);
3547 gsl_test(f, " solve_TDS_cyc dim=%lu A", dim);
3552 unsigned long dim = 5;
3553 double diag[] = { 4, 2, 1, 2, 4 };
3554 double offdiag[] = { 1, 1, 1, 1, 1 };
3555 double rhs[] = { 30, -24, 3, 21, -30 };
3556 double actual[] = { 12, 3, -42, 42, -21 };
3558 /* f = test_TDS_cyc_solve_one(dim, diag, offdiag, rhs, actual, 7.0 * GSL_DBL_EPSILON);
3559 FIXME: bad accuracy */
3560 f = test_TDS_cyc_solve_one(dim, diag, offdiag, rhs, actual, 35.0 * GSL_DBL_EPSILON);
3561 gsl_test(f, " solve_TDS_cyc dim=%lu B", dim);
3569 test_TDN_solve_dim(unsigned long dim, double d, double a, double b, const double * actual, double eps)
3574 gsl_vector * abovediag = gsl_vector_alloc(dim-1);
3575 gsl_vector * belowdiag = gsl_vector_alloc(dim-1);
3576 gsl_vector * diag = gsl_vector_alloc(dim);
3577 gsl_vector * rhs = gsl_vector_alloc(dim);
3578 gsl_vector * x = gsl_vector_alloc(dim);
3580 for(i=0; i<dim; i++) {
3581 gsl_vector_set(diag, i, d);
3582 gsl_vector_set(rhs, i, i + 1.0);
3584 for(i=0; i<dim-1; i++) {
3585 gsl_vector_set(abovediag, i, a);
3586 gsl_vector_set(belowdiag, i, b);
3589 s += gsl_linalg_solve_tridiag(diag, abovediag, belowdiag, rhs, x);
3591 for(i=0; i<dim; i++) {
3592 double si = gsl_vector_get(x, i);
3593 double ai = actual[i];
3594 int foo = check(si, ai, eps);
3596 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
3602 gsl_vector_free(rhs);
3603 gsl_vector_free(diag);
3604 gsl_vector_free(abovediag);
3605 gsl_vector_free(belowdiag);
3611 int test_TDN_solve(void)
3617 actual[0] = -7.0/3.0;
3618 actual[1] = 5.0/3.0;
3619 actual[2] = 4.0/3.0;
3620 f = test_TDN_solve_dim(3, 1.0, 2.0, 1.0, actual, 2.0 * GSL_DBL_EPSILON);
3621 gsl_test(f, " solve_TDN dim=2 A");
3627 f = test_TDN_solve_dim(3, 1.0, 1.0/3.0, 1.0/2.0, actual, 2.0 * GSL_DBL_EPSILON);
3628 gsl_test(f, " solve_TDN dim=2 B");
3631 actual[0] = 99.0/140.0;
3632 actual[1] = 41.0/35.0;
3633 actual[2] = 19.0/10.0;
3634 actual[3] = 72.0/35.0;
3635 actual[4] = 139.0/35.0;
3636 f = test_TDN_solve_dim(5, 1.0, 1.0/4.0, 1.0/2.0, actual, 35.0/8.0 * GSL_DBL_EPSILON);
3637 gsl_test(f, " solve_TDN dim=5");
3644 test_TDN_cyc_solve_dim(unsigned long dim, double d, double a, double b, const double * actual, double eps)
3649 gsl_vector * abovediag = gsl_vector_alloc(dim);
3650 gsl_vector * belowdiag = gsl_vector_alloc(dim);
3651 gsl_vector * diag = gsl_vector_alloc(dim);
3652 gsl_vector * rhs = gsl_vector_alloc(dim);
3653 gsl_vector * x = gsl_vector_alloc(dim);
3655 for(i=0; i<dim; i++) {
3656 gsl_vector_set(diag, i, d);
3657 gsl_vector_set(rhs, i, i + 1.0);
3659 for(i=0; i<dim; i++) {
3660 gsl_vector_set(abovediag, i, a);
3661 gsl_vector_set(belowdiag, i, b);
3664 s += gsl_linalg_solve_cyc_tridiag(diag, abovediag, belowdiag, rhs, x);
3666 for(i=0; i<dim; i++) {
3667 double si = gsl_vector_get(x, i);
3668 double ai = actual[i];
3669 int foo = check(si, ai, eps);
3671 printf("%3lu[%lu]: %22.18g %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);
3677 gsl_vector_free(rhs);
3678 gsl_vector_free(diag);
3679 gsl_vector_free(abovediag);
3680 gsl_vector_free(belowdiag);
3686 int test_TDN_cyc_solve(void)
3692 actual[0] = 3.0/2.0;
3693 actual[1] = -1.0/2.0;
3694 actual[2] = 1.0/2.0;
3695 f = test_TDN_cyc_solve_dim(3, 1.0, 2.0, 1.0, actual, 32.0 * GSL_DBL_EPSILON);
3696 gsl_test(f, " solve_TDN_cyc dim=2 A");
3699 actual[0] = -5.0/22.0;
3700 actual[1] = -3.0/22.0;
3701 actual[2] = 29.0/22.0;
3702 actual[3] = -9.0/22.0;
3703 actual[4] = 43.0/22.0;
3704 f = test_TDN_cyc_solve_dim(5, 3.0, 2.0, 1.0, actual, 66.0 * GSL_DBL_EPSILON);
3705 gsl_test(f, " solve_TDN_cyc dim=5");
3712 test_bidiag_decomp_dim(const gsl_matrix * m, double eps)
3715 unsigned long i,j,k,r, M = m->size1, N = m->size2;
3717 gsl_matrix * A = gsl_matrix_alloc(M,N);
3718 gsl_matrix * a = gsl_matrix_alloc(M,N);
3719 gsl_matrix * b = gsl_matrix_alloc(N,N);
3721 gsl_matrix * u = gsl_matrix_alloc(M,N);
3722 gsl_matrix * v = gsl_matrix_alloc(N,N);
3724 gsl_vector * tau1 = gsl_vector_alloc(N);
3725 gsl_vector * tau2 = gsl_vector_alloc(N-1);
3726 gsl_vector * d = gsl_vector_alloc(N);
3727 gsl_vector * sd = gsl_vector_alloc(N-1);
3729 gsl_matrix_memcpy(A,m);
3731 s += gsl_linalg_bidiag_decomp(A, tau1, tau2);
3732 s += gsl_linalg_bidiag_unpack(A, tau1, u, tau2, v, d, sd);
3734 gsl_matrix_set_zero(b);
3735 for (i = 0; i < N; i++) gsl_matrix_set(b, i,i, gsl_vector_get(d,i));
3736 for (i = 0; i < N-1; i++) gsl_matrix_set(b, i,i+1, gsl_vector_get(sd,i));
3738 /* Compute A = U B V^T */
3740 for (i = 0; i < M ; i++)
3742 for (j = 0; j < N; j++)
3746 for (k = 0; k < N; k++)
3748 for (r = 0; r < N; r++)
3750 sum += gsl_matrix_get(u, i, k) * gsl_matrix_get (b, k, r)
3751 * gsl_matrix_get(v, j, r);
3754 gsl_matrix_set (a, i, j, sum);
3758 for(i=0; i<M; i++) {
3759 for(j=0; j<N; j++) {
3760 double aij = gsl_matrix_get(a, i, j);
3761 double mij = gsl_matrix_get(m, i, j);
3762 int foo = check(aij, mij, eps);
3764 printf("(%3lu,%3lu)[%lu,%lu]: %22.18g %22.18g\n", M, N, i,j, aij, mij);
3775 gsl_vector_free(tau1);
3776 gsl_vector_free(tau2);
3778 gsl_vector_free(sd);
3783 int test_bidiag_decomp(void)
3788 f = test_bidiag_decomp_dim(m53, 2 * 64.0 * GSL_DBL_EPSILON);
3789 gsl_test(f, " bidiag_decomp m(5,3)");
3792 f = test_bidiag_decomp_dim(m97, 2 * 64.0 * GSL_DBL_EPSILON);
3793 gsl_test(f, " bidiag_decomp m(9,7)");
3796 f = test_bidiag_decomp_dim(hilb2, 2 * 8.0 * GSL_DBL_EPSILON);
3797 gsl_test(f, " bidiag_decomp hilbert(2)");
3800 f = test_bidiag_decomp_dim(hilb3, 2 * 64.0 * GSL_DBL_EPSILON);
3801 gsl_test(f, " bidiag_decomp hilbert(3)");
3804 f = test_bidiag_decomp_dim(hilb4, 2 * 1024.0 * GSL_DBL_EPSILON);
3805 gsl_test(f, " bidiag_decomp hilbert(4)");
3808 f = test_bidiag_decomp_dim(hilb12, 2 * 1024.0 * GSL_DBL_EPSILON);
3809 gsl_test(f, " bidiag_decomp hilbert(12)");
3816 my_error_handler (const char *reason, const char *file, int line, int err)
3818 if (0) printf ("(caught [%s:%d: %s (%d)])\n", file, line, reason, err) ;
3823 gsl_ieee_env_setup ();
3824 gsl_set_error_handler (&my_error_handler);
3826 m11 = create_general_matrix(1,1);
3827 m51 = create_general_matrix(5,1);
3829 m35 = create_general_matrix(3,5);
3830 m53 = create_general_matrix(5,3);
3831 m97 = create_general_matrix(9,7);
3833 s35 = create_singular_matrix(3,5);
3834 s53 = create_singular_matrix(5,3);
3836 hilb2 = create_hilbert_matrix(2);
3837 hilb3 = create_hilbert_matrix(3);
3838 hilb4 = create_hilbert_matrix(4);
3839 hilb12 = create_hilbert_matrix(12);
3841 vander2 = create_vandermonde_matrix(2);
3842 vander3 = create_vandermonde_matrix(3);
3843 vander4 = create_vandermonde_matrix(4);
3844 vander12 = create_vandermonde_matrix(12);
3846 moler10 = create_moler_matrix(10);
3848 c7 = create_complex_matrix(7);
3850 row3 = create_row_matrix(3,3);
3851 row5 = create_row_matrix(5,5);
3852 row12 = create_row_matrix(12,12);
3854 A22 = create_2x2_matrix (0.0, 0.0, 0.0, 0.0);
3855 A33 = gsl_matrix_alloc(3,3);
3856 A44 = gsl_matrix_alloc(4,4);
3857 A55 = gsl_matrix_alloc(5,5);
3859 inf5 = create_diagonal_matrix (inf5_data, 5);
3860 gsl_matrix_set(inf5, 3, 3, GSL_POSINF);
3862 nan5 = create_diagonal_matrix (inf5_data, 5);
3863 gsl_matrix_set(nan5, 3, 3, GSL_NAN);
3865 dblmin3 = create_general_matrix (3, 3);
3866 gsl_matrix_scale(dblmin3, GSL_DBL_MIN);
3868 dblmin5 = create_general_matrix (5, 5);
3869 gsl_matrix_scale(dblmin5, GSL_DBL_MIN);
3871 /* Matmult now obsolete */
3873 gsl_test(test_matmult(), "Matrix Multiply");
3874 gsl_test(test_matmult_mod(), "Matrix Multiply with Modification");
3876 gsl_test(test_bidiag_decomp(), "Bidiagonal Decomposition");
3877 gsl_test(test_LU_solve(), "LU Decomposition and Solve");
3878 gsl_test(test_LUc_solve(), "Complex LU Decomposition and Solve");
3879 gsl_test(test_QR_decomp(), "QR Decomposition");
3880 gsl_test(test_QR_solve(), "QR Solve");
3881 gsl_test(test_LQ_solve(), "LQ Solve");
3882 gsl_test(test_PTLQ_solve(), "PTLQ Solve");
3884 gsl_test(test_LQ_decomp(), "LQ Decomposition");
3885 gsl_test(test_LQ_LQsolve(), "LQ LQ Solve");
3886 gsl_test(test_LQ_lssolve(), "LQ LS Solve");
3887 gsl_test(test_LQ_update(), "LQ Rank-1 Update");
3888 gsl_test(test_QRPT_decomp(), "PTLQ Decomposition");
3889 gsl_test(test_PTLQ_solve(), "PTLQ Solve");
3891 gsl_test(test_QR_QRsolve(), "QR QR Solve");
3892 gsl_test(test_QR_lssolve(), "QR LS Solve");
3893 gsl_test(test_QR_update(), "QR Rank-1 Update");
3894 gsl_test(test_QRPT_decomp(), "QRPT Decomposition");
3895 gsl_test(test_QRPT_solve(), "QRPT Solve");
3896 gsl_test(test_QRPT_QRsolve(), "QRPT QR Solve");
3897 gsl_test(test_SV_decomp(), "Singular Value Decomposition");
3898 gsl_test(test_SV_decomp_jacobi(), "Singular Value Decomposition (Jacobi)");
3899 gsl_test(test_SV_decomp_mod(), "Singular Value Decomposition (Mod)");
3900 gsl_test(test_SV_solve(), "SVD Solve");
3901 gsl_test(test_cholesky_decomp(), "Cholesky Decomposition");
3902 gsl_test(test_cholesky_decomp_unit(), "Cholesky Decomposition [unit triangular]");
3903 gsl_test(test_cholesky_solve(), "Cholesky Solve");
3904 gsl_test(test_choleskyc_decomp(), "Complex Cholesky Decomposition");
3905 gsl_test(test_choleskyc_solve(), "Complex Cholesky Solve");
3906 gsl_test(test_HH_solve(), "Householder solve");
3907 gsl_test(test_TDS_solve(), "Tridiagonal symmetric solve");
3908 gsl_test(test_TDS_cyc_solve(), "Tridiagonal symmetric cyclic solve");
3909 gsl_test(test_TDN_solve(), "Tridiagonal nonsymmetric solve");
3910 gsl_test(test_TDN_cyc_solve(), "Tridiagonal nonsymmetric cyclic solve");
3912 gsl_matrix_free(m11);
3913 gsl_matrix_free(m35);
3914 gsl_matrix_free(m51);
3915 gsl_matrix_free(m53);
3916 gsl_matrix_free(m97);
3917 gsl_matrix_free(s35);
3918 gsl_matrix_free(s53);
3920 gsl_matrix_free(hilb2);
3921 gsl_matrix_free(hilb3);
3922 gsl_matrix_free(hilb4);
3923 gsl_matrix_free(hilb12);
3925 gsl_matrix_free(vander2);
3926 gsl_matrix_free(vander3);
3927 gsl_matrix_free(vander4);
3928 gsl_matrix_free(vander12);
3930 gsl_matrix_free(moler10);
3932 gsl_matrix_complex_free(c7);
3933 gsl_matrix_free(row3);
3934 gsl_matrix_free(row5);
3935 gsl_matrix_free(row12);
3937 gsl_matrix_free(A22);
3938 gsl_matrix_free(A33);
3939 gsl_matrix_free(A44);
3940 gsl_matrix_free(A55);
3942 gsl_matrix_free (inf5);
3943 gsl_matrix_free (nan5);
3945 exit (gsl_test_summary());