[R] passing a matrix from R to C code

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 25 02:13:05 CEST 2012


Hello,

Also, R uses doubles, not floats.

Hope this helps,

Rui Barradas
Em 24-09-2012 23:27, Peter Langfelder escreveu:
> Erin,
>
> you seem to confuse R and C syntax a bit, among other things. See below.
>
> On Mon, Sep 24, 2012 at 3:03 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
>> Dear R People:
>>
>> I'm working on a project that will pass a matrix from an R function to
>> a C subroutine.
>>
>> I tried to write the following:
>>
>> #include<R.h>
>> #include<Rinternals.h>
>>
>>
>>
>>    void lu1(int *n, float *a, float *b, float *ab)
>>
>> {
>>    int   i,k,j,p,na=*n+10;
>>    float sum, l[200][200],u[200][200]z[200];
> Comma missing?
>
>>
>>      for(i=0;i<na;i++) {
>>        for(j=0;j<na;j++) {
>>          l[i,j]=0.0;
>>          u[i,j]=0.0;
> In C you must write l[i][j] and u[i][j].
> l[i,j] is actually equal to l[j] and that's an array of 200 doubles.
>
>>        }
>>        z[i] =0.0;
>>      }
>>
>>
>>
>>
>>      //********** LU decompositio*n *****//
>>      for(k=1;k<=*n;k++)
>>      {
>>          u[k][k]=1;
>>          for(i=k;i<=*n;i++)
>>          {
>>              sum=0;
>>              for(p=1;p<=k-1;p++)
>>                  sum+=l[i][p]*u[p][k];
>>              l[i][k]=a[i][k]-sum;
> a is a single-index array. You cannot index it using two indices.
> Convert the two into one using something like i*dimension + k. Same
> below.
>
>
> HTH,
>
> Peter
>
>>          }
>>
>>          for(j=k+1;j<=*n;j++)
>>          {
>>              sum=0;
>>              for(p=1;p<=k-1;p++)
>>                  sum+=l[k][p]*u[p][j];
>>              u[k][j]=(a[k][j]-sum)/l[k][k];
>>          }
>>      }
>>
>>
>>      //***** FI*NDI*NG Z; LZ=b*********//
>>
>>      for(i=1;i<=*n;i++)
>>      {                                        //forward subtitutio*n method
>>          sum=0;
>>          for(p=1<i;p++)
>>          sum+=l[i][p]*z[p];
>>          z[i]=(b[i]-sum)/l[i][i];
>>      }
>>      //********** FI*NDI*NG X; UX=Z***********//
>>
>>      for(i=*n;i>0;i--)
>>      {
>>          sum=0;
>>          for(p=*n;p>i;p--)
>>            sum+=u[i][p]*ab[p];
>>          ab[i]=(z[i]-sum)/u[i][i];
>>      }
>>
>> }
>>
>>
>> And here is the output:
>>   R CMD SHLIB lu1.c
>> gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      -fpic  -O3 -pipe
>> -g  -c lu1.c -o lu1.o
>> lu1.c: In function ‘lu1’:
>> lu1.c:15:8: error: incompatible types when assigning to type
>> ‘float[200]’ from type ‘double’
>> lu1.c:16:8: error: incompatible types when assigning to type
>> ‘float[200]’ from type ‘double’
>> lu1.c:33:25: error: subscripted value is neither array nor pointer nor vector
>> lu1.c:41:26: error: subscripted value is neither array nor pointer nor vector
>> make: *** [lu1.o] Error 1
>> erin at ubuntu:~$
>>
>> I'm thinking that the matrix is hanging things up.
>>
>> Does this look familiar, please?
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list