我在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.
但我不知道这是否可行或是否可行。如果您认为这不是解决问题的正确方法,我愿意接受建议。因为我真的坚持这一点。
我已将问题编辑得更清楚,希望对您有所帮助。