0

我是 C 编程的新手。我正在使用“C 中的数值食谱”一书中的 ac 代码进行多项式回归。在这个程序中,我需要用 fpoly 函数替换 (*funcs) 函数。但我不知道如何做到这一点以及如何将 fpoly 函数更改为像 (*fpoly)。你能帮我解决这个问题吗?

我真的很感激任何帮助。

void fpoly(float x, float p[], int np)
//Fitting routine for a polynomial of degree np-1, with coefficients in the array p[1..np].
{
    int j;
    p[1]=1.0;
    for (j=2;j<=np;j++) p[j]=p[j-1]*x;
}



void lfit( float x[], float y[], float sig[], int ndat, float a[], int ia[], int ma, float **covar, float *chisq, void (*funcs)(float, float [], int))

这是完整的程序:

void lfit(float x[], float y[], float sig[], int ndat, float a[], int ia[],
      int ma, float **covar, float *chisq, void (*funcs) (float,float[],  int))

/*Given a set of data points x[1..ndat], y[1..ndat] with individual standard deviations
sig[1..ndat], use χ2 minimization to fit for some or all of the coefficients a[1..ma] of
a function that depends linearly on a, y =sum(i)( ai × afunci(x)). The input array ia[1..ma]
indicates by nonzero entries those components of a that should be fitted for, and by zero entries
those components that should be held fixed at their input values. The program returns values
for a[1..ma], χ2 = chisq, and the covariance matrix covar[1..ma][1..ma]. (Parameters
held fixed will return zero covariances.)Th e user supplies a routine funcs(x,afunc,ma) that
returns the ma basis functions evaluated at x = x in the array afunc[1..ma].*/
{

    void covsrt(float **covar, int ma, int ia[], int mfit);
    void gaussj(float **a, int n, float **b, int m);

    int i, j, k, l, m, mfit = 0;
    float ym, wt, sum, sig2i, **beta, *afunc;

    beta = matrix(1, ma, 1, 1);
    afunc = vector(1, ma);

    for (j = 1; j <= ma; j++)
        if (ia[j])
          mfit++;
    if (mfit == 0)
       nrerror("lfit: no parameters to be fitted");

    for (j = 1; j <= mfit; j++) {   //Initialize the (symmetric)mat rix.
        for (k = 1; k <= mfit; k++)
            covar[j][k] = 0.0;
        beta[j][1] = 0.0;
    }

    for (i = 1; i <= ndat; i++) {
    (*funcs) (x[i], afunc, ma);
    ym = y[i];
    if (mfit < ma) {              //Subtract off dependences on known pieces
        for (j = 1; j <= ma; j++)     //of the fitting function.
        if (!ia[j])
            ym -= a[j] * afunc[j];
    }
    sig2i = 1.0 / SQR(sig[i]);
    for (j = 0, l = 1; l <= ma; l++) {
        if (ia[l]) {
        wt = afunc[l] * sig2i;
        for (j++, k = 0, m = 1; m <= l; m++)
            if (ia[m])
            covar[j][++k] += wt * afunc[m];
        beta[j][1] += ym * wt;
        }
    }
    }
    for (j = 2; j <= mfit; j++)      //Fill in above the diagonal from symmetry.
        for (k = 1; k < j; k++)
           covar[k][j] = covar[j][k];
    gaussj(covar, mfit, beta, 1);      //Matrix solution.
    for (j = 0, l = 1; l <= ma; l++)
         if (ia[l])
           a[l] = beta[++j][1];    //Partition solution to appropriate coefficients
    *chisq = 0.0;   

    for (i = 1; i <= ndat; i++) {      //Evaluate χ2 of the fit.
        (*funcs) (x[i], afunc, ma);
            for (sum = 0.0, j = 1; j <= ma; j++)
                sum += a[j] * afunc[j];
    *chisq += SQR((y[i] - sum) / sig[i]);
    }
    covsrt(covar, ma, ia, mfit);      //Sort covariance matrix to true order of fittin
    free_vector(afunc, 1, ma);    //coefficients.
    free_matrix(beta, 1, ma, 1, 1);
}
4

3 回答 3

1

如果我理解正确,如果是这样,您想传递函数poly,那么只需传递函数的名称:

lfit(x, y,...., poly);
于 2012-11-19T10:03:51.687 回答
1

void lfit( float [], float [], float [], int, float [], int [], int, float**, float*, void (*funcs)(float, float [], int))

“void (*funcs)(float, float [], int)”是函数指针的类型签名。如果它在范围内,您可以只传递函数的名称 (fpoly) 来代替“void (*funcs)(float, float [], int)”,不带括号或任何东西。您也可以使用 & 运算符获取其地址,但我相信它是等效的:

lfit( all_the_other_args, ..., fpoly);

您还可以有一个保存 fpoly 的本地函数指针:

void (*local_function_pointer_variable)(float, float [], int) = fpoly;
lfit( all_the_other_args, ..., local_function_pointer_variable);

在 C 中,函数指针类型的语法有点不方便,但希望你可以定义一个类型来在某种程度上隐藏它

typedef void (*poly_fitter)(float, float [], int);
poly_fitter function_pointer_var_of_type_poly_fitter = fpoly;
lfit( all_the_other_args, ..., function_pointer_var_of_type_poly_fitter)
于 2012-11-19T10:13:10.667 回答
1

lfit 函数有很多输入参数。

参数的 1 是函数的地址。这就是我们*在该输入参数的定义中添加的原因。

void (*funcs) (float, float[], int))

因此,当您调用lfit()函数时,您可以提及函数地址作为函数fpoly()的输入lfit()

的地址void fpoly(float x, float p[], int np)fpoly&fpoly

所以当你调用你的函数时,lfit()你可以这样做:

lfit(x,y,...,fpoly)

或以这种方式:

lfit(x,y,...,&fpoly)

于 2012-11-19T10:15:19.960 回答