Python 如何用 numpy 在 Cython 中表示 inf 或 -inf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16050549/
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
How to represent inf or -inf in Cython with numpy?
提问by
I am building an array with cython element by element. I'd like to store the constant np.inf(or -1 * np.inf) in some entries. However, this will require the overhead of going back into Python to look up inf. Is there a libc.mathequivalent of this constant? Or some other value that could easily be used that's equivalent to (-1*np.inf)and can be used from Cython without overhead?
我正在逐个元素构建一个带有 cython 的数组。我想在某些条目中存储常量np.inf(或-1 * np.inf)。但是,这将需要返回到 Python 中查找inf. 有libc.math这个常数的等价物吗?或者其他一些可以轻松使用的值,相当于(-1*np.inf)并且可以在没有开销的情况下从 Cython 使用?
EDITexample, you have:
编辑示例,您有:
cdef double value = 0
for k in xrange(...):
# use -inf here -- how to avoid referring to np.inf and calling back to python?
value = -1 * np.inf
采纳答案by Cairnarvon
There's no literal for it, but floatcan parse it from a string:
它没有文字,但float可以从字符串中解析它:
>>> float('inf')
inf
>>> np.inf == float('inf')
True
Alternatively, math.hmay(almost certainly will) declare a macro that evaluates to inf, in which case you can just use that:
或者,math.h可以(几乎肯定会)声明一个计算为 inf 的宏,在这种情况下,您可以使用它:
cdef extern from "math.h":
float INFINITY
(There's no clean way to check if INFINITY is defined in pure Cython, so if you want to cover all your bases you'll need to get hacky. One way of doing it is to create a small C header, say fallbackinf.h:
(没有干净的方法来检查 INFINITY 是否是在纯 Cython 中定义的,所以如果你想覆盖所有的基础,你需要变得 hacky。一种方法是创建一个小的 C 头文件,比如fallbackinf.h:
#ifndef INFINITY
#define INFINITY 0
#endif
And then in your .pyx file:
然后在您的 .pyx 文件中:
cdef extern from "math.h":
float INFINITY
cdef extern from "fallbackinf.h":
pass
inf = INFINITY if INFINITY != 0 else float('inf')
(You can't assign to INFINITY, because it's an rvalue. You could do away with the ternary operator if you #defined INFINITY as 1.0/0.0 in your header, but that might raise SIGFPE, depending on your compiler.)
(您不能分配给 INFINITY,因为它是一个右值。如果您在标头中将 INFINITY 定义为 1.0/0.0,您可以取消三元运算符,但这可能会引发 SIGFPE,具体取决于您的编译器。)
This is definitely in the realm of cargo cult optimisation, though.)
不过,这绝对属于货物崇拜优化领域。)
回答by pv.
You can use Numpy's math library, see here for what's available:
您可以使用 Numpy 的数学库,请参阅此处了解可用内容:
cdef extern from "numpy/npy_math.h":
double inf "NPY_INFINITY"
When building the Cython extension module, you need to specify the correct include directory and library to link:
在构建 Cython 扩展模块时,需要指定正确的包含目录和库进行链接:
>>> from numpy.distutils.misc_util import get_info
>>> get_info('npymath')
{'define_macros': [],
'libraries': ['npymath', 'm'],
'library_dirs': ['/usr/lib/python2.7/dist-packages/numpy/core/lib'],
'include_dirs': ['/usr/lib/python2.7/dist-packages/numpy/core/include']}
The information obtained from that function can be passed onto Python distutils, or whatever build system you use.
从该函数获得的信息可以传递到 Python distutils 或您使用的任何构建系统。
回答by Tom
The recommended way of doing this in Cython is:
在 Cython 中执行此操作的推荐方法是:
from numpy.math cimport INFINITY
Note, that this is a "cimport" rather than a regular import. This is Cython's official wrapper around NumPy's npymath.
请注意,这是“cimport”而不是常规导入。这是 Cython 对 NumPy 的官方包装npymath。

