Skip to content

.Internal

4 messages · rkevinburton at charter.net, Berwin A Turlach

#
I was trying to find source for optimize and I ran across

function (f, interval, ..., lower = min(interval), upper = max(interval), 
    maximum = FALSE, tol = .Machine$double.eps^0.25) 
{
    if (maximum) {
        val <- .Internal(fmin(function(arg) -f(arg, ...), lower, 
            upper, tol))
        list(maximum = val, objective = f(val, ...))
    }
    else {
        val <- .Internal(fmin(function(arg) f(arg, ...), lower, 
            upper, tol))
        list(minimum = val, objective = f(val, ...))
    }
}

Then I did a search for fmin and i came up with:

/* fmin(f, xmin, xmax tol) */
SEXP attribute_hidden do_fmin(SEXP call, SEXP op, SEXP args, SEXP rho)


So my question is where do I find the intermediary step between 

.Internal(fmin(function(arg) f(arg, ...), lower,  upper, tol))

and

SEXP attribute_hidden do_fmin(SEXP call, SEXP op, SEXP args, SEXP rho)

The number of arguments doesn't match up. I am guessing that lower and upper somehow get merged into the args. And rho is 'tol'. Right?

Thank you for your comments.

Kevin
#
G'day Kevin,

On Wed, 18 Mar 2009 21:46:51 -0700
<rkevinburton at charter.net> wrote:

            
@Article{Rnews:Ligges:2006,
  author       = {Uwe Ligges},
  title        = {{R} {H}elp {D}esk: {Accessing} the Sources},
  journal      = {R News},
  year         = 2006,
  volume       = 6,
  number       = 4,
  pages        = {43--45},
  month        = {October},
  url          = http,
  pdf	       = Rnews2006-4
}

http://CRAN.R-project.org/doc/Rnews/Rnews_2006-4.pdf
Unlikely.  In "Writing R Extensions" (and the functions I looked up),
'rho' usually denotes an environment that is used to evaluate
expressions in.  Typically (i.e. in cases that I had need to look at),
all arguments are rolled into the SEXP arg for internal functions.

HTH.

Cheers,

	Berwin
4 days later
#
Sorry to be so dense but the article that you suggest does not give any information on how the arguments are packed up. I look at the call:

val <- .Internal(fmin(function(arg) -f(arg, ...), lower, upper, tol))

and then with the help of this article I find do_fmin in optimize.c:

SEXP attribute_hidden do_fmin(SEXP call, SEXP op, SEXP args, SEXP rho)

Again there doesn't seem to be any coorespondance between lower, upper, tol and the arguments to do_fmin. So I am missing a step.

Thank you.

Kevin
---- Berwin A Turlach <berwin at maths.uwa.edu.au> wrote:
#
G'day Kevin,

On Mon, 23 Mar 2009 18:48:16 -0400
<rkevinburton at charter.net> wrote:

            
Sorry for not being clear enough.  Yes, the article tells you how to
find out that do_fmin is (eventually) called when you call optimize on
the command line.  I thought that this was one of your questions.
As far as I know, .Internal has the same interface as .External.  So a
study of "Writing R Extensions" should give insight regarding this
step.  In particular Chapter 5 (System and foreign language interfaces)
-> Section 5.10 (Interface functions .Call and .External) -> Section
5.10.2 (Calling .External).

Essentially, all arguments to a C function called via .Internal
or .External are passed as a single SEXP; this allows to pass an
unlimited number of arguments to a C function as all other interfaces
to native routines (.C, .Call, .Fortran) have some limit, albeit a
rather generous one, on the number of arguments that can be passed to
the native routine.

I believe you can think of that single SEXP as a list containing the
individual arguments.  Accessing those arguments one by one involves
macros with names like CDR, CAR, CADR, CADDR, CADDDR, CAD4R and so
forth.  As I understand it, if you are fluent in Lisp (Scheme?) and how
components of a list are referred to in that language, then you have no
problems with understanding the names of those macros.  Since I never
became comfortable with those languages, I restrict myself to .C
and .Call; YMMV.

HTH.

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