[Rd] .C-calls

Martin Schlather Martin.Schlather@uni-bayreuth.de
Tue, 13 Mar 2001 22:26:23 +0100

Dear all,

(sorry I got the wrong button for subscribing a minute ago)

At the moment I'm writing on a package for random field
simulation that I'd like to make publically availabe
in near future.
To this end I've asked Martin Maechler to have a look
at my R-code. He was very surprised about how
I perform the ".C"-calls, and encouraged me to
make this request for comments.

My calls typically look like
    result <- double(10000)

In order to avoid sending C-code, I'd like to
give the following example that uses `chol' of
the "base"-package of R. The fact that it is a
.Fortran-call instead of a .C-call shouldn't

## creating a positive definite matrix of size 100x100
lc <- as.integer(100);
cov.matrix <- matrix(runif(lc * lc), nrow=lc)
cov.matrix <- t(cov.matrix) %*% cov.matrix

## the usual way of getting the Cholesky decomposition
c0 <- chol(cov.matrix)

## direct call of the Fortran routine; the code is
## taken from the R-code of `chol'
dummy1 <- .Fortran("chol", cov.matrix, lc, lc,
               v = matrix(0, nr = lc, nc = lc), info = integer(1),
               DUP = FALSE, PACKAGE = "base")
c1 <- dummy1$v

## The third way.
## The code seems working very well... but?
dummy2 <- double(lc * lc)
.Fortran("chol", cov.matrix, lc, lc,
         dummy2, info = integer(1),
         DUP = FALSE, PACKAGE = "base")
c2 <- matrix(dummy2, lc, lc)

sum(abs(c0-c2)) # == 0


Martin Schlather                 email: Martin.Schlather@uni-bayreuth.de
Abteilung Bodenphysik            phone: +49 (0)921 55 2193          
Univ. Bayreuth                   Fax  : +49 (0)921 55 2246
D -- 95440 Bayreuth, Germany     http://www.geo.uni-bayreuth.de/~martin/
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch