1
0
Fork 0
mirror of https://github.com/gwm17/catima.git synced 2024-11-29 21:48:51 -05:00
catima/integrator.cpp

50 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
//#ifdef GSL_INTEGRATION
2017-07-25 12:19:11 -04:00
#include "gsl/gsl_integration.h"
#include "gsl/gsl_errno.h"
2018-10-21 16:08:16 -04:00
//#endif
2017-07-25 12:19:11 -04:00
namespace catima{
2017-12-14 09:07:54 -05:00
integrator_type integrator;
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
}