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

convert vectorspacebasis to matrix

+1 vote

How can I convert a VectorSpaceBasis of dimension $M$ with vectors of size $N$ to a matrix of size $M\times N$.

asked Feb 16, 2014 by monien FEniCS Novice (790 points)

1 Answer

+2 votes

VectorSpaceBasis is just a container for GenericVectors. I'd recommend using petsc4py to manipulate these and create matrix; refer to PETSc/petsc4py documentation, please.

# Get petsc4py.PETSc.Vec objects
M = vector_space_basis.dim()
vecs = [vector_space_basis._sub(i).vec() for i in range(M)]

# Do some petsc4py manipulation with vecs and obtain PETSc.Mat object
from petsc4py import PETSc
...
mat = PETSc.Mat()
...

# Wrap PETSc.Mat as PETScMatrix
basis_matrix = PETScMatrix(mat)
answered Aug 6, 2014 by Jan Blechta FEniCS Expert (51,420 points)
...