How to implement the R function "optimize" in C ++?

Disclaimer: I searched for the answer using the keywords: R, optimization, C ++, C, optimization, maxima, minima, local maximum, optimization, Newton's method, descent of the gradient, etc. and did not find satisfactory answers. R optimizing the manual page gives Fortran source code, but not C translation. Please let me know if I should look for other keywords or if you can quickly find a website that clearly answers this question.

Question: I am new to C ++ and want to convert one of my R programs to C ++. I am using the optimization function in R and want to know if there are any libraries / header / function files in C ++ that will easily give me the same results. Give an example, if possible.

Here is a simple optimization example R, maximizing f (p) = p * (1-p) over (0,1), where the maximum is 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!

+6
source share
3 answers

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

+5
source

The source code for R is available at http://cran.r-project.org/ . You should be able to implement a c-implementation, so C ++ should be trivial.

+5
source

Optimization of the R () function essentially implements the Brent method. To implement it in C ++ or several other languages, you can simply download here: https://people.sc.fsu.edu/~jburkardt/cpp_src/brent/brent.html

0
source

Source: https://habr.com/ru/post/896172/


All Articles