【问题标题】:Trying to integrate an interpolated function with C GSL尝试将插值函数与 C GSL 集成
【发布时间】:2017-07-01 14:48:26
【问题描述】:

我是 C 语言的新手,但在类型声明和指针方面遇到了一些问题。

我正在尝试插入数据集然后对其进行整合,这两种数值方法都是使用 C GSL 库完成的。该代码改编自 GSL 关于蒙特卡罗积分和二维插值的手动示例。在使用数据集之前,我正在使用已知且非常简单的 2D 函数进行测试。

2D插值部分运行良好,可以从插值函数interpFun获取值。

但是我不能使用这个插值函数作为积分算法的输入。

#include <stdio.h>
#include <stdlib.h>

#include <gsl/gsl_math.h>
#include <gsl/gsl_interp2d.h>
#include <gsl/gsl_spline2d.h>
#include <gsl/gsl_monte.h>
#include <gsl/gsl_monte_vegas.h>

int
main()
{
  const gsl_interp2d_type *T = gsl_interp2d_bilinear;
  const size_t N = 100;             /* number of points to interpolate */
  double xa[] = { 0.0, 1.0 }; /* define unit square */
  double ya[] = { 0.0, 1.0 };
  double xl[] = { 0., 0.};
  double xu[] = { 1., 1.}, xp[] = {1.0, 0.0}, zz[] = {.2,.6};
  const size_t nx = sizeof(xa) / sizeof(double); /* x grid points */
  const size_t ny = sizeof(ya) / sizeof(double); /* y grid points */
  double *za = malloc(nx * ny * sizeof(double));
  double fun( double *x, size_t dim, void *params);
  gsl_spline2d *spline = gsl_spline2d_alloc(T, nx, ny);
  gsl_interp_accel *xacc = gsl_interp_accel_alloc();
  gsl_interp_accel *yacc = gsl_interp_accel_alloc();
  size_t i, j;
  const gsl_rng_type *S; 
  gsl_rng *r; 
  double res, err;
  struct fparams { gsl_spline2d *spline; gsl_interp_accel *xacc; gsl_interp_accel *yacc; };

  /* set z grid values */
  gsl_spline2d_set(spline, za, 0, 0, fun( xl, 2, 0) );
  gsl_spline2d_set(spline, za, 0, 1, fun( xa, 2, 0) );
  gsl_spline2d_set(spline, za, 1, 1, fun( xu, 2, 0) );
  gsl_spline2d_set(spline, za, 1, 0, fun( xp, 2, 0) );

  /* initialize interpolation */
  gsl_spline2d_init(spline, xa, ya, za, nx, ny);

  struct fparams params1 = { spline, xacc, yacc };

  double interpFun( double *x, size_t dim, struct fparams p ) {
  (void)(dim);
  struct fparams *par = &p;
  return gsl_spline2d_eval(par->spline, x[0], x[1], par->xacc, par->yacc);
   };

  gsl_monte_function G = { &interpFun, 2, &params1 };
  size_t calls = 500000;
  gsl_rng_env_setup ();
  S = gsl_rng_default;
  r = gsl_rng_alloc (S);
  {
    gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (2);
    gsl_monte_vegas_integrate (&G, xl, xu, 2, 10000, r, s, &res, &err);
    printf ("result = %.6f\n", res);
    printf ("sigma = %.6f\n", err);
    printf ("converging...\n");
    do
      {
       gsl_monte_vegas_integrate (&G, xl, xu, 2, calls/5, r, s, &res, &err);
       printf ("result = %.6f sigma = %.6f "
               "chisq/dof = %.1f\n", res, err, gsl_monte_vegas_chisq (s));
     }
    while (fabs (gsl_monte_vegas_chisq(s)-1.0) > 0.5);
    printf ("result = %.6f\n", res);
    printf ("sigma = %.6f\n", err);
    gsl_monte_vegas_free (s);
  }

  gsl_rng_free (r);
  gsl_spline2d_free(spline);
  gsl_interp_accel_free(xacc);
  gsl_interp_accel_free(yacc);
  free(za);

  return 0;
}

double fun( double *x, size_t dim, void *p ) {
 (void)(dim);
 (void)(p);
 return 3*x[0]+5*x[1];
}

此代码返回错误消息:

inter_test.c: In function ‘main’:
inter_test.c:55:3: warning: initialization from incompatible pointer type [enabled by default]
   gsl_monte_function G = { &interpFun, 2, &params1 };
   ^
inter_test.c:55:3: warning: (near initialization for ‘G.f’) [enabled by default]

如果我尝试集成fun 而不是interpFun,则不会出现错误消息,但两者是同一类型。

【问题讨论】:

  • 它们是同一类型。 fun 的第三个参数是指向void 的指针,而interpFun 的第三个参数是struct
  • @DavidBowling ,如何更改 fun 中的类型以使其兼容?如果我将void *p 替换为struct fparams p,则会出现更多错误。

标签: c interpolation gsl numerical-integration


【解决方案1】:

程序中定义的interpFun() 函数的类型为double (*)(double *, size_t, struct fparams),但根据the GSL docs,您需要类型为double (*)(double *, size_t, void *)。这是一个修复:

double interpFun( double *x, size_t dim, void *p ) {
    (void)(dim);
    struct fparams *par = (struct fparams *) p;
    return gsl_spline2d_eval(par->spline, x[0], x[1], par->xacc, par->yacc);
}

此外,C 不允许嵌套函数定义,尽管有 GCC extension for this

【讨论】:

    猜你喜欢
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多