稀疏矩阵模式上的Scipy边界条件

2024-05-08 11:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的系统最好用对角稀疏矩阵(Poisson)来描述。我有我的对角线稀疏矩阵,但是,我想改变边界条件(即我的矩阵的“边”)为零。这一定是一种常见的情况,建模者想用稀疏对角矩阵来描述一个具有不同边界条件的系统,有没有一个最佳实践来做到这一点?在

[[0,0,0,0,..0],
 [0,2,1,0,..0],
 [0,1,2,1,..0],
 ...
 [0,0,0,0,..0]]

Tags: 系统情况矩阵建模poisson对角对角线边界条件
1条回答
网友
1楼 · 发布于 2024-05-08 11:28:07

这取决于您使用哪种稀疏矩阵格式。显然^{} and ^{}可以使用片分配。

To construct a matrix efficiently, use either lil_matrix (recommended) or dok_matrix. The lil_matrix class supports basic slicing and fancy indexing with a similar syntax to NumPy arrays.

这使得这一点相当简单:

In : x = scipy.sparse.lil_matrix(np.ones((6,6)))

In : x.todense()
Out:
matrix([[ 1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.]])

In : x[:, 0] = 0

In : x[:, -1] = 0

In : x[0, :] = 0

In : x[-1, :] = 0

In : x.todense()
Out:
matrix([[ 0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  1.,  1.,  1.,  1.,  0.],
        [ 0.,  1.,  1.,  1.,  1.,  0.],
        [ 0.,  1.,  1.,  1.,  1.,  0.],
        [ 0.,  1.,  1.,  1.,  1.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.]])

{你的对角线矩阵}不是^矩阵^。

相关问题 更多 >

    热门问题