📄 test.c
字号:
/* linalg/test.c * * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005 * Gerard Jungman, Brian Gough * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *//* Author: G. Jungman */#include <config.h>#include <stdlib.h>#include <gsl/gsl_test.h>#include <gsl/gsl_math.h>#include <gsl/gsl_ieee_utils.h>#include <gsl/gsl_permute_vector.h>#include <gsl/gsl_blas.h>#include <gsl/gsl_complex_math.h>#include <gsl/gsl_linalg.h>#define TEST_SVD_4X4 1int check (double x, double actual, double eps);gsl_matrix * create_hilbert_matrix(unsigned long size);gsl_matrix * create_general_matrix(unsigned long size1, unsigned long size2);gsl_matrix * create_vandermonde_matrix(unsigned long size);gsl_matrix * create_moler_matrix(unsigned long size);gsl_matrix * create_row_matrix(unsigned long size1, unsigned long size2);gsl_matrix * create_2x2_matrix(double a11, double a12, double a21, double a22);gsl_matrix * create_diagonal_matrix(double a[], unsigned long size);int test_matmult(void);int test_matmult_mod(void);int test_LU_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_LU_solve(void);int test_LUc_solve_dim(const gsl_matrix_complex * m, const double * actual, double eps);int test_LUc_solve(void);int test_QR_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_QR_solve(void);int test_QR_QRsolve_dim(const gsl_matrix * m, const double * actual, double eps);int test_QR_QRsolve(void);int test_QR_lssolve_dim(const gsl_matrix * m, const double * actual, double eps);int test_QR_lssolve(void);int test_QR_decomp_dim(const gsl_matrix * m, double eps);int test_QR_decomp(void);int test_QRPT_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_QRPT_solve(void);int test_QRPT_QRsolve_dim(const gsl_matrix * m, const double * actual, double eps);int test_QRPT_QRsolve(void);int test_QRPT_decomp_dim(const gsl_matrix * m, double eps);int test_QRPT_decomp(void);int test_QR_update_dim(const gsl_matrix * m, double eps);int test_QR_update(void);int test_LQ_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_LQ_solve(void);int test_LQ_LQsolve_dim(const gsl_matrix * m, const double * actual, double eps);int test_LQ_LQsolve(void);int test_LQ_lssolve_dim(const gsl_matrix * m, const double * actual, double eps);int test_LQ_lssolve(void);int test_LQ_decomp_dim(const gsl_matrix * m, double eps);int test_LQ_decomp(void);int test_PTLQ_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_PTLQ_solve(void);int test_PTLQ_LQsolve_dim(const gsl_matrix * m, const double * actual, double eps);int test_PTLQ_LQsolve(void);int test_PTLQ_decomp_dim(const gsl_matrix * m, double eps);int test_PTLQ_decomp(void);int test_LQ_update_dim(const gsl_matrix * m, double eps);int test_LQ_update(void);int test_SV_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_SV_solve(void);int test_SV_decomp_dim(const gsl_matrix * m, double eps);int test_SV_decomp(void);int test_SV_decomp_mod_dim(const gsl_matrix * m, double eps);int test_SV_decomp_mod(void);int test_SV_decomp_jacobi_dim(const gsl_matrix * m, double eps);int test_SV_decomp_jacobi(void);int test_cholesky_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_cholesky_solve(void);int test_cholesky_decomp_dim(const gsl_matrix * m, double eps);int test_cholesky_decomp(void);int test_HH_solve_dim(const gsl_matrix * m, const double * actual, double eps);int test_HH_solve(void);int test_TDS_solve_dim(unsigned long dim, double d, double od, const double * actual, double eps);int test_TDS_solve(void);int test_TDN_solve_dim(unsigned long dim, double d, double a, double b, const double * actual, double eps);int test_TDN_solve(void);int test_TDS_cyc_solve_one(const unsigned long dim, const double * d, const double * od, const double * r, const double * actual, double eps);int test_TDS_cyc_solve(void);int test_TDN_cyc_solve_dim(unsigned long dim, double d, double a, double b, const double * actual, double eps);int test_TDN_cyc_solve(void);int test_bidiag_decomp_dim(const gsl_matrix * m, double eps);int test_bidiag_decomp(void);int check (double x, double actual, double eps){ if (x == actual) { return 0; } else if (actual == 0) { return fabs(x) > eps; } else { return (fabs(x - actual)/fabs(actual)) > eps; }}gsl_matrix *create_hilbert_matrix(unsigned long size){ unsigned long i, j; gsl_matrix * m = gsl_matrix_alloc(size, size); for(i=0; i<size; i++) { for(j=0; j<size; j++) { gsl_matrix_set(m, i, j, 1.0/(i+j+1.0)); } } return m;}gsl_matrix *create_general_matrix(unsigned long size1, unsigned long size2){ unsigned long i, j; gsl_matrix * m = gsl_matrix_alloc(size1, size2); for(i=0; i<size1; i++) { for(j=0; j<size2; j++) { gsl_matrix_set(m, i, j, 1.0/(i+j+1.0)); } } return m;}gsl_matrix *create_singular_matrix(unsigned long size1, unsigned long size2){ unsigned long i, j; gsl_matrix * m = gsl_matrix_alloc(size1, size2); for(i=0; i<size1; i++) { for(j=0; j<size2; j++) { gsl_matrix_set(m, i, j, 1.0/(i+j+1.0)); } } /* zero the first column */ for(j = 0; j <size2; j++) gsl_matrix_set(m,0,j,0.0); return m;}gsl_matrix *create_vandermonde_matrix(unsigned long size){ unsigned long i, j; gsl_matrix * m = gsl_matrix_alloc(size, size); for(i=0; i<size; i++) { for(j=0; j<size; j++) { gsl_matrix_set(m, i, j, pow(i + 1.0, size - j - 1.0)); } } return m;}gsl_matrix *create_moler_matrix(unsigned long size){ unsigned long i, j; gsl_matrix * m = gsl_matrix_alloc(size, size); for(i=0; i<size; i++) { for(j=0; j<size; j++) { gsl_matrix_set(m, i, j, GSL_MIN(i+1,j+1)-2.0); } } return m;}gsl_matrix_complex *create_complex_matrix(unsigned long size){ unsigned long i, j; gsl_matrix_complex * m = gsl_matrix_complex_alloc(size, size); for(i=0; i<size; i++) { for(j=0; j<size; j++) { gsl_complex z = gsl_complex_rect(1.0/(i+j+1.0), 1/(i*i+j*j+0.5)); gsl_matrix_complex_set(m, i, j, z); } } return m;}gsl_matrix *create_row_matrix(unsigned long size1, unsigned long size2){ unsigned long i; gsl_matrix * m = gsl_matrix_calloc(size1, size2); for(i=0; i<size1; i++) { gsl_matrix_set(m, i, 0, 1.0/(i+1.0)); } return m;}gsl_matrix *create_2x2_matrix(double a11, double a12, double a21, double a22){ gsl_matrix * m = gsl_matrix_alloc(2, 2); gsl_matrix_set(m, 0, 0, a11); gsl_matrix_set(m, 0, 1, a12); gsl_matrix_set(m, 1, 0, a21); gsl_matrix_set(m, 1, 1, a22); return m;}gsl_matrix *create_diagonal_matrix(double a[], unsigned long size){ unsigned long i; gsl_matrix * m = gsl_matrix_calloc(size, size); for(i=0; i<size; i++) { gsl_matrix_set(m, i, i, a[i]); } return m;}gsl_matrix * m11;gsl_matrix * m51;gsl_matrix * m35;gsl_matrix * m53;gsl_matrix * m97;gsl_matrix * s35;gsl_matrix * s53;gsl_matrix * hilb2;gsl_matrix * hilb3;gsl_matrix * hilb4;gsl_matrix * hilb12;gsl_matrix * row3;gsl_matrix * row5;gsl_matrix * row12;gsl_matrix * A22;gsl_matrix * A33;gsl_matrix * A44;gsl_matrix * A55;gsl_matrix_complex * c7;gsl_matrix * inf5; double inf5_data[] = {1.0, 0.0, -3.0, 0.0, -5.0};gsl_matrix * nan5;double m53_lssolution[] = {52.5992295702070, -337.7263113752073, 351.8823436427604};double hilb2_solution[] = {-8.0, 18.0} ;double hilb3_solution[] = {27.0, -192.0, 210.0};double hilb4_solution[] = {-64.0, 900.0, -2520.0, 1820.0};double hilb12_solution[] = {-1728.0, 245388.0, -8528520.0, 127026900.0, -1009008000.0, 4768571808.0, -14202796608.0, 27336497760.0, -33921201600.0, 26189163000.0, -11437874448.0, 2157916488.0 };double c7_solution[] = { 2.40717272023734e+01, -9.84612797621247e+00, -2.69338853034031e+02, 8.75455232472528e+01, 2.96661356736296e+03, -1.02624473923993e+03, -1.82073812124749e+04, 5.67384473042410e+03, 5.57693879019068e+04, -1.61540963210502e+04, -7.88941207561151e+04, 1.95053812987858e+04, 3.95548551241728e+04, -7.76593696255317e+03 };gsl_matrix * vander2;gsl_matrix * vander3;gsl_matrix * vander4;gsl_matrix * vander12;double vander2_solution[] = {1.0, 0.0}; double vander3_solution[] = {0.0, 1.0, 0.0}; double vander4_solution[] = {0.0, 0.0, 1.0, 0.0}; double vander12_solution[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0}; gsl_matrix * moler10;/* matmult now obsolete */#ifdef MATMULTinttest_matmult(void){ int s = 0; gsl_matrix * A = gsl_matrix_calloc(2, 2); gsl_matrix * B = gsl_matrix_calloc(2, 3); gsl_matrix * C = gsl_matrix_calloc(2, 3); gsl_matrix_set(A, 0, 0, 10.0); gsl_matrix_set(A, 0, 1, 5.0); gsl_matrix_set(A, 1, 0, 1.0); gsl_matrix_set(A, 1, 1, 20.0); gsl_matrix_set(B, 0, 0, 10.0); gsl_matrix_set(B, 0, 1, 5.0); gsl_matrix_set(B, 0, 2, 2.0); gsl_matrix_set(B, 1, 0, 1.0); gsl_matrix_set(B, 1, 1, 3.0); gsl_matrix_set(B, 1, 2, 2.0); gsl_linalg_matmult(A, B, C); s += ( fabs(gsl_matrix_get(C, 0, 0) - 105.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 1) - 65.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 2) - 30.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 0) - 30.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 1) - 65.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 2) - 42.0) > GSL_DBL_EPSILON ); gsl_matrix_free(A); gsl_matrix_free(B); gsl_matrix_free(C); return s;}inttest_matmult_mod(void){ int s = 0; gsl_matrix * A = gsl_matrix_calloc(3, 3); gsl_matrix * B = gsl_matrix_calloc(3, 3); gsl_matrix * C = gsl_matrix_calloc(3, 3); gsl_matrix * D = gsl_matrix_calloc(2, 3); gsl_matrix * E = gsl_matrix_calloc(2, 3); gsl_matrix * F = gsl_matrix_calloc(2, 2); gsl_matrix_set(A, 0, 0, 10.0); gsl_matrix_set(A, 0, 1, 5.0); gsl_matrix_set(A, 0, 2, 1.0); gsl_matrix_set(A, 1, 0, 1.0); gsl_matrix_set(A, 1, 1, 20.0); gsl_matrix_set(A, 1, 2, 5.0); gsl_matrix_set(A, 2, 0, 1.0); gsl_matrix_set(A, 2, 1, 3.0); gsl_matrix_set(A, 2, 2, 7.0); gsl_matrix_set(B, 0, 0, 10.0); gsl_matrix_set(B, 0, 1, 5.0); gsl_matrix_set(B, 0, 2, 2.0); gsl_matrix_set(B, 1, 0, 1.0); gsl_matrix_set(B, 1, 1, 3.0); gsl_matrix_set(B, 1, 2, 2.0); gsl_matrix_set(B, 2, 0, 1.0); gsl_matrix_set(B, 2, 1, 3.0); gsl_matrix_set(B, 2, 2, 2.0); gsl_matrix_set(D, 0, 0, 10.0); gsl_matrix_set(D, 0, 1, 5.0); gsl_matrix_set(D, 0, 2, 1.0); gsl_matrix_set(D, 1, 0, 1.0); gsl_matrix_set(D, 1, 1, 20.0); gsl_matrix_set(D, 1, 2, 5.0); gsl_matrix_set(E, 0, 0, 10.0); gsl_matrix_set(E, 0, 1, 5.0); gsl_matrix_set(E, 0, 2, 2.0); gsl_matrix_set(E, 1, 0, 1.0); gsl_matrix_set(E, 1, 1, 3.0); gsl_matrix_set(E, 1, 2, 2.0); gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_NONE, B, GSL_LINALG_MOD_NONE, C); s += ( fabs(gsl_matrix_get(C, 0, 0) - 106.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 1) - 68.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 2) - 32.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 0) - 35.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 1) - 80.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 2) - 52.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 0) - 20.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 1) - 35.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 2) - 22.0) > GSL_DBL_EPSILON ); gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_TRANSPOSE, B, GSL_LINALG_MOD_NONE, C); s += ( fabs(gsl_matrix_get(C, 0, 0) - 102.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 1) - 56.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 2) - 24.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 0) - 73.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 1) - 94.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 2) - 56.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 0) - 22.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 1) - 41.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 2) - 26.0) > GSL_DBL_EPSILON ); gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_NONE, B, GSL_LINALG_MOD_TRANSPOSE, C); s += ( fabs(gsl_matrix_get(C, 0, 0) - 127.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 1) - 27.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 2) - 27.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 0) - 120.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 1) - 71.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 2) - 71.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 0) - 39.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 1) - 24.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 2) - 24.0) > GSL_DBL_EPSILON ); gsl_linalg_matmult_mod(A, GSL_LINALG_MOD_TRANSPOSE, B, GSL_LINALG_MOD_TRANSPOSE, C); s += ( fabs(gsl_matrix_get(C, 0, 0) - 107.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 1) - 15.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 2) - 15.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 0) - 156.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 1) - 71.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 2) - 71.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 0) - 49.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 1) - 30.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 2) - 30.0) > GSL_DBL_EPSILON ); /* now try for non-symmetric matrices */ gsl_linalg_matmult_mod(D, GSL_LINALG_MOD_TRANSPOSE, E, GSL_LINALG_MOD_NONE, C); s += ( fabs(gsl_matrix_get(C, 0, 0) - 101.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 1) - 53.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 0, 2) - 22.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 0) - 70.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 1) - 85.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 1, 2) - 50.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 0) - 15.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 1) - 20.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(C, 2, 2) - 12.0) > GSL_DBL_EPSILON ); gsl_linalg_matmult_mod(D, GSL_LINALG_MOD_NONE, E, GSL_LINALG_MOD_TRANSPOSE, F); s += ( fabs(gsl_matrix_get(F, 0, 0) - 127.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(F, 0, 1) - 27.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(F, 1, 0) - 120.0) > GSL_DBL_EPSILON ); s += ( fabs(gsl_matrix_get(F, 1, 1) - 71.0) > GSL_DBL_EPSILON ); gsl_matrix_free(A); gsl_matrix_free(B); gsl_matrix_free(C); gsl_matrix_free(D); gsl_matrix_free(E); gsl_matrix_free(F); return s;}#endifinttest_LU_solve_dim(const gsl_matrix * m, const double * actual, double eps){ int s = 0; int signum; unsigned long i, dim = m->size1; gsl_permutation * perm = gsl_permutation_alloc(dim); gsl_vector * rhs = gsl_vector_alloc(dim);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -