调用一个 python 函数,该函数将 MATLAB 函数句柄作为来自 matlab 的参数 [英] Call a python function that takes in a MATLAB function handle as argument from matlab

查看:28
本文介绍了调用一个 python 函数,该函数将 MATLAB 函数句柄作为来自 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 suspect that what you want can't directly 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.

This very strongly suggests that once you have a MATLAB anonymous function you can't pass it to python.

还要注意,虽然缺少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 匿名函数转换为 python 函数,但我必须事先说明它很乱,如果我是您,我会避免这样做.由于 lambdas 不是由 MATLAB 的 Python API 直接公开的:

We could try converting your MATLAB anonymous function to a python one, but I have to note upfront that it's messy and I'd avoid doing this if I were you. Since lambdas aren't directly exposed by MATLAB's Python API:

>> py.lambda
Unable to resolve the name py.lambda.

我们不得不求助于使用 python lambda 调用 Python 的 eval(因为 exec 似乎也没有暴露):

we have to resort to calling Python's eval using a python lambda (because exec also doesn't seem to be exposed):

>> py_f = py.eval('lambda x: x**2', py.dict());
>> py_f(3)

ans =

     9

(感谢 @yuyichao 指出缺失的 globals 需要传递的字典.)

(Kudos to @yuyichao for fixing this snippet of mine by pointing out the missing globals dict that needs to be passed.)

然而,一个简单的问题是:你真的需要一个 MATLAB 匿名函数吗?您也可以使用适当的 Python 函数(或 lambda)并将其他可能的参数作为 args 传递给底层 scipy.optimize 函数.您可以在 python 文件中定义自定义函数,然后从 MATLAB 中import 并使用相应的函数句柄.这可能是最直接的方式.

However, a 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 probably be the straightforward way.

这篇关于调用一个 python 函数,该函数将 MATLAB 函数句柄作为来自 matlab 的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆