You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

37 lines
1.1 KiB

#pragma once
#include "GL_integrator.hpp"
#include "TS_integrator.hpp"
// 高斯-勒让德一维积分:积分区间 [a, b]
template<typename Func>
double gauss_integrate_1D(double a, double b, Func&& func, int q) {
double sum = 0.0;
for (int i = 0; i < q; ++i) {
double x = GLIntegrator<double>::x(q, i, a, b);
double w = GLIntegrator<double>::w(q, i, a, b);
sum += w * func(x);
}
return sum;
}
// tanh-sinh 一维积分:积分区间 [a, b]
template<typename Func>
double tanh_sinh_integrate_1D(double a, double b, Func&& func, int q) {
double result = 0.0;
for (int i = 0; i < q; ++i) {
double x = TSIntegrator<double>::x(q, i, a, b);
double w = TSIntegrator<double>::w(q, i, a, b);
result += w * func(x);
}
return result;
}
template<typename Func>
double integrate_1D(double a, double b, Func&& func, int q, bool use_tanh_sinh = false) {
if (use_tanh_sinh) {
return tanh_sinh_integrate_1D(a, b, std::forward<Func>(func), q);
} else {
return gauss_integrate_1D(a, b, std::forward<Func>(func), q);
}
}