summaryrefslogtreecommitdiff
path: root/content/math/goldenSectionSearch.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
commit98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (patch)
tree5113d5cc24d1ad5f93810b6442ce584a36950dc8 /content/math/goldenSectionSearch.cpp
parentad3856a6b766087df0036de0b556f4700a6498c9 (diff)
parent8d11c6c8213f46f0fa19826917c255edd5d43cb1 (diff)
mzuenni tests
Diffstat (limited to 'content/math/goldenSectionSearch.cpp')
-rw-r--r--content/math/goldenSectionSearch.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/content/math/goldenSectionSearch.cpp b/content/math/goldenSectionSearch.cpp
new file mode 100644
index 0000000..28ee4c3
--- /dev/null
+++ b/content/math/goldenSectionSearch.cpp
@@ -0,0 +1,15 @@
+template<typename F>
+ld gss(ld l, ld r, F&& f) {
+ ld inv = (sqrt(5.0l) - 1) / 2;
+ ld x1 = r - inv*(r-l), x2 = l + inv*(r-l);
+ ld f1 = f(x1), f2 = f(x2);
+ for (int i = 0; i < 200; i++) {
+ if (f1 < f2) { //change to > to find maximum
+ r = x2; x2 = x1; f2 = f1;
+ x1 = r - inv*(r-l); f1 = f(x1);
+ } else {
+ l = x1; x1 = x2; f1 = f2;
+ x2 = l + inv*(r-l); f2 = f(x2);
+ }}
+ return l;
+}