Scipy sparse matrices as input for petsc4py

I cannot find a way to efficiently load scipy sparse matrices, for example. csr_matrixinto the petsc4py matrix, for example. PETSc.Mat().createAIJ. I found this thread , but I cannot apply it.

I would also appreciate a pointer to where this material is really documented. The examples in the directory demoexplain only part, and I do not see any docstrings.

+5
source share
1 answer

Your link says that to create a sparse matrix in PETSc you should use the following command:

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

According to the this , in PETSc say ai, ajand aa:

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

.indptr, .indices .data a scypy.sparse.csr_matrix, . docs .

, , :

>>> 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))

, .

+6

All Articles