[petsc-users] remove new zeros values with petsc4py

Pierre Jolivet pierre at joliv.et
Wed Apr 16 08:58:00 CDT 2025


Dear Benoit,

> On 16 Apr 2025, at 2:36 PM, NENNIG Benoit <benoit.nennig at isae-supmeca.fr> wrote:
> 
> Dear petsc users,
> 
> I need to compute the difference of two matrices M(nu) and M(nu+epsilon) to estimate the derivative of the matrix with respect to the parameter nu.
> Since only very few entries are modified by epsilon, D = M(nu+epsilon) - M(nu) contains many new zeros and I would like to remove them.
> 
> I am using PETSc from petsc4py.
> I have seen the method `chop` which call `Filter` but without the `compress` argument.

At the C level, [Mat,Vec]Chop() has been replaced by [Mat,Vec]Filter().
This didn’t make it to the Python level (yet), but if you feel like it, you could modify the chop functions in Mat.pyx and Vec.pyx and rename them (to filter) plus add an optional parameter for the `compress` argument.
If you submit a MR, I don’t see why it would not get approved, deprecation is difficult to handle in petsc4py (and in PETSc in general), and sometimes there can be small discrepancies between the Python and the C APIs.

Thanks,
Pierre

> At the end I need to store this matrix into a file. So my questions are:
> - Is is possible to remove the zeros entries with petsc4py with Mat methods?
> - Is is possible to do it when I save the file, ie with Viewer methods ?
> 
> Thanks a lot,
> 
> Benoit



More information about the petsc-users mailing list