scipy 稀疏矩阵作为 petsc4py 的输入 [英] scipy sparse matrices as an input for petsc4py

查看:34
本文介绍了scipy 稀疏矩阵作为 petsc4py 的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到如何有效加载 scipy 稀疏矩阵的方法,例如csr_matrix,进入 petsc4py 矩阵,例如PETSc.Mat().createAIJ.我找到了这个话题,但我无法应用它.

我也很感激这个东西被实际记录的指针.demo 目录下的例子只解释了一部分,我看不到任何文档字符串.

解决方案

你的链接说要在 PETSc 中创建一个稀疏矩阵,你应该使用这样的命令:

PETSc.Mat().createAIJ(size=(nrows,ncols), csr=(ai,aj,aa))

根据这个aiajaa 在 PETSc 中是:

<代码>>i - 行索引>j - 列索引>a - 矩阵值

这些分别等同于 scypy.sparse 的 .indptr.indices.data 属性.csr_matrix,请参阅文档详情.

因此,如果您的链接是正确的,则以下内容应该有效:

<预><代码>>>>从 petsc4py 导入 PETSc>>>导入 scipy.sparse>>>csr_mat = scipy.sparse.rand(1000, 1000, 密度=0.001, format='csr')>>>petsc_mat = PETSc.Mat().createAIJ(size=csr_mat.shape,... csr=(csr_mat.indptr, csr_mat.indices,... csr_mat.data))

很遗憾,我无法自己测试.

I can't seem to find a way how to efficiently load scipy sparse matrices, e.g. csr_matrix, into a petsc4py matrix, e.g. PETSc.Mat().createAIJ. I found this thread, but I'm not able to apply it.

I would also appreciate a pointer where this stuff is actually documented. The examples in the demo directory only explain a part, and I can't see any docstrings.

解决方案

Your link says that to create a sparse matrix in PETSc, you should use a command like this:

PETSc.Mat().createAIJ(size=(nrows,ncols), csr=(ai,aj,aa))

According to this, the ai, aj and aa are, in PETSc-speak:

> i - row indices
> j - column indices
> a - matrix values

These are equivalent, respectively, to the .indptr, .indices and .data attributes of a scypy.sparse.csr_matrix, see the docs for details.

So, if your link is right, the following should work:

>>> from petsc4py import PETSc
>>> import scipy.sparse
>>> csr_mat = scipy.sparse.rand(1000, 1000, density=0.001, format='csr')
>>> petsc_mat = PETSc.Mat().createAIJ(size=csr_mat.shape,
...                                   csr=(csr_mat.indptr, csr_mat.indices,
...                                        csr_mat.data))

Unfortunately, I cannot test it myself.

这篇关于scipy 稀疏矩阵作为 petsc4py 的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆