4222 |
10 Sep 22 |
peter |
// $Id$ |
4222 |
10 Sep 22 |
peter |
2 |
|
4222 |
10 Sep 22 |
peter |
3 |
/* |
4250 |
18 Nov 22 |
peter |
Copyright (C) 2022 Jari Häkkinen, Peter Johansson |
4222 |
10 Sep 22 |
peter |
5 |
|
4222 |
10 Sep 22 |
peter |
This file is part of the yat library, https://dev.thep.lu.se/yat |
4222 |
10 Sep 22 |
peter |
7 |
|
4222 |
10 Sep 22 |
peter |
The yat library is free software; you can redistribute it and/or |
4222 |
10 Sep 22 |
peter |
modify it under the terms of the GNU General Public License as |
4222 |
10 Sep 22 |
peter |
published by the Free Software Foundation; either version 3 of the |
4222 |
10 Sep 22 |
peter |
License, or (at your option) any later version. |
4222 |
10 Sep 22 |
peter |
12 |
|
4222 |
10 Sep 22 |
peter |
The yat library is distributed in the hope that it will be useful, |
4222 |
10 Sep 22 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4222 |
10 Sep 22 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4222 |
10 Sep 22 |
peter |
General Public License for more details. |
4222 |
10 Sep 22 |
peter |
17 |
|
4222 |
10 Sep 22 |
peter |
You should have received a copy of the GNU General Public License |
4222 |
10 Sep 22 |
peter |
along with yat. If not, see <https://www.gnu.org/licenses/>. |
4222 |
10 Sep 22 |
peter |
20 |
*/ |
4222 |
10 Sep 22 |
peter |
21 |
|
4222 |
10 Sep 22 |
peter |
22 |
#include <config.h> |
4222 |
10 Sep 22 |
peter |
23 |
|
4222 |
10 Sep 22 |
peter |
24 |
#include "Suite.h" |
4222 |
10 Sep 22 |
peter |
25 |
|
4230 |
14 Sep 22 |
peter |
26 |
#include "yat/utility/BrentMinimizer.h" |
4222 |
10 Sep 22 |
peter |
27 |
#include "yat/utility/GoldenSection.h" |
4222 |
10 Sep 22 |
peter |
28 |
#include "yat/utility/Minimizer.h" |
4222 |
10 Sep 22 |
peter |
29 |
#include "yat/utility/QuadGolden.h" |
4222 |
10 Sep 22 |
peter |
30 |
|
4225 |
13 Sep 22 |
jari |
31 |
#include <cmath> |
4225 |
13 Sep 22 |
jari |
32 |
|
4222 |
10 Sep 22 |
peter |
33 |
using namespace theplu::yat; |
4222 |
10 Sep 22 |
peter |
34 |
using namespace theplu::yat::utility; |
4222 |
10 Sep 22 |
peter |
35 |
|
4222 |
10 Sep 22 |
peter |
36 |
struct MyFunction |
4222 |
10 Sep 22 |
peter |
37 |
{ |
4222 |
10 Sep 22 |
peter |
38 |
double operator()(double x) const |
4222 |
10 Sep 22 |
peter |
39 |
{ |
4222 |
10 Sep 22 |
peter |
40 |
return std::pow(x - 1.0, 2); |
4222 |
10 Sep 22 |
peter |
41 |
} |
4222 |
10 Sep 22 |
peter |
42 |
}; |
4222 |
10 Sep 22 |
peter |
43 |
|
4222 |
10 Sep 22 |
peter |
44 |
|
4222 |
10 Sep 22 |
peter |
45 |
void run_test(Minimizer& minimizer, test::Suite& suite) |
4222 |
10 Sep 22 |
peter |
46 |
{ |
4222 |
10 Sep 22 |
peter |
47 |
MyFunction func; |
4247 |
24 Sep 22 |
peter |
48 |
double x0 = minimizer(func, 0, -3, 3, Minimizer::Interval(0.001, 1e-5), 10); |
4222 |
10 Sep 22 |
peter |
49 |
suite.out() << "x after 10 iterations: " << x0 << "\n"; |
4247 |
24 Sep 22 |
peter |
50 |
double x = minimizer(func, x0, -3, 3, Minimizer::Interval(0.001, 1e-5)); |
4222 |
10 Sep 22 |
peter |
51 |
suite.out() << "result x: " << x << "\n"; |
4224 |
13 Sep 22 |
peter |
52 |
if (!suite.equal_fix(x, 1.0, 0.005)) { |
4222 |
10 Sep 22 |
peter |
53 |
suite.err() << "error: incorrect x\n"; |
4222 |
10 Sep 22 |
peter |
54 |
suite.add(false); |
4222 |
10 Sep 22 |
peter |
55 |
} |
4222 |
10 Sep 22 |
peter |
56 |
} |
4222 |
10 Sep 22 |
peter |
57 |
|
4222 |
10 Sep 22 |
peter |
58 |
|
4222 |
10 Sep 22 |
peter |
59 |
template<typename T> |
4222 |
10 Sep 22 |
peter |
60 |
void run_test(test::Suite& suite) |
4222 |
10 Sep 22 |
peter |
61 |
{ |
4222 |
10 Sep 22 |
peter |
62 |
T minimizer; |
4222 |
10 Sep 22 |
peter |
63 |
run_test(minimizer, suite); |
4222 |
10 Sep 22 |
peter |
64 |
} |
4222 |
10 Sep 22 |
peter |
65 |
|
4222 |
10 Sep 22 |
peter |
66 |
|
4222 |
10 Sep 22 |
peter |
67 |
int main(int argc, char* argv[]) |
4222 |
10 Sep 22 |
peter |
68 |
{ |
4222 |
10 Sep 22 |
peter |
69 |
test::Suite suite(argc, argv); |
4222 |
10 Sep 22 |
peter |
70 |
|
4222 |
10 Sep 22 |
peter |
71 |
suite.out() << "test GoldenSection\n"; |
4222 |
10 Sep 22 |
peter |
72 |
run_test<GoldenSection>(suite); |
4230 |
14 Sep 22 |
peter |
73 |
suite.out() << "test BrentMinimizer\n"; |
4230 |
14 Sep 22 |
peter |
74 |
run_test<BrentMinimizer>(suite); |
4222 |
10 Sep 22 |
peter |
75 |
suite.out() << "test QuadGolden\n"; |
4222 |
10 Sep 22 |
peter |
76 |
run_test<QuadGolden>(suite); |
4222 |
10 Sep 22 |
peter |
77 |
return suite.return_value(); |
4222 |
10 Sep 22 |
peter |
78 |
} |