java 在 Jython 中使用 NumPy 和 Cpython
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3097466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using NumPy and Cpython with Jython
提问by gappy
I mustuse a commercial Java library, and would like to do it from Python. Jython is robust and I am fine with it being a few dot releases behind. However, I would liketo use NumPy as well, which obviously does not work with Jython. Options like CPypeand Java numeric libraries are unappealing. The former is essentially dead. The latter are mostly immature and lack the ease of use and wide acceptance of NumPy. My question is: How can one have Jython and Python code interoperate? It would be acceptable for me to call Jython from Cpython or the other way around.
我必须使用商业 Java 库,并希望从 Python 中实现。Jython 很健壮,我觉得它落后了几个点版本。但是,我也想使用 NumPy,这显然不适用于 Jython。像CPype和 Java 数字库这样的选项没有吸引力。前者基本上已经死了。后者大多不成熟,缺乏 NumPy 的易用性和广泛接受性。我的问题是:如何让 Jython 和 Python 代码互操作?从 Cpython 或其他方式调用 Jython 对我来说是可以接受的。
回答by Alex Martelli
It's ironic, considering that Jython and Numeric (NumPy's ancestor) were initiated by the same developer (Jim Hugunin, who then moved on to also initiate IronPython and now holds some kind of senior architect position at Microsoft, working on all kind of dynamic languages support for .NET and Silverlight), that there's no really goodway to use numpy in Jython. The closest thing to that, which I know of, is the "jnumerical" project -- the (scarce) docs are on sourceforge, but the updated sources are on bitbucket.
具有讽刺意味的是,考虑到 Jython 和 Numeric(NumPy 的祖先)是由同一个开发人员发起的(Jim Hugunin,然后他还发起了 IronPython,现在在微软担任某种高级架构师职位,致力于各种动态语言支持对于 .NET 和 Silverlight),在 Jython 中没有使用 numpy 的好方法。我所知道的最接近的是“jnumerical”项目——(稀缺的)文档在sourceforge 上,但更新的源在bitbucket 上。
"Numeric Python", what jnumerical implements, is not as slick and streamlined as its numpy descendant, but it has about the same functionality and shares a lot of the concepts and philosophy, so maybe you could find it usable -- worth checking out, at least.
jnumeric 实现的“Numeric Python”不像它的 numpy 后代那样圆滑和流线型,但它具有大致相同的功能并共享许多概念和哲学,所以也许你会发现它可用——值得一试,至少。
回答by Jim Baker
Consider using execnet, which allows you to combine the strengths of both Jython and CPython, including current NumPy. The disadvantage here is that you will have to pay for the cost of serializing/deserializing objects between the two interpreters in two different process spaces. (You can avoid network overhead by using its support for subprocess.) But such a combination may work well, given that you're considering JPype, which would have similar (and probably higher) overhead. Just ensure you've partitioned the work appropriately.
考虑使用execnet,它允许您结合 Jython 和 CPython 的优势,包括当前的 NumPy。这里的缺点是您必须支付在两个不同进程空间中的两个解释器之间序列化/反序列化对象的成本。(您可以通过使用它对子进程的支持来避免网络开销。)但这种组合可能会很好地工作,因为您正在考虑 JPype,它具有类似(并且可能更高)的开销。只需确保您对工作进行了适当的分区。
The Jython developers (and I'm one of them) are looking at supporting NumPy in the future, via support of the C Extension API, but this is very much preliminary planning indeed.
Jython 开发人员(我就是其中之一)正在考虑在未来通过支持 C 扩展 API 来支持 NumPy,但这确实是非常初步的规划。
回答by eldad-a
Disclaimer: Have not had persnal experience with it yet
免责声明:还没有亲身体验过
Seems like JyNI – Jython Native Interfaceis the way to go.
看起来像JyNI – Jython Native Interface是要走的路。
There's also a newer question postedwhich may have newer alternatives.
还有一个较新的问题发布,可能有更新的替代方案。
回答by roman
I look very much formard to the Jython C Extension API! That would be awesome!
我非常看重 Jython C 扩展 API!那将是真棒!
Until, that point, I think you have two alternatives:
在那之前,我认为您有两种选择:
http://jepp.sourceforge.net/for embedding python in java, it has a nice console. The disadvatage, for me a too big disadvatage, is that it needs to be compiled against your own python. And with the python upgrade, you have to recompile (I don't want to compile python, in order to compile and use the extension - it is also not possible, especially if the code should be executed on different machines, on grid for example)
http://lucene.apache.org/pylucene/jcc/- this is used for lucene, and for many other projects. I personally use it to wrap GATE NLP engine and also solr. To make that available to Python. Jcc is much faster than the (dead) JPype, probably because some data structures (like lists) are optimized and also because it is interfacing python<->java via C++ extension (according to this: http://www.slideshare.net/onyame/mixing-python-and-javapage 30) I have tried moving 6mil of integers in the list between python and java, JPype was orders of magnitude slower (but i don't remember the numbers)
http://jepp.sourceforge.net/用于在 java 中嵌入 python,它有一个很好的控制台。缺点,对我来说一个太大的缺点,是它需要针对你自己的python进行编译。并且随着python升级,你必须重新编译(我不想编译python,为了编译和使用扩展——这也是不可能的,特别是如果代码应该在不同的机器上执行,例如在网格上)
http://lucene.apache.org/pylucene/jcc/- 这用于 lucene 和许多其他项目。我个人用它来包装 GATE NLP 引擎和 solr。使其可用于 Python。Jcc 比(死的)JPype 快得多,可能是因为一些数据结构(如列表)被优化了,也因为它通过 C++ 扩展接口连接 python<->java(根据这个:http: //www.slideshare.net /onyame/mixing-python-and-java第 30 页)我尝试在 python 和 java 之间移动列表中的 600 万个整数,JPype 慢了几个数量级(但我不记得数字了)
However, using Jcc, you can wrap only public methods, and sometimes it is tricky, especially if that method is receiving or returning certain java objects (in short, JCC must compile wrappers also for the passed-in objects, otherwise all the methods using/returning such methods are not accessible). So unless you need to distribute your code, you are better of with JEPP.
但是,使用 Jcc,您只能包装公共方法,有时这很棘手,特别是如果该方法正在接收或返回某些 java 对象(简而言之,JCC 也必须为传入的对象编译包装器,否则所有方法都使用/returning 此类方法不可访问)。因此,除非您需要分发代码,否则最好使用 JEPP。
回答by woodz
If you stick to vector and matrix maths, I suggest to have a look onto vectorz. It is a pure Java implementation and shall be 100% usable from within jython. I still didn't try it, but will soon, since I have the same necessity in finding a numpy alternative.
如果您坚持使用向量和矩阵数学,我建议您查看vectorz。它是一个纯 Java 实现,在 jython 中应该是 100% 可用的。我仍然没有尝试,但很快就会尝试,因为我同样需要寻找一个 numpy 替代方案。

