0

我在struct中有几个变量。

struct my_struct{
    float variable_2_x[2],variable_2_y[2],variable_2_z[2];
    float coef_2_xyz[3];    

    float variable_3_x[3],variable_3_y[3],variable_3_z[3];
    float coef_3_xyz[3];

    float variable_4_x[4],variable_4_y[4],variable_4_z[4];
    float coef_4_xyz[3];
};

这个结构将包含拉格朗日多项式(en.wikipedia.org/wiki/Lagrange_polynomial)系数,用于几个多项式长度:2、3、4。这个系数的值很容易计算,但问题是,我必须重复相同的代码来创建每一个多项式。例如

// T_space is a cube with {[-1:1][-1:1][-1:1]} dimension, 
// its call transformed space.
// distance is the distance between two points of T_space
// point_1 its the point where the function has value 1
p = 2; 
step = distance / p;

polinoms.coef_2_xyz[0] = 1.0:
polinoms.coef_2_xyz[1] = 1.0:
polinoms.coef_2_xyz[2] = 1.0:
for( i = 0; i < p ; ++i) 
{
    polinoms.pol_2_x[i] = (T_space.xi[point_1] + step) + (i * step);
    polinoms.pol_2_y[i] = (T_space.eta[point_1] + step) + (i * step);
    polinoms.pol_2_z[i] = (T_space.sigma[point_1] + step) + (i * step);


    polinoms.coef_2_xyz[0]*= (T_space.eta[point_1] - polinoms.pol_2_x[i]);
    polinoms.coef_2_xyz[1]*= (T_space.eta[point_1] - polinoms.pol_2_y[i]);
    polinoms.coef_2_xyz[2]*= (T_space.eta[point_1] - polinoms.pol_2_z[i]);

}

因为我不想在代码中多次重复相同的循环。在代码中更重要的下一步是我需要将多项式梯度的乘积集成到立方体中的每个点。

能够独立调用结构的每个数组将非常有用。据我所知,变量不能在运行时动态调用。我想制作一个包含struct的内存方向的数组。像这样的东西。

// declare variable to store memory directions
float mem_array[12];

// some code

mem_array[0] = &variable_2_x;
mem_array[1] = &variable_2_y;
mem_array[2] = &variable_2_z;
mem_array[3] = &coef_2_xyz;
mem_array[4] = &variable_3_x;

mem_array[11] = &variable_4_z;
mem_array[12] = &coef_4_xyz;

// work calling mem_array.

但我不知道这是否可行或是否可行。如果您认为这不是解决问题的正确方法,我愿意接受建议。因为我真的坚持这一点。

我已将问题编辑得更清楚,希望对您有所帮助。

4

1 回答 1

2

You'd be better to allocate the memory you need dynamically. You can have a struct that represents a single Lagrange polynomial (of any order), and then have an array of these, one for each order.

You could also store the order of the polynomial as a member of the struct if you wish. You should be able to factor out code that deals with these into functions that take a LagrangePolynomial*, determine the order, and do whatever computation is required.

The key benefit of all of this is that you don't need to have special code for each order, you can use the same code (and the same struct) for any size of polynomial.

Example below:

struct LagrangePolynomial {
    float *x;
    float *y;
    float *z;
};

For p=2:

LagrangePolynomial p;
p.x = malloc(sizeof(float)*2);
p.y = malloc(sizeof(float)*2);
p.z = malloc(sizeof(float)*2);
for (size_t i=0; i<2; i++) {
    p.x[i] = ...;
    p.y[i] = ...;
    p.z[i] = ...;
}

When you've finished with the structure you can free all the memory you've allocated.

free(p.x);
free(p.y);
free(p.z);

As mentioned before you can have an array of these.

LagrangePolynomial ps[4];
for (size_t i=0; i<4; i++) {
    p[i].x = malloc(sizeof(float)*2);
    p[i].y = malloc(sizeof(float)*2);
    p[i].z = malloc(sizeof(float)*2);
    for (size_t j=0; j<2; j++) {
        p[i].x[j] = ...;
        p[i].y[j] = ...;
        p[i].z[j] = ...;
    }
}
于 2012-08-09T16:08:21.343 回答