Re: [R] Matrix variable in C code
On 2/2/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Selon Gabor Csardi <[EMAIL PROTECTED]>:
>
> > On Thu, Feb 02, 2006 at 03:11:42PM +0100, [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>MTA RMKI, ELTE TTK
> >
> > __
> > [email protected] 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
> #include
>
> #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,
...).
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Matrix variable in C code
No idea, your code (now that you're not using as.double) kinda works for me.
> source("test.R")
[,1] [,2]
[1,] 21 20
[2,] 300
[3,] 400
verifie taille: 4
verifie de X: 4.00 - 2.00 - 3.00 - 2.00
verifie dX: 2 1 1
verifie de Z
4 1
40.00 0.00
21.00 0.00
30.00 0.00
>
The only thing is that your code is very messy (for me at least), and it's
true that you print the value of ZT and it is a 3x3 matrix, but if you print
its value just before the .Call it is just a vector of length 4. Then
as.matrix() creates a 4x1 matrix of this.
Look at this simple R code, it works with your test.c file fine:
##
dyn.load("test.so")
f.check <- function(a) a
XT <- 1:4
ZT <- matrix( c(21,30,40,20,0,0), nr=3, nc=2)
dXT <- 1:3
tailleS <- 5
.Call("VPCEfron",body(f.check),as.double(XT),as.matrix(ZT),as.integer(dXT),
as.integer(tailleS),new.env())
#
Gabor
On Fri, Feb 03, 2006 at 11:45:24AM +0100, [EMAIL PROTECTED] wrote:
> 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)
>
> 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]
>
>
> 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
> #include
> #include
>
> #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 =
>
>
> This message was sent using IMP, the Internet Messaging Program.
--
Csardi Gabor <[EMAIL PROTECTED]>MTA RMKI, ELTE TTK
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Matrix variable in C code
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)
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]
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
#include
#include
#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 =
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Matrix variable in C code
Selon Gabor Csardi <[EMAIL PROTECTED]>:
> On Thu, Feb 02, 2006 at 06:27:47PM +0100, [EMAIL PROTECTED] wrote:
> [...]
>
> The problem is that as.double drops the dim attribute:
> > b <- matrix( 1:4, 2, 2)
> > b
> [,1] [,2]
> [1,]13
> [2,]24
> > 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,]13
> [2,]24
>
> 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
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Matrix variable in C code
Selon Gabor Csardi <[EMAIL PROTECTED]>:
> On Thu, Feb 02, 2006 at 06:27:47PM +0100, [EMAIL PROTECTED] wrote:
> [...]
>
> The problem is that as.double drops the dim attribute:
> > b <- matrix( 1:4, 2, 2)
> > b
> [,1] [,2]
> [1,]13
> [2,]24
> > 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,]13
> [2,]24
>
> 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 make a mistake writing the small code, the correct one is the following
with the correct dim for ZT
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
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Matrix variable in C code
On Thu, Feb 02, 2006 at 06:27:47PM +0100, [EMAIL PROTECTED] wrote: [...] The problem is that as.double drops the dim attribute: > b <- matrix( 1:4, 2, 2) > b [,1] [,2] [1,]13 [2,]24 > 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,]13 [2,]24 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? Also, this is an unrelated issue but your VPCEfron C function takes five parameters and the R code provides six Gabor > = > > You can save these two simple programs in order to test my code. > > Thanks. > [...] -- Csardi Gabor <[EMAIL PROTECTED]>MTA RMKI, ELTE TTK __ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Matrix variable in C code
Selon Gabor Csardi <[EMAIL PROTECTED]>:
> On Thu, Feb 02, 2006 at 03:11:42PM +0100, [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>MTA RMKI, ELTE TTK
>
> __
> [email protected] 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
#include
#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.
Thanks.
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Matrix variable in C code
On Thu, Feb 02, 2006 at 03:11:42PM +0100, [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>MTA RMKI, ELTE TTK
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
