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

How to perform a matrix multiply

+3 votes

I would like to construct something similar to a K M_L^{_1} K matrix. Is there a way of multiplying two spaces matrices to generate a third sparse matrix? Or is there some alternative method I should consider?

For background: I am using this in an eigenvalue solver. Solving in an outer product space is possible but inefficient.

asked Apr 1, 2014 by James R. Maddison FEniCS User (2,230 points)

2 Answers

+4 votes
 
Best answer

There are tools to do this in fenicstools as well. Otherwise, it's quite straightforward to do it yourselves as Kent-Andrè suggests by, e.g.,

from dolfin import *
mesh = UnitSquareMesh(5, 5)
V = FunctionSpace(mesh, 'CG', 1)
A = assemble(TrialFunction(V)*TestFunction(V)*dx)
B = A.copy()
B._scale(2.)
am = as_backend_type(A).mat()
bm = as_backend_type(B).mat()
cm = am.matMult(bm)

# Now get it back into a dolfin matrix
C = Matrix(PETScMatrix(cm))
print C.getrow(0)

This works with development version, not quite sure about older versions.

answered Apr 1, 2014 by mikael-mortensen FEniCS Expert (29,340 points)
selected Apr 1, 2014 by James R. Maddison
+1 vote

There are tools that enable this in cbc.block. Furthermore,
the backends PETSC and Epetra have this functionality, but
you must cast the matrices to their underlying types and look up the
specific functions in the respective backends.

answered Apr 1, 2014 by Kent-Andre Mardal FEniCS Expert (14,380 points)
...