Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@m1t0

# 黄金分割法のRcppでの実装例

More than 1 year has passed since last update.

# 2. サンプルプログラム

cpp, goldensection.cpp
```#include<Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]

List Gsearch(Function f, List interval, double tol = 1e-6, int niter = 1000){
double upper, lower, gam, ai, bi;
List output(3), fs(2);
lower = interval[0];
upper = interval[1];
gam = (3-sqrt(5))/2;
//std::cout << gam << std::endl;
ai = lower + gam*(upper - lower);
bi = lower + (1-gam)*(upper - lower);
output[2] = niter;
for(int i = 0; i < niter; i++){
//std::cout << lower << " " <<  ai << " " << bi << " " << upper << std::endl;
double fai = as<double>(f(ai));
double fbi = as<double>(f(bi));
if(fai <= fbi){
upper = bi;
bi = ai;
ai = lower + gam * (upper - lower);
}else{
lower = ai;
ai = bi;
bi = lower + (1-gam)*(upper - lower);
}
if(abs(upper - lower) < tol){
output[2] = i;
break;
}
}
output[0] = lower;
output[1] = upper;
return output;
}
```

f(ai) <= f(bi)の評価の際に，そのまま評価すると，結果が安定しないという点に詰まりました．f(ai),f(bi)の返り値の型をdoubleに変換することで解決しました．

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
Major: statistics/Multivariate analysis/Psychometrics/R/ (R) C++/python3