[英]Fast n-dimensional sparse array in Python / Cython
I have an application that involves large n-dimensional arrays which are very sparse. 我有一个涉及非常稀疏的大型n维数组的应用程序。
scipy.sparse
has a useful 'vectorized getting and setting' feature, so that Cython can be used to populate a sparse matrix quickly. scipy.sparse
有一个有用的'矢量化获取和设置'功能,因此可以使用Cython快速填充稀疏矩阵。
Of course the scipy package can't handle n-dimensions. 当然,scipy包不能处理n维。 There are two packages I have found that do n-dimensional sparse arrays in python
sparray
and ndsparse
. 我发现有两个包在python
sparray
和ndsparse
中执行n维稀疏数组。 However it seems neither has the vectorized getting and setting feature. 然而,它似乎既没有矢量化的获取和设置功能。
So I need either: 所以我需要:
For my purpose I think mapping the n-dimension coordinates back to 1 or two dimensions could work. 为了我的目的,我认为将n维坐标映射回1或2维可能有效。 What would be better though is to have a dict equivalent that i can access fast inside a Cython loop.
更好的是有一个dict等价物,我可以在Cython循环内快速访问。 I assume this rules out the python
dict
. 我假设这排除了python
dict
。
Wondering if someone could give me an example of how to use the c++ map object from within Cython? 想知道是否有人可以给我一个如何在Cython中使用c ++地图对象的例子?
If you decide to go with the C dict option, you can use the C++ STL's std::map. 如果您决定使用C dict选项,则可以使用C ++ STL的std :: map。 It's unlikely that you'll find faster or more robust native code that implements a dictionary/map.
您不太可能找到更快或更强大的本机代码来实现字典/地图。
cppmap.pyx: cppmap.pyx:
# distutils: language = c++
cdef extern from "<map>" namespace "std":
cdef cppclass mymap "std::map<int, float>":
mymap()
float& operator[] (const int& k)
cdef mymap m = mymap()
cdef int i
cdef float value
for i in range(100):
value = 3.0 * i**2
m[i] = value
print m[10]
setup.py: setup.py:
from distutils.core import setup
from Cython.Build import cythonize
setup(name = "cppmapapp"
ext_modules = cythonize('*.pyx'))
Command line: 命令行:
$ python setup.py build
$ cd build/lib.macosx-10.5-x86_64-2.7
$ python -c 'import cppmap'
300.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.