题
我正在尝试根据C中的Ruby进行独立的FFT扩展 这个食谱
我注意到Ruby和C之间传递不同值的几种方法。但是,我对Ruby和C的新鲜事物是相当陌生的,无法确定如何将数组从值Ruby对象复制到C数组中。
编译错误:SimpleFFT.C:47:错误:订阅值既不是数组也不是指针
和代码:
#include "ruby.h"
#include "fft.c" // the c file I wish to wrap in ruby
VALUE SimpleFFT = Qnil;
void Init_simplefft();
VALUE method_rfft(VALUE self, VALUE anObject);
void Init_simplefft() {
SimpleFFT = rb_define_module("SimpleFFT");
rb_define_method(SimpleFFT, "rfft", method_rfft, 1);
}
VALUE method_rfft(VALUE self, VALUE inputArr) {
int N = RARRAY_LEN(inputArr); // this works :)
// the FFT function takes an array of real and imaginary paired values
double (*x)[2] = malloc(2 * N * sizeof(double));
// and requires as an argument another such array (empty) for the transformed output
double (*X)[2] = malloc(2 * N * sizeof(double));
for(i=0; i<N; i++) {
x[i][0]=NUM2DBL(inputArr[i]); // ***THIS LINE CAUSES THE ERROR***
x[i][1]=0; // setting all the imaginary values to zero for simplicity
}
fft(N, x, X); // the target function call
// this bit should work in principle, dunno if it's optimal
VALUE outputArr = rb_ary_new();
for(i=0; i<N; i++){
rb_ary_push(outputArr, DBL2NUM(X[i][0]));
}
free(x);
free(X);
return outputArr;
}
提前致谢 :)
解决方案
您不能下标 inputArr
因为这是 VALUE
而不是C数组。即,这是标量类型。要访问特定索引,请使用
rb_ary_entry(inputArr, i)
顺便说一句,您可能要首先验证它是一个数组:
Check_Type(rarray, T_ARRAY);
其他提示
看起来回答问题(并进行双重检查我的资源)帮助我解决了答案。
替换:
rb_ary_push(outputArr, DBL2NUM(X[i][0]));
和:
x[i][0]=NUM2DBL(rb_ary_pop(inputArr));
似乎可以解决问题:)
我仍然想知道这是否是最有效的做事方式,但它可以正常工作。
不隶属于 StackOverflow