4238 |
18 Sep 22 |
peter |
// $Id$ |
4238 |
18 Sep 22 |
peter |
2 |
|
4238 |
18 Sep 22 |
peter |
3 |
/* |
4238 |
18 Sep 22 |
peter |
Copyright (C) 2022 Peter Johansson |
4238 |
18 Sep 22 |
peter |
5 |
|
4238 |
18 Sep 22 |
peter |
This file is part of the yat library, https://dev.thep.lu.se/yat |
4238 |
18 Sep 22 |
peter |
7 |
|
4238 |
18 Sep 22 |
peter |
The yat library is free software; you can redistribute it and/or |
4238 |
18 Sep 22 |
peter |
modify it under the terms of the GNU General Public License as |
4238 |
18 Sep 22 |
peter |
published by the Free Software Foundation; either version 3 of the |
4238 |
18 Sep 22 |
peter |
License, or (at your option) any later version. |
4238 |
18 Sep 22 |
peter |
12 |
|
4238 |
18 Sep 22 |
peter |
The yat library is distributed in the hope that it will be useful, |
4238 |
18 Sep 22 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4238 |
18 Sep 22 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4238 |
18 Sep 22 |
peter |
General Public License for more details. |
4238 |
18 Sep 22 |
peter |
17 |
|
4238 |
18 Sep 22 |
peter |
You should have received a copy of the GNU General Public License |
4238 |
18 Sep 22 |
peter |
along with yat. If not, see <https://www.gnu.org/licenses/>. |
4238 |
18 Sep 22 |
peter |
20 |
*/ |
4238 |
18 Sep 22 |
peter |
21 |
|
4238 |
18 Sep 22 |
peter |
22 |
#include <config.h> |
4238 |
18 Sep 22 |
peter |
23 |
|
4238 |
18 Sep 22 |
peter |
24 |
#include "Suite.h" |
4238 |
18 Sep 22 |
peter |
25 |
|
4238 |
18 Sep 22 |
peter |
26 |
#include "yat/utility/Newton.h" |
4238 |
18 Sep 22 |
peter |
27 |
#include "yat/utility/Secant.h" |
4238 |
18 Sep 22 |
peter |
28 |
#include "yat/utility/Steffenson.h" |
4238 |
18 Sep 22 |
peter |
29 |
|
4238 |
18 Sep 22 |
peter |
30 |
#include <gsl/gsl_math.h> |
4238 |
18 Sep 22 |
peter |
31 |
|
4238 |
18 Sep 22 |
peter |
32 |
#include <cassert> |
4238 |
18 Sep 22 |
peter |
33 |
#include <cmath> |
4238 |
18 Sep 22 |
peter |
34 |
|
4238 |
18 Sep 22 |
peter |
35 |
using namespace theplu::yat; |
4238 |
18 Sep 22 |
peter |
36 |
using namespace theplu::yat::utility; |
4238 |
18 Sep 22 |
peter |
37 |
|
4238 |
18 Sep 22 |
peter |
38 |
struct MyFunction |
4238 |
18 Sep 22 |
peter |
39 |
{ |
4238 |
18 Sep 22 |
peter |
40 |
double operator()(double x) const |
4238 |
18 Sep 22 |
peter |
41 |
{ |
4238 |
18 Sep 22 |
peter |
42 |
return std::exp(x) - 2; |
4238 |
18 Sep 22 |
peter |
43 |
} |
4238 |
18 Sep 22 |
peter |
44 |
|
4239 |
18 Sep 22 |
peter |
45 |
double derivative(double x) const |
4238 |
18 Sep 22 |
peter |
46 |
{ |
4238 |
18 Sep 22 |
peter |
47 |
return std::exp(x); |
4238 |
18 Sep 22 |
peter |
48 |
} |
4238 |
18 Sep 22 |
peter |
49 |
}; |
4238 |
18 Sep 22 |
peter |
50 |
|
4238 |
18 Sep 22 |
peter |
51 |
|
4238 |
18 Sep 22 |
peter |
52 |
void run_test(RootFinderDerivative& finder, test::Suite& suite) |
4238 |
18 Sep 22 |
peter |
53 |
{ |
4238 |
18 Sep 22 |
peter |
54 |
MyFunction func; |
4238 |
18 Sep 22 |
peter |
55 |
suite.out() << "name: " << finder.name() << "\n"; |
4238 |
18 Sep 22 |
peter |
56 |
|
4238 |
18 Sep 22 |
peter |
57 |
double x = finder(func, 0, RootFinderDerivative::Delta(0, 1e-5)); |
4238 |
18 Sep 22 |
peter |
58 |
suite.out() << "x: " << x << "\n"; |
4238 |
18 Sep 22 |
peter |
59 |
|
4238 |
18 Sep 22 |
peter |
60 |
double margin = 1e-3; |
4238 |
18 Sep 22 |
peter |
61 |
x = finder(func, 0, |
4238 |
18 Sep 22 |
peter |
62 |
RootFinderDerivative::Residual<MyFunction>(func, margin)); |
4238 |
18 Sep 22 |
peter |
63 |
suite.out() << "x: " << x << "\n"; |
4238 |
18 Sep 22 |
peter |
64 |
if (!suite.equal_fix(func(x), 0, margin)) { |
4238 |
18 Sep 22 |
peter |
65 |
suite.err() << "error using Residual as stopper\n"; |
4238 |
18 Sep 22 |
peter |
66 |
suite.add(false); |
4238 |
18 Sep 22 |
peter |
67 |
} |
4238 |
18 Sep 22 |
peter |
68 |
} |
4238 |
18 Sep 22 |
peter |
69 |
|
4238 |
18 Sep 22 |
peter |
70 |
|
4238 |
18 Sep 22 |
peter |
71 |
template<typename T> |
4238 |
18 Sep 22 |
peter |
72 |
void run_test(test::Suite& suite) |
4238 |
18 Sep 22 |
peter |
73 |
{ |
4238 |
18 Sep 22 |
peter |
74 |
T finder; |
4238 |
18 Sep 22 |
peter |
75 |
run_test(finder, suite); |
4238 |
18 Sep 22 |
peter |
76 |
} |
4238 |
18 Sep 22 |
peter |
77 |
|
4238 |
18 Sep 22 |
peter |
78 |
|
4238 |
18 Sep 22 |
peter |
79 |
int main(int argc, char* argv[]) |
4238 |
18 Sep 22 |
peter |
80 |
{ |
4238 |
18 Sep 22 |
peter |
81 |
test::Suite suite(argc, argv); |
4238 |
18 Sep 22 |
peter |
82 |
|
4238 |
18 Sep 22 |
peter |
83 |
suite.out() << "test Newton\n"; |
4238 |
18 Sep 22 |
peter |
84 |
run_test<Newton>(suite); |
4238 |
18 Sep 22 |
peter |
85 |
suite.out() << "test Secant\n"; |
4238 |
18 Sep 22 |
peter |
86 |
run_test<Secant>(suite); |
4238 |
18 Sep 22 |
peter |
87 |
suite.out() << "test Steffenson\n"; |
4238 |
18 Sep 22 |
peter |
88 |
run_test<Steffenson>(suite); |
4238 |
18 Sep 22 |
peter |
89 |
return suite.return_value(); |
4238 |
18 Sep 22 |
peter |
90 |
} |