简体   繁体   English

在python中存储大对称稀疏矩阵的最有效方法

[英]The most efficient way to store large symmetric sparse matrices in python

I was working on drafting/testing a technique I devised for solving differential equations for speed and efficiency. 我正在起草/测试我为解决速度和效率的微分方程而设计的技术。

It would require a storing, manipulating, resizing, and (at some point) probably diagonalizing very large sparse matrices. 它需要存储,操纵,调整大小,并且(在某些时候)可能对非常大的稀疏矩阵进行对角化。 I would like to be able to have rows consisting of zeros and a few (say <5) ones, and add them a few at a time (on the order of the number of cpus being used.) 我希望能够有由零和一些(比如<5)组成的行,并一次添加几个(按照使用的cpus数量的顺序)。

I thought it would be useful to have gpu accelleration--so any suggestions as to the best way to take advange of that would be appreciated too (say pycuda, theano, etc.) 我认为让gpu加速是有用的 - 所以任何关于采取优势的最佳方式的建议也会受到赞赏(比如pycuda,theano等)

The most efficient storage method for symmetric sparse matrices is probably sparse skyline format (this is what Intel MKL uses, for example). 对称稀疏矩阵的最有效存储方法可能是稀疏天际线格式 (例如,这是英特尔MKL使用的)。 AFAIK scipy.sparse doesn't contain a sparse, symmetric matrix format. AFAIK scipy.sparse不包含稀疏的对称矩阵格式。 Pysparse , however, does . 然而, Pysparse 确实如此 Using Pysparse, you can build the matrix incrementally using the link list format, then convert the matrix into sparse skyline format. 使用Pysparse,您可以使用链接列表格式逐步构建矩阵,然后将矩阵转换为稀疏天际线格式。 Performance wise, I have usually found Pysparse to be superior to scipy with large sparse systems, and all the basic building blocks (matrix product, eigenvalue solver, direct solver, iterative solver) as present, although the range of routines is perhaps a little smaller than what is available in scipy. 性能方面,我通常发现Pysparse优于scipy大型稀疏系统,并且所有基本构建块(矩阵乘积,特征值求解器,直接求解器,迭代求解器)都存在,尽管例程的范围可能稍微小一点比scipy中的可用。

You can use a dictionary and tuples to access the data: 您可以使用字典和元组来访问数据:

>>> size = (4,4)
>>> mat = {}
>>> mat[0,1] = 3
>>> mat[2,3] = 5
>>> for i in range(size[0]):
        for j in range(size[1]):
            print mat.get((i,j), 0) ,
        print

0 3 0 0
0 0 0 0
0 0 0 5
0 0 0 0

Of course you should make a class for that and add the methods you need: 当然你应该为它做一个类并添加你需要的方法:

class Sparse(dict):
    pass

BTW, You can also use scipy.sparse from the scipy lib 顺便说一句,您还可以使用scipy.sparseSciPy的 LIB

使用scipy.sparse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM