gpt4 book ai didi

python - 使用索引和值将值添加到 Scipy 稀疏矩阵

转载 作者:太空狗 更新时间:2023-10-29 21:53:14 28 4
gpt4 key购买 nike

我正在研究电力系统分析程序,我需要使用稀疏矩阵。

有一个例程,我只使用以下调用填充稀疏矩阵:

self.A = bsr_matrix((val, (row,col)), shape=(nele, nbus), dtype=complex)

因为这个矩阵不会随时间改变。另一个矩阵确实会随着时间的推移而变化,我需要更新它。有没有办法,例如:

co     = [ 2, 3, 6]
row = [ 5, 5, 5]
val = [ 0.1 + 0.1j, 0.1 - 0.2j, 0.1 - 0.4j]

我可以将它们添加到先前初始化的稀疏矩阵吗?怎样才是更 Pythonic 的方式来做到这一点?

谢谢

最佳答案

您应该改用coo_matrix,您可以在其中更改属性colrowdata先前创建的稀疏矩阵:

from scipy.sparse import coo_matrix
nele=30
nbus=40
col = [ 2, 3, 6]
row = [ 5, 5, 5]
val = [ 0.1 + 0.1j, 0.1 - 0.2j, 0.1 - 0.4j]
test = coo_matrix((val, (row,col)), shape=(nele, nbus), dtype=complex)

print test.col
#[2 3 6]
print test.row
#[5 5 5]
print test.data
#[ 0.1+0.1j 0.1-0.2j 0.1-0.4j]

关于python - 使用索引和值将值添加到 Scipy 稀疏矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17713119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com