# Re: [R] Matrix variable in C code

From: <depire_at_inrets.fr>
Date: Fri 03 Feb 2006 - 21:45:24 EST

I correct a little my code, in R code, i use "as.matrix" for ZT, so i haven't got "segment fault" but it seems that i transmit only the first colon and because "ncol" gives "1" and not "2".

So what happen ?

Programs

• R CODE - test.R ===== X<-c(4,2,3,2) Z<-c(40,21,30,20) dX<-c(2,1,1)

Phi<-function(z,a,b)
{

Phi<-z
}

VPEfron<-function(XType,ZType,dXType,G,c0,c1)
{

####################
ZT<-matrix(0,3,2)
ZT[1,1]<-Z[2]
ZT[1,2]<-Z[4]
ZT[2,1]<-Z[3]
ZT[3,1]<-Z[1]
####################

print(ZT)
# A OPTIMISER
VPCEfron<-function(f,XT,ZT,dXT,tailleS)
{
f.check<-function(x) {
x<-f(x)
}

.Call("VPCEfron",body(f.check),as.double(XT),as.matrix(ZT),as.integer(dXT),as.integer(tailleS),new.env())
}

GG<-function(z) G(z,c0,c1)

VPEfron<-VPCEfron(GG,XType,ZType,dXType,length(XType)) }

resultat<-VPEfron(X,Z,dX,Phi,0,0)
==== END R CODE ==========

• C CODE of test.c ============ #include <R.h> #include <Rdefines.h> #include <Rinternals.h>

#define RMATRIX(m,i,j) (REAL(m)[ INTEGER(GET_DIM(m))[0]*(j)+(i) ])

SEXP mkans(double x)
{

SEXP ans;
PROTECT(ans = allocVector(REALSXP,1));
REAL(ans)[0]=x;
UNPROTECT(1);
return ans;

}

SEXP VPCEfron(SEXP f, SEXP XR, SEXP ZR, SEXP DIR, SEXP tailleR, SEXP rho)
{

double* X=REAL(XR);
int* DI=INTEGER(DIR);
int taille=INTEGER(tailleR)[0];
int nligne=INTEGER(GET_DIM(ZR))[0];
int ncol=INTEGER(GET_DIM(ZR))[1];

printf("verifie taille: %d\n",taille);
printf("verifie de X: %f - %f - %f - %f\n",X[0],X[1],X[2],X[3]);
printf("verifie dX: %d %d %d\n",DI[0],DI[1],DI[2]);

printf("verifie de Z\n");
printf("%d %d\n",nligne,ncol);
printf("%f %f\n",RMATRIX(ZR,0,0),RMATRIX(ZR,0,1));
printf("%f %f\n",RMATRIX(ZR,1,0),RMATRIX(ZR,1,1));
printf("%f %f\n",RMATRIX(ZR,2,0),RMATRIX(ZR,2,1));

return mkans(0.0);

}
==== END CODE =====

R-help@stat.math.ethz.ch mailing list