Pasando valores de la matriz de rubí en una matriz de C
-
12-10-2019 - |
Pregunta
Estoy tratando de hacer una extensión independiente FFT de rubí en C, basado en esta receta
he señalado varios métodos para pasar valores diferentes entre rubí y c. Sin embargo im bastante nuevo en tanto rubí y C y no puede encontrar la manera de copiar una matriz de un objeto de valor rubí en una matriz C.
El error de compilación: SimpleFFT.c: 47: error: subíndice valor no es ni variedad ni puntero
Y el código:
#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;
}
Gracias de antemano:)
Solución
You can't subscript inputArr
because it's a VALUE
rather than a C array. Ie, it's a scalar type. To access a particular index, use
rb_ary_entry(inputArr, i)
As an aside, you might want to verify first that it's an array:
Check_Type(rarray, T_ARRAY);
Otros consejos
looks like answering the question (and double checking my sources) helped me work out the answer.
replacing:
rb_ary_push(outputArr, DBL2NUM(X[i][0]));
with:
x[i][0]=NUM2DBL(rb_ary_pop(inputArr));
seemed to do the trick :)
I'm still wonder if this is the most efficient way of doing things, but it works.