我正在尝试使用 FFTW 进行多维实到实转换。根据the documentation ,函数定义如下:
fftw_plan fftw_plan_r2r(int rank,
const int *n,
double *in,
double *out,
const fftw_r2r_kind *kind,
unsigned flags);
此外,关于第五个参数:
Each dimension has a kind parameter, of type fftw_r2r_kind, specifying the kind of r2r transform to be used for that dimension. (In the case of fftw_plan_r2r, this is an array kind[rank] where kind[i] is the transform kind for the dimension n[i].) The kind can be one of a set of predefined constants, defined in the following subsections.
我在分配种类数组时遇到问题。以下是我尝试过的一些方法:
这给了我一个“只读变量不可分配”错误:
const fftw_r2r_kind *kind;
kind = (fftw_r2r_kind*) fftw_malloc(sizeof(fftw_r2r_kind) * 2);
kind[0] = FFTW_REDFT11;
kind[1] = FFTW_REDFT11;
p = fftw_plan_r2r(WorkImageType::ImageDimension, n, in, out, kind, FFTW_ESTIMATE);
这给了我一个“意外的表达式”错误:
const fftw_r2r_kind *kind;
kind = (fftw_r2r_kind*) fftw_malloc(sizeof(fftw_r2r_kind) * 2);
kind[0] = FFTW_REDFT11;
kind[1] = FFTW_REDFT11;
p = fftw_plan_r2r(WorkImageType::ImageDimension, n, in, out, kind, FFTW_ESTIMATE);
这给了我一个奇怪的“无法用类型为‘fftw_r2r_kind_do_not_use_me’的右值初始化类型为‘const fftw_r2r_kind *’(又名‘const fftw_r2r_kind_do_not_use_me *’)的数组元素”错误:
const fftw_r2r_kind *kind[2] = {FFTW_REDFT11, FFTW_REDFT11};
p = fftw_plan_r2r(WorkImageType::ImageDimension, n, in, out, kind, FFTW_ESTIMATE);
我是 C++ 和 FFTW 的新手,我很困惑——如果有任何建议,我将不胜感激!
最佳答案
您看到的错误是由于尝试为 const
变量赋值以及对指针/数组的混淆所致。
在前两种情况下,您声明:
const fftw_r2r_kind *kind;
这是指向 const fftw_r2r_kind
对象(或此类数组中的第一个)的(非常量)指针。然后你分配内存并将指针存储在这个变量中,这很好。但是,由于您告诉编译器这些对象将是 const
,因此尝试为它们赋值将失败。
请注意 fftw_plan_r2r()
定义中的 const fftw_r2r_kind *kind
位并不意味着您必须声明最终传递给它的变量方式。函数声明中的 const
promise 该函数不会触及传入的值,而不是要求值实际上是该函数之外的 const
(尽管它们可以是)。
在您的最后一个示例中,错误源于您声明的事实:
const fftw_r2r_kind *kind[2] = { ... };
这是指向 const fftw_r2r_kind
的两个指针数组的声明,而不是两个 const fftw_r2r_kind
的数组。然后您尝试用两个不是指针的值来初始化数组。在这种情况下,由于您声明的是数组本身,而不是用于保存对动态分配数组的引用的指针,因此它应该只是 const fftw_r2r_kind kind[2] = { ... };
.这应该允许您在声明时正确初始化数组,这是在 const
数组中设置值的正确方法。
关于c++ - FFTW:fftw_plan_r2r() 的种类参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23918286/