[R] Matrix variable in C code

depire@inrets.fr depire at inrets.fr
Thu Feb 2 15:11:42 CET 2006


Dear all,
I try to compute some piece of my R code in C.

My problem is about matrix.

My code in R is the following:
=============================================
VPEfron<-function(XType,ZType,dX,G,c0,c1)
{
	XS<-sort(XType)
	rang<-sort.list(XType)

     	ZS<-matrix(0,k,max(dX))
	ZS<-ZType[rang,]
	S<-rep(0,length(XType))
	for(i in 1:length(XType))
		S[i]<-sum(ZS[i,])

      VPCEfron<-function(f,S,ZS,dX,tailleS=length(XType))
      {
		f.check<-function(x) {
            x<-f(x)
            }
     
.Call("VPCEfron",body(f.check),as.double(S),as.double(ZS),as.integer(dX),as.integer(tailleS),new.env())
	}
GG<-function(z) G(z,c0,c1)

Vraisemblancepartielle<-VPCEfron(GG,S,ZS,dX)
}
=============================================

and my test code in C is:
================================================
SEXP VPCEfron(SEXP f, SEXP SR, SEXP ZR, SEXP DIR, SEXP nsR, SEXP rho)
{
	int taille=INTEGER(nsR)[0];
	int* DI=INTEGER(DIR);
	double* S=REAL(SR);
	double* Z=(ZR);

// just to check the value
	printf("verifie de S: %f - %f - %f\n",S[0],S[1],S[2]);
	printf("verifie dX: %d %d %d\n",DI[0],DI[1],DI[2]);
	printf("verifie k: %d\n",taille);
	printf("verifie de Z\n");
	printf("%f %f\n",Z[0][0]);
	printf("%f %f\n",Z[1][1]);
	printf("%f %f\n",Z[2][1]);
	somme=0.0;
	printf("STOP\n");
return(somme);
}
=========================================


All works, except ZS, the variable ZS is a matrix in R, and when i try to give
to C code, with ZR, ZR is only a vector.

How to obtain a matrix variable in C ?




More information about the R-help mailing list