[R] Matrix variable in C code

depire@inrets.fr depire at inrets.fr
Fri Feb 3 09:11:42 CET 2006


Selon Gabor Csardi <csardi at rmki.kfki.hu>:

> On Thu, Feb 02, 2006 at 06:27:47PM +0100, depire at inrets.fr wrote:
> [...]
>
> The problem is that as.double drops the dim attribute:
> > b <- matrix( 1:4, 2, 2)
> > b
>      [,1] [,2]
> [1,]    1    3
> [2,]    2    4
> > as.double(b)
> [1] 1 2 3 4
> >
>
> You can try:
>
> > b <- matrix( 1:4, 2, 2)
> > d <- dim(b)
> > b <- as.double(b)
> > b
> [1] 1 2 3 4
> > dim(b) <- d
> > b
>      [,1] [,2]
> [1,]    1    3
> [2,]    2    4
>
> Another thing is that i cannot really see why the ZT parameter of the .Call
> should be a matrix anyway. It's just the vector (40 21 30 20). Or am i
> missing something?

Sorry, i made a mistake writing the small code, the correct one is the following
with the correct dim for ZT. But it doesn't work...

========================================
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)
{
        ####################
        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]
        ####################

        # 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 new piece of code is between ###.

Thanks,

Alex




More information about the R-help mailing list