[R] R function calling. Do I understand this right?
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Tue Aug 29 08:47:11 CEST 2000
On Mon, 28 Aug 2000, Paul E Johnson wrote:
> I need to write a nonhierarchical clustering routine and I'm studying
> the way hclust (in the mva library) is built in R to see how things are
> done and what I can modify. I ran f2c on the hclust.f file (so I could
> read it in a language I know!) and there is one thing I don't quite
> understand about the way it gets called and the way it returns values.
> That Fortran function gets called in hclust.R, like this:
>
> hclust <- function(d, method="complete")
> {
> <snip>
> n <- attr(d, "Size")
> if(is.null(n))
> stop("invalid dissimilarities")
> labels <- attr(d, "Labels")
>
> len <- n*(n-1)/2
> hcl <- .Fortran("hclust",
> n = as.integer(n),
> len = as.integer(len),
> method = as.integer(method),
> ia = integer(n),
> ib = integer(n),
> crit = double(n),
> membr = double(n),
> nn = integer(n),
> disnn = double(n),
> flag = logical(n),
> diss = as.double(d), PACKAGE="mva")
> <snip>
>
> In this call, the "actual data" input is an integer "n", an integer
> "len", an integer "method", and "diss", which is the vector obtained by
> coercing bottom left part of a dissimiliarty matrix into a vector of
> doubles.
>
> So, I believe that means d is like this
>
>
> x x x
> 1.1 x x
> 3.3 4.4 x
>
> And diss is {1.1,3.3,4.4}
>
> Now here is the qustion. All of the other variables listed as input,
> ia,ib, crit,membr,nn,disnn, and flag, are just empty vectors being
> passed through to be used inside the hclust function. Right? And the
> changes made in those vectors inside the Fortran function are permanent,
> so the other functions in the hclust.R file can use those values? (like
> passing in a pointer in C?)
No. Copies of variables are made on entry and on exit. Thus the .C
call returns a list of vectors with the names given. You cannot
alter the versions in the function, but you have to use explicitly
the components of the returned result. It's normal only to name
components you are going to use subsequently, but some people
name them all as an aide-memoire.
(I am ignoring DUP=TRUE in .C in this dicussion.)
If you are going to write in C you might want to start with .Call these
days.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list