Disclaimer: I've searched for an answer using the keywords: R, optimize, C++, C, optima, maxima, minima, local maximum, optimization, Newton's Method, Gradient descent, etc. and haven't found any satisfactory answers. R's optimize man page gives the original Fortran code but not the C translation of it. Please let me know if I should have searched for other keywords or if you can quickly find a website that clearly answers this question.
Question: I'm new to C++ and want to convert one of my R programs into C++. I use the optimize function in R and want to know if there are any libraries/header files/functions in C++ that will easily give me the same results. Please give an example if possible.
Here is a simple example of R's optimize, maximizing f(p) = p*(1-p) over (0,1) where the maxi开发者_Go百科mum is at p = 0.5 and f(0.5) = 0.25:
> optimize(function(p) p*(1-p),c(0,1),maximum=T)
$maximum
[1] 0.5
$objective
[1] 0.25
Thank you for your help!
http://cran.r-project.org/src/base/R-2/R-2.13.1.tar.gz
The code is in ../R-2.13.1/src/main/optimize.c
The R source code is available at http://cran.r-project.org/. You should be able to get the c implementation there, making it c++ should be trivial.
The R function optimize() essentially implements Brent's method. For its realization in C++ or several other languages, you can simply download from here: https://people.sc.fsu.edu/~jburkardt/cpp_src/brent/brent.html
精彩评论