调用将MATLAB函数句柄作为Matlab的参数的python函数 [英] Call a python function that takes in a MATLAB function handle as argument from matlab
问题描述
我正在尝试使用MATLAB环境中的scipy.optimize.differential_evolution
(python).
I'm trying to use the scipy.optimize.differential_evolution
(python) from MATLAB environment.
到目前为止,我已经可以调用differential_evolution
函数.唯一的问题是它显然无法接受MATLAB函数句柄作为参数.
So far, I am able to call the differential_evolution
function. The only problem is that it apprently cannot receive MATLAB function handles as an argument.
我收到以下错误:
使用py.scipy.optimize.differential_evolution处理MATLAB时出错 不支持函数'@(x)x(1).^ 2. * x(2).^ 2'.使用手柄 Python函数.
Error using py.scipy.optimize.differential_evolution Handle to MATLAB function '@(x)x(1).^2.*x(2).^2' is not supported. Use a handle to a Python function.
是否有一些简洁的方法或函数可以将MATLAB函数句柄转换"为python函数,以便我可以使用scipy中的简洁优化函数?
Is there some neat way or function to "convert" a MATLAB function handle into a python function, so that I could use the neat optimization function from scipy?
推荐答案
我不确定,但我怀疑您想做的事无法完成.
I'm not sure but I suspect that what you want can't be done.
首先,如果您将其传递给MATLAB匿名函数,则python接口中的任何内容都会给您该错误:
Firstly, anything in the python interface will give you that error if you pass it a MATLAB anonymous function:
>> py.print(@(x) x)
Error using py.print
Handle to MATLAB function '@(x)x' is not supported. Use a handle to a Python function.
因此,似乎引擎意味着它,您必须放弃MATLAB函数.我们可以尝试将您的匿名函数转换为python函数,但是我不知道这是怎么可能的.我怀疑这根本不可能,因为最直接,最简单的自定义函数是lambda.但是py.lambda
不存在,我们甚至不能通过调用py.eval
来愚弄MATLAB引擎:
So it seems the engine means it, and you have to give up MATLAB functions. We could try converting your anonymous function to a python one...but I couldn't figure out how this was possible. I suspect it isn't possible at all, since the most straightforward and simplest custom function would be a lambda. But py.lambda
doesn't exist and we can't even fool the MATLAB engine by calling py.eval
:
>> py.lambda
Undefined variable "py" or class "py.lambda".
>> py.eval('lambda x: x')
Python Error: SystemError: frame does not exist
>> py.eval('def foo(x): return x')
Python Error: SystemError: frame does not exist
这非常有力地表明,一旦有了MATLAB匿名函数,就无法将其转换为python函数.
This very strongly suggests that once you have a MATLAB anonymous function you can't turn it into a python one.
直接的问题是:您真的需要MATLAB匿名函数吗?您也可以使用适当的python函数(或lambda),并将可能的其他参数作为args
传递给基础scipy.optimize
函数.您可以在python文件中定义自定义函数,然后在MATLAB中import
定义自定义函数,并使用相应的函数句柄.这将是您摆脱困境的直接方法.
The straightforward question is: do you really need a MATLAB anonymous function? You could just as well use a proper python function (or lambda) and pass possible other arguments to the underlying scipy.optimize
function as args
. You could define your custom function in a python file and import
that from MATLAB and use the corresponding function handle. This would be the straightforward way out from your situation.
还请注意,尽管缺少MATLAB函数处理支持,但部分中详细介绍了受支持的数据类型:
Also note that while the lack of MATLAB function handle support is not explicitly mentioned among the limitations, the section of the documentation detailing supported data types makes this remark:
MATLAB输入参数类型-仅标量值
函数句柄@py. module.function ,仅适用于Python函数
MATLAB Input Argument Type — Scalar Values Only
function handle @py.module.function, to Python functions only
Python函数的区别与以下事实相符:即使最简单的Python函数也拒绝接受MATLAB函数句柄.
The distinction for Python functions is in line with the fact that even the simplest Python functions refuse to accept MATLAB function handles.
这篇关于调用将MATLAB函数句柄作为Matlab的参数的python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!