How to efficiently create N sparse matrices in python without a for loop - and how to create a even bigger sparce matrix out of them?

问题描述 投票:0回答:0

这个问题可能很幼稚-我尝试在网上找到答案但没有成功。

本质上,我想创建一个稀疏块矩阵,其中各个块是稀疏的,大小为 MxM。整个矩阵的大小应为 MNxMN。 主对角线上的 N 个块都彼此相似,但取决于它们自己的“块索引”,即 A(j) 其中 j <= N.

The big sparse matrix, where A_j, B, C are sparse MxM matrices themselves

我正在使用 scipy.sparse 包 (

diags
) 在另一条对角线上创建块 B 和 C,但我不知道如何有效地(并且具有良好的编码风格)创建 A_js。

我的下一个问题可能是我不知道如何从这些块中创建大矩阵——我查看了

scipy.sparse.bmat
,但我不知道这对于任意数量的 N 是如何工作的。

所以我想我的问题是:

  1. 如何有效地创建和存储 A_js?我如何将它们放在一个大的稀疏矩阵中?
  2. 或者,有没有办法直接创建大矩阵?

我一开始就失败了,因为我无法使用 for 循环存储 N 个稀疏对角矩阵,例如显然以下是行不通的:

A = np.zeros(5)
for j in range(0,5):
    A[j] = j*scipy.sparse.diag(np.ones(3),shape(3,3))
scipy storage sparse-matrix
© www.soinside.com 2019 - 2024. All rights reserved.