1
0
Fork 0
mirror of https://github.com/gwm17/catima.git synced 2024-11-26 20:18:51 -05:00
catima/integrator.cpp

52 lines
1.3 KiB
C++
Raw Normal View History

2017-07-25 12:19:11 -04:00
#include "integrator.h"
2018-10-21 16:08:16 -04:00
2019-10-08 13:51:45 -04:00
#ifdef GSL_INTEGRATION
2017-07-25 12:19:11 -04:00
#include "gsl/gsl_integration.h"
#include "gsl/gsl_errno.h"
2019-10-08 13:51:45 -04:00
#endif
2017-07-25 12:19:11 -04:00
namespace catima{
2017-12-14 09:07:54 -05:00
integrator_type integrator;
2020-08-04 11:35:33 -04:00
integrator_adaptive_type integrator_adaptive;
2018-10-21 16:08:16 -04:00
#ifdef GSL_INTEGRATION
2017-07-25 12:19:11 -04:00
double funcwrapper3(double x, void *_c){
std::function<double(double)> *f = (std::function<double(double)> *)_c;
return (*f)(x);
}
2017-12-14 09:07:54 -05:00
IntegratorGSL::IntegratorGSL(bool adapt):adaptive(adapt){
2017-12-14 10:14:56 -05:00
gsl_set_error_handler_off();
2017-12-14 09:07:54 -05:00
if(adaptive){
w=gsl_integration_workspace_alloc(100);
}
2017-07-25 12:19:11 -04:00
};
2017-12-14 09:07:54 -05:00
2017-07-25 12:19:11 -04:00
IntegratorGSL::~IntegratorGSL(){
2017-12-14 09:07:54 -05:00
if(adaptive){
gsl_integration_workspace_free(w);
}
2017-07-25 12:19:11 -04:00
};
2017-12-14 09:07:54 -05:00
double IntegratorGSL::integrate(std::function<double(double)> f, double _min, double _max, double prec){
2017-07-25 12:19:11 -04:00
gsl_function F;
F.function = funcwrapper3;
F.params = &f;
min = _min;
max = _max;
size_t num;
if(adaptive){
#ifdef USE_THREADS
std::lock_guard<std::mutex> lock(integration_mutex);
#endif
2017-12-14 09:07:54 -05:00
gsl_integration_qag(&F,_min,_max,1e-6,prec,100,6,w,&result,&error);
2017-07-25 12:19:11 -04:00
}
2017-12-14 09:07:54 -05:00
else{
gsl_integration_qng(&F,_min,_max,1e-6,prec,&result,&error,&num);
}
2017-07-25 12:19:11 -04:00
return result;
};
2018-10-21 16:08:16 -04:00
#endif
2017-07-25 12:19:11 -04:00
}