Pass function as parameter to integrate object using lambda
用于教育目的
我有一个函数集成,它以 std::function 作为参数。
1
2 3 4 5 6 7 8 9 |
double calculus::integralSimple(std::function<double(double)> fn, double begin, double end)
{ double integral = 0; for (long double i = begin; i < end; i += _step) { integral += fn(i) * _step; // _step defined in class } return integral; } |
目前我正在使用
从 main.cpp 调用此函数
1
2 3 |
calculus cl;
std::cout << cl.integralSimple(calculus::identity,0,1); std::cout << cl.integralSimple([](double x) { return x*x; }, 0, 1); |
其中 identity 是 calculus.h 中定义的静态函数,另一个使用 lambda 函数。
我想知道我是否可以使语法对用户更容易并更接近数学方式。
所以我更希望用户只需要输入:
1
2 |
std::cout << cl.integralSimple( x*x ,0,1); // Always take a function of this form
std::cout << cl.integralSimple( x*sin(x) – x*x ,0,1); |
有什么方法可以在 C 中实现这一点吗?
- Boost 的 Lambda 和 Phoenix 库可以做到这一点,但实际上,lambda 只package了表达式;他们不干涉它。
- 我没有将 intergalSimple 更改为 integralSimple,但您可能应该(五次,如果我数正确的话)。
- 我理解在编程中使用数学语言的愿望。但是我们确实需要接受编程语言和数学语言是不同的,比如英语和汉语和日语。我们确实需要接受以不太熟悉的形式表达相同的含义。
- @JonathanLeffler 改变了他们。我的错。
这正是 Boost.Lambda 的设计目的。语法如下所示:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream>
#include <vector> #include #include <cmath> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> const double PI =3.141592653589793238463; double func(double v) { return std::sin(v); } // to avoid having to int main() std::vector<double> v = {0, PI / 4, PI / 2, PI}; std::for_each(v.begin(), v.end(), |
这是否比 C 11 lambda 语法更好完全取决于您。
请注意,由于 C 14 和一些功能滥用,我们实际上也可以准确地编写您想要的表达式:
1
2 3 4 5 |
auto x = _1;
auto sin(decltype(_1) ) { |
有了它,我们可以做到:
1
2 3 |
std::for_each(v.begin(), v.end(),
std::cout << x * sin(x) – x * x << ‘\ ‘); |
这将打印出与原始示例完全相同的内容。只是……更神秘。
- 谢谢。我将尝试将其合并到我的代码中。如果你不介意我问,这种语法是否可以在任何其他语言中使用。我计划在 C 中实现这些函数,然后从 Python 中调用它。那么有什么我可以在 Python 中做的,直到允许用户简单地说集成(x*x)
- @nnrales 我确信用 Python 编写这样的库是可能的。我不知道它是否存在。
来源:https://www.codenong.com/29551643/