2010-12-24 44 views
4

我试图让在C红宝石独立FFT扩展,基于this recipe传递红宝石数组值成C数组

我已经注意到通过红宝石和C之间的不同值的几种方法。然而,即时通讯相当新的红宝石和C,并不能解决如何将数组从一个VALUE 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; 
} 

感谢提前:)

回答

4

你可以't下标inputArr,因为它是VALUE而不是C数组。也就是说,这是一种标量类型。要访问一个特定的索引,使用

rb_ary_entry(inputArr, i) 

顺便说一句,你可能想先确认它是一个数组:

Check_Type(rarray, T_ARRAY); 
+0

感谢您的提示:)。我没有清醒地回答自己,哎呀!访问数组条目是否有任何原因可能比弹出数值更好或更差? – Nat 2010-12-24 02:24:08

2

貌似回答这个问题(和双重检查我的消息来源)帮我找出答案。

更换:

rb_ary_push(outputArr, DBL2NUM(X[i][0])); 

有:

x[i][0]=NUM2DBL(rb_ary_pop(inputArr)); 

似乎这样的伎俩:)

我仍然不知道这是做事情的最有效的方式,但有用。