小编Thu*_*ggi的帖子

在python中创建稀疏循环矩阵

我想在 Python 中创建一个大的(比如 10^5 x 10^5)稀疏循环矩阵。它在位置[i,i+1], [i,i+2], [i,i+N-2], [i,i+N-1]处每行有 4 个元素,我假设索引的周期性边界条件(即[10^5,10^5]=[0,0], [10^5+1,10^5+1]=[1,1]等等)。我查看了 scipy 稀疏矩阵文档,但我很困惑(我是 Python 新手)。

我可以用 numpy 创建矩阵

import numpy as np

def Bc(i, boundary):
    """(int, int) -> int

    Checks boundary conditions on index
    """
    if i > boundary - 1:
        return i - boundary
    elif i < 0:
        return boundary + i
    else:
        return i

N = 100
diffMat = np.zeros([N, N])
for i in np.arange(0, N, 1):
    diffMat[i, [Bc(i+1, N), Bc(i+2, N), Bc(i+2+(N-5)+1, …
Run Code Online (Sandbox Code Playgroud)

python numpy matrix scipy sparse-matrix

5
推荐指数
1
解决办法
1970
查看次数

标签 统计

matrix ×1

numpy ×1

python ×1

scipy ×1

sparse-matrix ×1