need help with narray: how to return an array from a C-extension

  • Thread starter Joachim (München)
  • Start date
J

Joachim (München)

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
 
M

Max Lapshin

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 =3D rb_ary_new(NUM2INT(param));
return result;
}=
 
M

masa16.tanaka

Hi,

|From: "Joachim (M=FCnchen)"
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.
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 =3D NUM2INT(mu_shifted);
v1 =3D na_cast_unless_narray(distribution,NA_DFLOAT);
GetNArray(v1,a1);
v2 =3D na_cast_unless_narray(lookuptable,NA_DFLOAT);
GetNArray(v2,a2);
// must check array sizes here
/* calculation seems to work */
for (i=3D0; i<=3D1000; i++)
*((double*)(a1->ptr+i)) +=3D *((double*)(a2->ptr+mt-i));
*((double*)(a1->ptr)+i) +=3D *((double*)(a2->ptr)+mt-i);
// a1->ptr is declared as char*
/* PROBLEM: this does not work. QUESTION: how can I overwrite the cal= l
parameter "distribution"
with a ruby array that contains the result a1->ptr of the above
calculation? */
distribution =3D na_to_array( a1->ref );
// I think ovewriting arguments is not a good idea.
// If you cannot avoid it, overwrite RARRAY(distribution)->ptr car=
efully.
/* 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top