2

我正在尝试使用定义多维积分例程的"Cuba" "library",并在 C 程序中使用这些例程(我通常使用 Mathematica,但对于我现在需要计算的积分,Mathematica 存在巨大的收敛问题)。我安装了包Cuba-3.1.tar.gz(“new”)的元素,并尝试编译demo文件demo-cc,但经过很多小时的尝试,我仍然没有成功(我只学到了一点点学校里有一点 C,但从那以后就再也没有用过,或者真的编译过其他任何东西,所以我是个新手),我不确定哪里出了问题。

总结:我不确定我的计算机/编译器配置是否正确,是否使用了正确的命令行,或者是否有其他问题,但是几乎没有任何关于 Cuba 的文档,也没有用户论坛。

为了清楚起见,让我分解代码的重要部分,然后在问题末尾复制整个内容。

包括:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cuba.h"

在 cd 到包含 demo-cc 和 cuba.h 的文件夹中(我将 cuba.h 复制到 demo 文件夹中),然后我运行:

gcc -o demo-c demo-c.c

我得到的输出是:

Undefined symbols:
"_Vegas", referenced from:
  _main in cc7i6pMP.o
ld: symbol(s) not found

我尝试了其他各种编译命令,这是一个列表:

  1. gcc -o demo-c demo-c.c -lcuba -lm
  2. gcc -o demo-c demo-c.c -L/users/username/Applications/Cuba-3.1/demo
  3. gcc -iquote ~/Applications/Cuba-3.1/demo/cuba.h -o demo-c demo-c.c
  4. gcc -I/../../../../Applications/Cuba-3.1/demo -o demo-c demo-c.c
  5. g95 -o -L/../../../../Applications/Cuba-3.1/demo demo-fortran demo-fortran.F
  6. g95 -o demo-fortran demo-fortran.F -L/../../../../Applications/Cuba-3.1/demo
  7. cc -o demo-c demo-c.c -lcuba -lm
  8. cc -o -iquote ../ demo-c demo-c.c
  9. gcc -o demo-c demo-c.c -I/./lcuba

和其他一些小的变化,但得到了基本相同的错误消息(也适用于 fortran 代码)。请注意,文档说代码应该完全使用第 6 行编译:

cc -o demo-c demo-c.c -lcuba -lm

从互联网上的广泛研究中,我了解到“主要”功能一定存在问题,即:

int main() {
int verbose, comp, nregions, neval, fail;
double integral[NCOMP], error[NCOMP], prob[NCOMP];

const char *env = getenv("CUBAVERBOSE");
verbose = 2;
if( env ) verbose = atoi(env);

#if 1
printf("-------------------- Vegas test --------------------\n");

Vegas(NDIM, NCOMP, Integrand, USERDATA,
EPSREL, EPSABS, verbose, SEED,
MINEVAL, MAXEVAL, NSTART, NINCREASE, NBATCH,
GRIDNO, STATEFILE,
&neval, &fail, integral, error, prob);

printf("VEGAS RESULT:\tneval %d\tfail %d\n",
neval, fail);
for( comp = 0; comp < NCOMP; ++comp )
printf("VEGAS RESULT:\t%.8f +- %.8f\tp = %.3f\n",
  integral[comp], error[comp], prob[comp]);
#endif

return 0;
}

但我真的看不出那是什么。事实上,维加斯的所有论点似乎都得到了明确的定义。

由于文件 cuba.h 实际上很小,我尝试将其复制并粘贴到 demo-cc 中,在 main 之前,我得到以下错误:

demo-c.c:74: error: array type has incomplete element type
demo-c.c:76: error: array type has incomplete element type
demo-c.c:83: error: array type has incomplete element type

第 74、76 和 83 行对应于

const double x[],    double f[],    and     double x[]);

typedef int (*integrand_t)(
  const int *ndim, 
  const double x[],
  const int *ncomp, 
  double f[], 
  void *userdata);

typedef void (*peakfinder_t)(
  const int *ndim, 
  const double b[],
  int *n, 
  double x[]);

(这是我粘贴在 demo-cc 中的 cuba.h 的摘录)。我试过了

const double x[10],    double f[10],    and     double x[10]);

但这毫不奇怪会产生(其他)错误。

系统

我正在使用运行 Snow Leopard (10.6.8) 和 XCode 3.2.6 的 MacBook(2009 年 5 月 - 2.4GHz Intel Core Duo 8Gb 1067MHz DDR3)。

完整代码演示-cc

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cuba.h"


static inline double Sq(double x) {
return x*x;
}


static int Integrand(const int *ndim, const double xx[],
  const int *ncomp, double ff[], void *userdata) {

#define x xx[0]
#define y xx[1]
#define z xx[2]
#define f ff[0]

#ifndef FUN
#define FUN 1
#endif

#define rsq (Sq(x) + Sq(y) + Sq(z))

#if FUN == 1
  f = sin(x)*cos(y)*exp(z);
#elif FUN == 2
  f = 1/(Sq(x + y) + .003)*cos(y)*exp(z);
#elif FUN == 3
  f = 1/(3.75 - cos(M_PI*x) - cos(M_PI*y) - cos(M_PI*z));
#elif FUN == 4
  f = fabs(rsq - .125);
#elif FUN == 5
  f = exp(-rsq);
#elif FUN == 6
  f = 1/(1 - x*y*z + 1e-10);
#elif FUN == 7
  f = sqrt(fabs(x - y - z));
#elif FUN == 8
  f = exp(-x*y*z);
#elif FUN == 9
  f = Sq(x)/(cos(x + y + z + 1) + 5);
#elif FUN == 10
  f = (x > .5) ? 1/sqrt(x*y*z + 1e-5) : sqrt(x*y*z);
#else
  f = (rsq < 1) ? 1 : 0;
#endif

  return 0;
}

/*********************************************************************/

#define NDIM 3
#define NCOMP 1
#define USERDATA NULL
#define EPSREL 1e-3
#define EPSABS 1e-12 
#define LAST 4
#define SEED 0
#define MINEVAL 0
#define MAXEVAL 50000

#define NSTART 1000
#define NINCREASE 500
#define NBATCH 1000
#define GRIDNO 0
#define STATEFILE NULL

#define NNEW 1000
#define FLATNESS 25.

#define KEY1 47
#define KEY2 1
#define KEY3 1
#define MAXPASS 5
#define BORDER 0.
#define MAXCHISQ 10.
#define MINDEVIATION .25
#define NGIVEN 0
#define LDXGIVEN NDIM
#define NEXTRA 0

#define KEY 0

int main() {
  int verbose, comp, nregions, neval, fail;
  double integral[NCOMP], error[NCOMP], prob[NCOMP];

const char *env = getenv("CUBAVERBOSE");
verbose = 2;
if( env ) verbose = atoi(env);

#if 1
  printf("-------------------- Vegas test --------------------\n");

Vegas(NDIM, NCOMP, Integrand, USERDATA,
EPSREL, EPSABS, verbose, SEED,
MINEVAL, MAXEVAL, NSTART, NINCREASE, NBATCH,
GRIDNO, STATEFILE,
&neval, &fail, integral, error, prob);

printf("VEGAS RESULT:\tneval %d\tfail %d\n",
  neval, fail);
for( comp = 0; comp < NCOMP; ++comp )
printf("VEGAS RESULT:\t%.8f +- %.8f\tp = %.3f\n",
  integral[comp], error[comp], prob[comp]);
#endif

return 0;
}

完整代码 cuba.h

#ifdef __cplusplus
extern "C" {
#endif

typedef int (*integrand_t)(const int *ndim, const double x[],
  const int *ncomp, double f[], void *userdata);

typedef void (*peakfinder_t)(const int *ndim, const double b[],
  int *n, double x[]);

void Vegas(const int ndim, const int ncomp,
 integrand_t integrand, void *userdata,
 const double epsrel, const double epsabs,
 const int flags, const int seed,
 const int mineval, const int maxeval,
 const int nstart, const int nincrease, const int nbatch,
 const int gridno, const char *statefile,
 int *neval, int *fail,
 double integral[], double error[], double prob[]);

void llVegas(const int ndim, const int ncomp,
 integrand_t integrand, void *userdata,
 const double epsrel, const double epsabs,
 const int flags, const int seed,
 const long long int mineval, const long long int maxeval,
 const long long int nstart, const long long int nincrease,
 const long long int nbatch,
 const int gridno, const char *statefile,
 long long int *neval, int *fail,
 double integral[], double error[], double prob[]);

void cubasetinit(void (*)(), void *);
void cubasetexit(void (*)(), void *);
void cubaruninit(void);
void cubaruninit(void);

#ifdef __cplusplus
}
#endif
4

1 回答 1

0

试试 gcc -o demo-c.exe demo-c.c ../libcuba.a 你需要然后执行demo-c.exe

./demo-c.exe > demo-c.out

输出应该在 demo 目录下的文件 demo-c.out 中

于 2013-10-13T08:30:57.660 回答