[R] passing a matrix from R to C code

Erin Hodgess erinm.hodgess at gmail.com
Tue Sep 25 00:03:24 CEST 2012


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];


    for(i=0;i<na;i++) {
      for(j=0;j<na;j++) {
	l[i,j]=0.0;
	u[i,j]=0.0;
      }
      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;
        }

        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?

Thanks,
Erin

-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodgess at gmail.com



More information about the R-help mailing list