0

为了解决计算机视觉问题,我必须最小化非线性能量函数,并在 C++ 中实现它。虽然我没有找到一个库来帮助我使用特定的功能,但我有它的数学。那么从符号数学到 C++ 代码的最佳方式是什么?

示例:给定函数 g(x):=x^2 和 f(x):=x+2,假设我有兴趣将 f(g(x)) 转换为 C 代码;明显的 C 代码是 y=x^2+2; 但是对于包括雅可比等复杂的数学,它并不那么容易,翻译成页面和操作页面。

我已经尝试过 Matlab 并将它的转换模块转换为 C 代码,但代码远未优化(例如:相同的操作重复多次而不是重用结果)。

4

1 回答 1

2

存在可从 C++、C、Matlab、Fortran、(...) 调用的NLopt库,用于非线性优化。使用这个库的最小化过程的实现可能如下所示:

#include <nlopt.hpp>

nlopt::opt opt(nlopt::LD_MMA, 2);

std::vector<double> lb(2);
lb[0] = -HUGE_VAL; lb[1] = 0;
opt.set_lower_bounds(lb);

opt.set_min_objective(myfunc, NULL);

my_constraint_data data[2] = { {2,0}, {-1,1} };
opt.add_inequality_constraint(myconstraint, &data[0], 1e-8);
opt.add_inequality_constraint(myconstraint, &data[1], 1e-8);

opt.set_xtol_rel(1e-4);

std::vector<double> x(2);
x[0] = 1.234; x[1] = 5.678;
double minf;
nlopt::result result = opt.optimize(x, minf);
于 2013-11-22T12:01:45.677 回答