Is there any function to create or change a matrix, to have $A_{ij} = \text{abs}(A_{ij})$ in PETSc? If possible it should work with MPIAIJ matrices, not only local.


1 Answer 1


Though this question is about a particular functionality of an individual library, I stumbled upon the same problem some time ago.

As far as I know, there is no ready matrix function in PETSc (3.8.3) that will do it for you right out-of-the-box. I was surprised not to find a solution back a couple of years ago and now, considering the existence of such functions as:

Considering that element-wise Re, Im, and conjugation operations on matrices are supported, I would assume that all the necessary building blocks for the MatAbs are there, but not formally coded up.

From MatRealPart subroutine source code, the following excerpt is doing the job


The list of supported and propagated operations on matrices is stored in include/petscmat.h in MatOperation typedef. Technically, you can code up your own operation and propagate it to the datatypes you require (maybe, even by using custom user matrix MatOperation and MatShellSetOperation). However, the question is if such a development is worth the required time and pain.


Not the answer you're looking for? Browse other questions tagged or ask your own question.