xquad.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xerror.h"
4 #include <fstream>
5 #include <gsl/gsl_integration.h>
6 
7 namespace nmx::gsl {
8 
14 template<class T>
16 {
17 protected:
18  T *_workspace = nullptr;
19  gsl_function _gslFunction;
20  double _result, _error;
22 
28  template<class FN>
29  BaseIntegral(FN &fn)
30  : _gslFunction{ nullptr, nullptr }
31  , _result(0)
32  , _error(0) {
33  // Definition der gsl-Funktion. Es werden das Funktionsargument
34  // und ein zusätzlicher Parameter übergeben
35  _gslFunction.function = [](double x, void *params) {
36  // der zusätzliche Parameter enthält einen Zeiger auf
37  // die Funktion fn...
38  FN *_obj = static_cast<FN *>(params);
39  // ihr wird hier x übergeben, um den Funktionswert zu
40  //berechnen
41  return (*_obj)(x);
42  };
43  //hier wird fn registriert
44  _gslFunction.params = static_cast<void *>(&fn);
45  }
46 
47 public:
54  inline void show_results(std::ofstream &sout, double expected) {
55  sout << "result = " << result() << "\n";
56  sout << "exact result = " << expected << "\n";
57  sout << "estimated error= " << error() << "\n";
58  sout << "actual error = " << abs(result() - expected) << "\n";
59  sout << "intervals = " << intervals() << "\n";
60  }
61 
66  inline double result() const { return _result; }
67 
72  inline double error() const { return _error; }
73 
78  inline size_t intervals() const { return _workspace->size; }
79 }; //BaseIntegral
80 
86 class Integral : public BaseIntegral<gsl_integration_workspace>
87 {
88 public:
95  template<class FN>
96  inline Integral(FN &fn, size_t size = 1000)
97  : BaseIntegral(fn) {
98  _workspace = gsl_integration_workspace_alloc(size);
99  }
100 
105  Integral(const Integral &i) = delete;
106 
112  Integral &operator=(const Integral &i) = delete;
113 
118  if (_workspace != nullptr) {
119  gsl_integration_workspace_free(_workspace);
120  }
121  }
122 
132  inline auto qng(double xmin, double xmax, double epsabs = 1e-9, double epsrel = 1e-9) {
133  size_t nofFunEvals;
134  // clang-format off
135  _errorCode = gsl_integration_qng(&_gslFunction, xmin, xmax,
136  epsabs,epsrel,&_result,
137  &_error,&nofFunEvals);
138  // clang-format on
139  return std::make_pair(_errorCode, nofFunEvals);
140  }
141 
152  inline int qag(double xmin, double xmax, int key, double epsabs = 1e-9, double epsrel = 1e-9) {
153  Check::error_if_not(nmx_msg, key >= 1 && key <= 6);
154  // clang-format off
155  _errorCode = gsl_integration_qag(&_gslFunction, xmin, xmax,
156  epsabs, epsrel, _workspace->limit,
157  key, _workspace, &_result, &_error);
158  // clang-format on
159  return _errorCode;
160  }
161 
171  inline int qags(double xmin, double xmax, double epsabs = 1e-9, double epsrel = 1e-9) {
172  // clang-format off
173  _errorCode = gsl_integration_qags(&_gslFunction, xmin, xmax,
174  epsabs, epsrel,_workspace->limit,
176  // clang-format on
177  return _errorCode;
178  }
179 
187  inline int qagi(double epsabs = 1e-9, double epsrel = 1e-9) {
188  // clang-format off
189  _errorCode = gsl_integration_qagi(&_gslFunction, epsabs,
190  epsrel,_workspace->limit,_workspace,
191  &_result, &_error);
192  // clang-format on
193  return _errorCode;
194  }
195 
204  inline int qagiu(double xmin, double epsabs = 0, double epsrel = 1e-9) {
205  // clang-format off
206  _errorCode = gsl_integration_qagiu(&_gslFunction, xmin,
207  epsabs, epsrel,
208  _workspace->limit, _workspace,
209  &_result, &_error);
210  // clang-format on
211  return _errorCode;
212  }
213 
222  inline int qagil(double xmax, double epsabs = 1e-9, double epsrel = 1e-9) {
223  // clang-format off
224  _errorCode = gsl_integration_qagil(&_gslFunction, xmax,
225  epsabs, epsrel,
226  _workspace->limit,_workspace,
227  &_result,&_error);
228  // clang-format on
229  return _errorCode;
230  }
231 }; //Integral
232 
238 class CQUADIntegral : public BaseIntegral<gsl_integration_cquad_workspace>
239 {
240 private:
241  size_t _nFunctionEvals; //Anzahl der Funktionsaufrufe
242 public:
248  template<class FN>
249  CQUADIntegral(FN &fn, size_t dim = 100)
250  : BaseIntegral(fn) {
251  _workspace = gsl_integration_cquad_workspace_alloc(dim);
252  //Überprüfung der minimalen Anzahl der Intervalle
253  Check::error_if_not(nmx_msg, dim >= 3);
254  }
255 
260  if (_workspace != nullptr) {
261  gsl_integration_cquad_workspace_free(_workspace);
262  }
263  }
264 
274  int apply(double xmin, double xmax, double epsabs = 1e-9, double epsrel = 1e-9) {
275  // clang-format off
276  _errorCode = gsl_integration_cquad(&_gslFunction, xmin, xmax,
277  epsabs, epsrel,
279  &_error, &_nFunctionEvals);
280  // clang-format on
281  return _errorCode;
282  }
283 }; //CQUADIntegral
284 
285 } // namespace nmx::gsl
int qagiu(double xmin, double epsabs=0, double epsrel=1e-9)
qagiu numerische Integration mit automatischer Schrittweitenanpassung von xmin bis + Unendlich ...
Definition: xquad.h:204
int qagil(double xmax, double epsabs=1e-9, double epsrel=1e-9)
qagil numerische Integration mit automatischer Schrittweitenanpassung von - Unendlich bis xmax ...
Definition: xquad.h:222
The BaseIntegral class Basisklasse zur numerischen Integration.
Definition: xquad.h:15
CQUADIntegral(FN &fn, size_t dim=100)
CQUADIntegral Konstruktor.
Definition: xquad.h:249
The CQUADIntegral class adaptive numerische Integration geeignet für Integranden mit Singularitäten o...
Definition: xquad.h:238
auto qng(double xmin, double xmax, double epsabs=1e-9, double epsrel=1e-9)
qng Integration von stetigen Funktionen keine automatische Schrittweitenanpassung ...
Definition: xquad.h:132
Integral(FN &fn, size_t size=1000)
Integral Konstruktor.
Definition: xquad.h:96
double error() const
error
Definition: xquad.h:72
#define nmx_msg
Definition: xerror.h:113
static void error_if_not(const std::string &s, bool arg)
error_if_not Fehler wenn Bedingung nicht erfüllt ist
Definition: xerror.h:63
gsl_function _gslFunction
Definition: xquad.h:19
int qags(double xmin, double xmax, double epsabs=1e-9, double epsrel=1e-9)
qags numerische Integration mit automatischer Schrittweitenanpassung von singulären Funktionen ...
Definition: xquad.h:171
void show_results(std::ofstream &sout, double expected)
show_results einfache Ausgabe des Ergebnisses mit Zusatzinformationen
Definition: xquad.h:54
BaseIntegral(FN &fn)
BaseIntegral Konstruktor.
Definition: xquad.h:29
The Integral class erste Varianten der gsl-Klasse zur numerischen Integration. Der Template-Parameter...
Definition: xquad.h:86
int apply(double xmin, double xmax, double epsabs=1e-9, double epsrel=1e-9)
apply Ausführung der numerischen Integration
Definition: xquad.h:274
int qagi(double epsabs=1e-9, double epsrel=1e-9)
qagi numerische Integration mit automatischer Schrittweitenanpassung von - Unendlich bis + Unendlich ...
Definition: xquad.h:187
int qag(double xmin, double xmax, int key, double epsabs=1e-9, double epsrel=1e-9)
qag numerische Integration mit automatischer Schrittweitenanpassung (Gauß-Kronrod) ...
Definition: xquad.h:152
double result() const
result
Definition: xquad.h:66
size_t intervals() const
intervals
Definition: xquad.h:78