This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to get the trace of a ufl mass matrix form

0 votes

Hi,

The question is, given a ufl form with a mass matrix of a problem:

m = inner(u, v)*dx

how can i calculate the trace (first invariant) of it?. I already try with:

Aij = v[i]*v[j]
A = as_tensor(Aij, (i,j))
t = tr(A)

but t doesn't contain explicitly the value of the trace of A, and i need to see it.

Thanks in advice!

asked Feb 12, 2016 by felipe_galarce FEniCS User (1,190 points)

1 Answer

+2 votes
 
Best answer

Hi, if PETSc is your linear algebra backend consider:

m = assemble(inner(u, v)*dx)
diag = as_backend_type(m).mat().getDiagonal().array
trace = diag.sum()
print trace

regards!

answered Feb 12, 2016 by hernan_mella FEniCS Expert (19,460 points)
selected Feb 12, 2016 by felipe_galarce
...