sudo make install
sudo make install

Reputation: 5669

FFTW: Kind argument of fftw_plan_r2r()

I'm trying to do a multidimensional real to real transform with FFTW. According to the documentation, the function is defined as follows:

fftw_plan fftw_plan_r2r(int rank,
                        const int *n,
                        double *in,
                        double *out,
                        const fftw_r2r_kind *kind,
                        unsigned flags);

Moreover, concerning the fifth argument:

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.

I'm having trouble assigning the kind array. Here are a few things I have tried:

This gives me a "read only variable is not assignable" error:

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);

This gives me an "unexpected expression" error:

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);

This gives me a strange "cannot initialize an array element of type 'const fftw_r2r_kind *' (aka 'const fftw_r2r_kind_do_not_use_me *') with an rvalue of type 'fftw_r2r_kind_do_not_use_me'" error:

const fftw_r2r_kind *kind[2] = {FFTW_REDFT11, FFTW_REDFT11};
p = fftw_plan_r2r(WorkImageType::ImageDimension, n, in, out, kind, FFTW_ESTIMATE);

I'm new to both C++ and FFTW, and am stumped--I would really appreciate any suggestions!

Upvotes: 1

Views: 1337

Answers (1)

twalberg
twalberg

Reputation: 62389

The errors you are seeing are due to a mixture of trying to assign values to const variables and confusion about pointers/arrays.

In the first two cases, you declare:

const fftw_r2r_kind *kind;

This is a (non-const) pointer to a const fftw_r2r_kind object (or the first in an array of such). You then allocate memory and store the pointer in this variable, which is fine. However, since you told the compiler that these objects would be const, attempting to assign values to them will fail.

Note that the const fftw_r2r_kind *kind bit in the fftw_plan_r2r() definition does not mean that you have to declare the variables that you ultimately pass to it in the same way. const in a function declaration is a promise that that function will not touch the values passed in, not a requirement that the values actually be const outside of that function (although they can be).

In your last example, the error stems from the fact that you declare:

const fftw_r2r_kind *kind[2] = { ... };

This is a declaration of an array of two pointers to const fftw_r2r_kind, not an array of two const fftw_r2r_kind. You then attempt to initialize the array with two values that are not pointers. In this case, since you are declaring the array itself, and not a pointer to hold a reference to a dynamically allocated array, it should simply be const fftw_r2r_kind kind[2] = { ... };. That should allow you to correctly initialize the array at declaration time, which is the right way to set values in a const array.

Upvotes: 2

Related Questions