如何从 python 访问 matlab/octave 模块?

发布于 2024-11-09 23:00:05 字数 570 浏览 2 评论 0原文

我正在寻找一种从 python 访问 matlab 模块的方法。我目前的情况是这样的:

  • 我有一个 python 代码,它通过调用 Lapack 例程来进行数值计算,同时内存被分配为 ctypes 并作为指针传递给 Lapack 例程。
  • 我还有一个与 Octave 兼容的 matlab 模块,它可以执行一些我想使用的数学技巧。

我现在的问题是:
有什么有效的方法可以将所有主要工作保留在 python 中,同时利用 matlab/octave 模块提供的可能性。如果我的 ctype 数组不必转换为其他对象即可运行八度音程,那就太好了。然而,我可以看到最后一点很难实现。

我目前的研究向我展示了两种可能的选择:

  1. Pytave:然而,这个包似乎是预阿尔法?!
  2. 走这条崎岖的路:ctypes -> *.mat 文件(通过 numpy)->八度 -> *.mat 文件 -> ctypes(通过 numpy)

I am looking for a way to access a matlab module from python. My current situation is this:

  • I have a python code that does numerical computations by calling Lapack routines while the memory is allocated as ctypes and passed as pointers to the Lapack routines.
  • I also have a matlab module, which is compatible with octave, that does some mathematical tricks I want to use.

My question now is this:
What is an efficient way to keep all the main work in python while at the same time exploit the possibilities that matlab/octave modules offer. Also it would be kind of nice, if my ctype arrays do not have to be converted into some other object in order to run octave. However, I can see that that last point is hard to accomplish.

My current research shows me two possible options:

  1. Pytave: However it seems that this packages is kind of pre alpha?!
  2. Go this humpy road: ctypes -> *.mat file (via numpy) -> octave -> *.mat file -> ctypes (via numpy)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-11-16 23:00:05

您可以使用 oct2py,IIUC 是由其作者启动的,因为 pytave 无法在 win32 上运行。它通过其 octavemagic 扩展 在 IPython 中成功使用,我可以说它很容易单独使用,代码得到维护(我报告了一点 Unicode 错误,作者在一天内修复了它)并且运行良好。大多数时候很简单:

>>> from oct2py import octave
>>> octave.run("cos(pi / 3)")
'ans =  0.50000'
>>> octave.call("foo", a, b)  # Function in foo.m

有关更多示例,您可以查看这篇博客文章< /a>.

You can use oct2py, which IIUC was started by its author because pytave didn't work on win32. It is successfully used in IPython through its octavemagic extension and I can tell it is easy to use on its own, the code is maintained (I reported a little Unicode bug and the author fixed it in a day) and works well. Most of the times is as simple as:

>>> from oct2py import octave
>>> octave.run("cos(pi / 3)")
'ans =  0.50000'
>>> octave.call("foo", a, b)  # Function in foo.m

For further examples you can check this blog article.

一生独一 2024-11-16 23:00:05

您是否考虑过使用 OMPC http://ompc.juricap.com/ ?当我不想重写一些数值线性代数例程时,我使用它取得了巨大的成功。我可以想象,Matlab 命令越深奥,翻译起来就越困难……但这可能值得一试。最后,您需要将 Matlab 代码转换为 Python,因为它将成为速度和性能的瓶颈。将 Matlab 代码保留为 Matlab 格式的唯一原因是,如果将其全部翻译起来会产生巨大的前期成本,OMPC 应该在一定程度上减轻这一成本。否则,为了完全摆脱 Matlab/Octave 的依赖而付出的前期成本几乎总是值得的。

Have you considered using OMPC, http://ompc.juricap.com/ ? I have used it with great success when not wishing to re-write some numerical linear algebra routines. I can imagine that the more esoteric the Matlab commands, the harder it would be to translate... but it might be worth a try. In the end, you're going to want to convert your Matlab code to Python because it will be a bottleneck on speed and performance. The only reason to leave the Matlab code in Matlab format is if it would be an enormous up-front cost to translate it all, which OMPC should mitigate somewhat. Otherwise, it's almost always worth that up-front cost to completely rid yourself of Matlab/Octave dependence.

清风无影 2024-11-16 23:00:05

我在让 OMPC 工作时遇到了一些麻烦,因为 (I) md5 模块已被弃用,(II) Python 2.6 及更高版本不再接受 Object.__new__() 或 Object.__init__() 的参数,以及 (III) byteplay.py脚本需要更新。

为了解决问题 (I),我更改了 ompc/ 目录中的 yacc.py 的第 74 行。该行导入 md5 等。我删除了 md5 模块并添加了以下行:

from hashlib import md5

稍后在 yacc.py 脚本中的第 1160 行,我将其更改

Signature = md5.new()

为以下内容:

Signature = md5()

要运行 ompcply.py 生成的代码,请将“from ompc import *”添加到开头的文件,然后使用早期版本的 Python 运行它,如下所示:

$ python2.5 ompcply.py script.m > newscript.pym
$ python2.5 newscript.pym

使用高于 2.5 的 Python 版本将给出以下错误:

/home/connor/downloads/OMPC/ompclib/ompclib_numpy.py:66: DeprecationWarning: object.__new__() takes no parameters
  a = super(mvar, cls).__new__(cls, *args, **kwargs)

为了解决问题 (III),我在 google 上搜索了 byteplay,并将现有脚本替换为较新的版本。

I had some trouble getting OMPC to work because (I) the md5 module is deprecated, (II) Python 2.6 and later no longer accept arguments for Object.__new__() or Object.__init__(), and (III) the byteplay.py script needed to be updated.

To solve issue (I), I changed line 74 of yacc.py found in the ompc/ directory. This line imports md5 among other things. I deleted the md5 module and added the line below:

from hashlib import md5

Later in the yacc.py script, at line 1160, I changed,

Signature = md5.new()

to the following,

Signature = md5()

To run the code generated by ompcply.py, add 'from ompc import *' to the beginning of the file and then run it with an earlier version of Python, as:

$ python2.5 ompcply.py script.m > newscript.pym
$ python2.5 newscript.pym

Using a version of Python later than 2.5 will give you the following error:

/home/connor/downloads/OMPC/ompclib/ompclib_numpy.py:66: DeprecationWarning: object.__new__() takes no parameters
  a = super(mvar, cls).__new__(cls, *args, **kwargs)

To solve issue (III) I googled byteplay, and replaced the existing script with the newer version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文