[Rd] a question about optim.R and optim.c in R

Michael Weylandt michael.weylandt at gmail.com
Mon Jul 7 23:36:04 CEST 2014



> On Jul 7, 2014, at 4:34 PM, Zhiyuan Dong <zhiyuan.dong at gmail.com> wrote:
> 
> Hi, I am learning R by reading R source code. Here is one question I have
> about the optim function in R.
> 
> The context : In the optim.R, after all the prep steps, the main function
> call call is made via :
> 
> .External2(C_optim, par, fn1, gr1, method, con, lower, upper).
> 
> So, it seems to me, to follow what is going on from here, that I should
> read the optim function in \src\library\stats\src\optim.c
> 
> where it has this signature :
> 
> SEXP optim(SEXP call, SEXP op, SEXP args, SEXP rho)
> 
> I am not sure I follow here : In the .External2 call, we have 7 parameters
> :  par, fn1, gr1, method, con, lower, upper; This does not seem to match
> the signature of
> 
> SEXP optim(SEXP call, SEXP op, SEXP args, SEXP rho)
> 
> However, it seems (from the source code) that the 7 parameters are somehow
> embedded in the 'args' parameter. I am not sure what is going on...Am I
> missing something?
> 

IIRC, args is a pairlist object. If the analogy helps, it's something like the OO pattern of sticking all your function parameters in an argument and passing that instead of passing them all as formals. One advantage of this is sane(er) handling of variadic functions at the C level. 

Michael


> Thanks much!!!
> 
> Best,
> 
> Zhiyuan
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list