我花了一些时间在互联网上寻找解决这个问题的方法,也许它就在外面,但我所看到的一切对我没有任何帮助。
我有个功能!
double integrand(double r, double phi, double theta)
我想把它和三维的一些给定的界限结合起来。我在互联网上发现了多行代码,这些代码实现了单变量定积分数值格式。我在想,“好吧,我将沿着一个又一个的维度进行整合”。
从算法上讲,我想做的是:
double firstIntegral(double r, double phi) {
double result = integrationFunction(integrand,lower_bound,upper_bound);
return result;
}
再做两次。这在Matlab这样的语言中很容易实现,在这里我可以在任何地方创建函数处理程序,但是我不知道如何在C++中实现它。我首先要定义一个函数,一些r和phi会计算任何θ的integrand(r,phi,theta),并使它在C++中仅是一个变量的函数,但我不知道怎么做。
,我如何使用一个-dimensional集成例程(或其他任何真正的.)来计算C++中三个变量函数的三重积分?
发布于 2017-07-06 06:44:42
对于笛卡尔坐标上的积分,这是一个非常缓慢和不精确的版本,应该与C++11一起工作。
它使用std::function和lambda来实现数值积分。没有采取任何步骤来优化这方面的工作。
基于模板的解决方案可能比这快得多(几个数量级),因为它可能允许编译器内联并简化一些代码。
#include<functional>
#include<iostream>
static double integrand(double /*x*/, double y, double /*z*/)
{
return y;
}
double integrate_1d(std::function<double(double)> const &func, double lower, double upper)
{
static const double increment = 0.001;
double integral = 0.0;
for(double x = lower; x < upper; x+=increment) {
integral += func(x) * increment;
}
return integral;
}
double integrate_2d(std::function<double(double, double)> const &func, double lower1, double upper1, double lower2, double upper2)
{
static const double increment = 0.001;
double integral = 0.0;
for(double x = lower2; x < upper2; x+=increment) {
auto func_x = [=](double y){ return func(x, y);};
integral += integrate_1d(func_x, lower1, upper1) * increment;
}
return integral;
}
double integrate_3d(std::function<double(double, double, double)> const &func,
double lower1, double upper1,
double lower2, double upper2,
double lower3, double upper3)
{
static const double increment = 0.001;
double integral = 0.0;
for(double x = lower3; x < upper3; x+=increment) {
auto func_x = [=](double y, double z){ return func(x, y, z);};
integral += integrate_2d(func_x, lower1, upper1, lower2, upper2) * increment;
}
return integral;
}
int main()
{
double integral = integrate_3d(integrand, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
std::cout << "Triple integral: " << integral << std::endl;
return 0;
}
发布于 2017-07-06 06:04:25
你可以使用函子
#include <iostream>
struct MyFunctorMultiply
{
double m_coeff;
MyFunctorMultiply(double coeff)
{
m_coeff = coeff;
}
double operator()(double value)
{
return m_coeff * value;
}
};
struct MyFunctorAdd
{
double m_a;
MyFunctorAdd(double a)
{
m_a = a;
}
double operator()(double value)
{
return m_a + value;
}
};
template<class t_functor>
double calculate(t_functor functor, double value, double other_param)
{
return functor(value) - other_param;
}
int main()
{
MyFunctorMultiply multiply2(2.);
MyFunctorAdd add3(3.);
double result_a = calculate(multiply2, 4, 1); // should obtain 4 * 2 - 1 = 7
double result_b = calculate(add3, 5, 6); // should obtain 5 + 3 - 6 = 2
std::cout << result_a << std::endl;
std::cout << result_b << std::endl;
}
发布于 2017-07-12 04:39:06
如果您只关心如何获得正确的原型来传递到集成函数,那么您可以很好地使用替代的数据传递机制,其中更简单的是使用全局变量。
假设集成顺序在theta
上,然后是phi
,然后是r
,则编写单个参数的三个函数:
It(theta)
根据theta
显式传递的参数以及全局phi
和r
计算integrand。
Ip(phi)
从显式传递的参数phi
和全局r
计算theta
的界限;它还将phi
参数复制到全局变量并调用integrationFunction(It, lower_t, upper_t)
。
Ir(r)
从显式传递的参数r
中计算phi
的边界;它还将r
参数复制到全局变量并调用integrationFunction(Ip, lower_p, upper_p)
。
现在,您可以调用integrationFunction(Ir, lower_r, upper_r)
了。
也可能是integrationFunction
支持一个"context
“参数,您可以在其中存储所需的内容。
https://stackoverflow.com/questions/44948663
复制