[R] Matrix variable in C code

Douglas Bates dmbates at gmail.com
Fri Feb 3 16:15:54 CET 2006


On 2/2/06, depire at inrets.fr <depire at inrets.fr> wrote:
> Selon Gabor Csardi <csardi at rmki.kfki.hu>:
>
> > On Thu, Feb 02, 2006 at 03:11:42PM +0100, depire at inrets.fr wrote:
> > [...]
> > >
> > > 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];
> > [...]
> > >
> > > 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 ?
> >
> > A matrix is the same as a vector (stored columnwise), except that is has a
> > dim attribute. Use can use SET_DIM to set the dim attribute, and GET_dim to
> > query it. Eg:
> >
> > int nrow=INTEGER(GET_DIM(ZR))[0];
> > int ncol=INTEGER(GET_DIM(ZR))[1];
> >
> > To access the values in the matrix you might use something like:
> >
> > #define RMATRIX(m,i,j) (REAL(m)[ INTEGER(GET_DIM(m))[0]*(j)+(i) ])
> >
> > and then
> >
> > RMATRIX(ZR, 0, 1), etc. works. Note that according to this #define the
> > matrix
> > is indexed from zero.
> >
> > Gabor
> >
> >
> > --
> > Csardi Gabor <csardi at rmki.kfki.hu>    MTA RMKI, ELTE TTK
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
>
> I try but i think that i make some mistake because i obtain segment fault.
> To be sure, i reduce the code to permit to test it, the goal is just to print
> the variable, i try with vector variables (real X and integer dX in the
> following), it works; but with matrix it doesn't work.
>
> The code of the R program - Test.R
> =====================================
> X<-c(4,2,3,2)
> Z<-c(40,21,30,20)
> dX<-c(2,1,1)
>
> dyn.load("test.so")
>
> Phi<-function(z,a,b)
> {
>         Phi<-z
> }
>
> VPEfron<-function(XType,ZType,dXType,G,c0,c1)
> {
>         # A OPTIMISER
>       VPCEfron<-function(f,XT,ZT,dXT,tailleS=length(XT))
>       {
>                 f.check<-function(x) {
>             x<-f(x)
>             }
>
> .Call("VPCEfron",body(f.check),as.double(XT),as.double(ZT),as.integer(dXT),as.integer(tailleS),new.env())
>         }
> GG<-function(z) G(z,c0,c1)
>
> Vraisemblancepartielle<-VPCEfron(GG,XType,ZType,dXType)
> }
>
> resultat<-VPEfron(X,Z,dX,Phi,0,0)
> ==============================
>
> The code of C code - test.c and test.so is obtained by "R CMD SHLIB test.c"
> ================================
> #include <R.h>
> #include <Rdefines.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 rho)
> {
>         double* X=REAL(XR);
>         int* DI=INTEGER(DIR);
>         int nligne=INTEGER(GET_DIM(ZR))[0];
>         int ncol=INTEGER(GET_DIM(ZR))[1];
>
>         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));
>         printf("%f %f\n",RMATRIX(ZR,0,1));
>         printf("%f %f\n",RMATRIX(ZR,1,0));
>         printf("%f %f\n",RMATRIX(ZR,2,0));
>
>         return mkans(0.0);
> }
> =====================================
>
> You can save these two simple programs in order to test my code.

It's a small point but there is no need to write the C function mkans
- you can use the function ScalarReal (or ScalarLogical, ScalarInt,
...).




More information about the R-help mailing list