Getting Numpy array out of PETSc Mat

Issue #26 resolved
jefflarson created an issue

I am trying to get a numpy array out of a PETSc.Mat. For PETSc.Vec objects, b.array does the trick.

But if c is a PETSc.Mat object, getDenseArray() gives the following error

>>> c.getDenseArray()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Mat.pyx", line 1287, in petsc4py.PETSc.Mat.getDenseArray (src/petsc4py.PETSc.c:122734)
petsc4py.PETSc.Error: error code 56
[0] MatDenseGetArray() line 1499 in /fusion/gpfs/home/jlarson/software/petsc-3.5.3/src/mat/impls/dense/seq/dense.c
[0] No support for this operation for this object type
[0] Cannot locate function MatDenseGetArray_C in object

Is there a way to get the full matrix out?

Comments (4)

  1. Lisandro Dalcin

    What matrix type are you using? Mat.getDenseArray() only works for the MATDENSE matrix type. You have to either convert your matrix to dense format (using c_dense = c.convert("dense")), of in case of a MATAIJ matrix, use ai, aj, av = c.getValuesCSR() to get three arrays in CSR format.

  2. Log in to comment