在 Julia 中定义回调函数或函数句柄的标准方法是什么?
假设我定义
function myFun(a, b, c, d)
a - 3* b - c * d # The return value
end
我的目标是修复 b = 1、c = 2、d = 3,并将 myFun 作为 a 的函数传递。就像是:
newFun2(x) = myFun(x, 1 ,2, 3)
myReceiver(myFun2)
在 Julia 中定义回调函数或函数句柄的标准方法是什么?
假设我定义
function myFun(a, b, c, d)
a - 3* b - c * d # The return value
end
我的目标是修复 b = 1、c = 2、d = 3,并将 myFun 作为 a 的函数传递。就像是:
newFun2(x) = myFun(x, 1 ,2, 3)
myReceiver(myFun2)
根据文档,目标函数NLOpt
的形式应为:
function f(x::Vector, grad::Vector):
if length(grad) > 0:
...set grad to gradient, in-place...
return ...value of f(x)...
end
因此,代码需要看起来像:
function myFun(a, b, c, d, grad)
a - 3* b - c * d # The return value
end
newFun2(x, grad) = myFun(x, 1 , 2, 3, grad)
myFun()
grad
如果要成功使用使用myFun()
写入所需的导数信息的优化算法,则必须计算向量的值并返回目标函数值grad
。