⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 该文件为c++的数学函数库!是一个非常有用的编程工具.它含有各种数学函数,为科学计算、工程应用等程序编写提供方便!
💻 C
📖 第 1 页 / 共 5 页
字号:
/* linalg/test.c *  * Copyright (C) 1996, 1997, 1998, 1999, 2000 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., 675 Mass Ave, Cambridge, MA 02139, 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>int check (double x, double actual, double eps);gsl_matrix * create_hilbert_matrix(size_t size);gsl_matrix * create_general_matrix(size_t size1, size_t size2);gsl_matrix * create_vandermonde_matrix(size_t size);gsl_matrix * create_moler_matrix(size_t size);gsl_matrix * create_row_matrix(size_t size1, size_t size2);gsl_matrix * create_2x2_matrix(double a11, double a12, double a21, double a22);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_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_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_TD_solve_dim(size_t dim, double d, double od, const double * actual, double eps);int test_TD_solve(void);int test_TDN_solve_dim(size_t dim, double d, double a, double b, const double * actual, double eps);int test_TDN_solve(void);int test_TD_cyc_solve_one(const size_t dim, const double * d, const double * od, const double * r,                          const double * actual, double eps);int test_TD_cyc_solve(void);int test_TDN_cyc_solve_dim(size_t 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 (actual == 0)    return fabs(x) > eps;  else    return (fabs(x - actual)/fabs(actual)) > eps;}gsl_matrix *create_hilbert_matrix(size_t size){  size_t 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(size_t size1, size_t size2){  size_t 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(size_t size1, size_t size2){  size_t 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(size_t size){  size_t 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(size_t size){  size_t 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(size_t size){  size_t 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(size_t size1, size_t size2){  size_t 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 * 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_complex * c7;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;  size_t i, dim = m->size1;  gsl_permutation * perm = gsl_permutation_alloc(dim);  gsl_vector * rhs = gsl_vector_alloc(dim);  gsl_matrix * lu  = gsl_matrix_alloc(dim,dim);  gsl_vector * x = gsl_vector_alloc(dim);  gsl_vector * residual = gsl_vector_alloc(dim);  gsl_matrix_memcpy(lu,m);  for(i=0; i<dim; i++) gsl_vector_set(rhs, i, i+1.0);  s += gsl_linalg_LU_decomp(lu, perm, &signum);  s += gsl_linalg_LU_solve(lu, perm, rhs, x);  for(i=0; i<dim; i++) {    int foo = check(gsl_vector_get(x, i),actual[i],eps);    if(foo) {      printf("%3d[%d]: %22.18g   %22.18g\n", dim, i, gsl_vector_get(x, i), actual[i]);    }    s += foo;  }  s += gsl_linalg_LU_refine(m, lu, perm, rhs, x, residual);  for(i=0; i<dim; i++) {    int foo = check(gsl_vector_get(x, i),actual[i],eps);    if(foo) {      printf("%3d[%d]: %22.18g   %22.18g (improved)\n", dim, i, gsl_vector_get(x, i), actual[i]);    }    s += foo;  }  gsl_vector_free(residual);  gsl_vector_free(x);  gsl_matrix_free(lu);  gsl_vector_free(rhs);  gsl_permutation_free(perm);  return s;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -