poolloopus at yahoo.com
2008-Mar-12 15:59 UTC
[R] Problem when calling FORTRAN subroutine (dll)
Hello, I am trying to call a FORTRAN subroutine from R. The Fortran code is @: http://lib.stat.cmu.edu/apstat/206 It performs a bivariate isotonic regression on a rectangular grid (m X n) matrix. I used the g77 compiler and successfully created a dll file and it also loads successfully from R. But somehow the programs fails to run properly. (I do get the correct result when I compile the FORTRAN code). Please see below and note that since my input matrix X is monotonically increasing in both directions, input should be equal to output since the weights are set to 1 for all elements. (also verified this in FORTRAN to make sure that the subroutine works). Output[[10]] is supposed to be the monotonically regressed output, but it does not come out correctly. I don't know if its something to do with the way I am feeding the parameters to the DLL. Thanks in advance for any help I can receive. I am not god with FORTRAN - very very infrequent FORTRAN user. I am using R 2.4.0. Regards Krishna.> rm(list=ls(all=TRUE)) > dyn.load("c:/smooth.dll") > is.loaded("smooth")[1] TRUE> NROW<-4 > NCOL<-4 > NDIM<-4 > NCYCLE<-5 > X<-array(c(0.05,0.2,0.35,0.43,0.09,0.29,0.41,0.55,0.2,0.33,0.55,0.7,0.33,0.48,0.6,0.8)) > W<-array(rep(1.0,length(X))) > X<-matrix(X,nrow=4,ncol=4) > X<-t(X) > W<-matrix(W,nrow=4,ncol=4) > A<-array(0,dim=c(NROW,NCOL,4)) > B<-array(0,dim=c(NDIM,5)) > NCYCLE<-5 > ICYCLE<-5 > G<-(X*0)+0.0 > EPS=0.001 > IFAULT<-5# Here is how the subroutine says to input the parameters; #SUBROUTINE SMOOTH(NROW, NCOL, NDIM, X, W, A, B, NCYCLE, ICYCLE, G, EPS, IFAULT) # Definitions of each of these input parameters are below; #REAL X(NROW,NCOL), W(NROW,NCOL), A(NROW,NCOL,4), B(NDIM,5), G(NROW,NCOL), EPS, ZERO, DELTA, DELC, DELR, ORD, WW, FRACT DATA ZERO/0.0/, DELTA/0.00001/, FRACT/0.5/ # Here is my R output;> output<-.C("smooth_",as.integer(NROW),as.integer(NCOL),as.integer(NDIM),as.real(X),as.real(W),as.real(A),as.real(B),as.integer(NCYCLE),as.integer(ICYCLE),as.real(G),as.real(EPS),as.integer(IFAULT)) > > output[[1]] [1] 4 [[2]] [1] 4 [[3]] [1] 4 [[4]] [1] 0.05 0.09 0.20 0.33 0.20 0.29 0.33 0.48 0.35 0.41 0.55 0.60 0.43 0.55 0.70 [16] 0.80 [[5]] [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [[6]] [1] 0.000000e+00 0.000000e+00 0.000000e+00 -5.377600e-19 0.000000e+00 [6] 5.377600e-19 0.000000e+00 0.000000e+00 1.290268e-11 1.803836e-13 [11] 1.333175e-27 0.000000e+00 1.333175e-27 0.000000e+00 -3.434319e-15 [16] 5.092237e-315 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 [21] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 4.999999e-02 [26] 8.999999e-02 2.000000e-01 3.300000e-01 2.000000e-01 2.900000e-01 [31] 3.300000e-01 4.799998e-01 0.000000e+00 0.000000e+00 0.000000e+00 [36] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 [41] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 [46] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 [51] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 [56] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 [61] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 [[7]] [1] 0.3300000 0.4799998 1.0000002 1.0000002 0.2716666 0.4799998 0.0000000 [8] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 [15] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 [[8]] [1] 5 [[9]] [1] 5 [[10]] [1] 0.0795833 0.1145833 0.2008331 0.3100000 0.2008331 0.3100000 0.2716666 [8] 0.4799998 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 [15] 0.0000000 0.0000000 [[11]] [1] 0.001 [[12]] [1] 5> > >____________________________________________________________________________________ Be a better friend, newshound, and
G'day Krishna, On Wed, 12 Mar 2008 08:59:30 -0700 (PDT) "poolloopus at yahoo.com" <poolloopus at yahoo.com> wrote:> > output<-.C("smooth_",as.integer(NROW),as.integer(NCOL),as.integer(NDIM),as.real(X),as.real(W),as.real(A),as.real(B),as.integer(NCYCLE),as.integer(ICYCLE),as.real(G),as.real(EPS),as.integer(IFAULT))1) You are calling FORTRAN code and not C code, so why are you using .C instead of .Fortran? 2) Why do you append an underscore to the name of the routine that you are using? 3) According to the FORTRAN source, the variables are defined as REALs, not as DOUBLE PRECISION. R, by default expects to interface to DOUBLE PRECISION. If you want to interface to REALs, you have to use as.single or set the storage mode to single. Details regarding all these points are in the "Writing R Extensions" manual which I would strongly recommend you to study. Cheers, Berwin =========================== Full address ============================Berwin A Turlach Tel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability +65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba