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.
petsc4py
on my system, but the instructions in your link seem to me pretty straightforward to follow. – Buckskin