Need help with narray: how to return an array from a C-exten

Hi there,
I am writing my first C extension module to ruby, and I need a little
help:
import of parameters works fine, but export of results not yet:

static VALUE
myarr_faltung1(VALUE m, VALUE distribution, VALUE lookuptable, VALUE
mu_shifted)
{
struct NARRAY *a1, *a2;
int mt, i;
volatile VALUE v1, v2;

/* import from parameters seems to work */
mt = NUM2INT(mu_shifted);
v1 = na_cast_unless_narray(distribution,NA_DFLOAT);
GetNArray(v1,a1);
v2 = na_cast_unless_narray(lookuptable,NA_DFLOAT);
GetNArray(v2,a2);

/* calculation seems to work */
for (i=0; i<=1000; i++)
((double)(a1->ptr+i)) += ((double)(a2->ptr+mt-i));

/* PROBLEM: this does not work. QUESTION: how can I overwrite the call
parameter “distribution”
with a ruby array that contains the result a1->ptr of the above
calculation? */
distribution = na_to_array( a1->ref );

/* PROBLEM: the following does not work either. QUESTION: how can I
return the result a1->ptr
of the above calculation to the calling program? */
return a1->ref ;
}

Thanks in advance, Joachim

On Oct 4, 2006, at 11:30 AM, Joachim (München) wrote:

Hi there,
I am writing my first C extension module to ruby, and I need a little
help:
import of parameters works fine, but export of results not yet:

VALUE my_function(VALUE self, VALUE param) {
VALUE result;

result = rb_ary_new(NUM2INT(param));
return result;
}

Hi,

|From: “Joachim (München)”

I am writing my first C extension module to ruby, and I need a little
help:
import of parameters works fine, but export of results not yet:

I recommend not to manipulate the C structure of NArray
because its structure is complicated and may be changed in future.

v1 = na_cast_unless_narray(distribution,NA_DFLOAT);
GetNArray(v1,a1);
v2 = na_cast_unless_narray(lookuptable,NA_DFLOAT);
GetNArray(v2,a2);

// must check array sizes here

/* calculation seems to work /
for (i=0; i<=1000; i++)
((double)(a1->ptr+i)) += ((double)(a2->ptr+mt-i));
((double)(a1->ptr)+i) += ((double)(a2->ptr)+mt-i);
// a1->ptr is declared as char

/* PROBLEM: this does not work. QUESTION: how can I overwrite the call
parameter “distribution”
with a ruby array that contains the result a1->ptr of the above
calculation? */
distribution = na_to_array( a1->ref );
// I think ovewriting arguments is not a good idea.
// If you cannot avoid it, overwrite RARRAY(distribution)->ptr[i]
carefully.

/* PROBLEM: the following does not work either. QUESTION: how can I
return the result a1->ptr
of the above calculation to the calling program? */
return a1->ref ;
return v1;

}

Masahiro Tanaka