A+B and its sparsity

When we add matrix B to A (PETSc Mat), is there a method to drop the non-zero elements in B which do not exist in A?

For example, if
A=
[ 1, 0
0, 2]

B=
[1, -1
0, 3]

Can we neglect -1 in B, so that B would have the same sparsity as A?

1 Like

No, you need to do that yourself.

do you have an idea? can I access every element of the matrix?

You can read the documentation.