i*****n 发帖数: 36 | 1 我想在c++ 里做optimization, 在网上找到如下的struct template, 只要自己写一个
函数
funcc 作为objective function就可以。 可是一直没有明白 class T 应该设成什么
。
比如说
double funcc(double a);
这时 class T 应写成什么呢? 多谢!
template
struct Funcd {
Doub EPS;
T &func;
Doub f;
Funcd(T &funcc) : EPS(1.0e-8), func(funcc) {}
Doub operator() (VecDoub_I &x)
{
return f=func(x);
}
void df(VecDoub_I &x, VecDoub_O &df)
{
Int n=x.size();
VecDoub xh=x;
Doub fold=f;
for (Int j= | t****t 发帖数: 6806 | 2 我google了一下好象是numerical recipe里抄来的, 不过你是不是理解不太对
这个struct Funcd似乎是你所需要的functor(你给它一个function, 它把你的function
包装成functor), 然后你把Funcd作为模板参数来instantiate另外的计算函数/类
【在 i*****n 的大作中提到】 : 我想在c++ 里做optimization, 在网上找到如下的struct template, 只要自己写一个 : 函数 : funcc 作为objective function就可以。 可是一直没有明白 class T 应该设成什么 : 。 : 比如说 : double funcc(double a); : 这时 class T 应写成什么呢? 多谢! : template : struct Funcd { : Doub EPS;
| r*********r 发帖数: 3195 | | i*****n 发帖数: 36 | 4 多谢, 可能我还是不太理解。 以下我尝试 min (x-1)^2 用dfpmin (是使用quasi
newton), 带入参数 Funcd(tryfun), 却无法compile, which
says "cannot convert from 'double (__cdecl *)(double &)' to 'Funcd'"
有解决的方法么?
#include
#include
#include
#include
#include
using namespace std;
typedef double Doub;
typedef int Int;
typedef vector VecDoub, VecDoub_I, VecDoub_O, VecDoub_IO;
template
void dfpmin(VecDoub_IO &p, const Doub gtol, Int &iter, Doub &f
【在 r*********r 的大作中提到】 : T 是 double (double)
| r*********r 发帖数: 3195 | 5 那么多 type matching 错误, 怎么可能编译得过去. | i*****n 发帖数: 36 | 6 除了 Funcd(tryfun)); type matching 外还有哪些问题呢? 多谢!
【在 r*********r 的大作中提到】 : 那么多 type matching 错误, 怎么可能编译得过去.
| i*****n 发帖数: 36 | 7 抱歉, 有些别的bug, 把 x的 type 从 double 改成 vector了
#include
#include
#include
#include
#include
using namespace std;
typedef double Doub;
typedef int Int;
typedef vector VecDoub, VecDoub_I, VecDoub_O, VecDoub_IO;
template
void dfpmin(VecDoub_IO &p, const Doub gtol, Int &iter, Doub &fret, T
&funcd)
{
const Int ITMAX=200;
const Doub EPS=numeric_limits::epsilon();
const Doub TOLX=4*EPS,STPMX=100.0;
Bool check;
【在 r*********r 的大作中提到】 : 那么多 type matching 错误, 怎么可能编译得过去.
|
|